Skip to content

Commit

Permalink
Add InitSubscribeToChatList to chat service
Browse files Browse the repository at this point in the history
  • Loading branch information
miladsoft committed Sep 25, 2024
1 parent a484675 commit 8b36c71
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
17 changes: 12 additions & 5 deletions src/app/components/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,12 @@ export class ChatService implements OnDestroy {


async getChats(): Promise<Observable<Chat[]>> {
const pubkey = this._signerService.getPublicKey();
const useExtension = await this._signerService.isUsingExtension();
this.decryptedPrivateKey = await this._signerService.getDecryptedSecretKey();

const storedChats = await this._indexedDBService.getAllChats();
if (storedChats && storedChats.length > 0) {
this.chatList = storedChats;
this._chats.next(this.chatList);
}

setTimeout(async () => {
try {
if (storedChats && storedChats.length > 0) {
Expand All @@ -190,10 +186,21 @@ export class ChatService implements OnDestroy {
console.error('Error updating chat contacts metadata:', error);
}
}, 0);
this.subscribeToChatList(pubkey, useExtension, this.decryptedPrivateKey);
return this.getChatListStream();
}


async InitSubscribeToChatList(): Promise<Observable<Chat[]>>
{
const pubkey = this._signerService.getPublicKey();
const useExtension = await this._signerService.isUsingExtension();
//this._signerService.getDecryptedSecretKey() = open password dialog or extension window
this.decryptedPrivateKey = await this._signerService.getDecryptedSecretKey();


return this.subscribeToChatList(pubkey, useExtension, this.decryptedPrivateKey);
}

subscribeToChatList(pubkey: string, useExtension: boolean, decryptedSenderPrivateKey: string): Observable<Chat[]> {
this._relayService.ensureConnectedRelays().then(async () => {
const lastSavedTimestamp = await this._indexedDBService.getLastSavedTimestamp();
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/chat/chats/chats.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class ChatsComponent implements OnInit, OnDestroy {
this._changeDetectorRef.markForCheck();
});


this._chatService.InitSubscribeToChatList();
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/app/services/signer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ export class SignerService {
return await this.securityService.decryptData(encryptedSecretKey, password);
}

async getDecryptedSecretKey(): Promise<string | null> {
async getDecryptedSecretKey(): Promise<string | null> {
try {
const storedPassword = this.getPassword();
const storedPassword = this.getPassword(); // Ensure this retrieves a valid password
if (storedPassword) {
return await this.getSecretKey(storedPassword);
return await this.getSecretKey(storedPassword); // Ensure getSecretKey returns a valid private key
}

const result = await this.requestPassword();
const result = await this.requestPassword(); // Prompt user for password if not stored
if (result?.password) {
const decryptedPrivateKey = await this.getSecretKey(result.password);
const decryptedPrivateKey = await this.getSecretKey(result.password); // Check that the private key is decrypted properly
if (result.duration !== 0) {
this.savePassword(result.password, result.duration);
}
Expand All @@ -166,7 +166,6 @@ export class SignerService {

console.error('Password not provided');
return null;

} catch (error) {
console.error('Error decrypting private key:', error);
return null;
Expand Down Expand Up @@ -341,16 +340,20 @@ export class SignerService {
// NIP-04: Decrypting Direct Messages
async decryptMessage(privateKey: string, senderPublicKey: string, encryptedMessage: string): Promise<string> {
try {
if (!privateKey) {
throw new Error('Private key is missing or undefined.');
}
const decryptedMessage = await nip04.decrypt(privateKey, senderPublicKey, encryptedMessage);
return decryptedMessage;
} catch (error) {
console.error('Error decrypting message:', error);
throw error;
throw error; // Re-throw or handle the error
}
}




getUnsignedEvent(kind: number, tags: string[][], content: string) {
const eventUnsigned: UnsignedEvent = {
kind: kind,
Expand Down

0 comments on commit 8b36c71

Please sign in to comment.