From 7761d830550f287462108e8174f217b82b03386a Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Mon, 19 Jul 2021 13:45:18 -0700 Subject: [PATCH] Update timers whenever we mark messages read --- js/modules/debug.js | 4 +- js/modules/messages_data_migrator.js | 13 +---- ts/background.ts | 11 ++--- ts/groups.ts | 2 - ts/messageModifiers/AttachmentDownloads.ts | 4 +- ts/models/conversations.ts | 49 +++++-------------- ts/models/messages.ts | 47 ++++-------------- ts/sql/Client.ts | 18 ++++--- ts/sql/Interface.ts | 24 +++------ ts/sql/Server.ts | 9 ++-- ts/test-electron/models/conversations_test.ts | 1 - ts/test-electron/sql/sendLog_test.ts | 2 +- ts/util/markConversationRead.ts | 3 ++ ts/util/messageBatcher.ts | 9 +--- ts/views/conversation_view.ts | 8 +-- ts/window.d.ts | 16 +++--- 16 files changed, 70 insertions(+), 150 deletions(-) diff --git a/js/modules/debug.js b/js/modules/debug.js index 330b26d34..1e2fb5bb0 100644 --- a/js/modules/debug.js +++ b/js/modules/debug.js @@ -2,7 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-only /* eslint-env node */ -/* global log, Signal, Whisper */ +/* global log, Signal */ const fs = require('fs-extra'); const path = require('path'); @@ -60,7 +60,7 @@ exports.createConversation = async ({ await sleep(index * 100); log.info(`Create message ${index + 1}`); const message = await createRandomMessage({ conversationId }); - return Signal.Data.saveMessage(message, { Message: Whisper.Message }); + return Signal.Data.saveMessage(message); }) ); }; diff --git a/js/modules/messages_data_migrator.js b/js/modules/messages_data_migrator.js index e20a825a0..ff3e38d10 100644 --- a/js/modules/messages_data_migrator.js +++ b/js/modules/messages_data_migrator.js @@ -10,7 +10,6 @@ const { isFunction, isNumber } = require('lodash'); const Message = require('./types/message'); exports.processNext = async ({ - BackboneMessage, BackboneMessageCollection, numMessagesPerBatch, upgradeMessageSchema, @@ -18,12 +17,6 @@ exports.processNext = async ({ saveMessage, maxVersion = Message.CURRENT_SCHEMA_VERSION, } = {}) => { - if (!isFunction(BackboneMessage)) { - throw new TypeError( - "'BackboneMessage' (Whisper.Message) constructor is required" - ); - } - if (!isFunction(BackboneMessageCollection)) { throw new TypeError( "'BackboneMessageCollection' (Whisper.MessageCollection)" + @@ -72,11 +65,7 @@ exports.processNext = async ({ const upgradeDuration = Date.now() - upgradeStartTime; const saveStartTime = Date.now(); - await Promise.all( - upgradedMessages.map(message => - saveMessage(message, { Message: BackboneMessage }) - ) - ); + await Promise.all(upgradedMessages.map(message => saveMessage(message))); const saveDuration = Date.now() - saveStartTime; const totalDuration = Date.now() - startTime; diff --git a/ts/background.ts b/ts/background.ts index b4300a4f6..e29d87cb9 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -29,7 +29,7 @@ import { routineProfileRefresh } from './routineProfileRefresh'; import { isMoreRecentThan, isOlderThan } from './util/timestamp'; import { isValidReactionEmoji } from './reactions/isValidReactionEmoji'; import { ConversationModel } from './models/conversations'; -import { getMessageById, MessageModel } from './models/messages'; +import { getMessageById } from './models/messages'; import { createBatcher } from './util/batcher'; import { updateConversationsWithUuidLookup } from './updateConversationsWithUuidLookup'; import { initializeAllJobQueues } from './jobs/initializeAllJobQueues'; @@ -859,7 +859,6 @@ export async function startApp(): Promise { if (!isMigrationWithIndexComplete) { const batchWithIndex = await MessageDataMigrator.processNext({ - BackboneMessage: window.Whisper.Message, BackboneMessageCollection: window.Whisper.MessageCollection, numMessagesPerBatch: NUM_MESSAGES_PER_BATCH, upgradeMessageSchema, @@ -1770,9 +1769,7 @@ export async function startApp(): Promise { } ); - await window.Signal.Data.saveMessages(newMessageAttributes, { - Message: MessageModel, - }); + await window.Signal.Data.saveMessages(newMessageAttributes); } window.log.info('Expiration start timestamp cleanup: complete'); @@ -2564,9 +2561,7 @@ export async function startApp(): Promise { messagesToSave.push(message.attributes); } }); - await window.Signal.Data.saveMessages(messagesToSave, { - Message: MessageModel, - }); + await window.Signal.Data.saveMessages(messagesToSave); } function onReconnect() { // We disable notifications on first connect, but the same applies to reconnect. In diff --git a/ts/groups.ts b/ts/groups.ts index 19110f542..b7f81ea21 100644 --- a/ts/groups.ts +++ b/ts/groups.ts @@ -1718,7 +1718,6 @@ export async function createGroupV2({ }; await window.Signal.Data.saveMessages([createdTheGroupMessage], { forceSave: true, - Message: window.Whisper.Message, }); const model = new window.Whisper.Message(createdTheGroupMessage); window.MessageController.register(model.id, model); @@ -2864,7 +2863,6 @@ async function updateGroup( if (changeMessagesToSave.length > 0) { await window.Signal.Data.saveMessages(changeMessagesToSave, { forceSave: true, - Message: window.Whisper.Message, }); changeMessagesToSave.forEach(changeMessage => { const model = new window.Whisper.Message(changeMessage); diff --git a/ts/messageModifiers/AttachmentDownloads.ts b/ts/messageModifiers/AttachmentDownloads.ts index 81918b011..0a0f5bc26 100644 --- a/ts/messageModifiers/AttachmentDownloads.ts +++ b/ts/messageModifiers/AttachmentDownloads.ts @@ -297,9 +297,7 @@ async function _finishJob( ): Promise { if (message) { logger.info(`attachment_downloads/_finishJob for job id: ${id}`); - await saveMessage(message.attributes, { - Message: window.Whisper.Message, - }); + await saveMessage(message.attributes); } await removeAttachmentDownloadJob(id); diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 83fdaeaf2..4e04348bc 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -1601,9 +1601,7 @@ export class ConversationModel extends window.Backbone const registered = window.MessageController.register(m.id, m); const shouldSave = await registered.queueAttachmentDownloads(); if (shouldSave) { - await window.Signal.Data.saveMessage(registered.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(registered.attributes); } }) ); @@ -2414,9 +2412,7 @@ export class ConversationModel extends window.Backbone // this type does not fully implement the interface it is expected to } as unknown) as typeof window.Whisper.MessageAttributesType; - const id = await window.Signal.Data.saveMessage(message, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(message); const model = window.MessageController.register( id, new window.Whisper.Message({ @@ -2454,9 +2450,7 @@ export class ConversationModel extends window.Backbone // this type does not fully implement the interface it is expected to } as unknown) as typeof window.Whisper.MessageAttributesType; - const id = await window.Signal.Data.saveMessage(message, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(message); const model = window.MessageController.register( id, new window.Whisper.Message({ @@ -2492,9 +2486,7 @@ export class ConversationModel extends window.Backbone // this type does not fully implement the interface it is expected to } as unknown) as typeof window.Whisper.MessageAttributesType; - const id = await window.Signal.Data.saveMessage(message, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(message); const model = window.MessageController.register( id, new window.Whisper.Message({ @@ -2552,9 +2544,7 @@ export class ConversationModel extends window.Backbone // TODO: DESKTOP-722 } as unknown) as typeof window.Whisper.MessageAttributesType; - const id = await window.Signal.Data.saveMessage(message, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(message); const model = window.MessageController.register( id, new window.Whisper.Message({ @@ -2613,9 +2603,7 @@ export class ConversationModel extends window.Backbone // TODO: DESKTOP-722 } as unknown) as typeof window.Whisper.MessageAttributesType; - const id = await window.Signal.Data.saveMessage(message, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(message); const model = window.MessageController.register( id, new window.Whisper.Message({ @@ -2666,9 +2654,7 @@ export class ConversationModel extends window.Backbone // TODO: DESKTOP-722 } as unknown) as typeof window.Whisper.MessageAttributesType; - const id = await window.Signal.Data.saveMessage(message, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(message); const model = window.MessageController.register( id, new window.Whisper.Message({ @@ -2702,9 +2688,7 @@ export class ConversationModel extends window.Backbone // TODO: DESKTOP-722 } as unknown) as typeof window.Whisper.MessageAttributesType; - const id = await window.Signal.Data.saveMessage(message, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(message); const model = window.MessageController.register( id, new window.Whisper.Message({ @@ -3579,7 +3563,6 @@ export class ConversationModel extends window.Backbone const messageId = message.id; await window.Signal.Data.saveMessage(message.attributes, { forceSave: true, - Message: window.Whisper.Message, }); const draftProperties = dontClearDraft @@ -4082,9 +4065,7 @@ export class ConversationModel extends window.Backbone if (isDirectConversation(this.attributes)) { model.set({ destination: this.getSendTarget() }); } - const id = await window.Signal.Data.saveMessage(model.attributes, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(model.attributes); model.set({ id }); @@ -4182,9 +4163,7 @@ export class ConversationModel extends window.Backbone if (isDirectConversation(this.attributes)) { model.set({ destination: this.id }); } - const id = await window.Signal.Data.saveMessage(model.attributes, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(model.attributes); model.set({ id }); @@ -4212,9 +4191,7 @@ export class ConversationModel extends window.Backbone // TODO: DESKTOP-722 } as unknown) as MessageAttributesType); - const id = await window.Signal.Data.saveMessage(model.attributes, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(model.attributes); model.set({ id }); const message = window.MessageController.register(model.id, model); @@ -4257,9 +4234,7 @@ export class ConversationModel extends window.Backbone // TODO: DESKTOP-722 } as unknown) as MessageAttributesType); - const id = await window.Signal.Data.saveMessage(model.attributes, { - Message: window.Whisper.Message, - }); + const id = await window.Signal.Data.saveMessage(model.attributes); model.set({ id }); const message = window.MessageController.register(model.id, model); diff --git a/ts/models/messages.ts b/ts/models/messages.ts index 068f221dc..49d543865 100644 --- a/ts/models/messages.ts +++ b/ts/models/messages.ts @@ -147,8 +147,6 @@ export function isQuoteAMatch( } export class MessageModel extends window.Backbone.Model { - static updateTimers: () => void; - static getLongMessageAttachment: ( attachment: typeof window.WhatIsThis ) => typeof window.WhatIsThis; @@ -985,9 +983,7 @@ export class MessageModel extends window.Backbone.Model { this.getConversation()?.debouncedUpdateLastMessage?.(); if (shouldPersist) { - await window.Signal.Data.saveMessage(this.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(this.attributes); } await window.Signal.Data.deleteSentProtoByMessageId(this.id); @@ -1167,9 +1163,7 @@ export class MessageModel extends window.Backbone.Model { } if (!skipSave && !this.doNotSave) { - await window.Signal.Data.saveMessage(this.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(this.attributes); } } @@ -1242,9 +1236,7 @@ export class MessageModel extends window.Backbone.Model { if (!recipients.length) { window.log.warn('retrySend: Nobody to send to!'); - return window.Signal.Data.saveMessage(this.attributes, { - Message: window.Whisper.Message, - }); + return window.Signal.Data.saveMessage(this.attributes); } const attachmentsWithData = await Promise.all( @@ -1553,9 +1545,7 @@ export class MessageModel extends window.Backbone.Model { }); if (!this.doNotSave) { - await window.Signal.Data.saveMessage(this.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(this.attributes); } if (updateLeftPane) { @@ -1756,9 +1746,7 @@ export class MessageModel extends window.Backbone.Model { // We don't save because we're about to save below. this.saveErrors(errors, { skipSave: true }); } finally { - await window.Signal.Data.saveMessage(this.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(this.attributes); if (updateLeftPane) { updateLeftPane(); @@ -1815,9 +1803,7 @@ export class MessageModel extends window.Backbone.Model { return result; } - await window.Signal.Data.saveMessage(this.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(this.attributes); return result; }); }; @@ -2360,9 +2346,7 @@ export class MessageModel extends window.Backbone.Model { originalMessage.attributes ); originalMessage.set(upgradedMessage); - await window.Signal.Data.saveMessage(upgradedMessage, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(upgradedMessage); } } catch (error) { window.log.error( @@ -2496,9 +2480,7 @@ export class MessageModel extends window.Backbone.Model { unidentifiedDeliveries ), }); - await window.Signal.Data.saveMessage(toUpdate.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(toUpdate.attributes); confirm(); return; @@ -3186,9 +3168,7 @@ export class MessageModel extends window.Backbone.Model { window.log.info( `modifyTargetMessage/${this.idForLogging()}: Changes in second run; saving.` ); - await window.Signal.Data.saveMessage(this.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(this.attributes); } } @@ -3294,9 +3274,7 @@ export class MessageModel extends window.Backbone.Model { ); if (shouldPersist) { - await window.Signal.Data.saveMessage(this.attributes, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessage(this.attributes); } return oldReaction; @@ -3390,11 +3368,6 @@ window.Whisper.Message.getLongMessageAttachment = ({ }; }; -window.Whisper.Message.updateTimers = () => { - window.Whisper.ExpiringMessagesListener.update(); - window.Whisper.TapToViewMessagesListener.update(); -}; - window.Whisper.MessageCollection = window.Backbone.Collection.extend({ model: window.Whisper.Message, comparator(left: Readonly, right: Readonly) { diff --git a/ts/sql/Client.ts b/ts/sql/Client.ts index 2a49ef115..d9281f292 100644 --- a/ts/sql/Client.ts +++ b/ts/sql/Client.ts @@ -1068,25 +1068,27 @@ async function hasUserInitiatedMessages(conversationId: string) { async function saveMessage( data: MessageType, - { forceSave, Message }: { forceSave?: boolean; Message: typeof MessageModel } + options?: { forceSave?: boolean } ) { - const id = await channels.saveMessage(_cleanMessageData(data), { - forceSave, - }); - Message.updateTimers(); + const id = await channels.saveMessage(_cleanMessageData(data), options); + + window.Whisper.ExpiringMessagesListener.update(); + window.Whisper.TapToViewMessagesListener.update(); return id; } async function saveMessages( arrayOfMessages: Array, - { forceSave, Message }: { forceSave?: boolean; Message: typeof MessageModel } + options?: { forceSave?: boolean } ) { await channels.saveMessages( arrayOfMessages.map(message => _cleanMessageData(message)), - { forceSave } + options ); - Message.updateTimers(); + + window.Whisper.ExpiringMessagesListener.update(); + window.Whisper.TapToViewMessagesListener.update(); } async function removeMessage( diff --git a/ts/sql/Interface.ts b/ts/sql/Interface.ts index 6c0c91714..35ec50798 100644 --- a/ts/sql/Interface.ts +++ b/ts/sql/Interface.ts @@ -293,6 +293,14 @@ export type DataInterface = { options?: { limit?: number } ) => Promise>; + saveMessage: ( + data: MessageType, + options?: { forceSave?: boolean } + ) => Promise; + saveMessages: ( + arrayOfMessages: Array, + options?: { forceSave?: boolean } + ) => Promise; getMessageCount: (conversationId?: string) => Promise; hasUserInitiatedMessages: (conversationId: string) => Promise; getAllMessageIds: () => Promise>; @@ -487,14 +495,6 @@ export type ServerInterface = DataInterface & { conversationId: string, options?: { limit?: number } ) => Promise>; - saveMessage: ( - data: MessageType, - options: { forceSave?: boolean } - ) => Promise; - saveMessages: ( - arrayOfMessages: Array, - options: { forceSave?: boolean } - ) => Promise; updateConversation: (data: ConversationType) => Promise; // For testing only @@ -599,14 +599,6 @@ export type ClientInterface = DataInterface & { ids: Array, options: { Message: typeof MessageModel } ) => Promise; - saveMessage: ( - data: MessageType, - options: { forceSave?: boolean; Message: typeof MessageModel } - ) => Promise; - saveMessages: ( - arrayOfMessages: Array, - options: { forceSave?: boolean; Message: typeof MessageModel } - ) => Promise; searchMessages: ( query: string, options?: { limit?: number } diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index e60d5117d..cdc74e6a3 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -3496,11 +3496,11 @@ async function hasUserInitiatedMessages( function saveMessageSync( data: MessageType, - options: { forceSave?: boolean; alreadyInTransaction?: boolean } = {} + options?: { forceSave?: boolean; alreadyInTransaction?: boolean } ): string { const db = getInstance(); - const { forceSave, alreadyInTransaction } = options; + const { forceSave, alreadyInTransaction } = options || {}; if (!alreadyInTransaction) { return db.transaction(() => { @@ -3657,16 +3657,17 @@ function saveMessageSync( async function saveMessage( data: MessageType, - options: { forceSave?: boolean; alreadyInTransaction?: boolean } + options?: { forceSave?: boolean; alreadyInTransaction?: boolean } ): Promise { return saveMessageSync(data, options); } async function saveMessages( arrayOfMessages: Array, - { forceSave }: { forceSave?: boolean } = {} + options?: { forceSave?: boolean } ): Promise { const db = getInstance(); + const { forceSave } = options || {}; db.transaction(() => { for (const message of arrayOfMessages) { diff --git a/ts/test-electron/models/conversations_test.ts b/ts/test-electron/models/conversations_test.ts index d32866eab..cd130aa5d 100644 --- a/ts/test-electron/models/conversations_test.ts +++ b/ts/test-electron/models/conversations_test.ts @@ -68,7 +68,6 @@ describe('Conversations', () => { // Saving to db and updating the convo's last message await window.Signal.Data.saveMessage(message.attributes, { forceSave: true, - Message: window.Whisper.Message, }); message = window.MessageController.register(message.id, message); await window.Signal.Data.saveConversation(conversation.attributes); diff --git a/ts/test-electron/sql/sendLog_test.ts b/ts/test-electron/sql/sendLog_test.ts index a9e755ee4..848dd02b5 100644 --- a/ts/test-electron/sql/sendLog_test.ts +++ b/ts/test-electron/sql/sendLog_test.ts @@ -111,7 +111,7 @@ describe('sendLog', () => { timestamp, type: 'outgoing', }, - { forceSave: true, Message: window.Whisper.Message } + { forceSave: true } ); const bytes = Buffer.from(getRandomBytes(128)); diff --git a/ts/util/markConversationRead.ts b/ts/util/markConversationRead.ts index 2768aa2b3..b5303c531 100644 --- a/ts/util/markConversationRead.ts +++ b/ts/util/markConversationRead.ts @@ -130,5 +130,8 @@ export async function markConversationRead( await sendReadReceiptsFor(conversationAttrs, unreadMessagesSyncData); } + window.Whisper.ExpiringMessagesListener.update(); + window.Whisper.TapToViewMessagesListener.update(); + return true; } diff --git a/ts/util/messageBatcher.ts b/ts/util/messageBatcher.ts index 22790ba93..0aff095f4 100644 --- a/ts/util/messageBatcher.ts +++ b/ts/util/messageBatcher.ts @@ -11,9 +11,7 @@ const updateMessageBatcher = createBatcher({ maxSize: 50, processBatch: async (messageAttrs: Array) => { window.log.info('updateMessageBatcher', messageAttrs.length); - await window.Signal.Data.saveMessages(messageAttrs, { - Message: window.Whisper.Message, - }); + await window.Signal.Data.saveMessages(messageAttrs); }, }); @@ -23,9 +21,7 @@ export function queueUpdateMessage(messageAttr: MessageAttributesType): void { if (shouldBatch) { updateMessageBatcher.add(messageAttr); } else { - window.Signal.Data.saveMessage(messageAttr, { - Message: window.Whisper.Message, - }); + window.Signal.Data.saveMessage(messageAttr); } } @@ -41,7 +37,6 @@ export const saveNewMessageBatcher = createWaitBatcher({ window.log.info('saveNewMessageBatcher', messageAttrs.length); await window.Signal.Data.saveMessages(messageAttrs, { forceSave: true, - Message: window.Whisper.Message, }); }, }); diff --git a/ts/views/conversation_view.ts b/ts/views/conversation_view.ts index ae8d20e0d..4ebec68f7 100644 --- a/ts/views/conversation_view.ts +++ b/ts/views/conversation_view.ts @@ -1150,9 +1150,7 @@ Whisper.ConversationView = Whisper.View.extend({ const upgradedMessage = await upgradeMessageSchema(attributes); message.set(upgradedMessage); // eslint-disable-next-line no-await-in-loop - await window.Signal.Data.saveMessage(upgradedMessage, { - Message: Whisper.Message, - }); + await window.Signal.Data.saveMessage(upgradedMessage); } } @@ -2543,9 +2541,7 @@ Whisper.ConversationView = Whisper.View.extend({ // eslint-disable-next-line no-await-in-loop rawMedia[i] = await upgradeMessageSchema(message); // eslint-disable-next-line no-await-in-loop - await window.Signal.Data.saveMessage(rawMedia[i], { - Message: Whisper.Message, - }); + await window.Signal.Data.saveMessage(rawMedia[i]); } } diff --git a/ts/window.d.ts b/ts/window.d.ts index 44d5fe562..9bb12201b 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -559,10 +559,7 @@ export type LoggerType = { export type LogFunctionType = (...args: Array) => void; export type WhisperType = { - events: { - on: (name: string, callback: (param1: any, param2?: any) => void) => void; - trigger: (name: string, param1?: any, param2?: any) => void; - }; + events: Backbone.Events; Database: { open: () => Promise; handleDOMException: ( @@ -620,8 +617,15 @@ export type WhisperType = { IdenticonSVGView: WhatIsThis; - ExpiringMessagesListener: WhatIsThis; - TapToViewMessagesListener: WhatIsThis; + ExpiringMessagesListener: { + init: (events: Backbone.Events) => void; + update: () => void; + }; + TapToViewMessagesListener: { + nextCheck: null | number; + init: (events: Backbone.Events) => void; + update: () => void; + }; deliveryReceiptQueue: PQueue; deliveryReceiptBatcher: BatcherType;