Extract Message.loadWithObjectURL
This commit is contained in:
parent
45d89d1e44
commit
4ce0472b9f
4 changed files with 56 additions and 33 deletions
|
@ -583,27 +583,14 @@
|
||||||
// - [ ] Fetch file attachments
|
// - [ ] Fetch file attachments
|
||||||
// - [ ] Add mechanism to fetch more data
|
// - [ ] Add mechanism to fetch more data
|
||||||
|
|
||||||
const mediaWithoutAttachmentData =
|
const media = await Signal.Backbone.Conversation.fetchVisualMediaAttachments({
|
||||||
await Signal.Backbone.Conversation.fetchVisualMediaAttachments({
|
conversationId: this.model.get('id'),
|
||||||
conversationId: this.model.get('id'),
|
WhisperMessageCollection: Whisper.MessageCollection,
|
||||||
WhisperMessageCollection: Whisper.MessageCollection,
|
});
|
||||||
});
|
const loadMessages = Signal.Components.PropTypes.Message.loadWithObjectURL(
|
||||||
|
Signal.Migrations.loadMessage
|
||||||
const mediaWithAttachmentData =
|
);
|
||||||
await Promise.all(mediaWithoutAttachmentData.map(Signal.Migrations.loadMessage));
|
const mediaWithObjectURLs = await loadMessages(media);
|
||||||
|
|
||||||
const withObjectURL = message => {
|
|
||||||
if (!message.attachments || message.attachments.length === 0) {
|
|
||||||
throw new TypeError('`message.attachments` cannot be empty');
|
|
||||||
}
|
|
||||||
const attachment = message.attachments[0];
|
|
||||||
const objectURL = Signal.Util.arrayBufferToObjectURL({
|
|
||||||
data: attachment.data,
|
|
||||||
type: attachment.contentType,
|
|
||||||
});
|
|
||||||
return Object.assign({}, message, {objectURL});
|
|
||||||
}
|
|
||||||
const mediaWithObjectURLs = mediaWithAttachmentData.map(withObjectURL);
|
|
||||||
|
|
||||||
const props = {
|
const props = {
|
||||||
media: mediaWithObjectURLs,
|
media: mediaWithObjectURLs,
|
||||||
|
|
|
@ -166,8 +166,14 @@ const { MediaGallery } =
|
||||||
require('./ts/components/conversation/media-gallery/MediaGallery');
|
require('./ts/components/conversation/media-gallery/MediaGallery');
|
||||||
const { Quote } = require('./ts/components/conversation/Quote');
|
const { Quote } = require('./ts/components/conversation/Quote');
|
||||||
|
|
||||||
|
const PropTypesMessage =
|
||||||
|
require('./ts/components/conversation/media-gallery/propTypes/Message');
|
||||||
|
|
||||||
window.Signal.Components = {
|
window.Signal.Components = {
|
||||||
MediaGallery,
|
MediaGallery,
|
||||||
|
PropTypes: {
|
||||||
|
Message: PropTypesMessage,
|
||||||
|
},
|
||||||
Quote,
|
Quote,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
*/
|
*/
|
||||||
import is from '@sindresorhus/is';
|
import is from '@sindresorhus/is';
|
||||||
|
|
||||||
import { deferredToPromise } from '../../js/modules/deferred_to_promise';
|
|
||||||
import { Collection as BackboneCollection } from '../types/backbone/Collection';
|
import { Collection as BackboneCollection } from '../types/backbone/Collection';
|
||||||
|
import { deferredToPromise } from '../../js/modules/deferred_to_promise';
|
||||||
import { Message } from '../types/Message';
|
import { Message } from '../types/Message';
|
||||||
|
|
||||||
export const fetchVisualMediaAttachments = async ({
|
export const fetchVisualMediaAttachments = async ({
|
||||||
|
|
|
@ -1,15 +1,45 @@
|
||||||
/**
|
/**
|
||||||
* @prettier
|
* @prettier
|
||||||
*/
|
*/
|
||||||
export interface Message {
|
import is from '@sindresorhus/is';
|
||||||
body?: string;
|
|
||||||
received_at: number;
|
|
||||||
attachments: Array<{
|
|
||||||
data?: ArrayBuffer;
|
|
||||||
fileName?: string;
|
|
||||||
size?: number;
|
|
||||||
}>;
|
|
||||||
|
|
||||||
// TODO: Revisit
|
import { arrayBufferToObjectURL } from '../../../../util/arrayBufferToObjectURL';
|
||||||
objectURL?: string;
|
import { Attachment } from '../../../../types/Attachment';
|
||||||
}
|
import { MapAsync } from '../../../../types/MapAsync';
|
||||||
|
import { MIMEType } from '../../../../types/MIME';
|
||||||
|
|
||||||
|
export type Message = {
|
||||||
|
attachments: Array<Attachment>;
|
||||||
|
received_at: number;
|
||||||
|
} & { objectURL?: string };
|
||||||
|
|
||||||
|
const DEFAULT_CONTENT_TYPE: MIMEType = 'application/octet-stream' as MIMEType;
|
||||||
|
|
||||||
|
export const loadWithObjectURL = (loadMessage: MapAsync<Message>) => async (
|
||||||
|
media: Array<Message>
|
||||||
|
): Promise<Array<Message>> => {
|
||||||
|
if (!is.function_(loadMessage)) {
|
||||||
|
throw new TypeError("'loadMessage' must be a function");
|
||||||
|
}
|
||||||
|
if (!is.array(media)) {
|
||||||
|
throw new TypeError("'media' must be a function");
|
||||||
|
}
|
||||||
|
|
||||||
|
const mediaWithAttachmentData = await Promise.all(media.map(loadMessage));
|
||||||
|
return mediaWithAttachmentData.map(withObjectURL);
|
||||||
|
};
|
||||||
|
|
||||||
|
const withObjectURL = (message: Message): Message => {
|
||||||
|
if (message.attachments.length === 0) {
|
||||||
|
throw new TypeError('`message.attachments` cannot be empty');
|
||||||
|
}
|
||||||
|
const attachment = message.attachments[0];
|
||||||
|
const objectURL = arrayBufferToObjectURL({
|
||||||
|
data: attachment.data,
|
||||||
|
type: attachment.contentType || DEFAULT_CONTENT_TYPE,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
...message,
|
||||||
|
objectURL,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue