move AttachmentsDownload to typescript

pull/1576/head
Audric Ackermann 4 years ago
parent 34148e67ec
commit bc938f650e
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -204,7 +204,7 @@
shutdown: async () => { shutdown: async () => {
// Stop background processing // Stop background processing
window.Signal.AttachmentDownloads.stop(); window.libsession.Utils.AttachmentDownloads.stop();
// Stop processing incoming messages // Stop processing incoming messages
if (messageReceiver) { if (messageReceiver) {
@ -746,7 +746,7 @@
if (messageReceiver) { if (messageReceiver) {
await messageReceiver.close(); await messageReceiver.close();
} }
window.Signal.AttachmentDownloads.stop(); window.libsession.Utils.AttachmentDownloads.stop();
} }
let connectCount = 0; let connectCount = 0;
@ -801,7 +801,7 @@
messageReceiver.addEventListener('configuration', onConfiguration); messageReceiver.addEventListener('configuration', onConfiguration);
// messageReceiver.addEventListener('typing', onTyping); // messageReceiver.addEventListener('typing', onTyping);
window.Signal.AttachmentDownloads.start({ window.libsession.Utils.AttachmentDownloads.start({
logger: window.log, logger: window.log,
}); });

@ -9,7 +9,6 @@ const OS = require('../../ts/OS');
const Settings = require('./settings'); const Settings = require('./settings');
const Util = require('../../ts/util'); const Util = require('../../ts/util');
const LinkPreviews = require('./link_previews'); const LinkPreviews = require('./link_previews');
const AttachmentDownloads = require('./attachment_downloads');
const { Message } = require('../../ts/components/conversation/Message'); const { Message } = require('../../ts/components/conversation/Message');
// Components // Components
@ -173,7 +172,6 @@ exports.setup = (options = {}) => {
}; };
return { return {
AttachmentDownloads,
Components, Components,
Crypto, Crypto,
Data, Data,

@ -3,7 +3,7 @@ import _ from 'lodash';
import { MessageModel } from '../models/message'; import { MessageModel } from '../models/message';
import { saveMessage } from '../../ts/data/data'; import { saveMessage } from '../../ts/data/data';
import { fromBase64ToArrayBuffer } from '../session/utils/String'; import { fromBase64ToArrayBuffer } from '../session/utils/String';
import { AttachmentUtils } from '../session/utils'; import { AttachmentDownloads, AttachmentUtils } from '../session/utils';
import { ConversationModel } from '../models/conversation'; import { ConversationModel } from '../models/conversation';
export async function downloadAttachment(attachment: any) { export async function downloadAttachment(attachment: any) {
@ -168,7 +168,7 @@ async function processNormalAttachments(
const isOpenGroupV2 = convo.isOpenGroupV2(); const isOpenGroupV2 = convo.isOpenGroupV2();
const attachments = await Promise.all( const attachments = await Promise.all(
normalAttachments.map((attachment: any, index: any) => { normalAttachments.map((attachment: any, index: any) => {
return window.Signal.AttachmentDownloads.addJob(attachment, { return AttachmentDownloads.addJob(attachment, {
messageId: message.id, messageId: message.id,
type: 'attachment', type: 'attachment',
index, index,
@ -193,7 +193,7 @@ async function processPreviews(message: MessageModel, convo: ConversationModel):
} }
addedCount += 1; addedCount += 1;
const image = await window.Signal.AttachmentDownloads.addJob(item.image, { const image = await AttachmentDownloads.addJob(item.image, {
messageId: message.id, messageId: message.id,
type: 'preview', type: 'preview',
index, index,
@ -223,7 +223,7 @@ async function processAvatars(message: MessageModel, convo: ConversationModel):
addedCount += 1; addedCount += 1;
const avatarJob = await window.Signal.AttachmentDownloads.addJob(item.avatar.avatar, { const avatarJob = await AttachmentDownloads.addJob(item.avatar.avatar, {
messaeId: message.id, messaeId: message.id,
type: 'contact', type: 'contact',
index, index,
@ -268,7 +268,7 @@ async function processQuoteAttachments(
addedCount += 1; addedCount += 1;
const thumbnail = await window.Signal.AttachmentDownloads.addJob(item.thumbnail, { const thumbnail = await AttachmentDownloads.addJob(item.thumbnail, {
messageId: message.id, messageId: message.id,
type: 'quote', type: 'quote',
index, index,
@ -297,7 +297,7 @@ async function processGroupAvatar(
group = { group = {
...group, ...group,
avatar: await window.Signal.AttachmentDownloads.addJob(group.avatar, { avatar: await AttachmentDownloads.addJob(group.avatar, {
messageId: message.id, messageId: message.id,
type: 'group-avatar', type: 'group-avatar',
index: 0, index: 0,

@ -1,8 +1,7 @@
/* global Signal, setTimeout, clearTimeout, getMessageController, NewReceiver */ import { isNumber, omit } from 'lodash';
// tslint:disable-next-line: no-submodule-imports
const { isNumber, omit } = require('lodash'); import { default as getGuid } from 'uuid/v4';
const getGuid = require('uuid/v4'); import {
const {
getMessageById, getMessageById,
getNextAttachmentDownloadJobs, getNextAttachmentDownloadJobs,
removeAttachmentDownloadJob, removeAttachmentDownloadJob,
@ -10,33 +9,30 @@ const {
saveAttachmentDownloadJob, saveAttachmentDownloadJob,
saveMessage, saveMessage,
setAttachmentDownloadJobPending, setAttachmentDownloadJobPending,
} = require('../../ts/data/data'); } from '../../../ts/data/data';
import { MessageModel } from '../../models/message';
module.exports = { import { downloadAttachment } from '../../receiver/attachments';
start, import { MessageController } from '../messages';
stop,
addJob,
};
const MAX_ATTACHMENT_JOB_PARALLELISM = 3; const MAX_ATTACHMENT_JOB_PARALLELISM = 3;
const SECOND = 1000; const SECOND = 1000;
const MINUTE = 60 * SECOND; const MINUTE = SECOND * 60;
const HOUR = 60 * MINUTE; const HOUR = MINUTE * 60;
const TICK_INTERVAL = MINUTE; const TICK_INTERVAL = MINUTE;
const RETRY_BACKOFF = { const RETRY_BACKOFF = {
1: 30 * SECOND, 1: SECOND * 30,
2: 30 * MINUTE, 2: MINUTE * 30,
3: 6 * HOUR, 3: HOUR * 6,
}; };
let enabled = false; let enabled = false;
let timeout; let timeout: any;
let logger; let logger: any;
const _activeAttachmentDownloadJobs = {}; const _activeAttachmentDownloadJobs: any = {};
async function start(options = {}) { export async function start(options: any = {}) {
({ logger } = options); ({ logger } = options);
if (!logger) { if (!logger) {
throw new Error('attachment_downloads/start: logger must be provided!'); throw new Error('attachment_downloads/start: logger must be provided!');
@ -45,10 +41,10 @@ async function start(options = {}) {
enabled = true; enabled = true;
await resetAttachmentDownloadPending(); await resetAttachmentDownloadPending();
_tick(); void _tick();
} }
async function stop() { export function stop() {
enabled = false; enabled = false;
if (timeout) { if (timeout) {
clearTimeout(timeout); clearTimeout(timeout);
@ -56,7 +52,7 @@ async function stop() {
} }
} }
async function addJob(attachment, job = {}) { export async function addJob(attachment: any, job: any = {}) {
if (!attachment) { if (!attachment) {
throw new Error('attachments_download/addJob: attachment is required'); throw new Error('attachments_download/addJob: attachment is required');
} }
@ -85,7 +81,7 @@ async function addJob(attachment, job = {}) {
await saveAttachmentDownloadJob(toSave); await saveAttachmentDownloadJob(toSave);
_maybeStartJob(); void _maybeStartJob();
return { return {
...attachment, ...attachment,
@ -94,8 +90,9 @@ async function addJob(attachment, job = {}) {
}; };
} }
// tslint:disable: function-name
async function _tick() { async function _tick() {
_maybeStartJob(); await _maybeStartJob();
timeout = setTimeout(_tick, TICK_INTERVAL); timeout = setTimeout(_tick, TICK_INTERVAL);
} }
@ -124,13 +121,14 @@ async function _maybeStartJob() {
} }
const jobs = nextJobs.slice(0, Math.min(needed, nextJobs.length)); const jobs = nextJobs.slice(0, Math.min(needed, nextJobs.length));
// tslint:disable: one-variable-per-declaration
for (let i = 0, max = jobs.length; i < max; i += 1) { for (let i = 0, max = jobs.length; i < max; i += 1) {
const job = jobs[i]; const job = jobs[i];
_activeAttachmentDownloadJobs[job.id] = _runJob(job); _activeAttachmentDownloadJobs[job.id] = _runJob(job);
} }
} }
async function _runJob(job) { async function _runJob(job: any) {
const { id, messageId, attachment, type, index, attempts, isOpenGroupV2 } = job || {}; const { id, messageId, attachment, type, index, attempts, isOpenGroupV2 } = job || {};
let message; let message;
@ -145,7 +143,7 @@ async function _runJob(job) {
await _finishJob(null, id); await _finishJob(null, id);
return; return;
} }
message = getMessageController().register(found.id, found); message = MessageController.getInstance().register(found.id, found);
const pending = true; const pending = true;
await setAttachmentDownloadJobPending(id, pending); await setAttachmentDownloadJobPending(id, pending);
@ -153,7 +151,7 @@ async function _runJob(job) {
let downloaded; let downloaded;
try { try {
downloaded = await NewReceiver.downloadAttachment(attachment); downloaded = await downloadAttachment(attachment);
} catch (error) { } catch (error) {
// Attachments on the server expire after 60 days, then start returning 404 // Attachments on the server expire after 60 days, then start returning 404
if (error && error.code === 404) { if (error && error.code === 404) {
@ -171,28 +169,29 @@ async function _runJob(job) {
throw error; throw error;
} }
const upgradedAttachment = await Signal.Migrations.processNewAttachment(downloaded); const upgradedAttachment = await window.Signal.Migrations.processNewAttachment(downloaded);
await _addAttachmentToMessage(message, upgradedAttachment, { type, index }); await _addAttachmentToMessage(message, upgradedAttachment, { type, index });
await _finishJob(message, id); await _finishJob(message, id);
} catch (error) { } catch (error) {
const currentAttempt = (attempts || 0) + 1; // tslint:disable: restrict-plus-operands
const currentAttempt: 1 | 2 | 3 = (attempts || 0) + 1;
if (currentAttempt >= 3) { if (currentAttempt >= 3) {
logger.error( logger.error(
`_runJob: ${currentAttempt} failed attempts, marking attachment ${id} from message ${message.idForLogging()} as permament error:`, `_runJob: ${currentAttempt} failed attempts, marking attachment ${id} from message ${message?.idForLogging()} as permament error:`,
error && error.stack ? error.stack : error error && error.stack ? error.stack : error
); );
await _finishJob(message, id); await _finishJob(message || null, id);
await _addAttachmentToMessage(message, _markAttachmentAsError(attachment), { type, index }); await _addAttachmentToMessage(message, _markAttachmentAsError(attachment), { type, index });
return; return;
} }
logger.error( logger.error(
`_runJob: Failed to download attachment type ${type} for message ${message.idForLogging()}, attempt ${currentAttempt}:`, `_runJob: Failed to download attachment type ${type} for message ${message?.idForLogging()}, attempt ${currentAttempt}:`,
error && error.stack ? error.stack : error error && error.stack ? error.stack : error
); );
@ -204,37 +203,40 @@ async function _runJob(job) {
}; };
await saveAttachmentDownloadJob(failedJob); await saveAttachmentDownloadJob(failedJob);
// tslint:disable-next-line: no-dynamic-delete
delete _activeAttachmentDownloadJobs[id]; delete _activeAttachmentDownloadJobs[id];
_maybeStartJob(); void _maybeStartJob();
} }
} }
async function _finishJob(message, id) { async function _finishJob(message: MessageModel | null, id: string) {
if (message) { if (message) {
await saveMessage(message.attributes); await saveMessage(message.attributes);
const conversation = message.getConversation(); const conversation = message.getConversation();
if (conversation) { if (conversation) {
message.commit(); await message.commit();
} }
} }
await removeAttachmentDownloadJob(id); await removeAttachmentDownloadJob(id);
// tslint:disable-next-line: no-dynamic-delete
delete _activeAttachmentDownloadJobs[id]; delete _activeAttachmentDownloadJobs[id];
_maybeStartJob(); await _maybeStartJob();
} }
function getActiveJobCount() { function getActiveJobCount() {
return Object.keys(_activeAttachmentDownloadJobs).length; return Object.keys(_activeAttachmentDownloadJobs).length;
} }
function _markAttachmentAsError(attachment) { function _markAttachmentAsError(attachment: any) {
return { return {
...omit(attachment, ['key', 'digest', 'id']), ...omit(attachment, ['key', 'digest', 'id']),
error: true, error: true,
}; };
} }
async function _addAttachmentToMessage(message, attachment, { type, index }) { // tslint:disable-next-line: cyclomatic-complexity
async function _addAttachmentToMessage(message: any, attachment: any, { type, index }: any) {
if (!message) { if (!message) {
return; return;
} }
@ -310,7 +312,7 @@ async function _addAttachmentToMessage(message, attachment, { type, index }) {
const existingAvatar = group.avatar; const existingAvatar = group.avatar;
if (existingAvatar && existingAvatar.path) { if (existingAvatar && existingAvatar.path) {
await Signal.Migrations.deleteAttachmentData(existingAvatar.path); await window.Signal.Migrations.deleteAttachmentData(existingAvatar.path);
} }
_replaceAttachment(group, 'avatar', attachment, logPrefix); _replaceAttachment(group, 'avatar', attachment, logPrefix);
@ -322,7 +324,7 @@ async function _addAttachmentToMessage(message, attachment, { type, index }) {
); );
} }
function _replaceAttachment(object, key, newAttachment, logPrefix) { function _replaceAttachment(object: any, key: any, newAttachment: any, logPrefix: any) {
const oldAttachment = object[key]; const oldAttachment = object[key];
if (oldAttachment && oldAttachment.path) { if (oldAttachment && oldAttachment.path) {
logger.warn( logger.warn(

@ -9,6 +9,7 @@ import * as ToastUtils from './Toast';
import * as UserUtils from './User'; import * as UserUtils from './User';
import * as SyncUtils from './syncUtils'; import * as SyncUtils from './syncUtils';
import * as AttachmentsV2Utils from './AttachmentsV2'; import * as AttachmentsV2Utils from './AttachmentsV2';
import * as AttachmentDownloads from './AttachmentsDownload';
export * from './Attachments'; export * from './Attachments';
export * from './TypedEmitter'; export * from './TypedEmitter';
@ -26,4 +27,5 @@ export {
UserUtils, UserUtils,
SyncUtils, SyncUtils,
AttachmentsV2Utils, AttachmentsV2Utils,
AttachmentDownloads,
}; };

Loading…
Cancel
Save