2021-05-27 20:17:05 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import * as z from 'zod';
|
2021-08-26 14:10:58 +00:00
|
|
|
import * as durations from '../util/durations';
|
2021-07-23 17:23:50 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2021-05-27 20:17:05 +00:00
|
|
|
import { waitForOnline } from '../util/waitForOnline';
|
|
|
|
import { isDone as isDeviceLinked } from '../util/registration';
|
2021-09-17 18:27:53 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
2023-08-16 20:54:39 +00:00
|
|
|
import { aciSchema } from '../types/ServiceId';
|
2021-05-27 20:17:05 +00:00
|
|
|
import { map } from '../util/iterables';
|
|
|
|
|
2023-10-28 00:14:35 +00:00
|
|
|
import type { JOB_STATUS } from './JobQueue';
|
2021-05-27 20:17:05 +00:00
|
|
|
import { JobQueue } from './JobQueue';
|
|
|
|
import { jobQueueDatabaseStore } from './JobQueueDatabaseStore';
|
|
|
|
import { parseIntWithFallback } from '../util/parseIntWithFallback';
|
2021-07-23 17:23:50 +00:00
|
|
|
import type { WebAPIType } from '../textsecure/WebAPI';
|
2021-09-22 00:58:03 +00:00
|
|
|
import { HTTPError } from '../textsecure/Errors';
|
2023-02-24 19:03:17 +00:00
|
|
|
import { sleeper } from '../util/sleeper';
|
2021-05-27 20:17:05 +00:00
|
|
|
|
2021-08-26 14:10:58 +00:00
|
|
|
const RETRY_WAIT_TIME = durations.MINUTE;
|
2021-05-27 20:17:05 +00:00
|
|
|
const RETRYABLE_4XX_FAILURE_STATUSES = new Set([
|
2021-11-11 22:43:05 +00:00
|
|
|
404, 408, 410, 412, 413, 414, 417, 423, 424, 425, 426, 428, 429, 431, 449,
|
2021-05-27 20:17:05 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
const is4xxStatus = (code: number): boolean => code >= 400 && code <= 499;
|
|
|
|
const is5xxStatus = (code: number): boolean => code >= 500 && code <= 599;
|
|
|
|
const isRetriable4xxStatus = (code: number): boolean =>
|
|
|
|
RETRYABLE_4XX_FAILURE_STATUSES.has(code);
|
|
|
|
|
|
|
|
const reportSpamJobDataSchema = z.object({
|
2023-08-16 20:54:39 +00:00
|
|
|
aci: aciSchema,
|
2023-02-08 00:55:12 +00:00
|
|
|
token: z.string().optional(),
|
2021-05-27 20:17:05 +00:00
|
|
|
serverGuids: z.string().array().min(1).max(1000),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type ReportSpamJobData = z.infer<typeof reportSpamJobDataSchema>;
|
|
|
|
|
2021-07-21 21:10:08 +00:00
|
|
|
export class ReportSpamJobQueue extends JobQueue<ReportSpamJobData> {
|
2021-07-23 17:23:50 +00:00
|
|
|
private server?: WebAPIType;
|
|
|
|
|
|
|
|
public initialize({ server }: { server: WebAPIType }): void {
|
|
|
|
this.server = server;
|
|
|
|
}
|
|
|
|
|
2021-07-21 21:10:08 +00:00
|
|
|
protected parseData(data: unknown): ReportSpamJobData {
|
2021-05-27 20:17:05 +00:00
|
|
|
return reportSpamJobDataSchema.parse(data);
|
2021-07-21 21:10:08 +00:00
|
|
|
}
|
2021-05-27 20:17:05 +00:00
|
|
|
|
2021-08-17 15:43:26 +00:00
|
|
|
protected async run(
|
|
|
|
{ data }: Readonly<{ data: ReportSpamJobData }>,
|
|
|
|
{ log }: Readonly<{ log: LoggerType }>
|
2023-10-28 00:14:35 +00:00
|
|
|
): Promise<typeof JOB_STATUS.NEEDS_RETRY | undefined> {
|
2023-08-16 20:54:39 +00:00
|
|
|
const { aci: senderAci, token, serverGuids } = data;
|
2021-05-27 20:17:05 +00:00
|
|
|
|
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
window.storage.onready(resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!isDeviceLinked()) {
|
|
|
|
log.info("reportSpamJobQueue: skipping this job because we're unlinked");
|
2023-10-28 00:14:35 +00:00
|
|
|
return undefined;
|
2021-05-27 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await waitForOnline(window.navigator, window);
|
|
|
|
|
2021-07-23 17:23:50 +00:00
|
|
|
const { server } = this;
|
|
|
|
strictAssert(server !== undefined, 'ReportSpamJobQueue not initialized');
|
2021-05-27 20:17:05 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
2023-02-08 00:55:12 +00:00
|
|
|
map(serverGuids, serverGuid =>
|
2023-08-16 20:54:39 +00:00
|
|
|
server.reportMessage({ senderAci, serverGuid, token })
|
2023-02-08 00:55:12 +00:00
|
|
|
)
|
2021-05-27 20:17:05 +00:00
|
|
|
);
|
2023-10-28 00:14:35 +00:00
|
|
|
|
|
|
|
return undefined;
|
2021-05-27 20:17:05 +00:00
|
|
|
} catch (err: unknown) {
|
2021-09-22 00:58:03 +00:00
|
|
|
if (!(err instanceof HTTPError)) {
|
2021-05-27 20:17:05 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
const code = parseIntWithFallback(err.code, -1);
|
|
|
|
|
|
|
|
// This is an unexpected case, except for -1, which can happen for network failures.
|
|
|
|
if (code < 400) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code === 508) {
|
|
|
|
log.info(
|
|
|
|
'reportSpamJobQueue: server responded with 508. Giving up on this job'
|
|
|
|
);
|
2023-10-28 00:14:35 +00:00
|
|
|
return undefined;
|
2021-05-27 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isRetriable4xxStatus(code) || is5xxStatus(code)) {
|
|
|
|
log.info(
|
|
|
|
`reportSpamJobQueue: server responded with ${code} status code. Sleeping before our next attempt`
|
|
|
|
);
|
2023-02-24 19:03:17 +00:00
|
|
|
await sleeper.sleep(
|
|
|
|
RETRY_WAIT_TIME,
|
|
|
|
`reportSpamJobQueue: server responded with ${code} status code`
|
|
|
|
);
|
2021-05-27 20:17:05 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is4xxStatus(code)) {
|
|
|
|
log.error(
|
|
|
|
`reportSpamJobQueue: server responded with ${code} status code. Giving up on this job`
|
|
|
|
);
|
2023-10-28 00:14:35 +00:00
|
|
|
return undefined;
|
2021-05-27 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
2021-07-21 21:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const reportSpamJobQueue = new ReportSpamJobQueue({
|
|
|
|
store: jobQueueDatabaseStore,
|
|
|
|
queueType: 'report spam',
|
|
|
|
maxAttempts: 25,
|
2021-05-27 20:17:05 +00:00
|
|
|
});
|