2022-01-14 21:34:52 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import PQueue from 'p-queue';
|
2022-07-01 16:55:13 +00:00
|
|
|
import { isBoolean } from 'lodash';
|
2022-01-14 21:34:52 +00:00
|
|
|
|
|
|
|
import * as Bytes from '../Bytes';
|
|
|
|
import type { LoggerType } from '../types/Logging';
|
|
|
|
import { exponentialBackoffMaxAttempts } from '../util/exponentialBackoff';
|
|
|
|
import type { ParsedJob } from './types';
|
2023-10-28 00:14:35 +00:00
|
|
|
import type { JOB_STATUS } from './JobQueue';
|
2022-01-14 21:34:52 +00:00
|
|
|
import { JobQueue } from './JobQueue';
|
|
|
|
import { jobQueueDatabaseStore } from './JobQueueDatabaseStore';
|
|
|
|
import { DAY } from '../util/durations';
|
|
|
|
import { commonShouldJobContinue } from './helpers/commonShouldJobContinue';
|
|
|
|
import { SignalService as Proto } from '../protobuf';
|
|
|
|
import { handleMessageSend } from '../util/handleMessageSend';
|
|
|
|
import { getSendOptions } from '../util/getSendOptions';
|
|
|
|
import type { SingleProtoJobData } from '../textsecure/SendMessage';
|
|
|
|
import { singleProtoJobDataSchema } from '../textsecure/SendMessage';
|
2022-02-16 18:36:21 +00:00
|
|
|
import {
|
|
|
|
handleMultipleSendErrors,
|
|
|
|
maybeExpandErrors,
|
|
|
|
} from './helpers/handleMultipleSendErrors';
|
2022-02-22 20:34:57 +00:00
|
|
|
import { isConversationUnregistered } from '../util/isConversationUnregistered';
|
|
|
|
import { isConversationAccepted } from '../util/isConversationAccepted';
|
2024-10-02 19:03:10 +00:00
|
|
|
import { parseUnknown } from '../util/schemas';
|
2022-01-14 21:34:52 +00:00
|
|
|
|
|
|
|
const MAX_RETRY_TIME = DAY;
|
|
|
|
const MAX_PARALLEL_JOBS = 5;
|
|
|
|
const MAX_ATTEMPTS = exponentialBackoffMaxAttempts(MAX_RETRY_TIME);
|
|
|
|
|
|
|
|
export class SingleProtoJobQueue extends JobQueue<SingleProtoJobData> {
|
|
|
|
private parallelQueue = new PQueue({ concurrency: MAX_PARALLEL_JOBS });
|
|
|
|
|
2023-02-24 19:03:17 +00:00
|
|
|
protected override getQueues(): ReadonlySet<PQueue> {
|
|
|
|
return new Set([this.parallelQueue]);
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
protected override getInMemoryQueue(
|
|
|
|
_parsedJob: ParsedJob<SingleProtoJobData>
|
|
|
|
): PQueue {
|
|
|
|
return this.parallelQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected parseData(data: unknown): SingleProtoJobData {
|
2024-10-02 19:03:10 +00:00
|
|
|
return parseUnknown(singleProtoJobDataSchema, data);
|
2022-01-14 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected async run(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
timestamp,
|
|
|
|
}: Readonly<{ data: SingleProtoJobData; timestamp: number }>,
|
|
|
|
{ attempt, log }: Readonly<{ attempt: number; log: LoggerType }>
|
2023-10-28 00:14:35 +00:00
|
|
|
): Promise<typeof JOB_STATUS.NEEDS_RETRY | undefined> {
|
2022-01-14 21:34:52 +00:00
|
|
|
const timeRemaining = timestamp + MAX_RETRY_TIME - Date.now();
|
|
|
|
const isFinalAttempt = attempt >= MAX_ATTEMPTS;
|
|
|
|
|
|
|
|
const shouldContinue = await commonShouldJobContinue({
|
|
|
|
attempt,
|
|
|
|
log,
|
|
|
|
timeRemaining,
|
2022-04-12 19:41:12 +00:00
|
|
|
skipWait: false,
|
2022-01-14 21:34:52 +00:00
|
|
|
});
|
|
|
|
if (!shouldContinue) {
|
2023-10-28 00:14:35 +00:00
|
|
|
return undefined;
|
2022-01-14 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
contentHint,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId,
|
2022-01-14 21:34:52 +00:00
|
|
|
isSyncMessage,
|
|
|
|
messageIds = [],
|
|
|
|
protoBase64,
|
|
|
|
type,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent,
|
2022-01-14 21:34:52 +00:00
|
|
|
} = data;
|
|
|
|
log.info(
|
2023-08-16 20:54:39 +00:00
|
|
|
`starting ${type} send to ${serviceId} with timestamp ${timestamp}`
|
2022-01-14 21:34:52 +00:00
|
|
|
);
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const conversation = window.ConversationController.get(serviceId);
|
2022-01-14 21:34:52 +00:00
|
|
|
if (!conversation) {
|
2023-08-16 20:54:39 +00:00
|
|
|
throw new Error(`Failed to get conversation for serviceId ${serviceId}`);
|
2022-01-14 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 20:34:57 +00:00
|
|
|
if (!isConversationAccepted(conversation.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is not accepted; refusing to send`
|
|
|
|
);
|
2023-10-28 00:14:35 +00:00
|
|
|
return undefined;
|
2022-02-22 20:34:57 +00:00
|
|
|
}
|
|
|
|
if (isConversationUnregistered(conversation.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is unregistered; refusing to send`
|
|
|
|
);
|
2023-10-28 00:14:35 +00:00
|
|
|
return undefined;
|
2022-02-22 20:34:57 +00:00
|
|
|
}
|
2022-02-25 02:40:56 +00:00
|
|
|
if (conversation.isBlocked()) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is blocked; refusing to send`
|
2023-08-10 16:43:33 +00:00
|
|
|
);
|
2023-10-28 00:14:35 +00:00
|
|
|
return undefined;
|
2023-08-10 16:43:33 +00:00
|
|
|
}
|
2022-02-22 20:34:57 +00:00
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
const proto = Proto.Content.decode(Bytes.fromBase64(protoBase64));
|
|
|
|
const options = await getSendOptions(conversation.attributes, {
|
|
|
|
syncMessage: isSyncMessage,
|
|
|
|
});
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
if (!messaging) {
|
|
|
|
throw new Error('messaging is not available!');
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
try {
|
|
|
|
await handleMessageSend(
|
2022-06-13 21:39:35 +00:00
|
|
|
messaging.sendIndividualProto({
|
2022-01-14 21:34:52 +00:00
|
|
|
contentHint,
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2022-01-14 21:34:52 +00:00
|
|
|
options,
|
|
|
|
proto,
|
|
|
|
timestamp,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: isBoolean(urgent) ? urgent : true,
|
2022-01-14 21:34:52 +00:00
|
|
|
}),
|
|
|
|
{ messageIds, sendType: type }
|
|
|
|
);
|
|
|
|
} catch (error: unknown) {
|
|
|
|
await handleMultipleSendErrors({
|
2022-02-16 18:36:21 +00:00
|
|
|
errors: maybeExpandErrors(error),
|
2022-01-14 21:34:52 +00:00
|
|
|
isFinalAttempt,
|
|
|
|
log,
|
|
|
|
timeRemaining,
|
2022-02-16 18:36:21 +00:00
|
|
|
toThrow: error,
|
2022-01-14 21:34:52 +00:00
|
|
|
});
|
|
|
|
}
|
2023-10-28 00:14:35 +00:00
|
|
|
|
|
|
|
return undefined;
|
2022-01-14 21:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const singleProtoJobQueue = new SingleProtoJobQueue({
|
|
|
|
maxAttempts: MAX_ATTEMPTS,
|
|
|
|
queueType: 'single proto',
|
|
|
|
store: jobQueueDatabaseStore,
|
|
|
|
});
|