signal-desktop/ts/models/conversations.ts

5807 lines
167 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2022-11-30 00:53:39 +00:00
import {
compact,
has,
isNumber,
throttle,
debounce,
head,
sortBy,
} from 'lodash';
import { batch as batchDispatch } from 'react-redux';
import { v4 as generateGuid } from 'uuid';
2022-08-25 05:04:42 +00:00
import PQueue from 'p-queue';
import type {
2021-06-25 16:08:16 +00:00
ConversationAttributesType,
ConversationLastProfileType,
ConversationRenderInfoType,
MessageAttributesType,
2021-06-25 16:08:16 +00:00
QuotedMessageType,
SenderKeyInfoType,
} from '../model-types.d';
import { drop } from '../util/drop';
import { isShallowEqual } from '../util/isShallowEqual';
import { memoizeByThis } from '../util/memoizeByThis';
import { getInitials } from '../util/getInitials';
import { normalizeUuid } from '../util/normalizeUuid';
import { clearTimeoutIfNecessary } from '../util/clearTimeoutIfNecessary';
2022-07-13 00:37:21 +00:00
import type { AttachmentType, ThumbnailType } from '../types/Attachment';
2022-07-08 20:46:25 +00:00
import { toDayMillis } from '../util/timestamp';
import { isGIF } from '../types/Attachment';
import type { CallHistoryDetailsType } from '../types/Calling';
import { CallMode } from '../types/Calling';
2021-09-24 00:49:05 +00:00
import * as EmbeddedContact from '../types/EmbeddedContact';
import * as Conversation from '../types/Conversation';
2022-07-13 00:37:21 +00:00
import type { StickerType, StickerWithHydratedData } from '../types/Stickers';
2021-07-09 19:36:10 +00:00
import * as Stickers from '../types/Stickers';
import { StorySendMode } from '../types/Stories';
import type {
ContactWithHydratedAvatar,
GroupV1InfoType,
GroupV2InfoType,
} from '../textsecure/SendMessage';
import createTaskWithTimeout from '../textsecure/TaskWithTimeout';
import MessageSender from '../textsecure/SendMessage';
2022-08-15 21:53:33 +00:00
import type {
CallbackResultType,
PniSignatureMessageType,
} from '../textsecure/Types.d';
import type {
ConversationType,
LastMessageType,
} from '../state/ducks/conversations';
import type {
2021-05-28 16:15:17 +00:00
AvatarColorType,
ConversationColorType,
2021-06-02 21:05:09 +00:00
CustomColorType,
2021-05-28 16:15:17 +00:00
} from '../types/Colors';
import type { MessageModel } from './messages';
import { getContact } from '../messages/helpers';
2023-01-10 00:52:01 +00:00
import { assertDev, strictAssert } from '../util/assert';
2022-05-23 18:37:53 +00:00
import { isConversationMuted } from '../util/isConversationMuted';
import { isConversationSMSOnly } from '../util/isConversationSMSOnly';
import {
isConversationEverUnregistered,
isConversationUnregistered,
isConversationUnregisteredAndStale,
} from '../util/isConversationUnregistered';
import { missingCaseError } from '../util/missingCaseError';
2020-09-28 18:40:26 +00:00
import { sniffImageMimeType } from '../util/sniffImageMimeType';
2021-08-28 13:27:38 +00:00
import { isValidE164 } from '../util/isValidE164';
import type { MIMEType } from '../types/MIME';
import { IMAGE_JPEG, IMAGE_GIF, IMAGE_WEBP } from '../types/MIME';
2022-07-13 00:37:21 +00:00
import { UUID, UUIDKind } from '../types/UUID';
2021-10-26 22:59:08 +00:00
import type { UUIDStringType } from '../types/UUID';
import {
constantTimeEqual,
decryptProfile,
decryptProfileName,
deriveAccessKey,
} from '../Crypto';
2021-06-22 14:46:42 +00:00
import * as Bytes from '../Bytes';
import type { BodyRangesType, DraftBodyRangesType } from '../types/Util';
2021-09-24 00:49:05 +00:00
import { getTextWithMentions } from '../util/getTextWithMentions';
2021-01-07 18:06:09 +00:00
import { migrateColor } from '../util/migrateColor';
import { isNotNil } from '../util/isNotNil';
import { dropNull } from '../util/dropNull';
2021-09-23 18:16:09 +00:00
import { notificationService } from '../services/notifications';
import { storageServiceUploadJob } from '../services/storage';
import { getSendOptions } from '../util/getSendOptions';
import { isConversationAccepted } from '../util/isConversationAccepted';
import {
getNumber,
getProfileName,
getTitle,
getTitleNoDefault,
} 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 } from '../types/message/LinkPreviews';
import { MINUTE, SECOND, DurationInSeconds } from '../util/durations';
import {
concat,
filter,
map,
take,
repeat,
zipObject,
collect,
} from '../util/iterables';
2021-06-01 20:45:43 +00:00
import * as universalExpireTimer from '../util/universalExpireTimer';
import type { GroupNameCollisionsWithIdsByTitle } from '../util/groupMemberNameCollisions';
import {
isDirectConversation,
2022-04-20 23:33:38 +00:00
isGroup,
isGroupV1,
isGroupV2,
isMe,
} from '../util/whatTypeOfConversation';
2021-06-22 14:46:42 +00:00
import { SignalService as Proto } from '../protobuf';
import {
getMessagePropStatus,
hasErrors,
2022-05-11 20:59:58 +00:00
isGiftBadge,
isIncoming,
isStory,
isTapToView,
} from '../state/selectors/message';
2022-02-16 18:36:21 +00:00
import {
conversationJobQueue,
conversationQueueJobEnum,
} from '../jobs/conversationJobQueue';
import { readReceiptsJobQueue } from '../jobs/readReceiptsJobQueue';
import type { ReactionModel } from '../messageModifiers/Reactions';
import { isAnnouncementGroupReady } from '../util/isAnnouncementGroupReady';
import { getProfile } from '../util/getProfile';
import { SEALED_SENDER } from '../types/SealedSender';
2021-08-06 00:17:05 +00:00
import { getAvatarData } from '../util/getAvatarData';
2021-08-25 00:17:51 +00:00
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 { singleProtoJobQueue } from '../jobs/singleProtoJobQueue';
import { TimelineMessageLoadingState } from '../util/timelineUtil';
import { SeenStatus } from '../MessageSeenStatus';
import { getConversationIdForLogging } from '../util/idForLogging';
2022-07-01 00:52:03 +00:00
import { getSendTarget } from '../util/getSendTarget';
import { getRecipients } from '../util/getRecipients';
2022-07-13 00:37:21 +00:00
import { validateConversation } from '../util/validateConversation';
2022-11-09 02:38:19 +00:00
import { isSignalConversation } from '../util/isSignalConversation';
import { isMemberRequestingToJoin } from '../util/isMemberRequestingToJoin';
import { removePendingMember } from '../util/removePendingMember';
import { isMemberPending } from '../util/isMemberPending';
const EMPTY_ARRAY: Readonly<[]> = [];
const EMPTY_GROUP_COLLISIONS: GroupNameCollisionsWithIdsByTitle = {};
/* eslint-disable more/no-then */
window.Whisper = window.Whisper || {};
const { Util } = window.Signal;
2021-09-24 00:49:05 +00:00
const { Message } = window.Signal.Types;
const {
deleteAttachmentData,
doesAttachmentExist,
getAbsoluteAttachmentPath,
loadAttachmentData,
readStickerData,
upgradeMessageSchema,
writeNewAttachmentData,
} = window.Signal.Migrations;
const {
addStickerPackReference,
getConversationRangeCenteredOnMessage,
getOlderMessagesByConversation,
getMessageMetricsForConversation,
getMessageById,
getNewerMessagesByConversation,
} = window.Signal.Data;
2022-11-16 20:18:02 +00:00
const FIVE_MINUTES = MINUTE * 5;
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',
2022-02-08 18:00:18 +00:00
'storageVersion',
'storageUnknownFields',
]);
type CachedIdenticon = {
readonly url: string;
readonly content: string;
2021-05-28 16:15:17 +00:00
readonly color: AvatarColorType;
};
2021-04-26 16:38:50 +00:00
export class ConversationModel extends window.Backbone
.Model<ConversationAttributesType> {
static COLORS: string;
cachedProps?: ConversationType | null;
oldCachedProps?: ConversationType | null;
contactTypingTimers?: Record<
string,
{ senderId: string; timer: NodeJS.Timer }
>;
contactCollection?: Backbone.Collection<ConversationModel>;
debouncedUpdateLastMessage?: (() => void) & { flush(): void };
initialPromise?: Promise<unknown>;
inProgressFetch?: Promise<unknown>;
2022-08-25 05:04:42 +00:00
newMessageQueue?: PQueue;
2022-08-25 05:04:42 +00:00
jobQueue?: PQueue;
storeName?: string | null;
2021-08-30 21:32:56 +00:00
throttledBumpTyping?: () => void;
2023-01-02 21:34:41 +00:00
throttledFetchSMSOnlyUUID?: () => Promise<void> | undefined;
2023-01-02 21:34:41 +00:00
throttledMaybeMigrateV1Group?: () => Promise<void> | undefined;
2021-05-28 19:11:19 +00:00
2021-08-30 21:32:56 +00:00
throttledGetProfiles?: () => Promise<void>;
throttledUpdateVerified?: () => void;
typingRefreshTimer?: NodeJS.Timer | null;
typingPauseTimer?: NodeJS.Timer | null;
2021-03-02 16:27:11 +00:00
intlCollator = new Intl.Collator(undefined, { sensitivity: 'base' });
2020-11-03 01:19:52 +00:00
2021-05-07 20:07:24 +00:00
lastSuccessfulGroupFetch?: number;
throttledUpdateSharedGroups?: () => Promise<void>;
private cachedLatestGroupCallEraId?: string;
private cachedIdenticon?: CachedIdenticon;
private isFetchingUUID?: boolean;
private lastIsTyping?: boolean;
2021-10-06 21:59:34 +00:00
private muteTimer?: NodeJS.Timer;
private isInReduxBatch = false;
private privVerifiedEnum?: typeof window.textsecure.storage.protocol.VerifiedStatus;
override defaults(): Partial<ConversationAttributesType> {
return {
unreadCount: 0,
verified: window.textsecure.storage.protocol.VerifiedStatus.DEFAULT,
messageCount: 0,
sentMessageCount: 0,
};
}
idForLogging(): string {
return getConversationIdForLogging(this.attributes);
}
// This is one of the few times that we want to collapse our uuid/e164 pair down into
// just one bit of data. If we have a UUID, we'll send using it.
getSendTarget(): string | undefined {
2022-07-01 00:52:03 +00:00
return getSendTarget(this.attributes);
}
getContactCollection(): Backbone.Collection<ConversationModel> {
const collection = new window.Backbone.Collection<ConversationModel>();
2021-03-02 16:27:11 +00:00
const collator = new Intl.Collator(undefined, { sensitivity: 'base' });
collection.comparator = (
left: ConversationModel,
right: ConversationModel
) => {
2021-03-02 16:27:11 +00:00
return collator.compare(left.getTitle(), right.getTitle());
};
return collection;
}
override initialize(
attributes: Partial<ConversationAttributesType> = {}
): void {
const uuid = this.get('uuid');
const normalizedUuid =
uuid && normalizeUuid(uuid, 'ConversationModel.initialize');
if (uuid && normalizedUuid !== uuid) {
log.warn(
'ConversationModel.initialize: normalizing uuid from ' +
`${uuid} to ${normalizedUuid}`
);
this.set('uuid', normalizedUuid);
}
2021-08-28 13:27:38 +00:00
if (isValidE164(attributes.id, false)) {
2021-10-26 22:59:08 +00:00
this.set({ id: UUID.generate().toString(), 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);
const sealedSender = this.get('sealedSender');
if (sealedSender === undefined) {
this.set({ sealedSender: SEALED_SENDER.UNKNOWN });
}
this.unset('unidentifiedDelivery');
this.unset('unidentifiedDeliveryUnrestricted');
this.unset('hasFetchedProfile');
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(
2021-05-28 19:11:19 +00:00
this.fetchSMSOnlyUUID.bind(this),
FIVE_MINUTES
);
this.throttledMaybeMigrateV1Group = throttle(
2021-05-28 19:11:19 +00:00
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);
2021-05-28 16:15:17 +00:00
2021-08-06 00:17:05 +00:00
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.
2021-05-28 16:15:17 +00:00
}
}
toSenderKeyTarget(): SenderKeyTargetType {
return {
getGroupId: () => this.get('groupId'),
getMembers: () => this.getMembers(),
2022-07-08 20:46:25 +00:00
hasMember: (uuid: UUIDStringType) => this.hasMember(new UUID(uuid)),
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;
}
2022-07-08 20:46:25 +00:00
private isMemberRequestingToJoin(uuid: UUID): boolean {
return isMemberRequestingToJoin(this.attributes, uuid);
}
2022-07-08 20:46:25 +00:00
isMemberPending(uuid: UUID): boolean {
return isMemberPending(this.attributes, uuid);
}
2022-07-08 20:46:25 +00:00
private isMemberBanned(uuid: UUID): boolean {
if (!isGroupV2(this.attributes)) {
return false;
}
const bannedMembersV2 = this.get('bannedMembersV2');
if (!bannedMembersV2 || !bannedMembersV2.length) {
return false;
}
2022-07-08 20:46:25 +00:00
return bannedMembersV2.some(member => member.uuid === uuid.toString());
2020-10-06 17:06:34 +00:00
}
2022-07-08 20:46:25 +00:00
isMemberAwaitingApproval(uuid: UUID): boolean {
if (!isGroupV2(this.attributes)) {
return false;
}
const pendingAdminApprovalV2 = this.get('pendingAdminApprovalV2');
if (!pendingAdminApprovalV2 || !pendingAdminApprovalV2.length) {
return false;
}
2022-07-08 20:46:25 +00:00
return pendingAdminApprovalV2.some(
member => member.uuid === uuid.toString()
);
}
2022-07-08 20:46:25 +00:00
isMember(uuid: UUID): boolean {
if (!isGroupV2(this.attributes)) {
2022-07-08 20:46:25 +00:00
return false;
2020-10-06 17:06:34 +00:00
}
const membersV2 = this.get('membersV2');
if (!membersV2 || !membersV2.length) {
return false;
}
2022-11-30 00:53:39 +00:00
return membersV2.some(item => item.uuid === uuid.toString());
2020-10-06 17:06:34 +00:00
}
async updateExpirationTimerInGroupV2(
2022-11-16 20:18:02 +00:00
seconds?: DurationInSeconds
2021-06-22 14:46:42 +00:00
): Promise<Proto.GroupChange.Actions | undefined> {
2020-10-06 17:06:34 +00:00
const idLog = this.idForLogging();
const current = this.get('expireTimer');
const bothFalsey = Boolean(current) === false && Boolean(seconds) === false;
if (current === seconds || bothFalsey) {
log.warn(
2020-10-06 17:06:34 +00:00
`updateExpirationTimerInGroupV2/${idLog}: Requested timer ${seconds} is unchanged from existing ${current}.`
);
return undefined;
}
return window.Signal.Groups.buildDisappearingMessagesTimerChange({
2022-11-16 20:18:02 +00:00
expireTimer: seconds || DurationInSeconds.ZERO,
2020-10-06 17:06:34 +00:00
group: this.attributes,
});
}
2022-07-08 20:46:25 +00:00
private async promotePendingMember(
uuidKind: UUIDKind
2021-06-22 14:46:42 +00:00
): Promise<Proto.GroupChange.Actions | undefined> {
2020-10-06 17:06:34 +00:00
const idLog = this.idForLogging();
2022-07-08 20:46:25 +00:00
const us = window.ConversationController.getOurConversationOrThrow();
const uuid = window.storage.user.getCheckedUuid(uuidKind);
2020-10-06 17:06:34 +00:00
// 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.
2022-07-08 20:46:25 +00:00
if (!this.isMemberPending(uuid)) {
log.warn(
2022-07-08 20:46:25 +00:00
`promotePendingMember/${idLog}: we are not a pending member of group. Returning early.`
2020-10-06 17:06:34 +00:00
);
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.
2022-09-21 16:18:48 +00:00
if (!us.get('profileKeyCredential')) {
await us.getProfiles();
}
2022-07-08 20:46:25 +00:00
2022-09-21 16:18:48 +00:00
const profileKeyCredentialBase64 = us.get('profileKeyCredential');
strictAssert(profileKeyCredentialBase64, 'Must have profileKeyCredential');
2022-07-08 20:46:25 +00:00
2022-09-21 16:18:48 +00:00
if (uuidKind === UUIDKind.ACI) {
2022-07-08 20:46:25 +00:00
return window.Signal.Groups.buildPromoteMemberChange({
group: this.attributes,
2022-09-21 16:18:48 +00:00
isPendingPniAciProfileKey: false,
2022-07-08 20:46:25 +00:00
profileKeyCredentialBase64,
serverPublicParamsBase64: window.getServerPublicParams(),
});
}
strictAssert(uuidKind === UUIDKind.PNI, 'Must be a PNI promotion');
2020-10-06 17:06:34 +00:00
return window.Signal.Groups.buildPromoteMemberChange({
group: this.attributes,
2022-09-21 16:18:48 +00:00
isPendingPniAciProfileKey: true,
profileKeyCredentialBase64,
2020-10-06 17:06:34 +00:00
serverPublicParamsBase64: window.getServerPublicParams(),
});
}
2022-07-08 20:46:25 +00:00
private async denyPendingApprovalRequest(
uuid: UUID
2021-06-22 14:46:42 +00:00
): Promise<Proto.GroupChange.Actions | undefined> {
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.
2022-07-08 20:46:25 +00:00
if (!this.isMemberRequestingToJoin(uuid)) {
log.warn(
2022-07-08 20:46:25 +00:00
`denyPendingApprovalRequest/${idLog}: ${uuid} is not requesting ` +
'to join the group. Returning early.'
);
return undefined;
}
2022-07-08 20:46:25 +00:00
const ourUuid = window.textsecure.storage.user.getCheckedUuid(UUIDKind.ACI);
return window.Signal.Groups.buildDeletePendingAdminApprovalMemberChange({
group: this.attributes,
ourUuid,
uuid,
});
}
async addPendingApprovalRequest(): Promise<
2021-06-22 14:46:42 +00:00
Proto.GroupChange.Actions | undefined
> {
const idLog = this.idForLogging();
// Hard-coded to our own ID, because you don't add other users for admin approval
2021-11-11 22:43:05 +00:00
const conversationId =
window.ConversationController.getOurConversationIdOrThrow();
const toRequest = window.ConversationController.get(conversationId);
if (!toRequest) {
throw new Error(
`addPendingApprovalRequest/${idLog}: No conversation found for conversation ${conversationId}`
);
}
2022-07-08 20:46:25 +00:00
const uuid = toRequest.getCheckedUuid(`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.
2022-07-08 20:46:25 +00:00
if (this.isMemberAwaitingApproval(uuid)) {
log.warn(
2022-07-08 20:46:25 +00:00
`addPendingApprovalRequest/${idLog}: ` +
`${toRequest.idForLogging()} already in pending approval.`
);
return undefined;
}
return window.Signal.Groups.buildAddPendingAdminApprovalMemberChange({
group: this.attributes,
profileKeyCredentialBase64,
serverPublicParamsBase64: window.getServerPublicParams(),
});
}
2022-07-08 20:46:25 +00:00
async addMember(uuid: UUID): Promise<Proto.GroupChange.Actions | undefined> {
const idLog = this.idForLogging();
2022-07-08 20:46:25 +00:00
const toRequest = window.ConversationController.get(uuid.toString());
if (!toRequest) {
2022-07-08 20:46:25 +00:00
throw new Error(`addMember/${idLog}: No conversation found for ${uuid}`);
}
// 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.
2022-07-08 20:46:25 +00:00
if (this.isMember(uuid)) {
log.warn(
`addMember/${idLog}: ${toRequest.idForLogging()} ` +
'is already a member.'
);
return undefined;
}
return window.Signal.Groups.buildAddMember({
group: this.attributes,
profileKeyCredentialBase64,
serverPublicParamsBase64: window.getServerPublicParams(),
uuid,
});
}
2022-07-08 20:46:25 +00:00
private async removePendingMember(
uuids: ReadonlyArray<UUID>
2021-06-22 14:46:42 +00:00
): Promise<Proto.GroupChange.Actions | undefined> {
return removePendingMember(this.attributes, uuids);
}
2022-07-08 20:46:25 +00:00
private async removeMember(
uuid: UUID
2021-06-22 14:46:42 +00:00
): Promise<Proto.GroupChange.Actions | undefined> {
2020-10-06 17:06:34 +00:00
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.
2022-07-08 20:46:25 +00:00
if (!this.isMember(uuid)) {
log.warn(
2022-07-08 20:46:25 +00:00
`removeMember/${idLog}: ${uuid} is not a pending member of group. Returning early.`
2020-10-06 17:06:34 +00:00
);
return undefined;
}
2022-07-08 20:46:25 +00:00
const ourUuid = window.textsecure.storage.user.getCheckedUuid(UUIDKind.ACI);
2020-10-06 17:06:34 +00:00
return window.Signal.Groups.buildDeleteMemberChange({
group: this.attributes,
ourUuid,
2020-10-06 17:06:34 +00:00
uuid,
});
}
2022-07-08 20:46:25 +00:00
private async toggleAdminChange(
uuid: UUID
2021-06-22 14:46:42 +00:00
): Promise<Proto.GroupChange.Actions | undefined> {
if (!isGroupV2(this.attributes)) {
return undefined;
}
const idLog = this.idForLogging();
2022-07-08 20:46:25 +00:00
if (!this.isMember(uuid)) {
log.warn(
2022-07-08 20:46:25 +00:00
`toggleAdminChange/${idLog}: ${uuid} is not a pending member of group. Returning early.`
);
return undefined;
}
2021-07-02 19:21:24 +00:00
const MEMBER_ROLES = Proto.Member.Role;
2022-07-08 20:46:25 +00:00
const role = this.isAdmin(uuid)
? MEMBER_ROLES.DEFAULT
: MEMBER_ROLES.ADMINISTRATOR;
return window.Signal.Groups.buildModifyMemberRoleChange({
group: this.attributes,
uuid,
role,
});
}
2020-10-06 17:06:34 +00:00
async modifyGroupV2({
2022-07-08 20:46:25 +00:00
usingCredentialsFrom,
2020-10-06 17:06:34 +00:00
createGroupChange,
extraConversationsForSend,
inviteLinkPassword,
name,
2020-10-06 17:06:34 +00:00
}: {
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: ReadonlyArray<ConversationModel>;
2021-06-22 14:46:42 +00:00
createGroupChange: () => Promise<Proto.GroupChange.Actions | undefined>;
extraConversationsForSend?: ReadonlyArray<string>;
inviteLinkPassword?: string;
name: string;
2020-10-06 17:06:34 +00:00
}): Promise<void> {
await window.Signal.Groups.modifyGroupV2({
conversation: this,
2022-07-08 20:46:25 +00:00
usingCredentialsFrom,
createGroupChange,
extraConversationsForSend,
inviteLinkPassword,
name,
});
2020-10-02 22:19:52 +00:00
}
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(
`Conversation ${this.idForLogging()} 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);
}
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
) {
2021-05-28 19:11:19 +00:00
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 {
2022-02-16 18:36:21 +00:00
return isGroupV1(this.attributes);
}
isBlocked(): boolean {
const uuid = this.get('uuid');
if (uuid) {
return window.storage.blocked.isUuidBlocked(uuid);
}
const e164 = this.get('e164');
if (e164) {
return window.storage.blocked.isBlocked(e164);
}
const groupId = this.get('groupId');
if (groupId) {
return window.storage.blocked.isGroupBlocked(groupId);
}
return false;
}
block({ viaStorageServiceSync = false } = {}): void {
let blocked = false;
const wasBlocked = this.isBlocked();
const uuid = this.get('uuid');
if (uuid) {
drop(window.storage.blocked.addBlockedUuid(uuid));
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 uuid = this.get('uuid');
if (uuid) {
drop(window.storage.blocked.removeBlockedUuid(uuid));
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;
}
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 {
const draftAttachments = this.get('draftAttachments') || [];
return (this.get('draft') ||
this.get('quotedMessageId') ||
draftAttachments.length > 0) as boolean;
}
getDraftPreview(): string {
const draft = this.get('draft');
2020-11-03 01:19:52 +00:00
if (draft) {
2020-11-03 01:19:52 +00:00
const bodyRanges = this.get('draftBodyRanges') || [];
return getTextWithMentions(bodyRanges, draft);
}
const draftAttachments = this.get('draftAttachments') || [];
if (draftAttachments.length > 0) {
return window.i18n('Conversation--getDraftPreview--attachment');
}
const quotedMessageId = this.get('quotedMessageId');
if (quotedMessageId) {
return window.i18n('Conversation--getDraftPreview--quote');
}
return window.i18n('Conversation--getDraftPreview--draft');
}
bumpTyping(): void {
// We don't send typing messages if the setting is disabled
2021-08-18 20:08:14 +00:00
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<void> {
if (!isGroupV2(this.attributes)) {
return;
}
await window.Signal.Groups.waitThenMaybeUpdateGroup({
force: options.force,
conversation: this,
});
}
async fetchSMSOnlyUUID(): Promise<void> {
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()}`
);
}
2021-06-01 20:45:43 +00:00
if (!this.get('uuid')) {
return;
}
2021-06-01 20:45:43 +00:00
// On successful fetch - mark contact as registered.
this.setRegistered();
}
override isValid(): boolean {
return (
isDirectConversation(this.attributes) ||
isGroupV1(this.attributes) ||
isGroupV2(this.attributes)
);
}
2020-11-20 17:30:45 +00:00
async maybeMigrateV1Group(): Promise<void> {
if (!isGroupV1(this.attributes)) {
2020-11-20 17:30:45 +00:00
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<string>;
}
| { members: ReadonlyArray<string> }
)
> = {}
): GroupV2InfoType | undefined {
if (isDirectConversation(this.attributes) || !isGroupV2(this.attributes)) {
return undefined;
}
return {
2021-06-22 14:46:42 +00:00
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,
};
}
getGroupV1Info(members?: Array<string>): GroupV1InfoType | undefined {
const groupId = this.get('groupId');
const groupVersion = this.get('groupVersion');
if (
isDirectConversation(this.attributes) ||
!groupId ||
(groupVersion && groupVersion > 0)
) {
return undefined;
}
return {
id: groupId,
members: members || this.getRecipients(),
};
}
2021-09-24 00:49:05 +00:00
getGroupIdBuffer(): Uint8Array | undefined {
const groupIdString = this.get('groupId');
if (!groupIdString) {
return undefined;
}
if (isGroupV1(this.attributes)) {
2021-09-24 00:49:05 +00:00
return Bytes.fromBinary(groupIdString);
}
if (isGroupV2(this.attributes)) {
2021-09-24 00:49:05 +00:00
return Bytes.fromBase64(groupIdString);
}
return undefined;
}
2021-04-08 16:24:21 +00:00
async sendTypingMessage(isTyping: boolean): Promise<void> {
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 () => {
2021-05-25 22:40:04 +00:00
const groupMembers = this.getRecipients();
2021-05-25 22:40:04 +00:00
// We don't send typing messages if our recipients list is empty
if (!isDirectConversation(this.attributes) && !groupMembers.length) {
2021-05-25 22:40:04 +00:00
return;
}
if (this.lastIsTyping === undefined) {
log.info(`sendTypingMessage(${this.idForLogging()}): ignoring`);
return;
}
const recipientId = isDirectConversation(this.attributes)
? this.getSendTarget()
: undefined;
const groupId = this.getGroupIdBuffer();
2021-05-25 22:40:04 +00:00
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);
2021-05-25 22:40:04 +00:00
2021-07-02 19:21:24 +00:00
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
const sendOptions = {
...(await getSendOptions(this.attributes)),
online: true,
};
if (isDirectConversation(this.attributes)) {
2021-09-27 18:29:06 +00:00
await handleMessageSend(
messaging.sendMessageProtoAndWait({
contentHint: ContentHint.IMPLICIT,
groupId: undefined,
options: sendOptions,
proto: contentMessage,
recipients: groupMembers,
timestamp,
urgent: false,
}),
{ messageIds: [], sendType: 'typing' }
2021-05-25 22:40:04 +00:00
);
} else {
2021-09-27 18:29:06 +00:00
await handleMessageSend(
2021-05-25 22:40:04 +00:00
window.Signal.Util.sendContentMessageToGroup({
2021-06-08 21:51:58 +00:00
contentHint: ContentHint.IMPLICIT,
2021-05-25 22:40:04 +00:00
contentMessage,
messageId: undefined,
2021-05-25 22:40:04 +00:00
online: true,
recipients: groupMembers,
sendOptions,
sendTarget: this.toSenderKeyTarget(),
sendType: 'typing',
2021-05-25 22:40:04 +00:00
timestamp,
urgent: false,
}),
{ messageIds: [], sendType: 'typing' }
2021-05-25 22:40:04 +00:00
);
}
});
}
async onNewMessage(message: MessageModel): Promise<void> {
const uuid = message.get('sourceUuid');
const e164 = message.get('source');
const sourceDevice = message.get('sourceDevice');
const source = window.ConversationController.lookupOrCreate({
uuid,
e164,
reason: 'ConversationModel.onNewMessage',
});
const typingToken = `${source?.id}.${sourceDevice}`;
// Clear typing indicator for a given contact if we receive a message from them
this.clearContactTypingTimer(typingToken);
2022-04-20 23:33:38 +00:00
// 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;
}
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<void> {
2022-10-20 22:47:38 +00:00
await this.beforeAddSingleMessage(message);
this.doAddSingleMessage(message, { isJustSent });
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.debouncedUpdateLastMessage!();
}
2022-10-20 22:47:38 +00:00
private async beforeAddSingleMessage(message: MessageModel): Promise<void> {
await message.hydrateStoryContext();
if (!this.newMessageQueue) {
2022-08-25 05:04:42 +00:00
this.newMessageQueue = new PQueue({
concurrency: 1,
2022-11-16 20:18:02 +00:00
timeout: MINUTE * 30,
});
}
// 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) {
void this.loadNewestMessages(undefined, undefined);
2022-10-20 22:47:38 +00:00
} 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 }],
2022-07-05 16:44:53 +00:00
isActive: window.SignalContext.activeWindowService.isActive(),
isJustSent,
isNewMessage: true,
});
}
}
setInProgressFetch(): () => unknown {
let resolvePromise: (value?: unknown) => void;
this.inProgressFetch = new Promise(resolve => {
resolvePromise = resolve;
});
const finish = () => {
resolvePromise();
this.inProgressFetch = undefined;
};
return finish;
}
async loadNewestMessages(
newestMessageId: string | undefined,
setFocus: boolean | undefined
): Promise<void> {
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() && metrics.oldest) {
void this.loadAndScroll(metrics.oldest.id, { disableScroll: true });
return;
}
if (scrollToLatestUnread && metrics.oldestUnseen) {
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<MessageModel> = await this.cleanModels(messages);
const scrollToMessageId =
setFocus && metrics.newest ? metrics.newest.id : undefined;
// 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;
2022-01-20 00:40:29 +00:00
messagesReset({
conversationId,
2022-01-20 00:40:29 +00:00
messages: cleaned.map((messageModel: MessageModel) => ({
...messageModel.attributes,
})),
metrics,
scrollToMessageId,
2022-01-20 00:40:29 +00:00
unboundedFetch,
});
} catch (error) {
setMessageLoadingState(conversationId, undefined);
throw error;
} finally {
finish();
}
}
async loadOlderMessages(oldestMessageId: string): Promise<void> {
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(
`loadOlderMessages: 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('loadOlderMessages: requested, but loaded no messages');
repairOldestMessage(conversationId);
return;
}
const cleaned = await this.cleanModels(models);
messagesAdded({
conversationId,
messages: cleaned.map((messageModel: MessageModel) => ({
...messageModel.attributes,
})),
2022-07-05 16:44:53 +00:00
isActive: window.SignalContext.activeWindowService.isActive(),
isJustSent: false,
isNewMessage: false,
});
} catch (error) {
setMessageLoadingState(conversationId, undefined);
throw error;
} finally {
finish();
}
}
async loadNewerMessages(newestMessageId: string): Promise<void> {
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,
})),
2022-07-05 16:44:53 +00:00
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<void> {
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<MessageModel> = await this.cleanModels(all);
const scrollToMessageId =
options && options.disableScroll ? undefined : messageId;
2022-01-20 00:40:29 +00:00
messagesReset({
conversationId,
2022-01-20 00:40:29 +00:00
messages: cleaned.map((messageModel: MessageModel) => ({
...messageModel.attributes,
})),
metrics,
2022-01-20 00:40:29 +00:00
scrollToMessageId,
});
} catch (error) {
setMessageLoadingState(conversationId, undefined);
throw error;
} finally {
finish();
}
}
async cleanModels(
messages: ReadonlyArray<MessageAttributesType>
): Promise<Array<MessageModel>> {
const result = messages
.filter(message => Boolean(message.id))
.map(message => window.MessageController.register(message.id, message));
const eliminated = messages.length - result.length;
if (eliminated > 0) {
log.warn(`cleanModels: Eliminated ${eliminated} messages without an id`);
}
2021-12-20 21:04:02 +00:00
const ourUuid = window.textsecure.storage.user.getCheckedUuid().toString();
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
2021-12-20 21:04:02 +00:00
await window.Signal.Data.saveMessage(upgradedMessage, { ourUuid });
upgraded += 1;
}
}
if (upgraded > 0) {
log.warn(`cleanModels: Upgraded schema of ${upgraded} messages`);
}
2022-03-16 17:30:14 +00:00
await Promise.all(result.map(model => model.hydrateStoryContext()));
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 = this.getProps();
if (oldCachedProps && isShallowEqual(oldCachedProps, newCachedProps)) {
this.cachedProps = oldCachedProps;
} else {
this.cachedProps = newCachedProps;
}
return this.cachedProps;
} finally {
this.format = oldFormat;
}
}
// Note: this should never be called directly. Use conversation.format() instead, which
// maintains a cache, and protects against reentrant calls.
// Note: When writing code inside this function, do not call .format() on a conversation
// unless you are sure that it's not this very same conversation.
// Note: If you start relying on an attribute that is in
// `ATTRIBUTES_THAT_DONT_INVALIDATE_PROPS_CACHE`, remove it from that list.
private getProps(): ConversationType {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const color = this.getColor()!;
2022-11-30 00:53:39 +00:00
const typingValues = Object.values(this.contactTypingTimers || {});
const typingMostRecent = head(sortBy(typingValues, 'timestamp'));
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const timestamp = this.get('timestamp')!;
const draftTimestamp = this.get('draftTimestamp');
const draftPreview = this.getDraftPreview();
const draftText = dropNull(this.get('draft'));
const shouldShowDraft = Boolean(
this.hasDraft() && draftTimestamp && draftTimestamp >= timestamp
);
const inboxPosition = this.get('inbox_position');
const messageRequestsEnabled = window.Signal.RemoteConfig.isEnabled(
'desktop.messageRequests'
);
2021-11-11 22:43:05 +00:00
const ourConversationId =
window.ConversationController.getOurConversationId();
let groupVersion: undefined | 1 | 2;
if (isGroupV1(this.attributes)) {
groupVersion = 1;
} else if (isGroupV2(this.attributes)) {
groupVersion = 2;
}
const sortedGroupMembers = isGroupV2(this.attributes)
2020-11-03 01:19:52 +00:00
? this.getMembers()
.sort((left, right) =>
sortConversationTitles(left, right, this.intlCollator)
)
.map(member => member.format())
.filter(isNotNil)
2020-11-03 01:19:52 +00:00
: undefined;
2021-06-02 21:05:09 +00:00
const { customColor, customColorId } = this.getCustomColorData();
const ourACI = window.textsecure.storage.user.getUuid(UUIDKind.ACI);
2022-07-08 20:46:25 +00:00
const ourPNI = window.textsecure.storage.user.getUuid(UUIDKind.PNI);
// TODO: DESKTOP-720
return {
id: this.id,
uuid: this.get('uuid'),
2022-09-27 20:31:55 +00:00
pni: this.get('pni'),
e164: this.get('e164'),
2022-01-13 21:25:20 +00:00
// We had previously stored `null` instead of `undefined` in some cases. We should
// be able to remove this `dropNull` once usernames have gone to production.
username: dropNull(this.get('username')),
2021-01-26 01:01:19 +00:00
about: this.getAboutText(),
2021-07-19 19:26:06 +00:00
aboutText: this.get('about'),
aboutEmoji: this.get('aboutEmoji'),
acceptedMessageRequest: this.getAccepted(),
2022-05-11 20:59:58 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
activeAt: this.get('active_at')!,
2022-07-08 20:46:25 +00:00
areWePending:
ourACI &&
(this.isMemberPending(ourACI) ||
Boolean(
ourPNI && !this.isMember(ourACI) && this.isMemberPending(ourPNI)
)),
areWePendingApproval: Boolean(
ourConversationId && ourACI && this.isMemberAwaitingApproval(ourACI)
),
areWeAdmin: this.areWeAdmin(),
2021-08-06 00:17:05 +00:00
avatars: getAvatarData(this.attributes),
badges: this.get('badges') ?? EMPTY_ARRAY,
2020-10-30 17:52:21 +00:00
canChangeTimer: this.canChangeTimer(),
canEditGroupInfo: this.canEditGroupInfo(),
canAddNewMembers: this.canAddNewMembers(),
avatarPath: this.getAbsoluteAvatarPath(),
2021-07-19 19:26:06 +00:00
avatarHash: this.getAvatarHash(),
unblurredAvatarPath: this.getAbsoluteUnblurredAvatarPath(),
profileAvatarPath: this.getAbsoluteProfileAvatarPath(),
color,
2021-05-28 16:15:17 +00:00
conversationColor: this.getConversationColor(),
2021-06-02 21:05:09 +00:00
customColor,
customColorId,
discoveredUnregisteredAt: this.get('discoveredUnregisteredAt'),
draftBodyRanges: this.getDraftBodyRanges(),
draftPreview,
draftText,
2021-07-19 19:26:06 +00:00
familyName: this.get('profileFamilyName'),
2022-05-11 20:59:58 +00:00
firstName: this.get('profileName'),
2021-06-02 00:24:28 +00:00
groupDescription: this.get('description'),
groupVersion,
2020-11-13 19:57:55 +00:00
groupId: this.get('groupId'),
groupLink: this.getGroupLink(),
2022-03-04 21:14:52 +00:00
hideStory: Boolean(this.get('hideStory')),
inboxPosition,
2022-05-11 20:59:58 +00:00
isArchived: this.get('isArchived'),
isBlocked: this.isBlocked(),
isMe: isMe(this.attributes),
isGroupV1AndDisabled: this.isGroupV1AndDisabled(),
isPinned: this.get('isPinned'),
isUntrusted: this.isUntrusted(),
isVerified: this.isVerified(),
isFetchingUUID: this.isFetchingUUID,
lastMessage: this.getLastMessage(),
2022-05-11 20:59:58 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
lastUpdated: this.get('timestamp')!,
2020-10-30 17:52:21 +00:00
left: Boolean(this.get('left')),
2022-05-11 20:59:58 +00:00
markedUnread: this.get('markedUnread'),
membersCount: this.getMembersCount(),
memberships: this.getMemberships(),
hasMessages: (this.get('messageCount') ?? 0) > 0,
pendingMemberships: this.getPendingMemberships(),
pendingApprovalMemberships: this.getPendingApprovalMemberships(),
bannedMemberships: this.getBannedMemberships(),
2021-07-19 19:26:06 +00:00
profileKey: this.get('profileKey'),
messageRequestsEnabled,
2021-11-11 22:43:05 +00:00
accessControlAddFromInviteLink:
this.get('accessControl')?.addFromInviteLink,
accessControlAttributes: this.get('accessControl')?.attributes,
accessControlMembers: this.get('accessControl')?.members,
2021-07-20 20:18:35 +00:00
announcementsOnly: Boolean(this.get('announcementsOnly')),
announcementsOnlyReady: this.canBeAnnouncementGroup(),
2020-10-30 17:52:21 +00:00
expireTimer: this.get('expireTimer'),
2022-05-11 20:59:58 +00:00
muteExpiresAt: this.get('muteExpiresAt'),
2021-08-05 12:35:33 +00:00
dontNotifyForMentionsIfMuted: this.get('dontNotifyForMentionsIfMuted'),
2022-05-11 20:59:58 +00:00
name: this.get('name'),
systemGivenName: this.get('systemGivenName'),
systemFamilyName: this.get('systemFamilyName'),
2022-05-11 20:59:58 +00:00
phoneNumber: this.getNumber(),
profileName: this.getProfileName(),
profileSharing: this.get('profileSharing'),
2020-11-13 19:57:55 +00:00
publicParams: this.get('publicParams'),
secretParams: this.get('secretParams'),
shouldShowDraft,
sortedGroupMembers,
timestamp,
2022-05-11 20:59:58 +00:00
title: this.getTitle(),
titleNoDefault: this.getTitleNoDefault(),
typingContactId: typingMostRecent?.senderId,
searchableTitle: isMe(this.attributes)
2021-04-27 22:35:35 +00:00
? window.i18n('noteToSelf')
: this.getTitle(),
2022-05-11 20:59:58 +00:00
unreadCount: this.get('unreadCount') || 0,
...(isDirectConversation(this.attributes)
2021-05-07 22:21:10 +00:00
? {
type: 'direct' as const,
sharedGroupNames: this.get('sharedGroupNames') || EMPTY_ARRAY,
2021-05-07 22:21:10 +00:00
}
: {
type: 'group' as const,
2021-06-01 23:30:25 +00:00
acknowledgedGroupNameCollisions:
this.get('acknowledgedGroupNameCollisions') ||
EMPTY_GROUP_COLLISIONS,
sharedGroupNames: EMPTY_ARRAY,
storySendMode: this.getGroupStorySendMode(),
2021-05-07 22:21:10 +00:00
}),
voiceNotePlaybackRate: this.get('voiceNotePlaybackRate'),
};
}
updateE164(
e164?: string | null,
{
disableDiscoveryNotification,
}: {
disableDiscoveryNotification?: boolean;
} = {}
): void {
const oldValue = this.get('e164');
if (e164 === oldValue) {
return;
}
2021-08-05 23:34:49 +00:00
this.set('e164', e164 || undefined);
2021-08-05 23:34:49 +00:00
// We just discovered a new phone number for this account. If we're not merging
// then we'll add a standalone notification here.
const haveSentMessage = Boolean(
this.get('profileSharing') || this.get('sentMessageCount')
);
if (!oldValue && e164 && haveSentMessage && !disableDiscoveryNotification) {
void this.addPhoneNumberDiscovery(e164);
}
// This user changed their phone number
if (oldValue && e164) {
void this.addChangeNumberNotification(oldValue, e164);
}
window.Signal.Data.updateConversation(this.attributes);
this.trigger('idUpdated', this, 'e164', oldValue);
this.captureChange('updateE164');
}
updateUuid(uuid?: string): void {
const oldValue = this.get('uuid');
if (uuid === oldValue) {
return;
}
this.set('uuid', uuid ? UUID.cast(uuid.toLowerCase()) : undefined);
window.Signal.Data.updateConversation(this.attributes);
this.trigger('idUpdated', this, 'uuid', 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.get('pni')) {
drop(
window.textsecure.storage.protocol.removeIdentityKey(
UUID.cast(oldValue)
)
);
}
this.captureChange('updateUuid');
}
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?: string): void {
const oldValue = this.get('pni');
if (pni === oldValue) {
return;
}
this.set('pni', pni ? UUID.cast(pni.toLowerCase()) : undefined);
const pniIsPrimaryId =
!this.get('uuid') ||
this.get('uuid') === oldValue ||
this.get('uuid') === 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(
UUID.cast(oldValue)
);
const newIdentityRecord =
window.textsecure.storage.protocol.getIdentityRecord(
UUID.checkedLookup(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(
UUID.cast(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(
UUID.cast(oldValue)
)
);
}
if (pni && !this.get('uuid')) {
log.warn(
`updatePni/${this.idForLogging()}: pni field set to ${pni}, but uuid 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);
}
}
incrementMessageCount(): void {
this.set({
messageCount: (this.get('messageCount') || 0) + 1,
});
window.Signal.Data.updateConversation(this.attributes);
}
getMembersCount(): number | undefined {
if (isDirectConversation(this.attributes)) {
return undefined;
}
const memberList = this.get('membersV2') || this.get('members');
// We'll fail over if the member list is empty
if (memberList && memberList.length) {
return memberList.length;
}
const temporaryMemberCount = this.get('temporaryMemberCount');
2021-10-06 21:59:34 +00:00
if (isNumber(temporaryMemberCount)) {
return temporaryMemberCount;
}
return undefined;
}
decrementMessageCount(): void {
this.set({
messageCount: Math.max((this.get('messageCount') || 0) - 1, 0),
});
window.Signal.Data.updateConversation(this.attributes);
}
incrementSentMessageCount({ dry = false }: { dry?: boolean } = {}):
| Partial<ConversationAttributesType>
| undefined {
const update = {
messageCount: (this.get('messageCount') || 0) + 1,
sentMessageCount: (this.get('sentMessageCount') || 0) + 1,
};
if (dry) {
return update;
2021-08-26 16:17:57 +00:00
}
this.set(update);
window.Signal.Data.updateConversation(this.attributes);
return undefined;
}
decrementSentMessageCount(): void {
this.set({
messageCount: Math.max((this.get('messageCount') || 0) - 1, 0),
sentMessageCount: Math.max((this.get('sentMessageCount') || 0) - 1, 0),
});
window.Signal.Data.updateConversation(this.attributes);
}
/**
* This function is called when a message request is accepted in order to
* handle sending read receipts and download any pending attachments.
*/
2020-10-06 17:06:34 +00:00
async handleReadAndDownloadAttachments(
options: { isLocalAction?: boolean } = {}
): Promise<void> {
const { isLocalAction } = options;
2021-12-20 21:04:02 +00:00
const ourUuid = window.textsecure.storage.user.getCheckedUuid().toString();
2020-10-06 17:06:34 +00:00
let messages: Array<MessageAttributesType> | undefined;
do {
const first = messages ? messages[0] : undefined;
// eslint-disable-next-line no-await-in-loop
messages = await window.Signal.Data.getOlderMessagesByConversation(
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));
2020-10-06 17:06:34 +00:00
if (isLocalAction) {
// eslint-disable-next-line no-await-in-loop
await readReceiptsJobQueue.addIfAllowedByUser(
window.storage,
readMessages.map(m => ({
messageId: m.id,
senderE164: m.source,
senderUuid: m.sourceUuid,
timestamp: m.sent_at,
2022-08-15 21:53:33 +00:00
isDirectConversation: isDirectConversation(this.attributes),
}))
);
2020-10-06 17:06:34 +00:00
}
// eslint-disable-next-line no-await-in-loop
await Promise.all(
readMessages.map(async m => {
const registered = window.MessageController.register(m.id, m);
const shouldSave = await registered.queueAttachmentDownloads();
if (shouldSave) {
2021-12-20 21:04:02 +00:00
await window.Signal.Data.saveMessage(registered.attributes, {
ourUuid,
});
}
})
);
} while (messages.length > 0);
}
async applyMessageRequestResponse(
response: number,
{ fromSync = false, viaStorageServiceSync = false } = {}
): Promise<void> {
try {
2021-07-02 19:21:24 +00:00
const messageRequestEnum = Proto.SyncMessage.MessageRequestResponse.Type;
const isLocalAction = !fromSync && !viaStorageServiceSync;
const currentMessageRequestState = this.get('messageRequestResponseType');
const didResponseChange = response !== currentMessageRequestState;
const wasPreviouslyAccepted = this.getAccepted();
// Apply message request response locally
this.set({
messageRequestResponseType: response,
});
if (response === messageRequestEnum.ACCEPT) {
this.unblock({ viaStorageServiceSync });
this.enableProfileSharing({ viaStorageServiceSync });
2020-10-06 17:06:34 +00:00
// 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 });
2020-10-06 17:06:34 +00:00
}
if (isLocalAction) {
2022-07-08 20:46:25 +00:00
const ourACI = window.textsecure.storage.user.getCheckedUuid(
UUIDKind.ACI
);
const ourPNI = window.textsecure.storage.user.getUuid(UUIDKind.PNI);
const ourConversation =
window.ConversationController.getOurConversationOrThrow();
2022-07-08 20:46:25 +00:00
if (
isGroupV1(this.attributes) ||
isDirectConversation(this.attributes)
) {
void this.sendProfileKeyUpdate();
} else if (
isGroupV2(this.attributes) &&
2022-07-08 20:46:25 +00:00
this.isMemberPending(ourACI)
) {
await this.modifyGroupV2({
name: 'promotePendingMember',
usingCredentialsFrom: [ourConversation],
2022-07-08 20:46:25 +00:00
createGroupChange: () => this.promotePendingMember(UUIDKind.ACI),
});
} else if (
2022-07-08 20:46:25 +00:00
ourPNI &&
isGroupV2(this.attributes) &&
2022-07-08 20:46:25 +00:00
this.isMemberPending(ourPNI)
) {
2022-07-08 20:46:25 +00:00
await this.modifyGroupV2({
name: 'promotePendingMember',
usingCredentialsFrom: [ourConversation],
2022-07-08 20:46:25 +00:00
createGroupChange: () => this.promotePendingMember(UUIDKind.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'
);
}
2020-10-06 17:06:34 +00:00
}
} else if (response === messageRequestEnum.BLOCK) {
// Block locally, other devices should block upon receiving the sync message
this.block({ viaStorageServiceSync });
this.disableProfileSharing({ viaStorageServiceSync });
if (isLocalAction) {
if (isGroupV1(this.attributes)) {
await this.leaveGroup();
} else if (isGroupV2(this.attributes)) {
await this.leaveGroupV2();
}
2020-10-06 17:06:34 +00:00
}
} else if (response === messageRequestEnum.DELETE) {
this.disableProfileSharing({ viaStorageServiceSync });
2020-10-06 17:06:34 +00:00
// Delete messages locally, other devices should delete upon receiving
// the sync message
await this.destroyMessages();
void this.updateLastMessage();
2020-10-06 17:06:34 +00:00
if (isLocalAction) {
2023-01-02 21:34:41 +00:00
window.reduxActions.conversations.onConversationClosed(
this.id,
'deleted from message request'
);
2020-10-06 17:06:34 +00:00
if (isGroupV1(this.attributes)) {
await this.leaveGroup();
} else if (isGroupV2(this.attributes)) {
await this.leaveGroupV2();
}
}
} else if (response === messageRequestEnum.BLOCK_AND_DELETE) {
// Block locally, other devices should block upon receiving the sync message
this.block({ viaStorageServiceSync });
this.disableProfileSharing({ viaStorageServiceSync });
// Delete messages locally, other devices should delete upon receiving
// the sync message
await this.destroyMessages();
void this.updateLastMessage();
if (isLocalAction) {
2023-01-02 21:34:41 +00:00
window.reduxActions.conversations.onConversationClosed(
this.id,
'blocked and deleted from message request'
);
if (isGroupV1(this.attributes)) {
await this.leaveGroup();
} else if (isGroupV2(this.attributes)) {
await this.leaveGroupV2();
}
2020-10-06 17:06:34 +00:00
}
}
} finally {
window.Signal.Data.updateConversation(this.attributes);
}
}
async joinGroupV2ViaLinkAndMigrate({
approvalRequired,
inviteLinkPassword,
revision,
}: {
approvalRequired: boolean;
inviteLinkPassword: string;
revision: number;
}): Promise<void> {
await window.Signal.Groups.joinGroupV2ViaLinkAndMigrate({
approvalRequired,
conversation: this,
inviteLinkPassword,
revision,
});
}
async joinGroupV2ViaLink({
inviteLinkPassword,
approvalRequired,
}: {
inviteLinkPassword: string;
approvalRequired: boolean;
}): Promise<void> {
2022-07-08 20:46:25 +00:00
const ourACI = window.textsecure.storage.user.getCheckedUuid();
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,
2022-07-08 20:46:25 +00:00
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 {
2021-09-24 00:49:05 +00:00
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: [
{
2022-07-08 20:46:25 +00:00
uuid: ourACI.toString(),
timestamp: Date.now(),
},
],
});
}
}
}
2021-07-02 19:21:24 +00:00
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<void> {
2022-07-08 20:46:25 +00:00
const ourACI = window.storage.user.getCheckedUuid(UUIDKind.ACI);
const inviteLinkPassword = this.get('groupInviteLinkPassword');
if (!inviteLinkPassword) {
log.warn(
`cancelJoinRequest/${this.idForLogging()}: We don't have an inviteLinkPassword!`
);
}
await this.modifyGroupV2({
name: 'cancelJoinRequest',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
inviteLinkPassword,
2022-07-08 20:46:25 +00:00
createGroupChange: () => this.denyPendingApprovalRequest(ourACI),
});
}
2020-10-06 17:06:34 +00:00
async leaveGroupV2(): Promise<void> {
2022-07-08 20:46:25 +00:00
if (!isGroupV2(this.attributes)) {
return;
}
2020-10-06 17:06:34 +00:00
2022-07-08 20:46:25 +00:00
const ourACI = window.textsecure.storage.user.getCheckedUuid(UUIDKind.ACI);
const ourPNI = window.textsecure.storage.user.getUuid(UUIDKind.PNI);
const ourConversation =
window.ConversationController.getOurConversationOrThrow();
2022-07-08 20:46:25 +00:00
if (this.isMemberPending(ourACI)) {
2020-10-06 17:06:34 +00:00
await this.modifyGroupV2({
name: 'delete',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: () => this.removePendingMember([ourACI]),
2020-10-06 17:06:34 +00:00
});
2022-07-08 20:46:25 +00:00
} else if (this.isMember(ourACI)) {
await this.modifyGroupV2({
name: 'delete',
usingCredentialsFrom: [ourConversation],
2022-07-08 20:46:25 +00:00
createGroupChange: () => this.removeMember(ourACI),
});
// Keep PNI in pending if ACI was a member.
} else if (ourPNI && this.isMemberPending(ourPNI)) {
2020-10-06 17:06:34 +00:00
await this.modifyGroupV2({
name: 'delete',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: () => this.removePendingMember([ourPNI]),
2020-10-06 17:06:34 +00:00
});
} else {
2022-07-08 20:46:25 +00:00
const logId = this.idForLogging();
log.error(
2022-07-08 20:46:25 +00:00
'leaveGroupV2: We were neither a member nor a pending member of ' +
`the group ${logId}`
2020-10-06 17:06:34 +00:00
);
}
}
async addBannedMember(
2022-07-08 20:46:25 +00:00
uuid: UUID
): Promise<Proto.GroupChange.Actions | undefined> {
if (this.isMember(uuid)) {
log.warn('addBannedMember: Member is a part of the group!');
return;
}
if (this.isMemberPending(uuid)) {
log.warn('addBannedMember: Member is pending to be added to group!');
return;
}
if (this.isMemberBanned(uuid)) {
log.warn('addBannedMember: Member is already banned!');
return;
}
return window.Signal.Groups.buildAddBannedMemberChange({
group: this.attributes,
uuid,
});
}
async blockGroupLinkRequests(uuid: UUIDStringType): Promise<void> {
await this.modifyGroupV2({
name: 'addBannedMember',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: async () => this.addBannedMember(new UUID(uuid)),
});
}
async toggleAdmin(conversationId: string): Promise<void> {
if (!isGroupV2(this.attributes)) {
return;
}
2022-07-08 20:46:25 +00:00
const logId = this.idForLogging();
const member = window.ConversationController.get(conversationId);
if (!member) {
log.error(`toggleAdmin/${logId}: ${conversationId} does not exist`);
return;
}
const uuid = member.getCheckedUuid(`toggleAdmin/${logId}`);
if (!this.isMember(uuid)) {
log.error(
`toggleAdmin: Member ${conversationId} is not a member of the group`
);
return;
}
await this.modifyGroupV2({
name: 'toggleAdmin',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [member],
createGroupChange: () => this.toggleAdminChange(uuid),
});
}
async removeFromGroupV2(conversationId: string): Promise<void> {
2022-07-08 20:46:25 +00:00
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 uuid = pendingMember.getCheckedUuid(`removeFromGroupV2/${logId}`);
if (this.isMemberRequestingToJoin(uuid)) {
await this.modifyGroupV2({
name: 'denyPendingApprovalRequest',
usingCredentialsFrom: [],
2022-07-08 20:46:25 +00:00
createGroupChange: () => this.denyPendingApprovalRequest(uuid),
extraConversationsForSend: [conversationId],
});
2022-07-08 20:46:25 +00:00
} else if (this.isMemberPending(uuid)) {
await this.modifyGroupV2({
name: 'removePendingMember',
usingCredentialsFrom: [],
2022-07-08 20:46:25 +00:00
createGroupChange: () => this.removePendingMember([uuid]),
extraConversationsForSend: [conversationId],
});
2022-07-08 20:46:25 +00:00
} else if (this.isMember(uuid)) {
await this.modifyGroupV2({
name: 'removeFromGroup',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [pendingMember],
createGroupChange: () => this.removeMember(uuid),
extraConversationsForSend: [conversationId],
});
} else {
log.error(
`removeFromGroupV2: Member ${conversationId} is neither a member nor a pending member of the group`
);
}
}
async syncMessageRequestResponse(response: number): Promise<void> {
// In GroupsV2, this may modify the server. We only want to continue if those
// server updates were successful.
await this.applyMessageRequestResponse(response);
const groupId = this.getGroupIdBuffer();
if (window.ConversationController.areWePrimaryDevice()) {
log.warn(
'syncMessageRequestResponse: We are primary device; not sending message request sync'
);
return;
}
try {
await singleProtoJobQueue.add(
MessageSender.getMessageRequestResponseSync({
threadE164: this.get('e164'),
threadUuid: this.get('uuid'),
groupId,
type: response,
})
);
} catch (error) {
log.error(
'syncMessageRequestResponse: Failed to queue sync message',
Errors.toLogFormat(error)
);
2020-11-20 17:30:45 +00:00
}
}
async safeGetVerified(): Promise<number> {
2021-10-26 22:59:08 +00:00
const uuid = this.getUuid();
if (!uuid) {
return this.verifiedEnum.DEFAULT;
}
try {
return await window.textsecure.storage.protocol.getVerified(uuid);
} catch {
return this.verifiedEnum.DEFAULT;
}
}
async updateVerified(): Promise<void> {
if (isDirectConversation(this.attributes)) {
await this.initialPromise;
const verified = await this.safeGetVerified();
if (this.get('verified') !== verified) {
this.set({ 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();
}
})
);
}
2022-11-30 00:53:39 +00:00
setVerifiedDefault(): Promise<boolean> {
const { DEFAULT } = this.verifiedEnum;
return this.queueJob('setVerifiedDefault', () =>
2022-11-30 00:53:39 +00:00
this._setVerified(DEFAULT)
);
}
2022-11-30 00:53:39 +00:00
setVerified(): Promise<boolean> {
const { VERIFIED } = this.verifiedEnum;
2022-11-30 00:53:39 +00:00
return this.queueJob('setVerified', () => this._setVerified(VERIFIED));
}
2022-11-30 00:53:39 +00:00
setUnverified(): Promise<boolean> {
const { UNVERIFIED } = this.verifiedEnum;
2022-11-30 00:53:39 +00:00
return this.queueJob('setUnverified', () => this._setVerified(UNVERIFIED));
}
2022-11-30 00:53:39 +00:00
private async _setVerified(verified: number): Promise<boolean> {
const { VERIFIED, DEFAULT } = this.verifiedEnum;
if (!isDirectConversation(this.attributes)) {
throw new Error(
'You cannot verify a group conversation. ' +
'You must verify individual contacts.'
);
}
2021-10-26 22:59:08 +00:00
const uuid = this.getUuid();
const beginningVerified = this.get('verified') ?? DEFAULT;
const keyChange = false;
if (uuid) {
if (verified === this.verifiedEnum.DEFAULT) {
await window.textsecure.storage.protocol.setVerified(uuid, verified);
} else {
await window.textsecure.storage.protocol.setVerified(uuid, verified, {
firstUse: false,
nonblockingApproval: true,
});
}
} else {
log.warn(`_setVerified(${this.id}): no uuid to update protocol storage`);
}
this.set({ verified });
2022-03-09 18:22:34 +00:00
window.Signal.Data.updateConversation(this.attributes);
if (beginningVerified !== verified) {
this.captureChange(`verified 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 && uuid) {
await this.sendVerifySyncMessage(this.get('e164'), uuid, verified);
}
return keyChange;
}
async sendVerifySyncMessage(
e164: string | undefined,
2021-10-26 22:59:08 +00:00
uuid: UUID,
state: number
): Promise<CallbackResultType | void> {
2021-10-26 22:59:08 +00:00
const identifier = uuid ? uuid.toString() : e164;
if (!identifier) {
throw new Error(
'sendVerifySyncMessage: Neither e164 nor UUID were provided'
);
}
if (window.ConversationController.areWePrimaryDevice()) {
log.warn(
'sendVerifySyncMessage: We are primary device; not sending sync'
);
return;
}
const key = await window.textsecure.storage.protocol.loadIdentityKey(
UUID.checkedLookup(identifier)
);
if (!key) {
throw new Error(
`sendVerifySyncMessage: No identity key found for identifier ${identifier}`
);
}
try {
await singleProtoJobQueue.add(
MessageSender.getVerificationSync(e164, uuid.toString(), 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;
}
if (!this.contactCollection?.length) {
return false;
}
return this.contactCollection?.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<ConversationModel> {
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<void> {
if (!isDirectConversation(this.attributes)) {
throw new Error(
'You cannot set a group conversation as trusted. ' +
'You must set individual contacts as trusted.'
);
}
2021-10-26 22:59:08 +00:00
const uuid = this.getUuid();
if (!uuid) {
log.warn(`setApproved(${this.id}): no uuid, ignoring`);
return;
}
2021-10-26 22:59:08 +00:00
return window.textsecure.storage.protocol.setApproval(uuid, true);
}
safeIsUntrusted(timestampThreshold?: number): boolean {
try {
2021-10-26 22:59:08 +00:00
const uuid = this.getUuid();
strictAssert(uuid, `No uuid for conversation: ${this.id}`);
return window.textsecure.storage.protocol.isUntrusted(
uuid,
timestampThreshold
);
} catch (err) {
return false;
}
}
isUntrusted(timestampThreshold?: number): boolean {
if (isDirectConversation(this.attributes)) {
return this.safeIsUntrusted(timestampThreshold);
}
2022-11-30 00:53:39 +00:00
const { contactCollection } = this;
if (!contactCollection?.length) {
return false;
}
2022-11-30 00:53:39 +00:00
return contactCollection.some(contact => {
if (isMe(contact.attributes)) {
return false;
}
return contact.safeIsUntrusted(timestampThreshold);
});
}
getUntrusted(timestampThreshold?: number): Array<ConversationModel> {
if (isDirectConversation(this.attributes)) {
if (this.isUntrusted(timestampThreshold)) {
return [this];
}
return [];
}
return (
this.contactCollection?.filter(contact => {
if (isMe(contact.attributes)) {
2021-04-27 22:35:35 +00:00
return false;
}
return contact.isUntrusted(timestampThreshold);
}) || []
);
}
getSentMessageCount(): number {
return this.get('sentMessageCount') || 0;
}
getMessageRequestResponseType(): number {
return this.get('messageRequestResponseType') || 0;
}
2021-01-26 01:01:19 +00:00
getAboutText(): string | undefined {
if (!this.get('about')) {
return undefined;
}
const emoji = this.get('aboutEmoji');
const text = this.get('about');
if (!emoji) {
return text;
}
2021-01-26 01:01:19 +00:00
return window.i18n('message--getNotificationText--text-with-emoji', {
text,
emoji,
2021-01-26 01:01:19 +00:00
});
}
/**
* Determine if this conversation should be considered "accepted" in terms
* of message requests
*/
getAccepted(): boolean {
return isConversationAccepted(this.attributes);
}
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<unknown> {
if (this.isVerified()) {
return this.setVerifiedDefault();
}
return this.setVerified();
}
async addChatSessionRefreshed({
receivedAt,
receivedAtCounter,
}: {
receivedAt: number;
receivedAtCounter: number;
}): Promise<void> {
log.info(`addChatSessionRefreshed: adding for ${this.idForLogging()}`, {
receivedAt,
});
2021-02-18 16:40:26 +00:00
2021-11-11 22:43:05 +00:00
const message = {
2021-02-18 16:40:26 +00:00
conversationId: this.id,
type: 'chat-session-refreshed',
sent_at: receivedAt,
received_at: receivedAtCounter,
2021-03-04 21:44:57 +00:00
received_at_ms: receivedAt,
readStatus: ReadStatus.Unread,
seenStatus: SeenStatus.Unseen,
2021-02-18 16:40:26 +00:00
// TODO: DESKTOP-722
// this type does not fully implement the interface it is expected to
2021-11-11 22:43:05 +00:00
} as unknown as MessageAttributesType;
2021-02-18 16:40:26 +00:00
2021-12-20 21:04:02 +00:00
const id = await window.Signal.Data.saveMessage(message, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
2021-02-18 16:40:26 +00:00
const model = window.MessageController.register(
id,
new window.Whisper.Message({
...message,
id,
})
);
this.trigger('newmessage', model);
void this.updateUnread();
2021-02-18 16:40:26 +00:00
}
async addDeliveryIssue({
receivedAt,
receivedAtCounter,
senderUuid,
sentAt,
}: {
receivedAt: number;
receivedAtCounter: number;
senderUuid: string;
sentAt: number;
}): Promise<void> {
log.info(`addDeliveryIssue: adding for ${this.idForLogging()}`, {
sentAt,
2021-05-28 19:11:19 +00:00
senderUuid,
});
2021-11-11 22:43:05 +00:00
const message = {
2021-05-28 19:11:19 +00:00
conversationId: this.id,
type: 'delivery-issue',
sourceUuid: senderUuid,
sent_at: receivedAt,
received_at: receivedAtCounter,
2021-05-28 19:11:19 +00:00
received_at_ms: receivedAt,
readStatus: ReadStatus.Unread,
seenStatus: SeenStatus.Unseen,
2021-05-28 19:11:19 +00:00
// TODO: DESKTOP-722
// this type does not fully implement the interface it is expected to
2021-11-11 22:43:05 +00:00
} as unknown as MessageAttributesType;
2021-05-28 19:11:19 +00:00
2021-12-20 21:04:02 +00:00
const id = await window.Signal.Data.saveMessage(message, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
2021-05-28 19:11:19 +00:00
const model = window.MessageController.register(
id,
new window.Whisper.Message({
...message,
id,
})
);
this.trigger('newmessage', model);
await this.notify(model);
void this.updateUnread();
2021-05-28 19:11:19 +00:00
}
async addKeyChange(reason: string, keyChangedId?: UUID): Promise<void> {
const keyChangedIdString = keyChangedId?.toString();
return this.queueJob(`addKeyChange(${keyChangedIdString})`, async () => {
log.info(
'adding key change advisory in',
this.idForLogging(),
'for',
keyChangedIdString || '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: window.Signal.Util.incrementMessageCounter(),
received_at_ms: timestamp,
key_changed: keyChangedIdString,
readStatus: ReadStatus.Read,
seenStatus: SeenStatus.Unseen,
schemaVersion: Message.VERSION_NEEDED_FOR_DISPLAY,
};
await window.Signal.Data.saveMessage(message, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
forceSave: true,
});
const model = window.MessageController.register(
message.id,
new window.Whisper.Message(message)
);
const isUntrusted = await this.isUntrusted();
this.trigger('newmessage', model);
const uuid = this.get('uuid');
// Group calls are always with folks that have a UUID
if (isUntrusted && uuid) {
window.reduxActions.calling.keyChanged({ uuid });
}
if (isDirectConversation(this.attributes) && uuid) {
const parsedUuid = UUID.checkedLookup(uuid);
const groups =
await window.ConversationController.getAllGroupsInvolvingUuid(
parsedUuid
);
groups.forEach(group => {
void group.addKeyChange('addKeyChange - group fan-out', parsedUuid);
});
}
// Drop a member from sender key distribution list.
const senderKeyInfo = this.get('senderKeyInfo');
if (senderKeyInfo) {
const updatedSenderKeyInfo = {
...senderKeyInfo,
memberDevices: senderKeyInfo.memberDevices.filter(
({ identifier }) => {
return identifier !== keyChangedIdString;
}
),
};
this.set('senderKeyInfo', updatedSenderKeyInfo);
window.Signal.Data.updateConversation(this.attributes);
}
});
}
async addPhoneNumberDiscovery(e164: string): Promise<void> {
log.info(
`addPhoneNumberDiscovery/${this.idForLogging()}: Adding for ${e164}`
);
const timestamp = Date.now();
const message: MessageAttributesType = {
id: generateGuid(),
conversationId: this.id,
type: 'phone-number-discovery',
sent_at: timestamp,
timestamp,
received_at: window.Signal.Util.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, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
forceSave: true,
});
const model = window.MessageController.register(
id,
new window.Whisper.Message({
...message,
id,
})
);
this.trigger('newmessage', model);
}
async addConversationMerge(
renderInfo: ConversationRenderInfoType
): Promise<void> {
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: window.Signal.Util.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, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
forceSave: true,
});
const model = window.MessageController.register(
id,
new window.Whisper.Message({
...message,
id,
})
);
this.trigger('newmessage', model);
}
async addVerifiedChange(
verifiedChangeId: string,
verified: boolean,
options: { local?: boolean } = { local: true }
): Promise<void> {
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 shouldBeUnseen = !options.local && !verified;
const timestamp = Date.now();
const message: MessageAttributesType = {
id: generateGuid(),
conversationId: this.id,
local: Boolean(options.local),
readStatus: shouldBeUnseen ? ReadStatus.Unread : ReadStatus.Read,
received_at_ms: timestamp,
received_at: window.Signal.Util.incrementMessageCounter(),
seenStatus: shouldBeUnseen ? SeenStatus.Unseen : SeenStatus.Unseen,
sent_at: lastMessage,
timestamp,
type: 'verified-change',
verified,
verifiedChanged: verifiedChangeId,
};
await window.Signal.Data.saveMessage(message, {
2021-12-20 21:04:02 +00:00
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
forceSave: true,
2021-12-20 21:04:02 +00:00
});
const model = window.MessageController.register(
message.id,
new window.Whisper.Message(message)
);
this.trigger('newmessage', model);
void this.updateUnread();
2021-10-26 22:59:08 +00:00
const uuid = this.getUuid();
if (isDirectConversation(this.attributes) && uuid) {
void window.ConversationController.getAllGroupsInvolvingUuid(uuid).then(
groups => {
2022-11-30 00:53:39 +00:00
groups.forEach(group => {
void group.addVerifiedChange(this.id, verified, options);
});
}
);
}
}
async addCallHistory(
2022-01-28 19:55:36 +00:00
callHistoryDetails: CallHistoryDetailsType,
receivedAtCounter: number | undefined
): Promise<void> {
let timestamp: number;
let unread: boolean;
let detailsToSave: CallHistoryDetailsType;
switch (callHistoryDetails.callMode) {
2023-01-10 00:52:01 +00:00
case CallMode.Direct: {
2023-01-11 20:04:03 +00:00
log.info(
`addCallHistory: Adding direct call to history (Call ID ${callHistoryDetails.callId})`
);
2023-01-10 00:52:01 +00:00
const resolvedTime =
callHistoryDetails.acceptedTime ?? callHistoryDetails.endedTime;
assertDev(resolvedTime, 'Direct call must have accepted or ended time');
timestamp = resolvedTime;
unread =
!callHistoryDetails.wasDeclined && !callHistoryDetails.acceptedTime;
detailsToSave = {
...callHistoryDetails,
callMode: CallMode.Direct,
};
break;
2023-01-10 00:52:01 +00:00
}
case CallMode.Group:
timestamp = callHistoryDetails.startedTime;
unread = false;
detailsToSave = callHistoryDetails;
break;
default:
throw missingCaseError(callHistoryDetails);
}
2021-11-11 22:43:05 +00:00
const message = {
conversationId: this.id,
type: 'call-history',
sent_at: timestamp,
2022-01-28 19:55:36 +00:00
received_at:
receivedAtCounter || window.Signal.Util.incrementMessageCounter(),
2021-03-04 21:44:57 +00:00
received_at_ms: timestamp,
readStatus: unread ? ReadStatus.Unread : ReadStatus.Read,
seenStatus: unread ? SeenStatus.Unseen : SeenStatus.NotApplicable,
callHistoryDetails: detailsToSave,
// TODO: DESKTOP-722
2021-11-11 22:43:05 +00:00
} as unknown as MessageAttributesType;
2023-01-10 00:52:01 +00:00
if (callHistoryDetails.callMode === CallMode.Direct) {
const messageId = await window.Signal.Data.getCallHistoryMessageByCallId(
this.id,
callHistoryDetails.callId
);
if (messageId != null) {
2023-01-11 20:04:03 +00:00
log.info(
`addCallHistory: Found existing call history message (Call ID ${callHistoryDetails.callId}, Message ID: ${messageId})`
);
2023-01-10 00:52:01 +00:00
message.id = messageId;
}
}
2021-12-20 21:04:02 +00:00
const id = await window.Signal.Data.saveMessage(message, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
2023-01-10 00:52:01 +00:00
const model = window.MessageController.register(
id,
new window.Whisper.Message({
...message,
id,
})
);
this.trigger('newmessage', model);
void this.updateUnread();
if (this.get('isArchived')) {
this.setArchived(false);
}
}
/**
* Adds a group call history message if one is needed. It won't add history messages for
* the same group call era ID.
*
* Resolves with `true` if a new message was added, and `false` otherwise.
*/
async updateCallHistoryForGroupCall(
eraId: string,
creatorUuid: string
): Promise<boolean> {
// We want to update the cache quickly in case this function is called multiple times.
const oldCachedEraId = this.cachedLatestGroupCallEraId;
this.cachedLatestGroupCallEraId = eraId;
const alreadyHasMessage =
(oldCachedEraId && oldCachedEraId === eraId) ||
(await window.Signal.Data.hasGroupCallHistoryMessage(this.id, eraId));
if (alreadyHasMessage) {
void this.updateLastMessage();
return false;
}
2022-01-28 19:55:36 +00:00
await this.addCallHistory(
{
callMode: CallMode.Group,
creatorUuid,
eraId,
startedTime: Date.now(),
},
undefined
);
return true;
}
async addProfileChange(
profileChange: unknown,
conversationId?: string
): Promise<void> {
2021-03-04 21:44:57 +00:00
const now = Date.now();
2021-11-11 22:43:05 +00:00
const message = {
conversationId: this.id,
type: 'profile-change',
2021-03-04 21:44:57 +00:00
sent_at: now,
received_at: window.Signal.Util.incrementMessageCounter(),
received_at_ms: now,
readStatus: ReadStatus.Read,
seenStatus: SeenStatus.NotApplicable,
changedId: conversationId || this.id,
profileChange,
// TODO: DESKTOP-722
2021-11-11 22:43:05 +00:00
} as unknown as MessageAttributesType;
2021-12-20 21:04:02 +00:00
const id = await window.Signal.Data.saveMessage(message, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
const model = window.MessageController.register(
id,
new window.Whisper.Message({
...message,
id,
})
);
this.trigger('newmessage', model);
2021-10-26 22:59:08 +00:00
const uuid = this.getUuid();
if (isDirectConversation(this.attributes) && uuid) {
void window.ConversationController.getAllGroupsInvolvingUuid(uuid).then(
groups => {
2022-11-30 00:53:39 +00:00
groups.forEach(group => {
void group.addProfileChange(profileChange, this.id);
});
}
);
}
}
2021-08-05 23:34:49 +00:00
async addNotification(
type: MessageAttributesType['type'],
extra: Partial<MessageAttributesType> = {}
): Promise<string> {
2021-06-01 20:45:43 +00:00
const now = Date.now();
2021-08-05 23:34:49 +00:00
const message: Partial<MessageAttributesType> = {
2021-06-01 20:45:43 +00:00
conversationId: this.id,
2021-08-05 23:34:49 +00:00
type,
2021-06-01 20:45:43 +00:00
sent_at: now,
received_at: window.Signal.Util.incrementMessageCounter(),
received_at_ms: now,
readStatus: ReadStatus.Read,
seenStatus: SeenStatus.NotApplicable,
...extra,
2021-08-05 23:34:49 +00:00
};
2021-06-01 20:45:43 +00:00
2021-08-05 23:34:49 +00:00
const id = await window.Signal.Data.saveMessage(
// TODO: DESKTOP-722
2021-12-20 21:04:02 +00:00
message as MessageAttributesType,
{
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
}
2021-08-05 23:34:49 +00:00
);
2021-06-01 20:45:43 +00:00
const model = window.MessageController.register(
id,
new window.Whisper.Message({
2021-08-05 23:34:49 +00:00
...(message as MessageAttributesType),
2021-06-01 20:45:43 +00:00
id,
})
);
this.trigger('newmessage', model);
return id;
}
async maybeSetPendingUniversalTimer(
hasUserInitiatedMessages: boolean
): Promise<void> {
if (!isDirectConversation(this.attributes)) {
2021-06-01 20:45:43 +00:00
return;
}
if (this.isSMSOnly()) {
return;
}
2022-11-09 02:38:19 +00:00
if (isSignalConversation(this.attributes)) {
return;
}
if (hasUserInitiatedMessages) {
await this.maybeRemoveUniversalTimer();
2021-06-01 20:45:43 +00:00
return;
}
if (this.get('pendingUniversalTimer') || this.get('expireTimer')) {
2021-06-01 20:45:43 +00:00
return;
}
const expireTimer = universalExpireTimer.get();
if (!expireTimer) {
return;
}
log.info(
`maybeSetPendingUniversalTimer(${this.idForLogging()}): added notification`
);
2021-08-05 23:34:49 +00:00
const notificationId = await this.addNotification(
'universal-timer-notification'
);
2021-06-01 20:45:43 +00:00
this.set('pendingUniversalTimer', notificationId);
}
async maybeApplyUniversalTimer(): Promise<void> {
// Check if we had a notification
if (!(await this.maybeRemoveUniversalTimer())) {
2021-06-01 20:45:43 +00:00
return;
}
// We already have an expiration timer
if (this.get('expireTimer')) {
2021-06-01 20:45:43 +00:00
return;
}
const expireTimer = universalExpireTimer.get();
if (expireTimer) {
log.info(
`maybeApplyUniversalTimer(${this.idForLogging()}): applying timer`
);
await this.updateExpirationTimer(expireTimer, {
reason: 'maybeApplyUniversalTimer',
});
}
}
async maybeRemoveUniversalTimer(): Promise<boolean> {
const notificationId = this.get('pendingUniversalTimer');
if (!notificationId) {
return false;
2021-06-01 20:45:43 +00:00
}
this.set('pendingUniversalTimer', undefined);
log.info(
`maybeRemoveUniversalTimer(${this.idForLogging()}): removed notification`
);
const message = window.MessageController.getById(notificationId);
if (message) {
await window.Signal.Data.removeMessage(message.id);
}
return true;
2021-06-01 20:45:43 +00:00
}
async addChangeNumberNotification(
oldValue: string,
newValue: string
): Promise<void> {
2021-10-26 22:59:08 +00:00
const sourceUuid = this.getCheckedUuid(
'Change number notification without uuid'
);
const { storage } = window.textsecure;
if (storage.user.getOurUuidKind(sourceUuid) !== UUIDKind.Unknown) {
log.info(
`Conversation ${this.idForLogging()}: not adding change number ` +
'notification for ourselves'
);
return;
}
log.info(
`Conversation ${this.idForLogging()}: adding change number ` +
`notification for ${sourceUuid.toString()} from ${oldValue} to ${newValue}`
);
2021-08-05 23:34:49 +00:00
const convos = [
this,
2021-10-26 22:59:08 +00:00
...(await window.ConversationController.getAllGroupsInvolvingUuid(
sourceUuid
)),
2021-08-05 23:34:49 +00:00
];
await Promise.all(
convos.map(convo => {
return convo.addNotification('change-number-notification', {
readStatus: ReadStatus.Read,
seenStatus: SeenStatus.Unseen,
2021-10-26 22:59:08 +00:00
sourceUuid: sourceUuid.toString(),
2021-08-05 23:34:49 +00:00
});
})
);
}
async onReadMessage(message: MessageModel, readAt?: number): Promise<void> {
// 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: message.get('sent_at'),
sendReadReceipts: false,
readAt,
})
);
}
override validate(attributes = this.attributes): string | null {
2022-07-13 00:37:21 +00:00
return validateConversation(attributes);
}
2022-05-23 22:08:13 +00:00
queueJob<T>(
name: string,
callback: (abortSignal: AbortSignal) => Promise<T>
): Promise<T> {
2022-08-25 05:04:42 +00:00
this.jobQueue = this.jobQueue || new PQueue({ concurrency: 1 });
const logId = `conversation.queueJob(${this.idForLogging()}, ${name})`;
const taskWithTimeout = createTaskWithTimeout(callback, logId);
2022-05-23 22:08:13 +00:00
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 {
2022-05-23 22:08:13 +00:00
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`);
}
}
});
}
2022-07-08 20:46:25 +00:00
isAdmin(uuid: UUID): boolean {
if (!isGroupV2(this.attributes)) {
return false;
}
const members = this.get('membersV2') || [];
2022-07-08 20:46:25 +00:00
const member = members.find(x => x.uuid === uuid.toString());
if (!member) {
return false;
}
2021-07-02 19:21:24 +00:00
const MEMBER_ROLES = Proto.Member.Role;
return member.role === MEMBER_ROLES.ADMINISTRATOR;
}
2021-10-26 22:59:08 +00:00
getUuid(): UUID | undefined {
try {
const value = this.get('uuid');
return value ? new UUID(value) : undefined;
} catch (err) {
log.warn(
`getUuid(): failed to obtain conversation(${this.id}) uuid due to`,
Errors.toLogFormat(err)
);
return undefined;
}
2021-10-26 22:59:08 +00:00
}
getCheckedUuid(reason: string): UUID {
const result = this.getUuid();
strictAssert(result !== undefined, reason);
return result;
}
private getDraftBodyRanges = memoizeByThis(
(): DraftBodyRangesType | undefined => {
return this.get('draftBodyRanges');
}
);
private getLastMessage = memoizeByThis((): LastMessageType | undefined => {
if (this.get('lastMessageDeletedForEveryone')) {
return { deletedForEveryone: true };
}
const lastMessageText = this.get('lastMessage');
if (!lastMessageText) {
return undefined;
}
return {
status: dropNull(this.get('lastMessageStatus')),
text: lastMessageText,
author: dropNull(this.get('lastMessageAuthor')),
deletedForEveryone: false,
};
});
private getMemberships = memoizeByThis(
(): ReadonlyArray<{
uuid: UUIDStringType;
isAdmin: boolean;
}> => {
if (!isGroupV2(this.attributes)) {
return EMPTY_ARRAY;
}
const members = this.get('membersV2') || [];
return members.map(member => ({
isAdmin: member.role === Proto.Member.Role.ADMINISTRATOR,
uuid: member.uuid,
}));
}
);
getGroupLink(): string | undefined {
if (!isGroupV2(this.attributes)) {
return undefined;
}
if (!this.get('groupInviteLinkPassword')) {
return undefined;
}
return window.Signal.Groups.buildGroupLink(this);
}
private getPendingMemberships = memoizeByThis(
(): ReadonlyArray<{
addedByUserId?: UUIDStringType;
uuid: UUIDStringType;
}> => {
if (!isGroupV2(this.attributes)) {
return EMPTY_ARRAY;
}
const members = this.get('pendingMembersV2') || [];
return members.map(member => ({
addedByUserId: member.addedByUserId,
uuid: member.uuid,
}));
}
);
private getPendingApprovalMemberships = memoizeByThis(
(): ReadonlyArray<{ uuid: UUIDStringType }> => {
if (!isGroupV2(this.attributes)) {
return EMPTY_ARRAY;
}
const members = this.get('pendingAdminApprovalV2') || [];
return members.map(member => ({
uuid: member.uuid,
}));
}
);
private getBannedMemberships = memoizeByThis(
(): ReadonlyArray<UUIDStringType> => {
if (!isGroupV2(this.attributes)) {
return EMPTY_ARRAY;
}
return (this.get('bannedMembersV2') || []).map(member => member.uuid);
}
);
2020-10-06 17:06:34 +00:00
getMembers(
options: { includePendingMembers?: boolean } = {}
2020-11-03 01:19:52 +00:00
): Array<ConversationModel> {
return compact(
getConversationMembers(this.attributes, options).map(conversationAttrs =>
window.ConversationController.get(conversationAttrs.id)
)
);
}
2021-07-20 20:18:35 +00:00
canBeAnnouncementGroup(): boolean {
if (!isGroupV2(this.attributes)) {
return false;
}
if (!isAnnouncementGroupReady()) {
return false;
}
return true;
2021-07-20 20:18:35 +00:00
}
getMemberIds(): Array<string> {
const members = this.getMembers();
return members.map(member => member.id);
}
2021-10-26 22:59:08 +00:00
getMemberUuids(): Array<UUID> {
const members = this.getMembers();
return members.map(member => {
return member.getCheckedUuid('Group member without uuid');
});
}
getRecipients({
includePendingMembers,
extraConversationsForSend,
isStoryReply = false,
}: {
includePendingMembers?: boolean;
extraConversationsForSend?: ReadonlyArray<string>;
isStoryReply?: boolean;
} = {}): Array<string> {
2022-07-01 00:52:03 +00:00
return getRecipients(this.attributes, {
includePendingMembers,
extraConversationsForSend,
isStoryReply,
2022-07-01 00:52:03 +00:00
});
}
// Members is all people in the group
getMemberConversationIds(): Set<string> {
return new Set(map(this.getMembers(), conversation => conversation.id));
}
async getQuoteAttachment(
2022-07-13 00:37:21 +00:00
attachments?: Array<AttachmentType>,
preview?: Array<LinkPreviewType>,
sticker?: StickerType
): Promise<
Array<{
contentType: MIMEType;
fileName: string | null;
thumbnail: ThumbnailType | null;
}>
> {
if (attachments && attachments.length) {
const attachmentsToUse = Array.from(take(attachments, 1));
2021-09-22 18:15:48 +00:00
const isGIFQuote = isGIF(attachmentsToUse);
2021-05-18 18:07:00 +00:00
return Promise.all(
2021-05-18 18:07:00 +00:00
map(attachmentsToUse, async attachment => {
const { path, fileName, thumbnail, contentType } = attachment;
if (!path) {
return {
contentType: isGIFQuote ? IMAGE_GIF : contentType,
// Our protos library complains about this field being undefined, so we
// force it to null
fileName: fileName || null,
thumbnail: null,
};
}
2021-05-18 18:07:00 +00:00
return {
2021-09-22 18:15:48 +00:00
contentType: isGIFQuote ? IMAGE_GIF : contentType,
2021-05-18 18:07:00 +00:00
// Our protos library complains about this field being undefined, so we force
// it to null
fileName: fileName || null,
thumbnail: thumbnail
? {
...(await loadAttachmentData(thumbnail)),
2022-07-13 00:37:21 +00:00
objectUrl: thumbnail.path
? getAbsoluteAttachmentPath(thumbnail.path)
: undefined,
2021-05-18 18:07:00 +00:00
}
: null,
};
})
);
}
if (preview && preview.length) {
const previewImages = collect(preview, prev => prev.image);
const previewImagesToUse = take(previewImages, 1);
2021-05-18 18:07:00 +00:00
return Promise.all(
map(previewImagesToUse, async image => {
2021-05-18 18:07:00 +00:00
const { contentType } = image;
return {
contentType,
// Our protos library complains about this field being undefined, so we
// force it to null
fileName: null,
thumbnail: image
? {
...(await loadAttachmentData(image)),
2022-07-13 00:37:21 +00:00
objectUrl: image.path
? getAbsoluteAttachmentPath(image.path)
: undefined,
2021-05-18 18:07:00 +00:00
}
: null,
};
})
);
}
if (sticker && sticker.data && sticker.data.path) {
const { path, contentType } = sticker.data;
return [
{
contentType,
// Our protos library complains about this field being undefined, so we
// force it to null
fileName: null,
thumbnail: {
...(await loadAttachmentData(sticker.data)),
2022-07-13 00:37:21 +00:00
objectUrl: path ? getAbsoluteAttachmentPath(path) : undefined,
},
},
];
}
return [];
}
2021-08-30 21:32:56 +00:00
async makeQuote(quotedMessage: MessageModel): Promise<QuotedMessageType> {
const { getName } = EmbeddedContact;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contact = getContact(quotedMessage.attributes)!;
const attachments = quotedMessage.get('attachments');
const preview = quotedMessage.get('preview');
const sticker = quotedMessage.get('sticker');
const body = quotedMessage.get('body');
const embeddedContact = quotedMessage.get('contact');
const embeddedContactName =
embeddedContact && embeddedContact.length > 0
? getName(embeddedContact[0])
: '';
return {
authorUuid: contact.get('uuid'),
attachments: isTapToView(quotedMessage.attributes)
2021-09-22 18:15:48 +00:00
? [{ contentType: IMAGE_JPEG, fileName: null }]
: await this.getQuoteAttachment(attachments, preview, sticker),
2022-11-30 21:47:54 +00:00
payment: quotedMessage.get('payment'),
2021-06-25 16:08:16 +00:00
bodyRanges: quotedMessage.get('bodyRanges'),
2021-07-09 19:36:10 +00:00
id: quotedMessage.get('sent_at'),
2021-06-25 16:08:16 +00:00
isViewOnce: isTapToView(quotedMessage.attributes),
2022-05-11 20:59:58 +00:00
isGiftBadge: isGiftBadge(quotedMessage.attributes),
2021-06-25 16:08:16 +00:00
messageId: quotedMessage.get('id'),
referencedMessageNotFound: false,
text: body || embeddedContactName,
};
}
async sendStickerMessage(packId: string, stickerId: number): Promise<void> {
2021-07-09 19:36:10 +00:00
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, path, width, height } = stickerData;
const data = await readStickerData(path);
2020-09-28 18:40:26 +00:00
// We need this content type to be an image so we can display an `<img>` instead of a
// `<video>` or an error, but it's not critical that we get the full type correct.
// In other words, it's probably fine if we say that a GIF is `image/png`, but it's
// but it's bad if we say it's `video/mp4` or `text/plain`. We do our best to sniff
// the MIME type here, but it's okay if we have to use a possibly-incorrect
// fallback.
let contentType: MIMEType;
const sniffedMimeType = sniffImageMimeType(data);
2020-09-28 18:40:26 +00:00
if (sniffedMimeType) {
contentType = sniffedMimeType;
} else {
log.warn(
'sendStickerMessage: Unable to sniff sticker MIME type; falling back to WebP'
);
2020-09-28 18:40:26 +00:00
contentType = IMAGE_WEBP;
}
2022-07-13 00:37:21 +00:00
const sticker: StickerWithHydratedData = {
packId,
stickerId,
packKey: key,
emoji,
data: {
size: data.byteLength,
data,
2020-09-28 18:40:26 +00:00
contentType,
width,
height,
blurHash: await window.imageToBlurHash(
new Blob([data], {
type: IMAGE_JPEG,
})
),
},
};
drop(
this.enqueueMessageForSend({
body: undefined,
attachments: [],
sticker,
})
);
window.reduxActions.stickers.useSticker(packId, stickerId);
}
async sendProfileKeyUpdate(): Promise<void> {
2022-02-16 18:36:21 +00:00
if (isMe(this.attributes)) {
return;
}
if (!this.get('profileSharing')) {
log.error(
2022-02-16 18:36:21 +00:00
'sendProfileKeyUpdate: profileSharing not enabled for conversation',
this.idForLogging()
);
return;
}
2022-02-16 18:36:21 +00:00
try {
await conversationJobQueue.add({
type: conversationQueueJobEnum.enum.ProfileKey,
conversationId: this.id,
revision: this.get('revision'),
});
} catch (error) {
log.error(
2022-02-16 18:36:21 +00:00
'sendProfileKeyUpdate: Failed to queue profile share',
Errors.toLogFormat(error)
);
}
}
async enqueueMessageForSend(
{
attachments,
body,
contact,
mentions,
preview,
quote,
sticker,
}: {
attachments: Array<AttachmentType>;
body: string | undefined;
contact?: Array<ContactWithHydratedAvatar>;
mentions?: BodyRangesType;
preview?: Array<LinkPreviewType>;
quote?: QuotedMessageType;
2022-07-13 00:37:21 +00:00
sticker?: StickerWithHydratedData;
},
{
dontClearDraft,
2021-06-25 16:08:16 +00:00
sendHQImages,
2022-03-04 21:14:52 +00:00
storyId,
timestamp,
2022-05-23 22:04:33 +00:00
extraReduxActions,
2021-06-25 16:08:16 +00:00
}: {
dontClearDraft?: boolean;
sendHQImages?: boolean;
2022-03-04 21:14:52 +00:00
storyId?: string;
2021-06-25 16:08:16 +00:00
timestamp?: number;
2022-05-23 22:04:33 +00:00
extraReduxActions?: () => void;
2021-06-25 16:08:16 +00:00
} = {}
2022-04-15 00:08:46 +00:00
): Promise<MessageAttributesType | undefined> {
2021-04-28 20:44:48 +00:00
if (this.isGroupV1AndDisabled()) {
return;
}
2022-11-09 02:38:19 +00:00
if (isSignalConversation(this.attributes)) {
return;
}
const now = timestamp || Date.now();
log.info(
'Sending message to conversation',
this.idForLogging(),
'with timestamp',
now
);
this.clearTypingTimers();
2021-04-27 22:35:35 +00:00
const mandatoryProfileSharingEnabled = window.Signal.RemoteConfig.isEnabled(
'desktop.mandatoryProfileSharing'
);
2022-11-01 18:58:07 +00:00
let expirationStartTimestamp: number | undefined;
2022-11-16 20:18:02 +00:00
let expireTimer: DurationInSeconds | undefined;
2022-11-01 18:58:07 +00:00
// If it's a group story reply then let's match the expiration timers
// with the parent story's expiration.
if (storyId && isGroup(this.attributes)) {
const parentStory = await getMessageById(storyId);
expirationStartTimestamp =
parentStory?.expirationStartTimestamp || Date.now();
2022-11-16 20:18:02 +00:00
expireTimer = parentStory?.expireTimer || DurationInSeconds.DAY;
2022-11-01 18:58:07 +00:00
} else {
await this.maybeApplyUniversalTimer();
expireTimer = this.get('expireTimer');
}
const recipientMaybeConversations = map(
this.getRecipients({
isStoryReply: storyId !== undefined,
}),
identifier => window.ConversationController.get(identifier)
);
const recipientConversations = filter(
recipientMaybeConversations,
isNotNil
);
const recipientConversationIds = concat(
map(recipientConversations, c => c.id),
[window.ConversationController.getOurConversationIdOrThrow()]
);
// If there are link previews present in the message we shouldn't include
// any attachments as well.
const attachmentsToSend = preview && preview.length ? [] : attachments;
if (preview && preview.length) {
attachments.forEach(attachment => {
if (attachment.path) {
void deleteAttachmentData(attachment.path);
}
});
}
// Here we move attachments to disk
const attributes = await upgradeMessageSchema({
id: UUID.generate().toString(),
timestamp: now,
type: 'outgoing',
body,
conversationId: this.id,
contact,
quote,
preview,
attachments: attachmentsToSend,
sent_at: now,
received_at: window.Signal.Util.incrementMessageCounter(),
received_at_ms: now,
2022-11-01 18:58:07 +00:00
expirationStartTimestamp,
expireTimer,
readStatus: ReadStatus.Read,
seenStatus: SeenStatus.NotApplicable,
sticker,
bodyRanges: mentions,
sendHQImages,
sendStateByConversationId: zipObject(
recipientConversationIds,
repeat({
status: SendStatus.Pending,
updatedAt: now,
})
),
2022-03-04 21:14:52 +00:00
storyId,
});
const model = new window.Whisper.Message(attributes);
const message = window.MessageController.register(model.id, model);
message.cachedOutgoingContactData = contact;
message.cachedOutgoingPreviewData = preview;
message.cachedOutgoingQuoteData = quote;
message.cachedOutgoingStickerData = sticker;
2021-04-27 22:35:35 +00:00
const dbStart = Date.now();
strictAssert(
typeof message.attributes.timestamp === 'number',
'Expected a timestamp'
);
2022-02-16 18:36:21 +00:00
await conversationJobQueue.add(
{
type: conversationQueueJobEnum.enum.NormalMessage,
conversationId: this.id,
messageId: message.id,
revision: this.get('revision'),
},
async jobToInsert => {
log.info(
`enqueueMessageForSend: saving message ${message.id} and job ${jobToInsert.id}`
);
await window.Signal.Data.saveMessage(message.attributes, {
jobToInsert,
forceSave: true,
2021-12-20 21:04:02 +00:00
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
}
);
const dbDuration = Date.now() - dbStart;
if (dbDuration > SEND_REPORTING_THRESHOLD_MS) {
log.info(
`ConversationModel(${this.idForLogging()}.sendMessage(${now}): ` +
`db save took ${dbDuration}ms`
);
}
const renderStart = Date.now();
2022-10-20 22:47:38 +00:00
// Perform asynchronous tasks before entering the batching mode
await this.beforeAddSingleMessage(model);
2022-12-19 22:33:55 +00:00
if (sticker) {
await addStickerPackReference(model.id, sticker.packId);
}
2022-10-20 22:47:38 +00:00
this.isInReduxBatch = true;
batchDispatch(() => {
try {
const { clearUnreadMetrics } = window.reduxActions.conversations;
clearUnreadMetrics(this.id);
2022-10-20 22:47:38 +00:00
const enabledProfileSharing = Boolean(
mandatoryProfileSharingEnabled && !this.get('profileSharing')
);
const unarchivedConversation = Boolean(this.get('isArchived'));
this.doAddSingleMessage(model, { isJustSent: true });
const draftProperties = dontClearDraft
? {}
: {
draft: '',
2023-01-03 22:35:59 +00:00
draftBodyRanges: [],
2022-10-20 22:47:38 +00:00
draftTimestamp: null,
lastMessageAuthor: model.getAuthorText(),
2023-01-03 22:35:59 +00:00
lastMessage: model.getNotificationText(),
2022-10-20 22:47:38 +00:00
lastMessageStatus: 'sending' as const,
};
2022-10-20 22:47:38 +00:00
this.set({
...draftProperties,
...(enabledProfileSharing ? { profileSharing: true } : {}),
...this.incrementSentMessageCount({ dry: true }),
active_at: now,
timestamp: now,
...(unarchivedConversation ? { isArchived: false } : {}),
});
2022-05-23 22:04:33 +00:00
2022-10-20 22:47:38 +00:00
if (enabledProfileSharing) {
this.captureChange('enqueueMessageForSend/mandatoryProfileSharing');
}
2022-10-20 22:47:38 +00:00
if (unarchivedConversation) {
this.captureChange('enqueueMessageForSend/unarchive');
}
extraReduxActions?.();
} finally {
this.isInReduxBatch = false;
}
});
const renderDuration = Date.now() - renderStart;
if (renderDuration > SEND_REPORTING_THRESHOLD_MS) {
log.info(
`ConversationModel(${this.idForLogging()}.sendMessage(${now}): ` +
`render save took ${renderDuration}ms`
);
}
window.Signal.Data.updateConversation(this.attributes);
2022-04-15 00:08:46 +00:00
return attributes;
}
// Is this someone who is a contact, or are we sharing our profile with them?
// Or is the person who added us to this group a contact or are we sharing profile
// with them?
isFromOrAddedByTrustedContact(): boolean {
if (isDirectConversation(this.attributes)) {
return Boolean(this.get('name')) || Boolean(this.get('profileSharing'));
}
const addedBy = this.get('addedBy');
if (!addedBy) {
return false;
}
const conv = window.ConversationController.get(addedBy);
if (!conv) {
return false;
}
2020-10-06 17:06:34 +00:00
return Boolean(
isMe(conv.attributes) || conv.get('name') || conv.get('profileSharing')
2020-10-06 17:06:34 +00:00
);
}
async updateLastMessage(): Promise<void> {
if (!this.id) {
return;
}
2021-11-11 22:43:05 +00:00
const ourConversationId =
window.ConversationController.getOurConversationId();
if (!ourConversationId) {
throw new Error('updateLastMessage: Failed to fetch ourConversationId');
}
const conversationId = this.id;
2021-10-26 22:59:08 +00:00
const ourUuid = window.textsecure.storage.user.getCheckedUuid().toString();
const stats = await window.Signal.Data.getConversationMessageStats({
conversationId,
includeStoryReplies: !isGroup(this.attributes),
2021-10-26 22:59:08 +00:00
ourUuid,
});
// This runs as a job to avoid race conditions
drop(
this.queueJob('maybeSetPendingUniversalTimer', async () =>
this.maybeSetPendingUniversalTimer(stats.hasUserInitiatedMessages)
)
);
const { preview, activity } = stats;
let previewMessage: MessageModel | undefined;
let activityMessage: MessageModel | undefined;
// Register the message with MessageController so that if it already exists
// in memory we use that data instead of the data from the db which may
// be out of date.
if (preview) {
previewMessage = window.MessageController.register(preview.id, preview);
}
if (activity) {
activityMessage = window.MessageController.register(
activity.id,
activity
);
}
if (
this.hasDraft() &&
this.get('draftTimestamp') &&
(!previewMessage ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
previewMessage.get('sent_at') < this.get('draftTimestamp')!)
) {
return;
}
const currentTimestamp = this.get('timestamp') || null;
const timestamp = activityMessage
? activityMessage.get('sent_at') ||
activityMessage.get('received_at') ||
currentTimestamp
: currentTimestamp;
this.set({
lastMessage:
(previewMessage ? previewMessage.getNotificationText() : '') || '',
lastMessageAuthor: previewMessage?.getAuthorText(),
lastMessageStatus:
(previewMessage
? getMessagePropStatus(previewMessage.attributes, ourConversationId)
: null) || null,
timestamp,
lastMessageDeletedForEveryone: previewMessage
2020-10-30 17:56:03 +00:00
? previewMessage.get('deletedForEveryone')
: false,
});
window.Signal.Data.updateConversation(this.attributes);
}
setArchived(isArchived: boolean): void {
const before = this.get('isArchived');
this.set({ isArchived });
window.Signal.Data.updateConversation(this.attributes);
const after = this.get('isArchived');
if (Boolean(before) !== Boolean(after)) {
2020-10-02 18:30:43 +00:00
if (after) {
this.unpin();
2020-10-02 18:30:43 +00:00
}
2020-10-15 00:36:31 +00:00
this.captureChange('isArchived');
}
}
setMarkedUnread(markedUnread: boolean): void {
const previousMarkedUnread = this.get('markedUnread');
this.set({ markedUnread });
window.Signal.Data.updateConversation(this.attributes);
if (Boolean(previousMarkedUnread) !== Boolean(markedUnread)) {
this.captureChange('markedUnread');
}
}
async refreshGroupLink(): Promise<void> {
if (!isGroupV2(this.attributes)) {
return;
}
2021-09-24 00:49:05 +00:00
const groupInviteLinkPassword = Bytes.toBase64(
window.Signal.Groups.generateGroupInviteLinkPassword()
);
log.info('refreshGroupLink for conversation', this.idForLogging());
await this.modifyGroupV2({
name: 'updateInviteLinkPassword',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: async () =>
window.Signal.Groups.buildInviteLinkPasswordChange(
this.attributes,
groupInviteLinkPassword
),
});
this.set({ groupInviteLinkPassword });
}
async toggleGroupLink(value: boolean): Promise<void> {
if (!isGroupV2(this.attributes)) {
return;
}
const shouldCreateNewGroupLink =
value && !this.get('groupInviteLinkPassword');
const groupInviteLinkPassword =
this.get('groupInviteLinkPassword') ||
2021-09-24 00:49:05 +00:00
Bytes.toBase64(window.Signal.Groups.generateGroupInviteLinkPassword());
log.info('toggleGroupLink for conversation', this.idForLogging(), value);
2021-07-02 19:21:24 +00:00
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
const addFromInviteLink = value
? ACCESS_ENUM.ANY
: ACCESS_ENUM.UNSATISFIABLE;
if (shouldCreateNewGroupLink) {
await this.modifyGroupV2({
name: 'updateNewGroupLink',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: async () =>
window.Signal.Groups.buildNewGroupLinkChange(
this.attributes,
groupInviteLinkPassword,
addFromInviteLink
),
});
} else {
await this.modifyGroupV2({
name: 'updateAccessControlAddFromInviteLink',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: async () =>
window.Signal.Groups.buildAccessControlAddFromInviteLinkChange(
this.attributes,
addFromInviteLink
),
});
}
this.set({
accessControl: {
addFromInviteLink,
attributes: this.get('accessControl')?.attributes || ACCESS_ENUM.MEMBER,
members: this.get('accessControl')?.members || ACCESS_ENUM.MEMBER,
},
});
if (shouldCreateNewGroupLink) {
this.set({ groupInviteLinkPassword });
}
}
async updateAccessControlAddFromInviteLink(value: boolean): Promise<void> {
if (!isGroupV2(this.attributes)) {
return;
}
2021-07-02 19:21:24 +00:00
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
const addFromInviteLink = value
? ACCESS_ENUM.ADMINISTRATOR
: ACCESS_ENUM.ANY;
await this.modifyGroupV2({
name: 'updateAccessControlAddFromInviteLink',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: async () =>
window.Signal.Groups.buildAccessControlAddFromInviteLinkChange(
this.attributes,
addFromInviteLink
),
});
this.set({
accessControl: {
addFromInviteLink,
attributes: this.get('accessControl')?.attributes || ACCESS_ENUM.MEMBER,
members: this.get('accessControl')?.members || ACCESS_ENUM.MEMBER,
},
});
}
async updateAccessControlAttributes(value: number): Promise<void> {
if (!isGroupV2(this.attributes)) {
return;
}
await this.modifyGroupV2({
name: 'updateAccessControlAttributes',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: async () =>
window.Signal.Groups.buildAccessControlAttributesChange(
this.attributes,
value
),
});
2021-07-02 19:21:24 +00:00
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
this.set({
accessControl: {
addFromInviteLink:
this.get('accessControl')?.addFromInviteLink || ACCESS_ENUM.MEMBER,
attributes: value,
members: this.get('accessControl')?.members || ACCESS_ENUM.MEMBER,
},
});
}
async updateAccessControlMembers(value: number): Promise<void> {
if (!isGroupV2(this.attributes)) {
return;
}
await this.modifyGroupV2({
name: 'updateAccessControlMembers',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
createGroupChange: async () =>
window.Signal.Groups.buildAccessControlMembersChange(
this.attributes,
value
),
});
2021-07-02 19:21:24 +00:00
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
this.set({
accessControl: {
addFromInviteLink:
this.get('accessControl')?.addFromInviteLink || ACCESS_ENUM.MEMBER,
attributes: this.get('accessControl')?.attributes || ACCESS_ENUM.MEMBER,
members: value,
},
});
}
2021-07-20 20:18:35 +00:00
async updateAnnouncementsOnly(value: boolean): Promise<void> {
if (!isGroupV2(this.attributes) || !this.canBeAnnouncementGroup()) {
return;
}
await this.modifyGroupV2({
name: 'updateAnnouncementsOnly',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
2021-07-20 20:18:35 +00:00
createGroupChange: async () =>
window.Signal.Groups.buildAnnouncementsOnlyChange(
this.attributes,
value
),
});
this.set({ announcementsOnly: value });
}
async updateExpirationTimer(
2022-11-16 20:18:02 +00:00
providedExpireTimer: DurationInSeconds | undefined,
{
reason,
receivedAt,
receivedAtMS = Date.now(),
sentAt: providedSentAt,
source: providedSource,
fromSync = false,
isInitialSync = false,
fromGroupUpdate = false,
}: {
reason: string;
receivedAt?: number;
receivedAtMS?: number;
sentAt?: number;
source?: string;
fromSync?: boolean;
isInitialSync?: boolean;
fromGroupUpdate?: boolean;
}
): Promise<boolean | null | MessageModel | void> {
const isSetByOther = providedSource || providedSentAt !== undefined;
2022-11-09 02:38:19 +00:00
if (isSignalConversation(this.attributes)) {
return;
}
if (isGroupV2(this.attributes)) {
if (isSetByOther) {
throw new Error(
'updateExpirationTimer: GroupV2 timers are not updated this way'
);
}
2020-10-06 17:06:34 +00:00
await this.modifyGroupV2({
name: 'updateExpirationTimer',
2022-07-08 20:46:25 +00:00
usingCredentialsFrom: [],
2020-10-06 17:06:34 +00:00
createGroupChange: () =>
this.updateExpirationTimerInGroupV2(providedExpireTimer),
});
return false;
}
if (!isSetByOther && this.isGroupV1AndDisabled()) {
2022-02-16 18:36:21 +00:00
throw new Error(
'updateExpirationTimer: GroupV1 is deprecated; cannot update expiration timer'
);
}
2022-11-16 20:18:02 +00:00
let expireTimer: DurationInSeconds | undefined = providedExpireTimer;
let source = providedSource;
if (this.get('left')) {
return false;
}
if (!expireTimer) {
expireTimer = undefined;
}
if (
this.get('expireTimer') === expireTimer ||
(!expireTimer && !this.get('expireTimer'))
) {
return null;
}
const logId =
`updateExpirationTimer(${this.idForLogging()}, ` +
`${expireTimer || 'disabled'}) ` +
`source=${source ?? '?'} reason=${reason}`;
log.info(`${logId}: updating`);
2022-02-16 18:36:21 +00:00
// if change wasn't made remotely, send it to the number/group
if (!isSetByOther) {
2022-02-16 18:36:21 +00:00
try {
await conversationJobQueue.add({
type: conversationQueueJobEnum.enum.DirectExpirationTimerUpdate,
conversationId: this.id,
expireTimer,
});
} catch (error) {
log.error(
`${logId}: Failed to queue expiration timer update`,
2022-02-16 18:36:21 +00:00
Errors.toLogFormat(error)
);
throw error;
}
}
const ourConversationId =
window.ConversationController.getOurConversationId();
source = source || ourConversationId;
this.set({ expireTimer });
2021-06-01 20:45:43 +00:00
// This call actually removes universal timer notification and clears
// the pending flags.
await this.maybeRemoveUniversalTimer();
2021-06-01 20:45:43 +00:00
window.Signal.Data.updateConversation(this.attributes);
// When we add a disappearing messages notification to the conversation, we want it
// to be above the message that initiated that change, hence the subtraction.
const sentAt = (providedSentAt || receivedAtMS) - 1;
const isFromSyncOperation =
reason === 'group sync' || reason === 'contact sync';
const isFromMe =
window.ConversationController.get(source)?.id === ourConversationId;
const isNoteToSelf = isMe(this.attributes);
const shouldBeRead =
(isInitialSync && isFromSyncOperation) || isFromMe || isNoteToSelf;
2021-11-11 22:43:05 +00:00
const model = new window.Whisper.Message({
conversationId: this.id,
expirationTimerUpdate: {
expireTimer,
source,
fromSync,
fromGroupUpdate,
},
flags: Proto.DataMessage.Flags.EXPIRATION_TIMER_UPDATE,
readStatus: shouldBeRead ? ReadStatus.Read : ReadStatus.Unread,
received_at_ms: receivedAtMS,
received_at: receivedAt ?? window.Signal.Util.incrementMessageCounter(),
seenStatus: shouldBeRead ? SeenStatus.Seen : SeenStatus.Unseen,
sent_at: sentAt,
type: 'timer-notification',
// TODO: DESKTOP-722
2021-11-11 22:43:05 +00:00
} as unknown as MessageAttributesType);
2021-12-20 21:04:02 +00:00
const id = await window.Signal.Data.saveMessage(model.attributes, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
model.set({ id });
const message = window.MessageController.register(id, model);
void this.addSingleMessage(message);
void this.updateUnread();
log.info(
`${logId}: added a notification received_at=${model.get('received_at')}`
);
return message;
}
isSearchable(): boolean {
return !this.get('left');
}
2022-02-16 18:36:21 +00:00
// Deprecated: only applies to GroupV1
async leaveGroup(): Promise<void> {
const { messaging } = window.textsecure;
if (!messaging) {
throw new Error('leaveGroup: Cannot leave v1 group when offline!');
}
2022-02-16 18:36:21 +00:00
if (!isGroupV1(this.attributes)) {
throw new Error(
`leaveGroup: Group ${this.idForLogging()} is not GroupV1!`
);
}
const now = Date.now();
2022-02-16 18:36:21 +00:00
const groupId = this.get('groupId');
2022-02-16 18:36:21 +00:00
if (!groupId) {
throw new Error(`leaveGroup/${this.idForLogging()}: No groupId!`);
}
2022-02-16 18:36:21 +00:00
const groupIdentifiers = this.getRecipients();
this.set({ left: true });
window.Signal.Data.updateConversation(this.attributes);
2022-02-16 18:36:21 +00:00
const model = new window.Whisper.Message({
conversationId: this.id,
group_update: { left: 'You' },
readStatus: ReadStatus.Read,
2022-02-16 18:36:21 +00:00
received_at_ms: now,
received_at: window.Signal.Util.incrementMessageCounter(),
seenStatus: SeenStatus.NotApplicable,
sent_at: now,
type: 'group',
2022-02-16 18:36:21 +00:00
// TODO: DESKTOP-722
} as unknown as MessageAttributesType);
const id = await window.Signal.Data.saveMessage(model.attributes, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
model.set({ id });
const message = window.MessageController.register(model.id, model);
void this.addSingleMessage(message);
2022-02-16 18:36:21 +00:00
const options = await getSendOptions(this.attributes);
void message.send(
2022-02-16 18:36:21 +00:00
handleMessageSend(
messaging.leaveGroup(groupId, groupIdentifiers, options),
2022-02-16 18:36:21 +00:00
{ messageIds: [], sendType: 'legacyGroupChange' }
)
);
}
async markRead(
newestUnreadAt: number,
options: {
readAt?: number;
sendReadReceipts: boolean;
newestSentAt?: number;
} = {
sendReadReceipts: true,
}
2021-05-03 16:38:20 +00:00
): Promise<void> {
await markConversationRead(this.attributes, newestUnreadAt, options);
await this.updateUnread();
}
async updateUnread(): Promise<void> {
const unreadCount = await window.Signal.Data.getTotalUnreadForConversation(
this.id,
{
storyId: undefined,
includeStoryReplies: !isGroup(this.attributes),
}
);
const prevUnreadCount = this.get('unreadCount');
if (prevUnreadCount !== unreadCount) {
this.set({ unreadCount });
window.Signal.Data.updateConversation(this.attributes);
}
}
// This is an expensive operation we use to populate the message request hero row. It
// shows groups the current user has in common with this potential new contact.
async updateSharedGroups(): Promise<void> {
if (!isDirectConversation(this.attributes)) {
return;
}
if (isMe(this.attributes)) {
return;
}
2021-10-26 22:59:08 +00:00
const ourUuid = window.textsecure.storage.user.getCheckedUuid();
const theirUuid = this.getUuid();
if (!theirUuid) {
return;
}
const ourGroups =
await window.ConversationController.getAllGroupsInvolvingUuid(ourUuid);
const sharedGroups = ourGroups
2022-07-08 20:46:25 +00:00
.filter(c => c.hasMember(ourUuid) && c.hasMember(theirUuid))
.sort(
(left, right) =>
(right.get('timestamp') || 0) - (left.get('timestamp') || 0)
);
const sharedGroupNames = sharedGroups.map(conversation =>
conversation.getTitle()
);
this.set({ sharedGroupNames });
}
onChangeProfileKey(): void {
if (isDirectConversation(this.attributes)) {
void this.getProfiles();
}
}
2021-08-30 21:32:56 +00:00
async getProfiles(): Promise<void> {
// request all conversation members' keys
2021-11-11 22:43:05 +00:00
const conversations =
this.getMembers() as unknown as Array<ConversationModel>;
await Promise.all(
conversations.map(conversation =>
getProfile(conversation.get('uuid'), conversation.get('e164'))
)
);
}
async setEncryptedProfileName(
encryptedName: string,
decryptionKey: Uint8Array
): Promise<void> {
if (!encryptedName) {
return;
}
// decrypt
const { given, family } = decryptProfileName(encryptedName, decryptionKey);
// encode
2021-09-24 00:49:05 +00:00
const profileName = given ? Bytes.toString(given) : undefined;
const profileFamilyName = family ? Bytes.toString(family) : undefined;
// set then check for changes
const oldName = this.getProfileName();
const hadPreviousName = Boolean(oldName);
this.set({ profileName, profileFamilyName });
const newName = this.getProfileName();
// Note that we compare the combined names to ensure that we don't present the exact
// same before/after string, even if someone is moving from just first name to
// first/last name in their profile data.
const nameChanged = oldName !== newName;
if (!isMe(this.attributes) && hadPreviousName && nameChanged) {
const change = {
type: 'name',
oldName,
newName,
};
await this.addProfileChange(change);
}
}
async setProfileAvatar(
avatarPath: undefined | null | string,
decryptionKey: Uint8Array
): Promise<void> {
if (isMe(this.attributes)) {
if (avatarPath) {
await window.storage.put('avatarUrl', avatarPath);
} else {
await window.storage.remove('avatarUrl');
}
}
if (!avatarPath) {
this.set({ profileAvatar: undefined });
return;
}
const { messaging } = window.textsecure;
if (!messaging) {
throw new Error('setProfileAvatar: Cannot fetch avatar when offline!');
}
const avatar = await messaging.getAvatar(avatarPath);
// decrypt
const decrypted = decryptProfile(avatar, decryptionKey);
// update the conversation avatar only if hash differs
if (decrypted) {
2021-09-24 00:49:05 +00:00
const newAttributes = await Conversation.maybeUpdateProfileAvatar(
this.attributes,
decrypted,
{
writeNewAttachmentData,
deleteAttachmentData,
doesAttachmentExist,
}
);
this.set(newAttributes);
}
}
async setProfileKey(
profileKey: string | undefined,
{ viaStorageServiceSync = false } = {}
2022-03-09 18:22:34 +00:00
): Promise<boolean> {
// profileKey is a string so we can compare it directly
if (this.get('profileKey') !== profileKey) {
log.info(
`Setting sealedSender to UNKNOWN for conversation ${this.idForLogging()}`
);
this.set({
profileKeyCredential: null,
2022-07-08 20:46:25 +00:00
profileKeyCredentialExpiration: null,
accessKey: null,
sealedSender: SEALED_SENDER.UNKNOWN,
});
2022-03-09 18:22:34 +00:00
// Don't trigger immediate profile fetches when syncing to remote storage
this.set({ profileKey }, { silent: viaStorageServiceSync });
// If our profile key was cleared above, we don't tell our linked devices about it.
// We want linked devices to tell us what it should be, instead of telling them to
// erase their local value.
if (!viaStorageServiceSync && profileKey) {
2020-10-15 00:36:31 +00:00
this.captureChange('profileKey');
}
this.deriveAccessKeyIfNeeded();
2022-03-09 18:22:34 +00:00
// We will update the conversation during storage service sync
if (!viaStorageServiceSync) {
window.Signal.Data.updateConversation(this.attributes);
}
return true;
}
2022-03-09 18:22:34 +00:00
return false;
}
2022-07-08 20:46:25 +00:00
hasProfileKeyCredentialExpired(): boolean {
const profileKey = this.get('profileKey');
if (!profileKey) {
return false;
}
2022-07-08 20:46:25 +00:00
const profileKeyCredential = this.get('profileKeyCredential');
const profileKeyCredentialExpiration = this.get(
'profileKeyCredentialExpiration'
);
if (!profileKeyCredential) {
return true;
2022-07-08 20:46:25 +00:00
}
if (!isNumber(profileKeyCredentialExpiration)) {
const logId = this.idForLogging();
log.warn(`hasProfileKeyCredentialExpired(${logId}): missing expiration`);
return true;
}
const today = toDayMillis(Date.now());
return profileKeyCredentialExpiration <= today;
}
deriveAccessKeyIfNeeded(): void {
const profileKey = this.get('profileKey');
if (!profileKey) {
return;
}
if (this.get('accessKey')) {
return;
}
2021-09-24 00:49:05 +00:00
const profileKeyBuffer = Bytes.fromBase64(profileKey);
const accessKeyBuffer = deriveAccessKey(profileKeyBuffer);
const accessKey = Bytes.toBase64(accessKeyBuffer);
this.set({ accessKey });
}
deriveProfileKeyVersion(): string | undefined {
const profileKey = this.get('profileKey');
if (!profileKey) {
return;
}
const uuid = this.get('uuid');
if (!uuid) {
return;
}
const lastProfile = this.get('lastProfile');
if (lastProfile?.profileKey === profileKey) {
return lastProfile.profileKeyVersion;
}
const profileKeyVersion = Util.zkgroup.deriveProfileKeyVersion(
profileKey,
uuid
);
if (!profileKeyVersion) {
log.warn(
'deriveProfileKeyVersion: Failed to derive profile key version, ' +
'clearing profile key.'
);
void this.setProfileKey(undefined);
return;
}
return profileKeyVersion;
}
async updateLastProfile(
oldValue: ConversationLastProfileType | undefined,
{ profileKey, profileKeyVersion }: ConversationLastProfileType
): Promise<void> {
const lastProfile = this.get('lastProfile');
// Atomic updates only
if (lastProfile !== oldValue) {
return;
}
if (
lastProfile?.profileKey === profileKey &&
lastProfile?.profileKeyVersion === profileKeyVersion
) {
return;
}
log.warn(
'ConversationModel.updateLastProfile: updating for',
this.idForLogging()
);
this.set({ lastProfile: { profileKey, profileKeyVersion } });
await window.Signal.Data.updateConversation(this.attributes);
}
async removeLastProfile(
oldValue: ConversationLastProfileType | undefined
): Promise<void> {
// Atomic updates only
if (this.get('lastProfile') !== oldValue) {
return;
}
log.warn(
'ConversationModel.removeLastProfile: called for',
this.idForLogging()
);
this.set({
lastProfile: undefined,
// We don't have any knowledge of profile anymore. Drop all associated
// data.
about: undefined,
aboutEmoji: undefined,
profileAvatar: undefined,
});
await window.Signal.Data.updateConversation(this.attributes);
}
2022-07-08 20:46:25 +00:00
hasMember(uuid: UUID): boolean {
const members = this.getMembers();
2022-07-08 20:46:25 +00:00
return members.some(member => member.get('uuid') === uuid.toString());
}
fetchContacts(): void {
const members = this.getMembers();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.contactCollection!.reset(members);
}
async destroyMessages(): Promise<void> {
this.set({
lastMessage: null,
lastMessageAuthor: null,
timestamp: null,
active_at: null,
2021-06-01 20:45:43 +00:00
pendingUniversalTimer: undefined,
});
window.Signal.Data.updateConversation(this.attributes);
await window.Signal.Data.removeAllMessagesInConversation(this.id, {
2021-01-13 00:42:15 +00:00
logId: this.idForLogging(),
});
}
getTitle(options?: { isShort?: boolean }): string {
return getTitle(this.attributes, options);
}
getTitleNoDefault(options?: { isShort?: boolean }): string | undefined {
return getTitleNoDefault(this.attributes, options);
}
2020-11-17 15:07:53 +00:00
getProfileName(): string | undefined {
return getProfileName(this.attributes);
}
getNumber(): string {
return getNumber(this.attributes);
}
2021-05-28 16:15:17 +00:00
getColor(): AvatarColorType {
return migrateColor(this.get('color'));
}
getConversationColor(): ConversationColorType | undefined {
return this.get('conversationColor');
2021-06-02 21:05:09 +00:00
}
getCustomColorData(): {
customColor?: CustomColorType;
customColorId?: string;
} {
2021-06-03 18:46:38 +00:00
if (this.getConversationColor() !== 'custom') {
return {
customColor: undefined,
customColorId: undefined,
};
}
2021-06-02 21:05:09 +00:00
return {
customColor: this.get('customColor'),
customColorId: this.get('customColorId'),
2021-06-02 21:05:09 +00:00
};
2021-05-28 16:15:17 +00:00
}
private getAvatarPath(): undefined | string {
const shouldShowProfileAvatar =
isMe(this.attributes) ||
window.storage.get('preferContactAvatars') === false;
const avatar = shouldShowProfileAvatar
? this.get('profileAvatar') || this.get('avatar')
: this.get('avatar') || this.get('profileAvatar');
return avatar?.path || undefined;
}
2021-07-19 19:26:06 +00:00
private getAvatarHash(): undefined | string {
const avatar = isMe(this.attributes)
? this.get('profileAvatar') || this.get('avatar')
: this.get('avatar') || this.get('profileAvatar');
return avatar?.hash || undefined;
}
getAbsoluteAvatarPath(): string | undefined {
const avatarPath = this.getAvatarPath();
2022-11-09 02:38:19 +00:00
if (isSignalConversation(this.attributes)) {
return avatarPath;
}
return avatarPath ? getAbsoluteAttachmentPath(avatarPath) : undefined;
}
getAbsoluteProfileAvatarPath(): string | undefined {
const avatarPath = this.get('profileAvatar')?.path;
return avatarPath ? getAbsoluteAttachmentPath(avatarPath) : undefined;
}
getAbsoluteUnblurredAvatarPath(): string | undefined {
const unblurredAvatarPath = this.get('unblurredAvatarPath');
return unblurredAvatarPath
? getAbsoluteAttachmentPath(unblurredAvatarPath)
: undefined;
}
unblurAvatar(): void {
const avatarPath = this.getAvatarPath();
if (avatarPath) {
this.set('unblurredAvatarPath', avatarPath);
} else {
this.unset('unblurredAvatarPath');
}
}
2020-10-30 17:52:21 +00:00
private canChangeTimer(): boolean {
if (isDirectConversation(this.attributes)) {
return true;
}
if (this.isGroupV1AndDisabled()) {
return false;
}
if (!isGroupV2(this.attributes)) {
return true;
}
2021-07-02 19:21:24 +00:00
const accessControlEnum = Proto.AccessControl.AccessRequired;
const accessControl = this.get('accessControl');
const canAnyoneChangeTimer =
accessControl &&
(accessControl.attributes === accessControlEnum.ANY ||
accessControl.attributes === accessControlEnum.MEMBER);
if (canAnyoneChangeTimer) {
return true;
}
return this.areWeAdmin();
}
canEditGroupInfo(): boolean {
if (!isGroupV2(this.attributes)) {
return false;
}
if (this.get('left')) {
return false;
}
return (
this.areWeAdmin() ||
this.get('accessControl')?.attributes ===
2021-07-02 19:21:24 +00:00
Proto.AccessControl.AccessRequired.MEMBER
);
}
canAddNewMembers(): boolean {
if (!isGroupV2(this.attributes)) {
return false;
}
if (this.get('left')) {
return false;
}
return (
this.areWeAdmin() ||
this.get('accessControl')?.members ===
Proto.AccessControl.AccessRequired.MEMBER
);
}
areWeAdmin(): boolean {
if (!isGroupV2(this.attributes)) {
return false;
}
2021-07-02 19:21:24 +00:00
const memberEnum = Proto.Member.Role;
const members = this.get('membersV2') || [];
2021-11-09 23:48:27 +00:00
const ourUuid = window.textsecure.storage.user.getUuid()?.toString();
2021-10-26 22:59:08 +00:00
const me = members.find(item => item.uuid === ourUuid);
if (!me) {
return false;
}
return me.role === memberEnum.ADMINISTRATOR;
}
// Set of items to captureChanges on:
// [-] uuid
// [-] e164
// [X] profileKey
// [-] identityKey
// [X] verified!
// [-] profileName
// [-] profileFamilyName
// [X] blocked
// [X] whitelisted
// [X] archived
// [X] markedUnread
2021-08-05 12:35:33 +00:00
// [X] dontNotifyForMentionsIfMuted
// [x] firstUnregisteredAt
captureChange(logMessage: string): void {
2022-11-09 02:38:19 +00:00
if (isSignalConversation(this.attributes)) {
return;
}
log.info('storageService[captureChange]', logMessage, this.idForLogging());
this.set({ needsStorageServiceSync: true });
void this.queueJob('captureChange', async () => {
storageServiceUploadJob();
});
}
2022-02-08 18:00:18 +00:00
startMuteTimer({ viaStorageServiceSync = false } = {}): void {
clearTimeoutIfNecessary(this.muteTimer);
this.muteTimer = undefined;
2021-10-06 21:59:34 +00:00
const muteExpiresAt = this.get('muteExpiresAt');
if (isNumber(muteExpiresAt) && muteExpiresAt < Number.MAX_SAFE_INTEGER) {
const delay = muteExpiresAt - Date.now();
if (delay <= 0) {
2022-02-08 18:00:18 +00:00
this.setMuteExpiration(0, { viaStorageServiceSync });
2021-10-06 21:59:34 +00:00
return;
}
this.muteTimer = setTimeout(() => this.setMuteExpiration(0), delay);
}
}
2022-03-04 21:14:52 +00:00
toggleHideStories(): void {
const hideStory = !this.get('hideStory');
log.info(
`toggleHideStories(${this.idForLogging()}): newValue=${hideStory}`
);
this.set({ hideStory });
2022-03-04 21:14:52 +00:00
this.captureChange('hideStory');
window.Signal.Data.updateConversation(this.attributes);
2022-03-04 21:14:52 +00:00
}
2021-04-09 16:19:38 +00:00
setMuteExpiration(
muteExpiresAt = 0,
{ viaStorageServiceSync = false } = {}
): void {
const prevExpiration = this.get('muteExpiresAt');
if (prevExpiration === muteExpiresAt) {
return;
}
this.set({ muteExpiresAt });
2022-02-08 18:00:18 +00:00
// Don't cause duplicate captureChange
this.startMuteTimer({ viaStorageServiceSync: true });
2021-04-09 16:19:38 +00:00
if (!viaStorageServiceSync) {
this.captureChange('mutedUntilTimestamp');
2022-03-09 18:22:34 +00:00
window.Signal.Data.updateConversation(this.attributes);
2021-04-09 16:19:38 +00:00
}
}
isMuted(): boolean {
2022-05-23 18:37:53 +00:00
return isConversationMuted(this.attributes);
}
2021-05-03 16:38:20 +00:00
async notify(
message: Readonly<MessageModel>,
2021-10-13 16:29:15 +00:00
reaction?: Readonly<ReactionModel>
2021-05-03 16:38:20 +00:00
): Promise<void> {
// As a performance optimization don't perform any work if notifications are
// disabled.
2021-09-23 18:16:09 +00:00
if (!notificationService.isEnabled) {
return;
}
if (this.isMuted()) {
2021-08-05 12:35:33 +00:00
if (this.get('dontNotifyForMentionsIfMuted')) {
return;
}
const ourUuid = window.textsecure.storage.user.getUuid()?.toString();
2021-08-05 12:35:33 +00:00
const mentionsMe = (message.get('bodyRanges') || []).some(
range => range.mentionUuid && range.mentionUuid === ourUuid
);
if (!mentionsMe) {
return;
}
}
if (!isIncoming(message.attributes) && !reaction) {
return;
}
const conversationId = this.id;
const isMessageInDirectConversation = isDirectConversation(this.attributes);
const sender = reaction
? window.ConversationController.get(reaction.get('fromId'))
: getContact(message.attributes);
const senderName = sender
? sender.getTitle()
: window.i18n('unknownContact');
const senderTitle = isMessageInDirectConversation
? senderName
: window.i18n('notificationSenderInGroup', {
sender: senderName,
group: this.getTitle(),
});
let notificationIconUrl;
const avatar = this.get('avatar') || this.get('profileAvatar');
if (avatar && avatar.path) {
notificationIconUrl = getAbsoluteAttachmentPath(avatar.path);
} else if (isMessageInDirectConversation) {
notificationIconUrl = await this.getIdenticon();
} else {
// Not technically needed, but helps us be explicit: we don't show an icon for a
// group that doesn't have an icon.
notificationIconUrl = undefined;
}
const messageJSON = message.toJSON();
const messageId = message.id;
const isExpiringMessage = Message.hasExpiration(messageJSON);
2021-09-23 18:16:09 +00:00
notificationService.add({
senderTitle,
conversationId,
storyId: isMessageInDirectConversation
? undefined
: message.get('storyId'),
notificationIconUrl,
isExpiringMessage,
message: message.getNotificationText(),
messageId,
reaction: reaction ? reaction.toJSON() : null,
});
}
private async getIdenticon(): Promise<string> {
const color = this.getColor();
const title = this.getTitle();
const content = (title && getInitials(title)) || '#';
const cached = this.cachedIdenticon;
if (cached && cached.content === content && cached.color === color) {
return cached.url;
}
2021-08-25 00:17:51 +00:00
const url = await createIdenticon(color, content);
2021-08-25 00:17:51 +00:00
this.cachedIdenticon = { content, color, url };
2021-08-25 00:17:51 +00:00
return url;
}
2020-11-20 17:30:45 +00:00
notifyTyping(options: {
isTyping: boolean;
senderId: string;
fromMe: boolean;
2021-07-09 19:36:10 +00:00
senderDevice: number;
2020-11-20 17:30:45 +00:00
}): void {
const { isTyping, senderId, fromMe, senderDevice } = options;
// We don't do anything with typing messages from our other devices
2020-11-20 17:30:45 +00:00
if (fromMe) {
return;
}
2022-07-08 20:46:25 +00:00
const sender = window.ConversationController.get(senderId);
if (!sender) {
return;
}
const senderUuid = sender.getUuid();
if (!senderUuid) {
return;
}
2021-07-20 20:18:35 +00:00
// Drop typing indicators for announcement only groups where the sender
// is not an admin
2022-07-08 20:46:25 +00:00
if (this.get('announcementsOnly') && !this.isAdmin(senderUuid)) {
2021-07-20 20:18:35 +00:00
return;
}
const typingToken = `${senderId}.${senderDevice}`;
this.contactTypingTimers = this.contactTypingTimers || {};
const record = this.contactTypingTimers[typingToken];
if (record) {
clearTimeout(record.timer);
}
if (isTyping) {
this.contactTypingTimers[typingToken] = this.contactTypingTimers[
typingToken
] || {
timestamp: Date.now(),
senderId,
senderDevice,
};
this.contactTypingTimers[typingToken].timer = setTimeout(
this.clearContactTypingTimer.bind(this, typingToken),
15 * 1000
);
if (!record) {
// User was not previously typing before. State change!
this.trigger('change', this, { force: true });
}
} else {
delete this.contactTypingTimers[typingToken];
if (record) {
// User was previously typing, and is no longer. State change!
this.trigger('change', this, { force: true });
}
}
}
clearContactTypingTimer(typingToken: string): void {
this.contactTypingTimers = this.contactTypingTimers || {};
const record = this.contactTypingTimers[typingToken];
if (record) {
clearTimeout(record.timer);
delete this.contactTypingTimers[typingToken];
// User was previously typing, but timed out or we received message. State change!
this.trigger('change', this, { force: true });
}
}
2020-10-02 18:30:43 +00:00
pin(): void {
if (this.get('isPinned')) {
return;
}
log.info('pinning', this.idForLogging());
2020-10-02 18:30:43 +00:00
const pinnedConversationIds = new Set(
window.storage.get('pinnedConversationIds', new Array<string>())
2020-10-02 18:30:43 +00:00
);
2020-10-10 14:25:17 +00:00
pinnedConversationIds.add(this.id);
this.writePinnedConversations([...pinnedConversationIds]);
2020-10-02 18:30:43 +00:00
this.set('isPinned', true);
if (this.get('isArchived')) {
this.set({ isArchived: false });
2020-10-02 18:30:43 +00:00
}
window.Signal.Data.updateConversation(this.attributes);
2020-10-02 18:30:43 +00:00
}
unpin(): void {
if (!this.get('isPinned')) {
return;
}
log.info('un-pinning', this.idForLogging());
2020-10-15 00:36:31 +00:00
2020-10-02 18:30:43 +00:00
const pinnedConversationIds = new Set(
window.storage.get('pinnedConversationIds', new Array<string>())
2020-10-02 18:30:43 +00:00
);
pinnedConversationIds.delete(this.id);
this.writePinnedConversations([...pinnedConversationIds]);
2020-10-10 14:25:17 +00:00
this.set('isPinned', false);
window.Signal.Data.updateConversation(this.attributes);
2020-10-02 18:30:43 +00:00
}
writePinnedConversations(pinnedConversationIds: Array<string>): void {
drop(window.storage.put('pinnedConversationIds', pinnedConversationIds));
2020-10-02 18:30:43 +00:00
const myId = window.ConversationController.getOurConversationId();
const me = window.ConversationController.get(myId);
if (me) {
2020-10-15 00:36:31 +00:00
me.captureChange('pin');
2020-10-02 18:30:43 +00:00
}
}
2021-06-01 23:30:25 +00:00
2021-08-05 12:35:33 +00:00
setDontNotifyForMentionsIfMuted(newValue: boolean): void {
const previousValue = Boolean(this.get('dontNotifyForMentionsIfMuted'));
if (previousValue === newValue) {
return;
}
this.set({ dontNotifyForMentionsIfMuted: newValue });
window.Signal.Data.updateConversation(this.attributes);
this.captureChange('dontNotifyForMentionsIfMuted');
}
2021-06-01 23:30:25 +00:00
acknowledgeGroupMemberNameCollisions(
groupNameCollisions: Readonly<GroupNameCollisionsWithIdsByTitle>
): void {
this.set('acknowledgedGroupNameCollisions', groupNameCollisions);
window.Signal.Data.updateConversation(this.attributes);
}
2022-01-20 00:40:29 +00:00
onOpenStart(): void {
log.info(`conversation ${this.idForLogging()} open start`);
window.ConversationController.onConvoOpenStart(this.id);
}
onOpenComplete(startedAt: number): void {
const now = Date.now();
const delta = now - startedAt;
log.info(`conversation ${this.idForLogging()} open took ${delta}ms`);
window.CI?.handleEvent('conversation:open', { delta });
}
async flushDebouncedUpdates(): Promise<void> {
try {
await this.debouncedUpdateLastMessage?.flush();
} catch (error) {
const logId = this.idForLogging();
log.error(
`flushDebouncedUpdates(${logId}): got error`,
Errors.toLogFormat(error)
);
}
}
2022-08-15 21:53:33 +00:00
getPniSignatureMessage(): PniSignatureMessageType | undefined {
if (!this.get('shareMyPhoneNumber')) {
return undefined;
}
return window.textsecure.storage.protocol.signAlternateIdentity();
}
/** @return only undefined if not a group */
getStorySendMode(): StorySendMode | undefined {
// isDirectConversation is used instead of isGroup because this is what
// used in `format()` when sending conversation "type" to redux.
if (isDirectConversation(this.attributes)) {
return undefined;
}
return this.getGroupStorySendMode();
}
private getGroupStorySendMode(): StorySendMode {
strictAssert(
!isDirectConversation(this.attributes),
'Must be a group to have send story mode'
);
return this.get('storySendMode') ?? StorySendMode.IfActive;
}
}
window.Whisper.Conversation = ConversationModel;
window.Whisper.ConversationCollection = window.Backbone.Collection.extend({
model: window.Whisper.Conversation,
/**
* window.Backbone defines a `_byId` field. Here we set up additional `_byE164`,
* `_byUuid`, and `_byGroupId` fields so we can track conversations by more
* than just their id.
*/
initialize() {
this.eraseLookups();
this.on(
'idUpdated',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(model: ConversationModel, idProp: string, oldValue: any) => {
if (oldValue) {
if (idProp === 'e164') {
delete this._byE164[oldValue];
}
if (idProp === 'uuid') {
delete this._byUuid[oldValue];
}
if (idProp === 'pni') {
delete this._byPni[oldValue];
}
if (idProp === 'groupId') {
delete this._byGroupId[oldValue];
}
}
const e164 = model.get('e164');
if (e164) {
this._byE164[e164] = model;
}
const uuid = model.get('uuid');
if (uuid) {
this._byUuid[uuid] = model;
}
const pni = model.get('pni');
if (pni) {
this._byPni[pni] = model;
}
const groupId = model.get('groupId');
if (groupId) {
this._byGroupId[groupId] = model;
}
}
);
},
2022-07-13 00:37:21 +00:00
reset(models?: Array<ConversationModel>, options?: Backbone.Silenceable) {
window.Backbone.Collection.prototype.reset.call(this, models, options);
this.resetLookups();
},
resetLookups() {
this.eraseLookups();
this.generateLookups(this.models);
},
2021-05-03 16:38:20 +00:00
generateLookups(models: ReadonlyArray<ConversationModel>) {
models.forEach(model => {
const e164 = model.get('e164');
if (e164) {
const existing = this._byE164[e164];
// Prefer the contact with both e164 and uuid
if (!existing || (existing && !existing.get('uuid'))) {
this._byE164[e164] = model;
}
}
const uuid = model.get('uuid');
if (uuid) {
const existing = this._byUuid[uuid];
// Prefer the contact with both e164 and uuid
if (!existing || (existing && !existing.get('e164'))) {
this._byUuid[uuid] = model;
}
}
const pni = model.get('pni');
if (pni) {
const existing = this._byPni[pni];
// Prefer the contact with both uuid and pni
if (!existing || (existing && !existing.get('uuid'))) {
this._byPni[pni] = model;
}
}
const groupId = model.get('groupId');
if (groupId) {
this._byGroupId[groupId] = model;
}
});
},
eraseLookups() {
this._byE164 = Object.create(null);
this._byUuid = Object.create(null);
this._byPni = Object.create(null);
this._byGroupId = Object.create(null);
},
2022-07-13 00:37:21 +00:00
add(
data:
| ConversationModel
| ConversationAttributesType
| Array<ConversationModel>
| Array<ConversationAttributesType>
) {
let hydratedData: Array<ConversationModel> | ConversationModel;
// First, we need to ensure that the data we're working with is Conversation models
if (Array.isArray(data)) {
hydratedData = [];
for (let i = 0, max = data.length; i < max; i += 1) {
const item = data[i];
// We create a new model if it's not already a model
2022-07-13 00:37:21 +00:00
if (has(item, 'get')) {
hydratedData.push(item as ConversationModel);
} else {
2022-07-13 00:37:21 +00:00
hydratedData.push(
new window.Whisper.Conversation(item as ConversationAttributesType)
);
}
}
2022-07-13 00:37:21 +00:00
} else if (has(data, 'get')) {
hydratedData = data as ConversationModel;
} else {
2022-07-13 00:37:21 +00:00
hydratedData = new window.Whisper.Conversation(
data as ConversationAttributesType
);
}
// Next, we update our lookups first to prevent infinite loops on the 'add' event
this.generateLookups(
Array.isArray(hydratedData) ? hydratedData : [hydratedData]
);
// Lastly, we fire off the add events related to this change
2022-07-13 00:37:21 +00:00
// Go home Backbone, you're drunk.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window.Backbone.Collection.prototype.add.call(this, hydratedData as any);
return hydratedData;
},
/**
* window.Backbone collections have a `_byId` field that `get` defers to. Here, we
* override `get` to first access our custom `_byE164`, `_byUuid`, and
* `_byGroupId` functions, followed by falling back to the original
* window.Backbone implementation.
*/
get(id: string) {
return (
this._byE164[id] ||
this._byE164[`+${id}`] ||
this._byUuid[id] ||
this._byPni[id] ||
this._byGroupId[id] ||
window.Backbone.Collection.prototype.get.call(this, id)
);
},
2022-07-13 00:37:21 +00:00
comparator(m: ConversationModel) {
2022-02-04 19:01:09 +00:00
return -(m.get('active_at') || 0);
},
});
type SortableByTitle = {
2020-11-03 01:19:52 +00:00
getTitle: () => string;
};
2020-11-03 01:19:52 +00:00
const sortConversationTitles = (
left: SortableByTitle,
right: SortableByTitle,
collator: Intl.Collator
) => {
2021-03-02 16:27:11 +00:00
return collator.compare(left.getTitle(), right.getTitle());
2020-11-03 01:19:52 +00:00
};