ref `ChatMessage` by id instead of object reference

This commit is contained in:
Felicio Mununga 2022-08-24 14:21:23 +02:00
parent dc2c75f18d
commit 1284386d22
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
2 changed files with 40 additions and 16 deletions

View File

@ -1,12 +1,16 @@
// todo?: rename to notifications (center?), inbox, or keep same as other platforms // 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' import type { Client } from './client'
// todo?: rename to Activity // todo?: rename to Activity
type Notification = { type Notification = {
type: 'message' type: 'message'
value: ChatMessage // value: ChatMessage
id: string
// todo?: resolve in ui too
chatUuid: string
referencedMessageId?: string
isMention?: boolean isMention?: boolean
isReply?: boolean isReply?: boolean
} }
@ -36,7 +40,7 @@ export class ActivityCenter {
for (const notification of this.#notifications.values()) { for (const notification of this.#notifications.values()) {
if (notification.type === 'message') { if (notification.type === 'message') {
const chatUuid = notification.value.chatUuid const chatUuid = notification.chatUuid
const chat = unreadChats.get(chatUuid) const chat = unreadChats.get(chatUuid)
let count = chat?.count ?? 0 let count = chat?.count ?? 0
@ -50,17 +54,24 @@ export class ActivityCenter {
} else { } else {
unreadChats.set(chatUuid, { count }) unreadChats.set(chatUuid, { count })
} }
}
notifications.push(notification) notifications.push(notification)
} }
}
notifications.sort((a, b) => { 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 return -1
} }
if (a.value.clock < b.value.clock) { if (ma.clock < mb.clock) {
return 1 return 1
} }
@ -73,24 +84,37 @@ export class ActivityCenter {
} }
// todo: pass ids instead of values and resolve within // todo: pass ids instead of values and resolve within
public addMessageNotification = ( public addMessageNotification = (chatUuid: string, id: string) => {
newMessage: ChatMessage, const chat = this.#client.community.getChat(chatUuid)
referencedMessage?: ChatMessage
) => { if (!chat) {
return
}
const message = chat.getMessage(id)
if (!message) {
return
}
const referencedMessage = chat.getMessage(message.responseTo)
let isMention: boolean | undefined let isMention: boolean | undefined
let isReply: boolean | undefined let isReply: boolean | undefined
if (this.#client.account) { if (this.#client.account) {
const publicKey = `0x${this.#client.account.publicKey}` const publicKey = `0x${this.#client.account.publicKey}`
isMention = newMessage.text.includes(publicKey) isMention = message.text.includes(publicKey)
isReply = referencedMessage?.signer === publicKey isReply = referencedMessage?.signer === publicKey
} }
// todo?: getLatest on login // todo?: getLatest on login
this.#notifications.add({ this.#notifications.add({
type: 'message', type: 'message',
value: newMessage, id,
chatUuid,
referencedMessageId: referencedMessage?.messageId,
isMention, isMention,
isReply, isReply,
}) })
@ -119,7 +143,7 @@ export class ActivityCenter {
continue continue
} }
if (notification.value.chatUuid === chatUuid) { if (notification.chatUuid === chatUuid) {
this.#notifications.delete(notification) this.#notifications.delete(notification)
} }
} }

View File

@ -317,8 +317,8 @@ export class Chat {
if (!this.#isActive) { if (!this.#isActive) {
this.client.activityCenter.addMessageNotification( this.client.activityCenter.addMessageNotification(
newMessage, newMessage.chatUuid,
this.#messages.get(newMessage.responseTo) newMessage.chatId
) )
} }
} }