2024-04-16 00:11:48 +00:00
|
|
|
// Copyright 2024 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
import { omit } from 'lodash';
|
|
|
|
|
|
|
|
import * as durations from '../util/durations';
|
|
|
|
import * as log from '../logging/log';
|
|
|
|
import {
|
|
|
|
type AttachmentDownloadJobTypeType,
|
|
|
|
type AttachmentDownloadJobType,
|
2024-05-29 23:46:43 +00:00
|
|
|
type CoreAttachmentDownloadJobType,
|
|
|
|
coreAttachmentDownloadJobSchema,
|
2024-04-16 00:11:48 +00:00
|
|
|
} from '../types/AttachmentDownload';
|
|
|
|
import {
|
|
|
|
AttachmentNotFoundOnCdnError,
|
|
|
|
downloadAttachment,
|
|
|
|
} from '../util/downloadAttachment';
|
|
|
|
import dataInterface from '../sql/Client';
|
|
|
|
import { getValue } from '../RemoteConfig';
|
|
|
|
|
|
|
|
import { isInCall as isInCallSelector } from '../state/selectors/calling';
|
|
|
|
import { AttachmentSizeError, type AttachmentType } from '../types/Attachment';
|
|
|
|
import { __DEPRECATED$getMessageById } from '../messages/getMessageById';
|
|
|
|
import type { MessageModel } from '../models/messages';
|
|
|
|
import {
|
|
|
|
KIBIBYTE,
|
|
|
|
getMaximumIncomingAttachmentSizeInKb,
|
|
|
|
getMaximumIncomingTextAttachmentSizeInKb,
|
|
|
|
} from '../types/AttachmentSize';
|
|
|
|
import { addAttachmentToMessage } from '../messageModifiers/AttachmentDownloads';
|
|
|
|
import * as Errors from '../types/errors';
|
|
|
|
import { redactGenericText } from '../util/privacy';
|
2024-05-29 23:46:43 +00:00
|
|
|
import {
|
|
|
|
JobManager,
|
|
|
|
type JobManagerParamsType,
|
|
|
|
type JobManagerJobResultType,
|
|
|
|
} from './JobManager';
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
export enum AttachmentDownloadUrgency {
|
|
|
|
IMMEDIATE = 'immediate',
|
|
|
|
STANDARD = 'standard',
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type for adding a new job
|
|
|
|
export type NewAttachmentDownloadJobType = {
|
|
|
|
attachment: AttachmentType;
|
|
|
|
messageId: string;
|
|
|
|
receivedAt: number;
|
|
|
|
sentAt: number;
|
|
|
|
attachmentType: AttachmentDownloadJobTypeType;
|
|
|
|
urgency?: AttachmentDownloadUrgency;
|
|
|
|
};
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
const MAX_CONCURRENT_JOBS = 3;
|
|
|
|
|
|
|
|
const DEFAULT_RETRY_CONFIG = {
|
|
|
|
maxAttempts: 5,
|
|
|
|
backoffConfig: {
|
|
|
|
// 30 seconds, 5 minutes, 50 minutes, (max) 6 hrs
|
|
|
|
multiplier: 10,
|
|
|
|
firstBackoffs: [30 * durations.SECOND],
|
|
|
|
maxBackoffTime: 6 * durations.HOUR,
|
2024-04-16 00:11:48 +00:00
|
|
|
},
|
|
|
|
};
|
2024-05-29 23:46:43 +00:00
|
|
|
type AttachmentDownloadManagerParamsType = Omit<
|
|
|
|
JobManagerParamsType<CoreAttachmentDownloadJobType>,
|
|
|
|
'getNextJobs'
|
|
|
|
> & {
|
2024-04-16 00:11:48 +00:00
|
|
|
getNextJobs: (options: {
|
|
|
|
limit: number;
|
|
|
|
prioritizeMessageIds?: Array<string>;
|
|
|
|
timestamp?: number;
|
|
|
|
}) => Promise<Array<AttachmentDownloadJobType>>;
|
|
|
|
};
|
2024-05-29 23:46:43 +00:00
|
|
|
|
|
|
|
function getJobId(job: CoreAttachmentDownloadJobType): string {
|
|
|
|
const { messageId, attachmentType, digest } = job;
|
|
|
|
return `${messageId}.${attachmentType}.${digest}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJobIdForLogging(job: CoreAttachmentDownloadJobType): string {
|
|
|
|
const { sentAt, attachmentType, digest } = job;
|
|
|
|
const redactedDigest = redactGenericText(digest);
|
|
|
|
return `${sentAt}.${attachmentType}.${redactedDigest}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AttachmentDownloadManager extends JobManager<CoreAttachmentDownloadJobType> {
|
2024-04-16 00:11:48 +00:00
|
|
|
private visibleTimelineMessages: Array<string> = [];
|
2024-05-29 23:46:43 +00:00
|
|
|
private static _instance: AttachmentDownloadManager | undefined;
|
|
|
|
override logPrefix = 'AttachmentDownloadManager';
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
static defaultParams: AttachmentDownloadManagerParamsType = {
|
2024-05-29 23:46:43 +00:00
|
|
|
markAllJobsInactive: dataInterface.resetAttachmentDownloadActive,
|
2024-04-16 00:11:48 +00:00
|
|
|
saveJob: dataInterface.saveAttachmentDownloadJob,
|
|
|
|
removeJob: dataInterface.removeAttachmentDownloadJob,
|
2024-05-29 23:46:43 +00:00
|
|
|
getNextJobs: dataInterface.getNextAttachmentDownloadJobs,
|
2024-04-16 00:11:48 +00:00
|
|
|
runJob: runDownloadAttachmentJob,
|
2024-05-29 23:46:43 +00:00
|
|
|
shouldHoldOffOnStartingQueuedJobs: () => {
|
2024-04-16 00:11:48 +00:00
|
|
|
const reduxState = window.reduxStore?.getState();
|
|
|
|
if (reduxState) {
|
|
|
|
return isInCallSelector(reduxState);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2024-05-29 23:46:43 +00:00
|
|
|
getJobId,
|
|
|
|
getJobIdForLogging,
|
|
|
|
getRetryConfig: () => DEFAULT_RETRY_CONFIG,
|
|
|
|
maxConcurrentJobs: MAX_CONCURRENT_JOBS,
|
2024-04-16 00:11:48 +00:00
|
|
|
};
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
constructor(params: AttachmentDownloadManagerParamsType) {
|
|
|
|
super({
|
|
|
|
...params,
|
|
|
|
getNextJobs: ({ limit }) => {
|
|
|
|
return params.getNextJobs({
|
|
|
|
limit,
|
|
|
|
prioritizeMessageIds: this.visibleTimelineMessages,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
// @ts-expect-error we are overriding the return type of JobManager's addJob
|
|
|
|
override async addJob(
|
2024-04-16 00:11:48 +00:00
|
|
|
newJobData: NewAttachmentDownloadJobType
|
|
|
|
): Promise<AttachmentType> {
|
|
|
|
const {
|
|
|
|
attachment,
|
|
|
|
messageId,
|
|
|
|
attachmentType,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
urgency = AttachmentDownloadUrgency.STANDARD,
|
|
|
|
} = newJobData;
|
2024-05-29 23:46:43 +00:00
|
|
|
const parseResult = coreAttachmentDownloadJobSchema.safeParse({
|
2024-04-16 00:11:48 +00:00
|
|
|
messageId,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
attachmentType,
|
|
|
|
digest: attachment.digest,
|
|
|
|
contentType: attachment.contentType,
|
|
|
|
size: attachment.size,
|
|
|
|
attachment,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!parseResult.success) {
|
|
|
|
log.error(
|
|
|
|
`AttachmentDownloadManager/addJob(${sentAt}.${attachmentType}): invalid data`,
|
|
|
|
parseResult.error
|
|
|
|
);
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newJob = parseResult.data;
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
const { isAlreadyRunning } = await this._addJob(newJob, {
|
|
|
|
forceStart: urgency === AttachmentDownloadUrgency.IMMEDIATE,
|
|
|
|
});
|
2024-04-16 00:11:48 +00:00
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
if (isAlreadyRunning) {
|
|
|
|
return attachment;
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...attachment,
|
2024-05-29 23:46:43 +00:00
|
|
|
pending: !this.params.shouldHoldOffOnStartingQueuedJobs?.(),
|
2024-04-16 00:11:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
updateVisibleTimelineMessages(messageIds: Array<string>): void {
|
|
|
|
this.visibleTimelineMessages = messageIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
static get instance(): AttachmentDownloadManager {
|
|
|
|
if (!AttachmentDownloadManager._instance) {
|
2024-05-29 23:46:43 +00:00
|
|
|
AttachmentDownloadManager._instance = new AttachmentDownloadManager(
|
|
|
|
AttachmentDownloadManager.defaultParams
|
|
|
|
);
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
return AttachmentDownloadManager._instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async start(): Promise<void> {
|
|
|
|
log.info('AttachmentDownloadManager/starting');
|
|
|
|
await AttachmentDownloadManager.instance.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
static async stop(): Promise<void> {
|
|
|
|
log.info('AttachmentDownloadManager/stopping');
|
|
|
|
return AttachmentDownloadManager._instance?.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
static async addJob(
|
|
|
|
newJob: NewAttachmentDownloadJobType
|
|
|
|
): Promise<AttachmentType> {
|
|
|
|
return AttachmentDownloadManager.instance.addJob(newJob);
|
|
|
|
}
|
|
|
|
|
|
|
|
static updateVisibleTimelineMessages(messageIds: Array<string>): void {
|
|
|
|
AttachmentDownloadManager.instance.updateVisibleTimelineMessages(
|
|
|
|
messageIds
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
// TODO (DESKTOP-6913): if a prioritized job is selected, we will to update the
|
|
|
|
// in-memory job with that information so we can handle it differently, including
|
|
|
|
// e.g. downloading a thumbnail before the full-size version
|
2024-04-16 00:11:48 +00:00
|
|
|
async function runDownloadAttachmentJob(
|
|
|
|
job: AttachmentDownloadJobType,
|
|
|
|
isLastAttempt: boolean
|
2024-05-29 23:46:43 +00:00
|
|
|
): Promise<JobManagerJobResultType> {
|
2024-04-16 00:11:48 +00:00
|
|
|
const jobIdForLogging = getJobIdForLogging(job);
|
2024-04-19 17:09:51 +00:00
|
|
|
const logId = `AttachmentDownloadManager/runDownloadAttachmentJob/${jobIdForLogging}`;
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
const message = await __DEPRECATED$getMessageById(job.messageId);
|
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
log.error(`${logId} message not found`);
|
|
|
|
return { status: 'finished' };
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
log.info(`${logId}: Starting job`);
|
|
|
|
await runDownloadAttachmentJobInner(job, message);
|
|
|
|
return { status: 'finished' };
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
`${logId}: Failed to download attachment, attempt ${job.attempts}:`,
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (error instanceof AttachmentSizeError) {
|
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
_markAttachmentAsTooBig(job.attachment),
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type: job.attachmentType }
|
|
|
|
);
|
|
|
|
return { status: 'finished' };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error instanceof AttachmentNotFoundOnCdnError) {
|
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
_markAttachmentAsPermanentlyErrored(job.attachment),
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type: job.attachmentType }
|
|
|
|
);
|
|
|
|
|
|
|
|
return { status: 'finished' };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLastAttempt) {
|
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
_markAttachmentAsTransientlyErrored(job.attachment),
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type: job.attachmentType }
|
|
|
|
);
|
|
|
|
return { status: 'finished' };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove `pending` flag from the attachment and retry later
|
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
{
|
|
|
|
...job.attachment,
|
|
|
|
pending: false,
|
|
|
|
},
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type: job.attachmentType }
|
|
|
|
);
|
|
|
|
return { status: 'retry' };
|
|
|
|
} finally {
|
|
|
|
// This will fail if the message has been deleted before the download finished, which
|
|
|
|
// is good
|
|
|
|
await dataInterface.saveMessage(message.attributes, {
|
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runDownloadAttachmentJobInner(
|
|
|
|
job: AttachmentDownloadJobType,
|
|
|
|
message: MessageModel
|
|
|
|
): Promise<void> {
|
|
|
|
const { messageId, attachment, attachmentType: type } = job;
|
|
|
|
|
|
|
|
const jobIdForLogging = getJobIdForLogging(job);
|
2024-04-19 17:09:51 +00:00
|
|
|
const logId = `AttachmentDownloadManager/runDownloadJobInner(${jobIdForLogging})`;
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
if (!job || !attachment || !messageId) {
|
|
|
|
throw new Error(`${logId}: Key information required for job was missing.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`${logId}: starting`);
|
|
|
|
|
|
|
|
const maxInKib = getMaximumIncomingAttachmentSizeInKb(getValue);
|
|
|
|
const maxTextAttachmentSizeInKib =
|
|
|
|
getMaximumIncomingTextAttachmentSizeInKb(getValue);
|
|
|
|
|
|
|
|
const { size } = attachment;
|
|
|
|
const sizeInKib = size / KIBIBYTE;
|
|
|
|
|
|
|
|
if (!Number.isFinite(size) || size < 0 || sizeInKib > maxInKib) {
|
|
|
|
throw new AttachmentSizeError(
|
|
|
|
`${logId}: Attachment was ${sizeInKib}kib, max is ${maxInKib}kib`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (type === 'long-message' && sizeInKib > maxTextAttachmentSizeInKib) {
|
|
|
|
throw new AttachmentSizeError(
|
|
|
|
`${logId}: Text attachment was ${sizeInKib}kib, max is ${maxTextAttachmentSizeInKib}kib`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ ...attachment, pending: true },
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type }
|
|
|
|
);
|
|
|
|
|
|
|
|
const downloaded = await downloadAttachment(attachment);
|
|
|
|
|
|
|
|
const upgradedAttachment =
|
|
|
|
await window.Signal.Migrations.processNewAttachment(downloaded);
|
|
|
|
|
2024-04-18 20:49:12 +00:00
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-18 20:49:12 +00:00
|
|
|
omit(upgradedAttachment, ['error', 'pending']),
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-18 20:49:12 +00:00
|
|
|
{
|
|
|
|
type,
|
|
|
|
}
|
|
|
|
);
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _markAttachmentAsTooBig(attachment: AttachmentType): AttachmentType {
|
|
|
|
return {
|
|
|
|
..._markAttachmentAsPermanentlyErrored(attachment),
|
|
|
|
wasTooBig: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function _markAttachmentAsPermanentlyErrored(
|
|
|
|
attachment: AttachmentType
|
|
|
|
): AttachmentType {
|
|
|
|
return { ...omit(attachment, ['key', 'id']), pending: false, error: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
function _markAttachmentAsTransientlyErrored(
|
|
|
|
attachment: AttachmentType
|
|
|
|
): AttachmentType {
|
|
|
|
return { ...attachment, pending: false, error: true };
|
|
|
|
}
|