diff --git a/UI/app.tsx b/UI/app.tsx index c5229b4..66d9b52 100644 --- a/UI/app.tsx +++ b/UI/app.tsx @@ -163,7 +163,7 @@ export class App { // init conversation list const all_events = Array.from(args.database.getAllEvents()); const conversationLists = new DM_List(args.ctx); - const err = conversationLists.addEvents(all_events); + const err = conversationLists.addEvents(all_events, false); if (err instanceof InvalidEvent) { console.error(err); await args.database.remove(err.event.id); diff --git a/UI/app_update.tsx b/UI/app_update.tsx index 06df142..dc8e058 100644 --- a/UI/app_update.tsx +++ b/UI/app_update.tsx @@ -165,6 +165,7 @@ export async function* UI_Interaction_Update(args: { } app.popOverInputChan.put({ children: undefined }); app.model.dm.isGroupMessage = event.isGroupChat; + app.conversationLists.markRead(event.pubkey.hex, event.isGroupChat); } else if (event.type == "BackToContactList") { model.dm.currentEditor = undefined; } else if (event.type == "PinConversation") { @@ -482,7 +483,7 @@ export async function* Database_Update( } profileSyncer.add(...changes_events.map((e) => e.pubkey)); - convoLists.addEvents(changes_events); + convoLists.addEvents(changes_events, true); for (let e of changes_events) { const t = getTags(e).lamport_timestamp; if (t) { diff --git a/UI/conversation-list.test.tsx b/UI/conversation-list.test.tsx index 78815e8..d1e91be 100644 --- a/UI/conversation-list.test.tsx +++ b/UI/conversation-list.test.tsx @@ -38,7 +38,7 @@ for (let i = 0; i < 20; i++) { ["p", PrivateKey.Generate().toPublicKey().hex], ], }) as NostrEvent; - const err = dm_list.addEvents([event]); + const err = dm_list.addEvents([event], false); if (err instanceof Error) { fail(err.message); } @@ -49,7 +49,7 @@ const otherConfig = OtherConfig.Empty(new Channel(), ctx, new LamportTime()); const view = () => render( , newEvent: boolean) { + let whoAm_I_TalkingTo = ""; + if (event.pubkey == this.ctx.publicKey.hex) { + // I am the sender + whoAm_I_TalkingTo = getTags(event).p[0]; + if (whoAm_I_TalkingTo == undefined) { + return new InvalidEvent(event, `event ${event.id} does not have p tags`); + } + } else if (getTags(event).p[0] == this.ctx.publicKey.hex) { + // I am the receiver + whoAm_I_TalkingTo = event.pubkey; + } else { + // I am neither. Possible because other user has used this device before + return; + } - const userInfo = this.convoSummaries.get(whoAm_I_TalkingTo); - if (userInfo) { - if (whoAm_I_TalkingTo == this.ctx.publicKey.hex) { - // talking to myself - if (userInfo.newestEventSendByMe) { - if (event.created_at > userInfo.newestEventSendByMe?.created_at) { - userInfo.newestEventSendByMe = event; - userInfo.newestEventReceivedByMe = event; - } - } else { + if (newEvent && this.ctx.publicKey.hex != event.pubkey) { + this.newMessages.set(whoAm_I_TalkingTo, this.newNessageCount(whoAm_I_TalkingTo, false) + 1); + } + + const userInfo = this.convoSummaries.get(whoAm_I_TalkingTo); + if (userInfo) { + if (whoAm_I_TalkingTo == this.ctx.publicKey.hex) { + // talking to myself + if (userInfo.newestEventSendByMe) { + if (event.created_at > userInfo.newestEventSendByMe?.created_at) { userInfo.newestEventSendByMe = event; userInfo.newestEventReceivedByMe = event; } } else { - if (this.ctx.publicKey.hex == event.pubkey) { - // I am the sender - if (userInfo.newestEventSendByMe) { - if (event.created_at > userInfo.newestEventSendByMe.created_at) { - userInfo.newestEventSendByMe = event; - } - } else { + userInfo.newestEventSendByMe = event; + userInfo.newestEventReceivedByMe = event; + } + } else { + if (this.ctx.publicKey.hex == event.pubkey) { + // I am the sender + if (userInfo.newestEventSendByMe) { + if (event.created_at > userInfo.newestEventSendByMe.created_at) { userInfo.newestEventSendByMe = event; } } else { - // I am the receiver - if (userInfo.newestEventReceivedByMe) { - if (event.created_at > userInfo.newestEventReceivedByMe.created_at) { - userInfo.newestEventReceivedByMe = event; - } - } else { + userInfo.newestEventSendByMe = event; + } + } else { + // I am the receiver + if (userInfo.newestEventReceivedByMe) { + if (event.created_at > userInfo.newestEventReceivedByMe.created_at) { userInfo.newestEventReceivedByMe = event; } - } - } - } else { - const pubkey = PublicKey.FromHex(whoAm_I_TalkingTo); - if (pubkey instanceof Error) { - return new InvalidEvent(event, pubkey.message); - } - const newUserInfo: ConversationSummary = { - pubkey, - newestEventReceivedByMe: undefined, - newestEventSendByMe: undefined, - }; - if (whoAm_I_TalkingTo == this.ctx.publicKey.hex) { - // talking to myself - newUserInfo.newestEventSendByMe = event; - newUserInfo.newestEventReceivedByMe = event; - } else { - if (this.ctx.publicKey.hex == event.pubkey) { - // I am the sender - newUserInfo.newestEventSendByMe = event; } else { - // I am the receiver - newUserInfo.newestEventReceivedByMe = event; + userInfo.newestEventReceivedByMe = event; } } - this.convoSummaries.set(whoAm_I_TalkingTo, newUserInfo); } + } else { + const pubkey = PublicKey.FromHex(whoAm_I_TalkingTo); + if (pubkey instanceof Error) { + return new InvalidEvent(event, pubkey.message); + } + const newUserInfo: ConversationSummary = { + pubkey, + newestEventReceivedByMe: undefined, + newestEventSendByMe: undefined, + }; + if (whoAm_I_TalkingTo == this.ctx.publicKey.hex) { + // talking to myself + newUserInfo.newestEventSendByMe = event; + newUserInfo.newestEventReceivedByMe = event; + } else { + if (this.ctx.publicKey.hex == event.pubkey) { + // I am the sender + newUserInfo.newestEventSendByMe = event; + } else { + // I am the receiver + newUserInfo.newestEventReceivedByMe = event; + } + } + this.convoSummaries.set(whoAm_I_TalkingTo, newUserInfo); } } } diff --git a/UI/conversation-list.tsx b/UI/conversation-list.tsx index 1709443..d299b8a 100644 --- a/UI/conversation-list.tsx +++ b/UI/conversation-list.tsx @@ -39,7 +39,8 @@ export type ContactUpdate = | StartCreateGroupChat; export interface NewMessageChecker { - has(hex: string, isGourpChat: boolean): number; + newNessageCount(hex: string, isGourpChat: boolean): number; + markRead(hex: string, isGourpChat: boolean): void; } type Props = { @@ -103,7 +104,10 @@ export class ConversationList extends Component { for (const conversationSummary of listToRender) { convoListToRender.push({ conversation: conversationSummary, - newMessageCount: props.hasNewMessages.has(conversationSummary.pubkey.hex, isGroupChat), + newMessageCount: props.hasNewMessages.newNessageCount( + conversationSummary.pubkey.hex, + isGroupChat, + ), }); }