From 1284386d22146345d1778b92bf104b49781bd4cb Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Wed, 24 Aug 2022 14:21:23 +0200 Subject: [PATCH] ref `ChatMessage` by id instead of object reference --- .../status-js/src/client/activityCenter.ts | 52 ++++++++++++++----- packages/status-js/src/client/chat.ts | 4 +- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/packages/status-js/src/client/activityCenter.ts b/packages/status-js/src/client/activityCenter.ts index 5302d4a3..811d8027 100644 --- a/packages/status-js/src/client/activityCenter.ts +++ b/packages/status-js/src/client/activityCenter.ts @@ -1,12 +1,16 @@ // todo?: rename to notifications (center?), inbox, or keep same as other platforms -import type { ChatMessage } from './chat' +// import type { ChatMessage } from './chat' import type { Client } from './client' // todo?: rename to Activity type Notification = { type: 'message' - value: ChatMessage + // value: ChatMessage + id: string + // todo?: resolve in ui too + chatUuid: string + referencedMessageId?: string isMention?: boolean isReply?: boolean } @@ -36,7 +40,7 @@ export class ActivityCenter { for (const notification of this.#notifications.values()) { if (notification.type === 'message') { - const chatUuid = notification.value.chatUuid + const chatUuid = notification.chatUuid const chat = unreadChats.get(chatUuid) let count = chat?.count ?? 0 @@ -50,17 +54,24 @@ export class ActivityCenter { } else { unreadChats.set(chatUuid, { count }) } - } - notifications.push(notification) + notifications.push(notification) + } } notifications.sort((a, b) => { - if (a.value.clock > b.value.clock) { + const ma = this.#client.community.getChat(a.chatUuid)?.getMessage(a.id) + const mb = this.#client.community.getChat(b.chatUuid)?.getMessage(b.id) + + if (!ma || !mb) { + return 0 + } + + if (ma.clock > mb.clock) { return -1 } - if (a.value.clock < b.value.clock) { + if (ma.clock < mb.clock) { return 1 } @@ -73,24 +84,37 @@ export class ActivityCenter { } // todo: pass ids instead of values and resolve within - public addMessageNotification = ( - newMessage: ChatMessage, - referencedMessage?: ChatMessage - ) => { + public addMessageNotification = (chatUuid: string, id: string) => { + const chat = this.#client.community.getChat(chatUuid) + + if (!chat) { + return + } + + const message = chat.getMessage(id) + + if (!message) { + return + } + + const referencedMessage = chat.getMessage(message.responseTo) + let isMention: boolean | undefined let isReply: boolean | undefined if (this.#client.account) { const publicKey = `0x${this.#client.account.publicKey}` - isMention = newMessage.text.includes(publicKey) + isMention = message.text.includes(publicKey) isReply = referencedMessage?.signer === publicKey } // todo?: getLatest on login this.#notifications.add({ type: 'message', - value: newMessage, + id, + chatUuid, + referencedMessageId: referencedMessage?.messageId, isMention, isReply, }) @@ -119,7 +143,7 @@ export class ActivityCenter { continue } - if (notification.value.chatUuid === chatUuid) { + if (notification.chatUuid === chatUuid) { this.#notifications.delete(notification) } } diff --git a/packages/status-js/src/client/chat.ts b/packages/status-js/src/client/chat.ts index 88779418..4f0f0a33 100644 --- a/packages/status-js/src/client/chat.ts +++ b/packages/status-js/src/client/chat.ts @@ -317,8 +317,8 @@ export class Chat { if (!this.#isActive) { this.client.activityCenter.addMessageNotification( - newMessage, - this.#messages.get(newMessage.responseTo) + newMessage.chatUuid, + newMessage.chatId ) } }