// Copyright 2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import { compact, has, isNumber, throttle, debounce } from 'lodash'; import { batch as batchDispatch } from 'react-redux'; import { v4 as generateGuid } from 'uuid'; import PQueue from 'p-queue'; import type { ReadonlyDeep } from 'type-fest'; import type { ConversationAttributesType, ConversationLastProfileType, ConversationRenderInfoType, MessageAttributesType, QuotedMessageType, SenderKeyInfoType, } from '../model-types.d'; import { getConversation } from '../util/getConversation'; import { drop } from '../util/drop'; import { isShallowEqual } from '../util/isShallowEqual'; import { getInitials } from '../util/getInitials'; import { clearTimeoutIfNecessary } from '../util/clearTimeoutIfNecessary'; import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp'; import type { AttachmentType, ThumbnailType } from '../types/Attachment'; import { toDayMillis } from '../util/timestamp'; import { areWeAdmin } from '../util/areWeAdmin'; import { isBlocked } from '../util/isBlocked'; import { getAboutText } from '../util/getAboutText'; import { getAvatar, getRawAvatarPath, getLocalAvatarUrl, } from '../util/avatarUtils'; import { getDraftPreview } from '../util/getDraftPreview'; import { hasDraft } from '../util/hasDraft'; import { missingCaseError } from '../util/missingCaseError'; import * as Conversation from '../types/Conversation'; import type { StickerType, StickerWithHydratedData } from '../types/Stickers'; import * as Stickers from '../types/Stickers'; import { StorySendMode } from '../types/Stories'; import type { EmbeddedContactWithHydratedAvatar } from '../types/EmbeddedContact'; import type { GroupV2InfoType } from '../textsecure/SendMessage'; import createTaskWithTimeout from '../textsecure/TaskWithTimeout'; import MessageSender from '../textsecure/SendMessage'; import type { CallbackResultType, PniSignatureMessageType, } from '../textsecure/Types.d'; import type { ConversationType, DraftPreviewType, } from '../state/ducks/conversations'; import type { AvatarColorType, ConversationColorType, CustomColorType, } from '../types/Colors'; import type { MessageModel } from './messages'; import { getAuthor } from '../messages/helpers'; import { strictAssert } from '../util/assert'; import { isConversationMuted } from '../util/isConversationMuted'; import { isConversationSMSOnly } from '../util/isConversationSMSOnly'; import { isConversationEverUnregistered, isConversationUnregistered, isConversationUnregisteredAndStale, } from '../util/isConversationUnregistered'; import { sniffImageMimeType } from '../util/sniffImageMimeType'; import { isValidE164 } from '../util/isValidE164'; import type { MIMEType } from '../types/MIME'; import { IMAGE_JPEG, IMAGE_WEBP } from '../types/MIME'; import type { AciString, PniString, ServiceIdString } from '../types/ServiceId'; import { ServiceIdKind, normalizeServiceId, normalizePni, } from '../types/ServiceId'; import { isAciString } from '../util/isAciString'; import { constantTimeEqual, decryptProfile, decryptProfileName, deriveAccessKey, } from '../Crypto'; import { decryptAttachmentV2 } from '../AttachmentCrypto'; import * as Bytes from '../Bytes'; import type { DraftBodyRanges } from '../types/BodyRange'; import { BodyRange } from '../types/BodyRange'; import { migrateColor } from '../util/migrateColor'; import { isNotNil } from '../util/isNotNil'; import { NotificationType, NotificationSetting, notificationService, } from '../services/notifications'; import { storageServiceUploadJob } from '../services/storage'; import { getSendOptions } from '../util/getSendOptions'; import type { IsConversationAcceptedOptionsType } from '../util/isConversationAccepted'; import { isConversationAccepted } from '../util/isConversationAccepted'; import { getNumber, getProfileName, getTitle, getTitleNoDefault, hasNumberTitle, hasUsernameTitle, canHaveUsername, } from '../util/getTitle'; import { markConversationRead } from '../util/markConversationRead'; import { handleMessageSend } from '../util/handleMessageSend'; import { getConversationMembers } from '../util/getConversationMembers'; import { updateConversationsWithUuidLookup } from '../updateConversationsWithUuidLookup'; import { ReadStatus } from '../messages/MessageReadStatus'; import { SendStatus } from '../messages/MessageSendState'; import type { LinkPreviewType, LinkPreviewWithHydratedData, } from '../types/message/LinkPreviews'; import { MINUTE, SECOND, DurationInSeconds } from '../util/durations'; import { concat, filter, map, repeat, zipObject } from '../util/iterables'; import * as universalExpireTimer from '../util/universalExpireTimer'; import type { GroupNameCollisionsWithIdsByTitle } from '../util/groupMemberNameCollisions'; import { isDirectConversation, isGroup, isGroupV1, isGroupV2, isMe, } from '../util/whatTypeOfConversation'; import { SignalService as Proto } from '../protobuf'; import { getMessagePropStatus, hasErrors, isIncoming, isStory, } from '../state/selectors/message'; import { conversationJobQueue, conversationQueueJobEnum, } from '../jobs/conversationJobQueue'; import type { ReactionAttributesType } from '../messageModifiers/Reactions'; import { getProfile } from '../util/getProfile'; import { SEALED_SENDER } from '../types/SealedSender'; import { createIdenticon } from '../util/createIdenticon'; import * as log from '../logging/log'; import * as Errors from '../types/errors'; import { isMessageUnread } from '../util/isMessageUnread'; import type { SenderKeyTargetType } from '../util/sendToGroup'; import { resetSenderKey, sendContentMessageToGroup } from '../util/sendToGroup'; import { singleProtoJobQueue } from '../jobs/singleProtoJobQueue'; import { TimelineMessageLoadingState } from '../util/timelineUtil'; import { SeenStatus } from '../MessageSeenStatus'; import { getConversationIdForLogging } from '../util/idForLogging'; import { getSendTarget } from '../util/getSendTarget'; import { getRecipients } from '../util/getRecipients'; import { validateConversation } from '../util/validateConversation'; import { isSignalConversation } from '../util/isSignalConversation'; import { removePendingMember } from '../util/removePendingMember'; import { isMember, isMemberAwaitingApproval, isMemberBanned, isMemberPending, isMemberRequestingToJoin, } from '../util/groupMembershipUtils'; import { imageToBlurHash } from '../util/imageToBlurHash'; import { ReceiptType } from '../types/Receipt'; import { getQuoteAttachment } from '../util/makeQuote'; import { deriveProfileKeyVersion } from '../util/zkgroup'; import { incrementMessageCounter } from '../util/incrementMessageCounter'; import OS from '../util/os/osMain'; import { getMessageAuthorText } from '../util/getMessageAuthorText'; import { downscaleOutgoingAttachment } from '../util/attachments'; import { MessageRequestResponseEvent } from '../types/MessageRequestResponseEvent'; import type { MessageToDelete } from '../textsecure/messageReceiverEvents'; import { getConversationToDelete, getMessageToDelete, } from '../util/deleteForMe'; import { isEnabled } from '../RemoteConfig'; import { getCallHistorySelector } from '../state/selectors/callHistory'; /* eslint-disable more/no-then */ window.Whisper = window.Whisper || {}; const { Message } = window.Signal.Types; const { copyIntoTempDirectory, deleteAttachmentData, doesAttachmentExist, getAbsoluteAttachmentPath, getAbsoluteTempPath, readStickerData, upgradeMessageSchema, writeNewAttachmentData, } = window.Signal.Migrations; const { addStickerPackReference, getConversationRangeCenteredOnMessage, getOlderMessagesByConversation, getMessageMetricsForConversation, getMessageById, getMostRecentAddressableMessages, getMostRecentAddressableNondisappearingMessages, getNewerMessagesByConversation, } = window.Signal.Data; const FIVE_MINUTES = MINUTE * 5; const FETCH_TIMEOUT = SECOND * 30; const JOB_REPORTING_THRESHOLD_MS = 25; const SEND_REPORTING_THRESHOLD_MS = 25; const MESSAGE_LOAD_CHUNK_SIZE = 30; const ATTRIBUTES_THAT_DONT_INVALIDATE_PROPS_CACHE = new Set([ 'lastProfile', 'profileLastFetchedAt', 'needsStorageServiceSync', 'storageID', 'storageVersion', 'storageUnknownFields', ]); type CachedIdenticon = { readonly color: AvatarColorType; readonly text?: string; readonly path?: string; readonly url: string; }; export class ConversationModel extends window.Backbone .Model { static COLORS: string; cachedProps?: ConversationType | null; oldCachedProps?: ConversationType | null; contactTypingTimers?: Record< string, { senderId: string; timer: NodeJS.Timer; timestamp: number; } >; contactCollection?: Backbone.Collection; debouncedUpdateLastMessage: (() => void) & { flush(): void }; initialPromise?: Promise; inProgressFetch?: Promise; newMessageQueue?: PQueue; jobQueue?: PQueue; storeName?: string | null; throttledBumpTyping?: () => void; throttledFetchSMSOnlyUUID?: () => Promise | undefined; throttledMaybeMigrateV1Group?: () => Promise | undefined; throttledGetProfiles?: () => Promise; throttledUpdateVerified?: () => void; typingRefreshTimer?: NodeJS.Timer | null; typingPauseTimer?: NodeJS.Timer | null; intlCollator = new Intl.Collator(undefined, { sensitivity: 'base' }); lastSuccessfulGroupFetch?: number; throttledUpdateSharedGroups?: () => Promise; private cachedIdenticon?: CachedIdenticon; public isFetchingUUID?: boolean; private lastIsTyping?: boolean; private muteTimer?: NodeJS.Timer; private isInReduxBatch = false; private privVerifiedEnum?: typeof window.textsecure.storage.protocol.VerifiedStatus; private isShuttingDown = false; override defaults(): Partial { return { unreadCount: 0, verified: window.textsecure.storage.protocol.VerifiedStatus.DEFAULT, messageCount: 0, sentMessageCount: 0, }; } idForLogging(): string { return getConversationIdForLogging(this.attributes); } getSendTarget(): ServiceIdString | undefined { return getSendTarget(this.attributes); } getContactCollection(): Backbone.Collection { const collection = new window.Backbone.Collection(); const collator = new Intl.Collator(undefined, { sensitivity: 'base' }); collection.comparator = ( left: ConversationModel, right: ConversationModel ) => { return collator.compare(left.getTitle(), right.getTitle()); }; return collection; } constructor(attributes: ConversationAttributesType) { super(attributes); // Note that we intentionally don't use `initialize()` method because it // isn't compatible with esnext output of esbuild. const serviceId = this.getServiceId(); const normalizedServiceId = serviceId && normalizeServiceId(serviceId, 'ConversationModel.initialize'); if (serviceId && normalizedServiceId !== serviceId) { log.warn( 'ConversationModel.initialize: normalizing serviceId from ' + `${serviceId} to ${normalizedServiceId}` ); this.set('serviceId', normalizedServiceId); } if (isValidE164(attributes.id, false)) { this.set({ id: generateGuid(), e164: attributes.id }); } this.storeName = 'conversations'; this.privVerifiedEnum = window.textsecure.storage.protocol.VerifiedStatus; // This may be overridden by window.ConversationController.getOrCreate, and signify // our first save to the database. Or first fetch from the database. this.initialPromise = Promise.resolve(); this.debouncedUpdateLastMessage = debounce( this.updateLastMessage.bind(this), 200 ); this.contactCollection = this.getContactCollection(); this.contactCollection.on( 'change:name change:profileName change:profileFamilyName change:e164', this.debouncedUpdateLastMessage, this ); if (!isDirectConversation(this.attributes)) { this.contactCollection.on( 'change:verified', this.onMemberVerifiedChange.bind(this) ); } this.on('newmessage', this.onNewMessage); this.on('change:profileKey', this.onChangeProfileKey); this.on( 'change:name change:profileName change:profileFamilyName change:e164 ' + 'change:systemGivenName change:systemFamilyName change:systemNickname', () => this.maybeClearUsername() ); const sealedSender = this.get('sealedSender'); if (sealedSender === undefined) { this.set({ sealedSender: SEALED_SENDER.UNKNOWN }); } // @ts-expect-error -- Removing legacy prop this.unset('unidentifiedDelivery'); // @ts-expect-error -- Removing legacy prop this.unset('unidentifiedDeliveryUnrestricted'); // @ts-expect-error -- Removing legacy prop this.unset('hasFetchedProfile'); // @ts-expect-error -- Removing legacy prop this.unset('tokens'); this.on('change:members change:membersV2', this.fetchContacts); this.typingRefreshTimer = null; this.typingPauseTimer = null; // We clear our cached props whenever we change so that the next call to format() will // result in refresh via a getProps() call. See format() below. this.on( 'change', (_model: MessageModel, options: { force?: boolean } = {}) => { const changedKeys = Object.keys(this.changed || {}); const isPropsCacheStillValid = !options.force && Boolean( changedKeys.length && changedKeys.every(key => ATTRIBUTES_THAT_DONT_INVALIDATE_PROPS_CACHE.has(key) ) ); if (isPropsCacheStillValid) { return; } if (this.cachedProps) { this.oldCachedProps = this.cachedProps; } this.cachedProps = null; this.trigger('props-change', this, this.isInReduxBatch); } ); // Set `isFetchingUUID` eagerly to avoid UI flicker when opening the // conversation for the first time. this.isFetchingUUID = this.isSMSOnly(); this.throttledBumpTyping = throttle(this.bumpTyping, 300); this.throttledUpdateSharedGroups = throttle( this.updateSharedGroups.bind(this), FIVE_MINUTES ); this.throttledFetchSMSOnlyUUID = throttle( this.fetchSMSOnlyUUID.bind(this), FIVE_MINUTES ); this.throttledMaybeMigrateV1Group = throttle( this.maybeMigrateV1Group.bind(this), FIVE_MINUTES ); this.throttledGetProfiles = throttle( this.getProfiles.bind(this), FIVE_MINUTES ); this.throttledUpdateVerified = throttle( this.updateVerified.bind(this), SECOND ); this.on('newmessage', this.throttledUpdateVerified); const migratedColor = this.getColor(); if (this.get('color') !== migratedColor) { this.set('color', migratedColor); // Not saving the conversation here we're hoping it'll be saved elsewhere // this may cause some color thrashing if Signal is restarted without // the convo saving. If that is indeed the case and it's too disruptive // we should add batched saving. } } toSenderKeyTarget(): SenderKeyTargetType { return { getGroupId: () => this.get('groupId'), getMembers: () => this.getMembers(), hasMember: (serviceId: ServiceIdString) => this.hasMember(serviceId), idForLogging: () => this.idForLogging(), isGroupV2: () => isGroupV2(this.attributes), isValid: () => isGroupV2(this.attributes), getSenderKeyInfo: () => this.get('senderKeyInfo'), saveSenderKeyInfo: async (senderKeyInfo: SenderKeyInfoType) => { this.set({ senderKeyInfo }); window.Signal.Data.updateConversation(this.attributes); }, }; } private get verifiedEnum(): typeof window.textsecure.storage.protocol.VerifiedStatus { strictAssert(this.privVerifiedEnum, 'ConversationModel not initialize'); return this.privVerifiedEnum; } private isMemberRequestingToJoin(serviceId: ServiceIdString): boolean { return isMemberRequestingToJoin(this.attributes, serviceId); } isMemberPending(serviceId: ServiceIdString): boolean { return isMemberPending(this.attributes, serviceId); } isMemberAwaitingApproval(serviceId: ServiceIdString): boolean { return isMemberAwaitingApproval(this.attributes, serviceId); } isMember(serviceId: ServiceIdString): boolean { return isMember(this.attributes, serviceId); } async updateExpirationTimerInGroupV2( seconds?: DurationInSeconds ): Promise { const idLog = this.idForLogging(); const current = this.get('expireTimer'); const bothFalsey = Boolean(current) === false && Boolean(seconds) === false; if (current === seconds || bothFalsey) { log.warn( `updateExpirationTimerInGroupV2/${idLog}: Requested timer ${seconds} is unchanged from existing ${current}.` ); return undefined; } return window.Signal.Groups.buildDisappearingMessagesTimerChange({ expireTimer: seconds || DurationInSeconds.ZERO, group: this.attributes, }); } private async promotePendingMember( serviceIdKind: ServiceIdKind ): Promise { const idLog = this.idForLogging(); const us = window.ConversationController.getOurConversationOrThrow(); const serviceId = window.storage.user.getCheckedServiceId(serviceIdKind); // This user's pending state may have changed in the time between the user's // button press and when we get here. It's especially important to check here // in conflict/retry cases. if (!this.isMemberPending(serviceId)) { log.warn( `promotePendingMember/${idLog}: we are not a pending member of group. Returning early.` ); return undefined; } // We need the user's profileKeyCredential, which requires a roundtrip with the // server, and most definitely their profileKey. A getProfiles() call will // ensure that we have as much as we can get with the data we have. if (!us.get('profileKeyCredential')) { await us.getProfiles(); } const profileKeyCredentialBase64 = us.get('profileKeyCredential'); strictAssert(profileKeyCredentialBase64, 'Must have profileKeyCredential'); if (serviceIdKind === ServiceIdKind.ACI) { return window.Signal.Groups.buildPromoteMemberChange({ group: this.attributes, isPendingPniAciProfileKey: false, profileKeyCredentialBase64, serverPublicParamsBase64: window.getServerPublicParams(), }); } strictAssert( serviceIdKind === ServiceIdKind.PNI, 'Must be a PNI promotion' ); return window.Signal.Groups.buildPromoteMemberChange({ group: this.attributes, isPendingPniAciProfileKey: true, profileKeyCredentialBase64, serverPublicParamsBase64: window.getServerPublicParams(), }); } private async denyPendingApprovalRequest( aci: AciString ): Promise { const idLog = this.idForLogging(); // This user's pending state may have changed in the time between the user's // button press and when we get here. It's especially important to check here // in conflict/retry cases. if (!this.isMemberRequestingToJoin(aci)) { log.warn( `denyPendingApprovalRequest/${idLog}: ${aci} is not requesting ` + 'to join the group. Returning early.' ); return undefined; } const ourAci = window.textsecure.storage.user.getCheckedAci(); return window.Signal.Groups.buildDeletePendingAdminApprovalMemberChange({ group: this.attributes, ourAci, aci, }); } async addPendingApprovalRequest(): Promise< Proto.GroupChange.Actions | undefined > { const idLog = this.idForLogging(); // Hard-coded to our own ID, because you don't add other users for admin approval const conversationId = window.ConversationController.getOurConversationIdOrThrow(); const toRequest = window.ConversationController.get(conversationId); if (!toRequest) { throw new Error( `addPendingApprovalRequest/${idLog}: No conversation found for conversation ${conversationId}` ); } const serviceId = toRequest.getCheckedServiceId( `addPendingApprovalRequest/${idLog}` ); // We need the user's profileKeyCredential, which requires a roundtrip with the // server, and most definitely their profileKey. A getProfiles() call will // ensure that we have as much as we can get with the data we have. let profileKeyCredentialBase64 = toRequest.get('profileKeyCredential'); if (!profileKeyCredentialBase64) { await toRequest.getProfiles(); profileKeyCredentialBase64 = toRequest.get('profileKeyCredential'); if (!profileKeyCredentialBase64) { throw new Error( `promotePendingMember/${idLog}: No profileKeyCredential for conversation ${toRequest.idForLogging()}` ); } } // This user's pending state may have changed in the time between the user's // button press and when we get here. It's especially important to check here // in conflict/retry cases. if (this.isMemberAwaitingApproval(serviceId)) { log.warn( `addPendingApprovalRequest/${idLog}: ` + `${toRequest.idForLogging()} already in pending approval.` ); return undefined; } return window.Signal.Groups.buildAddPendingAdminApprovalMemberChange({ group: this.attributes, profileKeyCredentialBase64, serverPublicParamsBase64: window.getServerPublicParams(), }); } async addMember( serviceId: ServiceIdString ): Promise { const idLog = this.idForLogging(); const toRequest = window.ConversationController.get(serviceId); if (!toRequest) { throw new Error( `addMember/${idLog}: No conversation found for ${serviceId}` ); } // We need the user's profileKeyCredential, which requires a roundtrip with the // server, and most definitely their profileKey. A getProfiles() call will // ensure that we have as much as we can get with the data we have. let profileKeyCredentialBase64 = toRequest.get('profileKeyCredential'); if (!profileKeyCredentialBase64) { await toRequest.getProfiles(); profileKeyCredentialBase64 = toRequest.get('profileKeyCredential'); if (!profileKeyCredentialBase64) { throw new Error( `addMember/${idLog}: No profileKeyCredential for conversation ${toRequest.idForLogging()}` ); } } // This user's pending state may have changed in the time between the user's // button press and when we get here. It's especially important to check here // in conflict/retry cases. if (this.isMember(serviceId)) { log.warn( `addMember/${idLog}: ${toRequest.idForLogging()} ` + 'is already a member.' ); return undefined; } return window.Signal.Groups.buildAddMember({ group: this.attributes, profileKeyCredentialBase64, serverPublicParamsBase64: window.getServerPublicParams(), serviceId, }); } private async removePendingMember( serviceIds: ReadonlyArray ): Promise { return removePendingMember(this.attributes, serviceIds); } private async removeMember( serviceId: ServiceIdString ): Promise { const idLog = this.idForLogging(); // This user's pending state may have changed in the time between the user's // button press and when we get here. It's especially important to check here // in conflict/retry cases. if (!this.isMember(serviceId)) { log.warn( `removeMember/${idLog}: ${serviceId} is not a pending member of group. Returning early.` ); return undefined; } const ourAci = window.textsecure.storage.user.getCheckedAci(); return window.Signal.Groups.buildDeleteMemberChange({ group: this.attributes, ourAci, serviceId, }); } private async toggleAdminChange( serviceId: ServiceIdString ): Promise { if (!isGroupV2(this.attributes)) { return undefined; } const idLog = this.idForLogging(); if (!this.isMember(serviceId)) { log.warn( `toggleAdminChange/${idLog}: ${serviceId} is not a pending member of group. Returning early.` ); return undefined; } const MEMBER_ROLES = Proto.Member.Role; const role = this.isAdmin(serviceId) ? MEMBER_ROLES.DEFAULT : MEMBER_ROLES.ADMINISTRATOR; return window.Signal.Groups.buildModifyMemberRoleChange({ group: this.attributes, serviceId, role, }); } async modifyGroupV2({ usingCredentialsFrom, createGroupChange, extraConversationsForSend, inviteLinkPassword, name, syncMessageOnly, }: { usingCredentialsFrom: ReadonlyArray; createGroupChange: () => Promise; extraConversationsForSend?: ReadonlyArray; inviteLinkPassword?: string; name: string; syncMessageOnly?: boolean; }): Promise { await window.Signal.Groups.modifyGroupV2({ conversation: this, usingCredentialsFrom, createGroupChange, extraConversationsForSend, inviteLinkPassword, name, syncMessageOnly, }); } isEverUnregistered(): boolean { return isConversationEverUnregistered(this.attributes); } isUnregistered(): boolean { return isConversationUnregistered(this.attributes); } isUnregisteredAndStale(): boolean { return isConversationUnregisteredAndStale(this.attributes); } isSMSOnly(): boolean { return isConversationSMSOnly({ ...this.attributes, type: isDirectConversation(this.attributes) ? 'direct' : 'unknown', }); } setUnregistered({ timestamp = Date.now(), fromStorageService = false, shouldSave = true, }: { timestamp?: number; fromStorageService?: boolean; shouldSave?: boolean; } = {}): void { log.info( `setUnregistered(${this.idForLogging()}): conversation is now ` + `unregistered, timestamp=${timestamp}` ); const oldFirstUnregisteredAt = this.get('firstUnregisteredAt'); this.set({ // We always keep the latest `discoveredUnregisteredAt` because if it // was less than 6 hours ago - `isUnregistered()` has to return `false` // and let us retry sends. discoveredUnregisteredAt: Math.max( this.get('discoveredUnregisteredAt') ?? timestamp, timestamp ), // Here we keep the oldest `firstUnregisteredAt` unless timestamp is // coming from storage service where remote value always wins. firstUnregisteredAt: fromStorageService ? timestamp : Math.min(this.get('firstUnregisteredAt') ?? timestamp, timestamp), }); if (shouldSave) { window.Signal.Data.updateConversation(this.attributes); } const e164 = this.get('e164'); const pni = this.getPni(); const aci = this.getServiceId(); if (e164 && pni && aci && pni !== aci) { this.updateE164(undefined); this.updatePni(undefined, false); const { conversation: split } = window.ConversationController.maybeMergeContacts({ pni, e164, reason: `ConversationModel.setUnregistered(${aci})`, }); log.info( `setUnregistered(${this.idForLogging()}): splitting pni ${pni} and ` + `e164 ${e164} into a separate conversation ${split.idForLogging()}` ); } if ( !fromStorageService && oldFirstUnregisteredAt !== this.get('firstUnregisteredAt') ) { this.captureChange('setUnregistered'); } } setRegistered({ shouldSave = true, fromStorageService = false, }: { shouldSave?: boolean; fromStorageService?: boolean; } = {}): void { if ( this.get('discoveredUnregisteredAt') === undefined && this.get('firstUnregisteredAt') === undefined ) { return; } const oldFirstUnregisteredAt = this.get('firstUnregisteredAt'); log.info(`Conversation ${this.idForLogging()} is registered once again`); this.set({ discoveredUnregisteredAt: undefined, firstUnregisteredAt: undefined, }); if (shouldSave) { window.Signal.Data.updateConversation(this.attributes); } if ( !fromStorageService && oldFirstUnregisteredAt !== this.get('firstUnregisteredAt') ) { this.captureChange('setRegistered'); } } isGroupV1AndDisabled(): boolean { return isGroupV1(this.attributes); } isBlocked(): boolean { return isBlocked(this.attributes); } block({ viaStorageServiceSync = false } = {}): void { let blocked = false; const wasBlocked = this.isBlocked(); const serviceId = this.getServiceId(); if (serviceId) { drop(window.storage.blocked.addBlockedServiceId(serviceId)); blocked = true; } const e164 = this.get('e164'); if (e164) { drop(window.storage.blocked.addBlockedNumber(e164)); blocked = true; } const groupId = this.get('groupId'); if (groupId) { drop(window.storage.blocked.addBlockedGroup(groupId)); blocked = true; } if (blocked && !wasBlocked) { // We need to force a props refresh - blocked state is not in backbone attributes this.trigger('change', this, { force: true }); if (!viaStorageServiceSync) { this.captureChange('block'); } } } unblock({ viaStorageServiceSync = false } = {}): boolean { let unblocked = false; const wasBlocked = this.isBlocked(); const serviceId = this.getServiceId(); if (serviceId) { drop(window.storage.blocked.removeBlockedServiceId(serviceId)); unblocked = true; } const e164 = this.get('e164'); if (e164) { drop(window.storage.blocked.removeBlockedNumber(e164)); unblocked = true; } const groupId = this.get('groupId'); if (groupId) { drop(window.storage.blocked.removeBlockedGroup(groupId)); unblocked = true; } if (unblocked && wasBlocked) { // We need to force a props refresh - blocked state is not in backbone attributes this.trigger('change', this, { force: true }); if (!viaStorageServiceSync) { this.captureChange('unblock'); } void this.fetchLatestGroupV2Data({ force: true }); } return unblocked; } async removeContact({ viaStorageServiceSync = false, shouldSave = true, } = {}): Promise { const logId = `removeContact(${this.idForLogging()}) storage? ${viaStorageServiceSync}`; if (!isDirectConversation(this.attributes)) { log.warn(`${logId}: not direct conversation`); return; } if (this.get('removalStage')) { log.warn(`${logId}: already removed`); return; } // Don't show message request state until first incoming message. log.info(`${logId}: updating`); this.set({ removalStage: 'justNotification' }); if (!viaStorageServiceSync) { this.captureChange('removeContact'); } this.disableProfileSharing({ viaStorageServiceSync }); // Drop existing message request state to avoid sending receipts and // display MR actions. const messageRequestEnum = Proto.SyncMessage.MessageRequestResponse.Type; await this.applyMessageRequestResponse(messageRequestEnum.UNKNOWN, { viaStorageServiceSync, shouldSave: false, }); window.reduxActions?.stories.removeAllContactStories(this.id); const serviceId = this.getServiceId(); if (serviceId) { window.reduxActions?.storyDistributionLists.removeMemberFromAllDistributionLists( serviceId ); } // Add notification drop(this.queueJob('removeContact', () => this.maybeSetContactRemoved())); if (shouldSave) { await window.Signal.Data.updateConversation(this.attributes); } } async restoreContact({ viaStorageServiceSync = false, shouldSave = true, } = {}): Promise { const logId = `restoreContact(${this.idForLogging()}) storage? ${viaStorageServiceSync}`; if (!isDirectConversation(this.attributes)) { log.warn(`${logId}: not direct conversation`); return; } if (this.get('removalStage') === undefined) { if (!viaStorageServiceSync) { log.warn(`${logId}: not removed`); } return; } log.info(`${logId}: updating`); this.set({ removalStage: undefined }); if (!viaStorageServiceSync) { this.captureChange('restoreContact'); } // Remove notification since the conversation isn't hidden anymore await this.maybeClearContactRemoved(); if (shouldSave) { await window.Signal.Data.updateConversation(this.attributes); } } enableProfileSharing({ viaStorageServiceSync = false } = {}): void { log.info( `enableProfileSharing: ${this.idForLogging()} storage? ${viaStorageServiceSync}` ); const before = this.get('profileSharing'); this.set({ profileSharing: true }); const after = this.get('profileSharing'); if (!viaStorageServiceSync && Boolean(before) !== Boolean(after)) { this.captureChange('enableProfileSharing'); } } disableProfileSharing({ viaStorageServiceSync = false } = {}): void { log.info( `disableProfileSharing: ${this.idForLogging()} storage? ${viaStorageServiceSync}` ); const before = this.get('profileSharing'); this.set({ profileSharing: false }); const after = this.get('profileSharing'); if (!viaStorageServiceSync && Boolean(before) !== Boolean(after)) { this.captureChange('disableProfileSharing'); } } hasDraft(): boolean { return hasDraft(this.attributes); } getDraftPreview(): DraftPreviewType { return getDraftPreview(this.attributes); } bumpTyping(): void { // We don't send typing messages if the setting is disabled if (!window.Events.getTypingIndicatorSetting()) { return; } if (!this.typingRefreshTimer) { const isTyping = true; this.setTypingRefreshTimer(); void this.sendTypingMessage(isTyping); } this.setTypingPauseTimer(); } setTypingRefreshTimer(): void { clearTimeoutIfNecessary(this.typingRefreshTimer); this.typingRefreshTimer = setTimeout( this.onTypingRefreshTimeout.bind(this), 10 * 1000 ); } onTypingRefreshTimeout(): void { const isTyping = true; void this.sendTypingMessage(isTyping); // This timer will continue to reset itself until the pause timer stops it this.setTypingRefreshTimer(); } setTypingPauseTimer(): void { clearTimeoutIfNecessary(this.typingPauseTimer); this.typingPauseTimer = setTimeout( this.onTypingPauseTimeout.bind(this), 3 * 1000 ); } onTypingPauseTimeout(): void { const isTyping = false; void this.sendTypingMessage(isTyping); this.clearTypingTimers(); } clearTypingTimers(): void { clearTimeoutIfNecessary(this.typingPauseTimer); this.typingPauseTimer = null; clearTimeoutIfNecessary(this.typingRefreshTimer); this.typingRefreshTimer = null; } async fetchLatestGroupV2Data( options: { force?: boolean } = {} ): Promise { if (!isGroupV2(this.attributes)) { return; } await window.Signal.Groups.waitThenMaybeUpdateGroup({ force: options.force, conversation: this, }); } async fetchSMSOnlyUUID(): Promise { const { server } = window.textsecure; if (!server) { return; } if (!this.isSMSOnly()) { return; } log.info( `Fetching uuid for a sms-only conversation ${this.idForLogging()}` ); this.isFetchingUUID = true; this.trigger('change', this, { force: true }); try { // Attempt to fetch UUID await updateConversationsWithUuidLookup({ conversationController: window.ConversationController, conversations: [this], server, }); } finally { // No redux update here this.isFetchingUUID = false; this.trigger('change', this, { force: true }); log.info( `Done fetching uuid for a sms-only conversation ${this.idForLogging()}` ); } if (!this.getServiceId()) { return; } // On successful fetch - mark contact as registered. this.setRegistered(); } override isValid(): boolean { return ( isDirectConversation(this.attributes) || isGroupV1(this.attributes) || isGroupV2(this.attributes) ); } async maybeMigrateV1Group(): Promise { if (!isGroupV1(this.attributes)) { return; } const isMigrated = await window.Signal.Groups.hasV1GroupBeenMigrated(this); if (!isMigrated) { return; } await window.Signal.Groups.waitThenRespondToGroupV2Migration({ conversation: this, }); } maybeRepairGroupV2(data: { masterKey: string; secretParams: string; publicParams: string; }): void { if ( this.get('groupVersion') && this.get('masterKey') && this.get('secretParams') && this.get('publicParams') ) { return; } log.info(`Repairing GroupV2 conversation ${this.idForLogging()}`); const { masterKey, secretParams, publicParams } = data; this.set({ masterKey, secretParams, publicParams, groupVersion: 2 }); window.Signal.Data.updateConversation(this.attributes); } getGroupV2Info( options: Readonly< { groupChange?: Uint8Array } & ( | { includePendingMembers?: boolean; extraConversationsForSend?: ReadonlyArray; } | { members: ReadonlyArray } ) > = {} ): GroupV2InfoType | undefined { if (isDirectConversation(this.attributes) || !isGroupV2(this.attributes)) { return undefined; } return { masterKey: Bytes.fromBase64( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.get('masterKey')! ), // eslint-disable-next-line @typescript-eslint/no-non-null-assertion revision: this.get('revision')!, members: 'members' in options ? options.members : this.getRecipients(options), groupChange: options.groupChange, }; } getGroupIdBuffer(): Uint8Array | undefined { const groupIdString = this.get('groupId'); if (!groupIdString) { return undefined; } if (isGroupV1(this.attributes)) { return Bytes.fromBinary(groupIdString); } if (isGroupV2(this.attributes)) { return Bytes.fromBase64(groupIdString); } return undefined; } async sendTypingMessage(isTyping: boolean): Promise { const { messaging } = window.textsecure; if (!messaging) { return; } // We don't send typing messages to our other devices if (isMe(this.attributes)) { return; } // Coalesce multiple sendTypingMessage calls into one. // // `lastIsTyping` is set to the last `isTyping` value passed to the // `sendTypingMessage`. The first 'sendTypingMessage' job to run will // pick it and reset it back to `undefined` so that later jobs will // in effect be ignored. this.lastIsTyping = isTyping; await this.queueJob('sendTypingMessage', async () => { const groupMembers = this.getRecipients(); // We don't send typing messages if our recipients list is empty if (!isDirectConversation(this.attributes) && !groupMembers.length) { return; } if (this.lastIsTyping === undefined) { log.info(`sendTypingMessage(${this.idForLogging()}): ignoring`); return; } const recipientId = isDirectConversation(this.attributes) ? this.getSendTarget() : undefined; const groupId = this.getGroupIdBuffer(); const timestamp = Date.now(); const content = { recipientId, groupId, groupMembers, isTyping: this.lastIsTyping, timestamp, }; this.lastIsTyping = undefined; log.info( `sendTypingMessage(${this.idForLogging()}): sending ${content.isTyping}` ); const contentMessage = messaging.getTypingContentMessage(content); const { ContentHint } = Proto.UnidentifiedSenderMessage.Message; const sendOptions = { ...(await getSendOptions(this.attributes)), online: true, }; if (isDirectConversation(this.attributes)) { await handleMessageSend( messaging.sendMessageProtoAndWait({ contentHint: ContentHint.IMPLICIT, groupId: undefined, options: sendOptions, proto: contentMessage, recipients: groupMembers, timestamp, urgent: false, }), { messageIds: [], sendType: 'typing' } ); } else { await handleMessageSend( sendContentMessageToGroup({ contentHint: ContentHint.IMPLICIT, contentMessage, messageId: undefined, online: true, recipients: groupMembers, sendOptions, sendTarget: this.toSenderKeyTarget(), sendType: 'typing', timestamp, urgent: false, }), { messageIds: [], sendType: 'typing' } ); } }); } async onNewMessage(message: MessageModel): Promise { const serviceId = message.get('sourceServiceId'); const e164 = message.get('source'); const sourceDevice = message.get('sourceDevice'); const source = window.ConversationController.lookupOrCreate({ serviceId, e164, reason: 'ConversationModel.onNewMessage', }); if (source) { const typingToken = `${source.id}.${sourceDevice}`; // Clear typing indicator for a given contact if we receive a message from them this.clearContactTypingTimer(typingToken); } // If it's a group story reply or a story message, we don't want to update // the last message or add new messages to redux. const isGroupStoryReply = isGroup(this.attributes) && message.get('storyId'); if (isGroupStoryReply || isStory(message.attributes)) { return; } // Change to message request state if contact was removed and sent message. if ( this.get('removalStage') === 'justNotification' && isIncoming(message.attributes) ) { this.set({ removalStage: 'messageRequest', }); await this.maybeClearContactRemoved(); window.Signal.Data.updateConversation(this.attributes); } void this.addSingleMessage(message); } // New messages might arrive while we're in the middle of a bulk fetch from the // database. We'll wait until that is done before moving forward. async addSingleMessage( message: MessageModel, { isJustSent }: { isJustSent: boolean } = { isJustSent: false } ): Promise { await this.beforeAddSingleMessage(message); this.doAddSingleMessage(message, { isJustSent }); this.debouncedUpdateLastMessage(); } private async beforeAddSingleMessage(message: MessageModel): Promise { await message.hydrateStoryContext(undefined, { shouldSave: true }); if (!this.newMessageQueue) { this.newMessageQueue = new PQueue({ concurrency: 1, timeout: FETCH_TIMEOUT * 2, }); } // We use a queue here to ensure messages are added to the UI in the order received await this.newMessageQueue.add(async () => { await this.inProgressFetch; }); } private doAddSingleMessage( message: MessageModel, { isJustSent }: { isJustSent: boolean } ): void { const { messagesAdded } = window.reduxActions.conversations; const { conversations } = window.reduxStore.getState(); const { messagesByConversation } = conversations; const conversationId = this.id; const existingConversation = messagesByConversation[conversationId]; const newestId = existingConversation?.metrics?.newest?.id; const messageIds = existingConversation?.messageIds; const isLatestInMemory = newestId && messageIds && messageIds[messageIds.length - 1] === newestId; if (isJustSent && existingConversation && !isLatestInMemory) { // The message is being sent before the user has scrolled down to load the newest // messages into memory; in that case, we scroll the user all the way down by // loading the newest message drop(this.loadNewestMessages(newestId, undefined)); } else if ( // The message has to be not a story or has to be a story reply in direct // conversation. !isStory(message.attributes) && (message.get('storyId') == null || isDirectConversation(this.attributes)) ) { messagesAdded({ conversationId, messages: [{ ...message.attributes }], isActive: window.SignalContext.activeWindowService.isActive(), isJustSent, isNewMessage: true, }); } } private setInProgressFetch(): () => unknown { const logId = `setInProgressFetch(${this.idForLogging()})`; const start = Date.now(); let resolvePromise: (value?: unknown) => void; this.inProgressFetch = new Promise(resolve => { resolvePromise = resolve; }); let timeout: NodeJS.Timeout; const finish = () => { const duration = Date.now() - start; if (duration > 500) { log.warn(`${logId}: in progress fetch took ${duration}ms`); } resolvePromise(); clearTimeout(timeout); this.inProgressFetch = undefined; }; timeout = setTimeout(() => { log.warn(`${logId}: Calling finish manually after timeout`); finish(); }, FETCH_TIMEOUT); return finish; } async loadNewestMessages( newestMessageId: string | undefined, setFocus: boolean | undefined ): Promise { const logId = `loadNewestMessages/${this.idForLogging()}`; const { messagesReset, setMessageLoadingState } = window.reduxActions.conversations; const conversationId = this.id; setMessageLoadingState( conversationId, TimelineMessageLoadingState.DoingInitialLoad ); const finish = this.setInProgressFetch(); try { let scrollToLatestUnread = true; if (newestMessageId) { const newestInMemoryMessage = await getMessageById(newestMessageId); if (newestInMemoryMessage) { // If newest in-memory message is unread, scrolling down would mean going to // the very bottom, not the oldest unread. if (isMessageUnread(newestInMemoryMessage)) { scrollToLatestUnread = false; } } else { log.warn( `loadNewestMessages: did not find message ${newestMessageId}` ); } } const metrics = await getMessageMetricsForConversation({ conversationId, includeStoryReplies: !isGroup(this.attributes), }); // If this is a message request that has not yet been accepted, we always show the // oldest messages, to ensure that the ConversationHero is shown. We don't want to // scroll directly to the oldest message, because that could scroll the hero off // the screen. if ( !newestMessageId && !this.getAccepted() && this.get('removalStage') !== 'justNotification' && metrics.oldest ) { log.info(`${logId}: scrolling to oldest ${metrics.oldest.sent_at}`); void this.loadAndScroll(metrics.oldest.id, { disableScroll: true }); return; } if (scrollToLatestUnread && metrics.oldestUnseen) { log.info( `${logId}: scrolling to oldest unseen ${metrics.oldestUnseen.sent_at}` ); void this.loadAndScroll(metrics.oldestUnseen.id, { disableScroll: !setFocus, }); return; } const messages = await getOlderMessagesByConversation({ conversationId, includeStoryReplies: !isGroup(this.attributes), limit: MESSAGE_LOAD_CHUNK_SIZE, storyId: undefined, }); const cleaned: Array = await this.cleanModels(messages); const scrollToMessageId = setFocus && metrics.newest ? metrics.newest.id : undefined; log.info( `${logId}: loaded ${cleaned.length} messages, ` + `latest timestamp=${cleaned.at(-1)?.get('sent_at')}` ); // Because our `getOlderMessages` fetch above didn't specify a receivedAt, we got // the most recent N messages in the conversation. If it has a conflict with // metrics, fetched a bit before, that's likely a race condition. So we tell our // reducer to trust the message set we just fetched for determining if we have // the newest message loaded. const unboundedFetch = true; messagesReset({ conversationId, messages: cleaned.map((messageModel: MessageModel) => ({ ...messageModel.attributes, })), metrics, scrollToMessageId, unboundedFetch, }); } catch (error) { setMessageLoadingState(conversationId, undefined); throw error; } finally { finish(); } } async loadOlderMessages(oldestMessageId: string): Promise { const logId = `loadOlderMessages/${this.idForLogging()}`; const { messagesAdded, setMessageLoadingState, repairOldestMessage } = window.reduxActions.conversations; const conversationId = this.id; setMessageLoadingState( conversationId, TimelineMessageLoadingState.LoadingOlderMessages ); const finish = this.setInProgressFetch(); try { const message = await getMessageById(oldestMessageId); if (!message) { throw new Error(`${logId}: failed to load message ${oldestMessageId}`); } const receivedAt = message.received_at; const sentAt = message.sent_at; const models = await getOlderMessagesByConversation({ conversationId, includeStoryReplies: !isGroup(this.attributes), limit: MESSAGE_LOAD_CHUNK_SIZE, messageId: oldestMessageId, receivedAt, sentAt, storyId: undefined, }); if (models.length < 1) { log.warn(`${logId}: requested, but loaded no messages`); repairOldestMessage(conversationId); return; } const cleaned = await this.cleanModels(models); log.info( `${logId}: loaded ${cleaned.length} messages, ` + `first timestamp=${cleaned.at(0)?.get('sent_at')}` ); messagesAdded({ conversationId, messages: cleaned.map((messageModel: MessageModel) => ({ ...messageModel.attributes, })), isActive: window.SignalContext.activeWindowService.isActive(), isJustSent: false, isNewMessage: false, }); } catch (error) { setMessageLoadingState(conversationId, undefined); throw error; } finally { finish(); } } async loadNewerMessages(newestMessageId: string): Promise { const { messagesAdded, setMessageLoadingState, repairNewestMessage } = window.reduxActions.conversations; const conversationId = this.id; setMessageLoadingState( conversationId, TimelineMessageLoadingState.LoadingNewerMessages ); const finish = this.setInProgressFetch(); try { const message = await getMessageById(newestMessageId); if (!message) { throw new Error( `loadNewerMessages: failed to load message ${newestMessageId}` ); } const receivedAt = message.received_at; const sentAt = message.sent_at; const models = await getNewerMessagesByConversation({ conversationId, includeStoryReplies: !isGroup(this.attributes), limit: MESSAGE_LOAD_CHUNK_SIZE, receivedAt, sentAt, storyId: undefined, }); if (models.length < 1) { log.warn('loadNewerMessages: requested, but loaded no messages'); repairNewestMessage(conversationId); return; } const cleaned = await this.cleanModels(models); messagesAdded({ conversationId, messages: cleaned.map((messageModel: MessageModel) => ({ ...messageModel.attributes, })), isActive: window.SignalContext.activeWindowService.isActive(), isJustSent: false, isNewMessage: false, }); } catch (error) { setMessageLoadingState(conversationId, undefined); throw error; } finally { finish(); } } async loadAndScroll( messageId: string, options?: { disableScroll?: boolean } ): Promise { const { messagesReset, setMessageLoadingState } = window.reduxActions.conversations; const conversationId = this.id; setMessageLoadingState( conversationId, TimelineMessageLoadingState.DoingInitialLoad ); const finish = this.setInProgressFetch(); try { const message = await getMessageById(messageId); if (!message) { throw new Error( `loadMoreAndScroll: failed to load message ${messageId}` ); } const receivedAt = message.received_at; const sentAt = message.sent_at; const { older, newer, metrics } = await getConversationRangeCenteredOnMessage({ conversationId, includeStoryReplies: !isGroup(this.attributes), limit: MESSAGE_LOAD_CHUNK_SIZE, messageId, receivedAt, sentAt, storyId: undefined, }); const all = [...older, message, ...newer]; const cleaned: Array = await this.cleanModels(all); const scrollToMessageId = options && options.disableScroll ? undefined : messageId; messagesReset({ conversationId, messages: cleaned.map((messageModel: MessageModel) => ({ ...messageModel.attributes, })), metrics, scrollToMessageId, }); } catch (error) { setMessageLoadingState(conversationId, undefined); throw error; } finally { finish(); } } async cleanModels( messages: ReadonlyArray ): Promise> { const result = messages .filter(message => Boolean(message.id)) .map(message => window.MessageCache.__DEPRECATED$register( message.id, message, 'cleanModels' ) ); const eliminated = messages.length - result.length; if (eliminated > 0) { log.warn(`cleanModels: Eliminated ${eliminated} messages without an id`); } const ourAci = window.textsecure.storage.user.getCheckedAci(); let upgraded = 0; for (let max = result.length, i = 0; i < max; i += 1) { const message = result[i]; const { attributes } = message; const { schemaVersion } = attributes; if ((schemaVersion || 0) < Message.VERSION_NEEDED_FOR_DISPLAY) { // Yep, we really do want to wait for each of these // eslint-disable-next-line no-await-in-loop const upgradedMessage = await upgradeMessageSchema(attributes); message.set(upgradedMessage); // eslint-disable-next-line no-await-in-loop await window.Signal.Data.saveMessage(upgradedMessage, { ourAci }); upgraded += 1; } } if (upgraded > 0) { log.warn(`cleanModels: Upgraded schema of ${upgraded} messages`); } await Promise.all( result.map(model => model.hydrateStoryContext(undefined, { shouldSave: true }) ) ); return result; } format(): ConversationType { if (this.cachedProps) { return this.cachedProps; } const oldFormat = this.format; // We don't want to crash or have an infinite loop if we loop back into this function // again. We'll log a warning and returned old cached props or throw an error. this.format = () => { if (!this.oldCachedProps) { throw new Error( `Conversation.format()/${this.idForLogging()} reentrant call, no old cached props!` ); } const { stack } = new Error('for stack'); log.warn( `Conversation.format()/${this.idForLogging()} reentrant call! ${stack}` ); return this.oldCachedProps; }; try { const { oldCachedProps } = this; const newCachedProps = getConversation(this); if (oldCachedProps && isShallowEqual(oldCachedProps, newCachedProps)) { this.cachedProps = oldCachedProps; } else { this.cachedProps = newCachedProps; } return this.cachedProps; } finally { this.format = oldFormat; } } updateE164(e164?: string | null): void { const oldValue = this.get('e164'); if (e164 === oldValue) { return; } this.set('e164', e164 || undefined); // This user changed their phone number if (oldValue && e164 && this.get('sharingPhoneNumber')) { void this.addChangeNumberNotification(oldValue, e164); } window.Signal.Data.updateConversation(this.attributes); this.trigger('idUpdated', this, 'e164', oldValue); this.captureChange('updateE164'); } updateServiceId(serviceId?: ServiceIdString): void { const oldValue = this.getServiceId(); if (serviceId === oldValue) { return; } this.set( 'serviceId', serviceId ? normalizeServiceId(serviceId, 'Conversation.updateServiceId') : undefined ); window.Signal.Data.updateConversation(this.attributes); this.trigger('idUpdated', this, 'serviceId', oldValue); // We should delete the old sessions and identity information in all situations except // for the case where we need to do old and new PNI comparisons. We'll wait // for the PNI update to do that. if (oldValue && oldValue !== this.getPni()) { drop(window.textsecure.storage.protocol.removeIdentityKey(oldValue)); } this.captureChange('updateServiceId'); } trackPreviousIdentityKey(publicKey: Uint8Array): void { const logId = `trackPreviousIdentityKey/${this.idForLogging()}`; const identityKey = Bytes.toBase64(publicKey); if (!isDirectConversation(this.attributes)) { throw new Error(`${logId}: Called for non-private conversation`); } const existingIdentityKey = this.get('previousIdentityKey'); if (existingIdentityKey && existingIdentityKey !== identityKey) { log.warn( `${logId}: Already had previousIdentityKey, new one does not match` ); void this.addKeyChange('trackPreviousIdentityKey - change'); } log.warn(`${logId}: Setting new previousIdentityKey`); this.set({ previousIdentityKey: identityKey, }); window.Signal.Data.updateConversation(this.attributes); } updatePni(pni: PniString | undefined, pniSignatureVerified: boolean): void { const oldValue = this.getPni(); if (pni === oldValue) { return; } this.set( 'pni', pni ? normalizePni(pni, 'Conversation.updatePni') : undefined ); const newPniSignatureVerified = pni ? pniSignatureVerified : false; if (this.get('pniSignatureVerified') !== newPniSignatureVerified) { log.warn( `updatePni/${this.idForLogging()}: setting ` + `pniSignatureVerified to ${newPniSignatureVerified}` ); this.set('pniSignatureVerified', newPniSignatureVerified); this.captureChange('pniSignatureVerified'); } const pniIsPrimaryId = !this.getServiceId() || this.getServiceId() === oldValue || this.getServiceId() === pni; const haveSentMessage = Boolean( this.get('profileSharing') || this.get('sentMessageCount') ); if (oldValue && pniIsPrimaryId && haveSentMessage) { // We're going from an old PNI to a new PNI if (pni) { const oldIdentityRecord = window.textsecure.storage.protocol.getIdentityRecord(oldValue); const newIdentityRecord = window.textsecure.storage.protocol.getIdentityRecord(pni); if ( newIdentityRecord && oldIdentityRecord && !constantTimeEqual( oldIdentityRecord.publicKey, newIdentityRecord.publicKey ) ) { void this.addKeyChange('updatePni - change'); } else if (!newIdentityRecord && oldIdentityRecord) { this.trackPreviousIdentityKey(oldIdentityRecord.publicKey); } } // We're just dropping the PNI if (!pni) { const oldIdentityRecord = window.textsecure.storage.protocol.getIdentityRecord(oldValue); if (oldIdentityRecord) { this.trackPreviousIdentityKey(oldIdentityRecord.publicKey); } } } // If this PNI is going away or going to someone else, we'll delete all its sessions if (oldValue) { drop(window.textsecure.storage.protocol.removeIdentityKey(oldValue)); } if (pni && !this.getServiceId()) { log.warn( `updatePni/${this.idForLogging()}: pni field set to ${pni}, but service id field is empty!` ); } window.Signal.Data.updateConversation(this.attributes); this.trigger('idUpdated', this, 'pni', oldValue); this.captureChange('updatePni'); } updateGroupId(groupId?: string): void { const oldValue = this.get('groupId'); if (groupId && groupId !== oldValue) { this.set('groupId', groupId); window.Signal.Data.updateConversation(this.attributes); this.trigger('idUpdated', this, 'groupId', oldValue); } } async updateReportingToken(token?: Uint8Array): Promise { const oldValue = this.get('reportingToken'); const newValue = token ? Bytes.toBase64(token) : undefined; if (oldValue === newValue) { return; } this.set('reportingToken', newValue); await window.Signal.Data.updateConversation(this.attributes); } incrementMessageCount(): void { this.set({ messageCount: (this.get('messageCount') || 0) + 1, }); window.Signal.Data.updateConversation(this.attributes); } incrementSentMessageCount({ dry = false }: { dry?: boolean } = {}): | Partial | undefined { const needsTitleTransition = hasNumberTitle(this.attributes) || hasUsernameTitle(this.attributes); const update = { messageCount: (this.get('messageCount') || 0) + 1, sentMessageCount: (this.get('sentMessageCount') || 0) + 1, ...(needsTitleTransition ? { needsTitleTransition: true } : {}), }; if (dry) { return update; } this.set(update); window.Signal.Data.updateConversation(this.attributes); return undefined; } /** * This function is called when a message request is accepted in order to * handle sending read receipts and download any pending attachments. */ async handleReadAndDownloadAttachments( options: { isLocalAction?: boolean } = {} ): Promise { const { isLocalAction } = options; const ourAci = window.textsecure.storage.user.getCheckedAci(); let messages: Array | undefined; do { const first = messages ? messages[0] : undefined; // eslint-disable-next-line no-await-in-loop messages = await window.Signal.Data.getOlderMessagesByConversation({ conversationId: this.get('id'), includeStoryReplies: !isGroup(this.attributes), limit: 100, messageId: first ? first.id : undefined, receivedAt: first ? first.received_at : undefined, sentAt: first ? first.sent_at : undefined, storyId: undefined, }); if (!messages.length) { return; } const readMessages = messages.filter(m => !hasErrors(m) && isIncoming(m)); if (isLocalAction) { const conversationId = this.get('id'); // eslint-disable-next-line no-await-in-loop await conversationJobQueue.add({ type: conversationQueueJobEnum.enum.Receipts, conversationId: this.get('id'), receiptsType: ReceiptType.Read, receipts: readMessages.map(m => { const { sourceServiceId: senderAci } = m; strictAssert(isAciString(senderAci), "Can't send receipt to PNI"); return { messageId: m.id, conversationId, senderE164: m.source, senderAci, timestamp: getMessageSentTimestamp(m, { log }), isDirectConversation: isDirectConversation(this.attributes), }; }), }); } // eslint-disable-next-line no-await-in-loop await Promise.all( readMessages.map(async m => { const registered = window.MessageCache.__DEPRECATED$register( m.id, m, 'handleReadAndDownloadAttachments' ); const shouldSave = await registered.queueAttachmentDownloads(); if (shouldSave) { await window.Signal.Data.saveMessage(registered.attributes, { ourAci, }); } }) ); } while (messages.length > 0); } async addMessageRequestResponseEventMessage( event: MessageRequestResponseEvent ): Promise { const timestamp = Date.now(); const lastMessageTimestamp = // Fallback to `timestamp` since `lastMessageReceivedAtMs` is new this.get('lastMessageReceivedAtMs') ?? this.get('timestamp') ?? timestamp; const maybeLastMessageTimestamp = event === MessageRequestResponseEvent.ACCEPT ? timestamp : lastMessageTimestamp; const message: MessageAttributesType = { id: generateGuid(), conversationId: this.id, type: 'message-request-response-event', sent_at: maybeLastMessageTimestamp, received_at: incrementMessageCounter(), received_at_ms: maybeLastMessageTimestamp, readStatus: ReadStatus.Read, seenStatus: SeenStatus.NotApplicable, timestamp, messageRequestResponseEvent: event, }; const id = await window.Signal.Data.saveMessage(message, { ourAci: window.textsecure.storage.user.getCheckedAci(), forceSave: true, }); const model = new window.Whisper.Message({ ...message, id, }); window.MessageCache.toMessageAttributes(model.attributes); this.trigger('newmessage', model); drop(this.updateLastMessage()); } async applyMessageRequestResponse( response: Proto.SyncMessage.MessageRequestResponse.Type, { fromSync = false, viaStorageServiceSync = false, shouldSave = true } = {} ): Promise { try { const messageRequestEnum = Proto.SyncMessage.MessageRequestResponse.Type; const isLocalAction = !fromSync && !viaStorageServiceSync; const currentMessageRequestState = this.get('messageRequestResponseType'); const didResponseChange = response !== currentMessageRequestState; const wasPreviouslyAccepted = this.getAccepted(); const didUnblock = response === messageRequestEnum.ACCEPT && this.isBlocked(); if (didResponseChange) { if (response === messageRequestEnum.ACCEPT) { // Only add a message when the user took an explicit action to accept // the message request on one of their devices if (!viaStorageServiceSync) { drop( this.addMessageRequestResponseEventMessage( didUnblock ? MessageRequestResponseEvent.UNBLOCK : MessageRequestResponseEvent.ACCEPT ) ); } } if ( response === messageRequestEnum.BLOCK || response === messageRequestEnum.BLOCK_AND_SPAM || response === messageRequestEnum.BLOCK_AND_DELETE ) { drop( this.addMessageRequestResponseEventMessage( MessageRequestResponseEvent.BLOCK ) ); } if ( response === messageRequestEnum.SPAM || response === messageRequestEnum.BLOCK_AND_SPAM ) { drop( this.addMessageRequestResponseEventMessage( MessageRequestResponseEvent.SPAM ) ); } } // Apply message request response locally this.set({ messageRequestResponseType: response, }); const rejectConversation = async ({ isBlock = false, isDelete = false, isSpam = false, }: { isBlock?: boolean; isDelete?: boolean; isSpam?: boolean; }) => { if (isBlock) { this.block({ viaStorageServiceSync }); } if (isBlock || isDelete) { this.disableProfileSharing({ viaStorageServiceSync }); } if (isDelete) { await this.destroyMessages({ source: 'message-request' }); void this.updateLastMessage(); } if (isBlock || isDelete) { if (isLocalAction) { window.reduxActions.conversations.onConversationClosed( this.id, isBlock ? 'blocked from message request' : 'deleted from message request' ); if (isGroupV2(this.attributes)) { await this.leaveGroupV2(); } } } if (isSpam) { this.set({ isReported: true }); } }; if (response === messageRequestEnum.ACCEPT) { this.unblock({ viaStorageServiceSync }); if (!viaStorageServiceSync) { await this.restoreContact({ shouldSave: false }); } this.enableProfileSharing({ viaStorageServiceSync }); // We really don't want to call this if we don't have to. It can take a lot of // time to go through old messages to download attachments. if (didResponseChange && !wasPreviouslyAccepted) { await this.handleReadAndDownloadAttachments({ isLocalAction }); } if (isLocalAction) { const ourAci = window.textsecure.storage.user.getCheckedAci(); const ourPni = window.textsecure.storage.user.getPni(); const ourConversation = window.ConversationController.getOurConversationOrThrow(); if ( isGroupV1(this.attributes) || isDirectConversation(this.attributes) ) { void this.sendProfileKeyUpdate(); } else if ( isGroupV2(this.attributes) && this.isMemberPending(ourAci) ) { await this.modifyGroupV2({ name: 'promotePendingMember', usingCredentialsFrom: [ourConversation], createGroupChange: () => this.promotePendingMember(ServiceIdKind.ACI), }); } else if ( ourPni && isGroupV2(this.attributes) && this.isMemberPending(ourPni) ) { await this.modifyGroupV2({ name: 'promotePendingMember', usingCredentialsFrom: [ourConversation], createGroupChange: () => this.promotePendingMember(ServiceIdKind.PNI), }); } else if (isGroupV2(this.attributes) && this.isMember(ourAci)) { log.info( 'applyMessageRequestResponse/accept: Already a member of v2 group' ); } else { log.error( 'applyMessageRequestResponse/accept: Neither member nor pending member of v2 group' ); } } } else if (response === messageRequestEnum.BLOCK) { await rejectConversation({ isBlock: true }); } else if (response === messageRequestEnum.DELETE) { await rejectConversation({ isDelete: true }); } else if (response === messageRequestEnum.BLOCK_AND_DELETE) { await rejectConversation({ isBlock: true, isDelete: true }); } else if (response === messageRequestEnum.SPAM) { await rejectConversation({ isSpam: true }); } else if (response === messageRequestEnum.BLOCK_AND_SPAM) { await rejectConversation({ isBlock: true, isSpam: true }); } } finally { if (shouldSave) { window.Signal.Data.updateConversation(this.attributes); } } } async joinGroupV2ViaLinkAndMigrate({ approvalRequired, inviteLinkPassword, revision, }: { approvalRequired: boolean; inviteLinkPassword: string; revision: number; }): Promise { await window.Signal.Groups.joinGroupV2ViaLinkAndMigrate({ approvalRequired, conversation: this, inviteLinkPassword, revision, }); } async joinGroupV2ViaLink({ inviteLinkPassword, approvalRequired, }: { inviteLinkPassword: string; approvalRequired: boolean; }): Promise { const ourAci = window.textsecure.storage.user.getCheckedAci(); const ourConversation = window.ConversationController.getOurConversationOrThrow(); try { if (approvalRequired) { await this.modifyGroupV2({ name: 'requestToJoin', usingCredentialsFrom: [ourConversation], inviteLinkPassword, createGroupChange: () => this.addPendingApprovalRequest(), }); } else { await this.modifyGroupV2({ name: 'joinGroup', usingCredentialsFrom: [ourConversation], inviteLinkPassword, createGroupChange: () => this.addMember(ourAci), }); } } catch (error) { const ALREADY_REQUESTED_TO_JOIN = '{"code":400,"message":"cannot ask to join via invite link if already asked to join"}'; if (!error.response) { throw error; } else { const errorDetails = Bytes.toString(error.response); if (errorDetails !== ALREADY_REQUESTED_TO_JOIN) { throw error; } else { log.info( 'joinGroupV2ViaLink: Got 400, but server is telling us we have already requested to join. Forcing that local state' ); this.set({ pendingAdminApprovalV2: [ { aci: ourAci, timestamp: Date.now(), }, ], }); } } } const messageRequestEnum = Proto.SyncMessage.MessageRequestResponse.Type; // Ensure active_at is set, because this is an event that justifies putting the group // in the left pane. this.set({ messageRequestResponseType: messageRequestEnum.ACCEPT, active_at: this.get('active_at') || Date.now(), }); window.Signal.Data.updateConversation(this.attributes); } async cancelJoinRequest(): Promise { const ourAci = window.storage.user.getCheckedAci(); const inviteLinkPassword = this.get('groupInviteLinkPassword'); if (!inviteLinkPassword) { log.warn( `cancelJoinRequest/${this.idForLogging()}: We don't have an inviteLinkPassword!` ); } await this.modifyGroupV2({ name: 'cancelJoinRequest', usingCredentialsFrom: [], inviteLinkPassword, createGroupChange: () => this.denyPendingApprovalRequest(ourAci), }); } async leaveGroupV2(): Promise { if (!isGroupV2(this.attributes)) { return; } const ourAci = window.textsecure.storage.user.getCheckedAci(); const ourPni = window.textsecure.storage.user.getPni(); const ourConversation = window.ConversationController.getOurConversationOrThrow(); if (this.isMemberPending(ourAci)) { await this.modifyGroupV2({ name: 'delete', usingCredentialsFrom: [], createGroupChange: () => this.removePendingMember([ourAci]), }); } else if (this.isMember(ourAci)) { await this.modifyGroupV2({ name: 'delete', usingCredentialsFrom: [ourConversation], createGroupChange: () => this.removeMember(ourAci), }); // Keep PNI in pending if ACI was a member. } else if (ourPni && this.isMemberPending(ourPni)) { await this.modifyGroupV2({ name: 'delete', usingCredentialsFrom: [], createGroupChange: () => this.removePendingMember([ourPni]), syncMessageOnly: true, }); } else { const logId = this.idForLogging(); log.error( 'leaveGroupV2: We were neither a member nor a pending member of ' + `the group ${logId}` ); } } async addBannedMember( serviceId: ServiceIdString ): Promise { if (this.isMember(serviceId)) { log.warn('addBannedMember: Member is a part of the group!'); return; } if (this.isMemberPending(serviceId)) { log.warn('addBannedMember: Member is pending to be added to group!'); return; } if (isMemberBanned(this.attributes, serviceId)) { log.warn('addBannedMember: Member is already banned!'); return; } return window.Signal.Groups.buildAddBannedMemberChange({ group: this.attributes, serviceId, }); } async blockGroupLinkRequests(serviceId: ServiceIdString): Promise { await this.modifyGroupV2({ name: 'addBannedMember', usingCredentialsFrom: [], createGroupChange: async () => this.addBannedMember(serviceId), }); } async toggleAdmin(conversationId: string): Promise { if (!isGroupV2(this.attributes)) { return; } const logId = this.idForLogging(); const member = window.ConversationController.get(conversationId); if (!member) { log.error(`toggleAdmin/${logId}: ${conversationId} does not exist`); return; } const serviceId = member.getCheckedServiceId(`toggleAdmin/${logId}`); if (!this.isMember(serviceId)) { log.error( `toggleAdmin: Member ${conversationId} is not a member of the group` ); return; } await this.modifyGroupV2({ name: 'toggleAdmin', usingCredentialsFrom: [member], createGroupChange: () => this.toggleAdminChange(serviceId), }); } async removeFromGroupV2(conversationId: string): Promise { if (!isGroupV2(this.attributes)) { return; } const logId = this.idForLogging(); const pendingMember = window.ConversationController.get(conversationId); if (!pendingMember) { throw new Error( `removeFromGroupV2/${logId}: No conversation found for conversation ${conversationId}` ); } const serviceId = pendingMember.getCheckedServiceId( `removeFromGroupV2/${logId}` ); if (this.isMemberRequestingToJoin(serviceId)) { strictAssert(isAciString(serviceId), 'Requesting member is not ACI'); await this.modifyGroupV2({ name: 'denyPendingApprovalRequest', usingCredentialsFrom: [], createGroupChange: () => this.denyPendingApprovalRequest(serviceId), extraConversationsForSend: [conversationId], }); } else if (this.isMemberPending(serviceId)) { await this.modifyGroupV2({ name: 'removePendingMember', usingCredentialsFrom: [], createGroupChange: () => this.removePendingMember([serviceId]), extraConversationsForSend: [conversationId], }); } else if (this.isMember(serviceId)) { await this.modifyGroupV2({ name: 'removeFromGroup', usingCredentialsFrom: [pendingMember], createGroupChange: () => this.removeMember(serviceId), extraConversationsForSend: [conversationId], }); } else { log.error( `removeFromGroupV2: Member ${conversationId} is neither a member nor a pending member of the group` ); } } async safeGetVerified(): Promise { const serviceId = this.getServiceId(); if (!serviceId) { return this.verifiedEnum.DEFAULT; } try { return await window.textsecure.storage.protocol.getVerified(serviceId); } catch { return this.verifiedEnum.DEFAULT; } } async updateVerified(): Promise { if (isDirectConversation(this.attributes)) { await this.initialPromise; const verified = await this.safeGetVerified(); const oldVerified = this.get('verified'); if (oldVerified !== verified) { this.set({ verified }); this.captureChange(`updateVerified from=${oldVerified} to=${verified}`); window.Signal.Data.updateConversation(this.attributes); } return; } this.fetchContacts(); await Promise.all( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.contactCollection!.map(async contact => { if (!isMe(contact.attributes)) { await contact.updateVerified(); } }) ); } setVerifiedDefault(): Promise { const { DEFAULT } = this.verifiedEnum; return this.queueJob('setVerifiedDefault', () => this._setVerified(DEFAULT) ); } setVerified(): Promise { const { VERIFIED } = this.verifiedEnum; return this.queueJob('setVerified', () => this._setVerified(VERIFIED)); } setUnverified(): Promise { const { UNVERIFIED } = this.verifiedEnum; return this.queueJob('setUnverified', () => this._setVerified(UNVERIFIED)); } private async _setVerified(verified: number): Promise { const { VERIFIED, DEFAULT } = this.verifiedEnum; if (!isDirectConversation(this.attributes)) { throw new Error( 'You cannot verify a group conversation. ' + 'You must verify individual contacts.' ); } const aci = this.getAci(); const beginningVerified = this.get('verified') ?? DEFAULT; const keyChange = false; if (aci) { if (verified === this.verifiedEnum.DEFAULT) { await window.textsecure.storage.protocol.setVerified(aci, verified); } else { await window.textsecure.storage.protocol.setVerified(aci, verified, { firstUse: false, nonblockingApproval: true, }); } } else { log.warn(`_setVerified(${this.id}): no aci to update protocol storage`); } this.set({ verified }); window.Signal.Data.updateConversation(this.attributes); if (beginningVerified !== verified) { this.captureChange( `_setVerified from=${beginningVerified} to=${verified}` ); } const didVerifiedChange = beginningVerified !== verified; const isExplicitUserAction = true; if ( // The message came from an explicit verification in a client (not // storage service sync) (didVerifiedChange && isExplicitUserAction) || // Our local verification status is VERIFIED and it hasn't changed, but the key did // change (Key1/VERIFIED -> Key2/VERIFIED), but we don't want to show DEFAULT -> // DEFAULT or UNVERIFIED -> UNVERIFIED (keyChange && verified === VERIFIED) ) { await this.addVerifiedChange(this.id, verified === VERIFIED, { local: isExplicitUserAction, }); } if (isExplicitUserAction && aci) { await this.sendVerifySyncMessage(this.get('e164'), aci, verified); } return keyChange; } async sendVerifySyncMessage( e164: string | undefined, aci: AciString, state: number ): Promise { if (window.ConversationController.areWePrimaryDevice()) { log.warn( 'sendVerifySyncMessage: We are primary device; not sending sync' ); return; } const key = await window.textsecure.storage.protocol.loadIdentityKey(aci); if (!key) { throw new Error( `sendVerifySyncMessage: No identity key found for aci ${aci}` ); } try { await singleProtoJobQueue.add( MessageSender.getVerificationSync(e164, aci, state, key) ); } catch (error) { log.error( 'sendVerifySyncMessage: Failed to queue sync message', Errors.toLogFormat(error) ); } } isVerified(): boolean { if (isDirectConversation(this.attributes)) { return this.get('verified') === this.verifiedEnum.VERIFIED; } const contacts = this.contactCollection; if (contacts == null || contacts.length === 0) { return false; } if (contacts.length === 1 && isMe(contacts.first()?.attributes)) { return false; } return contacts.every(contact => { if (isMe(contact.attributes)) { return true; } return contact.isVerified(); }); } isUnverified(): boolean { if (isDirectConversation(this.attributes)) { const verified = this.get('verified'); return ( verified !== this.verifiedEnum.VERIFIED && verified !== this.verifiedEnum.DEFAULT ); } if (!this.contactCollection?.length) { return true; } return this.contactCollection?.some(contact => { if (isMe(contact.attributes)) { return false; } return contact.isUnverified(); }); } getUnverified(): Array { if (isDirectConversation(this.attributes)) { return this.isUnverified() ? [this] : []; } return ( this.contactCollection?.filter(contact => { if (isMe(contact.attributes)) { return false; } return contact.isUnverified(); }) || [] ); } async setApproved(): Promise { if (!isDirectConversation(this.attributes)) { throw new Error( 'You cannot set a group conversation as trusted. ' + 'You must set individual contacts as trusted.' ); } const serviceId = this.getServiceId(); if (!serviceId) { log.warn(`setApproved(${this.id}): no serviceId, ignoring`); return; } return this.queueJob('setApproved', async () => { return window.textsecure.storage.protocol.setApproval(serviceId, true); }); } safeIsUntrusted(timestampThreshold?: number): boolean { try { const serviceId = this.getServiceId(); strictAssert(serviceId, `No serviceId for conversation: ${this.id}`); return window.textsecure.storage.protocol.isUntrusted( serviceId, timestampThreshold ); } catch (err) { return false; } } isUntrusted(timestampThreshold?: number): boolean { if (isDirectConversation(this.attributes)) { return this.safeIsUntrusted(timestampThreshold); } const { contactCollection } = this; if (!contactCollection?.length) { return false; } return contactCollection.some(contact => { if (isMe(contact.attributes)) { return false; } return contact.safeIsUntrusted(timestampThreshold); }); } getUntrusted(timestampThreshold?: number): Array { if (isDirectConversation(this.attributes)) { if (this.isUntrusted(timestampThreshold)) { return [this]; } return []; } return ( this.contactCollection?.filter(contact => { if (isMe(contact.attributes)) { return false; } return contact.isUntrusted(timestampThreshold); }) || [] ); } getSentMessageCount(): number { return this.get('sentMessageCount') || 0; } getMessageRequestResponseType(): number { return this.get('messageRequestResponseType') || 0; } getAboutText(): string | undefined { return getAboutText(this.attributes); } /** * Determine if this conversation should be considered "accepted" in terms * of message requests */ getAccepted(options?: IsConversationAcceptedOptionsType): boolean { return isConversationAccepted(this.attributes, options); } onMemberVerifiedChange(): void { // If the verified state of a member changes, our aggregate state changes. // We trigger both events to replicate the behavior of window.Backbone.Model.set() this.trigger('change:verified', this); this.trigger('change', this, { force: true }); } async toggleVerified(): Promise { if (this.isVerified()) { return this.setVerifiedDefault(); } return this.setVerified(); } async addChatSessionRefreshed({ receivedAt, receivedAtCounter, }: { receivedAt: number; receivedAtCounter: number; }): Promise { log.info(`addChatSessionRefreshed: adding for ${this.idForLogging()}`, { receivedAt, }); const message = { conversationId: this.id, type: 'chat-session-refreshed', sent_at: receivedAt, received_at: receivedAtCounter, received_at_ms: receivedAt, readStatus: ReadStatus.Unread, seenStatus: SeenStatus.Unseen, // TODO: DESKTOP-722 // this type does not fully implement the interface it is expected to } as unknown as MessageAttributesType; const id = await window.Signal.Data.saveMessage(message, { ourAci: window.textsecure.storage.user.getCheckedAci(), }); const model = window.MessageCache.__DEPRECATED$register( id, new window.Whisper.Message({ ...message, id, }), 'addChatSessionRefreshed' ); this.trigger('newmessage', model); void this.updateUnread(); } async addDeliveryIssue({ receivedAt, receivedAtCounter, senderAci, sentAt, }: { receivedAt: number; receivedAtCounter: number; senderAci: AciString; sentAt: number; }): Promise { log.info(`addDeliveryIssue: adding for ${this.idForLogging()}`, { sentAt, senderAci, }); if (!this.get('active_at')) { log.warn( `addDeliveryIssue: ${this.idForLogging()} has no active_at, dropping delivery issue instead of adding` ); return; } const message = { conversationId: this.id, type: 'delivery-issue', sourceServiceId: senderAci, sent_at: receivedAt, received_at: receivedAtCounter, received_at_ms: receivedAt, readStatus: ReadStatus.Unread, seenStatus: SeenStatus.Unseen, // TODO: DESKTOP-722 // this type does not fully implement the interface it is expected to } as unknown as MessageAttributesType; const id = await window.Signal.Data.saveMessage(message, { ourAci: window.textsecure.storage.user.getCheckedAci(), }); const model = window.MessageCache.__DEPRECATED$register( id, new window.Whisper.Message({ ...message, id, }), 'addDeliveryIssue' ); this.trigger('newmessage', model); await this.notify(model); void this.updateUnread(); } async addKeyChange( reason: string, keyChangedId?: ServiceIdString ): Promise { return this.queueJob(`addKeyChange(${keyChangedId})`, async () => { log.info( 'adding key change advisory in', this.idForLogging(), 'for', keyChangedId || 'this conversation', this.get('timestamp'), 'reason:', reason ); if (!keyChangedId && !isDirectConversation(this.attributes)) { throw new Error( 'addKeyChange: Cannot omit keyChangedId in group conversation!' ); } const timestamp = Date.now(); const message: MessageAttributesType = { id: generateGuid(), conversationId: this.id, type: 'keychange', sent_at: timestamp, timestamp, received_at: incrementMessageCounter(), received_at_ms: timestamp, key_changed: keyChangedId, readStatus: ReadStatus.Read, seenStatus: SeenStatus.Unseen, schemaVersion: Message.VERSION_NEEDED_FOR_DISPLAY, }; await window.Signal.Data.saveMessage(message, { ourAci: window.textsecure.storage.user.getCheckedAci(), forceSave: true, }); const model = window.MessageCache.__DEPRECATED$register( message.id, new window.Whisper.Message(message), 'addKeyChange' ); this.trigger('newmessage', model); const serviceId = this.getServiceId(); if (isDirectConversation(this.attributes)) { window.reduxActions?.safetyNumber.clearSafetyNumber(this.id); } if (isDirectConversation(this.attributes) && serviceId) { const groups = await window.ConversationController.getAllGroupsInvolvingServiceId( serviceId ); groups.forEach(group => { void group.addKeyChange('addKeyChange - group fan-out', serviceId); }); } // Reset sender key for next send const senderKeyInfo = this.get('senderKeyInfo'); if (senderKeyInfo) { await resetSenderKey(this.toSenderKeyTarget()); } if (isDirectConversation(this.attributes)) { this.captureChange(`addKeyChange(${reason})`); } }); } async addConversationMerge( renderInfo: ConversationRenderInfoType ): Promise { log.info( `addConversationMerge/${this.idForLogging()}: Adding notification` ); const timestamp = Date.now(); const message: MessageAttributesType = { id: generateGuid(), conversationId: this.id, type: 'conversation-merge', sent_at: timestamp, timestamp, received_at: incrementMessageCounter(), received_at_ms: timestamp, conversationMerge: { renderInfo, }, readStatus: ReadStatus.Read, seenStatus: SeenStatus.Unseen, schemaVersion: Message.VERSION_NEEDED_FOR_DISPLAY, }; const id = await window.Signal.Data.saveMessage(message, { ourAci: window.textsecure.storage.user.getCheckedAci(), forceSave: true, }); const model = window.MessageCache.__DEPRECATED$register( id, new window.Whisper.Message({ ...message, id, }), 'addConversationMerge' ); this.trigger('newmessage', model); } async addPhoneNumberDiscoveryIfNeeded(originalPni: PniString): Promise { const logId = `addPhoneNumberDiscoveryIfNeeded(${this.idForLogging()}, ${originalPni})`; const e164 = this.get('e164'); if (!e164) { log.info(`${logId}: not adding, no e164`); return; } const hadSession = await window.textsecure.storage.protocol.hasSessionWith( originalPni ); if (!hadSession) { log.info(`${logId}: not adding, no PNI session`); return; } log.info(`${logId}: adding notification`); const timestamp = Date.now(); const message: MessageAttributesType = { id: generateGuid(), conversationId: this.id, type: 'phone-number-discovery', sent_at: timestamp, timestamp, received_at: incrementMessageCounter(), received_at_ms: timestamp, phoneNumberDiscovery: { e164, }, readStatus: ReadStatus.Read, seenStatus: SeenStatus.Unseen, schemaVersion: Message.VERSION_NEEDED_FOR_DISPLAY, }; const id = await window.Signal.Data.saveMessage(message, { ourAci: window.textsecure.storage.user.getCheckedAci(), forceSave: true, }); const model = window.MessageCache.__DEPRECATED$register( id, new window.Whisper.Message({ ...message, id, }), 'addPhoneNumberDiscoveryIfNeeded' ); this.trigger('newmessage', model); } async addVerifiedChange( verifiedChangeId: string, verified: boolean, options: { local?: boolean } = { local: true } ): Promise { if (isMe(this.attributes)) { log.info('refusing to add verified change advisory for our own number'); return; } const lastMessage = this.get('timestamp') || Date.now(); log.info( 'adding verified change advisory for', this.idForLogging(), verifiedChangeId, lastMessage ); const timestamp = Date.now(); const message: MessageAttributesType = { id: generateGuid(), conversationId: this.id, local: Boolean(options.local), readStatus: ReadStatus.Read, received_at_ms: timestamp, received_at: incrementMessageCounter(), seenStatus: options.local ? SeenStatus.Seen : SeenStatus.Unseen, sent_at: lastMessage, timestamp, type: 'verified-change', verified, verifiedChanged: verifiedChangeId, }; await window.Signal.Data.saveMessage(message, { ourAci: window.textsecure.storage.user.getCheckedAci(), forceSave: true, }); const model = window.MessageCache.__DEPRECATED$register( message.id, new window.Whisper.Message(message), 'addVerifiedChange' ); this.trigger('newmessage', model); drop(this.updateUnread()); const serviceId = this.getServiceId(); if (isDirectConversation(this.attributes) && serviceId) { void window.ConversationController.getAllGroupsInvolvingServiceId( serviceId ).then(groups => { groups.forEach(group => { void group.addVerifiedChange(this.id, verified, options); }); }); } } async addProfileChange( profileChange: unknown, conversationId?: string ): Promise { const now = Date.now(); const message = { conversationId: this.id, type: 'profile-change', sent_at: now, received_at: incrementMessageCounter(), received_at_ms: now, readStatus: ReadStatus.Read, seenStatus: SeenStatus.NotApplicable, changedId: conversationId || this.id, profileChange, // TODO: DESKTOP-722 } as unknown as MessageAttributesType; const id = await window.Signal.Data.saveMessage(message, { ourAci: window.textsecure.storage.user.getCheckedAci(), }); const model = window.MessageCache.__DEPRECATED$register( id, new window.Whisper.Message({ ...message, id, }), 'addProfileChange' ); this.trigger('newmessage', model); const serviceId = this.getServiceId(); if (isDirectConversation(this.attributes) && serviceId) { this.set({ profileLastUpdatedAt: Date.now() }); void window.ConversationController.getAllGroupsInvolvingServiceId( serviceId ).then(groups => { groups.forEach(group => { void group.addProfileChange(profileChange, this.id); }); }); } } async addNotification( type: MessageAttributesType['type'], extra: Partial = {} ): Promise { const now = Date.now(); const message: Partial = { conversationId: this.id, type, sent_at: now, received_at: incrementMessageCounter(), received_at_ms: now, readStatus: ReadStatus.Read, seenStatus: SeenStatus.NotApplicable, ...extra, }; const id = await window.Signal.Data.saveMessage( // TODO: DESKTOP-722 message as MessageAttributesType, { ourAci: window.textsecure.storage.user.getCheckedAci(), } ); const model = window.MessageCache.__DEPRECATED$register( id, new window.Whisper.Message({ ...(message as MessageAttributesType), id, }), 'addNotification' ); this.trigger('newmessage', model); return id; } async maybeSetPendingUniversalTimer( hasUserInitiatedMessages: boolean ): Promise { if (!isDirectConversation(this.attributes)) { return; } if (this.isSMSOnly()) { return; } if (isSignalConversation(this.attributes)) { return; } if (hasUserInitiatedMessages) { await this.maybeRemoveUniversalTimer(); return; } if (this.get('pendingUniversalTimer') || this.get('expireTimer')) { return; } const expireTimer = universalExpireTimer.get(); if (!expireTimer) { return; } log.info( `maybeSetPendingUniversalTimer(${this.idForLogging()}): added notification` ); const notificationId = await this.addNotification( 'universal-timer-notification' ); this.set('pendingUniversalTimer', notificationId); } async maybeApplyUniversalTimer(): Promise { // Check if we had a notification if (!(await this.maybeRemoveUniversalTimer())) { return; } // We already have an expiration timer if (this.get('expireTimer')) { return; } const expireTimer = universalExpireTimer.get(); if (expireTimer) { log.info( `maybeApplyUniversalTimer(${this.idForLogging()}): applying timer` ); await this.updateExpirationTimer(expireTimer, { reason: 'maybeApplyUniversalTimer', }); } } async maybeRemoveUniversalTimer(): Promise { const notificationId = this.get('pendingUniversalTimer'); if (!notificationId) { return false; } this.set('pendingUniversalTimer', undefined); log.info( `maybeRemoveUniversalTimer(${this.idForLogging()}): removed notification` ); const message = window.MessageCache.__DEPRECATED$getById(notificationId); if (message) { await window.Signal.Data.removeMessage(message.id, { singleProtoJobQueue, }); } return true; } async maybeSetContactRemoved(): Promise { if (!isDirectConversation(this.attributes)) { return; } if (this.attributes.removalStage !== 'justNotification') { return; } if (this.get('pendingRemovedContactNotification')) { return; } log.info( `maybeSetContactRemoved(${this.idForLogging()}): added notification` ); const notificationId = await this.addNotification( 'contact-removed-notification' ); this.set('pendingRemovedContactNotification', notificationId); await window.Signal.Data.updateConversation(this.attributes); } async maybeClearContactRemoved(): Promise { const notificationId = this.get('pendingRemovedContactNotification'); if (!notificationId) { return false; } this.set('pendingRemovedContactNotification', undefined); log.info( `maybeClearContactRemoved(${this.idForLogging()}): removed notification` ); const message = window.MessageCache.__DEPRECATED$getById(notificationId); if (message) { await window.Signal.Data.removeMessage(message.id, { singleProtoJobQueue, }); } return true; } async addChangeNumberNotification( oldValue: string, newValue: string ): Promise { const sourceServiceId = this.getCheckedServiceId( 'Change number notification without service id' ); const { storage } = window.textsecure; if ( storage.user.getOurServiceIdKind(sourceServiceId) !== ServiceIdKind.Unknown ) { log.info( `Conversation ${this.idForLogging()}: not adding change number ` + 'notification for ourselves' ); return; } log.info( `Conversation ${this.idForLogging()}: adding change number ` + `notification for ${sourceServiceId} from ${oldValue} to ${newValue}` ); const convos = [ this, ...(await window.ConversationController.getAllGroupsInvolvingServiceId( sourceServiceId )), ]; await Promise.all( convos.map(convo => { return convo.addNotification('change-number-notification', { readStatus: ReadStatus.Read, seenStatus: SeenStatus.Unseen, sourceServiceId, }); }) ); } async onReadMessage( message: MessageModel, readAt?: number, newestSentAt?: number ): Promise { // We mark as read everything older than this message - to clean up old stuff // still marked unread in the database. If the user generally doesn't read in // the desktop app, so the desktop app only gets read syncs, we can very // easily end up with messages never marked as read (our previous early read // sync handling, read syncs never sent because app was offline) // We queue it because we often get a whole lot of read syncs at once, and // their markRead calls could very easily overlap given the async pull from DB. // Lastly, we don't send read syncs for any message marked read due to a read // sync. That's a notification explosion we don't need. return this.queueJob('onReadMessage', () => // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.markRead(message.get('received_at')!, { newestSentAt: newestSentAt || message.get('sent_at'), sendReadReceipts: false, readAt, }) ); } override validate(attributes = this.attributes): string | null { return validateConversation(attributes); } async queueJob( name: string, callback: (abortSignal: AbortSignal) => Promise ): Promise { const logId = `conversation.queueJob(${this.idForLogging()}, ${name})`; if (this.isShuttingDown) { log.warn(`${logId}: shutting down, can't accept more work`); throw new Error(`${logId}: shutting down, can't accept more work`); } this.jobQueue = this.jobQueue || new PQueue({ concurrency: 1 }); const taskWithTimeout = createTaskWithTimeout(callback, logId); const abortController = new AbortController(); const { signal: abortSignal } = abortController; const queuedAt = Date.now(); return this.jobQueue.add(async () => { const startedAt = Date.now(); const waitTime = startedAt - queuedAt; if (waitTime > JOB_REPORTING_THRESHOLD_MS) { log.info(`${logId}: was blocked for ${waitTime}ms`); } try { return await taskWithTimeout(abortSignal); } catch (error) { abortController.abort(); throw error; } finally { const duration = Date.now() - startedAt; if (duration > JOB_REPORTING_THRESHOLD_MS) { log.info(`${logId}: took ${duration}ms`); } } }); } isAdmin(serviceId: ServiceIdString): boolean { if (!isGroupV2(this.attributes)) { return false; } const members = this.get('membersV2') || []; const member = members.find(x => x.aci === serviceId); if (!member) { return false; } const MEMBER_ROLES = Proto.Member.Role; return member.role === MEMBER_ROLES.ADMINISTRATOR; } getServiceId(): ServiceIdString | undefined { return this.get('serviceId'); } getCheckedServiceId(reason: string): ServiceIdString { const serviceId = this.getServiceId(); strictAssert(serviceId !== undefined, reason); return serviceId; } getAci(): AciString | undefined { const value = this.getServiceId(); if (value && isAciString(value)) { return value; } return undefined; } getCheckedAci(reason: string): AciString { const aci = this.getAci(); strictAssert(aci !== undefined, reason); return aci; } getPni(): PniString | undefined { return this.get('pni'); } getGroupLink(): string | undefined { if (!isGroupV2(this.attributes)) { return undefined; } if (!this.get('groupInviteLinkPassword')) { return undefined; } return window.Signal.Groups.buildGroupLink(this.attributes); } getMembers( options: { includePendingMembers?: boolean } = {} ): Array { return compact( getConversationMembers(this.attributes, options).map(conversationAttrs => window.ConversationController.get(conversationAttrs.id) ) ); } canBeAnnouncementGroup(): boolean { if (!isGroupV2(this.attributes)) { return false; } return true; } getMemberIds(): Array { const members = this.getMembers(); return members.map(member => member.id); } getRecipients({ includePendingMembers, extraConversationsForSend, }: { includePendingMembers?: boolean; extraConversationsForSend?: ReadonlyArray; isStoryReply?: boolean; } = {}): Array { return getRecipients(this.attributes, { includePendingMembers, extraConversationsForSend, }); } // Members is all people in the group getMemberConversationIds(): Set { return new Set(map(this.getMembers(), conversation => conversation.id)); } async getQuoteAttachment( attachments?: Array, preview?: Array, sticker?: StickerType ): Promise< Array<{ contentType: MIMEType; fileName?: string | null; thumbnail?: ThumbnailType | null; }> > { return getQuoteAttachment(attachments, preview, sticker); } async sendStickerMessage(packId: string, stickerId: number): Promise { const packData = Stickers.getStickerPack(packId); const stickerData = Stickers.getSticker(packId, stickerId); if (!stickerData || !packData) { log.warn( `Attempted to send nonexistent (${packId}, ${stickerId}) sticker!` ); return; } const { key } = packData; const { emoji, width, height } = stickerData; const data = await readStickerData(stickerData); // We need this content type to be an image so we can display an `` instead of a // `