2019-05-16 22:32:11 +00:00
|
|
|
/* global
|
|
|
|
ConversationController,
|
|
|
|
Whisper,
|
|
|
|
Signal,
|
|
|
|
setTimeout,
|
|
|
|
clearTimeout,
|
|
|
|
MessageController
|
|
|
|
*/
|
2019-01-30 20:15:07 +00:00
|
|
|
|
|
|
|
const { isFunction, isNumber, omit } = require('lodash');
|
2019-05-16 22:32:11 +00:00
|
|
|
const { computeHash } = require('./types/conversation');
|
2019-01-30 20:15:07 +00:00
|
|
|
const getGuid = require('uuid/v4');
|
|
|
|
const {
|
|
|
|
getMessageById,
|
|
|
|
getNextAttachmentDownloadJobs,
|
|
|
|
removeAttachmentDownloadJob,
|
|
|
|
resetAttachmentDownloadPending,
|
|
|
|
saveAttachmentDownloadJob,
|
|
|
|
saveMessage,
|
|
|
|
setAttachmentDownloadJobPending,
|
|
|
|
} = require('./data');
|
2019-03-13 20:38:28 +00:00
|
|
|
const { stringFromBytes } = require('./crypto');
|
2019-01-30 20:15:07 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
start,
|
|
|
|
stop,
|
|
|
|
addJob,
|
|
|
|
};
|
|
|
|
|
|
|
|
const MAX_ATTACHMENT_JOB_PARALLELISM = 3;
|
|
|
|
|
|
|
|
const SECOND = 1000;
|
|
|
|
const MINUTE = 60 * SECOND;
|
|
|
|
const HOUR = 60 * MINUTE;
|
|
|
|
const TICK_INTERVAL = MINUTE;
|
|
|
|
|
|
|
|
const RETRY_BACKOFF = {
|
|
|
|
1: 30 * SECOND,
|
|
|
|
2: 30 * MINUTE,
|
|
|
|
3: 6 * HOUR,
|
|
|
|
};
|
|
|
|
|
|
|
|
let enabled = false;
|
|
|
|
let timeout;
|
|
|
|
let getMessageReceiver;
|
|
|
|
let logger;
|
|
|
|
const _activeAttachmentDownloadJobs = {};
|
|
|
|
|
|
|
|
async function start(options = {}) {
|
|
|
|
({ getMessageReceiver, logger } = options);
|
|
|
|
if (!isFunction(getMessageReceiver)) {
|
|
|
|
throw new Error(
|
|
|
|
'attachment_downloads/start: getMessageReceiver must be a function'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!logger) {
|
|
|
|
throw new Error('attachment_downloads/start: logger must be provided!');
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled = true;
|
|
|
|
await resetAttachmentDownloadPending();
|
|
|
|
|
|
|
|
_tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function stop() {
|
|
|
|
enabled = false;
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addJob(attachment, job = {}) {
|
|
|
|
if (!attachment) {
|
|
|
|
throw new Error('attachments_download/addJob: attachment is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { messageId, type, index } = job;
|
|
|
|
if (!messageId) {
|
|
|
|
throw new Error('attachments_download/addJob: job.messageId is required');
|
|
|
|
}
|
|
|
|
if (!type) {
|
|
|
|
throw new Error('attachments_download/addJob: job.type is required');
|
|
|
|
}
|
|
|
|
if (!isNumber(index)) {
|
|
|
|
throw new Error('attachments_download/addJob: index must be a number');
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = getGuid();
|
|
|
|
const timestamp = Date.now();
|
|
|
|
const toSave = {
|
|
|
|
...job,
|
|
|
|
id,
|
|
|
|
attachment,
|
|
|
|
timestamp,
|
|
|
|
pending: 0,
|
|
|
|
attempts: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveAttachmentDownloadJob(toSave);
|
|
|
|
|
|
|
|
_maybeStartJob();
|
|
|
|
|
|
|
|
return {
|
|
|
|
...attachment,
|
|
|
|
pending: true,
|
|
|
|
downloadJobId: id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _tick() {
|
2019-06-24 20:39:37 +00:00
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = null;
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
_maybeStartJob();
|
|
|
|
timeout = setTimeout(_tick, TICK_INTERVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _maybeStartJob() {
|
|
|
|
if (!enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const jobCount = getActiveJobCount();
|
|
|
|
const limit = MAX_ATTACHMENT_JOB_PARALLELISM - jobCount;
|
|
|
|
if (limit <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextJobs = await getNextAttachmentDownloadJobs(limit);
|
|
|
|
if (nextJobs.length <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent the race condition caused by two parallel database calls, eached kicked
|
|
|
|
// off because the jobCount wasn't at the max.
|
|
|
|
const secondJobCount = getActiveJobCount();
|
|
|
|
const needed = MAX_ATTACHMENT_JOB_PARALLELISM - secondJobCount;
|
|
|
|
if (needed <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const jobs = nextJobs.slice(0, Math.min(needed, nextJobs.length));
|
|
|
|
for (let i = 0, max = jobs.length; i < max; i += 1) {
|
|
|
|
const job = jobs[i];
|
2019-09-17 21:09:01 +00:00
|
|
|
const existing = _activeAttachmentDownloadJobs[job.id];
|
|
|
|
if (existing) {
|
|
|
|
logger.warn(`_maybeStartJob: Job ${job.id} is already running`);
|
|
|
|
} else {
|
|
|
|
_activeAttachmentDownloadJobs[job.id] = _runJob(job);
|
|
|
|
}
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _runJob(job) {
|
|
|
|
const { id, messageId, attachment, type, index, attempts } = job || {};
|
|
|
|
let message;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!job || !attachment || !messageId) {
|
|
|
|
throw new Error(
|
|
|
|
`_runJob: Key information required for job was missing. Job id: ${id}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:10:30 +00:00
|
|
|
const found = await getMessageById(messageId, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
if (!found) {
|
2019-01-30 20:15:07 +00:00
|
|
|
logger.error('_runJob: Source message not found, deleting job');
|
2019-03-26 01:10:30 +00:00
|
|
|
await _finishJob(null, id);
|
2019-01-30 20:15:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-26 01:10:30 +00:00
|
|
|
message = MessageController.register(found.id, found);
|
2019-01-30 20:15:07 +00:00
|
|
|
|
|
|
|
const pending = true;
|
|
|
|
await setAttachmentDownloadJobPending(id, pending);
|
|
|
|
|
|
|
|
let downloaded;
|
|
|
|
const messageReceiver = getMessageReceiver();
|
|
|
|
if (!messageReceiver) {
|
|
|
|
throw new Error('_runJob: messageReceiver not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
downloaded = await messageReceiver.downloadAttachment(attachment);
|
|
|
|
} catch (error) {
|
|
|
|
// Attachments on the server expire after 30 days, then start returning 404
|
|
|
|
if (error && error.code === 404) {
|
|
|
|
logger.warn(
|
|
|
|
`_runJob: Got 404 from server, marking attachment ${
|
|
|
|
attachment.id
|
|
|
|
} from message ${message.idForLogging()} as permanent error`
|
|
|
|
);
|
|
|
|
|
|
|
|
await _finishJob(message, id);
|
|
|
|
await _addAttachmentToMessage(
|
|
|
|
message,
|
|
|
|
_markAttachmentAsError(attachment),
|
|
|
|
{ type, index }
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const upgradedAttachment = await Signal.Migrations.processNewAttachment(
|
|
|
|
downloaded
|
|
|
|
);
|
|
|
|
|
|
|
|
await _addAttachmentToMessage(message, upgradedAttachment, { type, index });
|
|
|
|
|
|
|
|
await _finishJob(message, id);
|
|
|
|
} catch (error) {
|
|
|
|
const currentAttempt = (attempts || 0) + 1;
|
|
|
|
|
|
|
|
if (currentAttempt >= 3) {
|
|
|
|
logger.error(
|
|
|
|
`_runJob: ${currentAttempt} failed attempts, marking attachment ${id} from message ${message.idForLogging()} as permament error:`,
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
|
|
|
|
await _finishJob(message, id);
|
|
|
|
await _addAttachmentToMessage(
|
|
|
|
message,
|
|
|
|
_markAttachmentAsError(attachment),
|
|
|
|
{ type, index }
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.error(
|
|
|
|
`_runJob: Failed to download attachment type ${type} for message ${message.idForLogging()}, attempt ${currentAttempt}:`,
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
|
|
|
|
const failedJob = {
|
|
|
|
...job,
|
|
|
|
pending: 0,
|
|
|
|
attempts: currentAttempt,
|
|
|
|
timestamp: Date.now() + RETRY_BACKOFF[currentAttempt],
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveAttachmentDownloadJob(failedJob);
|
|
|
|
delete _activeAttachmentDownloadJobs[id];
|
|
|
|
_maybeStartJob();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _finishJob(message, id) {
|
|
|
|
if (message) {
|
|
|
|
await saveMessage(message.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
const conversation = message.getConversation();
|
|
|
|
if (conversation) {
|
|
|
|
const fromConversation = conversation.messageCollection.get(message.id);
|
|
|
|
|
|
|
|
if (fromConversation && message !== fromConversation) {
|
|
|
|
fromConversation.set(message.attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await removeAttachmentDownloadJob(id);
|
|
|
|
delete _activeAttachmentDownloadJobs[id];
|
|
|
|
_maybeStartJob();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getActiveJobCount() {
|
|
|
|
return Object.keys(_activeAttachmentDownloadJobs).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _markAttachmentAsError(attachment) {
|
|
|
|
return {
|
|
|
|
...omit(attachment, ['key', 'digest', 'id']),
|
|
|
|
error: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _addAttachmentToMessage(message, attachment, { type, index }) {
|
|
|
|
if (!message) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-22 01:16:37 +00:00
|
|
|
const logPrefix = `${message.idForLogging()} (type: ${type}, index: ${index})`;
|
|
|
|
|
2019-03-13 20:38:28 +00:00
|
|
|
if (type === 'long-message') {
|
|
|
|
try {
|
|
|
|
const { data } = await Signal.Migrations.loadAttachmentData(attachment);
|
|
|
|
message.set({
|
|
|
|
body: attachment.isError ? message.get('body') : stringFromBytes(data),
|
|
|
|
bodyPending: false,
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
Signal.Migrations.deleteAttachmentData(attachment.path);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
if (type === 'attachment') {
|
|
|
|
const attachments = message.get('attachments');
|
|
|
|
if (!attachments || attachments.length <= index) {
|
|
|
|
throw new Error(
|
|
|
|
`_addAttachmentToMessage: attachments didn't exist or ${index} was too large`
|
|
|
|
);
|
|
|
|
}
|
2019-09-04 00:07:47 +00:00
|
|
|
_checkOldAttachment(attachments, index, attachment, logPrefix);
|
|
|
|
|
|
|
|
const newAttachments = [...attachments];
|
|
|
|
newAttachments[index] = attachment;
|
|
|
|
|
|
|
|
message.set({ attachments: newAttachments });
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'preview') {
|
|
|
|
const preview = message.get('preview');
|
|
|
|
if (!preview || preview.length <= index) {
|
|
|
|
throw new Error(
|
|
|
|
`_addAttachmentToMessage: preview didn't exist or ${index} was too large`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const item = preview[index];
|
|
|
|
if (!item) {
|
|
|
|
throw new Error(`_addAttachmentToMessage: preview ${index} was falsey`);
|
|
|
|
}
|
2019-09-04 00:07:47 +00:00
|
|
|
|
|
|
|
_checkOldAttachment(item, 'image', attachment, logPrefix);
|
|
|
|
|
|
|
|
const newPreview = [...preview];
|
|
|
|
newPreview[index] = {
|
|
|
|
...preview[index],
|
|
|
|
image: attachment,
|
|
|
|
};
|
|
|
|
|
|
|
|
message.set({ preview: newPreview });
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'contact') {
|
|
|
|
const contact = message.get('contact');
|
|
|
|
if (!contact || contact.length <= index) {
|
|
|
|
throw new Error(
|
|
|
|
`_addAttachmentToMessage: contact didn't exist or ${index} was too large`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const item = contact[index];
|
|
|
|
if (item && item.avatar && item.avatar.avatar) {
|
2019-09-04 00:07:47 +00:00
|
|
|
_checkOldAttachment(item.avatar, 'avatar', attachment, logPrefix);
|
|
|
|
|
|
|
|
const newContact = [...contact];
|
|
|
|
newContact[index] = {
|
|
|
|
...contact[index],
|
|
|
|
avatar: {
|
|
|
|
...contact[index].avatar,
|
|
|
|
avatar: attachment,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
message.set({ contact: newContact });
|
2019-01-30 20:15:07 +00:00
|
|
|
} else {
|
|
|
|
logger.warn(
|
|
|
|
`_addAttachmentToMessage: Couldn't update contact with avatar attachment for message ${message.idForLogging()}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'quote') {
|
|
|
|
const quote = message.get('quote');
|
|
|
|
if (!quote) {
|
|
|
|
throw new Error("_addAttachmentToMessage: quote didn't exist");
|
|
|
|
}
|
|
|
|
const { attachments } = quote;
|
|
|
|
if (!attachments || attachments.length <= index) {
|
|
|
|
throw new Error(
|
|
|
|
`_addAttachmentToMessage: quote attachments didn't exist or ${index} was too large`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = attachments[index];
|
|
|
|
if (!item) {
|
|
|
|
throw new Error(
|
2019-09-04 00:07:47 +00:00
|
|
|
`_addAttachmentToMessage: quote attachment ${index} was falsey`
|
2019-01-30 20:15:07 +00:00
|
|
|
);
|
|
|
|
}
|
2019-09-04 00:07:47 +00:00
|
|
|
|
|
|
|
_checkOldAttachment(item, 'thumbnail', attachment, logPrefix);
|
|
|
|
|
|
|
|
const newAttachments = [...attachments];
|
|
|
|
newAttachments[index] = {
|
|
|
|
...attachments[index],
|
|
|
|
thumbnail: attachment,
|
|
|
|
};
|
|
|
|
|
|
|
|
const newQuote = {
|
|
|
|
...quote,
|
|
|
|
attachments: newAttachments,
|
|
|
|
};
|
|
|
|
|
|
|
|
message.set({ quote: newQuote });
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'group-avatar') {
|
2019-06-24 18:51:33 +00:00
|
|
|
const conversationId = message.get('conversationId');
|
2019-05-16 22:32:11 +00:00
|
|
|
const conversation = ConversationController.get(conversationId);
|
|
|
|
if (!conversation) {
|
|
|
|
logger.warn("_addAttachmentToMessage: conversation didn't exist");
|
2019-06-24 18:51:33 +00:00
|
|
|
return;
|
2019-01-30 20:15:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
const existingAvatar = conversation.get('avatar');
|
2019-01-30 20:15:07 +00:00
|
|
|
if (existingAvatar && existingAvatar.path) {
|
|
|
|
await Signal.Migrations.deleteAttachmentData(existingAvatar.path);
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:51:33 +00:00
|
|
|
const loadedAttachment = await Signal.Migrations.loadAttachmentData(
|
|
|
|
attachment
|
|
|
|
);
|
2019-05-16 22:32:11 +00:00
|
|
|
conversation.set({
|
|
|
|
avatar: {
|
|
|
|
...attachment,
|
2019-06-24 18:51:33 +00:00
|
|
|
hash: await computeHash(loadedAttachment.data),
|
2019-05-16 22:32:11 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
await Signal.Data.updateConversation(
|
|
|
|
conversationId,
|
|
|
|
conversation.attributes,
|
|
|
|
{
|
|
|
|
Conversation: Whisper.Conversation,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'sticker') {
|
|
|
|
const sticker = message.get('sticker');
|
|
|
|
if (!sticker) {
|
|
|
|
throw new Error("_addAttachmentToMessage: sticker didn't exist");
|
|
|
|
}
|
|
|
|
|
2019-08-22 22:04:14 +00:00
|
|
|
message.set({
|
|
|
|
sticker: {
|
|
|
|
...sticker,
|
|
|
|
data: attachment,
|
|
|
|
},
|
|
|
|
});
|
2019-01-30 20:15:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
`_addAttachmentToMessage: Unknown job type ${type} for message ${message.idForLogging()}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-04 00:07:47 +00:00
|
|
|
function _checkOldAttachment(object, key, newAttachment, logPrefix) {
|
2019-01-30 20:15:07 +00:00
|
|
|
const oldAttachment = object[key];
|
|
|
|
if (oldAttachment && oldAttachment.path) {
|
2019-09-04 00:07:47 +00:00
|
|
|
logger.error(
|
|
|
|
`_checkOldAttachment: ${logPrefix} - old attachment already had path, not replacing`
|
|
|
|
);
|
|
|
|
throw new Error(
|
|
|
|
'_checkOldAttachment: old attachment already had path, not replacing'
|
2019-01-30 20:15:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|