diff --git a/js/delivery_receipts.js b/js/delivery_receipts.js index ea1a2704e..09fd1826f 100644 --- a/js/delivery_receipts.js +++ b/js/delivery_receipts.js @@ -100,7 +100,7 @@ await message.setToExpire(false, { skipSave: true }); } - window.Signal.Util.updateMessageBatcher.add(message.attributes); + window.Signal.Util.queueUpdateMessage(message.attributes); // notify frontend listeners const conversation = ConversationController.get( diff --git a/js/read_receipts.js b/js/read_receipts.js index 30a7f28ce..7bca12c42 100644 --- a/js/read_receipts.js +++ b/js/read_receipts.js @@ -101,7 +101,7 @@ await message.setToExpire(false, { skipSave: true }); } - window.Signal.Util.updateMessageBatcher.add(message.attributes); + window.Signal.Util.queueUpdateMessage(message.attributes); // notify frontend listeners const conversation = ConversationController.get( diff --git a/js/read_syncs.js b/js/read_syncs.js index 23296062d..9b6f05eef 100644 --- a/js/read_syncs.js +++ b/js/read_syncs.js @@ -94,7 +94,7 @@ } } - window.Signal.Util.updateMessageBatcher.add(message.attributes); + window.Signal.Util.queueUpdateMessage(message.attributes); this.remove(receipt); } catch (error) { diff --git a/ts/background.ts b/ts/background.ts index 20dff3fc3..fefd8657b 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -2067,6 +2067,7 @@ export async function startApp(): Promise { messageReceiver.getProcessedCount() ); window.sqlInitializer.goBackToMainProcess(); + window.Signal.Util.setBatchingStrategy(false); const attachmentDownloadQueue = window.attachmentDownloadQueue || []; const THREE_DAYS_AGO = Date.now() - 3600 * 72 * 1000; const MAX_ATTACHMENT_MSGS_TO_DOWNLOAD = 250; diff --git a/ts/models/messages.ts b/ts/models/messages.ts index 8d6075d34..6a8eed3a3 100644 --- a/ts/models/messages.ts +++ b/ts/models/messages.ts @@ -1163,7 +1163,7 @@ export class MessageModel extends window.Backbone.Model { referencedMessageNotFound: false, }, }); - window.Signal.Util.updateMessageBatcher.add(this.attributes); + window.Signal.Util.queueUpdateMessage(this.attributes); } } @@ -1959,7 +1959,7 @@ export class MessageModel extends window.Backbone.Model { window.Whisper.Notifications.removeBy({ messageId: this.id }); if (!skipSave) { - window.Signal.Util.updateMessageBatcher.add(this.attributes); + window.Signal.Util.queueUpdateMessage(this.attributes); } } @@ -2008,7 +2008,7 @@ export class MessageModel extends window.Backbone.Model { const id = this.get('id'); if (id && !skipSave) { - window.Signal.Util.updateMessageBatcher.add(this.attributes); + window.Signal.Util.queueUpdateMessage(this.attributes); } } } diff --git a/ts/util/index.ts b/ts/util/index.ts index fb75f1992..23801bd0d 100644 --- a/ts/util/index.ts +++ b/ts/util/index.ts @@ -17,7 +17,11 @@ import { hasExpired } from './hasExpired'; import { incrementMessageCounter } from './incrementMessageCounter'; import { isFileDangerous } from './isFileDangerous'; import { makeLookup } from './makeLookup'; -import { saveNewMessageBatcher, updateMessageBatcher } from './messageBatcher'; +import { + queueUpdateMessage, + saveNewMessageBatcher, + setBatchingStrategy, +} from './messageBatcher'; import { missingCaseError } from './missingCaseError'; import { parseRemoteClientExpiration } from './parseRemoteClientExpiration'; import { sleep } from './sleep'; @@ -52,11 +56,12 @@ export { mapToSupportLocale, missingCaseError, parseRemoteClientExpiration, + queueUpdateMessage, saveNewMessageBatcher, + setBatchingStrategy, sessionRecordToProtobuf, sessionStructureToArrayBuffer, sleep, toWebSafeBase64, - updateMessageBatcher, zkgroup, }; diff --git a/ts/util/messageBatcher.ts b/ts/util/messageBatcher.ts index ec8813dd2..e3c9baf0f 100644 --- a/ts/util/messageBatcher.ts +++ b/ts/util/messageBatcher.ts @@ -5,7 +5,7 @@ import { MessageAttributesType } from '../model-types.d'; import { createBatcher } from './batcher'; import { createWaitBatcher } from './waitBatcher'; -export const updateMessageBatcher = createBatcher({ +const updateMessageBatcher = createBatcher({ wait: 500, maxSize: 50, processBatch: async (messageAttrs: Array) => { @@ -14,6 +14,22 @@ export const updateMessageBatcher = createBatcher({ }, }); +let shouldBatch = true; + +export function queueUpdateMessage(messageAttr: MessageAttributesType): void { + if (shouldBatch) { + updateMessageBatcher.add(messageAttr); + } else { + window.Signal.Data.saveMessage(messageAttr, { + Message: window.Whisper.Message, + }); + } +} + +export function setBatchingStrategy(keepBatching = false): void { + shouldBatch = keepBatching; +} + export const saveNewMessageBatcher = createWaitBatcher({ wait: 500, maxSize: 30,