diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 88275d7663..a3dbb16a02 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -2797,12 +2797,11 @@ export class ConversationModel extends window.Backbone return; } - if (this.get('pendingUniversalTimer') || this.get('expireTimer')) { + if (await window.Signal.Data.hasUserInitiatedMessages(this.get('id'))) { return; } - const activeAt = this.get('active_at'); - if (activeAt) { + if (this.get('pendingUniversalTimer') || this.get('expireTimer')) { return; } @@ -2824,6 +2823,9 @@ export class ConversationModel extends window.Backbone const message = window.MessageController.getById(notificationId); if (message) { message.cleanup(); + window.Signal.Data.removeMessage(message.id, { + Message: window.Whisper.Message, + }); } if (this.get('expireTimer')) { diff --git a/ts/sql/Client.ts b/ts/sql/Client.ts index efcb5b3187..668376799e 100644 --- a/ts/sql/Client.ts +++ b/ts/sql/Client.ts @@ -171,6 +171,7 @@ const dataInterface: ClientInterface = { searchMessagesInConversation, getMessageCount, + hasUserInitiatedMessages, saveMessage, saveMessages, removeMessage, @@ -976,6 +977,10 @@ async function getMessageCount(conversationId?: string) { return channels.getMessageCount(conversationId); } +async function hasUserInitiatedMessages(conversationId: string) { + return channels.hasUserInitiatedMessages(conversationId); +} + async function saveMessage( data: MessageType, { forceSave, Message }: { forceSave?: boolean; Message: typeof MessageModel } diff --git a/ts/sql/Interface.ts b/ts/sql/Interface.ts index 1b833d2641..a0e2233a5b 100644 --- a/ts/sql/Interface.ts +++ b/ts/sql/Interface.ts @@ -215,6 +215,7 @@ export type DataInterface = { ) => Promise>; getMessageCount: (conversationId?: string) => Promise; + hasUserInitiatedMessages: (conversationId: string) => Promise; saveMessages: ( arrayOfMessages: Array, options: { forceSave?: boolean } diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index ce4e782940..44f32c9d2f 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -161,6 +161,7 @@ const dataInterface: ServerInterface = { searchMessagesInConversation, getMessageCount, + hasUserInitiatedMessages, saveMessage, saveMessages, removeMessage, @@ -2925,6 +2926,43 @@ async function getMessageCount(conversationId?: string): Promise { return row['count(*)']; } +// Called only for private conversations +async function hasUserInitiatedMessages( + conversationId: string +): Promise { + const db = getInstance(); + + // We apply the limit in the sub-query so that `json_extract` wouldn't run + // for additional messages. + const row: { count: number } = db + .prepare( + ` + SELECT COUNT(*) as count FROM + ( + SELECT 1 FROM messages + WHERE + conversationId = $conversationId AND + (type IS NULL + OR + type NOT IN ( + 'profile-change', + 'verified-change', + 'message-history-unsynced', + 'keychange', + 'group-v1-migration', + 'universal-timer-notification' + ) + ) AND + json_extract(json, '$.expirationTimerUpdate') IS NULL + LIMIT 1 + ); + ` + ) + .get({ conversationId }); + + return row.count !== 0; +} + function saveMessageSync( data: MessageType, options: { forceSave?: boolean; alreadyInTransaction?: boolean } = {}