diff --git a/src/app/components/chat/chat.service.ts b/src/app/components/chat/chat.service.ts index 0c506f5..fff5b46 100644 --- a/src/app/components/chat/chat.service.ts +++ b/src/app/components/chat/chat.service.ts @@ -102,8 +102,7 @@ export class ChatService implements OnDestroy { if (!contact.pubKey) { console.error('Contact is missing pubKey:', contact); } - contact.name = contact.name ? contact.name : 'Unknown'; - return contact; + return contact; }); this._contacts.next(validatedContacts); @@ -196,6 +195,10 @@ export class ChatService implements OnDestroy { async getChats(): Promise> { return this.getChatListStream().pipe( tap(chats => { + if (chats && chats.length === 0) { + return; + } + const pubKeys = chats .filter(chat => chat.contact?.pubKey) .map(chat => chat.contact!.pubKey); @@ -251,8 +254,11 @@ export class ChatService implements OnDestroy { } }, oneose: () => { - console.log('Subscription closed'); - this._chats.next(this.chatList); + + const currentChats = this.chatList || []; + if (currentChats.length > 0) { + this._chats.next(this.chatList); + } } }); }); @@ -440,42 +446,26 @@ export class ChatService implements OnDestroy { ); } - getChatById(id: string): Observable { + getChatById(id: string, contact: Contact = null): Observable { this.recipientPublicKey = id; - const pubkeyPromise = this._signerService.getPublicKey(); - - return from(Promise.all([pubkeyPromise])).pipe( + return from(Promise.all([this._signerService.getPublicKey()])).pipe( switchMap(() => { return this.chats$.pipe( take(1), - distinctUntilChanged(), - filter((chats: Chat[] | null) => !!chats), switchMap((chats: Chat[] | null) => { - const cachedChat = chats?.find(chat => chat.id === id); - if (cachedChat && cachedChat.messages.length > 0) { + if (!chats || chats.length === 0) { + return this.createNewChat(id, contact); + } + + const cachedChat = chats.find(chat => chat.id === id); + if (cachedChat) { this._chat.next(cachedChat); - this.loadChatHistory(this.recipientPublicKey); - return of(cachedChat); } - const newChat: Chat = { - id: this.recipientPublicKey, - contact: { pubKey: this.recipientPublicKey, picture: "/images/avatars/avatar-placeholder.png" }, - lastMessage: '', - lastMessageAt: new Date().toISOString(), - messages: [] - }; - - const updatedChats = chats ? [...chats, newChat] : [newChat]; - this._chats.next(updatedChats); - this._chat.next(newChat); - - this.loadChatHistory(this.recipientPublicKey); - - return of(newChat); + return this.createNewChat(id, contact); }) ); }), @@ -486,64 +476,30 @@ export class ChatService implements OnDestroy { ); } + createNewChat(id: string, contact: Contact = null): Observable { + const newChat: Chat = { + id: id, + contact: contact || { pubKey: id,name: "Unknown", picture: "/images/avatars/avatar-placeholder.png" }, + lastMessage: '...', + lastMessageAt: Math.floor(Date.now() / 1000).toString(), + messages: [] + }; + const updatedChats = this._chats.value ? [...this._chats.value, newChat] : [newChat]; + this._chats.next(updatedChats); + this._chat.next(newChat); - openChat(contact: Contact): Observable { - if (!contact.pubKey) { - console.error('The contact does not have a public key!'); - return throwError('The contact does not have a public key!'); - } + this.loadChatHistory(id); - this.recipientPublicKey = contact.pubKey; - - return this.chats$.pipe( - take(1), - distinctUntilChanged(), - concatMap((chats: Chat[] | null) => { - const cachedChat = chats?.find(chat => chat.id === contact.pubKey); - if (cachedChat) { - this._chat.next(cachedChat); - - this.loadChatHistory(contact.pubKey); - - return of(cachedChat); - } - - const newChat: Chat = { - id: contact.pubKey, - contact: { - pubKey: contact.pubKey, - name: contact.name, - picture: contact.picture || "/images/avatars/avatar-placeholder.png", - about: contact.about, - displayName: contact.displayName, - }, - lastMessage: '', - lastMessageAt: new Date().toISOString(), - messages: [] - }; - - const updatedChats = chats ? [...chats, newChat] : [newChat]; - this._chats.next(updatedChats); - this._chat.next(newChat); - - this.loadChatHistory(contact.pubKey); - - return of(newChat); - }), - catchError((error) => { - console.error('Error fetching chat by ID:', error); - const parentUrl = this.router.url.split('/').slice(0, -1).join('/'); - this.router.navigateByUrl(parentUrl); - return throwError(error); - }) - ); + return of(newChat); } + resetChat(): void { this._chat.next(null); } + public async sendPrivateMessage(message: string): Promise { try { this.message = message; diff --git a/src/app/components/chat/chats/chats.component.html b/src/app/components/chat/chats/chats.component.html index af8e298..106f11c 100644 --- a/src/app/components/chat/chats/chats.component.html +++ b/src/app/components/chat/chats/chats.component.html @@ -123,7 +123,7 @@ } Contact picture @@ -136,7 +136,7 @@
- {{ chat.contact.name }} + {{ chat.contact?.name }}
- @if (chat.contact.picture) { + @if (chat.contact?.picture) { } - @if (!chat.contact.picture) { + @if (!chat.contact?.picture) {
- {{ chat.contact.name.charAt(0) }} + {{ chat.contact?.name.charAt(0) }}
}
-
{{ chat.contact.name }}
+
{{ chat.contact?.name }}
- {{ chat.contact.about }} + {{ chat.contact?.about }}
diff --git a/src/app/components/chat/conversation/conversation.component.html b/src/app/components/chat/conversation/conversation.component.html index 49aae49..6992881 100644 --- a/src/app/components/chat/conversation/conversation.component.html +++ b/src/app/components/chat/conversation/conversation.component.html @@ -45,26 +45,26 @@
- @if (chat.contact.picture) { + @if (chat.contact?.picture) { Contact picture } - @if (!chat.contact.picture) { + @if (!chat.contact?.picture) {
- {{ chat.contact.name.charAt(0) }} + {{ chat.contact?.name.charAt(0) }}
}
- {{ chat.contact.name }} + {{ chat.contact?.name }}
diff --git a/src/app/components/chat/new-chat/new-chat.component.ts b/src/app/components/chat/new-chat/new-chat.component.ts index 435bb27..fd779d9 100644 --- a/src/app/components/chat/new-chat/new-chat.component.ts +++ b/src/app/components/chat/new-chat/new-chat.component.ts @@ -55,7 +55,7 @@ export class NewChatComponent implements OnInit, OnDestroy { } openChat(contact: Contact): void { - this._chatService.openChat(contact).subscribe({ + this._chatService.getChatById(contact.pubKey, contact).subscribe({ next: (chat) => { console.log('Chat loaded or created:', chat); this.router.navigate(['/chat', contact.pubKey]); diff --git a/src/app/components/explore/explore.component.html b/src/app/components/explore/explore.component.html index 1098fb7..71aeda9 100644 --- a/src/app/components/explore/explore.component.html +++ b/src/app/components/explore/explore.component.html @@ -66,12 +66,15 @@ {{ project.about || 'No description available' }} + @if (project.displayName ||project.name) {
+ } +
@@ -79,13 +82,12 @@ {{ project.totalInvestmentsCount || 0 }} investors
- - + Investor avatar {{ i + 1 }} + alt="Investor avatar {{ i + 1 }}" />
diff --git a/src/app/components/explore/explore.component.ts b/src/app/components/explore/explore.component.ts index 4c0720f..44dce55 100644 --- a/src/app/components/explore/explore.component.ts +++ b/src/app/components/explore/explore.component.ts @@ -23,7 +23,6 @@ import { Project } from 'app/interface/project.interface'; import { DomSanitizer, SafeUrl } from '@angular/platform-browser'; import { Contact } from '../chat/chat.types'; import { ChatService } from '../chat/chat.service'; -import { __metadata } from 'tslib'; @Component({ selector: 'explore', @@ -288,30 +287,27 @@ export class ExploreComponent implements OnInit, OnDestroy { return /\.(jpeg|jpg|gif|png|svg|bmp|webp|tiff|ico)$/i.test(url); } - async openChat(publicKey: string): Promise { - const metadata = await this.metadataService.getUserMetadata(publicKey); + try { + const metadata = await this.metadataService.fetchMetadataWithCache(publicKey); - if (metadata) { - const contact: Contact = { - pubKey: publicKey, - name: metadata.name, - username: metadata.username, - picture: metadata.picture, - about: metadata.about, - displayName: metadata.displayName, - website: metadata.website, - banner: metadata.banner, - lud06: metadata.lud06, - lud16: metadata.lud16, - nip05: metadata.nip05 - }; + if (metadata) { + const contact: Contact = { + pubKey: publicKey, + name: metadata.name || 'Unknown', + picture: metadata.picture || '/images/avatars/avatar-placeholder.png', + about: metadata.about || '', + displayName: metadata.displayName || metadata.name || 'Unknown', + }; - this._chatService.openChat(contact).subscribe((chat) => { - this.router.navigate(['/chat', contact.pubKey]); - }); - } else { - console.error('No metadata found for the public key:', publicKey); + this._chatService.getChatById(contact.pubKey, contact).subscribe((chat) => { + this.router.navigate(['/chat', contact.pubKey]); + }); + } else { + console.error('No metadata found for the public key:', publicKey); + } + } catch (error) { + console.error('Error opening chat:', error); } } diff --git a/src/app/layout/common/quick-chat/quick-chat.component.html b/src/app/layout/common/quick-chat/quick-chat.component.html index a14d4de..d4b3700 100644 --- a/src/app/layout/common/quick-chat/quick-chat.component.html +++ b/src/app/layout/common/quick-chat/quick-chat.component.html @@ -42,23 +42,23 @@
- @if (chat.contact.avatar) { + @if (chat.contact?.avatar) { Contact avatar } - @if (!chat.contact.avatar) { + @if (!chat.contact?.avatar) {
- {{ chat.contact.name.charAt(0) }} + {{ chat.contact?.name.charAt(0) }}
}
- {{ chat.contact.name }} + {{ chat.contact?.name }}