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
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)
}
}

View File

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