View All Media: Listen for updates to conversation, re-render

pull/272/head
Scott Nonnenberg 6 years ago
parent d4eacda649
commit 9afea3ae2b

@ -767,125 +767,141 @@
const DEFAULT_DOCUMENTS_FETCH_COUNT = 150; const DEFAULT_DOCUMENTS_FETCH_COUNT = 150;
const conversationId = this.model.get('id'); const conversationId = this.model.get('id');
const rawMedia = await Signal.Data.getMessagesWithVisualMediaAttachments(
conversationId,
{
limit: DEFAULT_MEDIA_FETCH_COUNT,
MessageCollection: Whisper.MessageCollection,
}
);
const rawDocuments = await Signal.Data.getMessagesWithFileAttachments(
conversationId,
{
limit: DEFAULT_DOCUMENTS_FETCH_COUNT,
MessageCollection: Whisper.MessageCollection,
}
);
// First we upgrade these messages to ensure that they have thumbnails const getProps = async () => {
for (let max = rawMedia.length, i = 0; i < max; i += 1) { const rawMedia = await Signal.Data.getMessagesWithVisualMediaAttachments(
const message = rawMedia[i]; conversationId,
const { schemaVersion } = message; {
limit: DEFAULT_MEDIA_FETCH_COUNT,
if (schemaVersion < Message.VERSION_NEEDED_FOR_DISPLAY) { MessageCollection: Whisper.MessageCollection,
// Yep, we really do want to wait for each of these }
// eslint-disable-next-line no-await-in-loop );
rawMedia[i] = await upgradeMessageSchema(message); const rawDocuments = await Signal.Data.getMessagesWithFileAttachments(
// eslint-disable-next-line no-await-in-loop conversationId,
await window.Signal.Data.saveMessage(rawMedia[i], { {
Message: Whisper.Message, limit: DEFAULT_DOCUMENTS_FETCH_COUNT,
}); MessageCollection: Whisper.MessageCollection,
} }
} );
const media = _.flatten( // First we upgrade these messages to ensure that they have thumbnails
rawMedia.map(message => { for (let max = rawMedia.length, i = 0; i < max; i += 1) {
const { attachments } = message; const message = rawMedia[i];
return (attachments || []) const { schemaVersion } = message;
.filter(
attachment => if (schemaVersion < Message.VERSION_NEEDED_FOR_DISPLAY) {
attachment.thumbnail && !attachment.pending && !attachment.error // Yep, we really do want to wait for each of these
) // eslint-disable-next-line no-await-in-loop
.map((attachment, index) => { rawMedia[i] = await upgradeMessageSchema(message);
const { thumbnail } = attachment; // eslint-disable-next-line no-await-in-loop
await window.Signal.Data.saveMessage(rawMedia[i], {
return { Message: Whisper.Message,
objectURL: getAbsoluteAttachmentPath(attachment.path),
thumbnailObjectUrl: thumbnail
? getAbsoluteAttachmentPath(thumbnail.path)
: null,
contentType: attachment.contentType,
index,
attachment,
message,
};
}); });
}) }
); }
// Unlike visual media, only one non-image attachment is supported const media = _.flatten(
const documents = rawDocuments.map(message => { rawMedia.map(message => {
const attachments = message.attachments || []; const { attachments } = message;
const attachment = attachments[0]; return (attachments || [])
return { .filter(
contentType: attachment.contentType, attachment =>
index: 0, attachment.thumbnail &&
attachment, !attachment.pending &&
message, !attachment.error
}; )
}); .map((attachment, index) => {
const { thumbnail } = attachment;
return {
objectURL: getAbsoluteAttachmentPath(attachment.path),
thumbnailObjectUrl: thumbnail
? getAbsoluteAttachmentPath(thumbnail.path)
: null,
contentType: attachment.contentType,
index,
attachment,
message,
};
});
})
);
const saveAttachment = async ({ attachment, message } = {}) => { // Unlike visual media, only one non-image attachment is supported
const timestamp = message.received_at; const documents = rawDocuments.map(message => {
Signal.Types.Attachment.save({ const attachments = message.attachments || [];
attachment, const attachment = attachments[0];
document, return {
getAbsolutePath: getAbsoluteAttachmentPath, contentType: attachment.contentType,
timestamp, index: 0,
attachment,
message,
};
}); });
};
const onItemClick = async ({ message, attachment, type }) => { const saveAttachment = async ({ attachment, message } = {}) => {
switch (type) { const timestamp = message.received_at;
case 'documents': { Signal.Types.Attachment.save({
saveAttachment({ message, attachment }); attachment,
break; document,
} getAbsolutePath: getAbsoluteAttachmentPath,
timestamp,
});
};
case 'media': { const onItemClick = async ({ message, attachment, type }) => {
const selectedIndex = media.findIndex( switch (type) {
mediaMessage => mediaMessage.attachment.path === attachment.path case 'documents': {
); saveAttachment({ message, attachment });
this.lightboxGalleryView = new Whisper.ReactWrapperView({ break;
className: 'lightbox-wrapper', }
Component: Signal.Components.LightboxGallery,
props: { case 'media': {
media, const selectedIndex = media.findIndex(
onSave: saveAttachment, mediaMessage => mediaMessage.attachment.path === attachment.path
selectedIndex, );
}, this.lightboxGalleryView = new Whisper.ReactWrapperView({
onClose: () => Signal.Backbone.Views.Lightbox.hide(), className: 'lightbox-wrapper',
}); Component: Signal.Components.LightboxGallery,
Signal.Backbone.Views.Lightbox.show(this.lightboxGalleryView.el); props: {
break; media,
onSave: saveAttachment,
selectedIndex,
},
onClose: () => Signal.Backbone.Views.Lightbox.hide(),
});
Signal.Backbone.Views.Lightbox.show(this.lightboxGalleryView.el);
break;
}
default:
throw new TypeError(`Unknown attachment type: '${type}'`);
} }
};
default: return {
throw new TypeError(`Unknown attachment type: '${type}'`); documents,
} media,
onItemClick,
};
}; };
const view = new Whisper.ReactWrapperView({ const view = new Whisper.ReactWrapperView({
className: 'panel-wrapper', className: 'panel-wrapper',
Component: Signal.Components.MediaGallery, Component: Signal.Components.MediaGallery,
props: { props: await getProps(),
documents, onClose: () => {
media, this.stopListening(this.model.messageCollection, 'remove', update);
onItemClick, this.resetPanel();
}, },
onClose: () => this.resetPanel(),
}); });
const update = async () => {
view.update(await getProps());
};
this.listenTo(this.model.messageCollection, 'remove', update);
this.listenBack(view); this.listenBack(view);
}, },
@ -1294,20 +1310,23 @@
}, },
showMessageDetail(message) { showMessageDetail(message) {
const onClose = () => {
this.stopListening(message, 'change', update);
this.resetPanel();
this.updateHeader();
};
const props = message.getPropsForMessageDetail(); const props = message.getPropsForMessageDetail();
const view = new Whisper.ReactWrapperView({ const view = new Whisper.ReactWrapperView({
className: 'message-detail-wrapper', className: 'message-detail-wrapper',
Component: Signal.Components.MessageDetail, Component: Signal.Components.MessageDetail,
props, props,
onClose: () => { onClose,
this.stopListening(message, 'change', update);
this.resetPanel();
this.updateHeader();
},
}); });
const update = () => view.update(message.getPropsForMessageDetail()); const update = () => view.update(message.getPropsForMessageDetail());
this.listenTo(message, 'change', update); this.listenTo(message, 'change', update);
this.listenTo(message, 'expired', onClose);
// We could listen to all involved contacts, but we'll call that overkill // We could listen to all involved contacts, but we'll call that overkill
this.listenBack(view); this.listenBack(view);

Loading…
Cancel
Save