2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-08-12 18:15:55 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { chunk } from 'lodash';
|
2021-09-17 18:27:53 +00:00
|
|
|
import type { LoggerType } from '../../types/Logging';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { getSendOptions } from '../../util/getSendOptions';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SendTypesType } from '../../util/handleMessageSend';
|
|
|
|
import { handleMessageSend } from '../../util/handleMessageSend';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { isNotNil } from '../../util/isNotNil';
|
2021-08-30 22:59:34 +00:00
|
|
|
import { strictAssert } from '../../util/assert';
|
|
|
|
import { isRecord } from '../../util/isRecord';
|
2021-08-17 15:43:26 +00:00
|
|
|
|
|
|
|
import { commonShouldJobContinue } from './commonShouldJobContinue';
|
|
|
|
import { handleCommonJobRequestError } from './handleCommonJobRequestError';
|
2022-02-08 17:30:42 +00:00
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
2022-06-13 21:39:35 +00:00
|
|
|
import type SendMessage from '../../textsecure/SendMessage';
|
2021-08-12 18:15:55 +00:00
|
|
|
|
|
|
|
const CHUNK_SIZE = 100;
|
|
|
|
|
2021-08-30 22:59:34 +00:00
|
|
|
export type SyncType = {
|
|
|
|
messageId?: string;
|
|
|
|
senderE164?: string;
|
|
|
|
senderUuid?: string;
|
|
|
|
timestamp: number;
|
|
|
|
};
|
2022-02-08 17:30:42 +00:00
|
|
|
export enum SyncTypeList {
|
|
|
|
Read = 'Read',
|
|
|
|
View = 'View',
|
|
|
|
ViewOnceOpen = 'ViewOnceOpen',
|
|
|
|
}
|
2021-08-30 22:59:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse what _should_ be an array of `SyncType`s.
|
|
|
|
*
|
|
|
|
* Notably, `null`s made it into the job system and caused jobs to fail. This cleans that
|
|
|
|
* up in addition to validating the data.
|
|
|
|
*/
|
|
|
|
export function parseRawSyncDataArray(value: unknown): Array<SyncType> {
|
|
|
|
strictAssert(Array.isArray(value), 'syncs are not an array');
|
|
|
|
return value.map((item: unknown) => {
|
|
|
|
strictAssert(isRecord(item), 'sync is not an object');
|
|
|
|
|
|
|
|
const { messageId, senderE164, senderUuid, timestamp } = item;
|
|
|
|
strictAssert(typeof timestamp === 'number', 'timestamp should be a number');
|
|
|
|
|
|
|
|
return {
|
|
|
|
messageId: parseOptionalString('messageId', messageId),
|
|
|
|
senderE164: parseOptionalString('senderE164', senderE164),
|
|
|
|
senderUuid: parseOptionalString('senderUuid', senderUuid),
|
|
|
|
timestamp,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseOptionalString(name: string, value: unknown): undefined | string {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
return value;
|
|
|
|
}
|
2022-09-14 21:40:44 +00:00
|
|
|
if (value == null) {
|
2021-08-30 22:59:34 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
throw new Error(`${name} was not a string`);
|
|
|
|
}
|
|
|
|
|
2022-02-08 17:30:42 +00:00
|
|
|
export async function runSyncJob({
|
2021-08-12 18:15:55 +00:00
|
|
|
attempt,
|
2022-02-08 17:30:42 +00:00
|
|
|
type,
|
2021-08-17 15:43:26 +00:00
|
|
|
log,
|
2021-08-12 18:15:55 +00:00
|
|
|
maxRetryTime,
|
|
|
|
syncs,
|
|
|
|
timestamp,
|
|
|
|
}: Readonly<{
|
|
|
|
attempt: number;
|
2022-02-08 17:30:42 +00:00
|
|
|
type: SyncTypeList;
|
2021-08-17 15:43:26 +00:00
|
|
|
log: LoggerType;
|
2021-08-12 18:15:55 +00:00
|
|
|
maxRetryTime: number;
|
2021-08-30 22:59:34 +00:00
|
|
|
syncs: ReadonlyArray<SyncType>;
|
2021-08-12 18:15:55 +00:00
|
|
|
timestamp: number;
|
|
|
|
}>): Promise<void> {
|
2022-01-22 01:04:30 +00:00
|
|
|
if (!syncs.length) {
|
|
|
|
log.info("skipping this job because there's nothing to sync");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
let sendType: SendTypesType;
|
2022-02-08 17:30:42 +00:00
|
|
|
switch (type) {
|
|
|
|
case SyncTypeList.View:
|
|
|
|
sendType = 'viewSync';
|
|
|
|
break;
|
|
|
|
case SyncTypeList.Read:
|
|
|
|
sendType = 'readSync';
|
|
|
|
break;
|
|
|
|
case SyncTypeList.ViewOnceOpen:
|
|
|
|
sendType = 'viewOnceSync';
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
throw missingCaseError(type);
|
|
|
|
}
|
2021-08-12 18:15:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 22:14:16 +00:00
|
|
|
const syncTimestamps = syncs.map(sync => sync.timestamp);
|
|
|
|
log.info(
|
|
|
|
`sending ${sendType}(s) for timestamp(s) ${syncTimestamps.join(', ')}`
|
|
|
|
);
|
|
|
|
|
2021-09-02 22:31:21 +00:00
|
|
|
const timeRemaining = timestamp + maxRetryTime - Date.now();
|
|
|
|
|
2021-08-17 15:43:26 +00:00
|
|
|
const shouldContinue = await commonShouldJobContinue({
|
|
|
|
attempt,
|
|
|
|
log,
|
2021-09-02 22:31:21 +00:00
|
|
|
timeRemaining,
|
2022-04-12 19:41:12 +00:00
|
|
|
skipWait: false,
|
2021-08-12 18:15:55 +00:00
|
|
|
});
|
2021-08-17 15:43:26 +00:00
|
|
|
if (!shouldContinue) {
|
2021-08-12 18:15:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:45:56 +00:00
|
|
|
await window.ConversationController.load();
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
2021-08-12 18:15:55 +00:00
|
|
|
const sendOptions = await getSendOptions(ourConversation.attributes, {
|
|
|
|
syncMessage: true,
|
|
|
|
});
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
if (!messaging) {
|
|
|
|
throw new Error('messaging is not available!');
|
|
|
|
}
|
|
|
|
|
2022-01-22 01:04:30 +00:00
|
|
|
let doSync:
|
2022-06-13 21:39:35 +00:00
|
|
|
| SendMessage['syncReadMessages']
|
|
|
|
| SendMessage['syncView']
|
|
|
|
| SendMessage['syncViewOnceOpen'];
|
2022-02-08 17:30:42 +00:00
|
|
|
switch (type) {
|
|
|
|
case SyncTypeList.View:
|
2022-06-13 21:39:35 +00:00
|
|
|
doSync = messaging.syncView.bind(messaging);
|
2022-02-08 17:30:42 +00:00
|
|
|
break;
|
|
|
|
case SyncTypeList.Read:
|
2022-06-13 21:39:35 +00:00
|
|
|
doSync = messaging.syncReadMessages.bind(messaging);
|
2022-02-08 17:30:42 +00:00
|
|
|
break;
|
|
|
|
case SyncTypeList.ViewOnceOpen:
|
2022-06-13 21:39:35 +00:00
|
|
|
doSync = messaging.syncViewOnceOpen.bind(messaging);
|
2022-02-08 17:30:42 +00:00
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
throw missingCaseError(type);
|
|
|
|
}
|
2022-01-22 01:04:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
try {
|
|
|
|
await Promise.all(
|
|
|
|
chunk(syncs, CHUNK_SIZE).map(batch => {
|
|
|
|
const messageIds = batch.map(item => item.messageId).filter(isNotNil);
|
|
|
|
|
|
|
|
return handleMessageSend(doSync(batch, sendOptions), {
|
|
|
|
messageIds,
|
|
|
|
sendType,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (err: unknown) {
|
2021-09-02 22:31:21 +00:00
|
|
|
await handleCommonJobRequestError({ err, log, timeRemaining });
|
2021-08-12 18:15:55 +00:00
|
|
|
}
|
|
|
|
}
|