2022-02-16 18:36:21 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
import type PQueue from 'p-queue';
|
|
|
|
import * as globalLogger from '../logging/log';
|
|
|
|
|
|
|
|
import * as durations from '../util/durations';
|
|
|
|
import { exponentialBackoffMaxAttempts } from '../util/exponentialBackoff';
|
|
|
|
import { commonShouldJobContinue } from './helpers/commonShouldJobContinue';
|
|
|
|
import { InMemoryQueues } from './helpers/InMemoryQueues';
|
|
|
|
import { jobQueueDatabaseStore } from './JobQueueDatabaseStore';
|
|
|
|
import { JobQueue } from './JobQueue';
|
|
|
|
|
|
|
|
import { sendNormalMessage } from './helpers/sendNormalMessage';
|
|
|
|
import { sendDirectExpirationTimerUpdate } from './helpers/sendDirectExpirationTimerUpdate';
|
|
|
|
import { sendGroupUpdate } from './helpers/sendGroupUpdate';
|
|
|
|
import { sendDeleteForEveryone } from './helpers/sendDeleteForEveryone';
|
|
|
|
import { sendProfileKey } from './helpers/sendProfileKey';
|
|
|
|
import { sendReaction } from './helpers/sendReaction';
|
2022-08-02 19:31:55 +00:00
|
|
|
import { sendStory } from './helpers/sendStory';
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
import type { LoggerType } from '../types/Logging';
|
|
|
|
import { ConversationVerificationState } from '../state/ducks/conversationsEnums';
|
|
|
|
import { sleep } from '../util/sleep';
|
2022-03-21 21:19:37 +00:00
|
|
|
import { MINUTE } from '../util/durations';
|
2022-02-16 18:36:21 +00:00
|
|
|
import {
|
|
|
|
OutgoingIdentityKeyError,
|
2022-03-21 21:19:37 +00:00
|
|
|
SendMessageChallengeError,
|
2022-02-16 18:36:21 +00:00
|
|
|
SendMessageProtoError,
|
|
|
|
} from '../textsecure/Errors';
|
|
|
|
import { strictAssert } from '../util/assert';
|
|
|
|
import { missingCaseError } from '../util/missingCaseError';
|
|
|
|
import { explodePromise } from '../util/explodePromise';
|
2022-03-21 21:19:37 +00:00
|
|
|
import type { Job } from './Job';
|
|
|
|
import type { ParsedJob } from './types';
|
2022-06-13 21:39:35 +00:00
|
|
|
import type SendMessage from '../textsecure/SendMessage';
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
// Note: generally, we only want to add to this list. If you do need to change one of
|
|
|
|
// these values, you'll likely need to write a database migration.
|
|
|
|
export const conversationQueueJobEnum = z.enum([
|
|
|
|
'DeleteForEveryone',
|
|
|
|
'DirectExpirationTimerUpdate',
|
|
|
|
'GroupUpdate',
|
|
|
|
'NormalMessage',
|
|
|
|
'ProfileKey',
|
|
|
|
'Reaction',
|
2022-08-02 19:31:55 +00:00
|
|
|
'Story',
|
2022-02-16 18:36:21 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
const deleteForEveryoneJobDataSchema = z.object({
|
|
|
|
type: z.literal(conversationQueueJobEnum.enum.DeleteForEveryone),
|
|
|
|
conversationId: z.string(),
|
|
|
|
messageId: z.string(),
|
|
|
|
recipients: z.array(z.string()),
|
|
|
|
revision: z.number().optional(),
|
|
|
|
targetTimestamp: z.number(),
|
|
|
|
});
|
|
|
|
export type DeleteForEveryoneJobData = z.infer<
|
|
|
|
typeof deleteForEveryoneJobDataSchema
|
|
|
|
>;
|
|
|
|
|
|
|
|
const expirationTimerUpdateJobDataSchema = z.object({
|
|
|
|
type: z.literal(conversationQueueJobEnum.enum.DirectExpirationTimerUpdate),
|
|
|
|
conversationId: z.string(),
|
|
|
|
expireTimer: z.number().or(z.undefined()),
|
|
|
|
// Note: no recipients/revision, because this job is for 1:1 conversations only!
|
|
|
|
});
|
|
|
|
export type ExpirationTimerUpdateJobData = z.infer<
|
|
|
|
typeof expirationTimerUpdateJobDataSchema
|
|
|
|
>;
|
|
|
|
|
|
|
|
const groupUpdateJobDataSchema = z.object({
|
|
|
|
type: z.literal(conversationQueueJobEnum.enum.GroupUpdate),
|
|
|
|
conversationId: z.string(),
|
|
|
|
groupChangeBase64: z.string().optional(),
|
|
|
|
recipients: z.array(z.string()),
|
|
|
|
revision: z.number(),
|
|
|
|
});
|
|
|
|
export type GroupUpdateJobData = z.infer<typeof groupUpdateJobDataSchema>;
|
|
|
|
|
|
|
|
const normalMessageSendJobDataSchema = z.object({
|
|
|
|
type: z.literal(conversationQueueJobEnum.enum.NormalMessage),
|
|
|
|
conversationId: z.string(),
|
|
|
|
messageId: z.string(),
|
|
|
|
// Note: recipients are baked into the message itself
|
|
|
|
revision: z.number().optional(),
|
|
|
|
});
|
|
|
|
export type NormalMessageSendJobData = z.infer<
|
|
|
|
typeof normalMessageSendJobDataSchema
|
|
|
|
>;
|
|
|
|
|
|
|
|
const profileKeyJobDataSchema = z.object({
|
|
|
|
type: z.literal(conversationQueueJobEnum.enum.ProfileKey),
|
|
|
|
conversationId: z.string(),
|
|
|
|
// Note: we will use whichever recipients list is up to date when this job runs
|
|
|
|
revision: z.number().optional(),
|
|
|
|
});
|
|
|
|
export type ProfileKeyJobData = z.infer<typeof profileKeyJobDataSchema>;
|
|
|
|
|
|
|
|
const reactionJobDataSchema = z.object({
|
|
|
|
type: z.literal(conversationQueueJobEnum.enum.Reaction),
|
|
|
|
conversationId: z.string(),
|
|
|
|
messageId: z.string(),
|
|
|
|
// Note: recipients are baked into the message itself
|
|
|
|
revision: z.number().optional(),
|
|
|
|
});
|
|
|
|
export type ReactionJobData = z.infer<typeof reactionJobDataSchema>;
|
|
|
|
|
2022-08-02 19:31:55 +00:00
|
|
|
const storyJobDataSchema = z.object({
|
|
|
|
type: z.literal(conversationQueueJobEnum.enum.Story),
|
|
|
|
conversationId: z.string(),
|
|
|
|
// Note: recipients are baked into the message itself
|
|
|
|
messageIds: z.string().array(),
|
|
|
|
timestamp: z.number(),
|
|
|
|
revision: z.number().optional(),
|
|
|
|
});
|
|
|
|
export type StoryJobData = z.infer<typeof storyJobDataSchema>;
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
export const conversationQueueJobDataSchema = z.union([
|
|
|
|
deleteForEveryoneJobDataSchema,
|
|
|
|
expirationTimerUpdateJobDataSchema,
|
|
|
|
groupUpdateJobDataSchema,
|
|
|
|
normalMessageSendJobDataSchema,
|
|
|
|
profileKeyJobDataSchema,
|
|
|
|
reactionJobDataSchema,
|
2022-08-02 19:31:55 +00:00
|
|
|
storyJobDataSchema,
|
2022-02-16 18:36:21 +00:00
|
|
|
]);
|
|
|
|
export type ConversationQueueJobData = z.infer<
|
|
|
|
typeof conversationQueueJobDataSchema
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type ConversationQueueJobBundle = {
|
|
|
|
isFinalAttempt: boolean;
|
2022-06-13 21:39:35 +00:00
|
|
|
log: LoggerType;
|
|
|
|
messaging: SendMessage;
|
2022-02-16 18:36:21 +00:00
|
|
|
shouldContinue: boolean;
|
|
|
|
timeRemaining: number;
|
|
|
|
timestamp: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
const MAX_RETRY_TIME = durations.DAY;
|
|
|
|
const MAX_ATTEMPTS = exponentialBackoffMaxAttempts(MAX_RETRY_TIME);
|
|
|
|
|
|
|
|
export class ConversationJobQueue extends JobQueue<ConversationQueueJobData> {
|
|
|
|
private readonly inMemoryQueues = new InMemoryQueues();
|
|
|
|
private readonly verificationWaitMap = new Map<
|
|
|
|
string,
|
|
|
|
{
|
|
|
|
resolve: (value: unknown) => unknown;
|
|
|
|
reject: (error: Error) => unknown;
|
|
|
|
promise: Promise<unknown>;
|
|
|
|
}
|
|
|
|
>();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
public override async add(
|
|
|
|
data: Readonly<ConversationQueueJobData>,
|
|
|
|
insert?: (job: ParsedJob<ConversationQueueJobData>) => Promise<void>
|
|
|
|
): Promise<Job<ConversationQueueJobData>> {
|
|
|
|
const { conversationId } = data;
|
2022-06-13 21:39:35 +00:00
|
|
|
strictAssert(
|
|
|
|
window.Signal.challengeHandler,
|
|
|
|
'conversationJobQueue.add: Missing challengeHandler!'
|
|
|
|
);
|
2022-03-21 21:19:37 +00:00
|
|
|
window.Signal.challengeHandler.maybeSolve(conversationId);
|
|
|
|
|
|
|
|
return super.add(data, insert);
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
protected parseData(data: unknown): ConversationQueueJobData {
|
|
|
|
return conversationQueueJobDataSchema.parse(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override getInMemoryQueue({
|
|
|
|
data,
|
|
|
|
}: Readonly<{ data: ConversationQueueJobData }>): PQueue {
|
|
|
|
return this.inMemoryQueues.get(data.conversationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private startVerificationWaiter(conversationId: string): Promise<unknown> {
|
|
|
|
const existing = this.verificationWaitMap.get(conversationId);
|
|
|
|
if (existing) {
|
|
|
|
globalLogger.info(
|
|
|
|
`startVerificationWaiter: Found existing waiter for conversation ${conversationId}. Returning it.`
|
|
|
|
);
|
|
|
|
return existing.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
globalLogger.info(
|
|
|
|
`startVerificationWaiter: Starting new waiter for conversation ${conversationId}.`
|
|
|
|
);
|
|
|
|
const { resolve, reject, promise } = explodePromise();
|
|
|
|
this.verificationWaitMap.set(conversationId, {
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
promise,
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public resolveVerificationWaiter(conversationId: string): void {
|
|
|
|
const existing = this.verificationWaitMap.get(conversationId);
|
|
|
|
if (existing) {
|
|
|
|
globalLogger.info(
|
|
|
|
`resolveVerificationWaiter: Found waiter for conversation ${conversationId}. Resolving.`
|
|
|
|
);
|
|
|
|
existing.resolve('resolveVerificationWaiter: success');
|
|
|
|
this.verificationWaitMap.delete(conversationId);
|
|
|
|
} else {
|
|
|
|
globalLogger.warn(
|
|
|
|
`resolveVerificationWaiter: Missing waiter for conversation ${conversationId}.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async run(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
timestamp,
|
|
|
|
}: Readonly<{ data: ConversationQueueJobData; timestamp: number }>,
|
|
|
|
{ attempt, log }: Readonly<{ attempt: number; log: LoggerType }>
|
|
|
|
): Promise<void> {
|
|
|
|
const { type, conversationId } = data;
|
|
|
|
const isFinalAttempt = attempt >= MAX_ATTEMPTS;
|
|
|
|
|
|
|
|
await window.ConversationController.load();
|
|
|
|
|
|
|
|
const conversation = window.ConversationController.get(conversationId);
|
|
|
|
if (!conversation) {
|
|
|
|
throw new Error(`Failed to find conversation ${conversationId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeRemaining: number;
|
|
|
|
let shouldContinue: boolean;
|
2022-04-12 19:41:12 +00:00
|
|
|
let count = 0;
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
// eslint-disable-next-line no-constant-condition
|
|
|
|
while (true) {
|
2022-04-12 19:41:12 +00:00
|
|
|
count += 1;
|
2022-02-16 18:36:21 +00:00
|
|
|
log.info('calculating timeRemaining and shouldContinue...');
|
|
|
|
timeRemaining = timestamp + MAX_RETRY_TIME - Date.now();
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
shouldContinue = await commonShouldJobContinue({
|
|
|
|
attempt,
|
|
|
|
log,
|
|
|
|
timeRemaining,
|
2022-04-12 19:41:12 +00:00
|
|
|
skipWait: count > 1,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
if (!shouldContinue) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
if (window.Signal.challengeHandler?.isRegistered(conversationId)) {
|
2022-03-21 21:19:37 +00:00
|
|
|
log.info(
|
|
|
|
'captcha challenge is pending for this conversation; waiting at most 5m...'
|
|
|
|
);
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await Promise.race([
|
|
|
|
this.startVerificationWaiter(conversation.id),
|
|
|
|
sleep(5 * MINUTE),
|
|
|
|
]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
const verificationData =
|
|
|
|
window.reduxStore.getState().conversations
|
|
|
|
.verificationDataByConversation[conversationId];
|
|
|
|
|
|
|
|
if (!verificationData) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
verificationData.type ===
|
|
|
|
ConversationVerificationState.PendingVerification
|
|
|
|
) {
|
|
|
|
log.info(
|
2022-03-21 21:19:37 +00:00
|
|
|
'verification is pending for this conversation; waiting at most 5m...'
|
2022-02-16 18:36:21 +00:00
|
|
|
);
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await Promise.race([
|
|
|
|
this.startVerificationWaiter(conversation.id),
|
2022-03-21 21:19:37 +00:00
|
|
|
sleep(5 * MINUTE),
|
2022-02-16 18:36:21 +00:00
|
|
|
]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
verificationData.type ===
|
|
|
|
ConversationVerificationState.VerificationCancelled
|
|
|
|
) {
|
|
|
|
if (verificationData.canceledAt >= timestamp) {
|
|
|
|
log.info(
|
|
|
|
'cancelling job; user cancelled out of verification dialog.'
|
|
|
|
);
|
|
|
|
shouldContinue = false;
|
|
|
|
} else {
|
|
|
|
log.info(
|
|
|
|
'clearing cancellation tombstone; continuing ahead with job'
|
|
|
|
);
|
|
|
|
window.reduxActions.conversations.clearCancelledConversationVerification(
|
|
|
|
conversation.id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw missingCaseError(verificationData);
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
if (!messaging) {
|
|
|
|
throw new Error('messaging interface is not available!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const jobBundle: ConversationQueueJobBundle = {
|
|
|
|
messaging,
|
2022-02-16 18:36:21 +00:00
|
|
|
isFinalAttempt,
|
|
|
|
shouldContinue,
|
|
|
|
timeRemaining,
|
|
|
|
timestamp,
|
|
|
|
log,
|
|
|
|
};
|
|
|
|
// Note: A six-letter variable makes below code autoformatting easier to read.
|
|
|
|
const jobSet = conversationQueueJobEnum.enum;
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (type) {
|
|
|
|
case jobSet.DeleteForEveryone:
|
|
|
|
await sendDeleteForEveryone(conversation, jobBundle, data);
|
|
|
|
break;
|
|
|
|
case jobSet.DirectExpirationTimerUpdate:
|
|
|
|
await sendDirectExpirationTimerUpdate(conversation, jobBundle, data);
|
|
|
|
break;
|
|
|
|
case jobSet.GroupUpdate:
|
|
|
|
await sendGroupUpdate(conversation, jobBundle, data);
|
|
|
|
break;
|
|
|
|
case jobSet.NormalMessage:
|
|
|
|
await sendNormalMessage(conversation, jobBundle, data);
|
|
|
|
break;
|
|
|
|
case jobSet.ProfileKey:
|
|
|
|
await sendProfileKey(conversation, jobBundle, data);
|
|
|
|
break;
|
|
|
|
case jobSet.Reaction:
|
|
|
|
await sendReaction(conversation, jobBundle, data);
|
|
|
|
break;
|
2022-08-02 19:31:55 +00:00
|
|
|
case jobSet.Story:
|
|
|
|
await sendStory(conversation, jobBundle, data);
|
|
|
|
break;
|
2022-02-16 18:36:21 +00:00
|
|
|
default: {
|
|
|
|
// Note: This should never happen, because the zod call in parseData wouldn't
|
|
|
|
// accept data that doesn't look like our type specification.
|
|
|
|
const problem: never = type;
|
|
|
|
log.error(
|
|
|
|
`conversationJobQueue: Got job with type ${problem}; Cancelling job.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error: unknown) {
|
2022-05-31 19:46:56 +00:00
|
|
|
const untrustedUuids: Array<string> = [];
|
2022-03-21 21:19:37 +00:00
|
|
|
|
|
|
|
const processError = (toProcess: unknown) => {
|
|
|
|
if (toProcess instanceof OutgoingIdentityKeyError) {
|
|
|
|
const failedConversation = window.ConversationController.getOrCreate(
|
|
|
|
toProcess.identifier,
|
|
|
|
'private'
|
|
|
|
);
|
|
|
|
strictAssert(failedConversation, 'Conversation should be created');
|
2022-05-31 19:46:56 +00:00
|
|
|
const uuid = failedConversation.get('uuid');
|
|
|
|
if (!uuid) {
|
|
|
|
log.error(
|
|
|
|
`failedConversation: Conversation ${failedConversation.idForLogging()} missing UUID!`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
untrustedUuids.push(uuid);
|
2022-03-21 21:19:37 +00:00
|
|
|
} else if (toProcess instanceof SendMessageChallengeError) {
|
2022-06-13 21:39:35 +00:00
|
|
|
window.Signal.challengeHandler?.register(
|
2022-03-21 21:19:37 +00:00
|
|
|
{
|
|
|
|
conversationId,
|
|
|
|
createdAt: Date.now(),
|
|
|
|
retryAt: toProcess.retryAt,
|
|
|
|
token: toProcess.data?.token,
|
|
|
|
},
|
|
|
|
toProcess.data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
processError(error);
|
|
|
|
if (error instanceof SendMessageProtoError) {
|
|
|
|
(error.errors || []).forEach(processError);
|
2022-02-16 18:36:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 19:46:56 +00:00
|
|
|
if (untrustedUuids.length) {
|
2022-02-16 18:36:21 +00:00
|
|
|
log.error(
|
2022-05-31 19:46:56 +00:00
|
|
|
`Send failed because ${untrustedUuids.length} conversation(s) were untrusted. Adding to verification list.`
|
2022-02-16 18:36:21 +00:00
|
|
|
);
|
|
|
|
window.reduxActions.conversations.conversationStoppedByMissingVerification(
|
|
|
|
{
|
|
|
|
conversationId: conversation.id,
|
2022-05-31 19:46:56 +00:00
|
|
|
untrustedUuids,
|
2022-02-16 18:36:21 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const conversationJobQueue = new ConversationJobQueue({
|
|
|
|
store: jobQueueDatabaseStore,
|
|
|
|
queueType: 'conversation',
|
|
|
|
maxAttempts: MAX_ATTEMPTS,
|
|
|
|
});
|