From cf9764c85aa47df597e79d7e153565e69d4882e6 Mon Sep 17 00:00:00 2001 From: Chris Svenningsen Date: Fri, 2 Oct 2020 15:19:52 -0700 Subject: [PATCH] Consolidate checks for v2 Groups --- ts/background.ts | 3 +-- ts/models/conversations.ts | 15 +++++++++------ ts/services/storage.ts | 2 +- ts/services/storageRecordOps.ts | 2 +- ts/views/conversation_view.ts | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/ts/background.ts b/ts/background.ts index 12abdd30e..720e733bc 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -2436,8 +2436,7 @@ type WhatIsThis = typeof window.WhatIsThis; id, 'group' ); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - if (conversation.get('groupVersion')! > 1) { + if (conversation.isGroupV2()) { window.log.warn( 'Got group sync for v2 group: ', conversation.idForLogging() diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 9fd35e0ac..6531045c3 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -142,8 +142,7 @@ export class ConversationModel extends window.Backbone.Model< const e164 = this.get('e164'); return `${uuid || e164} (${this.id})`; } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - if (this.get('groupVersion')! > 1) { + if (this.isGroupV2()) { return `groupv2(${this.get('groupId')})`; } @@ -267,6 +266,10 @@ export class ConversationModel extends window.Backbone.Model< return fromEncodedBinaryToArrayBuffer(groupID).byteLength === 16; } + isGroupV2(): boolean { + return (this.get('groupVersion') || 0) === 2; + } + isEverUnregistered(): boolean { return Boolean(this.get('discoveredUnregisteredAt')); } @@ -488,7 +491,7 @@ export class ConversationModel extends window.Backbone.Model< } async fetchLatestGroupV2Data(): Promise { - if (this.get('groupVersion') !== 2) { + if (!this.isGroupV2()) { return; } @@ -520,7 +523,7 @@ export class ConversationModel extends window.Backbone.Model< } getGroupV2Info(groupChange?: ArrayBuffer): WhatIsThis { - if (this.isPrivate() || this.get('groupVersion') !== 2) { + if (this.isPrivate() || !this.isGroupV2()) { return undefined; } return { @@ -2571,7 +2574,7 @@ export class ConversationModel extends window.Backbone.Model< receivedAt: number, options: { fromSync?: unknown; fromGroupUpdate?: unknown } = {} ): Promise { - if (this.get('groupVersion') === 2) { + if (this.isGroupV2()) { if (providedSource || receivedAt) { throw new Error( 'updateExpirationTimer: GroupV2 timers are not updated this way' @@ -3494,7 +3497,7 @@ export class ConversationModel extends window.Backbone.Model< return true; } - if (this.get('groupVersion') !== 2) { + if (!this.isGroupV2()) { return true; } diff --git a/ts/services/storage.ts b/ts/services/storage.ts index 5b24c0009..49fcb8ec7 100644 --- a/ts/services/storage.ts +++ b/ts/services/storage.ts @@ -148,7 +148,7 @@ async function generateManifest( // eslint-disable-next-line no-await-in-loop storageRecord.contact = await toContactRecord(conversation); identifier.type = ITEM_TYPE.CONTACT; - } else if ((conversation.get('groupVersion') || 0) > 1) { + } else if (conversation.isGroupV2()) { storageRecord = new window.textsecure.protobuf.StorageRecord(); // eslint-disable-next-line no-await-in-loop storageRecord.groupV2 = await toGroupV2Record(conversation); diff --git a/ts/services/storageRecordOps.ts b/ts/services/storageRecordOps.ts index 06a34f18f..0756bd043 100644 --- a/ts/services/storageRecordOps.ts +++ b/ts/services/storageRecordOps.ts @@ -166,7 +166,7 @@ export async function toAccountRecord( pinnedConversationRecord.legacyGroupId = fromEncodedBinaryToArrayBuffer( groupId ); - } else if ((pinnedConversation.get('groupVersion') || 0) > 1) { + } else if (pinnedConversation.isGroupV2()) { pinnedConversationRecord.identifier = 'groupMasterKey'; const masterKey = pinnedConversation.get('masterKey'); if (!masterKey) { diff --git a/ts/views/conversation_view.ts b/ts/views/conversation_view.ts index 6a33acadb..527479186 100644 --- a/ts/views/conversation_view.ts +++ b/ts/views/conversation_view.ts @@ -2139,7 +2139,7 @@ Whisper.ConversationView = Whisper.View.extend({ let model = providedMembers || this.model.contactCollection; - if (!providedMembers && this.model.get('groupVersion') === 2) { + if (!providedMembers && this.model.isGroupV2()) { model = new Whisper.GroupConversationCollection( this.model.get('membersV2').map(({ conversationId, role }: any) => ({ conversation: window.ConversationController.get(conversationId),