From 98894ab121ffa2f4031eecbb2dfea79028fabcc2 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Fri, 7 May 2021 13:07:24 -0700 Subject: [PATCH] Let group update happen on relink --- js/views/inbox_view.js | 14 ++++++++++++++ ts/groups.ts | 16 ++++++++++++++++ ts/models/conversations.ts | 2 ++ ts/views/conversation_view.ts | 8 +------- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 050485643e..fe79e88df0 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -52,6 +52,14 @@ // Make sure poppers are positioned properly window.dispatchEvent(new Event('resize')); }, + unload() { + const { lastConversation } = this; + if (!lastConversation) { + return; + } + + lastConversation.trigger('unload', 'force unload requested'); + }, onUnload(conversation) { if (this.lastConversation === conversation) { this.stopListening(this.lastConversation); @@ -93,6 +101,12 @@ } }); + // Close current opened conversation to reload the group information once + // linked. + Whisper.events.on('setupAsNewDevice', () => { + this.conversation_stack.unload(); + }); + if (!options.initialLoadComplete) { this.appLoadingScreen = new Whisper.AppLoadingScreen(); this.appLoadingScreen.render(); diff --git a/ts/groups.ts b/ts/groups.ts index f621c25499..fcfe7b8bde 100644 --- a/ts/groups.ts +++ b/ts/groups.ts @@ -22,6 +22,7 @@ import { isStorageWriteFeatureEnabled } from './storage/isFeatureEnabled'; import dataInterface from './sql/Client'; import { toWebSafeBase64, fromWebSafeBase64 } from './util/webSafeBase64'; import { assert } from './util/assert'; +import { isMoreRecentThan } from './util/timestamp'; import { ConversationAttributesType, GroupV2MemberType, @@ -2597,6 +2598,8 @@ type MaybeUpdatePropsType = { dropInitialJoinMessage?: boolean; }; +const FIVE_MINUTES = 1000 * 60 * 5; + export async function waitThenMaybeUpdateGroup( options: MaybeUpdatePropsType, { viaSync = false } = {} @@ -2607,10 +2610,23 @@ export async function waitThenMaybeUpdateGroup( // Then wait to process all outstanding messages for this conversation const { conversation } = options; + const { lastSuccessfulGroupFetch = 0 } = conversation; + + if (isMoreRecentThan(lastSuccessfulGroupFetch, FIVE_MINUTES)) { + const waitTime = lastSuccessfulGroupFetch + FIVE_MINUTES - Date.now(); + window.log.info( + `waitThenMaybeUpdateGroup/${conversation.idForLogging()}: group update ` + + `was fetched recently, skipping for ${waitTime}ms` + ); + return; + } + await conversation.queueJob(async () => { try { // And finally try to update the group await maybeUpdateGroup(options, { viaSync }); + + conversation.lastSuccessfulGroupFetch = Date.now(); } catch (error) { window.log.error( `waitThenMaybeUpdateGroup/${conversation.idForLogging()}: maybeUpdateGroup failure:`, diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 739dd3e76f..44ce01e898 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -150,6 +150,8 @@ export class ConversationModel extends window.Backbone intlCollator = new Intl.Collator(undefined, { sensitivity: 'base' }); + lastSuccessfulGroupFetch?: number; + private cachedLatestGroupCallEraId?: string; private cachedIdenticon?: CachedIdenticon; diff --git a/ts/views/conversation_view.ts b/ts/views/conversation_view.ts index aaa47d5555..eaf94682b2 100644 --- a/ts/views/conversation_view.ts +++ b/ts/views/conversation_view.ts @@ -391,12 +391,6 @@ Whisper.ConversationView = Whisper.View.extend({ this.model.updateSharedGroups.bind(this.model), FIVE_MINUTES ); - this.model.throttledFetchLatestGroupV2Data = - this.model.throttledFetchLatestGroupV2Data || - window._.throttle( - this.model.fetchLatestGroupV2Data.bind(this.model), - FIVE_MINUTES - ); this.model.throttledMaybeMigrateV1Group = this.model.throttledMaybeMigrateV1Group || window._.throttle( @@ -2181,7 +2175,7 @@ Whisper.ConversationView = Whisper.View.extend({ this.setQuoteMessage(quotedMessageId); } - this.model.throttledFetchLatestGroupV2Data(); + this.model.fetchLatestGroupV2Data(); this.model.throttledMaybeMigrateV1Group(); const statusPromise = this.model.throttledGetProfiles();