signal-desktop/ts/services/backups/export.ts

2874 lines
88 KiB
TypeScript
Raw Normal View History

2024-03-15 14:20:33 +00:00
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import Long from 'long';
2024-04-30 13:24:21 +00:00
import { Aci, Pni, ServiceId } from '@signalapp/libsignal-client';
import type { BackupLevel } from '@signalapp/libsignal-client/zkgroup';
2024-03-15 14:20:33 +00:00
import pMap from 'p-map';
import pTimeout from 'p-timeout';
import { Readable } from 'stream';
2024-08-13 18:39:04 +00:00
import { isNumber } from 'lodash';
import { CallLinkRootKey } from '@signalapp/ringrtc';
2024-03-15 14:20:33 +00:00
2024-04-30 13:24:21 +00:00
import { Backups, SignalService } from '../../protobuf';
2024-07-22 18:16:33 +00:00
import {
DataReader,
DataWriter,
pauseWriteAccess,
resumeWriteAccess,
} from '../../sql/Client';
2024-03-15 14:20:33 +00:00
import type { PageMessagesCursorType } from '../../sql/Interface';
import * as log from '../../logging/log';
import { GiftBadgeStates } from '../../components/conversation/Message';
import { type CustomColorType } from '../../types/Colors';
2024-03-15 14:20:33 +00:00
import { StorySendMode, MY_STORY_ID } from '../../types/Stories';
import { getStickerPacksForBackup } from '../../types/Stickers';
2024-04-30 13:24:21 +00:00
import {
isPniString,
type AciString,
type ServiceIdString,
} from '../../types/ServiceId';
2024-03-15 14:20:33 +00:00
import type { RawBodyRange } from '../../types/BodyRange';
import { LONG_ATTACHMENT_LIMIT } from '../../types/Message';
2024-05-22 16:34:19 +00:00
import { PaymentEventKind } from '../../types/Payment';
2024-07-31 13:43:26 +00:00
import { MessageRequestResponseEvent } from '../../types/MessageRequestResponseEvent';
2024-03-15 14:20:33 +00:00
import type {
ConversationAttributesType,
MessageAttributesType,
2024-05-23 21:06:41 +00:00
QuotedAttachmentType,
2024-03-15 14:20:33 +00:00
QuotedMessageType,
} from '../../model-types.d';
import { drop } from '../../util/drop';
import { isNotNil } from '../../util/isNotNil';
2024-03-15 14:20:33 +00:00
import { explodePromise } from '../../util/explodePromise';
import {
isDirectConversation,
2024-04-30 13:24:21 +00:00
isGroup,
isGroupV1,
2024-03-15 14:20:33 +00:00
isGroupV2,
isMe,
} from '../../util/whatTypeOfConversation';
import { uuidToBytes } from '../../util/uuidToBytes';
2024-09-12 23:48:27 +00:00
import { strictAssert } from '../../util/assert';
2024-03-15 14:20:33 +00:00
import { getSafeLongFromTimestamp } from '../../util/timestampLongUtils';
2024-08-06 15:28:27 +00:00
import { DAY, MINUTE, SECOND, DurationInSeconds } from '../../util/durations';
2024-03-15 14:20:33 +00:00
import {
PhoneNumberDiscoverability,
parsePhoneNumberDiscoverability,
} from '../../util/phoneNumberDiscoverability';
import {
PhoneNumberSharingMode,
parsePhoneNumberSharingMode,
} from '../../util/phoneNumberSharingMode';
import { missingCaseError } from '../../util/missingCaseError';
2024-04-30 13:24:21 +00:00
import {
isCallHistory,
isChatSessionRefreshed,
isContactRemovedNotification,
isConversationMerge,
isDeliveryIssue,
isEndSession,
isExpirationTimerUpdate,
isGiftBadge,
isGroupUpdate,
isGroupV1Migration,
isGroupV2Change,
isKeyChange,
isNormalBubble,
isPhoneNumberDiscovery,
isProfileChange,
2024-10-29 18:16:09 +00:00
isTapToView,
2024-04-30 13:24:21 +00:00
isUniversalTimerNotification,
isUnsupportedMessage,
isVerifiedChange,
2024-05-22 16:34:19 +00:00
isChangeNumberNotification,
isJoinedSignalNotification,
isTitleTransitionNotification,
2024-07-31 13:43:26 +00:00
isMessageRequestResponse,
2024-04-30 13:24:21 +00:00
} from '../../state/selectors/message';
2024-03-15 14:20:33 +00:00
import * as Bytes from '../../Bytes';
import { canBeSynced as canPreferredReactionEmojiBeSynced } from '../../reactions/preferredReactionEmoji';
import { SendStatus } from '../../messages/MessageSendState';
import { BACKUP_VERSION } from './constants';
2024-09-12 23:48:27 +00:00
import {
getMessageIdForLogging,
getConversationIdForLogging,
} from '../../util/idForLogging';
2024-04-30 13:24:21 +00:00
import { makeLookup } from '../../util/makeLookup';
2024-08-13 18:39:04 +00:00
import type {
CallHistoryDetails,
CallStatus,
} from '../../types/CallDisposition';
import {
CallMode,
CallDirection,
CallType,
DirectCallStatus,
GroupCallStatus,
AdhocCallStatus,
} from '../../types/CallDisposition';
2024-04-30 13:24:21 +00:00
import { isAciString } from '../../util/isAciString';
2024-07-15 20:58:55 +00:00
import { hslToRGB } from '../../util/hslToRGB';
import type { AboutMe, LocalChatStyle } from './types';
2024-09-21 03:10:28 +00:00
import { BackupType } from './types';
2024-04-30 13:24:21 +00:00
import { messageHasPaymentEvent } from '../../messages/helpers';
import {
numberToAddressType,
numberToPhoneType,
} from '../../types/EmbeddedContact';
import {
type AttachmentType,
isGIF,
isDownloaded,
} from '../../types/Attachment';
2024-05-29 23:46:43 +00:00
import {
getFilePointerForAttachment,
maybeGetBackupJobForAttachmentAndFilePointer,
} from './util/filePointers';
2024-10-31 17:01:03 +00:00
import { getBackupMediaRootKey } from './crypto';
2024-05-29 23:46:43 +00:00
import type { CoreAttachmentBackupJobType } from '../../types/AttachmentBackup';
import { AttachmentBackupManager } from '../../jobs/AttachmentBackupManager';
import { getBackupCdnInfo } from './util/mediaId';
2024-08-06 15:28:27 +00:00
import { calculateExpirationTimestamp } from '../../util/expirationTimer';
import { ReadStatus } from '../../messages/MessageReadStatus';
2024-08-13 18:39:04 +00:00
import { CallLinkRestrictions } from '../../types/CallLink';
import { toAdminKeyBytes } from '../../util/callLinks';
import { getRoomIdFromRootKey } from '../../util/callLinksRingrtc';
import { SeenStatus } from '../../MessageSeenStatus';
2024-09-23 19:24:41 +00:00
import { migrateBatchOfMessages } from '../../messages/migrateMessageData';
2024-03-15 14:20:33 +00:00
const MAX_CONCURRENCY = 10;
// We want a very generous timeout to make sure that we always resume write
// access to the database.
const FLUSH_TIMEOUT = 30 * MINUTE;
// Threshold for reporting slow flushes
const REPORTING_THRESHOLD = SECOND;
type GetRecipientIdOptionsType =
| Readonly<{
serviceId: ServiceIdString;
id?: string;
e164?: string;
}>
| Readonly<{
serviceId?: ServiceIdString;
id: string;
e164?: string;
}>
| Readonly<{
serviceId?: ServiceIdString;
id?: string;
e164: string;
}>;
2024-05-22 16:34:19 +00:00
type ToChatItemOptionsType = Readonly<{
aboutMe: AboutMe;
callHistoryByCallId: Record<string, CallHistoryDetails>;
backupLevel: BackupLevel;
}>;
type NonBubbleOptionsType = Pick<
ToChatItemOptionsType,
'aboutMe' | 'callHistoryByCallId'
> &
Readonly<{
authorId: Long | undefined;
message: MessageAttributesType;
}>;
enum NonBubbleResultKind {
Directed = 'Directed',
Directionless = 'Directionless',
Drop = 'Drop',
}
type NonBubbleResultType = Readonly<
| {
kind: NonBubbleResultKind.Drop;
patch?: undefined;
}
| {
kind: NonBubbleResultKind.Directed | NonBubbleResultKind.Directionless;
patch: Backups.IChatItem;
}
>;
2024-03-15 14:20:33 +00:00
export class BackupExportStream extends Readable {
2024-08-06 15:28:27 +00:00
// Shared between all methods for consistency.
private now = Date.now();
private readonly backupTimeMs = getSafeLongFromTimestamp(this.now);
2024-03-15 14:20:33 +00:00
private readonly convoIdToRecipientId = new Map<string, number>();
2024-09-12 23:48:27 +00:00
private readonly serviceIdToRecipientId = new Map<string, number>();
private readonly e164ToRecipientId = new Map<string, number>();
2024-08-13 18:39:04 +00:00
private readonly roomIdToRecipientId = new Map<string, number>();
2024-05-29 23:46:43 +00:00
private attachmentBackupJobs: Array<CoreAttachmentBackupJobType> = [];
2024-03-15 14:20:33 +00:00
private buffers = new Array<Uint8Array>();
private nextRecipientId = 0;
private flushResolve: (() => void) | undefined;
2024-07-15 20:58:55 +00:00
// Map from custom color uuid to an index in accountSettings.customColors
// array.
2024-07-31 13:43:26 +00:00
private customColorIdByUuid = new Map<string, Long>();
2024-07-15 20:58:55 +00:00
2024-09-21 03:10:28 +00:00
constructor(private readonly backupType: BackupType) {
super();
}
public run(backupLevel: BackupLevel): void {
2024-03-15 14:20:33 +00:00
drop(
(async () => {
log.info('BackupExportStream: starting...');
drop(AttachmentBackupManager.stop());
2024-09-23 19:24:41 +00:00
log.info('BackupExportStream: message migration starting...');
let batchMigrationResult:
| Awaited<ReturnType<typeof migrateBatchOfMessages>>
| undefined;
let totalMigrated = 0;
while (!batchMigrationResult?.done) {
// eslint-disable-next-line no-await-in-loop
batchMigrationResult = await migrateBatchOfMessages({
numMessagesPerBatch: 1000,
});
totalMigrated += batchMigrationResult.numProcessed;
log.info(
`BackupExportStream: Migrated batch of ${batchMigrationResult.numProcessed}`
);
}
log.info(
`BackupExportStream: message migration complete; ${totalMigrated} messages migrated`
);
2024-07-22 18:16:33 +00:00
await pauseWriteAccess();
2024-03-15 14:20:33 +00:00
try {
await this.unsafeRun(backupLevel);
2024-03-15 14:20:33 +00:00
} catch (error) {
this.emit('error', error);
} finally {
2024-07-22 18:16:33 +00:00
await resumeWriteAccess();
// TODO (DESKTOP-7344): Clear & add backup jobs in a single transaction
2024-07-22 18:16:33 +00:00
await DataWriter.clearAllAttachmentBackupJobs();
2024-09-21 03:10:28 +00:00
if (this.backupType !== BackupType.TestOnlyPlaintext) {
2024-09-12 23:48:27 +00:00
await Promise.all(
this.attachmentBackupJobs.map(job =>
AttachmentBackupManager.addJobAndMaybeThumbnailJob(job)
)
);
drop(AttachmentBackupManager.start());
}
2024-03-15 14:20:33 +00:00
log.info('BackupExportStream: finished');
}
})()
);
}
private async unsafeRun(backupLevel: BackupLevel): Promise<void> {
2024-03-15 14:20:33 +00:00
this.push(
Backups.BackupInfo.encodeDelimited({
version: Long.fromNumber(BACKUP_VERSION),
2024-04-18 17:02:32 +00:00
backupTimeMs: this.backupTimeMs,
2024-10-31 17:01:03 +00:00
mediaRootBackupKey: getBackupMediaRootKey().serialize(),
2024-03-15 14:20:33 +00:00
}).finish()
);
this.pushFrame({
account: await this.toAccountData(),
});
await this.flush();
const stats = {
2024-08-13 18:39:04 +00:00
adHocCalls: 0,
callLinks: 0,
2024-03-15 14:20:33 +00:00
conversations: 0,
chats: 0,
distributionLists: 0,
messages: 0,
skippedMessages: 0,
2024-06-12 20:47:56 +00:00
stickerPacks: 0,
2024-03-15 14:20:33 +00:00
};
for (const { attributes } of window.ConversationController.getAll()) {
const recipientId = this.getRecipientId({
id: attributes.id,
serviceId: attributes.serviceId,
e164: attributes.e164,
});
const recipient = this.toRecipient(recipientId, attributes);
if (recipient === undefined) {
// Can't be backed up.
continue;
}
this.pushFrame({
recipient,
});
// eslint-disable-next-line no-await-in-loop
await this.flush();
stats.conversations += 1;
}
2024-09-03 17:18:15 +00:00
this.pushFrame({
recipient: {
id: Long.fromNumber(this.getNextRecipientId()),
releaseNotes: {},
},
});
await this.flush();
2024-07-22 18:16:33 +00:00
const distributionLists =
await DataReader.getAllStoryDistributionsWithMembers();
2024-03-15 14:20:33 +00:00
for (const list of distributionLists) {
const { PrivacyMode } = Backups.DistributionList;
let privacyMode: Backups.DistributionList.PrivacyMode;
if (list.id === MY_STORY_ID) {
if (list.isBlockList) {
if (!list.members.length) {
privacyMode = PrivacyMode.ALL;
} else {
privacyMode = PrivacyMode.ALL_EXCEPT;
}
} else {
privacyMode = PrivacyMode.ONLY_WITH;
}
} else {
privacyMode = PrivacyMode.ONLY_WITH;
}
this.pushFrame({
recipient: {
2024-08-13 18:39:04 +00:00
id: Long.fromNumber(this.getNextRecipientId()),
2024-03-15 14:20:33 +00:00
distributionList: {
distributionId: uuidToBytes(list.id),
deletionTimestamp: list.deletedAtTimestamp
? Long.fromNumber(list.deletedAtTimestamp)
: null,
distributionList: list.deletedAtTimestamp
? null
: {
name: list.name,
allowReplies: list.allowsReplies,
privacyMode,
memberRecipientIds: list.members.map(serviceId =>
this.getOrPushPrivateRecipient({ serviceId })
),
},
2024-03-15 14:20:33 +00:00
},
},
});
// eslint-disable-next-line no-await-in-loop
await this.flush();
stats.distributionLists += 1;
}
2024-08-13 18:39:04 +00:00
const callLinks = await DataReader.getAllCallLinks();
for (const link of callLinks) {
const {
rootKey: rootKeyString,
adminKey,
name,
restrictions,
revoked,
expiration,
} = link;
if (revoked) {
continue;
}
const id = this.getNextRecipientId();
const rootKey = CallLinkRootKey.parse(rootKeyString);
const roomId = getRoomIdFromRootKey(rootKey);
this.roomIdToRecipientId.set(roomId, id);
this.pushFrame({
recipient: {
id: Long.fromNumber(id),
callLink: {
rootKey: rootKey.bytes,
adminKey: adminKey ? toAdminKeyBytes(adminKey) : null,
name,
restrictions: toCallLinkRestrictionsProto(restrictions),
expirationMs: isNumber(expiration)
? Long.fromNumber(expiration)
: null,
},
},
});
// eslint-disable-next-line no-await-in-loop
await this.flush();
stats.callLinks += 1;
}
const stickerPacks = await getStickerPacksForBackup();
2024-06-12 20:47:56 +00:00
for (const { id, key } of stickerPacks) {
this.pushFrame({
stickerPack: {
packId: Bytes.fromHex(id),
packKey: Bytes.fromBase64(key),
},
});
// eslint-disable-next-line no-await-in-loop
await this.flush();
stats.stickerPacks += 1;
}
2024-04-15 20:54:21 +00:00
const pinnedConversationIds =
window.storage.get('pinnedConversationIds') || [];
2024-03-15 14:20:33 +00:00
for (const { attributes } of window.ConversationController.getAll()) {
if (isGroupV1(attributes)) {
log.warn('backups: skipping gv1 conversation');
continue;
}
2024-03-15 14:20:33 +00:00
const recipientId = this.getRecipientId(attributes);
2024-04-15 20:54:21 +00:00
let pinnedOrder: number | null = null;
if (attributes.isPinned) {
2024-09-12 23:48:27 +00:00
const index = pinnedConversationIds.indexOf(attributes.id);
if (index === -1) {
const convoId = getConversationIdForLogging(attributes);
log.warn(`backups: ${convoId} is pinned, but is not on the list`);
}
pinnedOrder = Math.max(1, index + 1);
}
// Skip conversations that have no presence in left pane (no chats)
if (!attributes.isPinned && !attributes.active_at) {
continue;
2024-04-15 20:54:21 +00:00
}
2024-03-15 14:20:33 +00:00
this.pushFrame({
chat: {
// We don't have to use separate identifiers
id: recipientId,
recipientId,
archived: attributes.isArchived === true,
2024-04-15 20:54:21 +00:00
pinnedOrder,
2024-03-15 14:20:33 +00:00
expirationTimerMs:
attributes.expireTimer != null
? Long.fromNumber(
DurationInSeconds.toMillis(attributes.expireTimer)
)
: null,
2024-09-10 21:03:06 +00:00
expireTimerVersion: attributes.expireTimerVersion,
2024-03-15 14:20:33 +00:00
muteUntilMs: getSafeLongFromTimestamp(attributes.muteExpiresAt),
markedUnread: attributes.markedUnread === true,
dontNotifyForMentionsIfMuted:
attributes.dontNotifyForMentionsIfMuted === true,
2024-07-15 20:58:55 +00:00
style: this.toChatStyle({
wallpaperPhotoPointer: attributes.wallpaperPhotoPointerBase64
? Bytes.fromBase64(attributes.wallpaperPhotoPointerBase64)
: undefined,
wallpaperPreset: attributes.wallpaperPreset,
color: attributes.conversationColor,
customColorId: attributes.customColorId,
2024-07-25 14:58:51 +00:00
dimWallpaperInDarkMode: attributes.dimWallpaperInDarkMode,
2024-09-12 23:48:27 +00:00
autoBubbleColor: attributes.autoBubbleColor,
2024-07-15 20:58:55 +00:00
}),
2024-03-15 14:20:33 +00:00
},
});
// eslint-disable-next-line no-await-in-loop
await this.flush();
stats.chats += 1;
}
2024-08-13 18:39:04 +00:00
const allCallHistoryItems = await DataReader.getAllCallHistory();
for (const item of allCallHistoryItems) {
const { callId, type, peerId: roomId, status, timestamp } = item;
if (type !== CallType.Adhoc) {
continue;
}
const recipientId = this.roomIdToRecipientId.get(roomId);
if (!recipientId) {
log.warn(
`backups: Dropping ad-hoc call; recipientId for roomId ${roomId.slice(-2)} not found`
);
continue;
}
this.pushFrame({
adHocCall: {
callId: Long.fromString(callId),
recipientId: Long.fromNumber(recipientId),
state: toAdHocCallStateProto(status),
callTimestamp: Long.fromNumber(timestamp),
},
});
// eslint-disable-next-line no-await-in-loop
await this.flush();
stats.adHocCalls += 1;
}
2024-03-15 14:20:33 +00:00
let cursor: PageMessagesCursorType | undefined;
const callHistory = await DataReader.getAllCallHistory();
2024-04-30 13:24:21 +00:00
const callHistoryByCallId = makeLookup(callHistory, 'callId');
const me = window.ConversationController.getOurConversationOrThrow();
const serviceId = me.get('serviceId');
const aci = isAciString(serviceId) ? serviceId : undefined;
strictAssert(aci, 'We must have our own ACI');
const aboutMe = {
aci,
pni: me.get('pni'),
};
2024-03-15 14:20:33 +00:00
try {
while (!cursor?.done) {
2024-07-24 00:31:40 +00:00
const { messages, cursor: newCursor } =
// eslint-disable-next-line no-await-in-loop
await DataReader.pageMessages(cursor);
2024-03-15 14:20:33 +00:00
// eslint-disable-next-line no-await-in-loop
const items = await pMap(
messages,
message =>
this.toChatItem(message, {
aboutMe,
callHistoryByCallId,
backupLevel,
}),
2024-03-15 14:20:33 +00:00
{ concurrency: MAX_CONCURRENCY }
);
for (const chatItem of items) {
if (chatItem === undefined) {
stats.skippedMessages += 1;
// Can't be backed up.
continue;
}
this.pushFrame({
chatItem,
});
// eslint-disable-next-line no-await-in-loop
await this.flush();
stats.messages += 1;
}
cursor = newCursor;
}
} finally {
if (cursor !== undefined) {
2024-07-22 18:16:33 +00:00
await DataReader.finishPageMessages(cursor);
2024-03-15 14:20:33 +00:00
}
}
await this.flush();
2024-05-29 23:46:43 +00:00
log.warn('backups: final stats', {
...stats,
attachmentBackupJobs: this.attachmentBackupJobs.length,
});
2024-03-15 14:20:33 +00:00
this.push(null);
}
private pushBuffer(buffer: Uint8Array): void {
this.buffers.push(buffer);
}
private pushFrame(frame: Backups.IFrame): void {
this.pushBuffer(Backups.Frame.encodeDelimited(frame).finish());
}
private async flush(): Promise<void> {
const chunk = Bytes.concatenate(this.buffers);
this.buffers = [];
// Below watermark, no pausing required
if (this.push(chunk)) {
return;
}
const { promise, resolve } = explodePromise<void>();
strictAssert(this.flushResolve === undefined, 'flush already pending');
this.flushResolve = resolve;
const start = Date.now();
log.info('backups: flush paused due to pushback');
try {
await pTimeout(promise, FLUSH_TIMEOUT);
} finally {
const duration = Date.now() - start;
if (duration > REPORTING_THRESHOLD) {
log.info(`backups: flush resumed after ${duration}ms`);
}
this.flushResolve = undefined;
}
}
override _read(): void {
this.flushResolve?.();
}
private async toAccountData(): Promise<Backups.IAccountData> {
const { storage } = window;
const me = window.ConversationController.getOurConversationOrThrow();
const rawPreferredReactionEmoji = window.storage.get(
'preferredReactionEmoji'
);
let preferredReactionEmoji: Array<string> | undefined;
if (canPreferredReactionEmojiBeSynced(rawPreferredReactionEmoji)) {
preferredReactionEmoji = rawPreferredReactionEmoji;
}
const PHONE_NUMBER_SHARING_MODE_ENUM =
Backups.AccountData.PhoneNumberSharingMode;
const rawPhoneNumberSharingMode = parsePhoneNumberSharingMode(
storage.get('phoneNumberSharingMode')
);
let phoneNumberSharingMode: Backups.AccountData.PhoneNumberSharingMode;
switch (rawPhoneNumberSharingMode) {
case PhoneNumberSharingMode.Everybody:
phoneNumberSharingMode = PHONE_NUMBER_SHARING_MODE_ENUM.EVERYBODY;
break;
case PhoneNumberSharingMode.ContactsOnly:
case PhoneNumberSharingMode.Nobody:
phoneNumberSharingMode = PHONE_NUMBER_SHARING_MODE_ENUM.NOBODY;
break;
default:
throw missingCaseError(rawPhoneNumberSharingMode);
}
2024-04-16 15:40:41 +00:00
const usernameLink = storage.get('usernameLink');
const subscriberId = storage.get('subscriberId');
const backupsSubscriberId = storage.get('backupsSubscriberId');
2024-03-15 14:20:33 +00:00
return {
profileKey: storage.get('profileKey'),
2024-04-16 15:40:41 +00:00
username: me.get('username') || null,
usernameLink: usernameLink
? {
...usernameLink,
2024-03-15 14:20:33 +00:00
2024-04-16 15:40:41 +00:00
// Same numeric value, no conversion needed
color: storage.get('usernameLinkColor'),
}
: null,
2024-03-15 14:20:33 +00:00
givenName: me.get('profileName'),
familyName: me.get('profileFamilyName'),
avatarUrlPath: storage.get('avatarUrl'),
backupsSubscriberData: Bytes.isNotEmpty(backupsSubscriberId)
? {
subscriberId: backupsSubscriberId,
currencyCode: storage.get('backupsSubscriberCurrencyCode'),
manuallyCancelled: storage.get(
'backupsSubscriptionManuallyCancelled',
false
),
}
: null,
donationSubscriberData: Bytes.isNotEmpty(subscriberId)
? {
subscriberId,
currencyCode: storage.get('subscriberCurrencyCode'),
manuallyCancelled: storage.get(
'donorSubscriptionManuallyCancelled',
false
),
}
: null,
2024-03-15 14:20:33 +00:00
accountSettings: {
readReceipts: storage.get('read-receipt-setting'),
sealedSenderIndicators: storage.get('sealedSenderIndicators'),
typingIndicators: window.Events.getTypingIndicatorSetting(),
linkPreviews: window.Events.getLinkPreviewSetting(),
notDiscoverableByPhoneNumber:
parsePhoneNumberDiscoverability(
storage.get('phoneNumberDiscoverability')
) === PhoneNumberDiscoverability.NotDiscoverable,
preferContactAvatars: storage.get('preferContactAvatars'),
2024-07-25 14:58:51 +00:00
universalExpireTimerSeconds: storage.get('universalExpireTimer'),
2024-03-15 14:20:33 +00:00
preferredReactionEmoji,
displayBadgesOnProfile: storage.get('displayBadgesOnProfile'),
keepMutedChatsArchived: storage.get('keepMutedChatsArchived'),
hasSetMyStoriesPrivacy: storage.get('hasSetMyStoriesPrivacy'),
hasViewedOnboardingStory: storage.get('hasViewedOnboardingStory'),
storiesDisabled: storage.get('hasStoriesDisabled'),
storyViewReceiptsEnabled: storage.get('storyViewReceiptsEnabled'),
hasCompletedUsernameOnboarding: storage.get(
'hasCompletedUsernameOnboarding'
),
hasSeenGroupStoryEducationSheet: storage.get(
'hasSeenGroupStoryEducationSheet'
),
2024-03-15 14:20:33 +00:00
phoneNumberSharingMode,
2024-07-15 20:58:55 +00:00
// Note that this should be called before `toDefaultChatStyle` because
// it builds `customColorIdByUuid`
customChatColors: this.toCustomChatColors(),
defaultChatStyle: this.toDefaultChatStyle(),
2024-03-15 14:20:33 +00:00
},
};
}
2024-09-12 23:48:27 +00:00
private getExistingRecipientId(
options: GetRecipientIdOptionsType
): Long | undefined {
let existing: number | undefined;
if (options.serviceId != null) {
existing = this.serviceIdToRecipientId.get(options.serviceId);
}
if (existing === undefined && options.e164 != null) {
existing = this.e164ToRecipientId.get(options.e164);
}
if (existing === undefined && options.id != null) {
existing = this.convoIdToRecipientId.get(options.id);
}
if (existing !== undefined) {
return Long.fromNumber(existing);
}
return undefined;
2024-03-15 14:20:33 +00:00
}
private getRecipientId(options: GetRecipientIdOptionsType): Long {
2024-09-12 23:48:27 +00:00
const existing = this.getExistingRecipientId(options);
2024-03-15 14:20:33 +00:00
if (existing !== undefined) {
2024-09-12 23:48:27 +00:00
return existing;
2024-03-15 14:20:33 +00:00
}
const { id, serviceId, e164 } = options;
const recipientId = this.nextRecipientId;
this.nextRecipientId += 1;
if (id !== undefined) {
this.convoIdToRecipientId.set(id, recipientId);
}
if (serviceId !== undefined) {
2024-09-12 23:48:27 +00:00
this.serviceIdToRecipientId.set(serviceId, recipientId);
2024-03-15 14:20:33 +00:00
}
if (e164 !== undefined) {
2024-09-12 23:48:27 +00:00
this.e164ToRecipientId.set(e164, recipientId);
2024-03-15 14:20:33 +00:00
}
const result = Long.fromNumber(recipientId);
return result;
}
private getOrPushPrivateRecipient(options: GetRecipientIdOptionsType): Long {
2024-09-12 23:48:27 +00:00
const existing = this.getExistingRecipientId(options);
const needsPush = existing == null;
2024-03-15 14:20:33 +00:00
const result = this.getRecipientId(options);
if (needsPush) {
const { serviceId, e164 } = options;
this.pushFrame({
recipient: this.toRecipient(result, {
type: 'private',
serviceId,
e164,
}),
});
}
return result;
}
2024-08-13 18:39:04 +00:00
private getNextRecipientId(): number {
2024-03-15 14:20:33 +00:00
const recipientId = this.nextRecipientId;
this.nextRecipientId += 1;
2024-08-13 18:39:04 +00:00
return recipientId;
2024-03-15 14:20:33 +00:00
}
private toRecipient(
recipientId: Long,
convo: Omit<
ConversationAttributesType,
'id' | 'version' | 'expireTimerVersion'
>
2024-03-15 14:20:33 +00:00
): Backups.IRecipient | undefined {
const res: Backups.IRecipient = {
id: recipientId,
};
if (isMe(convo)) {
res.self = {};
} else if (isDirectConversation(convo)) {
let visibility: Backups.Contact.Visibility;
if (convo.removalStage == null) {
visibility = Backups.Contact.Visibility.VISIBLE;
} else if (convo.removalStage === 'justNotification') {
visibility = Backups.Contact.Visibility.HIDDEN;
} else if (convo.removalStage === 'messageRequest') {
visibility = Backups.Contact.Visibility.HIDDEN_MESSAGE_REQUEST;
} else {
throw missingCaseError(convo.removalStage);
}
2024-03-15 14:20:33 +00:00
res.contact = {
aci:
convo.serviceId && convo.serviceId !== convo.pni
? Aci.parseFromServiceIdString(convo.serviceId).getRawUuidBytes()
: null,
pni: convo.pni
? Pni.parseFromServiceIdString(convo.pni).getRawUuidBytes()
: null,
username: convo.username,
e164: convo.e164 ? Long.fromString(convo.e164) : null,
blocked: convo.serviceId
? window.storage.blocked.isServiceIdBlocked(convo.serviceId)
: null,
visibility,
2024-09-16 21:30:39 +00:00
...(convo.discoveredUnregisteredAt
? {
notRegistered: {
unregisteredTimestamp: convo.firstUnregisteredAt
? Long.fromNumber(convo.firstUnregisteredAt)
: null,
},
}
: {
registered: {},
}),
2024-03-15 14:20:33 +00:00
profileKey: convo.profileKey
? Bytes.fromBase64(convo.profileKey)
: null,
profileSharing: convo.profileSharing,
profileGivenName: convo.profileName,
profileFamilyName: convo.profileFamilyName,
hideStory: convo.hideStory === true,
};
} else if (isGroupV2(convo) && convo.masterKey) {
let storySendMode: Backups.Group.StorySendMode;
switch (convo.storySendMode) {
case StorySendMode.Always:
storySendMode = Backups.Group.StorySendMode.ENABLED;
break;
case StorySendMode.Never:
storySendMode = Backups.Group.StorySendMode.DISABLED;
break;
default:
storySendMode = Backups.Group.StorySendMode.DEFAULT;
break;
}
2024-06-24 18:38:59 +00:00
const masterKey = Bytes.fromBase64(convo.masterKey);
2024-03-15 14:20:33 +00:00
res.group = {
2024-06-24 18:38:59 +00:00
masterKey,
2024-03-15 14:20:33 +00:00
whitelisted: convo.profileSharing,
hideStory: convo.hideStory === true,
storySendMode,
2024-06-24 18:38:59 +00:00
snapshot: {
title: {
title: convo.name ?? '',
},
2024-09-12 23:48:27 +00:00
description:
convo.description != null
? {
descriptionText: convo.description,
}
: null,
2024-06-24 18:38:59 +00:00
avatarUrl: convo.avatar?.url,
disappearingMessagesTimer:
convo.expireTimer != null
? {
disappearingMessagesDuration: DurationInSeconds.toSeconds(
convo.expireTimer
),
}
: null,
accessControl: convo.accessControl,
version: convo.revision || 0,
members: convo.membersV2?.map(member => {
return {
userId: this.aciToBytes(member.aci),
role: member.role,
joinedAtVersion: member.joinedAtVersion,
};
}),
membersPendingProfileKey: convo.pendingMembersV2?.map(member => {
return {
member: {
userId: this.serviceIdToBytes(member.serviceId),
role: member.role,
joinedAtVersion: 0,
},
addedByUserId: this.aciToBytes(member.addedByUserId),
timestamp: getSafeLongFromTimestamp(member.timestamp),
};
}),
membersPendingAdminApproval: convo.pendingAdminApprovalV2?.map(
member => {
return {
userId: this.aciToBytes(member.aci),
timestamp: getSafeLongFromTimestamp(member.timestamp),
};
}
),
membersBanned: convo.bannedMembersV2?.map(member => {
return {
userId: this.serviceIdToBytes(member.serviceId),
timestamp: getSafeLongFromTimestamp(member.timestamp),
};
}),
inviteLinkPassword: convo.groupInviteLinkPassword
? Bytes.fromBase64(convo.groupInviteLinkPassword)
: null,
announcementsOnly: convo.announcementsOnly === true,
},
2024-03-15 14:20:33 +00:00
};
} else {
return undefined;
}
return res;
}
private async toChatItem(
2024-04-30 13:24:21 +00:00
message: MessageAttributesType,
2024-05-22 16:34:19 +00:00
{ aboutMe, callHistoryByCallId, backupLevel }: ToChatItemOptionsType
2024-04-30 13:24:21 +00:00
): Promise<Backups.IChatItem | undefined> {
const conversation = window.ConversationController.get(
message.conversationId
);
if (conversation && isGroupV1(conversation.attributes)) {
log.warn('backups: skipping gv1 message');
return undefined;
}
2024-03-15 14:20:33 +00:00
const chatId = this.getRecipientId({ id: message.conversationId });
if (chatId === undefined) {
log.warn('backups: message chat not found');
return undefined;
}
2024-08-06 15:28:27 +00:00
const expirationTimestamp = calculateExpirationTimestamp(message);
if (expirationTimestamp != null && expirationTimestamp <= this.now + DAY) {
// Message expires too soon
return undefined;
}
2024-04-30 13:24:21 +00:00
let authorId: Long | undefined;
2024-03-15 14:20:33 +00:00
const isOutgoing = message.type === 'outgoing';
2024-04-30 13:24:21 +00:00
const isIncoming = message.type === 'incoming';
2024-03-15 14:20:33 +00:00
// Pacify typescript
if (message.sourceServiceId) {
2024-03-15 14:20:33 +00:00
authorId = this.getOrPushPrivateRecipient({
serviceId: message.sourceServiceId,
e164: message.source,
});
} else if (message.source) {
authorId = this.getOrPushPrivateRecipient({
serviceId: message.sourceServiceId,
e164: message.source,
});
} else {
strictAssert(!isIncoming, 'Incoming message must have source');
// Author must be always present, even if we are directionless
authorId = this.getOrPushPrivateRecipient({
serviceId: aboutMe.aci,
});
2024-04-30 13:24:21 +00:00
}
2024-04-30 13:24:21 +00:00
if (isOutgoing || isIncoming) {
strictAssert(authorId, 'Incoming/outgoing messages require an author');
2024-03-15 14:20:33 +00:00
}
2024-04-18 17:02:32 +00:00
let expireStartDate: Long | undefined;
let expiresInMs: Long | undefined;
if (
message.expireTimer != null &&
message.expirationStartTimestamp != null
) {
expireStartDate = getSafeLongFromTimestamp(
message.expirationStartTimestamp
);
expiresInMs = Long.fromNumber(
DurationInSeconds.toMillis(message.expireTimer)
);
}
2024-03-15 14:20:33 +00:00
const result: Backups.IChatItem = {
chatId,
authorId,
2024-06-03 17:02:25 +00:00
dateSent: getSafeLongFromTimestamp(
message.editMessageTimestamp || message.sent_at
),
2024-04-18 17:02:32 +00:00
expireStartDate,
expiresInMs,
2024-03-15 14:20:33 +00:00
revisions: [],
2024-06-13 23:26:26 +00:00
sms: message.sms === true,
2024-04-30 13:24:21 +00:00
};
2024-03-15 14:20:33 +00:00
2024-04-30 13:24:21 +00:00
if (!isNormalBubble(message)) {
2024-05-22 16:34:19 +00:00
const { patch, kind } = await this.toChatItemFromNonBubble({
authorId,
message,
aboutMe,
callHistoryByCallId,
});
2024-04-30 13:24:21 +00:00
2024-05-22 16:34:19 +00:00
if (kind === NonBubbleResultKind.Drop) {
return undefined;
}
2024-03-15 14:20:33 +00:00
2024-05-22 16:34:19 +00:00
if (kind === NonBubbleResultKind.Directed) {
strictAssert(
authorId,
'Incoming/outgoing non-bubble messages require an author'
);
const me = this.getOrPushPrivateRecipient({
serviceId: aboutMe.aci,
});
2024-03-15 14:20:33 +00:00
2024-05-22 16:34:19 +00:00
if (authorId === me) {
2024-06-03 17:02:25 +00:00
result.outgoing = this.getOutgoingMessageDetails(
message.sent_at,
message
);
2024-05-22 16:34:19 +00:00
} else {
result.incoming = this.getIncomingMessageDetails(message);
2024-03-15 14:20:33 +00:00
}
2024-05-22 16:34:19 +00:00
} else if (kind === NonBubbleResultKind.Directionless) {
result.directionless = {};
} else {
throw missingCaseError(kind);
2024-03-15 14:20:33 +00:00
}
2024-04-30 13:24:21 +00:00
2024-05-22 16:34:19 +00:00
return { ...result, ...patch };
2024-04-30 13:24:21 +00:00
}
2024-05-22 16:34:19 +00:00
const { contact, sticker } = message;
2024-10-29 18:16:09 +00:00
if (isTapToView(message)) {
result.viewOnceMessage = await this.toViewOnceMessage({
message,
backupLevel,
});
} else if (message.isErased) {
2024-05-22 16:34:19 +00:00
result.remoteDeletedMessage = {};
} else if (messageHasPaymentEvent(message)) {
const { payment } = message;
switch (payment.kind) {
case PaymentEventKind.ActivationRequest: {
2024-09-16 21:30:39 +00:00
result.directionless = {};
2024-05-22 16:34:19 +00:00
result.updateMessage = {
simpleUpdate: {
type: Backups.SimpleChatUpdate.Type.PAYMENT_ACTIVATION_REQUEST,
},
};
break;
}
case PaymentEventKind.Activation: {
2024-09-16 21:30:39 +00:00
result.directionless = {};
2024-05-22 16:34:19 +00:00
result.updateMessage = {
simpleUpdate: {
type: Backups.SimpleChatUpdate.Type.PAYMENTS_ACTIVATED,
},
};
break;
}
case PaymentEventKind.Notification:
result.paymentNotification = {
2024-09-16 21:30:39 +00:00
note: payment.note ?? null,
2024-05-22 16:34:19 +00:00
amountMob: payment.amountMob,
feeMob: payment.feeMob,
transactionDetails: payment.transactionDetailsBase64
? Backups.PaymentNotification.TransactionDetails.decode(
Bytes.fromBase64(payment.transactionDetailsBase64)
)
: undefined,
};
break;
default:
throw missingCaseError(payment);
}
} else if (contact && contact[0]) {
2024-04-30 13:24:21 +00:00
const contactMessage = new Backups.ContactMessage();
contactMessage.contact = await Promise.all(
contact.map(async contactDetails => ({
...contactDetails,
number: contactDetails.number?.map(number => ({
...number,
type: numberToPhoneType(number.type),
})),
email: contactDetails.email?.map(email => ({
...email,
type: numberToPhoneType(email.type),
})),
address: contactDetails.address?.map(address => ({
...address,
type: numberToAddressType(address.type),
})),
avatar: contactDetails.avatar?.avatar
? await this.processAttachment({
attachment: contactDetails.avatar.avatar,
backupLevel,
messageReceivedAt: message.received_at,
})
: undefined,
}))
);
2024-04-30 13:24:21 +00:00
2024-05-22 16:34:19 +00:00
const reactions = this.getMessageReactions(message);
if (reactions != null) {
contactMessage.reactions = reactions;
}
2024-04-30 13:24:21 +00:00
2024-05-22 16:34:19 +00:00
result.contactMessage = contactMessage;
} else if (sticker) {
2024-04-30 13:24:21 +00:00
const stickerProto = new Backups.Sticker();
stickerProto.emoji = sticker.emoji;
stickerProto.packId = Bytes.fromHex(sticker.packId);
stickerProto.packKey = Bytes.fromBase64(sticker.packKey);
stickerProto.stickerId = sticker.stickerId;
stickerProto.data = sticker.data
? await this.processAttachment({
attachment: sticker.data,
backupLevel,
messageReceivedAt: message.received_at,
})
: undefined;
2024-04-30 13:24:21 +00:00
2024-05-22 16:34:19 +00:00
result.stickerMessage = {
sticker: stickerProto,
reactions: this.getMessageReactions(message),
};
} else if (isGiftBadge(message)) {
const { giftBadge } = message;
strictAssert(giftBadge != null, 'Message must have gift badge');
if (giftBadge.state === GiftBadgeStates.Failed) {
result.giftBadge = {
state: Backups.GiftBadge.State.FAILED,
};
} else {
let state: Backups.GiftBadge.State;
switch (giftBadge.state) {
case GiftBadgeStates.Unopened:
state = Backups.GiftBadge.State.UNOPENED;
break;
case GiftBadgeStates.Opened:
state = Backups.GiftBadge.State.OPENED;
break;
case GiftBadgeStates.Redeemed:
state = Backups.GiftBadge.State.REDEEMED;
break;
default:
throw missingCaseError(giftBadge);
}
result.giftBadge = {
receiptCredentialPresentation: Bytes.fromBase64(
giftBadge.receiptCredentialPresentation
),
state,
};
}
2024-05-22 16:34:19 +00:00
} else {
2024-09-23 19:24:41 +00:00
result.standardMessage = await this.toStandardMessage({
2024-06-03 17:02:25 +00:00
message,
2024-09-23 19:24:41 +00:00
backupLevel,
});
2024-06-03 17:02:25 +00:00
result.revisions = await this.toChatItemRevisions(
result,
message,
backupLevel
);
2024-05-22 16:34:19 +00:00
}
2024-04-30 13:24:21 +00:00
2024-05-22 16:34:19 +00:00
if (isOutgoing) {
2024-06-03 17:02:25 +00:00
result.outgoing = this.getOutgoingMessageDetails(
message.sent_at,
message
);
2024-05-22 16:34:19 +00:00
} else {
result.incoming = this.getIncomingMessageDetails(message);
2024-04-30 13:24:21 +00:00
}
2024-05-22 16:34:19 +00:00
return result;
}
private aciToBytes(aci: AciString | string): Uint8Array {
return Aci.parseFromServiceIdString(aci).getRawUuidBytes();
}
private serviceIdToBytes(serviceId: ServiceIdString): Uint8Array {
return ServiceId.parseFromServiceIdString(serviceId).getRawUuidBytes();
}
private async toChatItemFromNonBubble(
options: NonBubbleOptionsType
): Promise<NonBubbleResultType> {
return this.toChatItemUpdate(options);
2024-04-30 13:24:21 +00:00
}
async toChatItemUpdate(
2024-05-22 16:34:19 +00:00
options: NonBubbleOptionsType
): Promise<NonBubbleResultType> {
2024-08-13 18:39:04 +00:00
const { authorId, callHistoryByCallId, message } = options;
2024-04-30 13:24:21 +00:00
const logId = `toChatItemUpdate(${getMessageIdForLogging(message)})`;
const updateMessage = new Backups.ChatUpdateMessage();
2024-05-22 16:34:19 +00:00
const patch: Backups.IChatItem = {
updateMessage,
};
2024-04-30 13:24:21 +00:00
if (isCallHistory(message)) {
2024-08-13 18:39:04 +00:00
const conversation = window.ConversationController.get(
message.conversationId
);
if (!conversation) {
throw new Error(
`${logId}: callHistory message had unknown conversationId!`
);
}
const { callId } = message;
if (!callId) {
throw new Error(`${logId}: callHistory message was missing callId!`);
}
const callHistory = callHistoryByCallId[callId];
if (!callHistory) {
throw new Error(
`${logId}: callHistory message had callId, but no call history details were found!`
);
}
if (isGroup(conversation.attributes)) {
const groupCall = new Backups.GroupCall();
strictAssert(
callHistory.mode === CallMode.Group,
'in group, should be group call'
);
if (callHistory.status === GroupCallStatus.Deleted) {
return { kind: NonBubbleResultKind.Drop };
}
const { ringerId, startedById } = callHistory;
2024-08-13 18:39:04 +00:00
if (ringerId) {
const ringerConversation =
window.ConversationController.get(ringerId);
if (!ringerConversation) {
throw new Error(
'toChatItemUpdate/callHistory: ringerId conversation not found!'
);
}
const recipientId = this.getRecipientId(
ringerConversation.attributes
);
groupCall.ringerRecipientId = recipientId;
}
if (startedById) {
const startedByConvo = window.ConversationController.get(startedById);
if (!startedByConvo) {
throw new Error(
'toChatItemUpdate/callHistory: startedById conversation not found!'
);
}
const recipientId = this.getRecipientId(startedByConvo.attributes);
2024-08-13 18:39:04 +00:00
groupCall.startedCallRecipientId = recipientId;
}
groupCall.callId = Long.fromString(callId);
groupCall.state = toGroupCallStateProto(callHistory.status);
groupCall.startedCallTimestamp = Long.fromNumber(callHistory.timestamp);
if (callHistory.endedTimestamp != null) {
groupCall.endedCallTimestamp = Long.fromNumber(
callHistory.endedTimestamp
);
}
2024-08-13 18:39:04 +00:00
groupCall.read = message.seenStatus === SeenStatus.Seen;
updateMessage.groupCall = groupCall;
return { kind: NonBubbleResultKind.Directionless, patch };
}
const individualCall = new Backups.IndividualCall();
const { direction, type, status, timestamp } = callHistory;
if (status === GroupCallStatus.Deleted) {
return { kind: NonBubbleResultKind.Drop };
}
individualCall.callId = Long.fromString(callId);
individualCall.type = toIndividualCallTypeProto(type);
individualCall.direction = toIndividualCallDirectionProto(direction);
individualCall.state = toIndividualCallStateProto(status);
individualCall.startedCallTimestamp = Long.fromNumber(timestamp);
individualCall.read = message.seenStatus === SeenStatus.Seen;
updateMessage.individualCall = individualCall;
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isExpirationTimerUpdate(message)) {
const expiresInSeconds = message.expirationTimerUpdate?.expireTimer;
2024-07-25 14:58:51 +00:00
const expiresInMs =
expiresInSeconds == null
? 0
: DurationInSeconds.toMillis(expiresInSeconds);
2024-04-30 13:24:21 +00:00
const conversation = window.ConversationController.get(
message.conversationId
);
if (conversation && isGroup(conversation.attributes)) {
const groupChatUpdate = new Backups.GroupChangeChatUpdate();
const timerUpdate = new Backups.GroupExpirationTimerUpdate();
2024-07-25 14:58:51 +00:00
timerUpdate.expiresInMs = Long.fromNumber(expiresInMs);
2024-04-30 13:24:21 +00:00
const sourceServiceId = message.expirationTimerUpdate?.sourceServiceId;
if (sourceServiceId && Aci.parseFromServiceIdString(sourceServiceId)) {
timerUpdate.updaterAci = uuidToBytes(sourceServiceId);
}
const innerUpdate = new Backups.GroupChangeChatUpdate.Update();
innerUpdate.groupExpirationTimerUpdate = timerUpdate;
groupChatUpdate.updates = [innerUpdate];
updateMessage.groupChange = groupChatUpdate;
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
const source =
message.expirationTimerUpdate?.sourceServiceId ||
message.expirationTimerUpdate?.source;
2024-05-22 16:34:19 +00:00
if (source && !authorId) {
patch.authorId = this.getOrPushPrivateRecipient({
2024-04-30 13:24:21 +00:00
id: source,
});
}
const expirationTimerChange = new Backups.ExpirationTimerChatUpdate();
2024-07-25 14:58:51 +00:00
expirationTimerChange.expiresInMs = Long.fromNumber(expiresInMs);
2024-04-30 13:24:21 +00:00
updateMessage.expirationTimerChange = expirationTimerChange;
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isGroupV2Change(message)) {
updateMessage.groupChange = await this.toGroupV2Update(message, options);
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isKeyChange(message)) {
const simpleUpdate = new Backups.SimpleChatUpdate();
simpleUpdate.type = Backups.SimpleChatUpdate.Type.IDENTITY_UPDATE;
2024-05-22 16:34:19 +00:00
if (message.key_changed) {
// This will override authorId on the original chatItem
patch.authorId = this.getOrPushPrivateRecipient({
id: message.key_changed,
});
}
2024-04-30 13:24:21 +00:00
updateMessage.simpleUpdate = simpleUpdate;
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isProfileChange(message)) {
const profileChange = new Backups.ProfileChangeChatUpdate();
if (!message.profileChange) {
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Drop };
}
if (message.changedId) {
// This will override authorId on the original chatItem
patch.authorId = this.getOrPushPrivateRecipient({
id: message.changedId,
});
2024-04-30 13:24:21 +00:00
}
const { newName, oldName } = message.profileChange;
profileChange.newName = newName;
profileChange.previousName = oldName;
updateMessage.profileChange = profileChange;
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isVerifiedChange(message)) {
2024-05-22 16:34:19 +00:00
if (!message.verifiedChanged) {
throw new Error(
`${logId}: Message was verifiedChange, but missing verifiedChange!`
);
}
2024-04-30 13:24:21 +00:00
const simpleUpdate = new Backups.SimpleChatUpdate();
2024-05-22 16:34:19 +00:00
simpleUpdate.type = message.verified
? Backups.SimpleChatUpdate.Type.IDENTITY_VERIFIED
: Backups.SimpleChatUpdate.Type.IDENTITY_DEFAULT;
2024-04-30 13:24:21 +00:00
updateMessage.simpleUpdate = simpleUpdate;
2024-05-22 16:34:19 +00:00
if (message.verifiedChanged) {
// This will override authorId on the original chatItem
patch.authorId = this.getOrPushPrivateRecipient({
id: message.verifiedChanged,
});
}
return { kind: NonBubbleResultKind.Directionless, patch };
}
if (isChangeNumberNotification(message)) {
updateMessage.simpleUpdate = {
type: Backups.SimpleChatUpdate.Type.CHANGE_NUMBER,
};
return { kind: NonBubbleResultKind.Directionless, patch };
}
if (isJoinedSignalNotification(message)) {
updateMessage.simpleUpdate = {
type: Backups.SimpleChatUpdate.Type.JOINED_SIGNAL,
};
return { kind: NonBubbleResultKind.Directionless, patch };
}
if (isTitleTransitionNotification(message)) {
strictAssert(
message.titleTransition != null,
'Missing title transition data'
);
const { renderInfo } = message.titleTransition;
if (renderInfo.e164) {
updateMessage.learnedProfileChange = {
e164: Long.fromString(renderInfo.e164),
};
} else {
strictAssert(
renderInfo.username,
'Title transition must have username or e164'
);
updateMessage.learnedProfileChange = { username: renderInfo.username };
}
return { kind: NonBubbleResultKind.Directionless, patch };
}
2024-07-31 13:43:26 +00:00
if (isMessageRequestResponse(message)) {
const { messageRequestResponseEvent: event } = message;
if (event == null) {
return { kind: NonBubbleResultKind.Drop };
}
let type: Backups.SimpleChatUpdate.Type;
const { Type } = Backups.SimpleChatUpdate;
switch (event) {
case MessageRequestResponseEvent.SPAM:
type = Type.REPORTED_SPAM;
break;
2024-09-16 21:30:39 +00:00
case MessageRequestResponseEvent.BLOCK:
type = Type.BLOCKED;
break;
case MessageRequestResponseEvent.UNBLOCK:
type = Type.UNBLOCKED;
break;
case MessageRequestResponseEvent.ACCEPT:
type = Type.MESSAGE_REQUEST_ACCEPTED;
break;
2024-07-31 13:43:26 +00:00
default:
throw missingCaseError(event);
}
updateMessage.simpleUpdate = { type };
return { kind: NonBubbleResultKind.Directionless, patch };
}
2024-05-22 16:34:19 +00:00
if (isDeliveryIssue(message)) {
updateMessage.simpleUpdate = {
type: Backups.SimpleChatUpdate.Type.BAD_DECRYPT,
};
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isConversationMerge(message)) {
const threadMerge = new Backups.ThreadMergeChatUpdate();
const e164 = message.conversationMerge?.renderInfo.e164;
if (!e164) {
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Drop };
2024-04-30 13:24:21 +00:00
}
threadMerge.previousE164 = Long.fromString(e164);
updateMessage.threadMerge = threadMerge;
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isPhoneNumberDiscovery(message)) {
const e164 = message.phoneNumberDiscovery?.e164;
if (!e164) {
return { kind: NonBubbleResultKind.Drop };
}
updateMessage.sessionSwitchover = {
e164: Long.fromString(e164),
};
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isUniversalTimerNotification(message)) {
2024-05-22 16:34:19 +00:00
// Transient, drop it
return { kind: NonBubbleResultKind.Drop };
2024-04-30 13:24:21 +00:00
}
if (isContactRemovedNotification(message)) {
// Transient, drop it
return { kind: NonBubbleResultKind.Drop };
2024-04-30 13:24:21 +00:00
}
// Create a GV2 tombstone for a deprecated GV1 notification
2024-04-30 13:24:21 +00:00
if (isGroupUpdate(message)) {
updateMessage.groupChange = {
updates: [
{
genericGroupUpdate: {
updaterAci: message.sourceServiceId
? this.serviceIdToBytes(message.sourceServiceId)
: undefined,
},
},
],
};
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
2024-05-22 16:34:19 +00:00
if (isUnsupportedMessage(message)) {
const simpleUpdate = new Backups.SimpleChatUpdate();
simpleUpdate.type =
Backups.SimpleChatUpdate.Type.UNSUPPORTED_PROTOCOL_MESSAGE;
updateMessage.simpleUpdate = simpleUpdate;
2024-05-22 16:34:19 +00:00
2024-09-16 21:30:39 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
}
2024-05-22 16:34:19 +00:00
2024-04-30 13:24:21 +00:00
if (isGroupV1Migration(message)) {
const { groupMigration } = message;
const groupChatUpdate = new Backups.GroupChangeChatUpdate();
groupChatUpdate.updates = [];
const areWeInvited = groupMigration?.areWeInvited ?? false;
const droppedMemberCount =
groupMigration?.droppedMemberCount ??
groupMigration?.droppedMemberIds?.length ??
message.droppedGV2MemberIds?.length ??
0;
const invitedMemberCount =
groupMigration?.invitedMemberCount ??
groupMigration?.invitedMembers?.length ??
message.invitedGV2Members?.length ??
0;
let addedItem = false;
if (areWeInvited) {
const container = new Backups.GroupChangeChatUpdate.Update();
container.groupV2MigrationSelfInvitedUpdate =
new Backups.GroupV2MigrationSelfInvitedUpdate();
groupChatUpdate.updates.push(container);
addedItem = true;
}
if (droppedMemberCount > 0) {
const container = new Backups.GroupChangeChatUpdate.Update();
const update = new Backups.GroupV2MigrationDroppedMembersUpdate();
update.droppedMembersCount = droppedMemberCount;
container.groupV2MigrationDroppedMembersUpdate = update;
groupChatUpdate.updates.push(container);
addedItem = true;
}
if (invitedMemberCount > 0) {
const container = new Backups.GroupChangeChatUpdate.Update();
const update = new Backups.GroupV2MigrationInvitedMembersUpdate();
update.invitedMembersCount = invitedMemberCount;
container.groupV2MigrationInvitedMembersUpdate = update;
groupChatUpdate.updates.push(container);
addedItem = true;
}
if (!addedItem) {
const container = new Backups.GroupChangeChatUpdate.Update();
container.groupV2MigrationUpdate = new Backups.GroupV2MigrationUpdate();
groupChatUpdate.updates.push(container);
}
updateMessage.groupChange = groupChatUpdate;
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isEndSession(message)) {
const simpleUpdate = new Backups.SimpleChatUpdate();
simpleUpdate.type = Backups.SimpleChatUpdate.Type.END_SESSION;
updateMessage.simpleUpdate = simpleUpdate;
2024-09-16 21:30:39 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
if (isChatSessionRefreshed(message)) {
const simpleUpdate = new Backups.SimpleChatUpdate();
simpleUpdate.type = Backups.SimpleChatUpdate.Type.CHAT_SESSION_REFRESH;
updateMessage.simpleUpdate = simpleUpdate;
2024-05-22 16:34:19 +00:00
return { kind: NonBubbleResultKind.Directionless, patch };
2024-04-30 13:24:21 +00:00
}
throw new Error(
`${logId}: Message was not a bubble, but didn't understand type`
);
}
async toGroupV2Update(
message: MessageAttributesType,
options: {
aboutMe: AboutMe;
}
): Promise<Backups.GroupChangeChatUpdate | undefined> {
const logId = `toGroupV2Update(${getMessageIdForLogging(message)})`;
const { groupV2Change } = message;
const { aboutMe } = options;
if (!isGroupV2Change(message) || !groupV2Change) {
throw new Error(`${logId}: Message was not a groupv2 change`);
}
const { from, details } = groupV2Change;
const updates: Array<Backups.GroupChangeChatUpdate.Update> = [];
details.forEach(detail => {
const update = new Backups.GroupChangeChatUpdate.Update();
const { type } = detail;
if (type === 'create') {
const innerUpdate = new Backups.GroupCreationUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
update.groupCreationUpdate = innerUpdate;
updates.push(update);
} else if (type === 'access-attributes') {
const innerUpdate =
new Backups.GroupAttributesAccessLevelChangeUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.accessLevel = detail.newPrivilege;
update.groupAttributesAccessLevelChangeUpdate = innerUpdate;
updates.push(update);
} else if (type === 'access-members') {
const innerUpdate =
new Backups.GroupMembershipAccessLevelChangeUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.accessLevel = detail.newPrivilege;
update.groupMembershipAccessLevelChangeUpdate = innerUpdate;
updates.push(update);
} else if (type === 'access-invite-link') {
const innerUpdate = new Backups.GroupInviteLinkAdminApprovalUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.linkRequiresAdminApproval =
detail.newPrivilege ===
SignalService.AccessControl.AccessRequired.ADMINISTRATOR;
update.groupInviteLinkAdminApprovalUpdate = innerUpdate;
updates.push(update);
} else if (type === 'announcements-only') {
const innerUpdate = new Backups.GroupAnnouncementOnlyChangeUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.isAnnouncementOnly = detail.announcementsOnly;
update.groupAnnouncementOnlyChangeUpdate = innerUpdate;
updates.push(update);
} else if (type === 'avatar') {
const innerUpdate = new Backups.GroupAvatarUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.wasRemoved = detail.removed;
update.groupAvatarUpdate = innerUpdate;
updates.push(update);
} else if (type === 'title') {
const innerUpdate = new Backups.GroupNameUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.newGroupName = detail.newTitle;
update.groupNameUpdate = innerUpdate;
updates.push(update);
} else if (type === 'group-link-add') {
const innerUpdate = new Backups.GroupInviteLinkEnabledUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.linkRequiresAdminApproval =
detail.privilege ===
SignalService.AccessControl.AccessRequired.ADMINISTRATOR;
update.groupInviteLinkEnabledUpdate = innerUpdate;
updates.push(update);
} else if (type === 'group-link-reset') {
const innerUpdate = new Backups.GroupInviteLinkResetUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
update.groupInviteLinkResetUpdate = innerUpdate;
updates.push(update);
} else if (type === 'group-link-remove') {
const innerUpdate = new Backups.GroupInviteLinkDisabledUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
update.groupInviteLinkDisabledUpdate = innerUpdate;
updates.push(update);
} else if (type === 'member-add') {
if (from && from === detail.aci) {
const innerUpdate = new Backups.GroupMemberJoinedUpdate();
innerUpdate.newMemberAci = this.serviceIdToBytes(from);
update.groupMemberJoinedUpdate = innerUpdate;
updates.push(update);
return;
}
const innerUpdate = new Backups.GroupMemberAddedUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.newMemberAci = this.aciToBytes(detail.aci);
update.groupMemberAddedUpdate = innerUpdate;
updates.push(update);
} else if (type === 'member-add-from-invite') {
const { aci, pni } = detail;
if (
from &&
((pni && from === pni) ||
(aci && from === aci) ||
checkServiceIdEquivalence(from, aci))
) {
2024-04-30 13:24:21 +00:00
const innerUpdate = new Backups.GroupInvitationAcceptedUpdate();
innerUpdate.newMemberAci = this.aciToBytes(detail.aci);
if (detail.inviter) {
innerUpdate.inviterAci = this.aciToBytes(detail.inviter);
}
update.groupInvitationAcceptedUpdate = innerUpdate;
updates.push(update);
return;
}
const innerUpdate = new Backups.GroupMemberAddedUpdate();
innerUpdate.newMemberAci = this.aciToBytes(detail.aci);
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
if (detail.inviter) {
innerUpdate.inviterAci = this.aciToBytes(detail.inviter);
}
innerUpdate.hadOpenInvitation = true;
update.groupMemberAddedUpdate = innerUpdate;
updates.push(update);
} else if (type === 'member-add-from-link') {
const innerUpdate = new Backups.GroupMemberJoinedByLinkUpdate();
innerUpdate.newMemberAci = this.aciToBytes(detail.aci);
update.groupMemberJoinedByLinkUpdate = innerUpdate;
updates.push(update);
} else if (type === 'member-add-from-admin-approval') {
const innerUpdate = new Backups.GroupJoinRequestApprovalUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.requestorAci = this.aciToBytes(detail.aci);
innerUpdate.wasApproved = true;
update.groupJoinRequestApprovalUpdate = innerUpdate;
updates.push(update);
} else if (type === 'member-privilege') {
const innerUpdate = new Backups.GroupAdminStatusUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.memberAci = this.aciToBytes(detail.aci);
innerUpdate.wasAdminStatusGranted =
detail.newPrivilege === SignalService.Member.Role.ADMINISTRATOR;
update.groupAdminStatusUpdate = innerUpdate;
updates.push(update);
} else if (type === 'member-remove') {
if (from && from === detail.aci) {
const innerUpdate = new Backups.GroupMemberLeftUpdate();
innerUpdate.aci = this.serviceIdToBytes(from);
update.groupMemberLeftUpdate = innerUpdate;
updates.push(update);
return;
}
const innerUpdate = new Backups.GroupMemberRemovedUpdate();
if (from) {
innerUpdate.removerAci = this.serviceIdToBytes(from);
}
innerUpdate.removedAci = this.aciToBytes(detail.aci);
update.groupMemberRemovedUpdate = innerUpdate;
updates.push(update);
} else if (type === 'pending-add-one') {
if (
(aboutMe.aci && detail.serviceId === aboutMe.aci) ||
(aboutMe.pni && detail.serviceId === aboutMe.pni)
) {
const innerUpdate = new Backups.SelfInvitedToGroupUpdate();
if (from) {
innerUpdate.inviterAci = this.serviceIdToBytes(from);
}
update.selfInvitedToGroupUpdate = innerUpdate;
updates.push(update);
return;
}
if (
from &&
((aboutMe.aci && from === aboutMe.aci) ||
(aboutMe.pni && from === aboutMe.pni))
) {
const innerUpdate = new Backups.SelfInvitedOtherUserToGroupUpdate();
innerUpdate.inviteeServiceId = this.serviceIdToBytes(
detail.serviceId
);
update.selfInvitedOtherUserToGroupUpdate = innerUpdate;
updates.push(update);
return;
}
const innerUpdate = new Backups.GroupUnknownInviteeUpdate();
if (from) {
innerUpdate.inviterAci = this.serviceIdToBytes(from);
}
innerUpdate.inviteeCount = 1;
update.groupUnknownInviteeUpdate = innerUpdate;
updates.push(update);
} else if (type === 'pending-add-many') {
const innerUpdate = new Backups.GroupUnknownInviteeUpdate();
if (from) {
innerUpdate.inviterAci = this.serviceIdToBytes(from);
}
innerUpdate.inviteeCount = detail.count;
update.groupUnknownInviteeUpdate = innerUpdate;
updates.push(update);
} else if (type === 'pending-remove-one') {
if (from && detail.serviceId && from === detail.serviceId) {
const innerUpdate = new Backups.GroupInvitationDeclinedUpdate();
if (detail.inviter) {
innerUpdate.inviterAci = this.aciToBytes(detail.inviter);
}
if (isAciString(detail.serviceId)) {
innerUpdate.inviteeAci = this.aciToBytes(detail.serviceId);
}
update.groupInvitationDeclinedUpdate = innerUpdate;
updates.push(update);
return;
}
if (
(aboutMe.aci && detail.serviceId === aboutMe.aci) ||
(aboutMe.pni && detail.serviceId === aboutMe.pni)
) {
const innerUpdate = new Backups.GroupSelfInvitationRevokedUpdate();
if (from) {
innerUpdate.revokerAci = this.serviceIdToBytes(from);
}
update.groupSelfInvitationRevokedUpdate = innerUpdate;
updates.push(update);
return;
}
const innerUpdate = new Backups.GroupInvitationRevokedUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.invitees = [
{
inviteeAci: isAciString(detail.serviceId)
? this.aciToBytes(detail.serviceId)
: undefined,
inviteePni: isPniString(detail.serviceId)
? this.serviceIdToBytes(detail.serviceId)
: undefined,
},
];
update.groupInvitationRevokedUpdate = innerUpdate;
updates.push(update);
} else if (type === 'pending-remove-many') {
const innerUpdate = new Backups.GroupInvitationRevokedUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.invitees = [];
for (let i = 0, max = detail.count; i < max; i += 1) {
// Yes, we're adding totally empty invitees. This is okay.
innerUpdate.invitees.push({});
}
update.groupInvitationRevokedUpdate = innerUpdate;
updates.push(update);
} else if (type === 'admin-approval-add-one') {
const innerUpdate = new Backups.GroupJoinRequestUpdate();
innerUpdate.requestorAci = this.aciToBytes(detail.aci);
update.groupJoinRequestUpdate = innerUpdate;
updates.push(update);
} else if (type === 'admin-approval-remove-one') {
if (from && detail.aci && from === detail.aci) {
const innerUpdate = new Backups.GroupJoinRequestCanceledUpdate();
innerUpdate.requestorAci = this.aciToBytes(detail.aci);
update.groupJoinRequestCanceledUpdate = innerUpdate;
updates.push(update);
return;
}
const innerUpdate = new Backups.GroupJoinRequestApprovalUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
innerUpdate.requestorAci = this.aciToBytes(detail.aci);
innerUpdate.wasApproved = false;
update.groupJoinRequestApprovalUpdate = innerUpdate;
updates.push(update);
} else if (type === 'admin-approval-bounce') {
// We can't express all we need in GroupSequenceOfRequestsAndCancelsUpdate, so we
// add an additional groupJoinRequestUpdate to express that there
// is an approval pending.
if (detail.isApprovalPending) {
const innerUpdate = new Backups.GroupJoinRequestUpdate();
innerUpdate.requestorAci = this.aciToBytes(detail.aci);
// We need to create another update since the items we put in Update are oneof
const secondUpdate = new Backups.GroupChangeChatUpdate.Update();
secondUpdate.groupJoinRequestUpdate = innerUpdate;
updates.push(secondUpdate);
// not returning because we really do want both of these
}
const innerUpdate =
new Backups.GroupSequenceOfRequestsAndCancelsUpdate();
innerUpdate.requestorAci = this.aciToBytes(detail.aci);
innerUpdate.count = detail.times;
update.groupSequenceOfRequestsAndCancelsUpdate = innerUpdate;
updates.push(update);
} else if (type === 'description') {
const innerUpdate = new Backups.GroupDescriptionUpdate();
innerUpdate.newDescription = detail.removed
? undefined
: detail.description;
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
update.groupDescriptionUpdate = innerUpdate;
updates.push(update);
} else if (type === 'summary') {
const innerUpdate = new Backups.GenericGroupUpdate();
if (from) {
innerUpdate.updaterAci = this.serviceIdToBytes(from);
}
update.genericGroupUpdate = innerUpdate;
updates.push(update);
} else {
throw missingCaseError(type);
}
});
if (updates.length === 0) {
throw new Error(`${logId}: No updates generated from message`);
}
const groupUpdate = new Backups.GroupChangeChatUpdate();
groupUpdate.updates = updates;
return groupUpdate;
}
private async toQuote({
quote,
backupLevel,
messageReceivedAt,
}: {
quote?: QuotedMessageType;
backupLevel: BackupLevel;
messageReceivedAt: number;
}): Promise<Backups.IQuote | null> {
2024-03-15 14:20:33 +00:00
if (!quote) {
return null;
}
let authorId: Long;
if (quote.authorAci) {
authorId = this.getOrPushPrivateRecipient({
serviceId: quote.authorAci,
e164: quote.author,
});
} else if (quote.author) {
authorId = this.getOrPushPrivateRecipient({
serviceId: quote.authorAci,
e164: quote.author,
});
} else {
log.warn('backups: quote has no author id');
return null;
}
return {
targetSentTimestamp: Long.fromNumber(quote.id),
2024-03-15 14:20:33 +00:00
authorId,
2024-09-12 23:48:27 +00:00
text:
quote.text != null
? {
body: quote.text,
bodyRanges: quote.bodyRanges?.map(range =>
this.toBodyRange(range)
),
}
: null,
attachments: await Promise.all(
quote.attachments.map(
async (
attachment: QuotedAttachmentType
): Promise<Backups.Quote.IQuotedAttachment> => {
return {
contentType: attachment.contentType,
fileName: attachment.fileName,
thumbnail: attachment.thumbnail
? await this.processMessageAttachment({
attachment: attachment.thumbnail,
backupLevel,
messageReceivedAt,
})
: undefined,
};
}
)
),
2024-03-15 14:20:33 +00:00
type: quote.isGiftBadge
? Backups.Quote.Type.GIFTBADGE
: Backups.Quote.Type.NORMAL,
};
}
private toBodyRange(range: RawBodyRange): Backups.IBodyRange {
return {
start: range.start,
length: range.length,
...('mentionAci' in range
? {
2024-04-30 13:24:21 +00:00
mentionAci: this.aciToBytes(range.mentionAci),
2024-03-15 14:20:33 +00:00
}
: {
// Numeric values are compatible between backup and message protos
style: range.style,
}),
};
}
private getMessageAttachmentFlag(
attachment: AttachmentType
): Backups.MessageAttachment.Flag {
2024-09-16 21:30:39 +00:00
const flag = SignalService.AttachmentPointer.Flags.VOICE_MESSAGE;
// eslint-disable-next-line no-bitwise
if (((attachment.flags || 0) & flag) === flag) {
return Backups.MessageAttachment.Flag.VOICE_MESSAGE;
}
if (isGIF([attachment])) {
return Backups.MessageAttachment.Flag.GIF;
}
if (
attachment.flags &&
// eslint-disable-next-line no-bitwise
attachment.flags & SignalService.AttachmentPointer.Flags.BORDERLESS
) {
return Backups.MessageAttachment.Flag.BORDERLESS;
}
return Backups.MessageAttachment.Flag.NONE;
}
private async processMessageAttachment({
attachment,
backupLevel,
2024-05-29 23:46:43 +00:00
messageReceivedAt,
}: {
attachment: AttachmentType;
backupLevel: BackupLevel;
2024-05-29 23:46:43 +00:00
messageReceivedAt: number;
}): Promise<Backups.MessageAttachment> {
const { clientUuid } = attachment;
const filePointer = await this.processAttachment({
attachment,
backupLevel,
2024-05-29 23:46:43 +00:00
messageReceivedAt,
});
return new Backups.MessageAttachment({
pointer: filePointer,
flag: this.getMessageAttachmentFlag(attachment),
2024-09-23 19:24:41 +00:00
wasDownloaded: isDownloaded(attachment),
clientUuid: clientUuid ? uuidToBytes(clientUuid) : undefined,
});
}
private async processAttachment({
attachment,
backupLevel,
2024-05-29 23:46:43 +00:00
messageReceivedAt,
}: {
attachment: AttachmentType;
backupLevel: BackupLevel;
2024-05-29 23:46:43 +00:00
messageReceivedAt: number;
}): Promise<Backups.FilePointer> {
2024-05-29 23:46:43 +00:00
const { filePointer, updatedAttachment } =
await getFilePointerForAttachment({
attachment,
backupLevel,
getBackupCdnInfo,
});
if (updatedAttachment) {
// TODO (DESKTOP-6688): ensure that we update the message/attachment in DB with the
// new keys so that we don't try to re-upload it again on the next export
}
2024-09-12 23:48:27 +00:00
// We don't download attachments during integration tests and thus have no
// "iv" for an attachment and can't create a job
2024-09-21 03:10:28 +00:00
if (this.backupType !== BackupType.TestOnlyPlaintext) {
2024-09-12 23:48:27 +00:00
const backupJob = await maybeGetBackupJobForAttachmentAndFilePointer({
attachment: updatedAttachment ?? attachment,
filePointer,
getBackupCdnInfo,
messageReceivedAt,
});
2024-05-29 23:46:43 +00:00
2024-09-12 23:48:27 +00:00
if (backupJob) {
this.attachmentBackupJobs.push(backupJob);
}
2024-05-29 23:46:43 +00:00
}
return filePointer;
}
2024-05-22 16:34:19 +00:00
private getMessageReactions({
reactions,
2024-06-03 17:02:25 +00:00
}: Pick<MessageAttributesType, 'reactions'>):
| Array<Backups.IReaction>
| undefined {
if (reactions == null) {
return undefined;
}
return reactions?.map((reaction, sortOrder) => {
2024-05-22 16:34:19 +00:00
return {
emoji: reaction.emoji,
authorId: this.getOrPushPrivateRecipient({
id: reaction.fromId,
}),
sentTimestamp: getSafeLongFromTimestamp(reaction.timestamp),
sortOrder: Long.fromNumber(sortOrder),
2024-05-22 16:34:19 +00:00
};
});
}
private getIncomingMessageDetails({
received_at_ms: receivedAtMs,
2024-06-03 17:02:25 +00:00
editMessageReceivedAtMs,
2024-05-22 16:34:19 +00:00
serverTimestamp,
readStatus,
unidentifiedDeliveryReceived,
2024-06-03 17:02:25 +00:00
}: Pick<
MessageAttributesType,
| 'received_at_ms'
| 'editMessageReceivedAtMs'
| 'serverTimestamp'
| 'readStatus'
| 'unidentifiedDeliveryReceived'
2024-06-03 17:02:25 +00:00
>): Backups.ChatItem.IIncomingMessageDetails {
const dateReceived = editMessageReceivedAtMs || receivedAtMs;
2024-05-22 16:34:19 +00:00
return {
dateReceived:
2024-06-03 17:02:25 +00:00
dateReceived != null ? getSafeLongFromTimestamp(dateReceived) : null,
2024-05-22 16:34:19 +00:00
dateServerSent:
serverTimestamp != null
? getSafeLongFromTimestamp(serverTimestamp)
: null,
2024-10-29 18:16:09 +00:00
read: readStatus === ReadStatus.Read || readStatus === ReadStatus.Viewed,
sealedSender: unidentifiedDeliveryReceived === true,
2024-05-22 16:34:19 +00:00
};
}
2024-06-03 17:02:25 +00:00
private getOutgoingMessageDetails(
sentAt: number,
{
sendStateByConversationId = {},
unidentifiedDeliveries = [],
errors = [],
}: Pick<
MessageAttributesType,
'sendStateByConversationId' | 'unidentifiedDeliveries' | 'errors'
>
2024-06-03 17:02:25 +00:00
): Backups.ChatItem.IOutgoingMessageDetails {
const sealedSenderServiceIds = new Set(unidentifiedDeliveries);
const errorMap = new Map(
errors.map(({ serviceId, name }) => {
return [serviceId, name];
})
);
const sendStatuses = new Array<Backups.ISendStatus>();
2024-05-22 16:34:19 +00:00
for (const [id, entry] of Object.entries(sendStateByConversationId)) {
const target = window.ConversationController.get(id);
if (!target) {
log.warn(`backups: no send target for a message ${sentAt}`);
continue;
}
const { serviceId } = target.attributes;
const recipientId = this.getOrPushPrivateRecipient(target.attributes);
const timestamp =
entry.updatedAt != null
? getSafeLongFromTimestamp(entry.updatedAt)
: null;
const sendStatus = new Backups.SendStatus({ recipientId, timestamp });
const sealedSender = serviceId
? sealedSenderServiceIds.has(serviceId)
: false;
2024-05-22 16:34:19 +00:00
switch (entry.status) {
case SendStatus.Pending:
sendStatus.pending = new Backups.SendStatus.Pending();
2024-05-22 16:34:19 +00:00
break;
case SendStatus.Sent:
sendStatus.sent = new Backups.SendStatus.Sent({
sealedSender,
});
2024-05-22 16:34:19 +00:00
break;
case SendStatus.Delivered:
sendStatus.delivered = new Backups.SendStatus.Delivered({
sealedSender,
});
2024-05-22 16:34:19 +00:00
break;
case SendStatus.Read:
sendStatus.read = new Backups.SendStatus.Read({
sealedSender,
});
2024-05-22 16:34:19 +00:00
break;
case SendStatus.Viewed:
sendStatus.viewed = new Backups.SendStatus.Viewed({
sealedSender,
});
2024-05-22 16:34:19 +00:00
break;
2024-09-12 23:48:27 +00:00
case SendStatus.Skipped:
sendStatus.skipped = {};
break;
case SendStatus.Failed: {
sendStatus.failed = new Backups.SendStatus.Failed();
if (!serviceId) {
break;
}
const errorName = errorMap.get(serviceId);
if (!errorName) {
break;
}
const identityKeyMismatch = errorName === 'OutgoingIdentityKeyError';
if (identityKeyMismatch) {
sendStatus.failed.reason =
Backups.SendStatus.Failed.FailureReason.IDENTITY_KEY_MISMATCH;
2024-09-12 23:48:27 +00:00
} else if (errorName === 'UnknownError') {
// See ts/backups/import.ts
sendStatus.failed.reason =
Backups.SendStatus.Failed.FailureReason.UNKNOWN;
} else {
sendStatus.failed.reason =
Backups.SendStatus.Failed.FailureReason.NETWORK;
}
2024-05-22 16:34:19 +00:00
break;
}
2024-05-22 16:34:19 +00:00
default:
throw missingCaseError(entry.status);
}
sendStatuses.push(sendStatus);
2024-05-22 16:34:19 +00:00
}
return {
sendStatus: sendStatuses,
2024-05-22 16:34:19 +00:00
};
}
2024-06-03 17:02:25 +00:00
2024-09-23 19:24:41 +00:00
private async toStandardMessage({
message,
backupLevel,
}: {
2024-06-03 17:02:25 +00:00
message: Pick<
MessageAttributesType,
| 'quote'
| 'attachments'
| 'body'
2024-09-23 19:24:41 +00:00
| 'bodyAttachment'
2024-06-03 17:02:25 +00:00
| 'bodyRanges'
| 'preview'
| 'reactions'
| 'received_at'
2024-09-23 19:24:41 +00:00
>;
backupLevel: BackupLevel;
}): Promise<Backups.IStandardMessage> {
2024-06-03 17:02:25 +00:00
return {
quote: await this.toQuote({
quote: message.quote,
backupLevel,
messageReceivedAt: message.received_at,
}),
2024-09-23 19:24:41 +00:00
attachments: message.attachments?.length
2024-06-03 17:02:25 +00:00
? await Promise.all(
message.attachments.map(attachment => {
return this.processMessageAttachment({
attachment,
backupLevel,
messageReceivedAt: message.received_at,
});
})
)
: undefined,
2024-09-23 19:24:41 +00:00
longText: message.bodyAttachment
? await this.processAttachment({
attachment: message.bodyAttachment,
backupLevel,
messageReceivedAt: message.received_at,
})
: undefined,
2024-09-16 21:30:39 +00:00
text:
message.body != null
? {
body: message.body?.slice(0, LONG_ATTACHMENT_LIMIT),
bodyRanges: message.bodyRanges?.map(range =>
this.toBodyRange(range)
),
}
: undefined,
2024-06-03 17:02:25 +00:00
linkPreview: message.preview
? await Promise.all(
message.preview.map(async preview => {
return {
url: preview.url,
title: preview.title,
description: preview.description,
date: getSafeLongFromTimestamp(preview.date),
image: preview.image
? await this.processAttachment({
attachment: preview.image,
backupLevel,
messageReceivedAt: message.received_at,
})
: undefined,
};
})
)
: undefined,
reactions: this.getMessageReactions(message),
};
}
2024-10-29 18:16:09 +00:00
private async toViewOnceMessage({
message,
backupLevel,
}: {
message: Pick<
MessageAttributesType,
'attachments' | 'received_at' | 'reactions'
>;
backupLevel: BackupLevel;
}): Promise<Backups.IViewOnceMessage> {
const attachment = message.attachments?.at(0);
return {
attachment:
attachment == null
? null
: await this.processMessageAttachment({
attachment,
backupLevel,
messageReceivedAt: message.received_at,
}),
reactions: this.getMessageReactions(message),
};
}
2024-06-03 17:02:25 +00:00
private async toChatItemRevisions(
parent: Backups.IChatItem,
message: MessageAttributesType,
backupLevel: BackupLevel
): Promise<Array<Backups.IChatItem> | undefined> {
const { editHistory } = message;
if (editHistory == null) {
return undefined;
}
const isOutgoing = message.type === 'outgoing';
return Promise.all(
editHistory
// The first history is the copy of the current message
.slice(1)
.map(async history => {
return {
// Required fields
chatId: parent.chatId,
authorId: parent.authorId,
dateSent: getSafeLongFromTimestamp(history.timestamp),
expireStartDate: parent.expireStartDate,
expiresInMs: parent.expiresInMs,
sms: parent.sms,
// Directional details
outgoing: isOutgoing
? this.getOutgoingMessageDetails(history.timestamp, history)
: undefined,
incoming: isOutgoing
? undefined
: this.getIncomingMessageDetails(history),
// Message itself
2024-09-23 19:24:41 +00:00
standardMessage: await this.toStandardMessage({
message: history,
backupLevel,
}),
2024-06-03 17:02:25 +00:00
};
// Backups use oldest to newest order
})
.reverse()
);
}
2024-07-15 20:58:55 +00:00
private toCustomChatColors(): Array<Backups.ChatStyle.ICustomChatColor> {
const customColors = window.storage.get('customColors');
if (!customColors) {
return [];
}
const { order = [] } = customColors;
const map = new Map(
order
.map((id: string): [string, CustomColorType] | undefined => {
const color = customColors.colors[id];
if (color == null) {
return undefined;
}
return [id, color];
})
.filter(isNotNil)
);
// Add colors not present in the order list
2024-07-15 20:58:55 +00:00
for (const [uuid, color] of Object.entries(customColors.colors)) {
if (map.has(uuid)) {
continue;
}
map.set(uuid, color);
}
const result = new Array<Backups.ChatStyle.ICustomChatColor>();
for (const [uuid, color] of map.entries()) {
2024-09-12 23:48:27 +00:00
const id = Long.fromNumber(result.length + 1);
2024-07-15 20:58:55 +00:00
this.customColorIdByUuid.set(uuid, id);
2024-09-03 17:18:15 +00:00
const start = hslToRGBInt(
color.start.hue,
color.start.saturation,
color.start.luminance
);
2024-07-15 20:58:55 +00:00
if (color.end == null) {
result.push({
id,
solid: start,
});
} else {
2024-09-03 17:18:15 +00:00
const end = hslToRGBInt(
color.end.hue,
color.end.saturation,
color.end.luminance
);
2024-07-15 20:58:55 +00:00
result.push({
id,
gradient: {
colors: [start, end],
positions: [0, 1],
angle: color.deg,
},
});
}
}
return result;
}
2024-09-12 23:48:27 +00:00
private toDefaultChatStyle(): Backups.IChatStyle | null {
2024-07-15 20:58:55 +00:00
const defaultColor = window.storage.get('defaultConversationColor');
2024-09-12 23:48:27 +00:00
const wallpaperPhotoPointer = window.storage.get(
'defaultWallpaperPhotoPointer'
);
const wallpaperPreset = window.storage.get('defaultWallpaperPreset');
const dimWallpaperInDarkMode = window.storage.get(
'defaultDimWallpaperInDarkMode',
false
);
const autoBubbleColor = window.storage.get('defaultAutoBubbleColor');
2024-07-15 20:58:55 +00:00
return this.toChatStyle({
2024-09-12 23:48:27 +00:00
wallpaperPhotoPointer,
wallpaperPreset,
2024-07-15 20:58:55 +00:00
color: defaultColor?.color,
customColorId: defaultColor?.customColorData?.id,
2024-09-12 23:48:27 +00:00
dimWallpaperInDarkMode,
autoBubbleColor,
2024-07-15 20:58:55 +00:00
});
}
private toChatStyle({
wallpaperPhotoPointer,
wallpaperPreset,
color,
customColorId,
2024-07-25 14:58:51 +00:00
dimWallpaperInDarkMode,
2024-09-12 23:48:27 +00:00
autoBubbleColor,
}: LocalChatStyle): Backups.IChatStyle | null {
2024-07-25 14:58:51 +00:00
const result: Backups.IChatStyle = {
dimWallpaperInDarkMode,
};
2024-07-15 20:58:55 +00:00
2024-09-12 23:48:27 +00:00
// The defaults
if (
(color == null || color === 'ultramarine') &&
wallpaperPhotoPointer == null &&
wallpaperPreset == null &&
!dimWallpaperInDarkMode &&
(autoBubbleColor === true || autoBubbleColor == null)
) {
return null;
}
2024-07-15 20:58:55 +00:00
if (Bytes.isNotEmpty(wallpaperPhotoPointer)) {
result.wallpaperPhoto = Backups.FilePointer.decode(wallpaperPhotoPointer);
} else if (wallpaperPreset) {
result.wallpaperPreset = wallpaperPreset;
}
2024-09-12 23:48:27 +00:00
if (color == null || autoBubbleColor) {
2024-07-15 20:58:55 +00:00
result.autoBubbleColor = {};
return result;
}
if (color === 'custom') {
strictAssert(
customColorId != null,
'No custom color id for custom color'
);
const index = this.customColorIdByUuid.get(customColorId);
strictAssert(index != null, 'Missing custom color');
result.customColorId = index;
return result;
}
const { BubbleColorPreset } = Backups.ChatStyle;
switch (color) {
case 'ultramarine':
result.bubbleColorPreset = BubbleColorPreset.SOLID_ULTRAMARINE;
break;
case 'crimson':
result.bubbleColorPreset = BubbleColorPreset.SOLID_CRIMSON;
break;
case 'vermilion':
result.bubbleColorPreset = BubbleColorPreset.SOLID_VERMILION;
break;
case 'burlap':
result.bubbleColorPreset = BubbleColorPreset.SOLID_BURLAP;
break;
case 'forest':
result.bubbleColorPreset = BubbleColorPreset.SOLID_FOREST;
break;
case 'wintergreen':
result.bubbleColorPreset = BubbleColorPreset.SOLID_WINTERGREEN;
break;
case 'teal':
result.bubbleColorPreset = BubbleColorPreset.SOLID_TEAL;
break;
case 'blue':
result.bubbleColorPreset = BubbleColorPreset.SOLID_BLUE;
break;
case 'indigo':
result.bubbleColorPreset = BubbleColorPreset.SOLID_INDIGO;
break;
case 'violet':
result.bubbleColorPreset = BubbleColorPreset.SOLID_VIOLET;
break;
case 'plum':
result.bubbleColorPreset = BubbleColorPreset.SOLID_PLUM;
break;
case 'taupe':
result.bubbleColorPreset = BubbleColorPreset.SOLID_TAUPE;
break;
case 'steel':
result.bubbleColorPreset = BubbleColorPreset.SOLID_STEEL;
break;
case 'ember':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_EMBER;
break;
case 'midnight':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_MIDNIGHT;
break;
case 'infrared':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_INFRARED;
break;
case 'lagoon':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_LAGOON;
break;
case 'fluorescent':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_FLUORESCENT;
break;
case 'basil':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_BASIL;
break;
case 'sublime':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_SUBLIME;
break;
case 'sea':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_SEA;
break;
case 'tangerine':
result.bubbleColorPreset = BubbleColorPreset.GRADIENT_TANGERINE;
break;
default:
throw missingCaseError(color);
}
return result;
}
2024-03-15 14:20:33 +00:00
}
2024-04-30 13:24:21 +00:00
function checkServiceIdEquivalence(
left: ServiceIdString | undefined,
right: ServiceIdString | undefined
) {
const leftConvo = window.ConversationController.get(left);
const rightConvo = window.ConversationController.get(right);
return leftConvo && rightConvo && leftConvo === rightConvo;
}
2024-07-15 20:58:55 +00:00
2024-09-03 17:18:15 +00:00
function hslToRGBInt(hue: number, saturation: number, luminance = 1): number {
const { r, g, b } = hslToRGB(hue, saturation, luminance);
2024-07-15 20:58:55 +00:00
// eslint-disable-next-line no-bitwise
return ((0xff << 24) | (r << 16) | (g << 8) | b) >>> 0;
}
2024-08-13 18:39:04 +00:00
function toGroupCallStateProto(state: CallStatus): Backups.GroupCall.State {
const values = Backups.GroupCall.State;
if (state === GroupCallStatus.GenericGroupCall) {
return values.GENERIC;
}
if (state === GroupCallStatus.OutgoingRing) {
return values.OUTGOING_RING;
}
if (state === GroupCallStatus.Ringing) {
return values.RINGING;
}
if (state === GroupCallStatus.Joined) {
return values.JOINED;
}
if (state === GroupCallStatus.Accepted) {
return values.ACCEPTED;
}
if (state === GroupCallStatus.Missed) {
return values.MISSED;
}
if (state === GroupCallStatus.MissedNotificationProfile) {
return values.MISSED_NOTIFICATION_PROFILE;
}
if (state === GroupCallStatus.Declined) {
return values.DECLINED;
}
if (state === GroupCallStatus.Deleted) {
throw new Error(
'groupCallStatusToGroupCallState: Never back up deleted items!'
);
}
return values.UNKNOWN_STATE;
}
function toIndividualCallDirectionProto(
direction: CallDirection
): Backups.IndividualCall.Direction {
const values = Backups.IndividualCall.Direction;
if (direction === CallDirection.Incoming) {
return values.INCOMING;
}
if (direction === CallDirection.Outgoing) {
return values.OUTGOING;
}
return values.UNKNOWN_DIRECTION;
}
function toIndividualCallTypeProto(
type: CallType
): Backups.IndividualCall.Type {
const values = Backups.IndividualCall.Type;
if (type === CallType.Audio) {
return values.AUDIO_CALL;
}
if (type === CallType.Video) {
return values.VIDEO_CALL;
}
return values.UNKNOWN_TYPE;
}
function toIndividualCallStateProto(
status: CallStatus
): Backups.IndividualCall.State {
const values = Backups.IndividualCall.State;
if (status === DirectCallStatus.Accepted) {
return values.ACCEPTED;
}
if (status === DirectCallStatus.Declined) {
return values.NOT_ACCEPTED;
}
if (status === DirectCallStatus.Missed) {
return values.MISSED;
}
if (status === DirectCallStatus.MissedNotificationProfile) {
return values.MISSED_NOTIFICATION_PROFILE;
}
if (status === DirectCallStatus.Deleted) {
throw new Error(
'statusToIndividualCallProtoEnum: Never back up deleted items!'
);
}
return values.UNKNOWN_STATE;
}
function toAdHocCallStateProto(status: CallStatus): Backups.AdHocCall.State {
const values = Backups.AdHocCall.State;
if (status === AdhocCallStatus.Generic) {
return values.GENERIC;
}
if (status === AdhocCallStatus.Joined) {
return values.GENERIC;
}
if (status === AdhocCallStatus.Pending) {
return values.GENERIC;
}
return values.UNKNOWN_STATE;
}
function toCallLinkRestrictionsProto(
restrictions: CallLinkRestrictions
): Backups.CallLink.Restrictions {
const values = Backups.CallLink.Restrictions;
if (restrictions === CallLinkRestrictions.None) {
return values.NONE;
}
if (restrictions === CallLinkRestrictions.AdminApproval) {
return values.ADMIN_APPROVAL;
}
return values.UNKNOWN;
}