Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/message-ops-utils.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | |||||
import _keyBy from 'lodash/fp/keyBy.js'; | import _keyBy from 'lodash/fp/keyBy.js'; | ||||
import { messageID } from '../shared/message-utils.js'; | import { messageID } from '../shared/message-utils.js'; | ||||
import { messageSpecs } from '../shared/messages/message-specs.js'; | import { messageSpecs } from '../shared/messages/message-specs.js'; | ||||
import type { | import type { | ||||
EncryptedVideo, | |||||
Media, | Media, | ||||
ClientDBMediaInfo, | ClientDBMediaInfo, | ||||
Image, | Image, | ||||
Video, | Video, | ||||
} from '../types/media-types.js'; | } from '../types/media-types'; | ||||
import { | import { | ||||
type ClientDBMessageInfo, | type ClientDBMessageInfo, | ||||
type RawMessageInfo, | type RawMessageInfo, | ||||
messageTypes, | messageTypes, | ||||
assertMessageType, | assertMessageType, | ||||
type MessageStoreOperation, | type MessageStoreOperation, | ||||
type ClientDBMessageStoreOperation, | type ClientDBMessageStoreOperation, | ||||
} from '../types/message-types.js'; | } from '../types/message-types.js'; | ||||
import type { MediaMessageServerDBContent } from '../types/messages/media.js'; | import type { MediaMessageServerDBContent } from '../types/messages/media.js'; | ||||
function translateMediaToClientDBMediaInfos( | function translateMediaToClientDBMediaInfos( | ||||
media: $ReadOnlyArray<Media>, | media: $ReadOnlyArray<Media>, | ||||
): $ReadOnlyArray<ClientDBMediaInfo> { | ): $ReadOnlyArray<ClientDBMediaInfo> { | ||||
const clientDBMediaInfos = []; | const clientDBMediaInfos = []; | ||||
for (const m of media) { | for (const m of media) { | ||||
invariant( | const type = | ||||
m.type === 'photo' || m.type === 'video', | m.type === 'encrypted_photo' | ||||
'unimplemented media type', | ? 'photo' | ||||
); | : m.type === 'encrypted_video' | ||||
? 'video' | |||||
: m.type; | |||||
const mediaURI = | |||||
m.type === 'encrypted_photo' || m.type === 'encrypted_video' | |||||
? m.holder | |||||
: m.uri; | |||||
clientDBMediaInfos.push({ | clientDBMediaInfos.push({ | ||||
id: m.id, | id: m.id, | ||||
uri: m.uri, | uri: mediaURI, | ||||
type: m.type, | type: type, | ||||
extras: JSON.stringify({ | extras: JSON.stringify({ | ||||
dimensions: m.dimensions, | dimensions: m.dimensions, | ||||
loop: m.type === 'video' ? m.loop : false, | loop: type === 'video' ? m.loop : false, | ||||
local_media_selection: m.localMediaSelection, | local_media_selection: m.localMediaSelection, | ||||
encryption_key: m.encryptionKey, | |||||
}), | }), | ||||
}); | }); | ||||
if (m.type === 'video') { | if (m.type === 'video' || m.type === 'encrypted_video') { | ||||
const thumbnailURI = | |||||
m.type === 'encrypted_video' ? m.thumbnailHolder : m.thumbnailURI; | |||||
clientDBMediaInfos.push({ | clientDBMediaInfos.push({ | ||||
id: m.thumbnailID, | id: m.thumbnailID, | ||||
uri: m.thumbnailURI, | uri: thumbnailURI, | ||||
type: 'photo', | type: 'photo', | ||||
extras: JSON.stringify({ | extras: JSON.stringify({ | ||||
dimensions: m.dimensions, | dimensions: m.dimensions, | ||||
loop: false, | loop: false, | ||||
encryption_key: m.thumbnailEncryptionKey, | |||||
}), | }), | ||||
}); | }); | ||||
} | } | ||||
} | } | ||||
return clientDBMediaInfos; | return clientDBMediaInfos; | ||||
} | } | ||||
function translateClientDBMediaInfoToImage( | function translateClientDBMediaInfoToImage( | ||||
Show All 39 Lines | ): $ReadOnlyArray<Media> { | ||||
const mediaMap = _keyBy('id')(mediaInfos); | const mediaMap = _keyBy('id')(mediaInfos); | ||||
if (!clientDBMessageInfo.content) { | if (!clientDBMessageInfo.content) { | ||||
return []; | return []; | ||||
} | } | ||||
const messageContent: $ReadOnlyArray<MediaMessageServerDBContent> = | const messageContent: $ReadOnlyArray<MediaMessageServerDBContent> = | ||||
JSON.parse(clientDBMessageInfo.content); | JSON.parse(clientDBMessageInfo.content); | ||||
const translatedMedia = []; | const translatedMedia: Media[] = []; | ||||
for (const media of messageContent) { | for (const media of messageContent) { | ||||
if (media.type === 'photo') { | if (media.type === 'photo') { | ||||
const extras = JSON.parse(mediaMap[media.uploadID].extras); | const extras = JSON.parse(mediaMap[media.uploadID].extras); | ||||
const { dimensions } = extras; | const { dimensions, encryption_key: encryptionKey } = extras; | ||||
const image: Image = { | let image; | ||||
if (encryptionKey) { | |||||
image = { | |||||
id: media.uploadID, | |||||
type: 'encrypted_photo', | |||||
holder: mediaMap[media.uploadID].uri, | |||||
dimensions, | |||||
encryptionKey, | |||||
}; | |||||
} else { | |||||
image = { | |||||
id: media.uploadID, | id: media.uploadID, | ||||
uri: mediaMap[media.uploadID].uri, | |||||
type: 'photo', | type: 'photo', | ||||
uri: mediaMap[media.uploadID].uri, | |||||
dimensions, | dimensions, | ||||
}; | }; | ||||
} | |||||
translatedMedia.push(image); | translatedMedia.push(image); | ||||
} else if (media.type === 'video') { | } else if (media.type === 'video') { | ||||
const extras = JSON.parse(mediaMap[media.uploadID].extras); | const extras = JSON.parse(mediaMap[media.uploadID].extras); | ||||
const { | const { | ||||
dimensions, | dimensions, | ||||
loop, | loop, | ||||
local_media_selection: localMediaSelection, | local_media_selection: localMediaSelection, | ||||
encryption_key: encryptionKey, | |||||
} = extras; | } = extras; | ||||
if (encryptionKey) { | |||||
const thumbnailEncryptionKey = JSON.parse( | |||||
mediaMap[media.thumbnailUploadID].extras, | |||||
).encryption_key; | |||||
const video: EncryptedVideo = { | |||||
id: media.uploadID, | |||||
type: 'encrypted_video', | |||||
holder: mediaMap[media.uploadID].uri, | |||||
dimensions, | |||||
loop, | |||||
encryptionKey, | |||||
thumbnailID: media.thumbnailUploadID, | |||||
thumbnailHolder: mediaMap[media.thumbnailUploadID].uri, | |||||
thumbnailEncryptionKey, | |||||
}; | |||||
translatedMedia.push(video); | |||||
} else { | |||||
const video: Video = { | const video: Video = { | ||||
id: media.uploadID, | id: media.uploadID, | ||||
uri: mediaMap[media.uploadID].uri, | uri: mediaMap[media.uploadID].uri, | ||||
type: 'video', | type: 'video', | ||||
dimensions, | dimensions, | ||||
loop, | loop, | ||||
thumbnailID: media.thumbnailUploadID, | thumbnailID: media.thumbnailUploadID, | ||||
thumbnailURI: mediaMap[media.thumbnailUploadID].uri, | thumbnailURI: mediaMap[media.thumbnailUploadID].uri, | ||||
}; | }; | ||||
translatedMedia.push( | translatedMedia.push( | ||||
localMediaSelection ? { ...video, localMediaSelection } : video, | localMediaSelection ? { ...video, localMediaSelection } : video, | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
} | |||||
return translatedMedia; | return translatedMedia; | ||||
} | } | ||||
function translateRawMessageInfoToClientDBMessageInfo( | function translateRawMessageInfoToClientDBMessageInfo( | ||||
rawMessageInfo: RawMessageInfo, | rawMessageInfo: RawMessageInfo, | ||||
): ClientDBMessageInfo { | ): ClientDBMessageInfo { | ||||
return { | return { | ||||
id: messageID(rawMessageInfo), | id: messageID(rawMessageInfo), | ||||
▲ Show 20 Lines • Show All 64 Lines • Show Last 20 Lines |