2021-08-12 18:15:55 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// 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';
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
if (value === undefined || value === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
throw new Error(`${name} was not a string`);
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
export async function runReadOrViewSyncJob({
|
|
|
|
attempt,
|
|
|
|
isView,
|
2021-08-17 15:43:26 +00:00
|
|
|
log,
|
2021-08-12 18:15:55 +00:00
|
|
|
maxRetryTime,
|
|
|
|
syncs,
|
|
|
|
timestamp,
|
|
|
|
}: Readonly<{
|
|
|
|
attempt: number;
|
|
|
|
isView: boolean;
|
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> {
|
|
|
|
let sendType: SendTypesType;
|
|
|
|
let doSync:
|
|
|
|
| typeof window.textsecure.messaging.syncReadMessages
|
|
|
|
| typeof window.textsecure.messaging.syncView;
|
|
|
|
if (isView) {
|
|
|
|
sendType = 'viewSync';
|
|
|
|
doSync = window.textsecure.messaging.syncView.bind(
|
|
|
|
window.textsecure.messaging
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
sendType = 'readSync';
|
|
|
|
doSync = window.textsecure.messaging.syncReadMessages.bind(
|
|
|
|
window.textsecure.messaging
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!syncs.length) {
|
2021-08-17 15:43:26 +00:00
|
|
|
log.info("skipping this job because there's nothing to sync");
|
2021-08-12 18:15:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ourConversation = window.ConversationController.getOurConversationOrThrow();
|
|
|
|
const sendOptions = await getSendOptions(ourConversation.attributes, {
|
|
|
|
syncMessage: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|