2021-08-17 15:43:26 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-disable class-methods-use-this */
|
|
|
|
|
|
|
|
import { z } from 'zod';
|
2021-08-26 14:10:58 +00:00
|
|
|
import * as durations from '../util/durations';
|
2021-09-17 18:27:53 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
2021-08-17 15:43:26 +00:00
|
|
|
import { exponentialBackoffMaxAttempts } from '../util/exponentialBackoff';
|
|
|
|
import { commonShouldJobContinue } from './helpers/commonShouldJobContinue';
|
|
|
|
import { sendViewedReceipt } from '../util/sendViewedReceipt';
|
|
|
|
|
|
|
|
import { JobQueue } from './JobQueue';
|
|
|
|
import { jobQueueDatabaseStore } from './JobQueueDatabaseStore';
|
|
|
|
import { handleCommonJobRequestError } from './helpers/handleCommonJobRequestError';
|
|
|
|
|
2021-08-26 14:10:58 +00:00
|
|
|
const MAX_RETRY_TIME = durations.DAY;
|
2021-08-17 15:43:26 +00:00
|
|
|
|
|
|
|
const viewedReceiptsJobDataSchema = z.object({
|
|
|
|
viewedReceipt: z.object({
|
|
|
|
messageId: z.string(),
|
|
|
|
senderE164: z.string().optional(),
|
|
|
|
senderUuid: z.string().optional(),
|
|
|
|
timestamp: z.number(),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
type ViewedReceiptsJobData = z.infer<typeof viewedReceiptsJobDataSchema>;
|
|
|
|
|
|
|
|
export class ViewedReceiptsJobQueue extends JobQueue<ViewedReceiptsJobData> {
|
|
|
|
protected parseData(data: unknown): ViewedReceiptsJobData {
|
|
|
|
return viewedReceiptsJobDataSchema.parse(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async run(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
timestamp,
|
|
|
|
}: Readonly<{ data: ViewedReceiptsJobData; timestamp: number }>,
|
|
|
|
{ attempt, log }: Readonly<{ attempt: number; log: LoggerType }>
|
|
|
|
): Promise<void> {
|
2021-09-02 22:31:21 +00:00
|
|
|
const timeRemaining = timestamp + MAX_RETRY_TIME - 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-17 15:43:26 +00:00
|
|
|
});
|
|
|
|
if (!shouldContinue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await sendViewedReceipt(data.viewedReceipt);
|
|
|
|
} catch (err: unknown) {
|
2021-09-02 22:31:21 +00:00
|
|
|
await handleCommonJobRequestError({ err, log, timeRemaining });
|
2021-08-17 15:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const viewedReceiptsJobQueue = new ViewedReceiptsJobQueue({
|
|
|
|
store: jobQueueDatabaseStore,
|
|
|
|
queueType: 'viewed receipts',
|
|
|
|
maxAttempts: exponentialBackoffMaxAttempts(MAX_RETRY_TIME),
|
|
|
|
});
|