From 215d90fbe85014d233dcd4da091bd46444a17331 Mon Sep 17 00:00:00 2001 From: Josh Perez <60019601+josh-signal@users.noreply.github.com> Date: Mon, 17 May 2021 12:52:09 -0400 Subject: [PATCH] Always update unreadCount for conversations --- ts/models/conversations.ts | 18 +++++++----------- ts/sql/Server.ts | 20 +++++++++++++------- ts/util/markConversationRead.ts | 10 +++++++++- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 1c62927131dc..c129db314e1a 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -4053,21 +4053,17 @@ export class ConversationModel extends window.Backbone sendReadReceipts: true, } ): Promise { - const markedUnread = await markConversationRead( - this.attributes, - newestUnreadId, - options - ); - - if (!markedUnread) { - return; - } + await markConversationRead(this.attributes, newestUnreadId, options); const unreadCount = await window.Signal.Data.getUnreadCountForConversation( this.id ); - this.set({ unreadCount }); - window.Signal.Data.updateConversation(this.attributes); + + const prevUnreadCount = this.get('unreadCount'); + if (prevUnreadCount !== unreadCount) { + this.set({ unreadCount }); + window.Signal.Data.updateConversation(this.attributes); + } } // This is an expensive operation we use to populate the message request hero row. It diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index 01c7f9885356..e8dc9c689d4f 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -3215,7 +3215,8 @@ async function getUnreadCountForConversation( ` SELECT COUNT(*) AS unreadCount FROM messages WHERE unread = 1 AND - conversationId = $conversationId + conversationId = $conversationId AND + type = 'incoming'; ` ) .get({ @@ -3346,12 +3347,17 @@ async function getUnreadReactionsAndMarkRead( newestUnreadId, }); - db.exec(` + db.prepare( + ` UPDATE reactions SET unread = 0 WHERE - $conversationId = conversationId AND - $messageReceivedAt <= messageReceivedAt; - `); + conversationId = $conversationId AND + messageReceivedAt <= $newestUnreadId; + ` + ).run({ + conversationId, + newestUnreadId, + }); return unreadMessages; })(); @@ -3385,8 +3391,8 @@ async function markReactionAsRead( ` UPDATE reactions SET unread = 0 WHERE - $targetAuthorUuid = targetAuthorUuid AND - $targetTimestamp = targetTimestamp; + targetAuthorUuid = $targetAuthorUuid AND + targetTimestamp = $targetTimestamp; ` ).run({ targetAuthorUuid, diff --git a/ts/util/markConversationRead.ts b/ts/util/markConversationRead.ts index 73594371fee6..2ec38e44bb86 100644 --- a/ts/util/markConversationRead.ts +++ b/ts/util/markConversationRead.ts @@ -13,7 +13,6 @@ export async function markConversationRead( } ): Promise { const { id: conversationId } = conversationAttrs; - window.Whisper.Notifications.removeBy({ conversationId }); const [unreadMessages, unreadReactions] = await Promise.all([ window.Signal.Data.getUnreadByConversationAndMarkRead( @@ -27,10 +26,19 @@ export async function markConversationRead( ), ]); + window.log.info('markConversationRead', { + conversationId, + newestUnreadId, + unreadMessages: unreadMessages.length, + unreadReactions: unreadReactions.length, + }); + if (!unreadMessages.length && !unreadReactions.length) { return false; } + window.Whisper.Notifications.removeBy({ conversationId }); + const unreadReactionSyncData = new Map< string, {