2021-01-14 18:07:05 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
/* eslint-disable no-nested-ternary */
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
/* eslint-disable no-bitwise */
|
|
|
|
/* eslint-disable max-classes-per-file */
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Dictionary } from 'lodash';
|
2021-09-22 00:58:03 +00:00
|
|
|
import Long from 'long';
|
2020-04-13 17:37:29 +00:00
|
|
|
import PQueue from 'p-queue';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { PlaintextContent } from '@signalapp/signal-client';
|
2021-05-25 22:40:04 +00:00
|
|
|
import {
|
|
|
|
ProtocolAddress,
|
|
|
|
SenderKeyDistributionMessage,
|
|
|
|
} from '@signalapp/signal-client';
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
import { assert } from '../util/assert';
|
2021-05-25 22:40:04 +00:00
|
|
|
import { parseIntOrThrow } from '../util/parseIntOrThrow';
|
2021-09-10 02:38:11 +00:00
|
|
|
import { Address } from '../types/Address';
|
|
|
|
import { QualifiedAddress } from '../types/QualifiedAddress';
|
2021-05-25 22:40:04 +00:00
|
|
|
import { SenderKeys } from '../LibSignalStores';
|
2021-10-05 22:10:08 +00:00
|
|
|
import type { LinkPreviewType } from '../types/message/LinkPreviews';
|
|
|
|
import { MIMETypeToString } from '../types/MIME';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type * as Attachment from '../types/Attachment';
|
2021-11-08 23:32:31 +00:00
|
|
|
import type { UUIDStringType } from '../types/UUID';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2021-07-19 19:26:06 +00:00
|
|
|
ChallengeType,
|
2020-09-09 02:25:05 +00:00
|
|
|
GroupCredentialsType,
|
|
|
|
GroupLogResponseType,
|
2021-07-19 19:26:06 +00:00
|
|
|
MultiRecipient200ResponseType,
|
|
|
|
ProfileRequestDataType,
|
2020-09-09 02:25:05 +00:00
|
|
|
ProxiedRequestOptionsType,
|
2021-07-19 19:26:06 +00:00
|
|
|
UploadAvatarHeadersType,
|
2020-09-09 02:25:05 +00:00
|
|
|
WebAPIType,
|
|
|
|
} from './WebAPI';
|
2020-04-13 17:37:29 +00:00
|
|
|
import createTaskWithTimeout from './TaskWithTimeout';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { CallbackResultType } from './Types.d';
|
|
|
|
import type {
|
2021-07-15 23:48:09 +00:00
|
|
|
SerializedCertificateType,
|
|
|
|
SendLogCallbackType,
|
|
|
|
} from './OutgoingMessage';
|
2021-10-26 19:15:33 +00:00
|
|
|
import OutgoingMessage from './OutgoingMessage';
|
2021-07-09 19:36:10 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2021-09-24 00:49:05 +00:00
|
|
|
import { getRandomBytes, getZeroes, encryptAttachment } from '../Crypto';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2020-07-07 00:56:56 +00:00
|
|
|
StorageServiceCallOptionsType,
|
|
|
|
StorageServiceCredentials,
|
2020-04-13 17:37:29 +00:00
|
|
|
} from '../textsecure.d';
|
2021-07-20 01:10:09 +00:00
|
|
|
import {
|
|
|
|
MessageError,
|
|
|
|
SignedPreKeyRotationError,
|
|
|
|
SendMessageProtoError,
|
2021-09-22 00:58:03 +00:00
|
|
|
HTTPError,
|
2021-07-20 01:10:09 +00:00
|
|
|
} from './Errors';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { BodyRangesType } from '../types/Util';
|
|
|
|
import type {
|
2020-09-28 23:46:31 +00:00
|
|
|
LinkPreviewImage,
|
|
|
|
LinkPreviewMetadata,
|
|
|
|
} from '../linkPreviews/linkPreviewFetch';
|
2021-07-19 22:44:49 +00:00
|
|
|
import { concat, isEmpty, map } from '../util/iterables';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SendTypesType } from '../util/handleMessageSend';
|
|
|
|
import { handleMessageSend, shouldSaveProto } from '../util/handleMessageSend';
|
2021-06-22 14:46:42 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
export type SendMetadataType = {
|
|
|
|
[identifier: string]: {
|
|
|
|
accessKey: string;
|
2021-04-05 20:38:36 +00:00
|
|
|
senderCertificate?: SerializedCertificateType;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SendOptionsType = {
|
|
|
|
sendMetadata?: SendMetadataType;
|
|
|
|
online?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type QuoteAttachmentType = {
|
|
|
|
thumbnail?: AttachmentType;
|
2021-07-09 19:36:10 +00:00
|
|
|
attachmentPointer?: Proto.IAttachmentPointer;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2020-10-06 17:06:34 +00:00
|
|
|
export type GroupV2InfoType = {
|
2021-06-22 14:46:42 +00:00
|
|
|
groupChange?: Uint8Array;
|
|
|
|
masterKey: Uint8Array;
|
2020-09-09 02:25:05 +00:00
|
|
|
revision: number;
|
|
|
|
members: Array<string>;
|
|
|
|
};
|
2021-08-23 23:15:34 +00:00
|
|
|
export type GroupV1InfoType = {
|
2020-09-09 02:25:05 +00:00
|
|
|
id: string;
|
|
|
|
members: Array<string>;
|
|
|
|
};
|
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type GroupCallUpdateType = {
|
2020-12-02 01:49:08 +00:00
|
|
|
eraId: string;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-12-02 01:49:08 +00:00
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
export type StickerType = {
|
|
|
|
packId: string;
|
|
|
|
stickerId: number;
|
|
|
|
packKey: string;
|
|
|
|
data: Readonly<AttachmentType>;
|
|
|
|
emoji?: string;
|
|
|
|
|
|
|
|
attachmentPointer?: Proto.IAttachmentPointer;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type QuoteType = {
|
|
|
|
id?: number;
|
|
|
|
authorUuid?: string;
|
|
|
|
text?: string;
|
|
|
|
attachments?: Array<AttachmentType>;
|
|
|
|
bodyRanges?: BodyRangesType;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ReactionType = {
|
|
|
|
emoji?: string;
|
|
|
|
remove?: boolean;
|
|
|
|
targetAuthorUuid?: string;
|
|
|
|
targetTimestamp?: number;
|
|
|
|
};
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
export type AttachmentType = {
|
|
|
|
size: number;
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Uint8Array;
|
2021-05-25 22:40:04 +00:00
|
|
|
contentType: string;
|
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
fileName?: string;
|
|
|
|
flags?: number;
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
|
|
|
caption?: string;
|
2021-05-25 22:40:04 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
attachmentPointer?: Proto.IAttachmentPointer;
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
blurHash?: string;
|
|
|
|
};
|
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
function makeAttachmentSendReady(
|
|
|
|
attachment: Attachment.AttachmentType
|
|
|
|
): AttachmentType | undefined {
|
|
|
|
const { data } = attachment;
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
throw new Error(
|
|
|
|
'makeAttachmentSendReady: Missing data, returning undefined'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...attachment,
|
|
|
|
contentType: MIMETypeToString(attachment.contentType),
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
export type MessageOptionsType = {
|
2021-07-29 19:00:11 +00:00
|
|
|
attachments?: ReadonlyArray<AttachmentType> | null;
|
2020-04-13 17:37:29 +00:00
|
|
|
body?: string;
|
|
|
|
expireTimer?: number;
|
|
|
|
flags?: number;
|
|
|
|
group?: {
|
|
|
|
id: string;
|
|
|
|
type: number;
|
|
|
|
};
|
2020-09-09 02:25:05 +00:00
|
|
|
groupV2?: GroupV2InfoType;
|
2020-04-13 17:37:29 +00:00
|
|
|
needsSync?: boolean;
|
2021-10-05 22:10:08 +00:00
|
|
|
preview?: ReadonlyArray<LinkPreviewType>;
|
2021-09-24 00:49:05 +00:00
|
|
|
profileKey?: Uint8Array;
|
2021-10-05 22:10:08 +00:00
|
|
|
quote?: QuoteType;
|
2021-07-29 19:00:11 +00:00
|
|
|
recipients: ReadonlyArray<string>;
|
2021-10-05 22:10:08 +00:00
|
|
|
sticker?: StickerType;
|
|
|
|
reaction?: ReactionType;
|
2020-09-29 22:55:56 +00:00
|
|
|
deletedForEveryoneTimestamp?: number;
|
2020-04-13 17:37:29 +00:00
|
|
|
timestamp: number;
|
2020-11-03 01:19:52 +00:00
|
|
|
mentions?: BodyRangesType;
|
2020-12-02 01:49:08 +00:00
|
|
|
groupCallUpdate?: GroupCallUpdateType;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
2021-05-25 22:40:04 +00:00
|
|
|
export type GroupSendOptionsType = {
|
|
|
|
attachments?: Array<AttachmentType>;
|
|
|
|
expireTimer?: number;
|
|
|
|
groupV2?: GroupV2InfoType;
|
|
|
|
groupV1?: GroupV1InfoType;
|
|
|
|
messageText?: string;
|
2021-10-05 22:10:08 +00:00
|
|
|
preview?: ReadonlyArray<LinkPreviewType>;
|
2021-09-24 00:49:05 +00:00
|
|
|
profileKey?: Uint8Array;
|
2021-10-05 22:10:08 +00:00
|
|
|
quote?: QuoteType;
|
|
|
|
reaction?: ReactionType;
|
|
|
|
sticker?: StickerType;
|
2021-05-25 22:40:04 +00:00
|
|
|
deletedForEveryoneTimestamp?: number;
|
|
|
|
timestamp: number;
|
|
|
|
mentions?: BodyRangesType;
|
|
|
|
groupCallUpdate?: GroupCallUpdateType;
|
|
|
|
};
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
class Message {
|
2021-10-05 22:10:08 +00:00
|
|
|
attachments: ReadonlyArray<AttachmentType>;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
body?: string;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
expireTimer?: number;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
flags?: number;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
group?: {
|
|
|
|
id: string;
|
|
|
|
type: number;
|
|
|
|
};
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
groupV2?: GroupV2InfoType;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
needsSync?: boolean;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
preview?: ReadonlyArray<LinkPreviewType>;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
profileKey?: Uint8Array;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
quote?: QuoteType;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-29 19:00:11 +00:00
|
|
|
recipients: ReadonlyArray<string>;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
sticker?: StickerType;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
reaction?: ReactionType;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
timestamp: number;
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
dataMessage?: Proto.DataMessage;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
attachmentPointers: Array<Proto.IAttachmentPointer> = [];
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2020-09-29 22:55:56 +00:00
|
|
|
deletedForEveryoneTimestamp?: number;
|
|
|
|
|
2020-11-03 01:19:52 +00:00
|
|
|
mentions?: BodyRangesType;
|
|
|
|
|
2020-12-02 01:49:08 +00:00
|
|
|
groupCallUpdate?: GroupCallUpdateType;
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
constructor(options: MessageOptionsType) {
|
|
|
|
this.attachments = options.attachments || [];
|
|
|
|
this.body = options.body;
|
|
|
|
this.expireTimer = options.expireTimer;
|
|
|
|
this.flags = options.flags;
|
|
|
|
this.group = options.group;
|
2020-09-09 02:25:05 +00:00
|
|
|
this.groupV2 = options.groupV2;
|
2020-04-13 17:37:29 +00:00
|
|
|
this.needsSync = options.needsSync;
|
|
|
|
this.preview = options.preview;
|
|
|
|
this.profileKey = options.profileKey;
|
|
|
|
this.quote = options.quote;
|
|
|
|
this.recipients = options.recipients;
|
|
|
|
this.sticker = options.sticker;
|
|
|
|
this.reaction = options.reaction;
|
|
|
|
this.timestamp = options.timestamp;
|
2020-09-29 22:55:56 +00:00
|
|
|
this.deletedForEveryoneTimestamp = options.deletedForEveryoneTimestamp;
|
2020-11-03 01:19:52 +00:00
|
|
|
this.mentions = options.mentions;
|
2020-12-02 01:49:08 +00:00
|
|
|
this.groupCallUpdate = options.groupCallUpdate;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
if (!(this.recipients instanceof Array)) {
|
|
|
|
throw new Error('Invalid recipient list');
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
if (!this.group && !this.groupV2 && this.recipients.length !== 1) {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new Error('Invalid recipient list for non-group');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.timestamp !== 'number') {
|
|
|
|
throw new Error('Invalid timestamp');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.expireTimer !== undefined && this.expireTimer !== null) {
|
|
|
|
if (typeof this.expireTimer !== 'number' || !(this.expireTimer >= 0)) {
|
|
|
|
throw new Error('Invalid expireTimer');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.attachments) {
|
|
|
|
if (!(this.attachments instanceof Array)) {
|
|
|
|
throw new Error('Invalid message attachments');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.flags !== undefined) {
|
|
|
|
if (typeof this.flags !== 'number') {
|
|
|
|
throw new Error('Invalid message flags');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.isEndSession()) {
|
|
|
|
if (
|
|
|
|
this.body !== null ||
|
|
|
|
this.group !== null ||
|
|
|
|
this.attachments.length !== 0
|
|
|
|
) {
|
|
|
|
throw new Error('Invalid end session message');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
typeof this.timestamp !== 'number' ||
|
|
|
|
(this.body && typeof this.body !== 'string')
|
|
|
|
) {
|
|
|
|
throw new Error('Invalid message body');
|
|
|
|
}
|
|
|
|
if (this.group) {
|
|
|
|
if (
|
|
|
|
typeof this.group.id !== 'string' ||
|
|
|
|
typeof this.group.type !== 'number'
|
|
|
|
) {
|
|
|
|
throw new Error('Invalid group context');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isEndSession() {
|
2021-07-09 19:36:10 +00:00
|
|
|
return (this.flags || 0) & Proto.DataMessage.Flags.END_SESSION;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
toProto(): Proto.DataMessage {
|
2021-09-24 00:49:05 +00:00
|
|
|
if (this.dataMessage) {
|
2020-04-13 17:37:29 +00:00
|
|
|
return this.dataMessage;
|
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
const proto = new Proto.DataMessage();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
proto.timestamp = this.timestamp;
|
|
|
|
proto.attachments = this.attachmentPointers;
|
|
|
|
|
|
|
|
if (this.body) {
|
|
|
|
proto.body = this.body;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
const mentionCount = this.mentions ? this.mentions.length : 0;
|
|
|
|
const placeholders = this.body.match(/\uFFFC/g);
|
|
|
|
const placeholderCount = placeholders ? placeholders.length : 0;
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-11-03 01:19:52 +00:00
|
|
|
`Sending a message with ${mentionCount} mentions and ${placeholderCount} placeholders`
|
|
|
|
);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
if (this.flags) {
|
|
|
|
proto.flags = this.flags;
|
|
|
|
}
|
2020-09-09 02:25:05 +00:00
|
|
|
if (this.groupV2) {
|
2021-07-09 19:36:10 +00:00
|
|
|
proto.groupV2 = new Proto.GroupContextV2();
|
2020-09-09 02:25:05 +00:00
|
|
|
proto.groupV2.masterKey = this.groupV2.masterKey;
|
|
|
|
proto.groupV2.revision = this.groupV2.revision;
|
|
|
|
proto.groupV2.groupChange = this.groupV2.groupChange || null;
|
|
|
|
} else if (this.group) {
|
2021-07-09 19:36:10 +00:00
|
|
|
proto.group = new Proto.GroupContext();
|
|
|
|
proto.group.id = Bytes.fromString(this.group.id);
|
2020-04-13 17:37:29 +00:00
|
|
|
proto.group.type = this.group.type;
|
|
|
|
}
|
|
|
|
if (this.sticker) {
|
2021-07-09 19:36:10 +00:00
|
|
|
proto.sticker = new Proto.DataMessage.Sticker();
|
|
|
|
proto.sticker.packId = Bytes.fromHex(this.sticker.packId);
|
|
|
|
proto.sticker.packKey = Bytes.fromBase64(this.sticker.packKey);
|
2020-04-13 17:37:29 +00:00
|
|
|
proto.sticker.stickerId = this.sticker.stickerId;
|
2021-10-05 22:10:08 +00:00
|
|
|
proto.sticker.emoji = this.sticker.emoji;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
if (this.sticker.attachmentPointer) {
|
|
|
|
proto.sticker.data = this.sticker.attachmentPointer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.reaction) {
|
2021-07-09 19:36:10 +00:00
|
|
|
proto.reaction = new Proto.DataMessage.Reaction();
|
2020-11-06 19:06:21 +00:00
|
|
|
proto.reaction.emoji = this.reaction.emoji || null;
|
2020-11-12 03:03:56 +00:00
|
|
|
proto.reaction.remove = this.reaction.remove || false;
|
2020-11-06 19:06:21 +00:00
|
|
|
proto.reaction.targetAuthorUuid = this.reaction.targetAuthorUuid || null;
|
|
|
|
proto.reaction.targetTimestamp = this.reaction.targetTimestamp || null;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-11-06 19:06:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
if (Array.isArray(this.preview)) {
|
|
|
|
proto.preview = this.preview.map(preview => {
|
2021-07-09 19:36:10 +00:00
|
|
|
const item = new Proto.DataMessage.Preview();
|
2020-04-13 17:37:29 +00:00
|
|
|
item.title = preview.title;
|
|
|
|
item.url = preview.url;
|
2020-09-28 23:46:31 +00:00
|
|
|
item.description = preview.description || null;
|
|
|
|
item.date = preview.date || null;
|
2021-10-05 22:10:08 +00:00
|
|
|
if (preview.attachmentPointer) {
|
|
|
|
item.image = preview.attachmentPointer;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.quote) {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { QuotedAttachment } = Proto.DataMessage.Quote;
|
|
|
|
const { BodyRange, Quote } = Proto.DataMessage;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
proto.quote = new Quote();
|
|
|
|
const { quote } = proto;
|
|
|
|
|
2020-11-06 19:06:21 +00:00
|
|
|
quote.id = this.quote.id || null;
|
|
|
|
quote.authorUuid = this.quote.authorUuid || null;
|
|
|
|
quote.text = this.quote.text || null;
|
2020-04-13 17:37:29 +00:00
|
|
|
quote.attachments = (this.quote.attachments || []).map(
|
|
|
|
(attachment: AttachmentType) => {
|
|
|
|
const quotedAttachment = new QuotedAttachment();
|
|
|
|
|
|
|
|
quotedAttachment.contentType = attachment.contentType;
|
2021-10-05 22:10:08 +00:00
|
|
|
if (attachment.fileName) {
|
|
|
|
quotedAttachment.fileName = attachment.fileName;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
if (attachment.attachmentPointer) {
|
|
|
|
quotedAttachment.thumbnail = attachment.attachmentPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return quotedAttachment;
|
|
|
|
}
|
|
|
|
);
|
2020-09-16 22:42:48 +00:00
|
|
|
const bodyRanges: BodyRangesType = this.quote.bodyRanges || [];
|
|
|
|
quote.bodyRanges = bodyRanges.map(range => {
|
|
|
|
const bodyRange = new BodyRange();
|
|
|
|
bodyRange.start = range.start;
|
|
|
|
bodyRange.length = range.length;
|
2021-07-09 19:36:10 +00:00
|
|
|
if (range.mentionUuid !== undefined) {
|
|
|
|
bodyRange.mentionUuid = range.mentionUuid;
|
|
|
|
}
|
2020-09-16 22:42:48 +00:00
|
|
|
return bodyRange;
|
|
|
|
});
|
2020-09-16 23:34:01 +00:00
|
|
|
if (
|
|
|
|
quote.bodyRanges.length &&
|
|
|
|
(!proto.requiredProtocolVersion ||
|
|
|
|
proto.requiredProtocolVersion <
|
2021-07-09 19:36:10 +00:00
|
|
|
Proto.DataMessage.ProtocolVersion.MENTIONS)
|
2020-09-16 23:34:01 +00:00
|
|
|
) {
|
|
|
|
proto.requiredProtocolVersion =
|
2021-07-09 19:36:10 +00:00
|
|
|
Proto.DataMessage.ProtocolVersion.MENTIONS;
|
2020-09-16 23:34:01 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
if (this.expireTimer) {
|
|
|
|
proto.expireTimer = this.expireTimer;
|
|
|
|
}
|
|
|
|
if (this.profileKey) {
|
2021-09-24 00:49:05 +00:00
|
|
|
proto.profileKey = this.profileKey;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-29 22:55:56 +00:00
|
|
|
if (this.deletedForEveryoneTimestamp) {
|
|
|
|
proto.delete = {
|
|
|
|
targetSentTimestamp: this.deletedForEveryoneTimestamp,
|
|
|
|
};
|
|
|
|
}
|
2020-11-03 01:19:52 +00:00
|
|
|
if (this.mentions) {
|
|
|
|
proto.requiredProtocolVersion =
|
2021-07-09 19:36:10 +00:00
|
|
|
Proto.DataMessage.ProtocolVersion.MENTIONS;
|
2020-11-03 01:19:52 +00:00
|
|
|
proto.bodyRanges = this.mentions.map(
|
|
|
|
({ start, length, mentionUuid }) => ({
|
|
|
|
start,
|
|
|
|
length,
|
|
|
|
mentionUuid,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2020-12-02 01:49:08 +00:00
|
|
|
if (this.groupCallUpdate) {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { GroupCallUpdate } = Proto.DataMessage;
|
2020-12-02 01:49:08 +00:00
|
|
|
|
|
|
|
const groupCallUpdate = new GroupCallUpdate();
|
|
|
|
groupCallUpdate.eraId = this.groupCallUpdate.eraId;
|
|
|
|
|
|
|
|
proto.groupCallUpdate = groupCallUpdate;
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
this.dataMessage = proto;
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
encode() {
|
|
|
|
return Proto.DataMessage.encode(this.toProto()).finish();
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class MessageSender {
|
|
|
|
pendingMessages: {
|
|
|
|
[id: string]: PQueue;
|
|
|
|
};
|
|
|
|
|
2021-07-23 17:23:50 +00:00
|
|
|
constructor(public readonly server: WebAPIType) {
|
2020-04-13 17:37:29 +00:00
|
|
|
this.pendingMessages = {};
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
async queueJobForIdentifier<T>(
|
2021-05-25 22:40:04 +00:00
|
|
|
identifier: string,
|
2021-10-05 22:10:08 +00:00
|
|
|
runJob: () => Promise<T>
|
|
|
|
): Promise<T> {
|
2021-05-25 22:40:04 +00:00
|
|
|
const { id } = await window.ConversationController.getOrCreateAndWait(
|
|
|
|
identifier,
|
|
|
|
'private'
|
|
|
|
);
|
|
|
|
this.pendingMessages[id] =
|
|
|
|
this.pendingMessages[id] || new PQueue({ concurrency: 1 });
|
|
|
|
|
|
|
|
const queue = this.pendingMessages[id];
|
|
|
|
|
|
|
|
const taskWithTimeout = createTaskWithTimeout(
|
|
|
|
runJob,
|
|
|
|
`queueJobForIdentifier ${identifier} ${id}`
|
|
|
|
);
|
|
|
|
|
|
|
|
return queue.add(taskWithTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attachment upload functions
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
_getAttachmentSizeBucket(size: number): number {
|
2020-04-13 17:37:29 +00:00
|
|
|
return Math.max(
|
|
|
|
541,
|
|
|
|
Math.floor(1.05 ** Math.ceil(Math.log(size) / Math.log(1.05)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
getRandomPadding(): Uint8Array {
|
2021-05-25 22:40:04 +00:00
|
|
|
// Generate a random int from 1 and 512
|
|
|
|
const buffer = getRandomBytes(2);
|
|
|
|
const paddingLength = (new Uint16Array(buffer)[0] & 0x1ff) + 1;
|
|
|
|
|
|
|
|
// Generate a random padding buffer of the chosen size
|
2021-09-24 00:49:05 +00:00
|
|
|
return getRandomBytes(paddingLength);
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
getPaddedAttachment(data: Readonly<Uint8Array>): Uint8Array {
|
2020-04-13 17:37:29 +00:00
|
|
|
const size = data.byteLength;
|
|
|
|
const paddedSize = this._getAttachmentSizeBucket(size);
|
2020-09-09 02:25:05 +00:00
|
|
|
const padding = getZeroes(paddedSize - size);
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return Bytes.concatenate([data, padding]);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async makeAttachmentPointer(
|
2021-07-29 19:00:11 +00:00
|
|
|
attachment: Readonly<AttachmentType>
|
2021-07-09 19:36:10 +00:00
|
|
|
): Promise<Proto.IAttachmentPointer> {
|
|
|
|
assert(
|
|
|
|
typeof attachment === 'object' && attachment !== null,
|
|
|
|
'Got null attachment in `makeAttachmentPointer`'
|
|
|
|
);
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
const { data, size } = attachment;
|
2021-09-24 00:49:05 +00:00
|
|
|
if (!(data instanceof Uint8Array)) {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new Error(
|
2021-09-24 00:49:05 +00:00
|
|
|
`makeAttachmentPointer: data was a '${typeof data}' instead of Uint8Array`
|
2020-04-13 17:37:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (data.byteLength !== size) {
|
|
|
|
throw new Error(
|
|
|
|
`makeAttachmentPointer: Size ${size} did not match data.byteLength ${data.byteLength}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const padded = this.getPaddedAttachment(data);
|
2021-04-16 23:13:13 +00:00
|
|
|
const key = getRandomBytes(64);
|
|
|
|
const iv = getRandomBytes(16);
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const result = encryptAttachment(padded, key, iv);
|
2020-04-13 17:37:29 +00:00
|
|
|
const id = await this.server.putAttachment(result.ciphertext);
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const proto = new Proto.AttachmentPointer();
|
2021-09-22 00:58:03 +00:00
|
|
|
proto.cdnId = Long.fromString(id);
|
2020-04-13 17:37:29 +00:00
|
|
|
proto.contentType = attachment.contentType;
|
2021-09-24 00:49:05 +00:00
|
|
|
proto.key = key;
|
2020-04-13 17:37:29 +00:00
|
|
|
proto.size = attachment.size;
|
2021-09-24 00:49:05 +00:00
|
|
|
proto.digest = result.digest;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
if (attachment.fileName) {
|
|
|
|
proto.fileName = attachment.fileName;
|
|
|
|
}
|
|
|
|
if (attachment.flags) {
|
|
|
|
proto.flags = attachment.flags;
|
|
|
|
}
|
|
|
|
if (attachment.width) {
|
|
|
|
proto.width = attachment.width;
|
|
|
|
}
|
|
|
|
if (attachment.height) {
|
|
|
|
proto.height = attachment.height;
|
|
|
|
}
|
|
|
|
if (attachment.caption) {
|
|
|
|
proto.caption = attachment.caption;
|
|
|
|
}
|
2020-05-27 21:37:06 +00:00
|
|
|
if (attachment.blurHash) {
|
|
|
|
proto.blurHash = attachment.blurHash;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async uploadAttachments(message: Message): Promise<void> {
|
2021-10-05 22:10:08 +00:00
|
|
|
await Promise.all(
|
|
|
|
message.attachments.map(attachment =>
|
|
|
|
this.makeAttachmentPointer(attachment)
|
|
|
|
)
|
2020-04-13 17:37:29 +00:00
|
|
|
)
|
|
|
|
.then(attachmentPointers => {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
message.attachmentPointers = attachmentPointers;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (error instanceof HTTPError) {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new MessageError(message, error);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async uploadLinkPreviews(message: Message): Promise<void> {
|
2020-04-13 17:37:29 +00:00
|
|
|
try {
|
|
|
|
const preview = await Promise.all(
|
2021-10-05 22:10:08 +00:00
|
|
|
(message.preview || []).map(async (item: Readonly<LinkPreviewType>) => {
|
|
|
|
if (!item.image) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
const attachment = makeAttachmentSendReady(item.image);
|
|
|
|
if (!attachment) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
attachmentPointer: await this.makeAttachmentPointer(attachment),
|
|
|
|
};
|
|
|
|
})
|
2020-04-13 17:37:29 +00:00
|
|
|
);
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
message.preview = preview;
|
|
|
|
} catch (error) {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (error instanceof HTTPError) {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new MessageError(message, error);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async uploadSticker(message: Message): Promise<void> {
|
2020-04-13 17:37:29 +00:00
|
|
|
try {
|
|
|
|
const { sticker } = message;
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
if (!sticker) {
|
2020-04-13 17:37:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-07-15 23:48:09 +00:00
|
|
|
if (!sticker.data) {
|
|
|
|
throw new Error('uploadSticker: No sticker data to upload!');
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
message.sticker = {
|
|
|
|
...sticker,
|
|
|
|
attachmentPointer: await this.makeAttachmentPointer(sticker.data),
|
|
|
|
};
|
|
|
|
} catch (error) {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (error instanceof HTTPError) {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new MessageError(message, error);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async uploadThumbnails(message: Message): Promise<void> {
|
|
|
|
const makePointer = this.makeAttachmentPointer.bind(this);
|
|
|
|
const { quote } = message;
|
|
|
|
|
|
|
|
if (!quote || !quote.attachments || quote.attachments.length === 0) {
|
2020-09-24 21:53:21 +00:00
|
|
|
return;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
quote.attachments.map((attachment: QuoteAttachmentType) => {
|
|
|
|
if (!attachment.thumbnail) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return makePointer(attachment.thumbnail).then(pointer => {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
attachment.attachmentPointer = pointer;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
).catch(error => {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (error instanceof HTTPError) {
|
2020-04-13 17:37:29 +00:00
|
|
|
throw new MessageError(message, error);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
// Proto assembly
|
|
|
|
|
2021-07-29 19:00:11 +00:00
|
|
|
async getDataMessage(
|
|
|
|
options: Readonly<MessageOptionsType>
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<Uint8Array> {
|
2021-05-25 22:40:04 +00:00
|
|
|
const message = await this.getHydratedMessage(options);
|
2021-09-24 00:49:05 +00:00
|
|
|
return message.encode();
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 19:00:11 +00:00
|
|
|
async getContentMessage(
|
|
|
|
options: Readonly<MessageOptionsType>
|
|
|
|
): Promise<Proto.Content> {
|
2021-05-25 22:40:04 +00:00
|
|
|
const message = await this.getHydratedMessage(options);
|
|
|
|
const dataMessage = message.toProto();
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2021-05-25 22:40:04 +00:00
|
|
|
contentMessage.dataMessage = dataMessage;
|
|
|
|
|
|
|
|
return contentMessage;
|
|
|
|
}
|
|
|
|
|
2021-07-29 19:00:11 +00:00
|
|
|
async getHydratedMessage(
|
|
|
|
attributes: Readonly<MessageOptionsType>
|
|
|
|
): Promise<Message> {
|
2021-05-25 22:40:04 +00:00
|
|
|
const message = new Message(attributes);
|
|
|
|
await Promise.all([
|
|
|
|
this.uploadAttachments(message),
|
|
|
|
this.uploadThumbnails(message),
|
|
|
|
this.uploadLinkPreviews(message),
|
|
|
|
this.uploadSticker(message),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2021-07-29 19:00:11 +00:00
|
|
|
getTypingContentMessage(
|
|
|
|
options: Readonly<{
|
|
|
|
recipientId?: string;
|
2021-09-24 00:49:05 +00:00
|
|
|
groupId?: Uint8Array;
|
2021-07-29 19:00:11 +00:00
|
|
|
groupMembers: ReadonlyArray<string>;
|
|
|
|
isTyping: boolean;
|
|
|
|
timestamp?: number;
|
|
|
|
}>
|
|
|
|
): Proto.Content {
|
2021-07-09 19:36:10 +00:00
|
|
|
const ACTION_ENUM = Proto.TypingMessage.Action;
|
2021-05-25 22:40:04 +00:00
|
|
|
const { recipientId, groupId, isTyping, timestamp } = options;
|
|
|
|
|
|
|
|
if (!recipientId && !groupId) {
|
|
|
|
throw new Error(
|
|
|
|
'getTypingContentMessage: Need to provide either recipientId or groupId!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const finalTimestamp = timestamp || Date.now();
|
|
|
|
const action = isTyping ? ACTION_ENUM.STARTED : ACTION_ENUM.STOPPED;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const typingMessage = new Proto.TypingMessage();
|
|
|
|
if (groupId) {
|
2021-09-24 00:49:05 +00:00
|
|
|
typingMessage.groupId = groupId;
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
2021-05-25 22:40:04 +00:00
|
|
|
typingMessage.action = action;
|
|
|
|
typingMessage.timestamp = finalTimestamp;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2021-05-25 22:40:04 +00:00
|
|
|
contentMessage.typingMessage = typingMessage;
|
|
|
|
|
|
|
|
return contentMessage;
|
|
|
|
}
|
|
|
|
|
2021-07-29 19:00:11 +00:00
|
|
|
getAttrsFromGroupOptions(
|
|
|
|
options: Readonly<GroupSendOptionsType>
|
|
|
|
): MessageOptionsType {
|
2021-05-25 22:40:04 +00:00
|
|
|
const {
|
|
|
|
messageText,
|
|
|
|
timestamp,
|
|
|
|
attachments,
|
|
|
|
quote,
|
|
|
|
preview,
|
|
|
|
sticker,
|
|
|
|
reaction,
|
|
|
|
expireTimer,
|
|
|
|
profileKey,
|
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
groupV2,
|
|
|
|
groupV1,
|
|
|
|
mentions,
|
|
|
|
groupCallUpdate,
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
if (!groupV1 && !groupV2) {
|
|
|
|
throw new Error(
|
|
|
|
'getAttrsFromGroupOptions: Neither group1 nor groupv2 information provided!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const myE164 = window.textsecure.storage.user.getNumber();
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getUuid()?.toString();
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
const groupMembers = groupV2?.members || groupV1?.members || [];
|
|
|
|
|
|
|
|
// We should always have a UUID but have this check just in case we don't.
|
|
|
|
let isNotMe: (recipient: string) => boolean;
|
|
|
|
if (myUuid) {
|
2021-09-10 02:38:11 +00:00
|
|
|
isNotMe = r => r !== myE164 && r !== myUuid.toString();
|
2021-05-25 22:40:04 +00:00
|
|
|
} else {
|
|
|
|
isNotMe = r => r !== myE164;
|
|
|
|
}
|
|
|
|
|
|
|
|
const blockedIdentifiers = new Set(
|
|
|
|
concat(
|
2021-06-15 00:09:37 +00:00
|
|
|
window.storage.blocked.getBlockedUuids(),
|
|
|
|
window.storage.blocked.getBlockedNumbers()
|
2021-05-25 22:40:04 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const recipients = groupMembers.filter(
|
|
|
|
recipient => isNotMe(recipient) && !blockedIdentifiers.has(recipient)
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
attachments,
|
|
|
|
body: messageText,
|
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
|
|
|
groupCallUpdate,
|
|
|
|
groupV2,
|
|
|
|
group: groupV1
|
|
|
|
? {
|
|
|
|
id: groupV1.id,
|
2021-07-09 19:36:10 +00:00
|
|
|
type: Proto.GroupContext.Type.DELIVER,
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
mentions,
|
|
|
|
preview,
|
|
|
|
profileKey,
|
|
|
|
quote,
|
|
|
|
reaction,
|
|
|
|
recipients,
|
|
|
|
sticker,
|
|
|
|
timestamp,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
createSyncMessage(): Proto.SyncMessage {
|
|
|
|
const syncMessage = new Proto.SyncMessage();
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
syncMessage.padding = this.getRandomPadding();
|
|
|
|
|
|
|
|
return syncMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Low-level sends
|
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
async sendMessage({
|
|
|
|
messageOptions,
|
|
|
|
contentHint,
|
|
|
|
groupId,
|
|
|
|
options,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-07-02 18:34:17 +00:00
|
|
|
messageOptions: MessageOptionsType;
|
|
|
|
contentHint: number;
|
|
|
|
groupId: string | undefined;
|
|
|
|
options?: SendOptionsType;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): Promise<CallbackResultType> {
|
2021-07-02 18:34:17 +00:00
|
|
|
const message = new Message(messageOptions);
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
this.uploadAttachments(message),
|
|
|
|
this.uploadThumbnails(message),
|
|
|
|
this.uploadLinkPreviews(message),
|
|
|
|
this.uploadSticker(message),
|
|
|
|
]).then(
|
2020-09-04 01:25:19 +00:00
|
|
|
async (): Promise<CallbackResultType> =>
|
2020-04-13 17:37:29 +00:00
|
|
|
new Promise((resolve, reject) => {
|
2021-07-02 18:34:17 +00:00
|
|
|
this.sendMessageProto({
|
|
|
|
callback: (res: CallbackResultType) => {
|
2021-09-24 00:49:05 +00:00
|
|
|
res.dataMessage = message.encode();
|
2020-04-13 17:37:29 +00:00
|
|
|
if (res.errors && res.errors.length > 0) {
|
2021-07-20 01:10:09 +00:00
|
|
|
reject(new SendMessageProtoError(res));
|
2020-04-13 17:37:29 +00:00
|
|
|
} else {
|
|
|
|
resolve(res);
|
|
|
|
}
|
|
|
|
},
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint,
|
|
|
|
groupId,
|
|
|
|
options,
|
|
|
|
proto: message.toProto(),
|
|
|
|
recipients: message.recipients || [],
|
|
|
|
timestamp: message.timestamp,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
sendMessageProto({
|
2021-07-15 23:48:09 +00:00
|
|
|
callback,
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint,
|
|
|
|
groupId,
|
|
|
|
options,
|
2021-07-15 23:48:09 +00:00
|
|
|
proto,
|
|
|
|
recipients,
|
|
|
|
sendLogCallback,
|
|
|
|
timestamp,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-07-15 23:48:09 +00:00
|
|
|
callback: (result: CallbackResultType) => void;
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint: number;
|
|
|
|
groupId: string | undefined;
|
|
|
|
options?: SendOptionsType;
|
2021-07-15 23:48:09 +00:00
|
|
|
proto: Proto.Content | Proto.DataMessage | PlaintextContent;
|
2021-07-29 19:00:11 +00:00
|
|
|
recipients: ReadonlyArray<string>;
|
2021-07-15 23:48:09 +00:00
|
|
|
sendLogCallback?: SendLogCallbackType;
|
|
|
|
timestamp: number;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): void {
|
2020-04-13 17:37:29 +00:00
|
|
|
const rejections = window.textsecure.storage.get(
|
|
|
|
'signedKeyRotationRejected',
|
|
|
|
0
|
|
|
|
);
|
|
|
|
if (rejections > 5) {
|
|
|
|
throw new SignedPreKeyRotationError();
|
|
|
|
}
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const outgoing = new OutgoingMessage({
|
|
|
|
callback,
|
2021-05-28 19:11:19 +00:00
|
|
|
contentHint,
|
|
|
|
groupId,
|
2021-07-15 23:48:09 +00:00
|
|
|
identifiers: recipients,
|
|
|
|
message: proto,
|
|
|
|
options,
|
|
|
|
sendLogCallback,
|
|
|
|
server: this.server,
|
|
|
|
timestamp,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
recipients.forEach(identifier => {
|
|
|
|
this.queueJobForIdentifier(identifier, async () =>
|
|
|
|
outgoing.sendToIdentifier(identifier)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
async sendMessageProtoAndWait({
|
|
|
|
timestamp,
|
|
|
|
recipients,
|
|
|
|
proto,
|
|
|
|
contentHint,
|
|
|
|
groupId,
|
|
|
|
options,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-07-02 18:34:17 +00:00
|
|
|
timestamp: number;
|
|
|
|
recipients: Array<string>;
|
2021-07-09 19:36:10 +00:00
|
|
|
proto: Proto.Content | Proto.DataMessage | PlaintextContent;
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint: number;
|
|
|
|
groupId: string | undefined;
|
|
|
|
options?: SendOptionsType;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): Promise<CallbackResultType> {
|
2020-04-13 17:37:29 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const callback = (result: CallbackResultType) => {
|
|
|
|
if (result && result.errors && result.errors.length > 0) {
|
2021-07-20 01:10:09 +00:00
|
|
|
reject(new SendMessageProtoError(result));
|
2020-04-13 17:37:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(result);
|
|
|
|
};
|
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
this.sendMessageProto({
|
|
|
|
callback,
|
2021-05-28 19:11:19 +00:00
|
|
|
contentHint,
|
|
|
|
groupId,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
|
|
|
proto,
|
|
|
|
recipients,
|
|
|
|
timestamp,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
async sendIndividualProto({
|
|
|
|
identifier,
|
|
|
|
proto,
|
|
|
|
timestamp,
|
|
|
|
contentHint,
|
|
|
|
options,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-07-02 18:34:17 +00:00
|
|
|
identifier: string | undefined;
|
2021-07-09 19:36:10 +00:00
|
|
|
proto: Proto.DataMessage | Proto.Content | PlaintextContent;
|
2021-07-02 18:34:17 +00:00
|
|
|
timestamp: number;
|
|
|
|
contentHint: number;
|
|
|
|
options?: SendOptionsType;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): Promise<CallbackResultType> {
|
2021-06-15 00:09:37 +00:00
|
|
|
assert(identifier, "Identifier can't be undefined");
|
2020-04-13 17:37:29 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const callback = (res: CallbackResultType) => {
|
|
|
|
if (res && res.errors && res.errors.length > 0) {
|
2021-07-20 01:10:09 +00:00
|
|
|
reject(new SendMessageProtoError(res));
|
2020-04-13 17:37:29 +00:00
|
|
|
} else {
|
|
|
|
resolve(res);
|
|
|
|
}
|
|
|
|
};
|
2021-07-02 18:34:17 +00:00
|
|
|
this.sendMessageProto({
|
2020-04-13 17:37:29 +00:00
|
|
|
callback,
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint,
|
|
|
|
groupId: undefined,
|
|
|
|
options,
|
|
|
|
proto,
|
|
|
|
recipients: [identifier],
|
|
|
|
timestamp,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-28 19:11:19 +00:00
|
|
|
// You might wonder why this takes a groupId. models/messages.resend() can send a group
|
|
|
|
// message to just one person.
|
2021-07-02 18:34:17 +00:00
|
|
|
async sendMessageToIdentifier({
|
|
|
|
identifier,
|
|
|
|
messageText,
|
|
|
|
attachments,
|
|
|
|
quote,
|
|
|
|
preview,
|
|
|
|
sticker,
|
|
|
|
reaction,
|
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
timestamp,
|
|
|
|
expireTimer,
|
|
|
|
contentHint,
|
|
|
|
groupId,
|
|
|
|
profileKey,
|
|
|
|
options,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-07-02 18:34:17 +00:00
|
|
|
identifier: string;
|
|
|
|
messageText: string | undefined;
|
2021-07-29 19:00:11 +00:00
|
|
|
attachments: ReadonlyArray<AttachmentType> | undefined;
|
2021-10-05 22:10:08 +00:00
|
|
|
quote?: QuoteType;
|
|
|
|
preview?: ReadonlyArray<LinkPreviewType> | undefined;
|
|
|
|
sticker?: StickerType;
|
|
|
|
reaction?: ReactionType;
|
2021-07-02 18:34:17 +00:00
|
|
|
deletedForEveryoneTimestamp: number | undefined;
|
|
|
|
timestamp: number;
|
|
|
|
expireTimer: number | undefined;
|
|
|
|
contentHint: number;
|
|
|
|
groupId: string | undefined;
|
2021-09-24 00:49:05 +00:00
|
|
|
profileKey?: Uint8Array;
|
2021-07-02 18:34:17 +00:00
|
|
|
options?: SendOptionsType;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): Promise<CallbackResultType> {
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendMessage({
|
|
|
|
messageOptions: {
|
2021-05-25 22:40:04 +00:00
|
|
|
recipients: [identifier],
|
|
|
|
body: messageText,
|
|
|
|
timestamp,
|
|
|
|
attachments,
|
|
|
|
quote,
|
|
|
|
preview,
|
|
|
|
sticker,
|
|
|
|
reaction,
|
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
|
|
|
profileKey,
|
|
|
|
},
|
2021-05-28 19:11:19 +00:00
|
|
|
contentHint,
|
|
|
|
groupId,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
// Support for sync messages
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
// Note: this is used for sending real messages to your other devices after sending a
|
|
|
|
// message to others.
|
2021-07-02 18:34:17 +00:00
|
|
|
async sendSyncMessage({
|
|
|
|
encodedDataMessage,
|
|
|
|
timestamp,
|
|
|
|
destination,
|
|
|
|
destinationUuid,
|
|
|
|
expirationStartTimestamp,
|
2021-07-19 22:44:49 +00:00
|
|
|
conversationIdsSentTo = [],
|
|
|
|
conversationIdsWithSealedSender = new Set(),
|
2021-07-02 18:34:17 +00:00
|
|
|
isUpdate,
|
|
|
|
options,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-09-24 00:49:05 +00:00
|
|
|
encodedDataMessage: Uint8Array;
|
2021-07-02 18:34:17 +00:00
|
|
|
timestamp: number;
|
|
|
|
destination: string | undefined;
|
|
|
|
destinationUuid: string | null | undefined;
|
|
|
|
expirationStartTimestamp: number | null;
|
2021-07-19 22:44:49 +00:00
|
|
|
conversationIdsSentTo?: Iterable<string>;
|
|
|
|
conversationIdsWithSealedSender?: Set<string>;
|
2021-07-02 18:34:17 +00:00
|
|
|
isUpdate?: boolean;
|
|
|
|
options?: SendOptionsType;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const dataMessage = Proto.DataMessage.decode(encodedDataMessage);
|
2021-07-09 19:36:10 +00:00
|
|
|
const sentMessage = new Proto.SyncMessage.Sent();
|
2020-04-13 17:37:29 +00:00
|
|
|
sentMessage.timestamp = timestamp;
|
|
|
|
sentMessage.message = dataMessage;
|
|
|
|
if (destination) {
|
|
|
|
sentMessage.destination = destination;
|
|
|
|
}
|
|
|
|
if (destinationUuid) {
|
|
|
|
sentMessage.destinationUuid = destinationUuid;
|
|
|
|
}
|
|
|
|
if (expirationStartTimestamp) {
|
|
|
|
sentMessage.expirationStartTimestamp = expirationStartTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUpdate) {
|
|
|
|
sentMessage.isRecipientUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Though this field has 'unidenified' in the name, it should have entries for each
|
|
|
|
// number we sent to.
|
2021-07-19 22:44:49 +00:00
|
|
|
if (!isEmpty(conversationIdsSentTo)) {
|
|
|
|
sentMessage.unidentifiedStatus = [
|
|
|
|
...map(conversationIdsSentTo, conversationId => {
|
2021-11-11 22:43:05 +00:00
|
|
|
const status =
|
|
|
|
new Proto.SyncMessage.Sent.UnidentifiedDeliveryStatus();
|
2021-07-19 22:44:49 +00:00
|
|
|
const conv = window.ConversationController.get(conversationId);
|
|
|
|
if (conv) {
|
|
|
|
const e164 = conv.get('e164');
|
|
|
|
if (e164) {
|
|
|
|
status.destination = e164;
|
|
|
|
}
|
|
|
|
const uuid = conv.get('uuid');
|
|
|
|
if (uuid) {
|
|
|
|
status.destinationUuid = uuid;
|
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
2021-11-11 22:43:05 +00:00
|
|
|
status.unidentified =
|
|
|
|
conversationIdsWithSealedSender.has(conversationId);
|
2021-07-19 22:44:49 +00:00
|
|
|
return status;
|
|
|
|
}),
|
|
|
|
];
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.sent = sentMessage;
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2020-04-13 17:37:29 +00:00
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
2020-04-13 17:37:29 +00:00
|
|
|
timestamp,
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint: ContentHint.RESENDABLE,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async sendRequestBlockSyncMessage(
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const request = new Proto.SyncMessage.Request();
|
|
|
|
request.type = Proto.SyncMessage.Request.Type.BLOCKED;
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.request = request;
|
|
|
|
const contentMessage = new Proto.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
|
|
|
|
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-15 23:48:09 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async sendRequestConfigurationSyncMessage(
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const request = new Proto.SyncMessage.Request();
|
|
|
|
request.type = Proto.SyncMessage.Request.Type.CONFIGURATION;
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.request = request;
|
|
|
|
const contentMessage = new Proto.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
|
|
|
|
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-15 23:48:09 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async sendRequestGroupSyncMessage(
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const request = new Proto.SyncMessage.Request();
|
|
|
|
request.type = Proto.SyncMessage.Request.Type.GROUPS;
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.request = request;
|
|
|
|
const contentMessage = new Proto.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
|
|
|
|
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-15 23:48:09 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async sendRequestContactSyncMessage(
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const request = new Proto.SyncMessage.Request();
|
|
|
|
request.type = Proto.SyncMessage.Request.Type.CONTACTS;
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.request = request;
|
|
|
|
const contentMessage = new Proto.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-15 23:48:09 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async sendFetchManifestSyncMessage(
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const fetchLatest = new Proto.SyncMessage.FetchLatest();
|
|
|
|
fetchLatest.type = Proto.SyncMessage.FetchLatest.Type.STORAGE_MANIFEST;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.fetchLatest = fetchLatest;
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2020-09-09 00:56:23 +00:00
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 20:45:41 +00:00
|
|
|
async sendFetchLocalProfileSyncMessage(
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-21 20:45:41 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2021-07-21 20:45:41 +00:00
|
|
|
|
|
|
|
const fetchLatest = new Proto.SyncMessage.FetchLatest();
|
|
|
|
fetchLatest.type = Proto.SyncMessage.FetchLatest.Type.LOCAL_PROFILE;
|
|
|
|
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.fetchLatest = fetchLatest;
|
|
|
|
const contentMessage = new Proto.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
|
|
|
|
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-21 20:45:41 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async sendRequestKeySyncMessage(
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-07-07 00:56:56 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const request = new Proto.SyncMessage.Request();
|
|
|
|
request.type = Proto.SyncMessage.Request.Type.KEYS;
|
2020-07-07 00:56:56 +00:00
|
|
|
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.request = request;
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2020-07-07 00:56:56 +00:00
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
2020-07-07 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
async syncReadMessages(
|
2021-07-29 19:00:11 +00:00
|
|
|
reads: ReadonlyArray<{
|
2020-07-10 18:28:49 +00:00
|
|
|
senderUuid?: string;
|
|
|
|
senderE164?: string;
|
|
|
|
timestamp: number;
|
|
|
|
}>,
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2021-07-15 23:48:09 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.read = [];
|
|
|
|
for (let i = 0; i < reads.length; i += 1) {
|
2021-07-09 19:36:10 +00:00
|
|
|
const proto = new Proto.SyncMessage.Read(reads[i]);
|
2021-07-02 18:34:17 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
syncMessage.read.push(proto);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2021-07-02 18:34:17 +00:00
|
|
|
contentMessage.syncMessage = syncMessage;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-07-02 18:34:17 +00:00
|
|
|
|
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint: ContentHint.RESENDABLE,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
async syncView(
|
|
|
|
views: ReadonlyArray<{
|
|
|
|
senderUuid?: string;
|
|
|
|
senderE164?: string;
|
|
|
|
timestamp: number;
|
|
|
|
}>,
|
|
|
|
options?: SendOptionsType
|
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2021-08-12 18:15:55 +00:00
|
|
|
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.viewed = views.map(view => new Proto.SyncMessage.Viewed(view));
|
|
|
|
const contentMessage = new Proto.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
|
|
|
|
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-08-12 18:15:55 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.RESENDABLE,
|
|
|
|
options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
async syncViewOnceOpen(
|
2021-05-18 20:27:16 +00:00
|
|
|
sender: string | undefined,
|
2020-04-13 17:37:29 +00:00
|
|
|
senderUuid: string,
|
|
|
|
timestamp: number,
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const viewOnceOpen = new Proto.SyncMessage.ViewOnceOpen();
|
|
|
|
if (sender !== undefined) {
|
|
|
|
viewOnceOpen.sender = sender;
|
|
|
|
}
|
|
|
|
viewOnceOpen.senderUuid = senderUuid;
|
|
|
|
viewOnceOpen.timestamp = timestamp;
|
2020-04-13 17:37:29 +00:00
|
|
|
syncMessage.viewOnceOpen = viewOnceOpen;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2020-04-13 17:37:29 +00:00
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint: ContentHint.RESENDABLE,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
async syncMessageRequestResponse(
|
2021-07-29 19:00:11 +00:00
|
|
|
responseArgs: Readonly<{
|
2020-05-27 21:37:06 +00:00
|
|
|
threadE164?: string;
|
|
|
|
threadUuid?: string;
|
2021-09-24 00:49:05 +00:00
|
|
|
groupId?: Uint8Array;
|
2020-05-27 21:37:06 +00:00
|
|
|
type: number;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>,
|
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-05-27 21:37:06 +00:00
|
|
|
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const response = new Proto.SyncMessage.MessageRequestResponse();
|
|
|
|
if (responseArgs.threadE164 !== undefined) {
|
|
|
|
response.threadE164 = responseArgs.threadE164;
|
|
|
|
}
|
|
|
|
if (responseArgs.threadUuid !== undefined) {
|
|
|
|
response.threadUuid = responseArgs.threadUuid;
|
|
|
|
}
|
|
|
|
if (responseArgs.groupId) {
|
2021-09-24 00:49:05 +00:00
|
|
|
response.groupId = responseArgs.groupId;
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
2020-05-27 21:37:06 +00:00
|
|
|
response.type = responseArgs.type;
|
|
|
|
syncMessage.messageRequestResponse = response;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2020-05-27 21:37:06 +00:00
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint: ContentHint.RESENDABLE,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
|
|
|
});
|
2020-05-27 21:37:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
async sendStickerPackSync(
|
2021-07-29 19:00:11 +00:00
|
|
|
operations: ReadonlyArray<{
|
2020-04-13 17:37:29 +00:00
|
|
|
packId: string;
|
|
|
|
packKey: string;
|
|
|
|
installed: boolean;
|
|
|
|
}>,
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2021-07-09 19:36:10 +00:00
|
|
|
const ENUM = Proto.SyncMessage.StickerPackOperation.Type;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
const packOperations = operations.map(item => {
|
|
|
|
const { packId, packKey, installed } = item;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const operation = new Proto.SyncMessage.StickerPackOperation();
|
|
|
|
operation.packId = Bytes.fromHex(packId);
|
|
|
|
operation.packKey = Bytes.fromBase64(packKey);
|
2020-04-13 17:37:29 +00:00
|
|
|
operation.type = installed ? ENUM.INSTALL : ENUM.REMOVE;
|
|
|
|
|
|
|
|
return operation;
|
|
|
|
});
|
|
|
|
|
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.stickerPackOperation = packOperations;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2020-04-13 17:37:29 +00:00
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async syncVerification(
|
2021-07-15 23:48:09 +00:00
|
|
|
destinationE164: string | undefined,
|
|
|
|
destinationUuid: string | undefined,
|
2020-04-13 17:37:29 +00:00
|
|
|
state: number,
|
2021-09-24 00:49:05 +00:00
|
|
|
identityKey: Readonly<Uint8Array>,
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getCheckedUuid();
|
2020-04-13 17:37:29 +00:00
|
|
|
const now = Date.now();
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
if (!destinationE164 && !destinationUuid) {
|
|
|
|
throw new Error('syncVerification: Neither e164 nor UUID were provided');
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 16:40:26 +00:00
|
|
|
// Get padding which we can share between null message and verified sync
|
|
|
|
const padding = this.getRandomPadding();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-02-18 16:40:26 +00:00
|
|
|
// First send a null message to mask the sync message.
|
2021-07-15 23:48:09 +00:00
|
|
|
await handleMessageSend(
|
|
|
|
this.sendNullMessage(
|
|
|
|
{ uuid: destinationUuid, e164: destinationE164, padding },
|
|
|
|
options
|
|
|
|
),
|
|
|
|
{
|
|
|
|
messageIds: [],
|
|
|
|
sendType: 'nullMessage',
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
);
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const verified = new Proto.Verified();
|
|
|
|
verified.state = state;
|
|
|
|
if (destinationE164) {
|
|
|
|
verified.destination = destinationE164;
|
|
|
|
}
|
|
|
|
if (destinationUuid) {
|
|
|
|
verified.destinationUuid = destinationUuid;
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
verified.identityKey = identityKey;
|
2021-07-15 23:48:09 +00:00
|
|
|
verified.nullMessage = padding;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const syncMessage = this.createSyncMessage();
|
|
|
|
syncMessage.verified = verified;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const secondMessage = new Proto.Content();
|
|
|
|
secondMessage.syncMessage = syncMessage;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-09-10 02:38:11 +00:00
|
|
|
identifier: myUuid.toString(),
|
2021-07-15 23:48:09 +00:00
|
|
|
proto: secondMessage,
|
|
|
|
timestamp: now,
|
|
|
|
contentHint: ContentHint.RESENDABLE,
|
|
|
|
options,
|
2020-04-13 17:37:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
// Sending messages to contacts
|
|
|
|
|
|
|
|
async sendProfileKeyUpdate(
|
2021-09-24 00:49:05 +00:00
|
|
|
profileKey: Readonly<Uint8Array>,
|
2021-07-29 19:00:11 +00:00
|
|
|
recipients: ReadonlyArray<string>,
|
|
|
|
options: Readonly<SendOptionsType>,
|
2021-05-25 22:40:04 +00:00
|
|
|
groupId?: string
|
2020-09-30 23:49:41 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendMessage({
|
|
|
|
messageOptions: {
|
2021-05-25 22:40:04 +00:00
|
|
|
recipients,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
profileKey,
|
2021-07-09 19:36:10 +00:00
|
|
|
flags: Proto.DataMessage.Flags.PROFILE_KEY_UPDATE,
|
2021-05-25 22:40:04 +00:00
|
|
|
...(groupId
|
|
|
|
? {
|
|
|
|
group: {
|
|
|
|
id: groupId,
|
2021-07-09 19:36:10 +00:00
|
|
|
type: Proto.GroupContext.Type.DELIVER,
|
2021-05-25 22:40:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
},
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
groupId: undefined,
|
|
|
|
options,
|
|
|
|
});
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
async sendCallingMessage(
|
|
|
|
recipientId: string,
|
2021-07-29 19:00:11 +00:00
|
|
|
callingMessage: Readonly<Proto.ICallingMessage>,
|
|
|
|
options?: Readonly<SendOptionsType>
|
2021-07-15 23:48:09 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-05-25 22:40:04 +00:00
|
|
|
const recipients = [recipientId];
|
|
|
|
const finalTimestamp = Date.now();
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2021-05-25 22:40:04 +00:00
|
|
|
contentMessage.callingMessage = callingMessage;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-25 22:40:04 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
return this.sendMessageProtoAndWait({
|
2021-07-02 18:34:17 +00:00
|
|
|
timestamp: finalTimestamp,
|
2021-05-25 22:40:04 +00:00
|
|
|
recipients,
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
contentHint: ContentHint.DEFAULT,
|
|
|
|
groupId: undefined,
|
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 15:43:26 +00:00
|
|
|
async sendDeliveryReceipt(
|
|
|
|
options: Readonly<{
|
|
|
|
senderE164?: string;
|
|
|
|
senderUuid?: string;
|
|
|
|
timestamps: Array<number>;
|
|
|
|
options?: Readonly<SendOptionsType>;
|
|
|
|
}>
|
|
|
|
): Promise<CallbackResultType> {
|
|
|
|
return this.sendReceiptMessage({
|
|
|
|
...options,
|
|
|
|
type: Proto.ReceiptMessage.Type.DELIVERY,
|
|
|
|
});
|
|
|
|
}
|
2021-05-25 22:40:04 +00:00
|
|
|
|
2021-08-17 15:43:26 +00:00
|
|
|
async sendReadReceipts(
|
|
|
|
options: Readonly<{
|
|
|
|
senderE164?: string;
|
|
|
|
senderUuid?: string;
|
|
|
|
timestamps: Array<number>;
|
|
|
|
options?: Readonly<SendOptionsType>;
|
|
|
|
}>
|
|
|
|
): Promise<CallbackResultType> {
|
|
|
|
return this.sendReceiptMessage({
|
|
|
|
...options,
|
|
|
|
type: Proto.ReceiptMessage.Type.READ,
|
|
|
|
});
|
|
|
|
}
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-08-17 15:43:26 +00:00
|
|
|
async sendViewedReceipts(
|
|
|
|
options: Readonly<{
|
|
|
|
senderE164?: string;
|
|
|
|
senderUuid?: string;
|
|
|
|
timestamps: Array<number>;
|
|
|
|
options?: Readonly<SendOptionsType>;
|
|
|
|
}>
|
|
|
|
): Promise<CallbackResultType> {
|
|
|
|
return this.sendReceiptMessage({
|
|
|
|
...options,
|
|
|
|
type: Proto.ReceiptMessage.Type.VIEWED,
|
2021-07-02 18:34:17 +00:00
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 15:43:26 +00:00
|
|
|
private async sendReceiptMessage({
|
2021-07-02 18:34:17 +00:00
|
|
|
senderE164,
|
|
|
|
senderUuid,
|
|
|
|
timestamps,
|
2021-08-17 15:43:26 +00:00
|
|
|
type,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-08-17 15:43:26 +00:00
|
|
|
senderE164?: string;
|
|
|
|
senderUuid?: string;
|
2021-07-02 18:34:17 +00:00
|
|
|
timestamps: Array<number>;
|
2021-08-17 15:43:26 +00:00
|
|
|
type: Proto.ReceiptMessage.Type;
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>;
|
|
|
|
}>): Promise<CallbackResultType> {
|
2021-08-17 15:43:26 +00:00
|
|
|
if (!senderUuid && !senderE164) {
|
|
|
|
throw new Error(
|
|
|
|
'sendReceiptMessage: Neither uuid nor e164 was provided!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const receiptMessage = new Proto.ReceiptMessage();
|
2021-08-17 15:43:26 +00:00
|
|
|
receiptMessage.type = type;
|
2021-05-25 22:40:04 +00:00
|
|
|
receiptMessage.timestamp = timestamps;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2021-05-25 22:40:04 +00:00
|
|
|
contentMessage.receiptMessage = receiptMessage;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendIndividualProto({
|
|
|
|
identifier: senderUuid || senderE164,
|
|
|
|
proto: contentMessage,
|
|
|
|
timestamp: Date.now(),
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint: ContentHint.RESENDABLE,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
async sendNullMessage(
|
|
|
|
{
|
|
|
|
uuid,
|
|
|
|
e164,
|
|
|
|
padding,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{ uuid?: string; e164?: string; padding?: Uint8Array }>,
|
|
|
|
options?: Readonly<SendOptionsType>
|
2020-09-24 21:53:21 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const nullMessage = new Proto.NullMessage();
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
const identifier = uuid || e164;
|
|
|
|
if (!identifier) {
|
|
|
|
throw new Error('sendNullMessage: Got neither uuid nor e164!');
|
|
|
|
}
|
|
|
|
|
|
|
|
nullMessage.padding = padding || this.getRandomPadding();
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2021-05-25 22:40:04 +00:00
|
|
|
contentMessage.nullMessage = nullMessage;
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
|
|
|
// We want the NullMessage to look like a normal outgoing message
|
2021-05-25 22:40:04 +00:00
|
|
|
const timestamp = Date.now();
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendIndividualProto({
|
2021-05-25 22:40:04 +00:00
|
|
|
identifier,
|
2021-07-02 18:34:17 +00:00
|
|
|
proto: contentMessage,
|
2021-05-25 22:40:04 +00:00
|
|
|
timestamp,
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint: ContentHint.IMPLICIT,
|
|
|
|
options,
|
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
async sendExpirationTimerUpdateToIdentifier(
|
|
|
|
identifier: string,
|
|
|
|
expireTimer: number | undefined,
|
|
|
|
timestamp: number,
|
2021-09-24 00:49:05 +00:00
|
|
|
profileKey?: Readonly<Uint8Array>,
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2020-09-04 01:25:19 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendMessage({
|
|
|
|
messageOptions: {
|
2021-05-25 22:40:04 +00:00
|
|
|
recipients: [identifier],
|
|
|
|
timestamp,
|
|
|
|
expireTimer,
|
|
|
|
profileKey,
|
2021-07-09 19:36:10 +00:00
|
|
|
flags: Proto.DataMessage.Flags.EXPIRATION_TIMER_UPDATE,
|
2021-05-25 22:40:04 +00:00
|
|
|
},
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint: ContentHint.RESENDABLE,
|
2021-07-02 18:34:17 +00:00
|
|
|
groupId: undefined,
|
|
|
|
options,
|
|
|
|
});
|
2021-05-28 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async sendRetryRequest({
|
2021-07-15 23:48:09 +00:00
|
|
|
groupId,
|
2021-05-28 19:11:19 +00:00
|
|
|
options,
|
|
|
|
plaintext,
|
|
|
|
uuid,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-07-15 23:48:09 +00:00
|
|
|
groupId?: string;
|
2021-05-28 19:11:19 +00:00
|
|
|
options?: SendOptionsType;
|
|
|
|
plaintext: PlaintextContent;
|
|
|
|
uuid: string;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): Promise<CallbackResultType> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendMessageProtoAndWait({
|
|
|
|
timestamp: Date.now(),
|
|
|
|
recipients: [uuid],
|
|
|
|
proto: plaintext,
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint: ContentHint.DEFAULT,
|
|
|
|
groupId,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
|
|
|
});
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Group sends
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
// Used to ensure that when we send to a group the old way, we save to the send log as
|
|
|
|
// we send to each recipient. Then we don't have a long delay between the first send
|
|
|
|
// and the final save to the database with all recipients.
|
|
|
|
makeSendLogCallback({
|
|
|
|
contentHint,
|
|
|
|
messageId,
|
|
|
|
proto,
|
|
|
|
sendType,
|
|
|
|
timestamp,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint: number;
|
|
|
|
messageId?: string;
|
|
|
|
proto: Buffer;
|
|
|
|
sendType: SendTypesType;
|
|
|
|
timestamp: number;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): SendLogCallbackType {
|
2021-07-15 23:48:09 +00:00
|
|
|
let initialSavePromise: Promise<number>;
|
|
|
|
|
|
|
|
return async ({
|
|
|
|
identifier,
|
|
|
|
deviceIds,
|
|
|
|
}: {
|
|
|
|
identifier: string;
|
|
|
|
deviceIds: Array<number>;
|
|
|
|
}) => {
|
|
|
|
if (!shouldSaveProto(sendType)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversation = window.ConversationController.get(identifier);
|
|
|
|
if (!conversation) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
`makeSendLogCallback: Unable to find conversation for identifier ${identifier}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const recipientUuid = conversation.get('uuid');
|
|
|
|
if (!recipientUuid) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
`makeSendLogCallback: Conversation ${conversation.idForLogging()} had no UUID`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!initialSavePromise) {
|
|
|
|
initialSavePromise = window.Signal.Data.insertSentProto(
|
|
|
|
{
|
|
|
|
timestamp,
|
|
|
|
proto,
|
|
|
|
contentHint,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
recipients: { [recipientUuid]: deviceIds },
|
|
|
|
messageIds: messageId ? [messageId] : [],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
await initialSavePromise;
|
|
|
|
} else {
|
|
|
|
const id = await initialSavePromise;
|
|
|
|
await window.Signal.Data.insertProtoRecipients({
|
|
|
|
id,
|
|
|
|
recipientUuid,
|
|
|
|
deviceIds,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
// No functions should really call this; since most group sends are now via Sender Key
|
2021-07-02 18:34:17 +00:00
|
|
|
async sendGroupProto({
|
|
|
|
contentHint,
|
|
|
|
groupId,
|
|
|
|
options,
|
2021-07-15 23:48:09 +00:00
|
|
|
proto,
|
|
|
|
recipients,
|
|
|
|
sendLogCallback,
|
|
|
|
timestamp = Date.now(),
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-07-02 18:34:17 +00:00
|
|
|
contentHint: number;
|
|
|
|
groupId: string | undefined;
|
|
|
|
options?: SendOptionsType;
|
2021-07-15 23:48:09 +00:00
|
|
|
proto: Proto.Content;
|
2021-07-29 19:00:11 +00:00
|
|
|
recipients: ReadonlyArray<string>;
|
2021-07-15 23:48:09 +00:00
|
|
|
sendLogCallback?: SendLogCallbackType;
|
|
|
|
timestamp: number;
|
2021-07-29 19:00:11 +00:00
|
|
|
}>): Promise<CallbackResultType> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const dataMessage = proto.dataMessage
|
2021-09-24 00:49:05 +00:00
|
|
|
? Proto.DataMessage.encode(proto.dataMessage).finish()
|
2021-07-09 19:36:10 +00:00
|
|
|
: undefined;
|
2021-06-02 20:29:31 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
const myE164 = window.textsecure.storage.user.getNumber();
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getUuid()?.toString();
|
2021-07-02 18:34:17 +00:00
|
|
|
const identifiers = recipients.filter(id => id !== myE164 && id !== myUuid);
|
2020-12-01 17:04:14 +00:00
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
if (identifiers.length === 0) {
|
|
|
|
return Promise.resolve({
|
2021-06-02 20:29:31 +00:00
|
|
|
dataMessage,
|
2021-05-25 22:40:04 +00:00
|
|
|
errors: [],
|
|
|
|
failoverIdentifiers: [],
|
|
|
|
successfulIdentifiers: [],
|
|
|
|
unidentifiedDeliveries: [],
|
|
|
|
});
|
|
|
|
}
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const callback = (res: CallbackResultType) => {
|
2021-06-02 20:29:31 +00:00
|
|
|
res.dataMessage = dataMessage;
|
2021-05-25 22:40:04 +00:00
|
|
|
if (res.errors && res.errors.length > 0) {
|
2021-07-20 01:10:09 +00:00
|
|
|
reject(new SendMessageProtoError(res));
|
2021-05-25 22:40:04 +00:00
|
|
|
} else {
|
|
|
|
resolve(res);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
this.sendMessageProto({
|
2021-07-15 23:48:09 +00:00
|
|
|
callback,
|
2021-05-28 19:11:19 +00:00
|
|
|
contentHint,
|
|
|
|
groupId,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
2021-07-15 23:48:09 +00:00
|
|
|
proto,
|
|
|
|
recipients: identifiers,
|
|
|
|
sendLogCallback,
|
|
|
|
timestamp,
|
2021-07-02 18:34:17 +00:00
|
|
|
});
|
2021-05-25 22:40:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-28 19:11:19 +00:00
|
|
|
async getSenderKeyDistributionMessage(
|
|
|
|
distributionId: string
|
|
|
|
): Promise<SenderKeyDistributionMessage> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const ourUuid = window.textsecure.storage.user.getCheckedUuid();
|
2021-05-25 22:40:04 +00:00
|
|
|
const ourDeviceId = parseIntOrThrow(
|
|
|
|
window.textsecure.storage.user.getDeviceId(),
|
2021-08-06 00:17:23 +00:00
|
|
|
'getSenderKeyDistributionMessage'
|
2021-05-25 22:40:04 +00:00
|
|
|
);
|
2020-09-28 23:25:18 +00:00
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const protocolAddress = ProtocolAddress.new(
|
|
|
|
ourUuid.toString(),
|
|
|
|
ourDeviceId
|
|
|
|
);
|
|
|
|
const address = new QualifiedAddress(
|
|
|
|
ourUuid,
|
|
|
|
new Address(ourUuid, ourDeviceId)
|
|
|
|
);
|
|
|
|
const senderKeyStore = new SenderKeys({ ourUuid });
|
2021-05-25 22:40:04 +00:00
|
|
|
|
2021-05-28 19:11:19 +00:00
|
|
|
return window.textsecure.storage.protocol.enqueueSenderKeyJob(
|
2021-05-25 22:40:04 +00:00
|
|
|
address,
|
|
|
|
async () =>
|
|
|
|
SenderKeyDistributionMessage.create(
|
|
|
|
protocolAddress,
|
|
|
|
distributionId,
|
|
|
|
senderKeyStore
|
|
|
|
)
|
2021-05-20 19:51:50 +00:00
|
|
|
);
|
2021-05-28 19:11:19 +00:00
|
|
|
}
|
2020-12-01 17:04:14 +00:00
|
|
|
|
2021-05-28 19:11:19 +00:00
|
|
|
// The one group send exception - a message that should never be sent via sender key
|
|
|
|
async sendSenderKeyDistributionMessage(
|
|
|
|
{
|
|
|
|
contentHint,
|
|
|
|
distributionId,
|
|
|
|
groupId,
|
|
|
|
identifiers,
|
2021-07-29 19:00:11 +00:00
|
|
|
}: Readonly<{
|
2021-05-28 19:11:19 +00:00
|
|
|
contentHint: number;
|
|
|
|
distributionId: string;
|
|
|
|
groupId: string | undefined;
|
2021-07-29 19:00:11 +00:00
|
|
|
identifiers: ReadonlyArray<string>;
|
|
|
|
}>,
|
|
|
|
options?: Readonly<SendOptionsType>
|
2021-05-28 19:11:19 +00:00
|
|
|
): Promise<CallbackResultType> {
|
2021-07-09 19:36:10 +00:00
|
|
|
const contentMessage = new Proto.Content();
|
2021-07-15 23:48:09 +00:00
|
|
|
const timestamp = Date.now();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-23 22:37:04 +00:00
|
|
|
`sendSenderKeyDistributionMessage: Sending ${distributionId} with timestamp ${timestamp}`
|
|
|
|
);
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const senderKeyDistributionMessage =
|
|
|
|
await this.getSenderKeyDistributionMessage(distributionId);
|
|
|
|
contentMessage.senderKeyDistributionMessage =
|
|
|
|
senderKeyDistributionMessage.serialize();
|
2020-12-01 17:04:14 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const sendLogCallback =
|
|
|
|
identifiers.length > 1
|
|
|
|
? this.makeSendLogCallback({
|
|
|
|
contentHint,
|
|
|
|
proto: Buffer.from(Proto.Content.encode(contentMessage).finish()),
|
|
|
|
sendType: 'senderKeyDistributionMessage',
|
|
|
|
timestamp,
|
|
|
|
})
|
|
|
|
: undefined;
|
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendGroupProto({
|
2021-05-28 19:11:19 +00:00
|
|
|
contentHint,
|
|
|
|
groupId,
|
2021-07-02 18:34:17 +00:00
|
|
|
options,
|
2021-07-15 23:48:09 +00:00
|
|
|
proto: contentMessage,
|
|
|
|
recipients: identifiers,
|
|
|
|
sendLogCallback,
|
|
|
|
timestamp,
|
2021-07-02 18:34:17 +00:00
|
|
|
});
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GroupV1-only functions; not to be used in the future
|
|
|
|
|
|
|
|
async leaveGroup(
|
|
|
|
groupId: string,
|
|
|
|
groupIdentifiers: Array<string>,
|
|
|
|
options?: SendOptionsType
|
|
|
|
): Promise<CallbackResultType> {
|
2021-07-15 23:48:09 +00:00
|
|
|
const timestamp = Date.now();
|
2021-07-09 19:36:10 +00:00
|
|
|
const proto = new Proto.Content({
|
|
|
|
dataMessage: {
|
|
|
|
group: {
|
|
|
|
id: Bytes.fromString(groupId),
|
|
|
|
type: Proto.GroupContext.Type.QUIT,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-05-28 19:11:19 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
const contentHint = ContentHint.RESENDABLE;
|
|
|
|
const sendLogCallback =
|
|
|
|
groupIdentifiers.length > 1
|
|
|
|
? this.makeSendLogCallback({
|
|
|
|
contentHint,
|
|
|
|
proto: Buffer.from(Proto.Content.encode(proto).finish()),
|
|
|
|
sendType: 'legacyGroupChange',
|
|
|
|
timestamp,
|
|
|
|
})
|
|
|
|
: undefined;
|
|
|
|
|
2021-07-02 18:34:17 +00:00
|
|
|
return this.sendGroupProto({
|
2021-07-15 23:48:09 +00:00
|
|
|
contentHint,
|
2021-07-02 18:34:17 +00:00
|
|
|
groupId: undefined, // only for GV2 ids
|
|
|
|
options,
|
2021-07-15 23:48:09 +00:00
|
|
|
proto,
|
|
|
|
recipients: groupIdentifiers,
|
|
|
|
sendLogCallback,
|
|
|
|
timestamp,
|
2021-07-02 18:34:17 +00:00
|
|
|
});
|
2021-05-25 22:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async sendExpirationTimerUpdateToGroup(
|
|
|
|
groupId: string,
|
2021-07-29 19:00:11 +00:00
|
|
|
groupIdentifiers: ReadonlyArray<string>,
|
2021-05-25 22:40:04 +00:00
|
|
|
expireTimer: number | undefined,
|
|
|
|
timestamp: number,
|
2021-09-24 00:49:05 +00:00
|
|
|
profileKey?: Readonly<Uint8Array>,
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<SendOptionsType>
|
2021-05-25 22:40:04 +00:00
|
|
|
): Promise<CallbackResultType> {
|
|
|
|
const myNumber = window.textsecure.storage.user.getNumber();
|
2021-09-10 02:38:11 +00:00
|
|
|
const myUuid = window.textsecure.storage.user.getUuid()?.toString();
|
2021-05-25 22:40:04 +00:00
|
|
|
const recipients = groupIdentifiers.filter(
|
|
|
|
identifier => identifier !== myNumber && identifier !== myUuid
|
|
|
|
);
|
2021-07-02 18:34:17 +00:00
|
|
|
const messageOptions = {
|
2020-12-01 17:04:14 +00:00
|
|
|
recipients,
|
2020-04-13 17:37:29 +00:00
|
|
|
timestamp,
|
|
|
|
expireTimer,
|
|
|
|
profileKey,
|
2021-07-09 19:36:10 +00:00
|
|
|
flags: Proto.DataMessage.Flags.EXPIRATION_TIMER_UPDATE,
|
2021-05-25 22:40:04 +00:00
|
|
|
group: {
|
|
|
|
id: groupId,
|
2021-07-09 19:36:10 +00:00
|
|
|
type: Proto.GroupContext.Type.DELIVER,
|
2021-05-25 22:40:04 +00:00
|
|
|
},
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
2021-07-15 23:48:09 +00:00
|
|
|
const proto = await this.getContentMessage(messageOptions);
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
if (recipients.length === 0) {
|
|
|
|
return Promise.resolve({
|
|
|
|
successfulIdentifiers: [],
|
|
|
|
failoverIdentifiers: [],
|
|
|
|
errors: [],
|
|
|
|
unidentifiedDeliveries: [],
|
2021-07-02 18:34:17 +00:00
|
|
|
dataMessage: await this.getDataMessage(messageOptions),
|
2020-04-13 17:37:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
2021-07-15 23:48:09 +00:00
|
|
|
const contentHint = ContentHint.RESENDABLE;
|
|
|
|
const sendLogCallback =
|
|
|
|
groupIdentifiers.length > 1
|
|
|
|
? this.makeSendLogCallback({
|
|
|
|
contentHint,
|
|
|
|
proto: Buffer.from(Proto.Content.encode(proto).finish()),
|
|
|
|
sendType: 'expirationTimerUpdate',
|
|
|
|
timestamp,
|
|
|
|
})
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
return this.sendGroupProto({
|
|
|
|
contentHint,
|
2021-07-02 18:34:17 +00:00
|
|
|
groupId: undefined, // only for GV2 ids
|
|
|
|
options,
|
2021-07-15 23:48:09 +00:00
|
|
|
proto,
|
|
|
|
recipients,
|
|
|
|
sendLogCallback,
|
|
|
|
timestamp,
|
2021-07-02 18:34:17 +00:00
|
|
|
});
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
// Simple pass-throughs
|
|
|
|
|
|
|
|
async getProfile(
|
|
|
|
number: string,
|
2021-07-29 19:00:11 +00:00
|
|
|
options: Readonly<{
|
2021-05-25 22:40:04 +00:00
|
|
|
accessKey?: string;
|
|
|
|
profileKeyVersion?: string;
|
|
|
|
profileKeyCredentialRequest?: string;
|
2021-11-02 23:01:13 +00:00
|
|
|
userLanguages: ReadonlyArray<string>;
|
|
|
|
}>
|
2021-11-12 01:17:29 +00:00
|
|
|
): ReturnType<WebAPIType['getProfile']> {
|
2021-05-25 22:40:04 +00:00
|
|
|
const { accessKey } = options;
|
|
|
|
|
|
|
|
if (accessKey) {
|
|
|
|
const unauthOptions = {
|
|
|
|
...options,
|
|
|
|
accessKey,
|
|
|
|
};
|
|
|
|
return this.server.getProfileUnauth(number, unauthOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.server.getProfile(number, options);
|
|
|
|
}
|
|
|
|
|
2021-11-12 01:17:29 +00:00
|
|
|
async getProfileForUsername(
|
|
|
|
username: string
|
|
|
|
): ReturnType<WebAPIType['getProfileForUsername']> {
|
|
|
|
return this.server.getProfileForUsername(username);
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
async getUuidsForE164s(
|
2021-07-29 19:00:11 +00:00
|
|
|
numbers: ReadonlyArray<string>
|
2021-11-08 23:32:31 +00:00
|
|
|
): Promise<Dictionary<UUIDStringType | null>> {
|
2021-05-25 22:40:04 +00:00
|
|
|
return this.server.getUuidsForE164s(numbers);
|
|
|
|
}
|
|
|
|
|
2021-11-08 23:32:31 +00:00
|
|
|
async getUuidsForE164sV2(
|
|
|
|
numbers: ReadonlyArray<string>
|
|
|
|
): Promise<Dictionary<UUIDStringType | null>> {
|
|
|
|
return this.server.getUuidsForE164sV2(numbers);
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
async getAvatar(path: string): Promise<ReturnType<WebAPIType['getAvatar']>> {
|
2021-05-25 22:40:04 +00:00
|
|
|
return this.server.getAvatar(path);
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
async getSticker(
|
|
|
|
packId: string,
|
|
|
|
stickerId: number
|
|
|
|
): Promise<ReturnType<WebAPIType['getSticker']>> {
|
2021-05-25 22:40:04 +00:00
|
|
|
return this.server.getSticker(packId, stickerId);
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
async getStickerPackManifest(
|
|
|
|
packId: string
|
|
|
|
): Promise<ReturnType<WebAPIType['getStickerPackManifest']>> {
|
2021-05-25 22:40:04 +00:00
|
|
|
return this.server.getStickerPackManifest(packId);
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
async createGroup(
|
2021-07-29 19:00:11 +00:00
|
|
|
group: Readonly<Proto.IGroup>,
|
|
|
|
options: Readonly<GroupCredentialsType>
|
2020-11-20 17:30:45 +00:00
|
|
|
): Promise<void> {
|
|
|
|
return this.server.createGroup(group, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async uploadGroupAvatar(
|
2021-07-29 19:00:11 +00:00
|
|
|
avatar: Readonly<Uint8Array>,
|
|
|
|
options: Readonly<GroupCredentialsType>
|
2020-11-20 17:30:45 +00:00
|
|
|
): Promise<string> {
|
|
|
|
return this.server.uploadGroupAvatar(avatar, options);
|
|
|
|
}
|
|
|
|
|
2021-07-29 19:00:11 +00:00
|
|
|
async getGroup(
|
|
|
|
options: Readonly<GroupCredentialsType>
|
|
|
|
): Promise<Proto.Group> {
|
2020-09-09 02:25:05 +00:00
|
|
|
return this.server.getGroup(options);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
async getGroupFromLink(
|
|
|
|
groupInviteLink: string,
|
2021-07-29 19:00:11 +00:00
|
|
|
auth: Readonly<GroupCredentialsType>
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.GroupJoinInfo> {
|
2021-01-29 22:16:48 +00:00
|
|
|
return this.server.getGroupFromLink(groupInviteLink, auth);
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
async getGroupLog(
|
|
|
|
startVersion: number,
|
2021-07-29 19:00:11 +00:00
|
|
|
options: Readonly<GroupCredentialsType>
|
2020-09-09 02:25:05 +00:00
|
|
|
): Promise<GroupLogResponseType> {
|
|
|
|
return this.server.getGroupLog(startVersion, options);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
async getGroupAvatar(key: string): Promise<Uint8Array> {
|
2020-09-09 02:25:05 +00:00
|
|
|
return this.server.getGroupAvatar(key);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
async modifyGroup(
|
2021-07-29 19:00:11 +00:00
|
|
|
changes: Readonly<Proto.GroupChange.IActions>,
|
|
|
|
options: Readonly<GroupCredentialsType>,
|
2021-01-29 22:16:48 +00:00
|
|
|
inviteLinkBase64?: string
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.IGroupChange> {
|
2021-01-29 22:16:48 +00:00
|
|
|
return this.server.modifyGroup(changes, options, inviteLinkBase64);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 22:40:04 +00:00
|
|
|
async sendWithSenderKey(
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Readonly<Uint8Array>,
|
|
|
|
accessKeys: Readonly<Uint8Array>,
|
2020-04-13 17:37:29 +00:00
|
|
|
timestamp: number,
|
2021-05-25 22:40:04 +00:00
|
|
|
online?: boolean
|
|
|
|
): Promise<MultiRecipient200ResponseType> {
|
|
|
|
return this.server.sendWithSenderKey(data, accessKeys, timestamp, online);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-09-28 23:46:31 +00:00
|
|
|
async fetchLinkPreviewMetadata(
|
|
|
|
href: string,
|
|
|
|
abortSignal: AbortSignal
|
|
|
|
): Promise<null | LinkPreviewMetadata> {
|
|
|
|
return this.server.fetchLinkPreviewMetadata(href, abortSignal);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchLinkPreviewImage(
|
|
|
|
href: string,
|
|
|
|
abortSignal: AbortSignal
|
|
|
|
): Promise<null | LinkPreviewImage> {
|
|
|
|
return this.server.fetchLinkPreviewImage(href, abortSignal);
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
async makeProxiedRequest(
|
|
|
|
url: string,
|
2021-07-29 19:00:11 +00:00
|
|
|
options?: Readonly<ProxiedRequestOptionsType>
|
2021-10-05 22:10:08 +00:00
|
|
|
): Promise<ReturnType<WebAPIType['makeProxiedRequest']>> {
|
2020-04-13 17:37:29 +00:00
|
|
|
return this.server.makeProxiedRequest(url, options);
|
|
|
|
}
|
2020-07-07 00:56:56 +00:00
|
|
|
|
|
|
|
async getStorageCredentials(): Promise<StorageServiceCredentials> {
|
|
|
|
return this.server.getStorageCredentials();
|
|
|
|
}
|
|
|
|
|
|
|
|
async getStorageManifest(
|
2021-07-29 19:00:11 +00:00
|
|
|
options: Readonly<StorageServiceCallOptionsType>
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<Uint8Array> {
|
2020-07-07 00:56:56 +00:00
|
|
|
return this.server.getStorageManifest(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getStorageRecords(
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Readonly<Uint8Array>,
|
2021-07-29 19:00:11 +00:00
|
|
|
options: Readonly<StorageServiceCallOptionsType>
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<Uint8Array> {
|
2020-07-07 00:56:56 +00:00
|
|
|
return this.server.getStorageRecords(data, options);
|
|
|
|
}
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
async modifyStorageRecords(
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Readonly<Uint8Array>,
|
2021-07-29 19:00:11 +00:00
|
|
|
options: Readonly<StorageServiceCallOptionsType>
|
2021-09-24 00:49:05 +00:00
|
|
|
): Promise<Uint8Array> {
|
2020-09-09 00:56:23 +00:00
|
|
|
return this.server.modifyStorageRecords(data, options);
|
|
|
|
}
|
2020-11-13 19:57:55 +00:00
|
|
|
|
|
|
|
async getGroupMembershipToken(
|
2021-07-29 19:00:11 +00:00
|
|
|
options: Readonly<GroupCredentialsType>
|
2021-06-22 14:46:42 +00:00
|
|
|
): Promise<Proto.GroupExternalCredential> {
|
2020-11-13 19:57:55 +00:00
|
|
|
return this.server.getGroupExternalCredential(options);
|
|
|
|
}
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
public async sendChallengeResponse(
|
2021-07-29 19:00:11 +00:00
|
|
|
challengeResponse: Readonly<ChallengeType>
|
2021-05-06 00:09:29 +00:00
|
|
|
): Promise<void> {
|
|
|
|
return this.server.sendChallengeResponse(challengeResponse);
|
|
|
|
}
|
2021-07-19 19:26:06 +00:00
|
|
|
|
|
|
|
async putProfile(
|
2021-07-29 19:00:11 +00:00
|
|
|
jsonData: Readonly<ProfileRequestDataType>
|
2021-07-19 19:26:06 +00:00
|
|
|
): Promise<UploadAvatarHeadersType | undefined> {
|
|
|
|
return this.server.putProfile(jsonData);
|
|
|
|
}
|
|
|
|
|
|
|
|
async uploadAvatar(
|
2021-07-29 19:00:11 +00:00
|
|
|
requestHeaders: Readonly<UploadAvatarHeadersType>,
|
2021-09-24 00:49:05 +00:00
|
|
|
avatarData: Readonly<Uint8Array>
|
2021-07-19 19:26:06 +00:00
|
|
|
): Promise<string> {
|
|
|
|
return this.server.uploadAvatar(requestHeaders, avatarData);
|
|
|
|
}
|
2021-11-01 19:13:35 +00:00
|
|
|
|
|
|
|
async putUsername(
|
|
|
|
username: string
|
|
|
|
): Promise<ReturnType<WebAPIType['putUsername']>> {
|
|
|
|
return this.server.putUsername(username);
|
|
|
|
}
|
|
|
|
async deleteUsername(): Promise<ReturnType<WebAPIType['deleteUsername']>> {
|
|
|
|
return this.server.deleteUsername();
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|