Ensure out-of-band attachment updates make new top-level objects

This commit is contained in:
Scott Nonnenberg 2019-09-03 17:07:47 -07:00 committed by Ken Powers
parent 15af1eea7a
commit 1ab844674a
2 changed files with 54 additions and 14 deletions

View file

@ -258,9 +258,6 @@ async function _finishJob(message, id) {
if (fromConversation && message !== fromConversation) { if (fromConversation && message !== fromConversation) {
fromConversation.set(message.attributes); fromConversation.set(message.attributes);
fromConversation.trigger('change', fromConversation);
} else {
message.trigger('change', message);
} }
} }
} }
@ -308,7 +305,13 @@ async function _addAttachmentToMessage(message, attachment, { type, index }) {
`_addAttachmentToMessage: attachments didn't exist or ${index} was too large` `_addAttachmentToMessage: attachments didn't exist or ${index} was too large`
); );
} }
_replaceAttachment(attachments, index, attachment, logPrefix); _checkOldAttachment(attachments, index, attachment, logPrefix);
const newAttachments = [...attachments];
newAttachments[index] = attachment;
message.set({ attachments: newAttachments });
return; return;
} }
@ -323,7 +326,17 @@ async function _addAttachmentToMessage(message, attachment, { type, index }) {
if (!item) { if (!item) {
throw new Error(`_addAttachmentToMessage: preview ${index} was falsey`); throw new Error(`_addAttachmentToMessage: preview ${index} was falsey`);
} }
_replaceAttachment(item, 'image', attachment, logPrefix);
_checkOldAttachment(item, 'image', attachment, logPrefix);
const newPreview = [...preview];
newPreview[index] = {
...preview[index],
image: attachment,
};
message.set({ preview: newPreview });
return; return;
} }
@ -336,7 +349,18 @@ async function _addAttachmentToMessage(message, attachment, { type, index }) {
} }
const item = contact[index]; const item = contact[index];
if (item && item.avatar && item.avatar.avatar) { if (item && item.avatar && item.avatar.avatar) {
_replaceAttachment(item.avatar, 'avatar', attachment, logPrefix); _checkOldAttachment(item.avatar, 'avatar', attachment, logPrefix);
const newContact = [...contact];
newContact[index] = {
...contact[index],
avatar: {
...contact[index].avatar,
avatar: attachment,
},
};
message.set({ contact: newContact });
} else { } else {
logger.warn( logger.warn(
`_addAttachmentToMessage: Couldn't update contact with avatar attachment for message ${message.idForLogging()}` `_addAttachmentToMessage: Couldn't update contact with avatar attachment for message ${message.idForLogging()}`
@ -361,10 +385,25 @@ async function _addAttachmentToMessage(message, attachment, { type, index }) {
const item = attachments[index]; const item = attachments[index];
if (!item) { if (!item) {
throw new Error( throw new Error(
`_addAttachmentToMessage: attachment ${index} was falsey` `_addAttachmentToMessage: quote attachment ${index} was falsey`
); );
} }
_replaceAttachment(item, 'thumbnail', attachment, logPrefix);
_checkOldAttachment(item, 'thumbnail', attachment, logPrefix);
const newAttachments = [...attachments];
newAttachments[index] = {
...attachments[index],
thumbnail: attachment,
};
const newQuote = {
...quote,
attachments: newAttachments,
};
message.set({ quote: newQuote });
return; return;
} }
@ -420,14 +459,14 @@ async function _addAttachmentToMessage(message, attachment, { type, index }) {
); );
} }
function _replaceAttachment(object, key, newAttachment, logPrefix) { function _checkOldAttachment(object, key, newAttachment, logPrefix) {
const oldAttachment = object[key]; const oldAttachment = object[key];
if (oldAttachment && oldAttachment.path) { if (oldAttachment && oldAttachment.path) {
logger.warn( logger.error(
`_replaceAttachment: ${logPrefix} - old attachment already had path, not replacing` `_checkOldAttachment: ${logPrefix} - old attachment already had path, not replacing`
);
throw new Error(
'_checkOldAttachment: old attachment already had path, not replacing'
); );
} }
// eslint-disable-next-line no-param-reassign
object[key] = newAttachment;
} }

View file

@ -561,6 +561,7 @@
margin-bottom: 5px; margin-bottom: 5px;
border-top-left-radius: 16px; border-top-left-radius: 16px;
border-top-right-radius: 16px; border-top-right-radius: 16px;
overflow: hidden;
} }
.module-message__link-preview--with-content-above { .module-message__link-preview--with-content-above {