From 68f705e6ad38494745f707b9d0a2d7ddfa3e6312 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 25 May 2022 11:15:09 -0700 Subject: [PATCH] Reset MessageReceiver processed count after reporting it --- app/main.ts | 8 ++++---- ts/ConversationController.ts | 13 ++++++++++++- ts/background.ts | 6 ++++-- ts/textsecure/MessageReceiver.ts | 6 ++++-- ts/window.d.ts | 4 +++- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/main.ts b/app/main.ts index 52ca71a5e..acff72ff0 100644 --- a/app/main.ts +++ b/app/main.ts @@ -486,10 +486,10 @@ type BoundsType = { }; function isVisible(window: BoundsType, bounds: BoundsType) { - const boundsX = get(bounds, 'x') || 0; - const boundsY = get(bounds, 'y') || 0; - const boundsWidth = get(bounds, 'width') || DEFAULT_WIDTH; - const boundsHeight = get(bounds, 'height') || DEFAULT_HEIGHT; + const boundsX = bounds?.x || 0; + const boundsY = bounds?.y || 0; + const boundsWidth = bounds?.width || DEFAULT_WIDTH; + const boundsHeight = bounds?.height || DEFAULT_HEIGHT; // requiring BOUNDS_BUFFER pixels on the left or right side const rightSideClearOfLeftBound = diff --git a/ts/ConversationController.ts b/ts/ConversationController.ts index 42ff09139..c1b38f910 100644 --- a/ts/ConversationController.ts +++ b/ts/ConversationController.ts @@ -44,13 +44,16 @@ export function start(): void { // This class is entirely designed to keep the app title, badge and tray icon updated. // In the future it could listen to redux changes and do its updates there. const inboxCollection = new (window.Backbone.Collection.extend({ + hasQueueEmptied: false, + initialize() { this.listenTo(conversations, 'add change:active_at', this.addActive); this.listenTo(conversations, 'reset', () => this.reset([])); const debouncedUpdateUnreadCount = debounce( this.updateUnreadCount.bind(this), - 1000 + 1000, + { leading: true, maxWait: 1000, trailing: true } ); this.on( @@ -65,6 +68,10 @@ export function start(): void { model.startMuteTimer(); }); }, + onEmpty() { + this.hasQueueEmptied = true; + this.updateUnreadCount(); + }, addActive(model: ConversationModel) { if (model.get('active_at')) { this.add(model); @@ -73,6 +80,10 @@ export function start(): void { } }, updateUnreadCount() { + if (!this.hasQueueEmptied) { + return; + } + const canCountMutedConversations = window.storage.get('badge-count-muted-conversations') || false; diff --git a/ts/background.ts b/ts/background.ts index d81ec041a..edf90b7f0 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -2287,6 +2287,7 @@ export async function startApp(): Promise { ]); log.info('onEmpty: All outstanding database requests complete'); window.readyForUpdates(); + window.getInboxCollection().onEmpty(); // Start listeners here, after we get through our queue. RotateSignedPreKeyListener.init(window.Whisper.events, newVersion); @@ -2305,11 +2306,12 @@ export async function startApp(): Promise { window.reduxActions.app.initialLoadComplete(); + const processedCount = messageReceiver?.getAndResetProcessedCount() || 0; window.logAppLoadedEvent?.({ - processedCount: messageReceiver && messageReceiver.getProcessedCount(), + processedCount, }); if (messageReceiver) { - log.info('App loaded - messages:', messageReceiver.getProcessedCount()); + log.info('App loaded - messages:', processedCount); } window.Signal.Util.setBatchingStrategy(false); diff --git a/ts/textsecure/MessageReceiver.ts b/ts/textsecure/MessageReceiver.ts index 9a79041d1..4a18659b5 100644 --- a/ts/textsecure/MessageReceiver.ts +++ b/ts/textsecure/MessageReceiver.ts @@ -251,8 +251,10 @@ export default class MessageReceiver }); } - public getProcessedCount(): number { - return this.processedCount; + public getAndResetProcessedCount(): number { + const count = this.processedCount; + this.processedCount = 0; + return count; } public handleRequest(request: IncomingWebSocketRequest): void { diff --git a/ts/window.d.ts b/ts/window.d.ts index 8b2396ba3..2735f05af 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -195,7 +195,9 @@ declare global { getEnvironment: typeof getEnvironment; getExpiration: () => string; getHostName: () => string; - getInboxCollection: () => ConversationModelCollectionType; + getInboxCollection: () => ConversationModelCollectionType & { + onEmpty: () => void; + }; getInteractionMode: () => 'mouse' | 'keyboard'; getLocale: () => ElectronLocaleType; getMediaCameraPermissions: () => Promise;