diff --git a/ts/textsecure/OutgoingMessage.ts b/ts/textsecure/OutgoingMessage.ts index 3dac943aed98..4e16a85e813f 100644 --- a/ts/textsecure/OutgoingMessage.ts +++ b/ts/textsecure/OutgoingMessage.ts @@ -21,7 +21,7 @@ import { UnidentifiedSenderMessageContent, } from '@signalapp/signal-client'; -import type { WebAPIType } from './WebAPI'; +import type { WebAPIType, MessageType } from './WebAPI'; import { SendMetadataType, SendOptionsType } from './SendMessage'; import { OutgoingIdentityKeyError, @@ -52,13 +52,6 @@ export type SendLogCallbackType = (options: { deviceIds: Array; }) => Promise; -type SendMetadata = { - type: number; - destinationDeviceId: number; - destinationRegistrationId: number; - content: string; -}; - export const serializedCertificateSchema = z .object({ expires: z.number().optional(), @@ -289,7 +282,7 @@ export default class OutgoingMessage { async transmitMessage( identifier: string, - jsonData: Array, + jsonData: ReadonlyArray, timestamp: number, { accessKey }: { accessKey?: string } = {} ): Promise { @@ -419,7 +412,7 @@ export default class OutgoingMessage { new Address(theirUuid, destinationDeviceId) ); - return window.textsecure.storage.protocol.enqueueSessionJob( + return window.textsecure.storage.protocol.enqueueSessionJob( address, async () => { const protocolAddress = ProtocolAddress.new( @@ -494,7 +487,7 @@ export default class OutgoingMessage { ); }) ) - .then(async (jsonData: Array) => { + .then(async (jsonData: Array) => { if (sealedSender) { return this.transmitMessage(identifier, jsonData, this.timestamp, { accessKey, diff --git a/ts/textsecure/WebAPI.ts b/ts/textsecure/WebAPI.ts index d392606ad850..8602793fd2a5 100644 --- a/ts/textsecure/WebAPI.ts +++ b/ts/textsecure/WebAPI.ts @@ -28,7 +28,7 @@ import { v4 as getGuid } from 'uuid'; import { z } from 'zod'; import Long from 'long'; -import { assert, strictAssert } from '../util/assert'; +import { assert } from '../util/assert'; import * as durations from '../util/durations'; import { getUserAgent } from '../util/getUserAgent'; import { toWebSafeBase64 } from '../util/webSafeBase64'; @@ -103,69 +103,6 @@ function getSgxConstants() { return sgxConstantCache; } -const _call = (object: any) => Object.prototype.toString.call(object); - -// TODO: DESKTOP-2424 -const ArrayBufferToString = _call(new ArrayBuffer(0)); -const Uint8ArrayToString = _call(new Uint8Array()); - -function _getString(thing: any): string { - if (typeof thing !== 'string') { - if (_call(thing) === Uint8ArrayToString) { - return String.fromCharCode.apply(null, thing); - } - if (_call(thing) === ArrayBufferToString) { - return _getString(new Uint8Array(thing)); - } - } - - return thing; -} - -function _getStringable(thing: any) { - return ( - typeof thing === 'string' || - typeof thing === 'number' || - typeof thing === 'boolean' || - (thing === Object(thing) && - (_call(thing) === ArrayBufferToString || - _call(thing) === Uint8ArrayToString)) - ); -} - -function _ensureStringed(thing: any): any { - if (_getStringable(thing)) { - return _getString(thing); - } - if (thing instanceof Array) { - const res = []; - for (let i = 0; i < thing.length; i += 1) { - res[i] = _ensureStringed(thing[i]); - } - - return res; - } - if (thing === Object(thing)) { - const res: any = {}; - for (const key in thing) { - res[key] = _ensureStringed(thing[key]); - } - - return res; - } - if (thing === null) { - return null; - } - if (thing === undefined) { - return undefined; - } - throw new Error(`unsure of how to jsonify object of type ${typeof thing}`); -} - -function _jsonThing(thing: any): string { - return JSON.stringify(_ensureStringed(thing)); -} - function _createRedactor( ...toReplace: ReadonlyArray ): RedactUrl { @@ -639,7 +576,12 @@ type InitializeOptionsType = { version: string; }; -type MessageType = unknown; +export type MessageType = Readonly<{ + type: number; + destinationDeviceId: number; + destinationRegistrationId: number; + content: string; +}>; type AjaxOptionsType = { accessKey?: string; @@ -882,13 +824,13 @@ export type WebAPIType = { requestVerificationVoice: (number: string) => Promise; sendMessages: ( destination: string, - messageArray: Array, + messageArray: ReadonlyArray, timestamp: number, online?: boolean ) => Promise; sendMessagesUnauth: ( destination: string, - messageArray: Array, + messageArray: ReadonlyArray, timestamp: number, online?: boolean, options?: { accessKey?: string } @@ -1143,7 +1085,7 @@ export function initialize({ contentType: param.contentType || 'application/json; charset=utf-8', data: param.data || - (param.jsonData ? _jsonThing(param.jsonData) : undefined), + (param.jsonData ? JSON.stringify(param.jsonData) : undefined), headers: param.headers, host: param.host || url, password: param.password || password, @@ -1722,15 +1664,9 @@ export function initialize({ return handleKeys(keys); } - function validateMessages(messages: Array): void { - for (const message of messages) { - strictAssert(message !== null, 'Attempting to send `null` message'); - } - } - async function sendMessagesUnauth( destination: string, - messages: Array, + messages: ReadonlyArray, timestamp: number, online?: boolean, { accessKey }: { accessKey?: string } = {} @@ -1742,8 +1678,6 @@ export function initialize({ jsonData = { messages, timestamp }; } - validateMessages(messages); - await _ajax({ call: 'messages', httpType: 'PUT', @@ -1757,7 +1691,7 @@ export function initialize({ async function sendMessages( destination: string, - messages: Array, + messages: ReadonlyArray, timestamp: number, online?: boolean ) { @@ -1768,8 +1702,6 @@ export function initialize({ jsonData = { messages, timestamp }; } - validateMessages(messages); - await _ajax({ call: 'messages', httpType: 'PUT',