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
|
|
|
|
|
2023-06-02 17:54:36 +00:00
|
|
|
import { compact, has, isNumber, throttle, debounce } from 'lodash';
|
2021-11-01 23:38:08 +00:00
|
|
|
import { batch as batchDispatch } from 'react-redux';
|
2022-06-13 21:29:10 +00:00
|
|
|
import { v4 as generateGuid } from 'uuid';
|
2022-08-25 05:04:42 +00:00
|
|
|
import PQueue from 'p-queue';
|
2022-01-11 19:40:20 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2021-06-25 16:08:16 +00:00
|
|
|
ConversationAttributesType,
|
2022-03-09 20:23:21 +00:00
|
|
|
ConversationLastProfileType,
|
2022-12-05 22:46:54 +00:00
|
|
|
ConversationRenderInfoType,
|
2020-09-24 20:57:54 +00:00
|
|
|
MessageAttributesType,
|
2021-06-25 16:08:16 +00:00
|
|
|
QuotedMessageType,
|
2021-12-10 02:15:59 +00:00
|
|
|
SenderKeyInfoType,
|
2020-09-24 20:57:54 +00:00
|
|
|
} from '../model-types.d';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader, DataWriter } from '../sql/Client';
|
2023-06-02 17:54:36 +00:00
|
|
|
import { getConversation } from '../util/getConversation';
|
2022-12-21 18:41:48 +00:00
|
|
|
import { drop } from '../util/drop';
|
2022-12-23 00:13:23 +00:00
|
|
|
import { isShallowEqual } from '../util/isShallowEqual';
|
2021-10-17 21:46:54 +00:00
|
|
|
import { getInitials } from '../util/getInitials';
|
2022-02-25 18:37:15 +00:00
|
|
|
import { clearTimeoutIfNecessary } from '../util/clearTimeoutIfNecessary';
|
2023-05-16 17:37:12 +00:00
|
|
|
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
|
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';
|
2023-06-02 17:54:36 +00:00
|
|
|
import { areWeAdmin } from '../util/areWeAdmin';
|
|
|
|
import { isBlocked } from '../util/isBlocked';
|
|
|
|
import { getAboutText } from '../util/getAboutText';
|
2024-07-11 19:44:09 +00:00
|
|
|
import {
|
|
|
|
getAvatar,
|
|
|
|
getRawAvatarPath,
|
|
|
|
getLocalAvatarUrl,
|
|
|
|
} from '../util/avatarUtils';
|
2023-06-02 17:54:36 +00:00
|
|
|
import { getDraftPreview } from '../util/getDraftPreview';
|
|
|
|
import { hasDraft } from '../util/hasDraft';
|
2024-07-11 19:44:09 +00:00
|
|
|
import { missingCaseError } from '../util/missingCaseError';
|
2021-09-24 00:49:05 +00:00
|
|
|
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';
|
2022-10-08 00:19:02 +00:00
|
|
|
import { StorySendMode } from '../types/Stories';
|
2023-04-20 16:31:59 +00:00
|
|
|
import type { EmbeddedContactWithHydratedAvatar } from '../types/EmbeddedContact';
|
|
|
|
import type { GroupV2InfoType } from '../textsecure/SendMessage';
|
2021-07-28 21:37:09 +00:00
|
|
|
import createTaskWithTimeout from '../textsecure/TaskWithTimeout';
|
2022-06-13 21:39:35 +00:00
|
|
|
import MessageSender from '../textsecure/SendMessage';
|
2022-08-15 21:53:33 +00:00
|
|
|
import type {
|
|
|
|
CallbackResultType,
|
|
|
|
PniSignatureMessageType,
|
|
|
|
} from '../textsecure/Types.d';
|
2022-12-23 00:13:23 +00:00
|
|
|
import type {
|
|
|
|
ConversationType,
|
2023-04-10 16:31:45 +00:00
|
|
|
DraftPreviewType,
|
2022-12-23 00:13:23 +00:00
|
|
|
} from '../state/ducks/conversations';
|
2021-10-26 19:15:33 +00:00
|
|
|
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';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from './messages';
|
2024-04-29 21:20:20 +00:00
|
|
|
import { getAuthor } from '../messages/helpers';
|
2023-08-09 00:53:06 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2022-05-23 18:37:53 +00:00
|
|
|
import { isConversationMuted } from '../util/isConversationMuted';
|
2021-05-13 20:57:27 +00:00
|
|
|
import { isConversationSMSOnly } from '../util/isConversationSMSOnly';
|
2022-09-19 18:47:49 +00:00
|
|
|
import {
|
2022-11-17 02:15:26 +00:00
|
|
|
isConversationEverUnregistered,
|
2022-09-19 18:47:49 +00:00
|
|
|
isConversationUnregistered,
|
|
|
|
isConversationUnregisteredAndStale,
|
|
|
|
} from '../util/isConversationUnregistered';
|
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';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MIMEType } from '../types/MIME';
|
2023-03-27 23:48:57 +00:00
|
|
|
import { IMAGE_JPEG, IMAGE_WEBP } from '../types/MIME';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { AciString, PniString, ServiceIdString } from '../types/ServiceId';
|
|
|
|
import {
|
|
|
|
ServiceIdKind,
|
|
|
|
normalizeServiceId,
|
|
|
|
normalizePni,
|
|
|
|
} from '../types/ServiceId';
|
2023-09-14 17:04:48 +00:00
|
|
|
import { isAciString } from '../util/isAciString';
|
2022-12-05 22:46:54 +00:00
|
|
|
import {
|
|
|
|
constantTimeEqual,
|
|
|
|
decryptProfile,
|
|
|
|
decryptProfileName,
|
|
|
|
deriveAccessKey,
|
|
|
|
} from '../Crypto';
|
2024-07-11 19:44:09 +00:00
|
|
|
import { decryptAttachmentV2 } from '../AttachmentCrypto';
|
2021-06-22 14:46:42 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2023-04-14 18:16:28 +00:00
|
|
|
import type { DraftBodyRanges } from '../types/BodyRange';
|
2023-06-02 17:54:36 +00:00
|
|
|
import { BodyRange } from '../types/BodyRange';
|
2021-01-07 18:06:09 +00:00
|
|
|
import { migrateColor } from '../util/migrateColor';
|
2021-03-24 21:28:55 +00:00
|
|
|
import { isNotNil } from '../util/isNotNil';
|
2023-08-01 16:06:29 +00:00
|
|
|
import {
|
|
|
|
NotificationType,
|
2024-07-11 19:44:09 +00:00
|
|
|
NotificationSetting,
|
2023-08-01 16:06:29 +00:00
|
|
|
notificationService,
|
|
|
|
} from '../services/notifications';
|
2022-10-08 00:19:02 +00:00
|
|
|
import { storageServiceUploadJob } from '../services/storage';
|
2021-05-07 01:15:25 +00:00
|
|
|
import { getSendOptions } from '../util/getSendOptions';
|
2024-02-02 00:52:44 +00:00
|
|
|
import type { IsConversationAcceptedOptionsType } from '../util/isConversationAccepted';
|
2021-05-07 01:15:25 +00:00
|
|
|
import { isConversationAccepted } from '../util/isConversationAccepted';
|
2022-12-05 22:46:54 +00:00
|
|
|
import {
|
|
|
|
getNumber,
|
|
|
|
getProfileName,
|
|
|
|
getTitle,
|
|
|
|
getTitleNoDefault,
|
2024-03-06 23:59:51 +00:00
|
|
|
hasNumberTitle,
|
|
|
|
hasUsernameTitle,
|
2023-02-02 18:03:51 +00:00
|
|
|
canHaveUsername,
|
2022-12-05 22:46:54 +00:00
|
|
|
} from '../util/getTitle';
|
2021-05-07 01:15:25 +00:00
|
|
|
import { markConversationRead } from '../util/markConversationRead';
|
|
|
|
import { handleMessageSend } from '../util/handleMessageSend';
|
|
|
|
import { getConversationMembers } from '../util/getConversationMembers';
|
2021-05-13 20:57:27 +00:00
|
|
|
import { updateConversationsWithUuidLookup } from '../updateConversationsWithUuidLookup';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { ReadStatus } from '../messages/MessageReadStatus';
|
2021-10-20 23:46:41 +00:00
|
|
|
import { SendStatus } from '../messages/MessageSendState';
|
2023-04-20 16:31:59 +00:00
|
|
|
import type {
|
|
|
|
LinkPreviewType,
|
|
|
|
LinkPreviewWithHydratedData,
|
|
|
|
} from '../types/message/LinkPreviews';
|
2022-12-05 22:46:54 +00:00
|
|
|
import { MINUTE, SECOND, DurationInSeconds } from '../util/durations';
|
2023-03-27 23:48:57 +00:00
|
|
|
import { concat, filter, map, repeat, zipObject } from '../util/iterables';
|
2021-06-01 20:45:43 +00:00
|
|
|
import * as universalExpireTimer from '../util/universalExpireTimer';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { GroupNameCollisionsWithIdsByTitle } from '../util/groupMemberNameCollisions';
|
2021-06-07 16:39:13 +00:00
|
|
|
import {
|
|
|
|
isDirectConversation,
|
2022-04-20 23:33:38 +00:00
|
|
|
isGroup,
|
2021-06-07 16:39:13 +00:00
|
|
|
isGroupV1,
|
|
|
|
isGroupV2,
|
|
|
|
isMe,
|
|
|
|
} from '../util/whatTypeOfConversation';
|
2021-06-22 14:46:42 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2021-06-17 17:15:10 +00:00
|
|
|
import {
|
2022-04-07 21:13:58 +00:00
|
|
|
getMessagePropStatus,
|
2021-06-17 17:15:10 +00:00
|
|
|
hasErrors,
|
|
|
|
isIncoming,
|
2022-04-07 21:13:58 +00:00
|
|
|
isStory,
|
2021-06-17 17:15:10 +00:00
|
|
|
} from '../state/selectors/message';
|
2022-02-16 18:36:21 +00:00
|
|
|
import {
|
|
|
|
conversationJobQueue,
|
|
|
|
conversationQueueJobEnum,
|
|
|
|
} from '../jobs/conversationJobQueue';
|
2023-08-21 20:08:27 +00:00
|
|
|
import type { ReactionAttributesType } from '../messageModifiers/Reactions';
|
2021-07-21 20:45:41 +00:00
|
|
|
import { getProfile } from '../util/getProfile';
|
|
|
|
import { SEALED_SENDER } from '../types/SealedSender';
|
2021-08-25 00:17:51 +00:00
|
|
|
import { createIdenticon } from '../util/createIdenticon';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-10-28 22:38:37 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2021-11-30 11:25:24 +00:00
|
|
|
import { isMessageUnread } from '../util/isMessageUnread';
|
2021-12-10 02:15:59 +00:00
|
|
|
import type { SenderKeyTargetType } from '../util/sendToGroup';
|
2024-02-09 03:56:23 +00:00
|
|
|
import { resetSenderKey, sendContentMessageToGroup } from '../util/sendToGroup';
|
2022-01-14 21:34:52 +00:00
|
|
|
import { singleProtoJobQueue } from '../jobs/singleProtoJobQueue';
|
2022-03-11 22:31:21 +00:00
|
|
|
import { TimelineMessageLoadingState } from '../util/timelineUtil';
|
2022-04-22 18:35:14 +00:00
|
|
|
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';
|
2022-12-05 22:56:23 +00:00
|
|
|
import { removePendingMember } from '../util/removePendingMember';
|
2023-06-02 17:54:36 +00:00
|
|
|
import {
|
|
|
|
isMember,
|
|
|
|
isMemberAwaitingApproval,
|
|
|
|
isMemberBanned,
|
|
|
|
isMemberPending,
|
|
|
|
isMemberRequestingToJoin,
|
|
|
|
} from '../util/groupMembershipUtils';
|
2023-01-13 00:24:59 +00:00
|
|
|
import { imageToBlurHash } from '../util/imageToBlurHash';
|
2023-02-06 17:24:34 +00:00
|
|
|
import { ReceiptType } from '../types/Receipt';
|
2023-03-27 23:48:57 +00:00
|
|
|
import { getQuoteAttachment } from '../util/makeQuote';
|
2023-04-11 03:54:43 +00:00
|
|
|
import { deriveProfileKeyVersion } from '../util/zkgroup';
|
|
|
|
import { incrementMessageCounter } from '../util/incrementMessageCounter';
|
2023-08-01 16:06:29 +00:00
|
|
|
import OS from '../util/os/osMain';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { getMessageAuthorText } from '../util/getMessageAuthorText';
|
2024-03-06 21:49:21 +00:00
|
|
|
import { downscaleOutgoingAttachment } from '../util/attachments';
|
2024-03-12 16:29:31 +00:00
|
|
|
import { MessageRequestResponseEvent } from '../types/MessageRequestResponseEvent';
|
2024-05-28 15:56:00 +00:00
|
|
|
import type { MessageToDelete } from '../textsecure/messageReceiverEvents';
|
|
|
|
import {
|
|
|
|
getConversationToDelete,
|
|
|
|
getMessageToDelete,
|
|
|
|
} from '../util/deleteForMe';
|
2024-05-28 05:13:09 +00:00
|
|
|
import { getCallHistorySelector } from '../state/selectors/callHistory';
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const { Message } = window.Signal.Types;
|
2020-09-24 20:57:54 +00:00
|
|
|
const {
|
2024-07-11 19:44:09 +00:00
|
|
|
copyIntoTempDirectory,
|
2020-09-24 20:57:54 +00:00
|
|
|
deleteAttachmentData,
|
|
|
|
doesAttachmentExist,
|
|
|
|
getAbsoluteAttachmentPath,
|
2023-08-01 16:06:29 +00:00
|
|
|
getAbsoluteTempPath,
|
2020-09-24 20:57:54 +00:00
|
|
|
readStickerData,
|
|
|
|
upgradeMessageSchema,
|
|
|
|
writeNewAttachmentData,
|
|
|
|
} = window.Signal.Migrations;
|
2021-11-30 11:25:24 +00:00
|
|
|
const {
|
2021-12-20 21:05:13 +00:00
|
|
|
getConversationRangeCenteredOnMessage,
|
2021-11-30 11:25:24 +00:00
|
|
|
getOlderMessagesByConversation,
|
|
|
|
getMessageMetricsForConversation,
|
|
|
|
getMessageById,
|
2024-05-28 15:56:00 +00:00
|
|
|
getMostRecentAddressableMessages,
|
2024-06-24 17:49:36 +00:00
|
|
|
getMostRecentAddressableNondisappearingMessages,
|
2021-11-30 11:25:24 +00:00
|
|
|
getNewerMessagesByConversation,
|
2024-07-22 18:16:33 +00:00
|
|
|
} = DataReader;
|
|
|
|
const { addStickerPackReference } = DataWriter;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
const FIVE_MINUTES = MINUTE * 5;
|
2023-03-20 18:03:21 +00:00
|
|
|
const FETCH_TIMEOUT = SECOND * 30;
|
2020-09-29 22:55:56 +00:00
|
|
|
|
2021-06-14 21:55:14 +00:00
|
|
|
const JOB_REPORTING_THRESHOLD_MS = 25;
|
2021-08-26 15:36:08 +00:00
|
|
|
const SEND_REPORTING_THRESHOLD_MS = 25;
|
2021-06-14 21:55:14 +00:00
|
|
|
|
2021-11-30 11:25:24 +00:00
|
|
|
const MESSAGE_LOAD_CHUNK_SIZE = 30;
|
|
|
|
|
2021-05-21 19:53:05 +00:00
|
|
|
const ATTRIBUTES_THAT_DONT_INVALIDATE_PROPS_CACHE = new Set([
|
2022-03-09 20:23:21 +00:00
|
|
|
'lastProfile',
|
2021-05-21 19:53:05 +00:00
|
|
|
'profileLastFetchedAt',
|
2021-10-28 22:38:37 +00:00
|
|
|
'needsStorageServiceSync',
|
|
|
|
'storageID',
|
2022-02-08 18:00:18 +00:00
|
|
|
'storageVersion',
|
2021-10-28 22:38:37 +00:00
|
|
|
'storageUnknownFields',
|
2021-05-21 19:53:05 +00:00
|
|
|
]);
|
|
|
|
|
2021-03-24 13:59:56 +00:00
|
|
|
type CachedIdenticon = {
|
2021-05-28 16:15:17 +00:00
|
|
|
readonly color: AvatarColorType;
|
2023-08-01 16:06:29 +00:00
|
|
|
readonly text?: string;
|
|
|
|
readonly path?: string;
|
|
|
|
readonly url: string;
|
2021-03-24 13:59:56 +00:00
|
|
|
};
|
|
|
|
|
2021-04-26 16:38:50 +00:00
|
|
|
export class ConversationModel extends window.Backbone
|
|
|
|
.Model<ConversationAttributesType> {
|
2020-09-24 20:57:54 +00:00
|
|
|
static COLORS: string;
|
|
|
|
|
|
|
|
cachedProps?: ConversationType | null;
|
|
|
|
|
2020-10-28 21:54:33 +00:00
|
|
|
oldCachedProps?: ConversationType | null;
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
contactTypingTimers?: Record<
|
|
|
|
string,
|
2023-09-27 21:23:52 +00:00
|
|
|
{
|
|
|
|
senderId: string;
|
2024-07-18 17:17:56 +00:00
|
|
|
timer: NodeJS.Timeout;
|
2023-09-27 21:23:52 +00:00
|
|
|
timestamp: number;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
>;
|
|
|
|
|
2021-03-26 16:36:57 +00:00
|
|
|
contactCollection?: Backbone.Collection<ConversationModel>;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-08-22 21:01:36 +00:00
|
|
|
debouncedUpdateLastMessage: (() => void) & { flush(): void };
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
initialPromise?: Promise<unknown>;
|
|
|
|
|
|
|
|
inProgressFetch?: Promise<unknown>;
|
|
|
|
|
2022-08-25 05:04:42 +00:00
|
|
|
newMessageQueue?: PQueue;
|
2020-09-28 16:43:21 +00:00
|
|
|
|
2022-08-25 05:04:42 +00:00
|
|
|
jobQueue?: PQueue;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
storeName?: string | null;
|
|
|
|
|
2021-08-30 21:32:56 +00:00
|
|
|
throttledBumpTyping?: () => void;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-01-02 21:34:41 +00:00
|
|
|
throttledFetchSMSOnlyUUID?: () => Promise<void> | undefined;
|
2021-05-13 20:57:27 +00:00
|
|
|
|
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>;
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
throttledUpdateVerified?: () => void;
|
|
|
|
|
2024-07-18 17:17:56 +00:00
|
|
|
typingRefreshTimer?: NodeJS.Timeout | null;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2024-07-18 17:17:56 +00:00
|
|
|
typingPauseTimer?: NodeJS.Timeout | null;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
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;
|
|
|
|
|
2021-05-21 17:27:28 +00:00
|
|
|
throttledUpdateSharedGroups?: () => Promise<void>;
|
|
|
|
|
2021-03-24 13:59:56 +00:00
|
|
|
private cachedIdenticon?: CachedIdenticon;
|
|
|
|
|
2023-06-02 17:54:36 +00:00
|
|
|
public isFetchingUUID?: boolean;
|
2021-05-13 20:57:27 +00:00
|
|
|
|
2021-09-29 22:46:35 +00:00
|
|
|
private lastIsTyping?: boolean;
|
|
|
|
|
2024-07-18 17:17:56 +00:00
|
|
|
private muteTimer?: NodeJS.Timeout;
|
2021-10-06 21:59:34 +00:00
|
|
|
|
2021-11-01 23:38:08 +00:00
|
|
|
private isInReduxBatch = false;
|
|
|
|
|
2022-11-07 23:21:12 +00:00
|
|
|
private privVerifiedEnum?: typeof window.textsecure.storage.protocol.VerifiedStatus;
|
|
|
|
|
2023-02-24 19:03:17 +00:00
|
|
|
private isShuttingDown = false;
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
override defaults(): Partial<ConversationAttributesType> {
|
2020-09-24 20:57:54 +00:00
|
|
|
return {
|
|
|
|
unreadCount: 0,
|
|
|
|
verified: window.textsecure.storage.protocol.VerifiedStatus.DEFAULT,
|
|
|
|
messageCount: 0,
|
|
|
|
sentMessageCount: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
idForLogging(): string {
|
2022-04-22 18:35:14 +00:00
|
|
|
return getConversationIdForLogging(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
getSendTarget(): ServiceIdString | undefined {
|
2022-07-01 00:52:03 +00:00
|
|
|
return getSendTarget(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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' });
|
2020-09-24 20:57:54 +00:00
|
|
|
collection.comparator = (
|
|
|
|
left: ConversationModel,
|
|
|
|
right: ConversationModel
|
|
|
|
) => {
|
2021-03-02 16:27:11 +00:00
|
|
|
return collator.compare(left.getTitle(), right.getTitle());
|
2020-09-24 20:57:54 +00:00
|
|
|
};
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
2023-01-12 20:58:53 +00:00
|
|
|
constructor(attributes: ConversationAttributesType) {
|
|
|
|
super(attributes);
|
|
|
|
|
|
|
|
// Note that we intentionally don't use `initialize()` method because it
|
|
|
|
// isn't compatible with esnext output of esbuild.
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = this.getServiceId();
|
2023-08-10 16:43:33 +00:00
|
|
|
const normalizedServiceId =
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId &&
|
|
|
|
normalizeServiceId(serviceId, 'ConversationModel.initialize');
|
|
|
|
if (serviceId && normalizedServiceId !== serviceId) {
|
2022-02-02 21:41:29 +00:00
|
|
|
log.warn(
|
2023-08-10 16:43:33 +00:00
|
|
|
'ConversationModel.initialize: normalizing serviceId from ' +
|
2023-08-16 20:54:39 +00:00
|
|
|
`${serviceId} to ${normalizedServiceId}`
|
2022-02-02 21:41:29 +00:00
|
|
|
);
|
2023-08-16 20:54:39 +00:00
|
|
|
this.set('serviceId', normalizedServiceId);
|
2022-02-02 21:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 13:27:38 +00:00
|
|
|
if (isValidE164(attributes.id, false)) {
|
2023-08-10 16:43:33 +00:00
|
|
|
this.set({ id: generateGuid(), e164: attributes.id });
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.storeName = 'conversations';
|
|
|
|
|
2022-11-07 23:21:12 +00:00
|
|
|
this.privVerifiedEnum = window.textsecure.storage.protocol.VerifiedStatus;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2022-04-12 18:16:58 +00:00
|
|
|
this.debouncedUpdateLastMessage = debounce(
|
2021-03-26 18:03:15 +00:00
|
|
|
this.updateLastMessage.bind(this),
|
|
|
|
200
|
|
|
|
);
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
this.contactCollection = this.getContactCollection();
|
2021-03-26 18:03:15 +00:00
|
|
|
this.contactCollection.on(
|
|
|
|
'change:name change:profileName change:profileFamilyName change:e164',
|
|
|
|
this.debouncedUpdateLastMessage,
|
|
|
|
this
|
|
|
|
);
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isDirectConversation(this.attributes)) {
|
2021-03-29 20:43:31 +00:00
|
|
|
this.contactCollection.on(
|
|
|
|
'change:verified',
|
|
|
|
this.onMemberVerifiedChange.bind(this)
|
|
|
|
);
|
|
|
|
}
|
2021-03-26 18:03:15 +00:00
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
this.on('newmessage', this.onNewMessage);
|
|
|
|
this.on('change:profileKey', this.onChangeProfileKey);
|
2023-02-02 18:03:51 +00:00
|
|
|
this.on(
|
2023-02-13 22:40:11 +00:00
|
|
|
'change:name change:profileName change:profileFamilyName change:e164 ' +
|
|
|
|
'change:systemGivenName change:systemFamilyName change:systemNickname',
|
2023-02-02 18:03:51 +00:00
|
|
|
() => this.maybeClearUsername()
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
const sealedSender = this.get('sealedSender');
|
|
|
|
if (sealedSender === undefined) {
|
|
|
|
this.set({ sealedSender: SEALED_SENDER.UNKNOWN });
|
|
|
|
}
|
2023-10-11 19:06:43 +00:00
|
|
|
// @ts-expect-error -- Removing legacy prop
|
2020-09-24 20:57:54 +00:00
|
|
|
this.unset('unidentifiedDelivery');
|
2023-10-11 19:06:43 +00:00
|
|
|
// @ts-expect-error -- Removing legacy prop
|
2020-09-24 20:57:54 +00:00
|
|
|
this.unset('unidentifiedDeliveryUnrestricted');
|
2023-10-11 19:06:43 +00:00
|
|
|
// @ts-expect-error -- Removing legacy prop
|
2020-09-24 20:57:54 +00:00
|
|
|
this.unset('hasFetchedProfile');
|
2023-10-11 19:06:43 +00:00
|
|
|
// @ts-expect-error -- Removing legacy prop
|
2020-09-24 20:57:54 +00:00
|
|
|
this.unset('tokens');
|
|
|
|
|
2021-03-26 18:03:15 +00:00
|
|
|
this.on('change:members change:membersV2', this.fetchContacts);
|
2020-09-24 20:57:54 +00:00
|
|
|
this.typingRefreshTimer = null;
|
|
|
|
this.typingPauseTimer = null;
|
|
|
|
|
2020-10-26 14:39:45 +00:00
|
|
|
// 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.
|
2021-09-20 18:51:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-05-21 19:53:05 +00:00
|
|
|
|
2021-09-20 18:51:30 +00:00
|
|
|
if (this.cachedProps) {
|
|
|
|
this.oldCachedProps = this.cachedProps;
|
|
|
|
}
|
|
|
|
this.cachedProps = null;
|
2021-11-01 23:38:08 +00:00
|
|
|
this.trigger('props-change', this, this.isInReduxBatch);
|
2020-10-28 21:54:33 +00:00
|
|
|
}
|
2021-09-20 18:51:30 +00:00
|
|
|
);
|
2021-05-13 20:57:27 +00:00
|
|
|
|
|
|
|
// Set `isFetchingUUID` eagerly to avoid UI flicker when opening the
|
|
|
|
// conversation for the first time.
|
|
|
|
this.isFetchingUUID = this.isSMSOnly();
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
this.throttledBumpTyping = throttle(this.bumpTyping, 300);
|
|
|
|
this.throttledUpdateSharedGroups = throttle(
|
|
|
|
this.updateSharedGroups.bind(this),
|
|
|
|
FIVE_MINUTES
|
|
|
|
);
|
2022-04-12 18:16:58 +00:00
|
|
|
this.throttledFetchSMSOnlyUUID = throttle(
|
2021-05-28 19:11:19 +00:00
|
|
|
this.fetchSMSOnlyUUID.bind(this),
|
|
|
|
FIVE_MINUTES
|
|
|
|
);
|
2022-04-12 18:16:58 +00:00
|
|
|
this.throttledMaybeMigrateV1Group = throttle(
|
2021-05-28 19:11:19 +00:00
|
|
|
this.maybeMigrateV1Group.bind(this),
|
2021-05-13 20:57:27 +00:00
|
|
|
FIVE_MINUTES
|
|
|
|
);
|
2022-12-05 22:46:54 +00:00
|
|
|
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
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 02:15:59 +00:00
|
|
|
toSenderKeyTarget(): SenderKeyTargetType {
|
|
|
|
return {
|
|
|
|
getGroupId: () => this.get('groupId'),
|
|
|
|
getMembers: () => this.getMembers(),
|
2023-08-10 16:43:33 +00:00
|
|
|
hasMember: (serviceId: ServiceIdString) => this.hasMember(serviceId),
|
2021-12-10 02:15:59 +00:00
|
|
|
idForLogging: () => this.idForLogging(),
|
|
|
|
isGroupV2: () => isGroupV2(this.attributes),
|
|
|
|
isValid: () => isGroupV2(this.attributes),
|
|
|
|
|
|
|
|
getSenderKeyInfo: () => this.get('senderKeyInfo'),
|
|
|
|
saveSenderKeyInfo: async (senderKeyInfo: SenderKeyInfoType) => {
|
|
|
|
this.set({ senderKeyInfo });
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2021-12-10 02:15:59 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-07 23:21:12 +00:00
|
|
|
private get verifiedEnum(): typeof window.textsecure.storage.protocol.VerifiedStatus {
|
|
|
|
strictAssert(this.privVerifiedEnum, 'ConversationModel not initialize');
|
|
|
|
return this.privVerifiedEnum;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
private isMemberRequestingToJoin(serviceId: ServiceIdString): boolean {
|
|
|
|
return isMemberRequestingToJoin(this.attributes, serviceId);
|
2021-01-29 21:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
isMemberPending(serviceId: ServiceIdString): boolean {
|
|
|
|
return isMemberPending(this.attributes, serviceId);
|
2022-03-16 00:11:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
isMemberAwaitingApproval(serviceId: ServiceIdString): boolean {
|
|
|
|
return isMemberAwaitingApproval(this.attributes, serviceId);
|
2021-01-29 22:16:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
isMember(serviceId: ServiceIdString): boolean {
|
|
|
|
return isMember(this.attributes, serviceId);
|
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) {
|
2021-09-17 18:27:53 +00:00
|
|
|
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(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceIdKind: ServiceIdKind
|
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();
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = window.storage.user.getCheckedServiceId(serviceIdKind);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
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.
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!this.isMemberPending(serviceId)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
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
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (serviceIdKind === ServiceIdKind.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(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
strictAssert(
|
|
|
|
serviceIdKind === ServiceIdKind.PNI,
|
|
|
|
'Must be a PNI promotion'
|
|
|
|
);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
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(
|
2023-08-10 16:43:33 +00:00
|
|
|
aci: AciString
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.GroupChange.Actions | undefined> {
|
2021-01-29 21:19:24 +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.
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!this.isMemberRequestingToJoin(aci)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2023-08-10 16:43:33 +00:00
|
|
|
`denyPendingApprovalRequest/${idLog}: ${aci} is not requesting ` +
|
2022-07-08 20:46:25 +00:00
|
|
|
'to join the group. Returning early.'
|
2021-01-29 21:19:24 +00:00
|
|
|
);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
2022-03-15 01:32:07 +00:00
|
|
|
|
2021-01-29 21:19:24 +00:00
|
|
|
return window.Signal.Groups.buildDeletePendingAdminApprovalMemberChange({
|
|
|
|
group: this.attributes,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
|
|
|
aci,
|
2021-01-29 21:19:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
async addPendingApprovalRequest(): Promise<
|
2021-06-22 14:46:42 +00:00
|
|
|
Proto.GroupChange.Actions | undefined
|
2021-01-29 22:16:48 +00:00
|
|
|
> {
|
|
|
|
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();
|
2021-01-29 22:16:48 +00:00
|
|
|
|
|
|
|
const toRequest = window.ConversationController.get(conversationId);
|
|
|
|
if (!toRequest) {
|
|
|
|
throw new Error(
|
|
|
|
`addPendingApprovalRequest/${idLog}: No conversation found for conversation ${conversationId}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = toRequest.getCheckedServiceId(
|
|
|
|
`addPendingApprovalRequest/${idLog}`
|
|
|
|
);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
// 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.
|
2023-08-10 16:43:33 +00:00
|
|
|
if (this.isMemberAwaitingApproval(serviceId)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2022-07-08 20:46:25 +00:00
|
|
|
`addPendingApprovalRequest/${idLog}: ` +
|
|
|
|
`${toRequest.idForLogging()} already in pending approval.`
|
2021-01-29 22:16:48 +00:00
|
|
|
);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.Signal.Groups.buildAddPendingAdminApprovalMemberChange({
|
|
|
|
group: this.attributes,
|
|
|
|
profileKeyCredentialBase64,
|
|
|
|
serverPublicParamsBase64: window.getServerPublicParams(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async addMember(
|
|
|
|
serviceId: ServiceIdString
|
|
|
|
): Promise<Proto.GroupChange.Actions | undefined> {
|
2021-01-29 22:16:48 +00:00
|
|
|
const idLog = this.idForLogging();
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const toRequest = window.ConversationController.get(serviceId);
|
2021-01-29 22:16:48 +00:00
|
|
|
if (!toRequest) {
|
2023-08-10 16:43:33 +00:00
|
|
|
throw new Error(
|
|
|
|
`addMember/${idLog}: No conversation found for ${serviceId}`
|
|
|
|
);
|
2022-03-15 01:32:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
// 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.
|
2023-08-10 16:43:33 +00:00
|
|
|
if (this.isMember(serviceId)) {
|
2022-07-08 20:46:25 +00:00
|
|
|
log.warn(
|
|
|
|
`addMember/${idLog}: ${toRequest.idForLogging()} ` +
|
|
|
|
'is already a member.'
|
|
|
|
);
|
2021-01-29 22:16:48 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.Signal.Groups.buildAddMember({
|
|
|
|
group: this.attributes,
|
|
|
|
profileKeyCredentialBase64,
|
|
|
|
serverPublicParamsBase64: window.getServerPublicParams(),
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2021-01-29 22:16:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
private async removePendingMember(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceIds: ReadonlyArray<ServiceIdString>
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.GroupChange.Actions | undefined> {
|
2023-08-10 16:43:33 +00:00
|
|
|
return removePendingMember(this.attributes, serviceIds);
|
2021-01-29 21:19:24 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
private async removeMember(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString
|
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.
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!this.isMember(serviceId)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2023-08-10 16:43:33 +00:00
|
|
|
`removeMember/${idLog}: ${serviceId} is not a pending member of group. Returning early.`
|
2020-10-06 17:06:34 +00:00
|
|
|
);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
2022-03-15 01:32:07 +00:00
|
|
|
|
2020-10-06 17:06:34 +00:00
|
|
|
return window.Signal.Groups.buildDeleteMemberChange({
|
|
|
|
group: this.attributes,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
|
|
|
serviceId,
|
2020-10-06 17:06:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
private async toggleAdminChange(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.GroupChange.Actions | undefined> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const idLog = this.idForLogging();
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!this.isMember(serviceId)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2023-08-10 16:43:33 +00:00
|
|
|
`toggleAdminChange/${idLog}: ${serviceId} is not a pending member of group. Returning early.`
|
2021-01-29 21:19:24 +00:00
|
|
|
);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-07-02 19:21:24 +00:00
|
|
|
const MEMBER_ROLES = Proto.Member.Role;
|
2021-01-29 21:19:24 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const role = this.isAdmin(serviceId)
|
2021-01-29 21:19:24 +00:00
|
|
|
? MEMBER_ROLES.DEFAULT
|
|
|
|
: MEMBER_ROLES.ADMINISTRATOR;
|
|
|
|
|
|
|
|
return window.Signal.Groups.buildModifyMemberRoleChange({
|
|
|
|
group: this.attributes,
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2021-01-29 21:19:24 +00:00
|
|
|
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,
|
2021-03-24 21:28:55 +00:00
|
|
|
extraConversationsForSend,
|
|
|
|
inviteLinkPassword,
|
|
|
|
name,
|
2023-05-23 23:38:58 +00:00
|
|
|
syncMessageOnly,
|
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>;
|
2022-12-22 00:07:02 +00:00
|
|
|
extraConversationsForSend?: ReadonlyArray<string>;
|
2021-03-24 21:28:55 +00:00
|
|
|
inviteLinkPassword?: string;
|
|
|
|
name: string;
|
2023-05-23 23:38:58 +00:00
|
|
|
syncMessageOnly?: boolean;
|
2020-10-06 17:06:34 +00:00
|
|
|
}): Promise<void> {
|
2021-01-29 22:16:48 +00:00
|
|
|
await window.Signal.Groups.modifyGroupV2({
|
|
|
|
conversation: this,
|
2022-07-08 20:46:25 +00:00
|
|
|
usingCredentialsFrom,
|
2021-03-24 21:28:55 +00:00
|
|
|
createGroupChange,
|
|
|
|
extraConversationsForSend,
|
2021-01-29 22:16:48 +00:00
|
|
|
inviteLinkPassword,
|
|
|
|
name,
|
2023-05-23 23:38:58 +00:00
|
|
|
syncMessageOnly,
|
2021-01-29 22:16:48 +00:00
|
|
|
});
|
2020-10-02 22:19:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
isEverUnregistered(): boolean {
|
2022-11-17 02:15:26 +00:00
|
|
|
return isConversationEverUnregistered(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isUnregistered(): boolean {
|
2021-02-23 20:34:28 +00:00
|
|
|
return isConversationUnregistered(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 18:47:49 +00:00
|
|
|
isUnregisteredAndStale(): boolean {
|
|
|
|
return isConversationUnregisteredAndStale(this.attributes);
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:57:27 +00:00
|
|
|
isSMSOnly(): boolean {
|
|
|
|
return isConversationSMSOnly({
|
|
|
|
...this.attributes,
|
2021-06-07 16:39:13 +00:00
|
|
|
type: isDirectConversation(this.attributes) ? 'direct' : 'unknown',
|
2021-05-13 20:57:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-19 18:47:49 +00:00
|
|
|
setUnregistered({
|
|
|
|
timestamp = Date.now(),
|
|
|
|
fromStorageService = false,
|
|
|
|
shouldSave = true,
|
|
|
|
}: {
|
|
|
|
timestamp?: number;
|
|
|
|
fromStorageService?: boolean;
|
|
|
|
shouldSave?: boolean;
|
|
|
|
} = {}): void {
|
|
|
|
log.info(
|
2023-02-01 21:32:46 +00:00
|
|
|
`setUnregistered(${this.idForLogging()}): conversation is now ` +
|
|
|
|
`unregistered, timestamp=${timestamp}`
|
2022-09-19 18:47:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const oldFirstUnregisteredAt = this.get('firstUnregisteredAt');
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({
|
2022-09-19 18:47:49 +00:00
|
|
|
// 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),
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2022-09-19 18:47:49 +00:00
|
|
|
|
|
|
|
if (shouldSave) {
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2022-09-19 18:47:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 21:32:46 +00:00
|
|
|
const e164 = this.get('e164');
|
2023-08-16 20:54:39 +00:00
|
|
|
const pni = this.getPni();
|
|
|
|
const aci = this.getServiceId();
|
2023-02-01 21:32:46 +00:00
|
|
|
if (e164 && pni && aci && pni !== aci) {
|
|
|
|
this.updateE164(undefined);
|
2024-01-29 22:37:26 +00:00
|
|
|
this.updatePni(undefined, false);
|
2023-02-01 21:32:46 +00:00
|
|
|
|
|
|
|
const { conversation: split } =
|
|
|
|
window.ConversationController.maybeMergeContacts({
|
|
|
|
pni,
|
|
|
|
e164,
|
|
|
|
reason: `ConversationModel.setUnregistered(${aci})`,
|
|
|
|
});
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`setUnregistered(${this.idForLogging()}): splitting pni ${pni} and ` +
|
|
|
|
`e164 ${e164} into a separate conversation ${split.idForLogging()}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-19 18:47:49 +00:00
|
|
|
if (
|
|
|
|
!fromStorageService &&
|
|
|
|
oldFirstUnregisteredAt !== this.get('firstUnregisteredAt')
|
|
|
|
) {
|
|
|
|
this.captureChange('setUnregistered');
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 18:47:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-19 18:47:49 +00:00
|
|
|
const oldFirstUnregisteredAt = this.get('firstUnregisteredAt');
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Conversation ${this.idForLogging()} is registered once again`);
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({
|
|
|
|
discoveredUnregisteredAt: undefined,
|
2022-09-19 18:47:49 +00:00
|
|
|
firstUnregisteredAt: undefined,
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2022-09-19 18:47:49 +00:00
|
|
|
|
|
|
|
if (shouldSave) {
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2022-09-19 18:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!fromStorageService &&
|
|
|
|
oldFirstUnregisteredAt !== this.get('firstUnregisteredAt')
|
|
|
|
) {
|
|
|
|
this.captureChange('setRegistered');
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:42:35 +00:00
|
|
|
isGroupV1AndDisabled(): boolean {
|
2022-02-16 18:36:21 +00:00
|
|
|
return isGroupV1(this.attributes);
|
2020-12-01 16:42:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
isBlocked(): boolean {
|
2023-06-02 17:54:36 +00:00
|
|
|
return isBlocked(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block({ viaStorageServiceSync = false } = {}): void {
|
|
|
|
let blocked = false;
|
2022-01-20 00:39:27 +00:00
|
|
|
const wasBlocked = this.isBlocked();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
if (serviceId) {
|
|
|
|
drop(window.storage.blocked.addBlockedServiceId(serviceId));
|
2020-09-24 20:57:54 +00:00
|
|
|
blocked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const e164 = this.get('e164');
|
|
|
|
if (e164) {
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(window.storage.blocked.addBlockedNumber(e164));
|
2020-09-24 20:57:54 +00:00
|
|
|
blocked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupId = this.get('groupId');
|
|
|
|
if (groupId) {
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(window.storage.blocked.addBlockedGroup(groupId));
|
2020-09-24 20:57:54 +00:00
|
|
|
blocked = true;
|
|
|
|
}
|
|
|
|
|
2022-01-20 00:39:27 +00:00
|
|
|
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');
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unblock({ viaStorageServiceSync = false } = {}): boolean {
|
|
|
|
let unblocked = false;
|
2022-01-20 00:39:27 +00:00
|
|
|
const wasBlocked = this.isBlocked();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
if (serviceId) {
|
|
|
|
drop(window.storage.blocked.removeBlockedServiceId(serviceId));
|
2020-09-24 20:57:54 +00:00
|
|
|
unblocked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const e164 = this.get('e164');
|
|
|
|
if (e164) {
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(window.storage.blocked.removeBlockedNumber(e164));
|
2020-09-24 20:57:54 +00:00
|
|
|
unblocked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupId = this.get('groupId');
|
|
|
|
if (groupId) {
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(window.storage.blocked.removeBlockedGroup(groupId));
|
2020-09-24 20:57:54 +00:00
|
|
|
unblocked = true;
|
|
|
|
}
|
|
|
|
|
2022-01-20 00:39:27 +00:00
|
|
|
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');
|
|
|
|
}
|
2022-06-20 18:55:34 +00:00
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.fetchLatestGroupV2Data({ force: true });
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return unblocked;
|
|
|
|
}
|
|
|
|
|
2023-04-05 20:48:00 +00:00
|
|
|
async removeContact({
|
|
|
|
viaStorageServiceSync = false,
|
|
|
|
shouldSave = true,
|
|
|
|
} = {}): Promise<void> {
|
|
|
|
const logId = `removeContact(${this.idForLogging()}) storage? ${viaStorageServiceSync}`;
|
|
|
|
|
|
|
|
if (!isDirectConversation(this.attributes)) {
|
|
|
|
log.warn(`${logId}: not direct conversation`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('removalStage')) {
|
|
|
|
log.warn(`${logId}: already removed`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't show message request state until first incoming message.
|
|
|
|
log.info(`${logId}: updating`);
|
|
|
|
this.set({ removalStage: 'justNotification' });
|
|
|
|
|
|
|
|
if (!viaStorageServiceSync) {
|
|
|
|
this.captureChange('removeContact');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.disableProfileSharing({ viaStorageServiceSync });
|
|
|
|
|
|
|
|
// Drop existing message request state to avoid sending receipts and
|
|
|
|
// display MR actions.
|
|
|
|
const messageRequestEnum = Proto.SyncMessage.MessageRequestResponse.Type;
|
|
|
|
await this.applyMessageRequestResponse(messageRequestEnum.UNKNOWN, {
|
|
|
|
viaStorageServiceSync,
|
|
|
|
shouldSave: false,
|
|
|
|
});
|
|
|
|
|
2023-07-21 20:01:12 +00:00
|
|
|
window.reduxActions?.stories.removeAllContactStories(this.id);
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
if (serviceId) {
|
2023-07-21 20:01:12 +00:00
|
|
|
window.reduxActions?.storyDistributionLists.removeMemberFromAllDistributionLists(
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId
|
2023-07-21 20:01:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-05 20:48:00 +00:00
|
|
|
// Add notification
|
|
|
|
drop(this.queueJob('removeContact', () => this.maybeSetContactRemoved()));
|
|
|
|
|
|
|
|
if (shouldSave) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2023-04-05 20:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async restoreContact({
|
|
|
|
viaStorageServiceSync = false,
|
|
|
|
shouldSave = true,
|
|
|
|
} = {}): Promise<void> {
|
|
|
|
const logId = `restoreContact(${this.idForLogging()}) storage? ${viaStorageServiceSync}`;
|
|
|
|
|
|
|
|
if (!isDirectConversation(this.attributes)) {
|
|
|
|
log.warn(`${logId}: not direct conversation`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('removalStage') === undefined) {
|
2023-04-19 16:13:48 +00:00
|
|
|
if (!viaStorageServiceSync) {
|
|
|
|
log.warn(`${logId}: not removed`);
|
|
|
|
}
|
2023-04-05 20:48:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`${logId}: updating`);
|
|
|
|
this.set({ removalStage: undefined });
|
|
|
|
|
|
|
|
if (!viaStorageServiceSync) {
|
|
|
|
this.captureChange('restoreContact');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove notification since the conversation isn't hidden anymore
|
|
|
|
await this.maybeClearContactRemoved();
|
|
|
|
|
|
|
|
if (shouldSave) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2023-04-05 20:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
enableProfileSharing({ viaStorageServiceSync = false } = {}): void {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-24 23:37:17 +00:00
|
|
|
`enableProfileSharing: ${this.idForLogging()} storage? ${viaStorageServiceSync}`
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
const before = this.get('profileSharing');
|
|
|
|
|
|
|
|
this.set({ profileSharing: true });
|
|
|
|
|
|
|
|
const after = this.get('profileSharing');
|
|
|
|
|
|
|
|
if (!viaStorageServiceSync && Boolean(before) !== Boolean(after)) {
|
2021-03-04 23:08:44 +00:00
|
|
|
this.captureChange('enableProfileSharing');
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disableProfileSharing({ viaStorageServiceSync = false } = {}): void {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-24 23:37:17 +00:00
|
|
|
`disableProfileSharing: ${this.idForLogging()} storage? ${viaStorageServiceSync}`
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
const before = this.get('profileSharing');
|
|
|
|
|
|
|
|
this.set({ profileSharing: false });
|
|
|
|
|
|
|
|
const after = this.get('profileSharing');
|
|
|
|
|
|
|
|
if (!viaStorageServiceSync && Boolean(before) !== Boolean(after)) {
|
2021-03-04 23:08:44 +00:00
|
|
|
this.captureChange('disableProfileSharing');
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasDraft(): boolean {
|
2023-06-02 17:54:36 +00:00
|
|
|
return hasDraft(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 16:31:45 +00:00
|
|
|
getDraftPreview(): DraftPreviewType {
|
2023-06-02 17:54:36 +00:00
|
|
|
return getDraftPreview(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.typingRefreshTimer) {
|
|
|
|
const isTyping = true;
|
|
|
|
this.setTypingRefreshTimer();
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.sendTypingMessage(isTyping);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setTypingPauseTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTypingRefreshTimer(): void {
|
2022-02-25 18:37:15 +00:00
|
|
|
clearTimeoutIfNecessary(this.typingRefreshTimer);
|
2020-09-24 20:57:54 +00:00
|
|
|
this.typingRefreshTimer = setTimeout(
|
|
|
|
this.onTypingRefreshTimeout.bind(this),
|
|
|
|
10 * 1000
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onTypingRefreshTimeout(): void {
|
|
|
|
const isTyping = true;
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.sendTypingMessage(isTyping);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// This timer will continue to reset itself until the pause timer stops it
|
|
|
|
this.setTypingRefreshTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTypingPauseTimer(): void {
|
2022-02-25 18:37:15 +00:00
|
|
|
clearTimeoutIfNecessary(this.typingPauseTimer);
|
2020-09-24 20:57:54 +00:00
|
|
|
this.typingPauseTimer = setTimeout(
|
|
|
|
this.onTypingPauseTimeout.bind(this),
|
|
|
|
3 * 1000
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onTypingPauseTimeout(): void {
|
|
|
|
const isTyping = false;
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.sendTypingMessage(isTyping);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
this.clearTypingTimers();
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTypingTimers(): void {
|
2022-02-25 18:37:15 +00:00
|
|
|
clearTimeoutIfNecessary(this.typingPauseTimer);
|
|
|
|
this.typingPauseTimer = null;
|
|
|
|
clearTimeoutIfNecessary(this.typingRefreshTimer);
|
|
|
|
this.typingRefreshTimer = null;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:25:15 +00:00
|
|
|
async fetchLatestGroupV2Data(
|
|
|
|
options: { force?: boolean } = {}
|
|
|
|
): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await window.Signal.Groups.waitThenMaybeUpdateGroup({
|
2021-08-06 21:25:15 +00:00
|
|
|
force: options.force,
|
2020-09-24 20:57:54 +00:00
|
|
|
conversation: this,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:57:27 +00:00
|
|
|
async fetchSMSOnlyUUID(): Promise<void> {
|
2022-08-18 20:44:53 +00:00
|
|
|
const { server } = window.textsecure;
|
|
|
|
if (!server) {
|
2021-05-13 20:57:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.isSMSOnly()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-05-13 20:57:27 +00:00
|
|
|
`Fetching uuid for a sms-only conversation ${this.idForLogging()}`
|
|
|
|
);
|
|
|
|
|
|
|
|
this.isFetchingUUID = true;
|
2021-09-20 18:51:30 +00:00
|
|
|
this.trigger('change', this, { force: true });
|
2021-05-13 20:57:27 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Attempt to fetch UUID
|
|
|
|
await updateConversationsWithUuidLookup({
|
|
|
|
conversationController: window.ConversationController,
|
|
|
|
conversations: [this],
|
2022-08-18 20:44:53 +00:00
|
|
|
server,
|
2021-05-13 20:57:27 +00:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
// No redux update here
|
|
|
|
this.isFetchingUUID = false;
|
2021-09-20 18:51:30 +00:00
|
|
|
this.trigger('change', this, { force: true });
|
2021-05-13 20:57:27 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-05-13 20:57:27 +00:00
|
|
|
`Done fetching uuid for a sms-only conversation ${this.idForLogging()}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
if (!this.getServiceId()) {
|
2021-06-01 20:45:43 +00:00
|
|
|
return;
|
2021-05-13 20:57:27 +00:00
|
|
|
}
|
2021-06-01 20:45:43 +00:00
|
|
|
|
|
|
|
// On successful fetch - mark contact as registered.
|
|
|
|
this.setRegistered();
|
2021-05-13 20:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
override isValid(): boolean {
|
2021-06-07 16:39:13 +00:00
|
|
|
return (
|
|
|
|
isDirectConversation(this.attributes) ||
|
|
|
|
isGroupV1(this.attributes) ||
|
|
|
|
isGroupV2(this.attributes)
|
|
|
|
);
|
2020-11-16 21:34:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
async maybeMigrateV1Group(): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
maybeRepairGroupV2(data: {
|
|
|
|
masterKey: string;
|
|
|
|
secretParams: string;
|
|
|
|
publicParams: string;
|
|
|
|
}): void {
|
|
|
|
if (
|
|
|
|
this.get('groupVersion') &&
|
|
|
|
this.get('masterKey') &&
|
|
|
|
this.get('secretParams') &&
|
|
|
|
this.get('publicParams')
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Repairing GroupV2 conversation ${this.idForLogging()}`);
|
2020-09-24 20:57:54 +00:00
|
|
|
const { masterKey, secretParams, publicParams } = data;
|
|
|
|
|
|
|
|
this.set({ masterKey, secretParams, publicParams, groupVersion: 2 });
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
getGroupV2Info(
|
|
|
|
options: Readonly<
|
|
|
|
{ groupChange?: Uint8Array } & (
|
|
|
|
| {
|
|
|
|
includePendingMembers?: boolean;
|
2022-12-22 00:07:02 +00:00
|
|
|
extraConversationsForSend?: ReadonlyArray<string>;
|
2021-10-29 23:19:44 +00:00
|
|
|
}
|
2023-08-10 16:43:33 +00:00
|
|
|
| { members: ReadonlyArray<ServiceIdString> }
|
2021-10-29 23:19:44 +00:00
|
|
|
)
|
|
|
|
> = {}
|
|
|
|
): GroupV2InfoType | undefined {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes) || !isGroupV2(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return {
|
2021-06-22 14:46:42 +00:00
|
|
|
masterKey: Bytes.fromBase64(
|
2020-09-24 20:57:54 +00:00
|
|
|
// 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')!,
|
2021-10-29 23:19:44 +00:00
|
|
|
members:
|
|
|
|
'members' in options ? options.members : this.getRecipients(options),
|
|
|
|
groupChange: options.groupChange,
|
2020-09-24 20:57:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
getGroupIdBuffer(): Uint8Array | undefined {
|
2020-11-04 00:16:20 +00:00
|
|
|
const groupIdString = this.get('groupId');
|
|
|
|
|
|
|
|
if (!groupIdString) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isGroupV1(this.attributes)) {
|
2021-09-24 00:49:05 +00:00
|
|
|
return Bytes.fromBinary(groupIdString);
|
2020-11-04 00:16:20 +00:00
|
|
|
}
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isGroupV2(this.attributes)) {
|
2021-09-24 00:49:05 +00:00
|
|
|
return Bytes.fromBase64(groupIdString);
|
2020-11-04 00:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:24:21 +00:00
|
|
|
async sendTypingMessage(isTyping: boolean): Promise<void> {
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
|
|
|
|
if (!messaging) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't send typing messages to our other devices
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isMe(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-19 22:06:44 +00:00
|
|
|
if (isGroupV1(this.attributes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-29 22:46:35 +00:00
|
|
|
// 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;
|
|
|
|
|
2024-07-19 22:06:44 +00:00
|
|
|
// If captchas are active, then we should drop typing messages because
|
|
|
|
// they're less important and could overwhelm the queue.
|
|
|
|
if (
|
|
|
|
window.Signal.challengeHandler?.areAnyRegistered() &&
|
|
|
|
this.isSealedSenderDisabled()
|
|
|
|
) {
|
|
|
|
log.info(
|
|
|
|
`sendTypingMessage(${this.idForLogging()}): Challenge is registered and can't send sealed, ignoring`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:55:14 +00:00
|
|
|
await this.queueJob('sendTypingMessage', async () => {
|
2021-05-25 22:40:04 +00:00
|
|
|
const groupMembers = this.getRecipients();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
// We don't send typing messages if our recipients list is empty
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isDirectConversation(this.attributes) && !groupMembers.length) {
|
2021-05-25 22:40:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-09-29 22:46:35 +00:00
|
|
|
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();
|
2021-09-29 22:46:35 +00:00
|
|
|
|
|
|
|
const content = {
|
|
|
|
recipientId,
|
|
|
|
groupId,
|
|
|
|
groupMembers,
|
|
|
|
isTyping: this.lastIsTyping,
|
|
|
|
timestamp,
|
|
|
|
};
|
|
|
|
this.lastIsTyping = undefined;
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`sendTypingMessage(${this.idForLogging()}): sending ${content.isTyping}`
|
|
|
|
);
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
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;
|
2021-07-02 18:34:17 +00:00
|
|
|
|
2021-08-06 00:17:23 +00:00
|
|
|
const sendOptions = {
|
|
|
|
...(await getSendOptions(this.attributes)),
|
|
|
|
online: true,
|
|
|
|
};
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2021-09-27 18:29:06 +00:00
|
|
|
await handleMessageSend(
|
2022-06-13 21:39:35 +00:00
|
|
|
messaging.sendMessageProtoAndWait({
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
groupId: undefined,
|
2021-08-06 00:17:23 +00:00
|
|
|
options: sendOptions,
|
2022-07-01 16:55:13 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
recipients: groupMembers,
|
|
|
|
timestamp,
|
|
|
|
urgent: false,
|
2021-07-15 23:48:09 +00:00
|
|
|
}),
|
|
|
|
{ messageIds: [], sendType: 'typing' }
|
2021-05-25 22:40:04 +00:00
|
|
|
);
|
|
|
|
} else {
|
2021-09-27 18:29:06 +00:00
|
|
|
await handleMessageSend(
|
2023-04-11 03:54:43 +00:00
|
|
|
sendContentMessageToGroup({
|
2021-06-08 21:51:58 +00:00
|
|
|
contentHint: ContentHint.IMPLICIT,
|
2021-05-25 22:40:04 +00:00
|
|
|
contentMessage,
|
2021-07-15 23:48:09 +00:00
|
|
|
messageId: undefined,
|
2021-05-25 22:40:04 +00:00
|
|
|
online: true,
|
|
|
|
recipients: groupMembers,
|
|
|
|
sendOptions,
|
2021-12-10 02:15:59 +00:00
|
|
|
sendTarget: this.toSenderKeyTarget(),
|
2021-07-15 23:48:09 +00:00
|
|
|
sendType: 'typing',
|
2021-05-25 22:40:04 +00:00
|
|
|
timestamp,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: false,
|
2021-07-15 23:48:09 +00:00
|
|
|
}),
|
|
|
|
{ messageIds: [], sendType: 'typing' }
|
2021-05-25 22:40:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 00:44:14 +00:00
|
|
|
async onNewMessage(message: MessageModel): Promise<void> {
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = message.get('sourceServiceId');
|
2021-06-16 00:44:14 +00:00
|
|
|
const e164 = message.get('source');
|
|
|
|
const sourceDevice = message.get('sourceDevice');
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
const source = window.ConversationController.lookupOrCreate({
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId,
|
2020-09-24 20:57:54 +00:00
|
|
|
e164,
|
2022-12-03 01:05:27 +00:00
|
|
|
reason: 'ConversationModel.onNewMessage',
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2023-03-20 18:03:21 +00:00
|
|
|
if (source) {
|
|
|
|
const typingToken = `${source.id}.${sourceDevice}`;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-03-20 18:03:21 +00:00
|
|
|
// Clear typing indicator for a given contact if we receive a message from them
|
|
|
|
this.clearContactTypingTimer(typingToken);
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
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;
|
2022-04-07 21:13:58 +00:00
|
|
|
}
|
2021-06-16 00:44:14 +00:00
|
|
|
|
2023-04-05 20:48:00 +00:00
|
|
|
// Change to message request state if contact was removed and sent message.
|
|
|
|
if (
|
|
|
|
this.get('removalStage') === 'justNotification' &&
|
|
|
|
isIncoming(message.attributes)
|
|
|
|
) {
|
|
|
|
this.set({
|
|
|
|
removalStage: 'messageRequest',
|
|
|
|
});
|
2023-05-03 21:10:05 +00:00
|
|
|
await this.maybeClearContactRemoved();
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2023-04-05 20:48:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.addSingleMessage(message);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 11:25:24 +00:00
|
|
|
// 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);
|
2022-03-29 22:30:55 +00:00
|
|
|
this.doAddSingleMessage(message, { isJustSent });
|
2023-08-22 21:01:36 +00:00
|
|
|
this.debouncedUpdateLastMessage();
|
2022-03-29 22:30:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 22:47:38 +00:00
|
|
|
private async beforeAddSingleMessage(message: MessageModel): Promise<void> {
|
2023-07-21 22:10:32 +00:00
|
|
|
await message.hydrateStoryContext(undefined, { shouldSave: true });
|
2022-10-20 22:47:38 +00:00
|
|
|
|
2021-11-30 11:25:24 +00:00
|
|
|
if (!this.newMessageQueue) {
|
2022-08-25 05:04:42 +00:00
|
|
|
this.newMessageQueue = new PQueue({
|
2020-09-28 16:43:21 +00:00
|
|
|
concurrency: 1,
|
2023-04-06 00:03:15 +00:00
|
|
|
timeout: FETCH_TIMEOUT * 2,
|
2020-09-28 16:43:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use a queue here to ensure messages are added to the UI in the order received
|
2021-11-30 11:25:24 +00:00
|
|
|
await this.newMessageQueue.add(async () => {
|
2020-09-28 16:43:21 +00:00
|
|
|
await this.inProgressFetch;
|
2021-11-30 11:25:24 +00:00
|
|
|
});
|
2022-03-29 22:30:55 +00:00
|
|
|
}
|
2021-11-30 11:25:24 +00:00
|
|
|
|
2022-03-29 22:30:55 +00:00
|
|
|
private doAddSingleMessage(
|
|
|
|
message: MessageModel,
|
|
|
|
{ isJustSent }: { isJustSent: boolean }
|
|
|
|
): void {
|
2021-11-30 11:25:24 +00:00
|
|
|
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;
|
|
|
|
|
2022-03-23 01:37:45 +00:00
|
|
|
if (isJustSent && existingConversation && !isLatestInMemory) {
|
2023-09-14 16:57:38 +00:00
|
|
|
// The message is being sent before the user has scrolled down to load the newest
|
|
|
|
// messages into memory; in that case, we scroll the user all the way down by
|
|
|
|
// loading the newest message
|
|
|
|
drop(this.loadNewestMessages(newestId, undefined));
|
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))
|
|
|
|
) {
|
2021-11-30 11:25:24 +00:00
|
|
|
messagesAdded({
|
|
|
|
conversationId,
|
|
|
|
messages: [{ ...message.attributes }],
|
2022-07-05 16:44:53 +00:00
|
|
|
isActive: window.SignalContext.activeWindowService.isActive(),
|
2021-11-30 11:25:24 +00:00
|
|
|
isJustSent,
|
|
|
|
isNewMessage: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-04-06 00:03:15 +00:00
|
|
|
private setInProgressFetch(): () => unknown {
|
|
|
|
const logId = `setInProgressFetch(${this.idForLogging()})`;
|
|
|
|
const start = Date.now();
|
|
|
|
|
2021-11-30 11:25:24 +00:00
|
|
|
let resolvePromise: (value?: unknown) => void;
|
|
|
|
this.inProgressFetch = new Promise(resolve => {
|
|
|
|
resolvePromise = resolve;
|
2020-09-28 16:43:21 +00:00
|
|
|
});
|
2021-11-30 11:25:24 +00:00
|
|
|
|
2023-03-20 18:03:21 +00:00
|
|
|
let timeout: NodeJS.Timeout;
|
2021-11-30 11:25:24 +00:00
|
|
|
const finish = () => {
|
2023-04-06 00:03:15 +00:00
|
|
|
const duration = Date.now() - start;
|
|
|
|
if (duration > 500) {
|
|
|
|
log.warn(`${logId}: in progress fetch took ${duration}ms`);
|
|
|
|
}
|
|
|
|
|
2021-11-30 11:25:24 +00:00
|
|
|
resolvePromise();
|
2023-03-20 18:03:21 +00:00
|
|
|
clearTimeout(timeout);
|
2021-11-30 11:25:24 +00:00
|
|
|
this.inProgressFetch = undefined;
|
|
|
|
};
|
2023-03-20 18:03:21 +00:00
|
|
|
timeout = setTimeout(() => {
|
2023-04-06 00:03:15 +00:00
|
|
|
log.warn(`${logId}: Calling finish manually after timeout`);
|
2023-03-20 18:03:21 +00:00
|
|
|
finish();
|
|
|
|
}, FETCH_TIMEOUT);
|
2021-11-30 11:25:24 +00:00
|
|
|
|
|
|
|
return finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadNewestMessages(
|
|
|
|
newestMessageId: string | undefined,
|
|
|
|
setFocus: boolean | undefined
|
|
|
|
): Promise<void> {
|
2023-04-06 00:03:15 +00:00
|
|
|
const logId = `loadNewestMessages/${this.idForLogging()}`;
|
|
|
|
|
2022-03-11 22:31:21 +00:00
|
|
|
const { messagesReset, setMessageLoadingState } =
|
2021-11-30 11:25:24 +00:00
|
|
|
window.reduxActions.conversations;
|
|
|
|
const conversationId = this.id;
|
|
|
|
|
2022-03-11 22:31:21 +00:00
|
|
|
setMessageLoadingState(
|
|
|
|
conversationId,
|
|
|
|
TimelineMessageLoadingState.DoingInitialLoad
|
|
|
|
);
|
2021-11-30 11:25:24 +00:00
|
|
|
const finish = this.setInProgressFetch();
|
|
|
|
|
|
|
|
try {
|
|
|
|
let scrollToLatestUnread = true;
|
|
|
|
|
|
|
|
if (newestMessageId) {
|
2021-12-10 22:51:54 +00:00
|
|
|
const newestInMemoryMessage = await getMessageById(newestMessageId);
|
2021-11-30 11:25:24 +00:00
|
|
|
if (newestInMemoryMessage) {
|
|
|
|
// If newest in-memory message is unread, scrolling down would mean going to
|
|
|
|
// the very bottom, not the oldest unread.
|
2021-12-10 22:51:54 +00:00
|
|
|
if (isMessageUnread(newestInMemoryMessage)) {
|
2021-11-30 11:25:24 +00:00
|
|
|
scrollToLatestUnread = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.warn(
|
|
|
|
`loadNewestMessages: did not find message ${newestMessageId}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 03:03:15 +00:00
|
|
|
const metrics = await getMessageMetricsForConversation({
|
|
|
|
conversationId,
|
2022-09-30 00:57:11 +00:00
|
|
|
includeStoryReplies: !isGroup(this.attributes),
|
|
|
|
});
|
2021-11-30 11:25:24 +00:00
|
|
|
|
|
|
|
// 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.
|
2023-04-05 20:48:00 +00:00
|
|
|
if (
|
|
|
|
!newestMessageId &&
|
|
|
|
!this.getAccepted() &&
|
|
|
|
this.get('removalStage') !== 'justNotification' &&
|
|
|
|
metrics.oldest
|
|
|
|
) {
|
2023-04-06 00:03:15 +00:00
|
|
|
log.info(`${logId}: scrolling to oldest ${metrics.oldest.sent_at}`);
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.loadAndScroll(metrics.oldest.id, { disableScroll: true });
|
2021-11-30 11:25:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-22 18:35:14 +00:00
|
|
|
if (scrollToLatestUnread && metrics.oldestUnseen) {
|
2023-04-06 00:03:15 +00:00
|
|
|
log.info(
|
|
|
|
`${logId}: scrolling to oldest unseen ${metrics.oldestUnseen.sent_at}`
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.loadAndScroll(metrics.oldestUnseen.id, {
|
2021-11-30 11:25:24 +00:00
|
|
|
disableScroll: !setFocus,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-04 03:03:15 +00:00
|
|
|
const messages = await getOlderMessagesByConversation({
|
|
|
|
conversationId,
|
2022-09-30 00:57:11 +00:00
|
|
|
includeStoryReplies: !isGroup(this.attributes),
|
2021-11-30 11:25:24 +00:00
|
|
|
limit: MESSAGE_LOAD_CHUNK_SIZE,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-11-30 11:25:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const cleaned: Array<MessageModel> = await this.cleanModels(messages);
|
|
|
|
const scrollToMessageId =
|
|
|
|
setFocus && metrics.newest ? metrics.newest.id : undefined;
|
|
|
|
|
2023-04-06 00:03:15 +00:00
|
|
|
log.info(
|
|
|
|
`${logId}: loaded ${cleaned.length} messages, ` +
|
|
|
|
`latest timestamp=${cleaned.at(-1)?.get('sent_at')}`
|
|
|
|
);
|
|
|
|
|
2021-11-30 11:25:24 +00:00
|
|
|
// 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({
|
2021-11-30 11:25:24 +00:00
|
|
|
conversationId,
|
2022-01-20 00:40:29 +00:00
|
|
|
messages: cleaned.map((messageModel: MessageModel) => ({
|
2021-11-30 11:25:24 +00:00
|
|
|
...messageModel.attributes,
|
|
|
|
})),
|
|
|
|
metrics,
|
|
|
|
scrollToMessageId,
|
2022-01-20 00:40:29 +00:00
|
|
|
unboundedFetch,
|
|
|
|
});
|
2021-11-30 11:25:24 +00:00
|
|
|
} catch (error) {
|
2022-03-11 22:31:21 +00:00
|
|
|
setMessageLoadingState(conversationId, undefined);
|
2021-11-30 11:25:24 +00:00
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async loadOlderMessages(oldestMessageId: string): Promise<void> {
|
2023-04-06 00:03:15 +00:00
|
|
|
const logId = `loadOlderMessages/${this.idForLogging()}`;
|
|
|
|
|
2022-03-11 22:31:21 +00:00
|
|
|
const { messagesAdded, setMessageLoadingState, repairOldestMessage } =
|
2021-11-30 11:25:24 +00:00
|
|
|
window.reduxActions.conversations;
|
|
|
|
const conversationId = this.id;
|
|
|
|
|
2022-03-11 22:31:21 +00:00
|
|
|
setMessageLoadingState(
|
|
|
|
conversationId,
|
|
|
|
TimelineMessageLoadingState.LoadingOlderMessages
|
|
|
|
);
|
2021-11-30 11:25:24 +00:00
|
|
|
const finish = this.setInProgressFetch();
|
|
|
|
|
|
|
|
try {
|
2021-12-10 22:51:54 +00:00
|
|
|
const message = await getMessageById(oldestMessageId);
|
2021-11-30 11:25:24 +00:00
|
|
|
if (!message) {
|
2023-04-06 00:03:15 +00:00
|
|
|
throw new Error(`${logId}: failed to load message ${oldestMessageId}`);
|
2021-11-30 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const receivedAt = message.received_at;
|
|
|
|
const sentAt = message.sent_at;
|
2023-03-04 03:03:15 +00:00
|
|
|
const models = await getOlderMessagesByConversation({
|
|
|
|
conversationId,
|
2022-09-30 00:57:11 +00:00
|
|
|
includeStoryReplies: !isGroup(this.attributes),
|
2022-05-11 22:20:47 +00:00
|
|
|
limit: MESSAGE_LOAD_CHUNK_SIZE,
|
|
|
|
messageId: oldestMessageId,
|
2021-11-30 11:25:24 +00:00
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-11-30 11:25:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (models.length < 1) {
|
2023-04-06 00:03:15 +00:00
|
|
|
log.warn(`${logId}: requested, but loaded no messages`);
|
2021-11-30 11:25:24 +00:00
|
|
|
repairOldestMessage(conversationId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cleaned = await this.cleanModels(models);
|
2022-03-11 22:31:21 +00:00
|
|
|
|
2023-04-06 00:03:15 +00:00
|
|
|
log.info(
|
|
|
|
`${logId}: loaded ${cleaned.length} messages, ` +
|
|
|
|
`first timestamp=${cleaned.at(0)?.get('sent_at')}`
|
|
|
|
);
|
|
|
|
|
2021-11-30 11:25:24 +00:00
|
|
|
messagesAdded({
|
|
|
|
conversationId,
|
|
|
|
messages: cleaned.map((messageModel: MessageModel) => ({
|
|
|
|
...messageModel.attributes,
|
|
|
|
})),
|
2022-07-05 16:44:53 +00:00
|
|
|
isActive: window.SignalContext.activeWindowService.isActive(),
|
2021-11-30 11:25:24 +00:00
|
|
|
isJustSent: false,
|
|
|
|
isNewMessage: false,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2022-03-11 22:31:21 +00:00
|
|
|
setMessageLoadingState(conversationId, undefined);
|
2021-11-30 11:25:24 +00:00
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadNewerMessages(newestMessageId: string): Promise<void> {
|
2022-03-11 22:31:21 +00:00
|
|
|
const { messagesAdded, setMessageLoadingState, repairNewestMessage } =
|
2021-11-30 11:25:24 +00:00
|
|
|
window.reduxActions.conversations;
|
|
|
|
const conversationId = this.id;
|
|
|
|
|
2022-03-11 22:31:21 +00:00
|
|
|
setMessageLoadingState(
|
|
|
|
conversationId,
|
|
|
|
TimelineMessageLoadingState.LoadingNewerMessages
|
|
|
|
);
|
2021-11-30 11:25:24 +00:00
|
|
|
const finish = this.setInProgressFetch();
|
|
|
|
|
|
|
|
try {
|
2021-12-10 22:51:54 +00:00
|
|
|
const message = await getMessageById(newestMessageId);
|
2021-11-30 11:25:24 +00:00
|
|
|
if (!message) {
|
|
|
|
throw new Error(
|
|
|
|
`loadNewerMessages: failed to load message ${newestMessageId}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const receivedAt = message.received_at;
|
|
|
|
const sentAt = message.sent_at;
|
2023-03-04 03:03:15 +00:00
|
|
|
const models = await getNewerMessagesByConversation({
|
|
|
|
conversationId,
|
2022-09-30 00:57:11 +00:00
|
|
|
includeStoryReplies: !isGroup(this.attributes),
|
2022-05-11 22:20:47 +00:00
|
|
|
limit: MESSAGE_LOAD_CHUNK_SIZE,
|
2021-11-30 11:25:24 +00:00
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-11-30 11:25:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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(),
|
2021-11-30 11:25:24 +00:00
|
|
|
isJustSent: false,
|
|
|
|
isNewMessage: false,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2022-03-11 22:31:21 +00:00
|
|
|
setMessageLoadingState(conversationId, undefined);
|
2021-11-30 11:25:24 +00:00
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadAndScroll(
|
|
|
|
messageId: string,
|
|
|
|
options?: { disableScroll?: boolean }
|
|
|
|
): Promise<void> {
|
2022-03-11 22:31:21 +00:00
|
|
|
const { messagesReset, setMessageLoadingState } =
|
2021-11-30 11:25:24 +00:00
|
|
|
window.reduxActions.conversations;
|
|
|
|
const conversationId = this.id;
|
|
|
|
|
2022-03-11 22:31:21 +00:00
|
|
|
setMessageLoadingState(
|
|
|
|
conversationId,
|
|
|
|
TimelineMessageLoadingState.DoingInitialLoad
|
|
|
|
);
|
2021-11-30 11:25:24 +00:00
|
|
|
const finish = this.setInProgressFetch();
|
|
|
|
|
|
|
|
try {
|
2021-12-10 22:51:54 +00:00
|
|
|
const message = await getMessageById(messageId);
|
2021-11-30 11:25:24 +00:00
|
|
|
if (!message) {
|
|
|
|
throw new Error(
|
|
|
|
`loadMoreAndScroll: failed to load message ${messageId}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const receivedAt = message.received_at;
|
|
|
|
const sentAt = message.sent_at;
|
2021-12-20 21:05:13 +00:00
|
|
|
const { older, newer, metrics } =
|
|
|
|
await getConversationRangeCenteredOnMessage({
|
|
|
|
conversationId,
|
2022-09-30 00:57:11 +00:00
|
|
|
includeStoryReplies: !isGroup(this.attributes),
|
2021-12-20 21:05:13 +00:00
|
|
|
limit: MESSAGE_LOAD_CHUNK_SIZE,
|
2022-05-11 22:20:47 +00:00
|
|
|
messageId,
|
2021-12-20 21:05:13 +00:00
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-12-20 21:05:13 +00:00
|
|
|
});
|
2021-12-10 22:51:54 +00:00
|
|
|
const all = [...older, message, ...newer];
|
2021-11-30 11:25:24 +00:00
|
|
|
|
|
|
|
const cleaned: Array<MessageModel> = await this.cleanModels(all);
|
|
|
|
const scrollToMessageId =
|
|
|
|
options && options.disableScroll ? undefined : messageId;
|
|
|
|
|
2022-01-20 00:40:29 +00:00
|
|
|
messagesReset({
|
2021-11-30 11:25:24 +00:00
|
|
|
conversationId,
|
2022-01-20 00:40:29 +00:00
|
|
|
messages: cleaned.map((messageModel: MessageModel) => ({
|
2021-11-30 11:25:24 +00:00
|
|
|
...messageModel.attributes,
|
|
|
|
})),
|
|
|
|
metrics,
|
2022-01-20 00:40:29 +00:00
|
|
|
scrollToMessageId,
|
|
|
|
});
|
2021-11-30 11:25:24 +00:00
|
|
|
} catch (error) {
|
2022-03-11 22:31:21 +00:00
|
|
|
setMessageLoadingState(conversationId, undefined);
|
2021-11-30 11:25:24 +00:00
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async cleanModels(
|
2021-12-10 22:51:54 +00:00
|
|
|
messages: ReadonlyArray<MessageAttributesType>
|
2021-11-30 11:25:24 +00:00
|
|
|
): Promise<Array<MessageModel>> {
|
2021-12-10 22:51:54 +00:00
|
|
|
const result = messages
|
|
|
|
.filter(message => Boolean(message.id))
|
2023-10-04 00:12:57 +00:00
|
|
|
.map(message =>
|
|
|
|
window.MessageCache.__DEPRECATED$register(
|
|
|
|
message.id,
|
|
|
|
message,
|
|
|
|
'cleanModels'
|
|
|
|
)
|
|
|
|
);
|
2021-11-30 11:25:24 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const eliminated = messages.length - result.length;
|
2021-11-30 11:25:24 +00:00
|
|
|
if (eliminated > 0) {
|
|
|
|
log.warn(`cleanModels: Eliminated ${eliminated} messages without an id`);
|
|
|
|
}
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
2021-11-30 11:25:24 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
let upgraded = 0;
|
2021-11-30 11:25:24 +00:00
|
|
|
for (let max = result.length, i = 0; i < max; i += 1) {
|
|
|
|
const message = result[i];
|
|
|
|
const { attributes } = message;
|
|
|
|
const { schemaVersion } = attributes;
|
|
|
|
|
2023-01-05 21:58:13 +00:00
|
|
|
if ((schemaVersion || 0) < Message.VERSION_NEEDED_FOR_DISPLAY) {
|
2021-11-30 11:25:24 +00:00
|
|
|
// 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
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.saveMessage(upgradedMessage, { ourAci });
|
2021-12-10 22:51:54 +00:00
|
|
|
upgraded += 1;
|
2021-11-30 11:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-10 22:51:54 +00:00
|
|
|
if (upgraded > 0) {
|
|
|
|
log.warn(`cleanModels: Upgraded schema of ${upgraded} messages`);
|
|
|
|
}
|
2021-11-30 11:25:24 +00:00
|
|
|
|
2023-07-21 22:10:32 +00:00
|
|
|
await Promise.all(
|
|
|
|
result.map(model =>
|
|
|
|
model.hydrateStoryContext(undefined, { shouldSave: true })
|
|
|
|
)
|
|
|
|
);
|
2022-03-16 17:30:14 +00:00
|
|
|
|
2021-11-30 11:25:24 +00:00
|
|
|
return result;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 18:31:57 +00:00
|
|
|
format(): ConversationType {
|
2020-10-28 21:54:33 +00:00
|
|
|
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!`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:31:35 +00:00
|
|
|
const { stack } = new Error('for stack');
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-06-08 21:31:35 +00:00
|
|
|
`Conversation.format()/${this.idForLogging()} reentrant call! ${stack}`
|
|
|
|
);
|
2020-10-28 21:54:33 +00:00
|
|
|
|
2021-06-08 21:31:35 +00:00
|
|
|
return this.oldCachedProps;
|
|
|
|
};
|
2020-10-16 18:31:57 +00:00
|
|
|
|
2021-06-08 21:31:35 +00:00
|
|
|
try {
|
2022-12-23 00:13:23 +00:00
|
|
|
const { oldCachedProps } = this;
|
2023-06-02 17:54:36 +00:00
|
|
|
const newCachedProps = getConversation(this);
|
2022-12-23 00:13:23 +00:00
|
|
|
|
|
|
|
if (oldCachedProps && isShallowEqual(oldCachedProps, newCachedProps)) {
|
|
|
|
this.cachedProps = oldCachedProps;
|
|
|
|
} else {
|
|
|
|
this.cachedProps = newCachedProps;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:31:35 +00:00
|
|
|
return this.cachedProps;
|
|
|
|
} finally {
|
|
|
|
this.format = oldFormat;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 22:18:08 +00:00
|
|
|
updateE164(e164?: string | null): void {
|
2020-09-24 20:57:54 +00:00
|
|
|
const oldValue = this.get('e164');
|
2022-12-05 22:46:54 +00:00
|
|
|
if (e164 === oldValue) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-05 23:34:49 +00:00
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
this.set('e164', e164 || undefined);
|
2021-08-05 23:34:49 +00:00
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
// This user changed their phone number
|
2024-02-07 21:38:43 +00:00
|
|
|
if (oldValue && e164 && this.get('sharingPhoneNumber')) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.addChangeNumberNotification(oldValue, e164);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2022-12-05 22:46:54 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2022-12-05 22:46:54 +00:00
|
|
|
this.trigger('idUpdated', this, 'e164', oldValue);
|
|
|
|
this.captureChange('updateE164');
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
updateServiceId(serviceId?: ServiceIdString): void {
|
|
|
|
const oldValue = this.getServiceId();
|
|
|
|
if (serviceId === oldValue) {
|
2022-12-05 22:46:54 +00:00
|
|
|
return;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2022-12-05 22:46:54 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
this.set(
|
2023-08-16 20:54:39 +00:00
|
|
|
'serviceId',
|
|
|
|
serviceId
|
|
|
|
? normalizeServiceId(serviceId, 'Conversation.updateServiceId')
|
|
|
|
: undefined
|
2023-08-10 16:43:33 +00:00
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2023-08-16 20:54:39 +00:00
|
|
|
this.trigger('idUpdated', this, 'serviceId', oldValue);
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
// 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.
|
2023-08-16 20:54:39 +00:00
|
|
|
if (oldValue && oldValue !== this.getPni()) {
|
2023-08-10 16:43:33 +00:00
|
|
|
drop(window.textsecure.storage.protocol.removeIdentityKey(oldValue));
|
2022-12-05 22:46:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
this.captureChange('updateServiceId');
|
2022-12-05 22:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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`
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.addKeyChange('trackPreviousIdentityKey - change');
|
2022-12-05 22:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.warn(`${logId}: Setting new previousIdentityKey`);
|
|
|
|
this.set({
|
|
|
|
previousIdentityKey: identityKey,
|
|
|
|
});
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 22:37:26 +00:00
|
|
|
updatePni(pni: PniString | undefined, pniSignatureVerified: boolean): void {
|
2023-08-16 20:54:39 +00:00
|
|
|
const oldValue = this.getPni();
|
2022-12-05 22:46:54 +00:00
|
|
|
if (pni === oldValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
this.set(
|
|
|
|
'pni',
|
|
|
|
pni ? normalizePni(pni, 'Conversation.updatePni') : undefined
|
|
|
|
);
|
2024-01-29 22:37:26 +00:00
|
|
|
const newPniSignatureVerified = pni ? pniSignatureVerified : false;
|
|
|
|
if (this.get('pniSignatureVerified') !== newPniSignatureVerified) {
|
|
|
|
log.warn(
|
|
|
|
`updatePni/${this.idForLogging()}: setting ` +
|
|
|
|
`pniSignatureVerified to ${newPniSignatureVerified}`
|
|
|
|
);
|
|
|
|
this.set('pniSignatureVerified', newPniSignatureVerified);
|
|
|
|
this.captureChange('pniSignatureVerified');
|
|
|
|
}
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
const pniIsPrimaryId =
|
2023-08-16 20:54:39 +00:00
|
|
|
!this.getServiceId() ||
|
|
|
|
this.getServiceId() === oldValue ||
|
|
|
|
this.getServiceId() === pni;
|
2022-12-05 22:46:54 +00:00
|
|
|
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 =
|
2023-08-10 16:43:33 +00:00
|
|
|
window.textsecure.storage.protocol.getIdentityRecord(oldValue);
|
2022-12-05 22:46:54 +00:00
|
|
|
const newIdentityRecord =
|
2023-08-10 16:43:33 +00:00
|
|
|
window.textsecure.storage.protocol.getIdentityRecord(pni);
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
newIdentityRecord &&
|
|
|
|
oldIdentityRecord &&
|
|
|
|
!constantTimeEqual(
|
|
|
|
oldIdentityRecord.publicKey,
|
|
|
|
newIdentityRecord.publicKey
|
|
|
|
)
|
|
|
|
) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.addKeyChange('updatePni - change');
|
2022-12-05 22:46:54 +00:00
|
|
|
} else if (!newIdentityRecord && oldIdentityRecord) {
|
|
|
|
this.trackPreviousIdentityKey(oldIdentityRecord.publicKey);
|
|
|
|
}
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
// We're just dropping the PNI
|
|
|
|
if (!pni) {
|
|
|
|
const oldIdentityRecord =
|
2023-08-10 16:43:33 +00:00
|
|
|
window.textsecure.storage.protocol.getIdentityRecord(oldValue);
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
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) {
|
2023-08-10 16:43:33 +00:00
|
|
|
drop(window.textsecure.storage.protocol.removeIdentityKey(oldValue));
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
2022-12-05 22:46:54 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
if (pni && !this.getServiceId()) {
|
2022-12-05 22:46:54 +00:00
|
|
|
log.warn(
|
2023-08-16 20:54:39 +00:00
|
|
|
`updatePni/${this.idForLogging()}: pni field set to ${pni}, but service id field is empty!`
|
2022-12-05 22:46:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2022-12-05 22:46:54 +00:00
|
|
|
this.trigger('idUpdated', this, 'pni', oldValue);
|
|
|
|
this.captureChange('updatePni');
|
2022-08-09 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
updateGroupId(groupId?: string): void {
|
|
|
|
const oldValue = this.get('groupId');
|
|
|
|
if (groupId && groupId !== oldValue) {
|
|
|
|
this.set('groupId', groupId);
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2020-09-24 20:57:54 +00:00
|
|
|
this.trigger('idUpdated', this, 'groupId', oldValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:55:12 +00:00
|
|
|
async updateReportingToken(token?: Uint8Array): Promise<void> {
|
|
|
|
const oldValue = this.get('reportingToken');
|
|
|
|
const newValue = token ? Bytes.toBase64(token) : undefined;
|
|
|
|
|
|
|
|
if (oldValue === newValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('reportingToken', newValue);
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2023-02-08 00:55:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
incrementMessageCount(): void {
|
|
|
|
this.set({
|
|
|
|
messageCount: (this.get('messageCount') || 0) + 1,
|
|
|
|
});
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 23:38:08 +00:00
|
|
|
incrementSentMessageCount({ dry = false }: { dry?: boolean } = {}):
|
|
|
|
| Partial<ConversationAttributesType>
|
|
|
|
| undefined {
|
2024-03-12 01:08:38 +00:00
|
|
|
const needsTitleTransition =
|
|
|
|
hasNumberTitle(this.attributes) || hasUsernameTitle(this.attributes);
|
2021-11-01 23:38:08 +00:00
|
|
|
const update = {
|
2020-09-24 20:57:54 +00:00
|
|
|
messageCount: (this.get('messageCount') || 0) + 1,
|
|
|
|
sentMessageCount: (this.get('sentMessageCount') || 0) + 1,
|
2024-03-12 01:08:38 +00:00
|
|
|
...(needsTitleTransition ? { needsTitleTransition: true } : {}),
|
2021-11-01 23:38:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (dry) {
|
|
|
|
return update;
|
2021-08-26 16:17:57 +00:00
|
|
|
}
|
2021-11-01 23:38:08 +00:00
|
|
|
this.set(update);
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2021-11-01 23:38:08 +00:00
|
|
|
|
|
|
|
return undefined;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
2020-10-06 17:06:34 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
let messages: Array<MessageAttributesType> | undefined;
|
2020-09-24 20:57:54 +00:00
|
|
|
do {
|
2021-12-10 22:51:54 +00:00
|
|
|
const first = messages ? messages[0] : undefined;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2024-07-22 18:16:33 +00:00
|
|
|
messages = await DataReader.getOlderMessagesByConversation({
|
2023-03-04 03:03:15 +00:00
|
|
|
conversationId: this.get('id'),
|
|
|
|
includeStoryReplies: !isGroup(this.attributes),
|
|
|
|
limit: 100,
|
|
|
|
messageId: first ? first.id : undefined,
|
|
|
|
receivedAt: first ? first.received_at : undefined,
|
|
|
|
sentAt: first ? first.sent_at : undefined,
|
|
|
|
storyId: undefined,
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
if (!messages.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const readMessages = messages.filter(m => !hasErrors(m) && isIncoming(m));
|
2020-10-06 17:06:34 +00:00
|
|
|
|
|
|
|
if (isLocalAction) {
|
2023-02-06 17:24:34 +00:00
|
|
|
const conversationId = this.get('id');
|
|
|
|
|
2020-10-06 17:06:34 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2023-02-06 17:24:34 +00:00
|
|
|
await conversationJobQueue.add({
|
|
|
|
type: conversationQueueJobEnum.enum.Receipts,
|
|
|
|
conversationId: this.get('id'),
|
|
|
|
receiptsType: ReceiptType.Read,
|
2023-08-16 20:54:39 +00:00
|
|
|
receipts: readMessages.map(m => {
|
|
|
|
const { sourceServiceId: senderAci } = m;
|
|
|
|
strictAssert(isAciString(senderAci), "Can't send receipt to PNI");
|
|
|
|
|
|
|
|
return {
|
|
|
|
messageId: m.id,
|
|
|
|
conversationId,
|
|
|
|
senderE164: m.source,
|
|
|
|
senderAci,
|
|
|
|
timestamp: getMessageSentTimestamp(m, { log }),
|
|
|
|
isDirectConversation: isDirectConversation(this.attributes),
|
|
|
|
};
|
|
|
|
}),
|
2023-02-06 17:24:34 +00:00
|
|
|
});
|
2020-10-06 17:06:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2020-11-12 21:22:40 +00:00
|
|
|
await Promise.all(
|
|
|
|
readMessages.map(async m => {
|
2023-10-04 00:12:57 +00:00
|
|
|
const registered = window.MessageCache.__DEPRECATED$register(
|
|
|
|
m.id,
|
|
|
|
m,
|
|
|
|
'handleReadAndDownloadAttachments'
|
|
|
|
);
|
2020-11-12 21:22:40 +00:00
|
|
|
const shouldSave = await registered.queueAttachmentDownloads();
|
|
|
|
if (shouldSave) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.saveMessage(registered.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-11-12 21:22:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
} while (messages.length > 0);
|
|
|
|
}
|
|
|
|
|
2024-03-12 16:29:31 +00:00
|
|
|
async addMessageRequestResponseEventMessage(
|
|
|
|
event: MessageRequestResponseEvent
|
|
|
|
): Promise<void> {
|
2024-04-02 16:41:28 +00:00
|
|
|
const timestamp = Date.now();
|
|
|
|
const lastMessageTimestamp =
|
|
|
|
// Fallback to `timestamp` since `lastMessageReceivedAtMs` is new
|
|
|
|
this.get('lastMessageReceivedAtMs') ?? this.get('timestamp') ?? timestamp;
|
|
|
|
|
|
|
|
const maybeLastMessageTimestamp =
|
|
|
|
event === MessageRequestResponseEvent.ACCEPT
|
|
|
|
? timestamp
|
|
|
|
: lastMessageTimestamp;
|
|
|
|
|
2024-03-12 16:29:31 +00:00
|
|
|
const message: MessageAttributesType = {
|
|
|
|
id: generateGuid(),
|
|
|
|
conversationId: this.id,
|
|
|
|
type: 'message-request-response-event',
|
2024-04-02 16:41:28 +00:00
|
|
|
sent_at: maybeLastMessageTimestamp,
|
2024-03-12 16:29:31 +00:00
|
|
|
received_at: incrementMessageCounter(),
|
2024-04-02 16:41:28 +00:00
|
|
|
received_at_ms: maybeLastMessageTimestamp,
|
2024-03-12 16:29:31 +00:00
|
|
|
readStatus: ReadStatus.Read,
|
|
|
|
seenStatus: SeenStatus.NotApplicable,
|
2024-04-02 16:41:28 +00:00
|
|
|
timestamp,
|
2024-03-12 16:29:31 +00:00
|
|
|
messageRequestResponseEvent: event,
|
|
|
|
};
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const id = await DataWriter.saveMessage(message, {
|
2024-03-12 16:29:31 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
|
|
|
forceSave: true,
|
|
|
|
});
|
|
|
|
const model = new window.Whisper.Message({
|
|
|
|
...message,
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
window.MessageCache.toMessageAttributes(model.attributes);
|
|
|
|
this.trigger('newmessage', model);
|
|
|
|
drop(this.updateLastMessage());
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
async applyMessageRequestResponse(
|
2024-03-12 16:29:31 +00:00
|
|
|
response: Proto.SyncMessage.MessageRequestResponse.Type,
|
2023-04-05 20:48:00 +00:00
|
|
|
{ fromSync = false, viaStorageServiceSync = false, shouldSave = true } = {}
|
2020-09-24 20:57:54 +00:00
|
|
|
): Promise<void> {
|
2020-11-12 20:03:07 +00:00
|
|
|
try {
|
2021-07-02 19:21:24 +00:00
|
|
|
const messageRequestEnum = Proto.SyncMessage.MessageRequestResponse.Type;
|
2020-11-12 20:03:07 +00:00
|
|
|
const isLocalAction = !fromSync && !viaStorageServiceSync;
|
|
|
|
|
|
|
|
const currentMessageRequestState = this.get('messageRequestResponseType');
|
|
|
|
const didResponseChange = response !== currentMessageRequestState;
|
|
|
|
const wasPreviouslyAccepted = this.getAccepted();
|
|
|
|
|
2024-04-12 17:07:57 +00:00
|
|
|
const didUnblock =
|
|
|
|
response === messageRequestEnum.ACCEPT && this.isBlocked();
|
|
|
|
|
2024-03-12 16:29:31 +00:00
|
|
|
if (didResponseChange) {
|
|
|
|
if (response === messageRequestEnum.ACCEPT) {
|
2024-03-15 19:11:48 +00:00
|
|
|
// Only add a message when the user took an explicit action to accept
|
|
|
|
// the message request on one of their devices
|
|
|
|
if (!viaStorageServiceSync) {
|
|
|
|
drop(
|
|
|
|
this.addMessageRequestResponseEventMessage(
|
2024-04-12 17:07:57 +00:00
|
|
|
didUnblock
|
|
|
|
? MessageRequestResponseEvent.UNBLOCK
|
|
|
|
: MessageRequestResponseEvent.ACCEPT
|
2024-03-15 19:11:48 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-03-12 16:29:31 +00:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
response === messageRequestEnum.BLOCK ||
|
|
|
|
response === messageRequestEnum.BLOCK_AND_SPAM ||
|
|
|
|
response === messageRequestEnum.BLOCK_AND_DELETE
|
|
|
|
) {
|
|
|
|
drop(
|
|
|
|
this.addMessageRequestResponseEventMessage(
|
|
|
|
MessageRequestResponseEvent.BLOCK
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
response === messageRequestEnum.SPAM ||
|
|
|
|
response === messageRequestEnum.BLOCK_AND_SPAM
|
|
|
|
) {
|
|
|
|
drop(
|
|
|
|
this.addMessageRequestResponseEventMessage(
|
|
|
|
MessageRequestResponseEvent.SPAM
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 20:03:07 +00:00
|
|
|
// Apply message request response locally
|
|
|
|
this.set({
|
|
|
|
messageRequestResponseType: response,
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2024-03-12 16:29:31 +00:00
|
|
|
const rejectConversation = async ({
|
|
|
|
isBlock = false,
|
|
|
|
isDelete = false,
|
|
|
|
isSpam = false,
|
|
|
|
}: {
|
|
|
|
isBlock?: boolean;
|
|
|
|
isDelete?: boolean;
|
|
|
|
isSpam?: boolean;
|
|
|
|
}) => {
|
|
|
|
if (isBlock) {
|
|
|
|
this.block({ viaStorageServiceSync });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBlock || isDelete) {
|
|
|
|
this.disableProfileSharing({ viaStorageServiceSync });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDelete) {
|
2024-05-28 15:56:00 +00:00
|
|
|
await this.destroyMessages({ source: 'message-request' });
|
2024-03-12 16:29:31 +00:00
|
|
|
void this.updateLastMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBlock || isDelete) {
|
|
|
|
if (isLocalAction) {
|
|
|
|
window.reduxActions.conversations.onConversationClosed(
|
|
|
|
this.id,
|
|
|
|
isBlock
|
|
|
|
? 'blocked from message request'
|
|
|
|
: 'deleted from message request'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isGroupV2(this.attributes)) {
|
|
|
|
await this.leaveGroupV2();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSpam) {
|
|
|
|
this.set({ isReported: true });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-12 20:03:07 +00:00
|
|
|
if (response === messageRequestEnum.ACCEPT) {
|
|
|
|
this.unblock({ viaStorageServiceSync });
|
2023-04-05 20:48:00 +00:00
|
|
|
if (!viaStorageServiceSync) {
|
|
|
|
await this.restoreContact({ shouldSave: false });
|
|
|
|
}
|
2020-11-12 20:03:07 +00:00
|
|
|
this.enableProfileSharing({ viaStorageServiceSync });
|
2020-10-06 17:06:34 +00:00
|
|
|
|
2020-11-12 20:03:07 +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
|
|
|
}
|
|
|
|
|
2020-11-12 20:03:07 +00:00
|
|
|
if (isLocalAction) {
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
|
|
|
const ourPni = window.textsecure.storage.user.getPni();
|
2022-07-11 18:50:14 +00:00
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
2022-07-08 20:46:25 +00:00
|
|
|
|
2021-06-07 16:39:13 +00:00
|
|
|
if (
|
|
|
|
isGroupV1(this.attributes) ||
|
|
|
|
isDirectConversation(this.attributes)
|
|
|
|
) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.sendProfileKeyUpdate();
|
2020-11-12 20:03:07 +00:00
|
|
|
} else if (
|
2021-06-07 16:39:13 +00:00
|
|
|
isGroupV2(this.attributes) &&
|
2023-08-10 16:43:33 +00:00
|
|
|
this.isMemberPending(ourAci)
|
2020-11-12 20:03:07 +00:00
|
|
|
) {
|
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'promotePendingMember',
|
2022-07-11 18:50:14 +00:00
|
|
|
usingCredentialsFrom: [ourConversation],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () =>
|
|
|
|
this.promotePendingMember(ServiceIdKind.ACI),
|
2020-11-12 20:03:07 +00:00
|
|
|
});
|
|
|
|
} else if (
|
2023-08-10 16:43:33 +00:00
|
|
|
ourPni &&
|
2021-06-07 16:39:13 +00:00
|
|
|
isGroupV2(this.attributes) &&
|
2023-08-10 16:43:33 +00:00
|
|
|
this.isMemberPending(ourPni)
|
2020-11-12 20:03:07 +00:00
|
|
|
) {
|
2022-07-08 20:46:25 +00:00
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'promotePendingMember',
|
2022-07-11 18:50:14 +00:00
|
|
|
usingCredentialsFrom: [ourConversation],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () =>
|
|
|
|
this.promotePendingMember(ServiceIdKind.PNI),
|
2022-07-08 20:46:25 +00:00
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
} else if (isGroupV2(this.attributes) && this.isMember(ourAci)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-11-12 20:03:07 +00:00
|
|
|
'applyMessageRequestResponse/accept: Already a member of v2 group'
|
|
|
|
);
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-11-12 20:03:07 +00:00
|
|
|
'applyMessageRequestResponse/accept: Neither member nor pending member of v2 group'
|
|
|
|
);
|
|
|
|
}
|
2020-10-06 17:06:34 +00:00
|
|
|
}
|
2020-11-12 20:03:07 +00:00
|
|
|
} else if (response === messageRequestEnum.BLOCK) {
|
2024-03-12 16:29:31 +00:00
|
|
|
await rejectConversation({ isBlock: true });
|
2020-11-12 20:03:07 +00:00
|
|
|
} else if (response === messageRequestEnum.DELETE) {
|
2024-03-12 16:29:31 +00:00
|
|
|
await rejectConversation({ isDelete: true });
|
2020-11-12 20:03:07 +00:00
|
|
|
} else if (response === messageRequestEnum.BLOCK_AND_DELETE) {
|
2024-03-12 16:29:31 +00:00
|
|
|
await rejectConversation({ isBlock: true, isDelete: true });
|
|
|
|
} else if (response === messageRequestEnum.SPAM) {
|
|
|
|
await rejectConversation({ isSpam: true });
|
|
|
|
} else if (response === messageRequestEnum.BLOCK_AND_SPAM) {
|
|
|
|
await rejectConversation({ isBlock: true, isSpam: true });
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2020-11-12 20:03:07 +00:00
|
|
|
} finally {
|
2023-04-05 20:48:00 +00:00
|
|
|
if (shouldSave) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2023-04-05 20:48:00 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
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> {
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
2022-07-11 18:50:14 +00:00
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
2021-01-29 22:16:48 +00:00
|
|
|
try {
|
|
|
|
if (approvalRequired) {
|
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'requestToJoin',
|
2022-07-11 18:50:14 +00:00
|
|
|
usingCredentialsFrom: [ourConversation],
|
2021-01-29 22:16:48 +00:00
|
|
|
inviteLinkPassword,
|
|
|
|
createGroupChange: () => this.addPendingApprovalRequest(),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'joinGroup',
|
2022-07-11 18:50:14 +00:00
|
|
|
usingCredentialsFrom: [ourConversation],
|
2021-01-29 22:16:48 +00:00
|
|
|
inviteLinkPassword,
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.addMember(ourAci),
|
2021-01-29 22:16:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} 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);
|
2021-01-29 22:16:48 +00:00
|
|
|
if (errorDetails !== ALREADY_REQUESTED_TO_JOIN) {
|
|
|
|
throw error;
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-01-29 22:16:48 +00:00
|
|
|
'joinGroupV2ViaLink: Got 400, but server is telling us we have already requested to join. Forcing that local state'
|
|
|
|
);
|
|
|
|
this.set({
|
|
|
|
pendingAdminApprovalV2: [
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
aci: ourAci,
|
2021-01-29 22:16:48 +00:00
|
|
|
timestamp: Date.now(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 19:21:24 +00:00
|
|
|
const messageRequestEnum = Proto.SyncMessage.MessageRequestResponse.Type;
|
2021-01-29 22:16:48 +00:00
|
|
|
|
|
|
|
// 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(),
|
|
|
|
});
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2021-01-29 22:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async cancelJoinRequest(): Promise<void> {
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.storage.user.getCheckedAci();
|
2021-01-29 22:16:48 +00:00
|
|
|
|
|
|
|
const inviteLinkPassword = this.get('groupInviteLinkPassword');
|
|
|
|
if (!inviteLinkPassword) {
|
2022-05-16 14:53:54 +00:00
|
|
|
log.warn(
|
|
|
|
`cancelJoinRequest/${this.idForLogging()}: We don't have an inviteLinkPassword!`
|
|
|
|
);
|
2021-01-29 22:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'cancelJoinRequest',
|
2022-07-08 20:46:25 +00:00
|
|
|
usingCredentialsFrom: [],
|
2021-01-29 22:16:48 +00:00
|
|
|
inviteLinkPassword,
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.denyPendingApprovalRequest(ourAci),
|
2021-01-29 22:16:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
|
|
|
const ourPni = window.textsecure.storage.user.getPni();
|
2022-07-11 18:50:14 +00:00
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
2022-07-08 20:46:25 +00:00
|
|
|
|
2023-08-10 16:43:33 +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: [],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.removePendingMember([ourAci]),
|
2020-10-06 17:06:34 +00:00
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
} else if (this.isMember(ourAci)) {
|
2022-07-08 20:46:25 +00:00
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'delete',
|
2022-07-11 18:50:14 +00:00
|
|
|
usingCredentialsFrom: [ourConversation],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.removeMember(ourAci),
|
2022-07-08 20:46:25 +00:00
|
|
|
});
|
|
|
|
// Keep PNI in pending if ACI was a member.
|
2023-08-10 16:43:33 +00:00
|
|
|
} 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: [],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.removePendingMember([ourPni]),
|
2023-05-23 23:38:58 +00:00
|
|
|
syncMessageOnly: true,
|
2020-10-06 17:06:34 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-07-08 20:46:25 +00:00
|
|
|
const logId = this.idForLogging();
|
2021-09-17 18:27:53 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:11:28 +00:00
|
|
|
async addBannedMember(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString
|
2022-03-16 00:11:28 +00:00
|
|
|
): Promise<Proto.GroupChange.Actions | undefined> {
|
2023-08-10 16:43:33 +00:00
|
|
|
if (this.isMember(serviceId)) {
|
2022-03-16 00:11:28 +00:00
|
|
|
log.warn('addBannedMember: Member is a part of the group!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (this.isMemberPending(serviceId)) {
|
2022-03-16 00:11:28 +00:00
|
|
|
log.warn('addBannedMember: Member is pending to be added to group!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (isMemberBanned(this.attributes, serviceId)) {
|
2022-03-16 00:11:28 +00:00
|
|
|
log.warn('addBannedMember: Member is already banned!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.Signal.Groups.buildAddBannedMemberChange({
|
|
|
|
group: this.attributes,
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2022-03-16 00:11:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async blockGroupLinkRequests(serviceId: ServiceIdString): Promise<void> {
|
2022-03-16 00:11:28 +00:00
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'addBannedMember',
|
2022-07-08 20:46:25 +00:00
|
|
|
usingCredentialsFrom: [],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: async () => this.addBannedMember(serviceId),
|
2022-03-16 00:11:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-29 21:19:24 +00:00
|
|
|
async toggleAdmin(conversationId: string): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = member.getCheckedServiceId(`toggleAdmin/${logId}`);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!this.isMember(serviceId)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-01-29 21:19:24 +00:00
|
|
|
`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],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.toggleAdminChange(serviceId),
|
2021-01-29 21:19:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:36:05 +00:00
|
|
|
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}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = pendingMember.getCheckedServiceId(
|
|
|
|
`removeFromGroupV2/${logId}`
|
|
|
|
);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (this.isMemberRequestingToJoin(serviceId)) {
|
|
|
|
strictAssert(isAciString(serviceId), 'Requesting member is not ACI');
|
2021-01-29 21:19:24 +00:00
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'denyPendingApprovalRequest',
|
2022-07-11 18:50:14 +00:00
|
|
|
usingCredentialsFrom: [],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.denyPendingApprovalRequest(serviceId),
|
2021-03-24 21:28:55 +00:00
|
|
|
extraConversationsForSend: [conversationId],
|
2021-01-29 21:19:24 +00:00
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
} else if (this.isMemberPending(serviceId)) {
|
2020-11-11 17:36:05 +00:00
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'removePendingMember',
|
2022-07-11 18:50:14 +00:00
|
|
|
usingCredentialsFrom: [],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.removePendingMember([serviceId]),
|
2021-03-24 21:28:55 +00:00
|
|
|
extraConversationsForSend: [conversationId],
|
2020-11-11 17:36:05 +00:00
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
} else if (this.isMember(serviceId)) {
|
2020-11-11 17:36:05 +00:00
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'removeFromGroup',
|
2022-07-08 20:46:25 +00:00
|
|
|
usingCredentialsFrom: [pendingMember],
|
2023-08-10 16:43:33 +00:00
|
|
|
createGroupChange: () => this.removeMember(serviceId),
|
2021-03-24 21:28:55 +00:00
|
|
|
extraConversationsForSend: [conversationId],
|
2020-11-11 17:36:05 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-11-11 17:36:05 +00:00
|
|
|
`removeFromGroupV2: Member ${conversationId} is neither a member nor a pending member of the group`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
async safeGetVerified(): Promise<number> {
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
if (!serviceId) {
|
2022-11-07 23:21:12 +00:00
|
|
|
return this.verifiedEnum.DEFAULT;
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 23:21:12 +00:00
|
|
|
try {
|
2023-08-10 16:43:33 +00:00
|
|
|
return await window.textsecure.storage.protocol.getVerified(serviceId);
|
2022-11-07 23:21:12 +00:00
|
|
|
} catch {
|
|
|
|
return this.verifiedEnum.DEFAULT;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateVerified(): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
await this.initialPromise;
|
|
|
|
const verified = await this.safeGetVerified();
|
|
|
|
|
2023-03-09 23:33:12 +00:00
|
|
|
const oldVerified = this.get('verified');
|
|
|
|
if (oldVerified !== verified) {
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({ verified });
|
2023-03-09 23:33:12 +00:00
|
|
|
this.captureChange(`updateVerified from=${oldVerified} to=${verified}`);
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.fetchContacts();
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
this.contactCollection!.map(async contact => {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isMe(contact.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
await contact.updateVerified();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-30 00:53:39 +00:00
|
|
|
setVerifiedDefault(): Promise<boolean> {
|
2022-11-07 23:21:12 +00:00
|
|
|
const { DEFAULT } = this.verifiedEnum;
|
2021-06-14 21:55:14 +00:00
|
|
|
return this.queueJob('setVerifiedDefault', () =>
|
2022-11-30 00:53:39 +00:00
|
|
|
this._setVerified(DEFAULT)
|
2021-06-14 21:55:14 +00:00
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 00:53:39 +00:00
|
|
|
setVerified(): Promise<boolean> {
|
2022-11-07 23:21:12 +00:00
|
|
|
const { VERIFIED } = this.verifiedEnum;
|
2022-11-30 00:53:39 +00:00
|
|
|
return this.queueJob('setVerified', () => this._setVerified(VERIFIED));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 00:53:39 +00:00
|
|
|
setUnverified(): Promise<boolean> {
|
2022-11-07 23:21:12 +00:00
|
|
|
const { UNVERIFIED } = this.verifiedEnum;
|
2022-11-30 00:53:39 +00:00
|
|
|
return this.queueJob('setUnverified', () => this._setVerified(UNVERIFIED));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 00:53:39 +00:00
|
|
|
private async _setVerified(verified: number): Promise<boolean> {
|
2022-11-07 23:21:12 +00:00
|
|
|
const { VERIFIED, DEFAULT } = this.verifiedEnum;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isDirectConversation(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
throw new Error(
|
|
|
|
'You cannot verify a group conversation. ' +
|
|
|
|
'You must verify individual contacts.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const aci = this.getAci();
|
2022-11-07 23:21:12 +00:00
|
|
|
const beginningVerified = this.get('verified') ?? DEFAULT;
|
|
|
|
const keyChange = false;
|
2023-08-10 16:43:33 +00:00
|
|
|
if (aci) {
|
2022-11-07 23:21:12 +00:00
|
|
|
if (verified === this.verifiedEnum.DEFAULT) {
|
2023-08-10 16:43:33 +00:00
|
|
|
await window.textsecure.storage.protocol.setVerified(aci, verified);
|
2022-11-07 23:21:12 +00:00
|
|
|
} else {
|
2023-08-10 16:43:33 +00:00
|
|
|
await window.textsecure.storage.protocol.setVerified(aci, verified, {
|
2022-11-07 23:21:12 +00:00
|
|
|
firstUse: false,
|
|
|
|
nonblockingApproval: true,
|
|
|
|
});
|
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
} else {
|
2023-08-10 16:43:33 +00:00
|
|
|
log.warn(`_setVerified(${this.id}): no aci to update protocol storage`);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.set({ verified });
|
2022-03-09 18:22:34 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-11-07 23:21:12 +00:00
|
|
|
if (beginningVerified !== verified) {
|
2023-03-09 23:33:12 +00:00
|
|
|
this.captureChange(
|
|
|
|
`_setVerified from=${beginningVerified} to=${verified}`
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 20:54:13 +00:00
|
|
|
const didVerifiedChange = beginningVerified !== verified;
|
2022-11-07 23:21:12 +00:00
|
|
|
const isExplicitUserAction = true;
|
2020-09-24 20:57:54 +00:00
|
|
|
if (
|
2022-04-25 20:54:13 +00:00
|
|
|
// The message came from an explicit verification in a client (not
|
|
|
|
// storage service sync)
|
|
|
|
(didVerifiedChange && isExplicitUserAction) ||
|
2022-01-04 22:24:18 +00:00
|
|
|
// 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
|
2020-09-24 20:57:54 +00:00
|
|
|
(keyChange && verified === VERIFIED)
|
|
|
|
) {
|
|
|
|
await this.addVerifiedChange(this.id, verified === VERIFIED, {
|
2022-04-20 23:41:52 +00:00
|
|
|
local: isExplicitUserAction,
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
|
|
|
}
|
2023-08-10 16:43:33 +00:00
|
|
|
if (isExplicitUserAction && aci) {
|
|
|
|
await this.sendVerifySyncMessage(this.get('e164'), aci, verified);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2022-03-21 22:06:34 +00:00
|
|
|
|
|
|
|
return keyChange;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async sendVerifySyncMessage(
|
2021-07-15 23:48:09 +00:00
|
|
|
e164: string | undefined,
|
2023-08-10 16:43:33 +00:00
|
|
|
aci: AciString,
|
2020-09-24 20:57:54 +00:00
|
|
|
state: number
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType | void> {
|
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
'sendVerifySyncMessage: We are primary device; not sending sync'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const key = await window.textsecure.storage.protocol.loadIdentityKey(aci);
|
2021-07-15 23:48:09 +00:00
|
|
|
if (!key) {
|
|
|
|
throw new Error(
|
2023-08-10 16:43:33 +00:00
|
|
|
`sendVerifySyncMessage: No identity key found for aci ${aci}`
|
2021-07-15 23:48:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
try {
|
|
|
|
await singleProtoJobQueue.add(
|
2023-08-10 16:43:33 +00:00
|
|
|
MessageSender.getVerificationSync(e164, aci, state, key)
|
2022-01-14 21:34:52 +00:00
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'sendVerifySyncMessage: Failed to queue sync message',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isVerified(): boolean {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2022-11-07 23:21:12 +00:00
|
|
|
return this.get('verified') === this.verifiedEnum.VERIFIED;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2021-07-15 23:48:09 +00:00
|
|
|
|
2024-05-13 20:13:58 +00:00
|
|
|
const contacts = this.contactCollection;
|
|
|
|
|
|
|
|
if (contacts == null || contacts.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contacts.length === 1 && isMe(contacts.first()?.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:13:58 +00:00
|
|
|
return contacts.every(contact => {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isMe(contact.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return contact.isVerified();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
isUnverified(): boolean {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
const verified = this.get('verified');
|
|
|
|
return (
|
2022-11-07 23:21:12 +00:00
|
|
|
verified !== this.verifiedEnum.VERIFIED &&
|
|
|
|
verified !== this.verifiedEnum.DEFAULT
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
}
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
if (!this.contactCollection?.length) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
return this.contactCollection?.some(contact => {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isMe(contact.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return contact.isUnverified();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-12 19:43:24 +00:00
|
|
|
getUnverified(): Array<ConversationModel> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2022-08-12 19:43:24 +00:00
|
|
|
return this.isUnverified() ? [this] : [];
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2022-08-12 19:43:24 +00:00
|
|
|
return (
|
2021-07-15 23:48:09 +00:00
|
|
|
this.contactCollection?.filter(contact => {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isMe(contact.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return contact.isUnverified();
|
2022-08-12 19:43:24 +00:00
|
|
|
}) || []
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
async setApproved(): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isDirectConversation(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
throw new Error(
|
|
|
|
'You cannot set a group conversation as trusted. ' +
|
|
|
|
'You must set individual contacts as trusted.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
if (!serviceId) {
|
|
|
|
log.warn(`setApproved(${this.id}): no serviceId, ignoring`);
|
2021-09-10 02:38:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-09 23:33:12 +00:00
|
|
|
return this.queueJob('setApproved', async () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
return window.textsecure.storage.protocol.setApproval(serviceId, true);
|
2023-03-09 23:33:12 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:05:31 +00:00
|
|
|
safeIsUntrusted(timestampThreshold?: number): boolean {
|
2020-12-08 19:37:04 +00:00
|
|
|
try {
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
strictAssert(serviceId, `No serviceId for conversation: ${this.id}`);
|
2022-08-19 18:05:31 +00:00
|
|
|
return window.textsecure.storage.protocol.isUntrusted(
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2022-08-19 18:05:31 +00:00
|
|
|
timestampThreshold
|
|
|
|
);
|
2020-12-08 19:37:04 +00:00
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:05:31 +00:00
|
|
|
isUntrusted(timestampThreshold?: number): boolean {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2022-08-19 18:05:31 +00:00
|
|
|
return this.safeIsUntrusted(timestampThreshold);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2022-11-30 00:53:39 +00:00
|
|
|
const { contactCollection } = this;
|
|
|
|
|
|
|
|
if (!contactCollection?.length) {
|
2020-12-08 19:37:04 +00:00
|
|
|
return false;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 00:53:39 +00:00
|
|
|
return contactCollection.some(contact => {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isMe(contact.attributes)) {
|
2020-12-08 19:37:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-19 18:05:31 +00:00
|
|
|
return contact.safeIsUntrusted(timestampThreshold);
|
2020-12-08 19:37:04 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:05:31 +00:00
|
|
|
getUntrusted(timestampThreshold?: number): Array<ConversationModel> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2022-08-19 18:05:31 +00:00
|
|
|
if (this.isUntrusted(timestampThreshold)) {
|
2022-08-12 19:43:24 +00:00
|
|
|
return [this];
|
2020-12-08 19:37:04 +00:00
|
|
|
}
|
2022-08-12 19:43:24 +00:00
|
|
|
return [];
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2020-12-08 19:37:04 +00:00
|
|
|
|
2022-08-12 19:43:24 +00:00
|
|
|
return (
|
|
|
|
this.contactCollection?.filter(contact => {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isMe(contact.attributes)) {
|
2021-04-27 22:35:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-19 18:05:31 +00:00
|
|
|
return contact.isUntrusted(timestampThreshold);
|
2022-08-12 19:43:24 +00:00
|
|
|
}) || []
|
2020-12-08 19:37:04 +00:00
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-06-02 17:54:36 +00:00
|
|
|
return getAboutText(this.attributes);
|
2021-01-26 01:01:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
/**
|
|
|
|
* Determine if this conversation should be considered "accepted" in terms
|
|
|
|
* of message requests
|
|
|
|
*/
|
2024-02-02 00:52:44 +00:00
|
|
|
getAccepted(options?: IsConversationAcceptedOptionsType): boolean {
|
|
|
|
return isConversationAccepted(this.attributes, options);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-09-20 18:51:30 +00:00
|
|
|
this.trigger('change', this, { force: true });
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async toggleVerified(): Promise<unknown> {
|
|
|
|
if (this.isVerified()) {
|
|
|
|
return this.setVerifiedDefault();
|
|
|
|
}
|
|
|
|
return this.setVerified();
|
|
|
|
}
|
|
|
|
|
2021-06-01 19:40:09 +00:00
|
|
|
async addChatSessionRefreshed({
|
|
|
|
receivedAt,
|
|
|
|
receivedAtCounter,
|
|
|
|
}: {
|
|
|
|
receivedAt: number;
|
|
|
|
receivedAtCounter: number;
|
|
|
|
}): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
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,
|
2021-06-01 19:40:09 +00:00
|
|
|
received_at: receivedAtCounter,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: receivedAt,
|
2021-08-12 18:15:55 +00:00
|
|
|
readStatus: ReadStatus.Unread,
|
2022-04-22 18:35:14 +00:00
|
|
|
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
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const id = await DataWriter.saveMessage(message, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2023-10-04 00:12:57 +00:00
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
2021-02-18 16:40:26 +00:00
|
|
|
id,
|
|
|
|
new window.Whisper.Message({
|
|
|
|
...message,
|
|
|
|
id,
|
2023-10-04 00:12:57 +00:00
|
|
|
}),
|
|
|
|
'addChatSessionRefreshed'
|
2021-02-18 16:40:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.trigger('newmessage', model);
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.updateUnread();
|
2021-02-18 16:40:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 19:40:09 +00:00
|
|
|
async addDeliveryIssue({
|
|
|
|
receivedAt,
|
|
|
|
receivedAtCounter,
|
2023-08-16 20:54:39 +00:00
|
|
|
senderAci,
|
2021-07-27 00:00:16 +00:00
|
|
|
sentAt,
|
2021-06-01 19:40:09 +00:00
|
|
|
}: {
|
|
|
|
receivedAt: number;
|
|
|
|
receivedAtCounter: number;
|
2023-08-16 20:54:39 +00:00
|
|
|
senderAci: AciString;
|
2021-07-27 00:00:16 +00:00
|
|
|
sentAt: number;
|
2021-06-01 19:40:09 +00:00
|
|
|
}): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`addDeliveryIssue: adding for ${this.idForLogging()}`, {
|
2021-07-27 00:00:16 +00:00
|
|
|
sentAt,
|
2023-08-16 20:54:39 +00:00
|
|
|
senderAci,
|
2021-05-28 19:11:19 +00:00
|
|
|
});
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
if (!this.get('active_at')) {
|
|
|
|
log.warn(
|
|
|
|
`addDeliveryIssue: ${this.idForLogging()} has no active_at, dropping delivery issue instead of adding`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const message = {
|
2021-05-28 19:11:19 +00:00
|
|
|
conversationId: this.id,
|
|
|
|
type: 'delivery-issue',
|
2023-08-16 20:54:39 +00:00
|
|
|
sourceServiceId: senderAci,
|
2021-05-28 19:11:19 +00:00
|
|
|
sent_at: receivedAt,
|
2021-06-01 19:40:09 +00:00
|
|
|
received_at: receivedAtCounter,
|
2021-05-28 19:11:19 +00:00
|
|
|
received_at_ms: receivedAt,
|
2021-08-12 18:15:55 +00:00
|
|
|
readStatus: ReadStatus.Unread,
|
2022-04-22 18:35:14 +00:00
|
|
|
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
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const id = await DataWriter.saveMessage(message, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2023-10-04 00:12:57 +00:00
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
2021-05-28 19:11:19 +00:00
|
|
|
id,
|
|
|
|
new window.Whisper.Message({
|
|
|
|
...message,
|
|
|
|
id,
|
2023-10-04 00:12:57 +00:00
|
|
|
}),
|
|
|
|
'addDeliveryIssue'
|
2021-05-28 19:11:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.trigger('newmessage', model);
|
|
|
|
|
|
|
|
await this.notify(model);
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.updateUnread();
|
2021-05-28 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
async addKeyChange(
|
|
|
|
reason: string,
|
|
|
|
keyChangedId?: ServiceIdString
|
|
|
|
): Promise<void> {
|
|
|
|
return this.queueJob(`addKeyChange(${keyChangedId})`, async () => {
|
2022-11-07 23:21:12 +00:00
|
|
|
log.info(
|
2022-12-05 22:46:54 +00:00
|
|
|
'adding key change advisory in',
|
2022-11-07 23:21:12 +00:00
|
|
|
this.idForLogging(),
|
2022-12-05 22:46:54 +00:00
|
|
|
'for',
|
2023-08-10 16:43:33 +00:00
|
|
|
keyChangedId || 'this conversation',
|
2022-12-05 22:46:54 +00:00
|
|
|
this.get('timestamp'),
|
|
|
|
'reason:',
|
|
|
|
reason
|
2022-11-07 23:21:12 +00:00
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
if (!keyChangedId && !isDirectConversation(this.attributes)) {
|
|
|
|
throw new Error(
|
|
|
|
'addKeyChange: Cannot omit keyChangedId in group conversation!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-07 23:21:12 +00:00
|
|
|
const timestamp = Date.now();
|
2022-12-05 22:46:54 +00:00
|
|
|
const message: MessageAttributesType = {
|
|
|
|
id: generateGuid(),
|
2022-11-07 23:21:12 +00:00
|
|
|
conversationId: this.id,
|
|
|
|
type: 'keychange',
|
2022-12-05 22:46:54 +00:00
|
|
|
sent_at: timestamp,
|
|
|
|
timestamp,
|
2023-04-11 03:54:43 +00:00
|
|
|
received_at: incrementMessageCounter(),
|
2022-11-07 23:21:12 +00:00
|
|
|
received_at_ms: timestamp,
|
2023-08-10 16:43:33 +00:00
|
|
|
key_changed: keyChangedId,
|
2022-11-07 23:21:12 +00:00
|
|
|
readStatus: ReadStatus.Read,
|
|
|
|
seenStatus: SeenStatus.Unseen,
|
|
|
|
schemaVersion: Message.VERSION_NEEDED_FOR_DISPLAY,
|
2022-12-05 22:46:54 +00:00
|
|
|
};
|
2022-11-07 23:21:12 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.saveMessage(message, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2022-12-05 22:46:54 +00:00
|
|
|
forceSave: true,
|
2022-11-07 23:21:12 +00:00
|
|
|
});
|
2023-10-04 00:12:57 +00:00
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
2022-12-05 22:46:54 +00:00
|
|
|
message.id,
|
2023-10-04 00:12:57 +00:00
|
|
|
new window.Whisper.Message(message),
|
|
|
|
'addKeyChange'
|
2022-11-07 23:21:12 +00:00
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-11-07 23:21:12 +00:00
|
|
|
this.trigger('newmessage', model);
|
2020-12-08 19:37:04 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = this.getServiceId();
|
2022-11-07 23:21:12 +00:00
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
|
|
|
window.reduxActions?.safetyNumber.clearSafetyNumber(this.id);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (isDirectConversation(this.attributes) && serviceId) {
|
2022-12-05 22:46:54 +00:00
|
|
|
const groups =
|
2023-08-10 16:43:33 +00:00
|
|
|
await window.ConversationController.getAllGroupsInvolvingServiceId(
|
|
|
|
serviceId
|
2022-12-05 22:46:54 +00:00
|
|
|
);
|
|
|
|
groups.forEach(group => {
|
2023-08-10 16:43:33 +00:00
|
|
|
void group.addKeyChange('addKeyChange - group fan-out', serviceId);
|
2022-12-05 22:46:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-09 03:56:23 +00:00
|
|
|
// Reset sender key for next send
|
2022-11-07 23:21:12 +00:00
|
|
|
const senderKeyInfo = this.get('senderKeyInfo');
|
|
|
|
if (senderKeyInfo) {
|
2024-02-09 03:56:23 +00:00
|
|
|
await resetSenderKey(this.toSenderKeyTarget());
|
2022-11-07 23:21:12 +00:00
|
|
|
}
|
2023-04-10 21:22:02 +00:00
|
|
|
|
|
|
|
if (isDirectConversation(this.attributes)) {
|
|
|
|
this.captureChange(`addKeyChange(${reason})`);
|
|
|
|
}
|
2022-11-07 23:21:12 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
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,
|
2023-04-11 03:54:43 +00:00
|
|
|
received_at: incrementMessageCounter(),
|
2022-12-05 22:46:54 +00:00
|
|
|
received_at_ms: timestamp,
|
|
|
|
conversationMerge: {
|
|
|
|
renderInfo,
|
|
|
|
},
|
|
|
|
readStatus: ReadStatus.Read,
|
|
|
|
seenStatus: SeenStatus.Unseen,
|
|
|
|
schemaVersion: Message.VERSION_NEEDED_FOR_DISPLAY,
|
|
|
|
};
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const id = await DataWriter.saveMessage(message, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2022-12-05 22:46:54 +00:00
|
|
|
forceSave: true,
|
|
|
|
});
|
2023-10-04 00:12:57 +00:00
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
2022-12-05 22:46:54 +00:00
|
|
|
id,
|
|
|
|
new window.Whisper.Message({
|
|
|
|
...message,
|
|
|
|
id,
|
2023-10-04 00:12:57 +00:00
|
|
|
}),
|
|
|
|
'addConversationMerge'
|
2022-12-05 22:46:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.trigger('newmessage', model);
|
|
|
|
}
|
|
|
|
|
2023-10-23 17:40:42 +00:00
|
|
|
async addPhoneNumberDiscoveryIfNeeded(originalPni: PniString): Promise<void> {
|
|
|
|
const logId = `addPhoneNumberDiscoveryIfNeeded(${this.idForLogging()}, ${originalPni})`;
|
|
|
|
|
|
|
|
const e164 = this.get('e164');
|
|
|
|
|
|
|
|
if (!e164) {
|
|
|
|
log.info(`${logId}: not adding, no e164`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hadSession = await window.textsecure.storage.protocol.hasSessionWith(
|
|
|
|
originalPni
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!hadSession) {
|
|
|
|
log.info(`${logId}: not adding, no PNI session`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`${logId}: adding notification`);
|
|
|
|
const timestamp = Date.now();
|
|
|
|
const message: MessageAttributesType = {
|
|
|
|
id: generateGuid(),
|
|
|
|
conversationId: this.id,
|
|
|
|
type: 'phone-number-discovery',
|
|
|
|
sent_at: timestamp,
|
|
|
|
timestamp,
|
|
|
|
received_at: incrementMessageCounter(),
|
|
|
|
received_at_ms: timestamp,
|
|
|
|
phoneNumberDiscovery: {
|
|
|
|
e164,
|
|
|
|
},
|
|
|
|
readStatus: ReadStatus.Read,
|
|
|
|
seenStatus: SeenStatus.Unseen,
|
|
|
|
schemaVersion: Message.VERSION_NEEDED_FOR_DISPLAY,
|
|
|
|
};
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const id = await DataWriter.saveMessage(message, {
|
2023-10-23 17:40:42 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
|
|
|
forceSave: true,
|
|
|
|
});
|
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
|
|
|
id,
|
|
|
|
new window.Whisper.Message({
|
|
|
|
...message,
|
|
|
|
id,
|
|
|
|
}),
|
|
|
|
'addPhoneNumberDiscoveryIfNeeded'
|
|
|
|
);
|
|
|
|
|
|
|
|
this.trigger('newmessage', model);
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
async addVerifiedChange(
|
|
|
|
verifiedChangeId: string,
|
|
|
|
verified: boolean,
|
2022-06-13 21:29:10 +00:00
|
|
|
options: { local?: boolean } = { local: true }
|
2020-09-24 20:57:54 +00:00
|
|
|
): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isMe(this.attributes)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('refusing to add verified change advisory for our own number');
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastMessage = this.get('timestamp') || Date.now();
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-09-24 20:57:54 +00:00
|
|
|
'adding verified change advisory for',
|
|
|
|
this.idForLogging(),
|
|
|
|
verifiedChangeId,
|
|
|
|
lastMessage
|
|
|
|
);
|
|
|
|
|
|
|
|
const timestamp = Date.now();
|
2022-06-13 21:29:10 +00:00
|
|
|
const message: MessageAttributesType = {
|
|
|
|
id: generateGuid(),
|
2020-09-24 20:57:54 +00:00
|
|
|
conversationId: this.id,
|
2022-06-13 21:29:10 +00:00
|
|
|
local: Boolean(options.local),
|
2024-01-12 00:40:39 +00:00
|
|
|
readStatus: ReadStatus.Read,
|
2022-04-22 18:35:14 +00:00
|
|
|
received_at_ms: timestamp,
|
2023-04-11 03:54:43 +00:00
|
|
|
received_at: incrementMessageCounter(),
|
2024-01-12 00:40:39 +00:00
|
|
|
seenStatus: options.local ? SeenStatus.Seen : SeenStatus.Unseen,
|
2022-04-22 18:35:14 +00:00
|
|
|
sent_at: lastMessage,
|
2022-06-13 21:29:10 +00:00
|
|
|
timestamp,
|
2022-04-22 18:35:14 +00:00
|
|
|
type: 'verified-change',
|
|
|
|
verified,
|
|
|
|
verifiedChanged: verifiedChangeId,
|
2022-06-13 21:29:10 +00:00
|
|
|
};
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.saveMessage(message, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2022-06-13 21:29:10 +00:00
|
|
|
forceSave: true,
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2023-10-04 00:12:57 +00:00
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
2022-06-13 21:29:10 +00:00
|
|
|
message.id,
|
2023-10-04 00:12:57 +00:00
|
|
|
new window.Whisper.Message(message),
|
|
|
|
'addVerifiedChange'
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.trigger('newmessage', model);
|
2024-01-12 00:40:39 +00:00
|
|
|
drop(this.updateUnread());
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
if (isDirectConversation(this.attributes) && serviceId) {
|
|
|
|
void window.ConversationController.getAllGroupsInvolvingServiceId(
|
|
|
|
serviceId
|
|
|
|
).then(groups => {
|
|
|
|
groups.forEach(group => {
|
|
|
|
void group.addVerifiedChange(this.id, verified, options);
|
|
|
|
});
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
2020-09-24 20:57:54 +00:00
|
|
|
conversationId: this.id,
|
|
|
|
type: 'profile-change',
|
2021-03-04 21:44:57 +00:00
|
|
|
sent_at: now,
|
2023-04-11 03:54:43 +00:00
|
|
|
received_at: incrementMessageCounter(),
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: now,
|
2021-08-12 18:15:55 +00:00
|
|
|
readStatus: ReadStatus.Read,
|
2022-04-22 18:35:14 +00:00
|
|
|
seenStatus: SeenStatus.NotApplicable,
|
2020-09-24 20:57:54 +00:00
|
|
|
changedId: conversationId || this.id,
|
|
|
|
profileChange,
|
|
|
|
// TODO: DESKTOP-722
|
2021-11-11 22:43:05 +00:00
|
|
|
} as unknown as MessageAttributesType;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const id = await DataWriter.saveMessage(message, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2023-10-04 00:12:57 +00:00
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
2020-09-24 20:57:54 +00:00
|
|
|
id,
|
|
|
|
new window.Whisper.Message({
|
|
|
|
...message,
|
|
|
|
id,
|
2023-10-04 00:12:57 +00:00
|
|
|
}),
|
|
|
|
'addProfileChange'
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.trigger('newmessage', model);
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
if (isDirectConversation(this.attributes) && serviceId) {
|
2024-02-06 02:13:13 +00:00
|
|
|
this.set({ profileLastUpdatedAt: Date.now() });
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
void window.ConversationController.getAllGroupsInvolvingServiceId(
|
|
|
|
serviceId
|
|
|
|
).then(groups => {
|
|
|
|
groups.forEach(group => {
|
|
|
|
void group.addProfileChange(profileChange, this.id);
|
|
|
|
});
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2023-04-11 03:54:43 +00:00
|
|
|
received_at: incrementMessageCounter(),
|
2021-06-01 20:45:43 +00:00
|
|
|
received_at_ms: now,
|
2021-08-12 18:15:55 +00:00
|
|
|
readStatus: ReadStatus.Read,
|
2022-04-22 18:35:14 +00:00
|
|
|
seenStatus: SeenStatus.NotApplicable,
|
|
|
|
|
|
|
|
...extra,
|
2021-08-05 23:34:49 +00:00
|
|
|
};
|
2021-06-01 20:45:43 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const id = await DataWriter.saveMessage(
|
2021-08-05 23:34:49 +00:00
|
|
|
// TODO: DESKTOP-722
|
2021-12-20 21:04:02 +00:00
|
|
|
message as MessageAttributesType,
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
}
|
2021-08-05 23:34:49 +00:00
|
|
|
);
|
2023-10-04 00:12:57 +00:00
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
2021-06-01 20:45:43 +00:00
|
|
|
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,
|
2023-10-04 00:12:57 +00:00
|
|
|
}),
|
|
|
|
'addNotification'
|
2021-06-01 20:45:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.trigger('newmessage', model);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:56:27 +00:00
|
|
|
async maybeSetPendingUniversalTimer(
|
|
|
|
hasUserInitiatedMessages: boolean
|
|
|
|
): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:56:27 +00:00
|
|
|
if (hasUserInitiatedMessages) {
|
2022-03-09 22:20:08 +00:00
|
|
|
await this.maybeRemoveUniversalTimer();
|
2021-06-01 20:45:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-02 20:55:10 +00:00
|
|
|
if (this.get('pendingUniversalTimer') || this.get('expireTimer')) {
|
2021-06-01 20:45:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const expireTimer = universalExpireTimer.get();
|
|
|
|
if (!expireTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-09 22:20:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-03-09 22:20:08 +00:00
|
|
|
async maybeApplyUniversalTimer(): Promise<void> {
|
|
|
|
// Check if we had a notification
|
|
|
|
if (!(await this.maybeRemoveUniversalTimer())) {
|
2021-06-01 20:45:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-09 22:20:08 +00:00
|
|
|
// 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) {
|
2022-03-09 22:20:08 +00:00
|
|
|
log.info(
|
|
|
|
`maybeApplyUniversalTimer(${this.idForLogging()}): applying timer`
|
|
|
|
);
|
|
|
|
|
2022-06-20 22:43:16 +00:00
|
|
|
await this.updateExpirationTimer(expireTimer, {
|
|
|
|
reason: 'maybeApplyUniversalTimer',
|
|
|
|
});
|
2022-03-09 22:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-03-09 22:20:08 +00:00
|
|
|
log.info(
|
|
|
|
`maybeRemoveUniversalTimer(${this.idForLogging()}): removed notification`
|
|
|
|
);
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$getById(notificationId);
|
2022-03-09 22:20:08 +00:00
|
|
|
if (message) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.removeMessage(message.id, {
|
2024-06-17 19:24:39 +00:00
|
|
|
singleProtoJobQueue,
|
|
|
|
});
|
2022-03-09 22:20:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
2021-06-01 20:45:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 20:48:00 +00:00
|
|
|
async maybeSetContactRemoved(): Promise<void> {
|
|
|
|
if (!isDirectConversation(this.attributes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-10 21:37:14 +00:00
|
|
|
if (this.attributes.removalStage !== 'justNotification') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-05 20:48:00 +00:00
|
|
|
if (this.get('pendingRemovedContactNotification')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`maybeSetContactRemoved(${this.idForLogging()}): added notification`
|
|
|
|
);
|
|
|
|
const notificationId = await this.addNotification(
|
|
|
|
'contact-removed-notification'
|
|
|
|
);
|
|
|
|
this.set('pendingRemovedContactNotification', notificationId);
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2023-04-05 20:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async maybeClearContactRemoved(): Promise<boolean> {
|
|
|
|
const notificationId = this.get('pendingRemovedContactNotification');
|
|
|
|
if (!notificationId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('pendingRemovedContactNotification', undefined);
|
|
|
|
log.info(
|
|
|
|
`maybeClearContactRemoved(${this.idForLogging()}): removed notification`
|
|
|
|
);
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$getById(notificationId);
|
2023-04-05 20:48:00 +00:00
|
|
|
if (message) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.removeMessage(message.id, {
|
2024-06-17 19:24:39 +00:00
|
|
|
singleProtoJobQueue,
|
|
|
|
});
|
2023-04-05 20:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-20 22:44:25 +00:00
|
|
|
async addChangeNumberNotification(
|
|
|
|
oldValue: string,
|
|
|
|
newValue: string
|
|
|
|
): Promise<void> {
|
2023-08-10 16:43:33 +00:00
|
|
|
const sourceServiceId = this.getCheckedServiceId(
|
2023-08-16 20:54:39 +00:00
|
|
|
'Change number notification without service id'
|
2021-10-26 22:59:08 +00:00
|
|
|
);
|
|
|
|
|
2021-11-18 20:26:53 +00:00
|
|
|
const { storage } = window.textsecure;
|
2023-08-10 16:43:33 +00:00
|
|
|
if (
|
|
|
|
storage.user.getOurServiceIdKind(sourceServiceId) !==
|
|
|
|
ServiceIdKind.Unknown
|
|
|
|
) {
|
2021-11-18 20:26:53 +00:00
|
|
|
log.info(
|
|
|
|
`Conversation ${this.idForLogging()}: not adding change number ` +
|
|
|
|
'notification for ourselves'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`Conversation ${this.idForLogging()}: adding change number ` +
|
2023-08-10 16:43:33 +00:00
|
|
|
`notification for ${sourceServiceId} from ${oldValue} to ${newValue}`
|
2021-11-18 20:26:53 +00:00
|
|
|
);
|
|
|
|
|
2021-08-05 23:34:49 +00:00
|
|
|
const convos = [
|
|
|
|
this,
|
2023-08-10 16:43:33 +00:00
|
|
|
...(await window.ConversationController.getAllGroupsInvolvingServiceId(
|
|
|
|
sourceServiceId
|
2021-10-26 22:59:08 +00:00
|
|
|
)),
|
2021-08-05 23:34:49 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
convos.map(convo => {
|
|
|
|
return convo.addNotification('change-number-notification', {
|
2022-04-22 18:35:14 +00:00
|
|
|
readStatus: ReadStatus.Read,
|
|
|
|
seenStatus: SeenStatus.Unseen,
|
2023-08-16 20:54:39 +00:00
|
|
|
sourceServiceId,
|
2021-08-05 23:34:49 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:42:33 +00:00
|
|
|
async onReadMessage(
|
|
|
|
message: MessageModel,
|
|
|
|
readAt?: number,
|
|
|
|
newestSentAt?: number
|
|
|
|
): Promise<void> {
|
2020-09-24 20:57:54 +00:00
|
|
|
// 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.
|
2021-06-14 21:55:14 +00:00
|
|
|
return this.queueJob('onReadMessage', () =>
|
2020-09-24 20:57:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
this.markRead(message.get('received_at')!, {
|
2023-09-13 14:42:33 +00:00
|
|
|
newestSentAt: newestSentAt || message.get('sent_at'),
|
2020-09-24 20:57:54 +00:00
|
|
|
sendReadReceipts: false,
|
|
|
|
readAt,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
override validate(attributes = this.attributes): string | null {
|
2022-07-13 00:37:21 +00:00
|
|
|
return validateConversation(attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 19:03:17 +00:00
|
|
|
async queueJob<T>(
|
2022-05-23 22:08:13 +00:00
|
|
|
name: string,
|
|
|
|
callback: (abortSignal: AbortSignal) => Promise<T>
|
|
|
|
): Promise<T> {
|
2022-11-09 18:59:32 +00:00
|
|
|
const logId = `conversation.queueJob(${this.idForLogging()}, ${name})`;
|
|
|
|
|
2023-02-24 19:03:17 +00:00
|
|
|
if (this.isShuttingDown) {
|
|
|
|
log.warn(`${logId}: shutting down, can't accept more work`);
|
|
|
|
throw new Error(`${logId}: shutting down, can't accept more work`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.jobQueue = this.jobQueue || new PQueue({ concurrency: 1 });
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
const taskWithTimeout = createTaskWithTimeout(callback, logId);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-05-23 22:08:13 +00:00
|
|
|
const abortController = new AbortController();
|
|
|
|
const { signal: abortSignal } = abortController;
|
|
|
|
|
2021-06-14 21:55:14 +00:00
|
|
|
const queuedAt = Date.now();
|
|
|
|
return this.jobQueue.add(async () => {
|
|
|
|
const startedAt = Date.now();
|
|
|
|
const waitTime = startedAt - queuedAt;
|
|
|
|
|
|
|
|
if (waitTime > JOB_REPORTING_THRESHOLD_MS) {
|
2022-11-09 18:59:32 +00:00
|
|
|
log.info(`${logId}: was blocked for ${waitTime}ms`);
|
2021-06-14 21:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-05-23 22:08:13 +00:00
|
|
|
return await taskWithTimeout(abortSignal);
|
|
|
|
} catch (error) {
|
|
|
|
abortController.abort();
|
|
|
|
throw error;
|
2021-06-14 21:55:14 +00:00
|
|
|
} finally {
|
|
|
|
const duration = Date.now() - startedAt;
|
|
|
|
|
|
|
|
if (duration > JOB_REPORTING_THRESHOLD_MS) {
|
2022-11-09 18:59:32 +00:00
|
|
|
log.info(`${logId}: took ${duration}ms`);
|
2021-06-14 21:55:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
isAdmin(serviceId: ServiceIdString): boolean {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const members = this.get('membersV2') || [];
|
2023-08-16 20:54:39 +00:00
|
|
|
const member = members.find(x => x.aci === serviceId);
|
2021-01-29 21:19:24 +00:00
|
|
|
if (!member) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-02 19:21:24 +00:00
|
|
|
const MEMBER_ROLES = Proto.Member.Role;
|
2021-01-29 21:19:24 +00:00
|
|
|
|
|
|
|
return member.role === MEMBER_ROLES.ADMINISTRATOR;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
getServiceId(): ServiceIdString | undefined {
|
2023-08-16 20:54:39 +00:00
|
|
|
return this.get('serviceId');
|
2023-08-10 16:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getCheckedServiceId(reason: string): ServiceIdString {
|
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
strictAssert(serviceId !== undefined, reason);
|
|
|
|
return serviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAci(): AciString | undefined {
|
2023-08-16 20:54:39 +00:00
|
|
|
const value = this.getServiceId();
|
2023-08-10 16:43:33 +00:00
|
|
|
if (value && isAciString(value)) {
|
|
|
|
return value;
|
2021-10-28 22:38:37 +00:00
|
|
|
}
|
2023-08-10 16:43:33 +00:00
|
|
|
return undefined;
|
2021-10-26 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
getCheckedAci(reason: string): AciString {
|
|
|
|
const aci = this.getAci();
|
|
|
|
strictAssert(aci !== undefined, reason);
|
|
|
|
return aci;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPni(): PniString | undefined {
|
|
|
|
return this.get('pni');
|
2021-10-26 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 21:19:24 +00:00
|
|
|
getGroupLink(): string | undefined {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.get('groupInviteLinkPassword')) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:54:36 +00:00
|
|
|
return window.Signal.Groups.buildGroupLink(this.attributes);
|
2021-01-29 21:19:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 17:06:34 +00:00
|
|
|
getMembers(
|
|
|
|
options: { includePendingMembers?: boolean } = {}
|
2020-11-03 01:19:52 +00:00
|
|
|
): Array<ConversationModel> {
|
2021-05-07 01:15:25 +00:00
|
|
|
return compact(
|
|
|
|
getConversationMembers(this.attributes, options).map(conversationAttrs =>
|
|
|
|
window.ConversationController.get(conversationAttrs.id)
|
|
|
|
)
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 20:18:35 +00:00
|
|
|
canBeAnnouncementGroup(): boolean {
|
|
|
|
if (!isGroupV2(this.attributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-09 18:15:46 +00:00
|
|
|
return true;
|
2021-07-20 20:18:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
getMemberIds(): Array<string> {
|
|
|
|
const members = this.getMembers();
|
|
|
|
return members.map(member => member.id);
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:28:55 +00:00
|
|
|
getRecipients({
|
|
|
|
includePendingMembers,
|
|
|
|
extraConversationsForSend,
|
|
|
|
}: {
|
|
|
|
includePendingMembers?: boolean;
|
2022-12-22 00:07:02 +00:00
|
|
|
extraConversationsForSend?: ReadonlyArray<string>;
|
2022-10-26 00:03:51 +00:00
|
|
|
isStoryReply?: boolean;
|
2023-08-10 16:43:33 +00:00
|
|
|
} = {}): Array<ServiceIdString> {
|
2022-07-01 00:52:03 +00:00
|
|
|
return getRecipients(this.attributes, {
|
|
|
|
includePendingMembers,
|
|
|
|
extraConversationsForSend,
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 19:22:31 +00:00
|
|
|
// Members is all people in the group
|
|
|
|
getMemberConversationIds(): Set<string> {
|
2021-08-31 20:58:39 +00:00
|
|
|
return new Set(map(this.getMembers(), conversation => conversation.id));
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
async getQuoteAttachment(
|
2022-07-13 00:37:21 +00:00
|
|
|
attachments?: Array<AttachmentType>,
|
|
|
|
preview?: Array<LinkPreviewType>,
|
|
|
|
sticker?: StickerType
|
|
|
|
): Promise<
|
|
|
|
Array<{
|
|
|
|
contentType: MIMEType;
|
2023-04-20 16:31:59 +00:00
|
|
|
fileName?: string | null;
|
|
|
|
thumbnail?: ThumbnailType | null;
|
2022-07-13 00:37:21 +00:00
|
|
|
}>
|
|
|
|
> {
|
2023-03-27 23:48:57 +00:00
|
|
|
return getQuoteAttachment(attachments, preview, sticker);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-09-24 20:57:54 +00:00
|
|
|
if (!stickerData || !packData) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2020-09-24 20:57:54 +00:00
|
|
|
`Attempted to send nonexistent (${packId}, ${stickerId}) sticker!`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { key } = packData;
|
2024-07-11 19:44:09 +00:00
|
|
|
const { emoji, width, height } = stickerData;
|
|
|
|
const data = await readStickerData(stickerData);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
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;
|
2021-10-05 22:10:08 +00:00
|
|
|
const sniffedMimeType = sniffImageMimeType(data);
|
2020-09-28 18:40:26 +00:00
|
|
|
if (sniffedMimeType) {
|
|
|
|
contentType = sniffedMimeType;
|
|
|
|
} else {
|
2021-10-05 22:10:08 +00:00
|
|
|
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 = {
|
2020-09-24 20:57:54 +00:00
|
|
|
packId,
|
|
|
|
stickerId,
|
|
|
|
packKey: key,
|
2021-10-05 22:10:08 +00:00
|
|
|
emoji,
|
2020-09-24 20:57:54 +00:00
|
|
|
data: {
|
2021-10-05 22:10:08 +00:00
|
|
|
size: data.byteLength,
|
|
|
|
data,
|
2020-09-28 18:40:26 +00:00
|
|
|
contentType,
|
2020-09-24 20:57:54 +00:00
|
|
|
width,
|
|
|
|
height,
|
2023-01-13 00:24:59 +00:00
|
|
|
blurHash: await imageToBlurHash(
|
2022-05-23 23:07:41 +00:00
|
|
|
new Blob([data], {
|
|
|
|
type: IMAGE_JPEG,
|
|
|
|
})
|
|
|
|
),
|
2020-09-24 20:57:54 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(
|
2023-03-03 04:49:16 +00:00
|
|
|
this.enqueueMessageForSend(
|
|
|
|
{
|
|
|
|
body: undefined,
|
|
|
|
attachments: [],
|
|
|
|
sticker,
|
|
|
|
},
|
|
|
|
{ dontClearDraft: true }
|
|
|
|
)
|
2022-12-21 18:41:48 +00:00
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
window.reduxActions.stickers.useSticker(packId, stickerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendProfileKeyUpdate(): Promise<void> {
|
2022-02-16 18:36:21 +00:00
|
|
|
if (isMe(this.attributes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
if (!this.get('profileSharing')) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-02-16 18:36:21 +00:00
|
|
|
'sendProfileKeyUpdate: profileSharing not enabled for conversation',
|
|
|
|
this.idForLogging()
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
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) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-02-16 18:36:21 +00:00
|
|
|
'sendProfileKeyUpdate: Failed to queue profile share',
|
|
|
|
Errors.toLogFormat(error)
|
2021-05-05 16:39:16 +00:00
|
|
|
);
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
batchReduxChanges(callback: () => void): void {
|
|
|
|
strictAssert(!this.isInReduxBatch, 'Nested redux batching is not allowed');
|
|
|
|
this.isInReduxBatch = true;
|
|
|
|
batchDispatch(() => {
|
|
|
|
try {
|
|
|
|
callback();
|
|
|
|
} finally {
|
|
|
|
this.isInReduxBatch = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeMessageSend({
|
|
|
|
message,
|
|
|
|
dontAddMessage,
|
|
|
|
dontClearDraft,
|
|
|
|
now,
|
|
|
|
extraReduxActions,
|
|
|
|
}: {
|
|
|
|
message: MessageModel;
|
|
|
|
dontAddMessage: boolean;
|
|
|
|
dontClearDraft: boolean;
|
|
|
|
now: number;
|
|
|
|
extraReduxActions?: () => void;
|
|
|
|
}): void {
|
|
|
|
this.batchReduxChanges(() => {
|
|
|
|
const { clearUnreadMetrics } = window.reduxActions.conversations;
|
|
|
|
clearUnreadMetrics(this.id);
|
|
|
|
|
2023-12-07 23:59:54 +00:00
|
|
|
const enabledProfileSharing = Boolean(!this.get('profileSharing'));
|
2023-04-20 16:31:59 +00:00
|
|
|
const unarchivedConversation = Boolean(this.get('isArchived'));
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`beforeMessageSend(${this.idForLogging()}): ` +
|
|
|
|
`clearDraft(${!dontClearDraft}) addMessage(${!dontAddMessage})`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!dontAddMessage) {
|
|
|
|
this.doAddSingleMessage(message, { isJustSent: true });
|
|
|
|
}
|
2023-05-30 17:05:07 +00:00
|
|
|
const notificationData = message.getNotificationData();
|
2023-04-20 16:31:59 +00:00
|
|
|
const draftProperties = dontClearDraft
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
draft: '',
|
|
|
|
draftEditMessage: undefined,
|
|
|
|
draftBodyRanges: [],
|
|
|
|
draftTimestamp: null,
|
|
|
|
quotedMessageId: undefined,
|
2023-10-04 00:12:57 +00:00
|
|
|
lastMessageAuthor: getMessageAuthorText(message.attributes),
|
2023-05-10 00:40:19 +00:00
|
|
|
lastMessageBodyRanges: message.get('bodyRanges'),
|
2023-05-30 17:05:07 +00:00
|
|
|
lastMessage:
|
|
|
|
notificationData?.text || message.getNotificationText() || '',
|
2023-04-20 16:31:59 +00:00
|
|
|
lastMessageStatus: 'sending' as const,
|
|
|
|
};
|
|
|
|
|
2023-06-16 01:48:50 +00:00
|
|
|
const isEditMessage = Boolean(message.get('editHistory'));
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
this.set({
|
|
|
|
...draftProperties,
|
|
|
|
...(enabledProfileSharing ? { profileSharing: true } : {}),
|
|
|
|
...(dontAddMessage
|
|
|
|
? {}
|
|
|
|
: this.incrementSentMessageCount({ dry: true })),
|
2023-06-16 01:48:50 +00:00
|
|
|
// If it's an edit message we don't want to optimistically set the
|
|
|
|
// active_at & timestamp to now. We want it to stay the same.
|
|
|
|
active_at: isEditMessage ? this.get('active_at') : now,
|
|
|
|
timestamp: isEditMessage ? this.get('timestamp') : now,
|
2023-04-20 16:31:59 +00:00
|
|
|
...(unarchivedConversation ? { isArchived: false } : {}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (enabledProfileSharing) {
|
|
|
|
this.captureChange('beforeMessageSend/mandatoryProfileSharing');
|
|
|
|
}
|
|
|
|
if (unarchivedConversation) {
|
|
|
|
this.captureChange('beforeMessageSend/unarchive');
|
|
|
|
}
|
|
|
|
|
|
|
|
extraReduxActions?.();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
async enqueueMessageForSend(
|
2022-04-11 20:57:44 +00:00
|
|
|
{
|
|
|
|
attachments,
|
|
|
|
body,
|
|
|
|
contact,
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges,
|
2022-04-11 20:57:44 +00:00
|
|
|
preview,
|
|
|
|
quote,
|
|
|
|
sticker,
|
|
|
|
}: {
|
|
|
|
attachments: Array<AttachmentType>;
|
|
|
|
body: string | undefined;
|
2023-04-20 16:31:59 +00:00
|
|
|
contact?: Array<EmbeddedContactWithHydratedAvatar>;
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges?: DraftBodyRanges;
|
2023-04-20 16:31:59 +00:00
|
|
|
preview?: Array<LinkPreviewWithHydratedData>;
|
2022-04-11 20:57:44 +00:00
|
|
|
quote?: QuotedMessageType;
|
2022-07-13 00:37:21 +00:00
|
|
|
sticker?: StickerWithHydratedData;
|
2022-04-11 20:57:44 +00:00
|
|
|
},
|
2021-06-03 21:26:56 +00:00
|
|
|
{
|
2023-04-20 16:31:59 +00:00
|
|
|
dontClearDraft = false,
|
2021-06-25 16:08:16 +00:00
|
|
|
sendHQImages,
|
2022-03-04 21:14:52 +00:00
|
|
|
storyId,
|
2021-06-03 21:26:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-01 23:38:08 +00:00
|
|
|
const now = timestamp || Date.now();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-11-01 23:38:08 +00:00
|
|
|
log.info(
|
|
|
|
'Sending message to conversation',
|
|
|
|
this.idForLogging(),
|
|
|
|
'with timestamp',
|
|
|
|
now
|
|
|
|
);
|
|
|
|
|
|
|
|
this.clearTypingTimers();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
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
|
|
|
|
2023-02-07 02:21:41 +00:00
|
|
|
// For normal messages and 1:1 story replies, we use the parent conversation's timer
|
|
|
|
if (!storyId || isDirectConversation(this.attributes)) {
|
2022-11-01 18:58:07 +00:00
|
|
|
await this.maybeApplyUniversalTimer();
|
|
|
|
expireTimer = this.get('expireTimer');
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-10-26 00:03:51 +00:00
|
|
|
const recipientMaybeConversations = map(
|
|
|
|
this.getRecipients({
|
|
|
|
isStoryReply: storyId !== undefined,
|
|
|
|
}),
|
|
|
|
identifier => window.ConversationController.get(identifier)
|
2021-08-31 20:58:39 +00:00
|
|
|
);
|
|
|
|
const recipientConversations = filter(
|
|
|
|
recipientMaybeConversations,
|
|
|
|
isNotNil
|
|
|
|
);
|
|
|
|
const recipientConversationIds = concat(
|
|
|
|
map(recipientConversations, c => c.id),
|
|
|
|
[window.ConversationController.getOurConversationIdOrThrow()]
|
|
|
|
);
|
2021-08-26 15:36:08 +00:00
|
|
|
|
2021-12-01 20:19:54 +00:00
|
|
|
// If there are link previews present in the message we shouldn't include
|
|
|
|
// any attachments as well.
|
2024-03-06 21:49:21 +00:00
|
|
|
let attachmentsToSend = preview && preview.length ? [] : attachments;
|
2021-12-01 20:19:54 +00:00
|
|
|
|
|
|
|
if (preview && preview.length) {
|
|
|
|
attachments.forEach(attachment => {
|
|
|
|
if (attachment.path) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void deleteAttachmentData(attachment.path);
|
2021-12-01 20:19:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-06 21:49:21 +00:00
|
|
|
/**
|
|
|
|
* At this point, all attachments have been processed and written to disk as draft
|
|
|
|
* attachments, via processAttachments. All transcodable images have been re-encoded
|
|
|
|
* via canvas to remove EXIF data. Images above the high-quality threshold size have
|
|
|
|
* been scaled to high-quality JPEGs.
|
|
|
|
*
|
|
|
|
* If we choose to send images in standard quality, we need to scale them down
|
|
|
|
* (potentially for the second time). When we do so, we also delete the current
|
|
|
|
* draft attachment on disk for cleanup.
|
|
|
|
*
|
|
|
|
* All draft attachments (with a path or just in-memory) will be written to disk for
|
|
|
|
* real in `upgradeMessageSchema`.
|
|
|
|
*/
|
|
|
|
if (!sendHQImages) {
|
|
|
|
attachmentsToSend = await Promise.all(
|
|
|
|
attachmentsToSend.map(async attachment => {
|
|
|
|
const downscaledAttachment = await downscaleOutgoingAttachment(
|
|
|
|
attachment
|
|
|
|
);
|
|
|
|
if (downscaledAttachment !== attachment && attachment.path) {
|
|
|
|
drop(deleteAttachmentData(attachment.path));
|
|
|
|
}
|
|
|
|
return downscaledAttachment;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
// Here we move attachments to disk
|
2022-06-13 21:39:35 +00:00
|
|
|
const attributes = await upgradeMessageSchema({
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateGuid(),
|
2021-08-31 20:58:39 +00:00
|
|
|
timestamp: now,
|
|
|
|
type: 'outgoing',
|
|
|
|
body,
|
|
|
|
conversationId: this.id,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
2021-08-31 20:58:39 +00:00
|
|
|
quote,
|
|
|
|
preview,
|
2021-12-01 20:19:54 +00:00
|
|
|
attachments: attachmentsToSend,
|
2021-08-31 20:58:39 +00:00
|
|
|
sent_at: now,
|
2023-04-11 03:54:43 +00:00
|
|
|
received_at: incrementMessageCounter(),
|
2021-08-31 20:58:39 +00:00
|
|
|
received_at_ms: now,
|
2022-11-01 18:58:07 +00:00
|
|
|
expirationStartTimestamp,
|
2021-08-31 20:58:39 +00:00
|
|
|
expireTimer,
|
2022-04-22 18:35:14 +00:00
|
|
|
readStatus: ReadStatus.Read,
|
|
|
|
seenStatus: SeenStatus.NotApplicable,
|
2021-08-31 20:58:39 +00:00
|
|
|
sticker,
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges,
|
2021-08-31 20:58:39 +00:00
|
|
|
sendHQImages,
|
|
|
|
sendStateByConversationId: zipObject(
|
|
|
|
recipientConversationIds,
|
|
|
|
repeat({
|
|
|
|
status: SendStatus.Pending,
|
|
|
|
updatedAt: now,
|
|
|
|
})
|
|
|
|
),
|
2022-03-04 21:14:52 +00:00
|
|
|
storyId,
|
2021-08-31 20:58:39 +00:00
|
|
|
});
|
2021-07-30 22:13:52 +00:00
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
const model = new window.Whisper.Message(attributes);
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$register(
|
|
|
|
model.id,
|
|
|
|
model,
|
|
|
|
'enqueueMessageForSend'
|
|
|
|
);
|
2021-04-27 22:35:35 +00:00
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
const dbStart = Date.now();
|
2021-08-26 15:36:08 +00:00
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
strictAssert(
|
|
|
|
typeof message.attributes.timestamp === 'number',
|
|
|
|
'Expected a timestamp'
|
|
|
|
);
|
2021-08-26 15:36:08 +00:00
|
|
|
|
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'),
|
|
|
|
},
|
2021-08-31 20:58:39 +00:00
|
|
|
async jobToInsert => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-31 20:58:39 +00:00
|
|
|
`enqueueMessageForSend: saving message ${message.id} and job ${jobToInsert.id}`
|
2021-08-26 15:36:08 +00:00
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.saveMessage(message.attributes, {
|
2021-08-31 20:58:39 +00:00
|
|
|
jobToInsert,
|
|
|
|
forceSave: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
|
|
|
}
|
2021-08-31 20:58:39 +00:00
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
const dbDuration = Date.now() - dbStart;
|
|
|
|
if (dbDuration > SEND_REPORTING_THRESHOLD_MS) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-31 20:58:39 +00:00
|
|
|
`ConversationModel(${this.idForLogging()}.sendMessage(${now}): ` +
|
|
|
|
`db save took ${dbDuration}ms`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
2021-08-31 20:58:39 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
const renderStart = Date.now();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-10-20 22:47:38 +00:00
|
|
|
// Perform asynchronous tasks before entering the batching mode
|
|
|
|
await this.beforeAddSingleMessage(model);
|
2022-03-29 22:30:55 +00:00
|
|
|
|
2022-12-19 22:33:55 +00:00
|
|
|
if (sticker) {
|
|
|
|
await addStickerPackReference(model.id, sticker.packId);
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
this.beforeMessageSend({
|
|
|
|
message: model,
|
|
|
|
dontClearDraft,
|
|
|
|
dontAddMessage: false,
|
|
|
|
now,
|
|
|
|
extraReduxActions,
|
2022-10-20 22:47:38 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-09-12 16:52:38 +00:00
|
|
|
// The call above enables profile sharing so we have to restore contact
|
|
|
|
// afterwards, otherwise Message Request state will flash.
|
|
|
|
if (!storyId || isDirectConversation(this.attributes)) {
|
|
|
|
await this.restoreContact();
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
const renderDuration = Date.now() - renderStart;
|
|
|
|
|
|
|
|
if (renderDuration > SEND_REPORTING_THRESHOLD_MS) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-31 20:58:39 +00:00
|
|
|
`ConversationModel(${this.idForLogging()}.sendMessage(${now}): ` +
|
|
|
|
`render save took ${renderDuration}ms`
|
2021-07-15 23:48:09 +00:00
|
|
|
);
|
2021-08-31 20:58:39 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2022-04-15 00:08:46 +00:00
|
|
|
|
|
|
|
return attributes;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2022-07-14 00:46:46 +00:00
|
|
|
return Boolean(this.get('name')) || Boolean(this.get('profileSharing'));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2021-06-07 16:39:13 +00:00
|
|
|
isMe(conv.attributes) || conv.get('name') || conv.get('profileSharing')
|
2020-10-06 17:06:34 +00:00
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 18:03:51 +00:00
|
|
|
async maybeClearUsername(): Promise<void> {
|
|
|
|
const ourConversationId =
|
|
|
|
window.ConversationController.getOurConversationId();
|
|
|
|
|
2024-03-06 23:59:51 +00:00
|
|
|
const oldUsername = this.get('username');
|
|
|
|
|
2023-02-02 18:03:51 +00:00
|
|
|
// Clear username once we have other information about the contact
|
2024-03-06 23:59:51 +00:00
|
|
|
if (canHaveUsername(this.attributes, ourConversationId) || !oldUsername) {
|
2023-02-02 18:03:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`maybeClearUsername(${this.idForLogging()}): clearing username`);
|
|
|
|
|
|
|
|
this.unset('username');
|
2024-03-06 23:59:51 +00:00
|
|
|
|
|
|
|
if (this.get('needsTitleTransition') && getProfileName(this.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`maybeClearUsername(${this.idForLogging()}): adding a notification`
|
|
|
|
);
|
|
|
|
const { type, e164, username } = this.attributes;
|
|
|
|
|
|
|
|
this.unset('needsTitleTransition');
|
|
|
|
|
|
|
|
await this.addNotification('title-transition-notification', {
|
|
|
|
readStatus: ReadStatus.Read,
|
|
|
|
seenStatus: SeenStatus.Unseen,
|
|
|
|
titleTransition: {
|
|
|
|
renderInfo: {
|
|
|
|
type,
|
|
|
|
e164,
|
|
|
|
username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2023-02-02 18:03:51 +00:00
|
|
|
this.captureChange('clearUsername');
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateUsername(
|
|
|
|
username: string | undefined,
|
|
|
|
{ shouldSave = true }: { shouldSave?: boolean } = {}
|
|
|
|
): Promise<void> {
|
|
|
|
const ourConversationId =
|
|
|
|
window.ConversationController.getOurConversationId();
|
|
|
|
|
|
|
|
if (!canHaveUsername(this.attributes, ourConversationId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('username') === username) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`updateUsername(${this.idForLogging()}): updating username`);
|
|
|
|
|
|
|
|
this.set('username', username);
|
|
|
|
this.captureChange('updateUsername');
|
|
|
|
|
|
|
|
if (shouldSave) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2023-02-02 18:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
async updateLastMessage(): Promise<void> {
|
|
|
|
if (!this.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const ourConversationId =
|
|
|
|
window.ConversationController.getOurConversationId();
|
2021-01-20 17:31:44 +00:00
|
|
|
if (!ourConversationId) {
|
|
|
|
throw new Error('updateLastMessage: Failed to fetch ourConversationId');
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversationId = this.id;
|
2021-08-16 16:56:27 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const stats = await DataReader.getConversationMessageStats({
|
2021-08-16 16:56:27 +00:00
|
|
|
conversationId,
|
2022-09-30 00:57:11 +00:00
|
|
|
includeStoryReplies: !isGroup(this.attributes),
|
2021-08-16 16:56:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// This runs as a job to avoid race conditions
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(
|
|
|
|
this.queueJob('maybeSetPendingUniversalTimer', async () =>
|
|
|
|
this.maybeSetPendingUniversalTimer(stats.hasUserInitiatedMessages)
|
|
|
|
)
|
2021-08-16 16:56:27 +00:00
|
|
|
);
|
2024-06-10 21:37:14 +00:00
|
|
|
drop(
|
|
|
|
this.queueJob('maybeAddRemovedNotificaiton', async () =>
|
|
|
|
this.maybeSetContactRemoved()
|
|
|
|
)
|
|
|
|
);
|
2021-08-16 16:56:27 +00:00
|
|
|
|
2022-03-16 00:11:28 +00:00
|
|
|
const { preview, activity } = stats;
|
2021-12-10 22:51:54 +00:00
|
|
|
let previewMessage: MessageModel | undefined;
|
|
|
|
let activityMessage: MessageModel | undefined;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
// Register the message with MessageCache so that if it already exists
|
2020-10-20 23:26:58 +00:00
|
|
|
// in memory we use that data instead of the data from the db which may
|
|
|
|
// be out of date.
|
2021-12-10 22:51:54 +00:00
|
|
|
if (preview) {
|
2023-10-04 00:12:57 +00:00
|
|
|
previewMessage = window.MessageCache.__DEPRECATED$register(
|
|
|
|
preview.id,
|
|
|
|
preview,
|
|
|
|
'previewMessage'
|
|
|
|
);
|
2020-10-20 23:26:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
if (activity) {
|
2023-10-04 00:12:57 +00:00
|
|
|
activityMessage = window.MessageCache.__DEPRECATED$register(
|
2021-12-10 22:51:54 +00:00
|
|
|
activity.id,
|
2023-10-04 00:12:57 +00:00
|
|
|
activity,
|
|
|
|
'activityMessage'
|
2020-10-20 23:26:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-25 19:21:14 +00:00
|
|
|
let timestamp = this.get('timestamp') || null;
|
|
|
|
let lastMessageReceivedAt = this.get('lastMessageReceivedAt');
|
|
|
|
let lastMessageReceivedAtMs = this.get('lastMessageReceivedAtMs');
|
2023-03-10 21:30:43 +00:00
|
|
|
if (activityMessage) {
|
2024-05-28 05:13:09 +00:00
|
|
|
const callId = activityMessage.get('callId');
|
|
|
|
const callHistory = callId
|
|
|
|
? getCallHistorySelector(window.reduxStore.getState())(callId)
|
|
|
|
: undefined;
|
|
|
|
|
2024-03-25 19:21:14 +00:00
|
|
|
timestamp =
|
2024-07-15 22:26:50 +00:00
|
|
|
callHistory?.timestamp || activityMessage.get('sent_at') || timestamp;
|
2024-03-25 19:21:14 +00:00
|
|
|
lastMessageReceivedAt =
|
2024-06-24 21:47:19 +00:00
|
|
|
activityMessage.get('received_at') || lastMessageReceivedAt;
|
2024-03-25 19:21:14 +00:00
|
|
|
lastMessageReceivedAtMs =
|
2024-06-24 21:47:19 +00:00
|
|
|
activityMessage.get('received_at_ms') || lastMessageReceivedAtMs;
|
2023-03-10 21:30:43 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-04-10 16:31:45 +00:00
|
|
|
const notificationData = previewMessage?.getNotificationData();
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({
|
2023-04-18 23:33:24 +00:00
|
|
|
lastMessage:
|
|
|
|
notificationData?.text || previewMessage?.getNotificationText() || '',
|
2023-04-10 16:31:45 +00:00
|
|
|
lastMessageBodyRanges: notificationData?.bodyRanges,
|
|
|
|
lastMessagePrefix: notificationData?.emoji,
|
2023-10-04 00:12:57 +00:00
|
|
|
lastMessageAuthor: getMessageAuthorText(previewMessage?.attributes),
|
2020-09-24 20:57:54 +00:00
|
|
|
lastMessageStatus:
|
2021-06-17 17:15:10 +00:00
|
|
|
(previewMessage
|
2021-07-21 19:21:16 +00:00
|
|
|
? getMessagePropStatus(previewMessage.attributes, ourConversationId)
|
2021-06-17 17:15:10 +00:00
|
|
|
: null) || null,
|
2024-03-25 19:21:14 +00:00
|
|
|
lastMessageReceivedAt,
|
|
|
|
lastMessageReceivedAtMs,
|
2020-09-24 20:57:54 +00:00
|
|
|
timestamp,
|
|
|
|
lastMessageDeletedForEveryone: previewMessage
|
2020-10-30 17:56:03 +00:00
|
|
|
? previewMessage.get('deletedForEveryone')
|
2020-09-24 20:57:54 +00:00
|
|
|
: false,
|
|
|
|
});
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setArchived(isArchived: boolean): void {
|
|
|
|
const before = this.get('isArchived');
|
|
|
|
|
|
|
|
this.set({ isArchived });
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
const after = this.get('isArchived');
|
|
|
|
|
|
|
|
if (Boolean(before) !== Boolean(after)) {
|
2020-10-02 18:30:43 +00:00
|
|
|
if (after) {
|
2022-02-03 01:21:02 +00:00
|
|
|
this.unpin();
|
2020-10-02 18:30:43 +00:00
|
|
|
}
|
2020-10-15 00:36:31 +00:00
|
|
|
this.captureChange('isArchived');
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 22:54:32 +00:00
|
|
|
setMarkedUnread(markedUnread: boolean): void {
|
2020-11-10 15:53:34 +00:00
|
|
|
const previousMarkedUnread = this.get('markedUnread');
|
|
|
|
|
2020-10-28 22:54:32 +00:00
|
|
|
this.set({ markedUnread });
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2020-11-10 15:53:34 +00:00
|
|
|
|
|
|
|
if (Boolean(previousMarkedUnread) !== Boolean(markedUnread)) {
|
|
|
|
this.captureChange('markedUnread');
|
|
|
|
}
|
2020-10-28 22:54:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 21:19:24 +00:00
|
|
|
async refreshGroupLink(): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const groupInviteLinkPassword = Bytes.toBase64(
|
2021-01-29 21:19:24 +00:00
|
|
|
window.Signal.Groups.generateGroupInviteLinkPassword()
|
|
|
|
);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('refreshGroupLink for conversation', this.idForLogging());
|
2021-01-29 21:19:24 +00:00
|
|
|
|
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'updateInviteLinkPassword',
|
2022-07-08 20:46:25 +00:00
|
|
|
usingCredentialsFrom: [],
|
2021-01-29 21:19:24 +00:00
|
|
|
createGroupChange: async () =>
|
|
|
|
window.Signal.Groups.buildInviteLinkPasswordChange(
|
|
|
|
this.attributes,
|
|
|
|
groupInviteLinkPassword
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set({ groupInviteLinkPassword });
|
|
|
|
}
|
|
|
|
|
|
|
|
async toggleGroupLink(value: boolean): Promise<void> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
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());
|
2021-01-29 21:19:24 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('toggleGroupLink for conversation', this.idForLogging(), value);
|
2021-01-29 21:19:24 +00:00
|
|
|
|
2021-07-02 19:21:24 +00:00
|
|
|
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
|
2021-01-29 21:19:24 +00:00
|
|
|
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: [],
|
2021-01-29 21:19:24 +00:00
|
|
|
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: [],
|
2021-01-29 21:19:24 +00:00
|
|
|
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> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-02 19:21:24 +00:00
|
|
|
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
|
2021-01-29 21:19:24 +00:00
|
|
|
|
|
|
|
const addFromInviteLink = value
|
|
|
|
? ACCESS_ENUM.ADMINISTRATOR
|
|
|
|
: ACCESS_ENUM.ANY;
|
|
|
|
|
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'updateAccessControlAddFromInviteLink',
|
2022-07-08 20:46:25 +00:00
|
|
|
usingCredentialsFrom: [],
|
2021-01-29 21:19:24 +00:00
|
|
|
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> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'updateAccessControlAttributes',
|
2022-07-08 20:46:25 +00:00
|
|
|
usingCredentialsFrom: [],
|
2021-01-29 21:19:24 +00:00
|
|
|
createGroupChange: async () =>
|
|
|
|
window.Signal.Groups.buildAccessControlAttributesChange(
|
|
|
|
this.attributes,
|
|
|
|
value
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2021-07-02 19:21:24 +00:00
|
|
|
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
|
2021-01-29 21:19:24 +00:00
|
|
|
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> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isGroupV2(this.attributes)) {
|
2021-01-29 21:19:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.modifyGroupV2({
|
|
|
|
name: 'updateAccessControlMembers',
|
2022-07-08 20:46:25 +00:00
|
|
|
usingCredentialsFrom: [],
|
2021-01-29 21:19:24 +00:00
|
|
|
createGroupChange: async () =>
|
|
|
|
window.Signal.Groups.buildAccessControlMembersChange(
|
|
|
|
this.attributes,
|
|
|
|
value
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2021-07-02 19:21:24 +00:00
|
|
|
const ACCESS_ENUM = Proto.AccessControl.AccessRequired;
|
2021-01-29 21:19:24 +00:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
async updateExpirationTimer(
|
2022-11-16 20:18:02 +00:00
|
|
|
providedExpireTimer: DurationInSeconds | undefined,
|
2022-05-25 20:48:23 +00:00
|
|
|
{
|
2022-06-20 22:43:16 +00:00
|
|
|
reason,
|
|
|
|
receivedAt,
|
|
|
|
receivedAtMS = Date.now(),
|
|
|
|
sentAt: providedSentAt,
|
|
|
|
source: providedSource,
|
2022-05-25 20:48:23 +00:00
|
|
|
fromSync = false,
|
|
|
|
isInitialSync = false,
|
|
|
|
}: {
|
2022-06-20 22:43:16 +00:00
|
|
|
reason: string;
|
|
|
|
receivedAt?: number;
|
|
|
|
receivedAtMS?: number;
|
|
|
|
sentAt?: number;
|
|
|
|
source?: string;
|
2022-05-25 20:48:23 +00:00
|
|
|
fromSync?: boolean;
|
|
|
|
isInitialSync?: boolean;
|
2022-06-20 22:43:16 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
): Promise<boolean | null | MessageModel | void> {
|
2022-06-20 22:43:16 +00:00
|
|
|
const isSetByOther = providedSource || providedSentAt !== undefined;
|
2022-03-29 00:25:11 +00:00
|
|
|
|
2022-11-09 02:38:19 +00:00
|
|
|
if (isSignalConversation(this.attributes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isGroupV2(this.attributes)) {
|
2022-03-29 00:25:11 +00:00
|
|
|
if (isSetByOther) {
|
2020-09-24 20:57:54 +00:00
|
|
|
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),
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-07 16:58:15 +00:00
|
|
|
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;
|
2020-09-24 20:57:54 +00:00
|
|
|
let source = providedSource;
|
|
|
|
if (this.get('left')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!expireTimer) {
|
|
|
|
expireTimer = undefined;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
this.get('expireTimer') === expireTimer ||
|
|
|
|
(!expireTimer && !this.get('expireTimer'))
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-20 22:43:16 +00:00
|
|
|
const logId =
|
|
|
|
`updateExpirationTimer(${this.idForLogging()}, ` +
|
|
|
|
`${expireTimer || 'disabled'}) ` +
|
|
|
|
`source=${source ?? '?'} reason=${reason}`;
|
|
|
|
|
|
|
|
log.info(`${logId}: updating`);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
// if change wasn't made remotely, send it to the number/group
|
2022-03-29 00:25:11 +00:00
|
|
|
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(
|
2022-06-20 22:43:16 +00:00
|
|
|
`${logId}: Failed to queue expiration timer update`,
|
2022-02-16 18:36:21 +00:00
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 13:24:21 +00:00
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
|
|
|
source = source || ourConversation.id;
|
|
|
|
const sourceServiceId = ourConversation.get('serviceId');
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
this.set({ expireTimer });
|
2021-06-01 20:45:43 +00:00
|
|
|
|
|
|
|
// This call actually removes universal timer notification and clears
|
|
|
|
// the pending flags.
|
2022-03-09 22:20:08 +00:00
|
|
|
await this.maybeRemoveUniversalTimer();
|
2021-06-01 20:45:43 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-06-20 22:43:16 +00:00
|
|
|
// 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;
|
|
|
|
|
2022-09-06 23:52:07 +00:00
|
|
|
const isFromSyncOperation =
|
|
|
|
reason === 'group sync' || reason === 'contact sync';
|
|
|
|
const isFromMe =
|
2024-04-30 13:24:21 +00:00
|
|
|
window.ConversationController.get(source) === ourConversation;
|
2022-07-01 00:18:18 +00:00
|
|
|
const isNoteToSelf = isMe(this.attributes);
|
2022-09-06 23:52:07 +00:00
|
|
|
const shouldBeRead =
|
|
|
|
(isInitialSync && isFromSyncOperation) || isFromMe || isNoteToSelf;
|
2022-07-01 00:18:18 +00:00
|
|
|
|
2024-02-22 02:02:31 +00:00
|
|
|
const id = generateGuid();
|
2024-05-28 15:56:00 +00:00
|
|
|
const attributes = {
|
2024-02-22 02:02:31 +00:00
|
|
|
id,
|
2020-09-24 20:57:54 +00:00
|
|
|
conversationId: this.id,
|
|
|
|
expirationTimerUpdate: {
|
|
|
|
expireTimer,
|
|
|
|
source,
|
2024-04-30 13:24:21 +00:00
|
|
|
sourceServiceId,
|
2022-05-25 20:48:23 +00:00
|
|
|
fromSync,
|
2020-09-24 20:57:54 +00:00
|
|
|
},
|
2022-04-22 18:35:14 +00:00
|
|
|
flags: Proto.DataMessage.Flags.EXPIRATION_TIMER_UPDATE,
|
2022-07-01 00:18:18 +00:00
|
|
|
readStatus: shouldBeRead ? ReadStatus.Read : ReadStatus.Unread,
|
2022-04-22 18:35:14 +00:00
|
|
|
received_at_ms: receivedAtMS,
|
2023-04-11 03:54:43 +00:00
|
|
|
received_at: receivedAt ?? incrementMessageCounter(),
|
2022-07-01 00:18:18 +00:00
|
|
|
seenStatus: shouldBeRead ? SeenStatus.Seen : SeenStatus.Unseen,
|
2022-04-22 18:35:14 +00:00
|
|
|
sent_at: sentAt,
|
2024-05-28 15:56:00 +00:00
|
|
|
timestamp: sentAt,
|
|
|
|
type: 'timer-notification' as const,
|
|
|
|
};
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.saveMessage(attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2024-04-15 20:30:26 +00:00
|
|
|
forceSave: true,
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$register(
|
|
|
|
id,
|
2024-05-28 15:56:00 +00:00
|
|
|
new window.Whisper.Message(attributes),
|
2023-10-04 00:12:57 +00:00
|
|
|
'updateExpirationTimer'
|
|
|
|
);
|
2022-05-12 02:45:20 +00:00
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.addSingleMessage(message);
|
|
|
|
void this.updateUnread();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-06-20 22:43:16 +00:00
|
|
|
log.info(
|
2024-05-28 15:56:00 +00:00
|
|
|
`${logId}: added a notification received_at=${message.get('received_at')}`
|
2022-06-20 22:43:16 +00:00
|
|
|
);
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2024-07-19 22:06:44 +00:00
|
|
|
isSealedSenderDisabled(): boolean {
|
|
|
|
const members = this.getMembers();
|
|
|
|
if (
|
|
|
|
members.some(
|
|
|
|
member => member.get('sealedSender') === SEALED_SENDER.DISABLED
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
isSearchable(): boolean {
|
|
|
|
return !this.get('left');
|
|
|
|
}
|
|
|
|
|
|
|
|
async markRead(
|
2021-12-08 19:52:46 +00:00
|
|
|
newestUnreadAt: number,
|
2022-04-22 18:35:14 +00:00
|
|
|
options: {
|
|
|
|
readAt?: number;
|
|
|
|
sendReadReceipts: boolean;
|
|
|
|
newestSentAt?: number;
|
|
|
|
} = {
|
2021-05-07 01:15:25 +00:00
|
|
|
sendReadReceipts: true,
|
|
|
|
}
|
2021-05-03 16:38:20 +00:00
|
|
|
): Promise<void> {
|
2021-12-08 19:52:46 +00:00
|
|
|
await markConversationRead(this.attributes, newestUnreadAt, options);
|
2022-05-03 20:24:31 +00:00
|
|
|
await this.updateUnread();
|
2024-03-04 22:26:49 +00:00
|
|
|
window.reduxActions.callHistory.updateCallHistoryUnreadCount();
|
2022-05-03 20:24:31 +00:00
|
|
|
}
|
2021-05-10 18:49:13 +00:00
|
|
|
|
2022-05-03 20:24:31 +00:00
|
|
|
async updateUnread(): Promise<void> {
|
2023-05-23 21:59:07 +00:00
|
|
|
const options = {
|
|
|
|
storyId: undefined,
|
|
|
|
includeStoryReplies: !isGroup(this.attributes),
|
|
|
|
};
|
|
|
|
const [unreadCount, unreadMentionsCount] = await Promise.all([
|
2024-07-22 18:16:33 +00:00
|
|
|
DataReader.getTotalUnreadForConversation(this.id, options),
|
|
|
|
DataReader.getTotalUnreadMentionsOfMeForConversation(this.id, options),
|
2023-05-23 21:59:07 +00:00
|
|
|
]);
|
2021-05-17 16:52:09 +00:00
|
|
|
|
|
|
|
const prevUnreadCount = this.get('unreadCount');
|
2023-05-23 21:59:07 +00:00
|
|
|
const prevUnreadMentionsCount = this.get('unreadMentionsCount');
|
|
|
|
if (
|
|
|
|
prevUnreadCount !== unreadCount ||
|
|
|
|
prevUnreadMentionsCount !== unreadMentionsCount
|
|
|
|
) {
|
|
|
|
this.set({
|
|
|
|
unreadCount,
|
|
|
|
unreadMentionsCount,
|
|
|
|
});
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2021-05-17 16:52:09 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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> {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isDirectConversation(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isMe(this.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
|
|
|
const theirAci = this.getAci();
|
|
|
|
if (!theirAci) {
|
2021-10-26 22:59:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-01-06 15:20:54 +00:00
|
|
|
const ourGroups =
|
2023-08-10 16:43:33 +00:00
|
|
|
await window.ConversationController.getAllGroupsInvolvingServiceId(
|
|
|
|
ourAci
|
|
|
|
);
|
2022-01-06 15:20:54 +00:00
|
|
|
const sharedGroups = ourGroups
|
2023-08-10 16:43:33 +00:00
|
|
|
.filter(c => c.hasMember(ourAci) && c.hasMember(theirAci))
|
2022-01-06 15:20:54 +00:00
|
|
|
.sort(
|
|
|
|
(left, right) =>
|
|
|
|
(right.get('timestamp') || 0) - (left.get('timestamp') || 0)
|
|
|
|
);
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
const sharedGroupNames = sharedGroups.map(conversation =>
|
|
|
|
conversation.getTitle()
|
|
|
|
);
|
|
|
|
|
|
|
|
this.set({ sharedGroupNames });
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeProfileKey(): void {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isDirectConversation(this.attributes)) {
|
2024-03-25 17:03:15 +00:00
|
|
|
drop(
|
|
|
|
this.getProfiles().catch(() => {
|
|
|
|
/* nothing to do here; logging already happened */
|
|
|
|
})
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 21:32:56 +00:00
|
|
|
async getProfiles(): Promise<void> {
|
2020-09-24 20:57:54 +00:00
|
|
|
// request all conversation members' keys
|
2021-11-11 22:43:05 +00:00
|
|
|
const conversations =
|
|
|
|
this.getMembers() as unknown as Array<ConversationModel>;
|
2022-01-11 19:40:20 +00:00
|
|
|
|
2022-07-14 00:46:46 +00:00
|
|
|
await Promise.all(
|
|
|
|
conversations.map(conversation =>
|
2023-08-16 20:54:39 +00:00
|
|
|
getProfile(conversation.getServiceId(), conversation.get('e164'))
|
2022-07-14 00:46:46 +00:00
|
|
|
)
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 20:23:21 +00:00
|
|
|
async setEncryptedProfileName(
|
|
|
|
encryptedName: string,
|
|
|
|
decryptionKey: Uint8Array
|
|
|
|
): Promise<void> {
|
2020-09-24 20:57:54 +00:00
|
|
|
if (!encryptedName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
2022-03-09 20:23:21 +00:00
|
|
|
const { given, family } = decryptProfileName(encryptedName, decryptionKey);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// encode
|
2021-09-24 00:49:05 +00:00
|
|
|
const profileName = given ? Bytes.toString(given) : undefined;
|
|
|
|
const profileFamilyName = family ? Bytes.toString(family) : undefined;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// 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;
|
2021-06-07 16:39:13 +00:00
|
|
|
if (!isMe(this.attributes) && hadPreviousName && nameChanged) {
|
2020-09-24 20:57:54 +00:00
|
|
|
const change = {
|
|
|
|
type: 'name',
|
|
|
|
oldName,
|
|
|
|
newName,
|
|
|
|
};
|
|
|
|
|
|
|
|
await this.addProfileChange(change);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 20:23:21 +00:00
|
|
|
async setProfileAvatar(
|
2024-07-11 19:44:09 +00:00
|
|
|
avatarUrl: undefined | null | string,
|
2022-03-09 20:23:21 +00:00
|
|
|
decryptionKey: Uint8Array
|
|
|
|
): Promise<void> {
|
2021-07-02 20:16:23 +00:00
|
|
|
if (isMe(this.attributes)) {
|
2024-07-11 19:44:09 +00:00
|
|
|
if (avatarUrl) {
|
|
|
|
await window.storage.put('avatarUrl', avatarUrl);
|
2021-07-02 20:16:23 +00:00
|
|
|
} else {
|
2022-12-21 18:41:48 +00:00
|
|
|
await window.storage.remove('avatarUrl');
|
2021-07-02 20:16:23 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 19:44:09 +00:00
|
|
|
if (!avatarUrl) {
|
2021-07-02 20:16:23 +00:00
|
|
|
this.set({ profileAvatar: undefined });
|
|
|
|
return;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
if (!messaging) {
|
|
|
|
throw new Error('setProfileAvatar: Cannot fetch avatar when offline!');
|
|
|
|
}
|
2024-07-11 19:44:09 +00:00
|
|
|
const avatar = await messaging.getAvatar(avatarUrl);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// decrypt
|
2022-03-09 20:23:21 +00:00
|
|
|
const decrypted = decryptProfile(avatar, decryptionKey);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// update the conversation avatar only if hash differs
|
|
|
|
if (decrypted) {
|
2021-09-24 00:49:05 +00:00
|
|
|
const newAttributes = await Conversation.maybeUpdateProfileAvatar(
|
2020-09-24 20:57:54 +00:00
|
|
|
this.attributes,
|
|
|
|
{
|
2023-10-30 16:24:28 +00:00
|
|
|
data: decrypted,
|
2020-09-24 20:57:54 +00:00
|
|
|
writeNewAttachmentData,
|
|
|
|
deleteAttachmentData,
|
|
|
|
doesAttachmentExist,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
this.set(newAttributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async setProfileKey(
|
2022-02-22 20:34:57 +00:00
|
|
|
profileKey: string | undefined,
|
2020-09-24 20:57:54 +00:00
|
|
|
{ viaStorageServiceSync = false } = {}
|
2022-03-09 18:22:34 +00:00
|
|
|
): Promise<boolean> {
|
2023-01-12 22:18:08 +00:00
|
|
|
const oldProfileKey = this.get('profileKey');
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
// profileKey is a string so we can compare it directly
|
2024-03-06 23:59:51 +00:00
|
|
|
if (oldProfileKey === profileKey) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`Setting sealedSender to UNKNOWN for conversation ${this.idForLogging()}`
|
|
|
|
);
|
|
|
|
this.set({
|
|
|
|
profileKeyCredential: null,
|
|
|
|
profileKeyCredentialExpiration: null,
|
|
|
|
accessKey: null,
|
|
|
|
sealedSender: SEALED_SENDER.UNKNOWN,
|
|
|
|
});
|
|
|
|
|
|
|
|
// We messaged the contact when it had either phone number or username
|
|
|
|
// title.
|
|
|
|
if (this.get('needsTitleTransition')) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2024-03-06 23:59:51 +00:00
|
|
|
`setProfileKey(${this.idForLogging()}): adding a ` +
|
|
|
|
'title transition notification'
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
|
2024-03-06 23:59:51 +00:00
|
|
|
const { type, e164, username } = this.attributes;
|
2022-03-09 18:22:34 +00:00
|
|
|
|
2024-03-06 23:59:51 +00:00
|
|
|
this.unset('needsTitleTransition');
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2024-03-06 23:59:51 +00:00
|
|
|
await this.addNotification('title-transition-notification', {
|
|
|
|
readStatus: ReadStatus.Read,
|
|
|
|
seenStatus: SeenStatus.Unseen,
|
|
|
|
titleTransition: {
|
|
|
|
renderInfo: {
|
|
|
|
type,
|
|
|
|
e164,
|
|
|
|
username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2024-03-06 23:59:51 +00:00
|
|
|
// Don't trigger immediate profile fetches when syncing to remote storage
|
|
|
|
this.set({ profileKey }, { silent: viaStorageServiceSync });
|
2022-03-09 18:22:34 +00:00
|
|
|
|
2024-03-06 23:59:51 +00:00
|
|
|
// 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) {
|
|
|
|
this.captureChange('profileKey');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.deriveAccessKeyIfNeeded();
|
|
|
|
|
|
|
|
// We will update the conversation during storage service sync
|
|
|
|
if (!viaStorageServiceSync) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2024-03-06 23:59:51 +00:00
|
|
|
|
|
|
|
return true;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
hasProfileKeyCredentialExpired(): boolean {
|
2022-07-11 18:50:14 +00:00
|
|
|
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) {
|
2022-07-11 18:50:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-09 20:23:21 +00:00
|
|
|
deriveAccessKeyIfNeeded(): void {
|
2021-12-13 18:23:52 +00:00
|
|
|
const profileKey = this.get('profileKey');
|
2020-09-24 20:57:54 +00:00
|
|
|
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);
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({ accessKey });
|
|
|
|
}
|
|
|
|
|
2022-03-09 20:23:21 +00:00
|
|
|
deriveProfileKeyVersion(): string | undefined {
|
2020-09-24 20:57:54 +00:00
|
|
|
const profileKey = this.get('profileKey');
|
|
|
|
if (!profileKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = this.getServiceId();
|
|
|
|
if (!serviceId) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-09 20:23:21 +00:00
|
|
|
const lastProfile = this.get('lastProfile');
|
|
|
|
if (lastProfile?.profileKey === profileKey) {
|
|
|
|
return lastProfile.profileKeyVersion;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const profileKeyVersion = deriveProfileKeyVersion(profileKey, serviceId);
|
2022-02-22 20:34:57 +00:00
|
|
|
if (!profileKeyVersion) {
|
|
|
|
log.warn(
|
2022-03-09 20:23:21 +00:00
|
|
|
'deriveProfileKeyVersion: Failed to derive profile key version, ' +
|
|
|
|
'clearing profile key.'
|
2022-02-22 20:34:57 +00:00
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.setProfileKey(undefined);
|
2022-03-09 20:23:21 +00:00
|
|
|
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;
|
2022-02-22 20:34:57 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-03-09 20:23:21 +00:00
|
|
|
if (
|
|
|
|
lastProfile?.profileKey === profileKey &&
|
|
|
|
lastProfile?.profileKeyVersion === profileKeyVersion
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.warn(
|
|
|
|
'ConversationModel.updateLastProfile: updating for',
|
|
|
|
this.idForLogging()
|
|
|
|
);
|
|
|
|
|
|
|
|
this.set({ lastProfile: { profileKey, profileKeyVersion } });
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2022-03-09 20:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
hasMember(serviceId: ServiceIdString): boolean {
|
2022-07-08 20:46:25 +00:00
|
|
|
const members = this.getMembers();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
return members.some(member => member.getServiceId() === serviceId);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchContacts(): void {
|
|
|
|
const members = this.getMembers();
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
this.contactCollection!.reset(members);
|
|
|
|
}
|
|
|
|
|
2024-05-28 15:56:00 +00:00
|
|
|
async destroyMessages({
|
|
|
|
source,
|
|
|
|
}: {
|
|
|
|
source: 'message-request' | 'local-delete-sync' | 'local-delete';
|
|
|
|
}): Promise<void> {
|
|
|
|
const logId = `destroyMessages(${this.idForLogging()})/${source}`;
|
|
|
|
|
|
|
|
log.info(`${logId}: Queuing job on conversation`);
|
|
|
|
|
|
|
|
await this.queueJob(logId, async () => {
|
|
|
|
log.info(`${logId}: Starting...`);
|
|
|
|
|
|
|
|
await this.destroyMessagesInner({ logId, source });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async destroyMessagesInner({
|
2024-06-24 17:49:36 +00:00
|
|
|
logId: providedLogId,
|
2024-05-28 15:56:00 +00:00
|
|
|
source,
|
|
|
|
}: {
|
|
|
|
logId: string;
|
|
|
|
source: 'message-request' | 'local-delete-sync' | 'local-delete';
|
|
|
|
}): Promise<void> {
|
2024-06-24 17:49:36 +00:00
|
|
|
const logId = `${providedLogId}/destroyMessagesInner`;
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({
|
|
|
|
lastMessage: null,
|
2022-08-25 16:16:37 +00:00
|
|
|
lastMessageAuthor: null,
|
2020-09-24 20:57:54 +00:00
|
|
|
timestamp: null,
|
|
|
|
active_at: null,
|
2021-06-01 20:45:43 +00:00
|
|
|
pendingUniversalTimer: undefined,
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
|
|
|
const capable = Boolean(ourConversation.get('capabilities')?.deleteSync);
|
2024-07-23 16:58:40 +00:00
|
|
|
if (source === 'local-delete' && capable) {
|
2024-05-28 15:56:00 +00:00
|
|
|
log.info(`${logId}: Preparing sync message`);
|
|
|
|
const timestamp = Date.now();
|
|
|
|
|
|
|
|
const addressableMessages = await getMostRecentAddressableMessages(
|
|
|
|
this.id
|
|
|
|
);
|
|
|
|
const mostRecentMessages: Array<MessageToDelete> = addressableMessages
|
|
|
|
.map(getMessageToDelete)
|
|
|
|
.filter(isNotNil)
|
|
|
|
.slice(0, 5);
|
2024-06-24 17:49:36 +00:00
|
|
|
log.info(
|
|
|
|
`${logId}: Found ${mostRecentMessages.length} most recent messages`
|
|
|
|
);
|
|
|
|
|
|
|
|
const areAnyDisappearing = addressableMessages.some(
|
|
|
|
item => item.expireTimer
|
|
|
|
);
|
|
|
|
|
|
|
|
let mostRecentNonExpiringMessages: Array<MessageToDelete> | undefined;
|
|
|
|
if (areAnyDisappearing) {
|
|
|
|
const nondisappearingAddressableMessages =
|
|
|
|
await getMostRecentAddressableNondisappearingMessages(this.id);
|
|
|
|
mostRecentNonExpiringMessages = nondisappearingAddressableMessages
|
|
|
|
.map(getMessageToDelete)
|
|
|
|
.filter(isNotNil)
|
|
|
|
.slice(0, 5);
|
|
|
|
log.info(
|
|
|
|
`${logId}: Found ${mostRecentNonExpiringMessages.length} most recent nondisappearing messages`
|
|
|
|
);
|
|
|
|
}
|
2024-05-28 15:56:00 +00:00
|
|
|
|
|
|
|
if (mostRecentMessages.length > 0) {
|
|
|
|
await singleProtoJobQueue.add(
|
|
|
|
MessageSender.getDeleteForMeSyncMessage([
|
|
|
|
{
|
|
|
|
type: 'delete-conversation',
|
|
|
|
conversation: getConversationToDelete(this.attributes),
|
|
|
|
isFullDelete: true,
|
|
|
|
mostRecentMessages,
|
2024-06-24 17:49:36 +00:00
|
|
|
mostRecentNonExpiringMessages,
|
2024-05-28 15:56:00 +00:00
|
|
|
timestamp,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await singleProtoJobQueue.add(
|
|
|
|
MessageSender.getDeleteForMeSyncMessage([
|
|
|
|
{
|
|
|
|
type: 'delete-local-conversation',
|
|
|
|
conversation: getConversationToDelete(this.attributes),
|
|
|
|
timestamp,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`${logId}: Sync message queue complete`);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`${logId}: Starting delete`);
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.removeMessagesInConversation(this.id, {
|
2024-06-17 19:24:39 +00:00
|
|
|
fromSync: source !== 'local-delete-sync',
|
2021-01-13 00:42:15 +00:00
|
|
|
logId: this.idForLogging(),
|
2024-06-17 19:24:39 +00:00
|
|
|
singleProtoJobQueue,
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2024-05-28 15:56:00 +00:00
|
|
|
log.info(`${logId}: Delete complete`);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 20:18:11 +00:00
|
|
|
getTitle(options?: { isShort?: boolean }): string {
|
2022-12-05 22:46:54 +00:00
|
|
|
return getTitle(this.attributes, options);
|
2022-09-26 20:18:11 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
getTitleNoDefault(options?: { isShort?: boolean }): string | undefined {
|
|
|
|
return getTitleNoDefault(this.attributes, options);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:07:53 +00:00
|
|
|
getProfileName(): string | undefined {
|
2022-12-05 22:46:54 +00:00
|
|
|
return getProfileName(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 17:46:59 +00:00
|
|
|
getNumber(): string | undefined {
|
2022-12-05 22:46:54 +00:00
|
|
|
return getNumber(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 16:15:17 +00:00
|
|
|
getColor(): AvatarColorType {
|
2024-03-15 14:20:33 +00:00
|
|
|
return migrateColor(this.getServiceId(), this.get('color'));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 00:15:12 +00:00
|
|
|
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 {
|
2021-08-25 00:15:12 +00:00
|
|
|
customColor: this.get('customColor'),
|
|
|
|
customColorId: this.get('customColorId'),
|
2021-06-02 21:05:09 +00:00
|
|
|
};
|
2021-05-28 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 19:40:25 +00:00
|
|
|
unblurAvatar(): void {
|
2024-07-11 19:44:09 +00:00
|
|
|
const avatarUrl = getRawAvatarPath(this.attributes);
|
|
|
|
if (avatarUrl) {
|
|
|
|
this.set('unblurredAvatarUrl', avatarUrl);
|
2021-04-30 19:40:25 +00:00
|
|
|
} else {
|
2024-07-11 19:44:09 +00:00
|
|
|
this.unset('unblurredAvatarUrl');
|
2021-04-30 19:40:25 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:36:05 +00:00
|
|
|
areWeAdmin(): boolean {
|
2023-06-02 17:54:36 +00:00
|
|
|
return areWeAdmin(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set of items to captureChanges on:
|
2023-08-16 20:54:39 +00:00
|
|
|
// [-] serviceId
|
2020-09-24 20:57:54 +00:00
|
|
|
// [-] e164
|
|
|
|
// [X] profileKey
|
|
|
|
// [-] identityKey
|
|
|
|
// [X] verified!
|
|
|
|
// [-] profileName
|
|
|
|
// [-] profileFamilyName
|
2024-03-26 19:48:33 +00:00
|
|
|
// [X] nicknameAndNote
|
2020-09-24 20:57:54 +00:00
|
|
|
// [X] blocked
|
|
|
|
// [X] whitelisted
|
|
|
|
// [X] archived
|
2020-10-28 22:54:32 +00:00
|
|
|
// [X] markedUnread
|
2021-08-05 12:35:33 +00:00
|
|
|
// [X] dontNotifyForMentionsIfMuted
|
2022-09-19 18:47:49 +00:00
|
|
|
// [x] firstUnregisteredAt
|
2021-03-04 23:08:44 +00:00
|
|
|
captureChange(logMessage: string): void {
|
2022-11-09 02:38:19 +00:00
|
|
|
if (isSignalConversation(this.attributes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService[captureChange]', logMessage, this.idForLogging());
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({ needsStorageServiceSync: true });
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.queueJob('captureChange', async () => {
|
2022-10-08 00:19:02 +00:00
|
|
|
storageServiceUploadJob();
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
startMuteTimer({ viaStorageServiceSync = false } = {}): void {
|
2022-02-25 18:37:15 +00:00
|
|
|
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 {
|
2022-10-21 16:57:07 +00:00
|
|
|
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');
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.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');
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2021-04-09 16:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
isMuted(): boolean {
|
2022-05-23 18:37:53 +00:00
|
|
|
return isConversationMuted(this.attributes);
|
2020-10-01 00:43:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 16:38:20 +00:00
|
|
|
async notify(
|
|
|
|
message: Readonly<MessageModel>,
|
2023-08-21 20:08:27 +00:00
|
|
|
reaction?: Readonly<ReactionAttributesType>
|
2021-05-03 16:38:20 +00:00
|
|
|
): Promise<void> {
|
2021-04-09 15:52:31 +00:00
|
|
|
// As a performance optimization don't perform any work if notifications are
|
|
|
|
// disabled.
|
2021-09-23 18:16:09 +00:00
|
|
|
if (!notificationService.isEnabled) {
|
2021-04-09 15:52:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
if (this.isMuted()) {
|
2021-08-05 12:35:33 +00:00
|
|
|
if (this.get('dontNotifyForMentionsIfMuted')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
|
|
|
const ourPni = window.textsecure.storage.user.getCheckedPni();
|
2023-08-16 20:54:39 +00:00
|
|
|
const ourServiceIds: Set<ServiceIdString> = new Set([ourAci, ourPni]);
|
2023-05-26 17:14:32 +00:00
|
|
|
|
|
|
|
const mentionsMe = (message.get('bodyRanges') || []).some(bodyRange => {
|
|
|
|
if (!BodyRange.isMention(bodyRange)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-16 20:54:39 +00:00
|
|
|
return ourServiceIds.has(
|
|
|
|
normalizeServiceId(bodyRange.mentionAci, 'notify: mentionsMe check')
|
2023-05-26 17:14:32 +00:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 12:35:33 +00:00
|
|
|
if (!mentionsMe) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
if (!isIncoming(message.attributes) && !reaction) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversationId = this.id;
|
2023-01-11 22:54:06 +00:00
|
|
|
const isMessageInDirectConversation = isDirectConversation(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
const sender = reaction
|
2023-08-21 20:08:27 +00:00
|
|
|
? window.ConversationController.get(reaction.fromId)
|
2024-04-29 21:20:20 +00:00
|
|
|
: getAuthor(message.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
const senderName = sender
|
|
|
|
? sender.getTitle()
|
2023-03-30 00:03:25 +00:00
|
|
|
: window.i18n('icu:unknownContact');
|
2023-01-11 22:54:06 +00:00
|
|
|
const senderTitle = isMessageInDirectConversation
|
2020-09-24 20:57:54 +00:00
|
|
|
? senderName
|
2023-03-30 00:03:25 +00:00
|
|
|
: window.i18n('icu:notificationSenderInGroup', {
|
2020-09-24 20:57:54 +00:00
|
|
|
sender: senderName,
|
|
|
|
group: this.getTitle(),
|
|
|
|
});
|
|
|
|
|
2023-08-01 16:06:29 +00:00
|
|
|
const { url, absolutePath } = await this.getAvatarOrIdenticon();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
const messageJSON = message.toJSON();
|
|
|
|
const messageId = message.id;
|
|
|
|
const isExpiringMessage = Message.hasExpiration(messageJSON);
|
|
|
|
|
2021-09-23 18:16:09 +00:00
|
|
|
notificationService.add({
|
2020-09-24 20:57:54 +00:00
|
|
|
senderTitle,
|
|
|
|
conversationId,
|
2023-01-11 22:54:06 +00:00
|
|
|
storyId: isMessageInDirectConversation
|
|
|
|
? undefined
|
|
|
|
: message.get('storyId'),
|
2023-08-01 16:06:29 +00:00
|
|
|
notificationIconUrl: url,
|
|
|
|
notificationIconAbsolutePath: absolutePath,
|
2020-09-24 20:57:54 +00:00
|
|
|
isExpiringMessage,
|
|
|
|
message: message.getNotificationText(),
|
|
|
|
messageId,
|
2023-08-21 20:08:27 +00:00
|
|
|
reaction: reaction
|
|
|
|
? {
|
|
|
|
emoji: reaction.emoji,
|
|
|
|
targetAuthorAci: reaction.targetAuthorAci,
|
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
|
|
|
}
|
|
|
|
: undefined,
|
2023-06-14 20:55:50 +00:00
|
|
|
sentAt: message.get('timestamp'),
|
2023-08-01 16:06:29 +00:00
|
|
|
type: reaction ? NotificationType.Reaction : NotificationType.Message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAvatarOrIdenticon(): Promise<{
|
|
|
|
url: string;
|
|
|
|
absolutePath?: string;
|
|
|
|
}> {
|
2024-07-11 19:44:09 +00:00
|
|
|
let saveToDisk: boolean;
|
|
|
|
|
|
|
|
const notificationSetting = notificationService.getNotificationSetting();
|
|
|
|
switch (notificationSetting) {
|
|
|
|
case NotificationSetting.NameOnly:
|
|
|
|
case NotificationSetting.NameAndMessage:
|
|
|
|
// According to the MSDN, avatars can only be loaded from disk or an
|
|
|
|
// http server:
|
|
|
|
// https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-image?redirectedfrom=MSDN
|
|
|
|
saveToDisk = OS.isWindows();
|
|
|
|
break;
|
|
|
|
case NotificationSetting.Off:
|
|
|
|
case NotificationSetting.NoNameOrMessage:
|
|
|
|
saveToDisk = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw missingCaseError(notificationSetting);
|
|
|
|
}
|
|
|
|
const avatarUrl = getLocalAvatarUrl(this.attributes);
|
|
|
|
if (avatarUrl) {
|
2023-08-01 16:06:29 +00:00
|
|
|
return {
|
2024-07-11 19:44:09 +00:00
|
|
|
url: avatarUrl,
|
|
|
|
absolutePath: saveToDisk
|
|
|
|
? await this.getTemporaryAvatarPath()
|
|
|
|
: undefined,
|
2023-08-01 16:06:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const { url, path } = await this.getIdenticon({
|
2024-07-11 19:44:09 +00:00
|
|
|
saveToDisk,
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2023-08-01 16:06:29 +00:00
|
|
|
return {
|
|
|
|
url,
|
|
|
|
absolutePath: path ? getAbsoluteTempPath(path) : undefined,
|
|
|
|
};
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 19:44:09 +00:00
|
|
|
private async getTemporaryAvatarPath(): Promise<string | undefined> {
|
|
|
|
const avatar = getAvatar(this.attributes);
|
|
|
|
if (avatar?.path == null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const avatarPath = getRawAvatarPath(this.attributes);
|
|
|
|
if (!avatarPath) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already plaintext
|
|
|
|
if (avatar.version !== 2) {
|
|
|
|
return avatarPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!avatar.localKey || !avatar.size) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { path: plaintextPath } = await decryptAttachmentV2({
|
|
|
|
ciphertextPath: avatarPath,
|
|
|
|
idForLogging: 'getAvatarOrIdenticon',
|
|
|
|
keysBase64: avatar.localKey,
|
|
|
|
size: avatar.size,
|
|
|
|
|
|
|
|
getAbsoluteAttachmentPath,
|
2024-07-16 20:39:56 +00:00
|
|
|
type: 'local',
|
2024-07-11 19:44:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { path: tempPath } = await copyIntoTempDirectory(
|
|
|
|
getAbsoluteAttachmentPath(plaintextPath)
|
|
|
|
);
|
|
|
|
return getAbsoluteTempPath(tempPath);
|
|
|
|
} finally {
|
|
|
|
await deleteAttachmentData(plaintextPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 16:06:29 +00:00
|
|
|
private async getIdenticon({
|
|
|
|
saveToDisk,
|
|
|
|
}: { saveToDisk?: boolean } = {}): Promise<{
|
|
|
|
url: string;
|
|
|
|
path?: string;
|
|
|
|
}> {
|
|
|
|
const isContact = isDirectConversation(this.attributes);
|
2021-03-24 13:59:56 +00:00
|
|
|
const color = this.getColor();
|
2021-10-17 21:46:54 +00:00
|
|
|
const title = this.getTitle();
|
2021-03-24 13:59:56 +00:00
|
|
|
|
2023-08-01 16:06:29 +00:00
|
|
|
if (isContact) {
|
|
|
|
const text = (title && getInitials(title)) || '#';
|
2021-03-24 13:59:56 +00:00
|
|
|
|
2023-08-01 16:06:29 +00:00
|
|
|
const cached = this.cachedIdenticon;
|
|
|
|
if (cached && cached.text === text && cached.color === color) {
|
|
|
|
return { ...cached };
|
|
|
|
}
|
|
|
|
|
|
|
|
const { url, path } = await createIdenticon(
|
|
|
|
color,
|
|
|
|
{
|
|
|
|
type: 'contact',
|
|
|
|
text,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
saveToDisk,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.cachedIdenticon = { text, color, url, path };
|
|
|
|
return { url, path };
|
2021-03-24 13:59:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 16:06:29 +00:00
|
|
|
const cached = this.cachedIdenticon;
|
|
|
|
if (cached && cached.color === color) {
|
|
|
|
return { ...cached };
|
|
|
|
}
|
2021-03-24 13:59:56 +00:00
|
|
|
|
2023-08-01 16:06:29 +00:00
|
|
|
const { url, path } = await createIdenticon(
|
|
|
|
color,
|
|
|
|
{ type: 'group' },
|
|
|
|
{
|
|
|
|
saveToDisk,
|
|
|
|
}
|
|
|
|
);
|
2021-03-24 13:59:56 +00:00
|
|
|
|
2023-08-01 16:06:29 +00:00
|
|
|
this.cachedIdenticon = { color, url, path };
|
|
|
|
return { url, path };
|
2021-03-24 13:59:56 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// We don't do anything with typing messages from our other devices
|
2020-11-20 17:30:45 +00:00
|
|
|
if (fromMe) {
|
2020-09-24 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
const sender = window.ConversationController.get(senderId);
|
|
|
|
if (!sender) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const senderServiceId = sender.getServiceId();
|
|
|
|
if (!senderServiceId) {
|
2022-07-08 20:46:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:18:35 +00:00
|
|
|
// Drop typing indicators for announcement only groups where the sender
|
|
|
|
// is not an admin
|
2023-08-10 16:43:33 +00:00
|
|
|
if (this.get('announcementsOnly') && !this.isAdmin(senderServiceId)) {
|
2021-07-20 20:18:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-20 18:03:21 +00:00
|
|
|
const typingToken = `${sender.id}.${senderDevice}`;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
);
|
2023-09-18 21:17:26 +00:00
|
|
|
// User was not previously typing before. State change!
|
2020-09-24 20:57:54 +00:00
|
|
|
if (!record) {
|
2021-09-20 18:51:30 +00:00
|
|
|
this.trigger('change', this, { force: true });
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete this.contactTypingTimers[typingToken];
|
|
|
|
if (record) {
|
|
|
|
// User was previously typing, and is no longer. State change!
|
2021-09-20 18:51:30 +00:00
|
|
|
this.trigger('change', this, { force: true });
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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!
|
2021-09-20 18:51:30 +00:00
|
|
|
this.trigger('change', this, { force: true });
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 18:30:43 +00:00
|
|
|
pin(): void {
|
2021-03-31 00:16:28 +00:00
|
|
|
if (this.get('isPinned')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:26:23 +00:00
|
|
|
const validationError = this.validate();
|
|
|
|
if (validationError) {
|
|
|
|
log.error(
|
|
|
|
`not pinning ${this.idForLogging()} because of ` +
|
|
|
|
`validation error ${validationError}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('pinning', this.idForLogging());
|
2020-10-02 18:30:43 +00:00
|
|
|
const pinnedConversationIds = new Set(
|
2021-06-15 00:09:37 +00:00
|
|
|
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')) {
|
2021-03-31 00:16:28 +00:00
|
|
|
this.set({ isArchived: false });
|
2020-10-02 18:30:43 +00:00
|
|
|
}
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2020-10-02 18:30:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 01:21:02 +00:00
|
|
|
unpin(): void {
|
2021-03-31 00:16:28 +00:00
|
|
|
if (!this.get('isPinned')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
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(
|
2021-06-15 00:09:37 +00:00
|
|
|
window.storage.get('pinnedConversationIds', new Array<string>())
|
2020-10-02 18:30:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
pinnedConversationIds.delete(this.id);
|
|
|
|
|
2022-02-03 01:21:02 +00:00
|
|
|
this.writePinnedConversations([...pinnedConversationIds]);
|
2020-10-10 14:25:17 +00:00
|
|
|
|
|
|
|
this.set('isPinned', false);
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2020-10-02 18:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writePinnedConversations(pinnedConversationIds: Array<string>): void {
|
2022-12-21 18:41:48 +00:00
|
|
|
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 });
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2021-08-05 12:35:33 +00:00
|
|
|
this.captureChange('dontNotifyForMentionsIfMuted');
|
|
|
|
}
|
|
|
|
|
2021-06-01 23:30:25 +00:00
|
|
|
acknowledgeGroupMemberNameCollisions(
|
2023-01-13 20:07:26 +00:00
|
|
|
groupNameCollisions: ReadonlyDeep<GroupNameCollisionsWithIdsByTitle>
|
2021-06-01 23:30:25 +00:00
|
|
|
): void {
|
|
|
|
this.set('acknowledgedGroupNameCollisions', groupNameCollisions);
|
2024-07-22 18:16:33 +00:00
|
|
|
drop(DataWriter.updateConversation(this.attributes));
|
2021-06-01 23:30:25 +00:00
|
|
|
}
|
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`);
|
2023-01-19 00:02:03 +00:00
|
|
|
window.SignalCI?.handleEvent('conversation:open', { delta });
|
2022-01-20 00:40:29 +00:00
|
|
|
}
|
2022-04-12 18:16:58 +00:00
|
|
|
|
|
|
|
async flushDebouncedUpdates(): Promise<void> {
|
|
|
|
try {
|
2023-08-22 21:01:36 +00:00
|
|
|
this.debouncedUpdateLastMessage.flush();
|
2022-04-12 18:16:58 +00:00
|
|
|
} 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();
|
|
|
|
}
|
2022-10-08 00:19:02 +00:00
|
|
|
|
2022-11-08 20:01:59 +00:00
|
|
|
/** @return only undefined if not a group */
|
2022-10-08 00:19:02 +00:00
|
|
|
getStorySendMode(): StorySendMode | undefined {
|
2022-11-19 08:31:18 +00:00
|
|
|
// isDirectConversation is used instead of isGroup because this is what
|
|
|
|
// used in `format()` when sending conversation "type" to redux.
|
|
|
|
if (isDirectConversation(this.attributes)) {
|
2022-10-08 00:19:02 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2022-11-19 08:31:18 +00:00
|
|
|
return this.getGroupStorySendMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
private getGroupStorySendMode(): StorySendMode {
|
|
|
|
strictAssert(
|
|
|
|
!isDirectConversation(this.attributes),
|
|
|
|
'Must be a group to have send story mode'
|
|
|
|
);
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
return this.get('storySendMode') ?? StorySendMode.IfActive;
|
|
|
|
}
|
2023-02-24 19:03:17 +00:00
|
|
|
|
|
|
|
async shutdownJobQueue(): Promise<void> {
|
|
|
|
log.info(`conversation ${this.idForLogging()} jobQueue shutdown start`);
|
|
|
|
|
|
|
|
if (!this.jobQueue) {
|
|
|
|
log.info(`conversation ${this.idForLogging()} no jobQueue to shutdown`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the queue takes more than 10 seconds to get to idle, we force it by setting
|
|
|
|
// isShuttingDown = true which will reject incoming requests.
|
|
|
|
const to = setTimeout(() => {
|
|
|
|
log.warn(
|
|
|
|
`conversation ${this.idForLogging()} jobQueue stop accepting new work`
|
|
|
|
);
|
|
|
|
this.isShuttingDown = true;
|
|
|
|
}, 10 * SECOND);
|
|
|
|
|
|
|
|
await this.jobQueue.onIdle();
|
|
|
|
this.isShuttingDown = true;
|
|
|
|
clearTimeout(to);
|
|
|
|
|
|
|
|
log.info(`conversation ${this.idForLogging()} jobQueue shutdown complete`);
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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`,
|
2023-08-16 20:54:39 +00:00
|
|
|
* `_byServiceId`, and `_byGroupId` fields so we can track conversations by more
|
2020-09-24 20:57:54 +00:00
|
|
|
* than just their id.
|
|
|
|
*/
|
|
|
|
initialize() {
|
|
|
|
this.eraseLookups();
|
|
|
|
this.on(
|
|
|
|
'idUpdated',
|
2021-09-20 18:51:30 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(model: ConversationModel, idProp: string, oldValue: any) => {
|
2020-09-24 20:57:54 +00:00
|
|
|
if (oldValue) {
|
|
|
|
if (idProp === 'e164') {
|
|
|
|
delete this._byE164[oldValue];
|
|
|
|
}
|
2023-08-16 20:54:39 +00:00
|
|
|
if (idProp === 'serviceId') {
|
|
|
|
delete this._byServiceId[oldValue];
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2022-08-09 21:39:00 +00:00
|
|
|
if (idProp === 'pni') {
|
|
|
|
delete this._byPni[oldValue];
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
if (idProp === 'groupId') {
|
2020-11-20 19:50:45 +00:00
|
|
|
delete this._byGroupId[oldValue];
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-20 18:51:30 +00:00
|
|
|
const e164 = model.get('e164');
|
|
|
|
if (e164) {
|
|
|
|
this._byE164[e164] = model;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = model.getServiceId();
|
|
|
|
if (serviceId) {
|
|
|
|
this._byServiceId[serviceId] = model;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2023-08-16 20:54:39 +00:00
|
|
|
const pni = model.getPni();
|
2022-08-09 21:39:00 +00:00
|
|
|
if (pni) {
|
|
|
|
this._byPni[pni] = model;
|
|
|
|
}
|
2021-09-20 18:51:30 +00:00
|
|
|
const groupId = model.get('groupId');
|
|
|
|
if (groupId) {
|
|
|
|
this._byGroupId[groupId] = model;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2022-07-13 00:37:21 +00:00
|
|
|
reset(models?: Array<ConversationModel>, options?: Backbone.Silenceable) {
|
|
|
|
window.Backbone.Collection.prototype.reset.call(this, models, options);
|
2020-09-24 20:57:54 +00:00
|
|
|
this.resetLookups();
|
|
|
|
},
|
|
|
|
|
|
|
|
resetLookups() {
|
|
|
|
this.eraseLookups();
|
|
|
|
this.generateLookups(this.models);
|
|
|
|
},
|
|
|
|
|
2021-05-03 16:38:20 +00:00
|
|
|
generateLookups(models: ReadonlyArray<ConversationModel>) {
|
2020-09-24 20:57:54 +00:00
|
|
|
models.forEach(model => {
|
|
|
|
const e164 = model.get('e164');
|
|
|
|
if (e164) {
|
|
|
|
const existing = this._byE164[e164];
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// Prefer the contact with both e164 and serviceId
|
|
|
|
if (!existing || (existing && !existing.getServiceId())) {
|
2020-09-24 20:57:54 +00:00
|
|
|
this._byE164[e164] = model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = model.getServiceId();
|
|
|
|
if (serviceId) {
|
|
|
|
const existing = this._byServiceId[serviceId];
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// Prefer the contact with both e164 and seviceId
|
2020-09-24 20:57:54 +00:00
|
|
|
if (!existing || (existing && !existing.get('e164'))) {
|
2023-08-16 20:54:39 +00:00
|
|
|
this._byServiceId[serviceId] = model;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const pni = model.getPni();
|
2022-08-09 21:39:00 +00:00
|
|
|
if (pni) {
|
|
|
|
const existing = this._byPni[pni];
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
// Prefer the contact with both serviceId and pni
|
|
|
|
if (!existing || (existing && !existing.getServiceId())) {
|
2022-08-09 21:39:00 +00:00
|
|
|
this._byPni[pni] = model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
const groupId = model.get('groupId');
|
|
|
|
if (groupId) {
|
|
|
|
this._byGroupId[groupId] = model;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
eraseLookups() {
|
|
|
|
this._byE164 = Object.create(null);
|
2023-08-16 20:54:39 +00:00
|
|
|
this._byServiceId = Object.create(null);
|
2022-08-09 21:39:00 +00:00
|
|
|
this._byPni = Object.create(null);
|
2020-09-24 20:57:54 +00:00
|
|
|
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;
|
2020-10-28 21:54:33 +00:00
|
|
|
|
|
|
|
// 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);
|
2020-10-28 21:54:33 +00:00
|
|
|
} else {
|
2022-07-13 00:37:21 +00:00
|
|
|
hydratedData.push(
|
|
|
|
new window.Whisper.Conversation(item as ConversationAttributesType)
|
|
|
|
);
|
2020-10-28 21:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-13 00:37:21 +00:00
|
|
|
} else if (has(data, 'get')) {
|
|
|
|
hydratedData = data as ConversationModel;
|
2020-10-28 21:54:33 +00:00
|
|
|
} else {
|
2022-07-13 00:37:21 +00:00
|
|
|
hydratedData = new window.Whisper.Conversation(
|
|
|
|
data as ConversationAttributesType
|
|
|
|
);
|
2020-10-28 21:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we update our lookups first to prevent infinite loops on the 'add' event
|
|
|
|
this.generateLookups(
|
|
|
|
Array.isArray(hydratedData) ? hydratedData : [hydratedData]
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
|
2020-10-28 21:54:33 +00:00
|
|
|
// 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);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2020-10-28 21:54:33 +00:00
|
|
|
return hydratedData;
|
2020-09-24 20:57:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* window.Backbone collections have a `_byId` field that `get` defers to. Here, we
|
2023-08-16 20:54:39 +00:00
|
|
|
* override `get` to first access our custom `_byE164`, `_byServiceId`, and
|
2020-09-24 20:57:54 +00:00
|
|
|
* `_byGroupId` functions, followed by falling back to the original
|
|
|
|
* window.Backbone implementation.
|
|
|
|
*/
|
|
|
|
get(id: string) {
|
|
|
|
return (
|
|
|
|
this._byE164[id] ||
|
|
|
|
this._byE164[`+${id}`] ||
|
2023-08-16 20:54:39 +00:00
|
|
|
this._byServiceId[id] ||
|
2022-08-09 21:39:00 +00:00
|
|
|
this._byPni[id] ||
|
2020-09-24 20:57:54 +00:00
|
|
|
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);
|
2020-09-24 20:57:54 +00:00
|
|
|
},
|
|
|
|
});
|