Changeset View
Changeset View
Standalone View
Standalone View
lib/types/media-types.js
// @flow | // @flow | ||||
import t, { type TInterface, type TUnion } from 'tcomb'; | |||||
import type { Shape } from './core.js'; | import type { Shape } from './core.js'; | ||||
import { type Platform } from './device-types.js'; | import { type Platform } from './device-types.js'; | ||||
import { tShape, tString, tID } from '../utils/validation-utils.js'; | |||||
export type Dimensions = $ReadOnly<{ | export type Dimensions = $ReadOnly<{ | ||||
+height: number, | +height: number, | ||||
+width: number, | +width: number, | ||||
}>; | }>; | ||||
export const dimensionsValidator: TInterface<Dimensions> = tShape<Dimensions>({ | |||||
height: t.Number, | |||||
width: t.Number, | |||||
}); | |||||
export type MediaType = 'photo' | 'video'; | export type MediaType = 'photo' | 'video'; | ||||
export type EncryptedMediaType = 'encrypted_photo' | 'encrypted_video'; | export type EncryptedMediaType = 'encrypted_photo' | 'encrypted_video'; | ||||
export type AvatarMediaInfo = { | export type AvatarMediaInfo = { | ||||
+type: 'photo', | +type: 'photo', | ||||
+uri: string, | +uri: string, | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 222 Lines • ▼ Show 20 Lines | | { | ||||
+uri: string, | +uri: string, | ||||
+mediaNativeID: ?string, | +mediaNativeID: ?string, | ||||
+selectTime: number, // ms timestamp | +selectTime: number, // ms timestamp | ||||
+sendTime: number, // ms timestamp | +sendTime: number, // ms timestamp | ||||
+retries: number, | +retries: number, | ||||
+duration: number, // seconds | +duration: number, // seconds | ||||
}; | }; | ||||
export const mediaLibrarySelectionValidator: TUnion<MediaLibrarySelection> = | |||||
t.union([ | |||||
tShape({ | |||||
step: tString('photo_library'), | |||||
dimensions: dimensionsValidator, | |||||
filename: t.maybe(t.String), | |||||
uri: t.String, | |||||
mediaNativeID: t.maybe(t.String), | |||||
selectTime: t.Number, | |||||
sendTime: t.Number, | |||||
retries: t.Number, | |||||
}), | |||||
tShape({ | |||||
step: tString('video_library'), | |||||
dimensions: dimensionsValidator, | |||||
filename: t.maybe(t.String), | |||||
uri: t.String, | |||||
mediaNativeID: t.maybe(t.String), | |||||
selectTime: t.Number, | |||||
sendTime: t.Number, | |||||
retries: t.Number, | |||||
duration: t.Number, | |||||
}), | |||||
]); | |||||
export type PhotoCapture = { | export type PhotoCapture = { | ||||
+step: 'photo_capture', | +step: 'photo_capture', | ||||
+time: number, // ms | +time: number, // ms | ||||
+dimensions: Dimensions, | +dimensions: Dimensions, | ||||
+filename: string, | +filename: string, | ||||
+uri: string, | +uri: string, | ||||
+captureTime: number, // ms timestamp | +captureTime: number, // ms timestamp | ||||
+selectTime: number, // ms timestamp | +selectTime: number, // ms timestamp | ||||
+sendTime: number, // ms timestamp | +sendTime: number, // ms timestamp | ||||
+retries: number, | +retries: number, | ||||
}; | }; | ||||
export const photoCaptureValidator: TInterface<PhotoCapture> = | |||||
tShape<PhotoCapture>({ | |||||
step: tString('photo_capture'), | |||||
time: t.Number, | |||||
dimensions: dimensionsValidator, | |||||
filename: t.String, | |||||
uri: t.String, | |||||
captureTime: t.Number, | |||||
selectTime: t.Number, | |||||
sendTime: t.Number, | |||||
retries: t.Number, | |||||
}); | |||||
export type PhotoPaste = { | export type PhotoPaste = { | ||||
+step: 'photo_paste', | +step: 'photo_paste', | ||||
+dimensions: Dimensions, | +dimensions: Dimensions, | ||||
+filename: string, | +filename: string, | ||||
+uri: string, | +uri: string, | ||||
+selectTime: number, // ms timestamp | +selectTime: number, // ms timestamp | ||||
+sendTime: number, // ms timestamp | +sendTime: number, // ms timestamp | ||||
+retries: number, | +retries: number, | ||||
}; | }; | ||||
export const photoPasteValidator: TInterface<PhotoPaste> = tShape<PhotoPaste>({ | |||||
step: tString('photo_paste'), | |||||
dimensions: dimensionsValidator, | |||||
filename: t.String, | |||||
uri: t.String, | |||||
selectTime: t.Number, | |||||
sendTime: t.Number, | |||||
retries: t.Number, | |||||
}); | |||||
export type NativeMediaSelection = | export type NativeMediaSelection = | ||||
| MediaLibrarySelection | | MediaLibrarySelection | ||||
| PhotoCapture | | PhotoCapture | ||||
| PhotoPaste; | | PhotoPaste; | ||||
export const nativeMediaSelectionValidator: TUnion<NativeMediaSelection> = | |||||
t.union([ | |||||
mediaLibrarySelectionValidator, | |||||
photoCaptureValidator, | |||||
photoPasteValidator, | |||||
]); | |||||
export type MediaMissionStep = | export type MediaMissionStep = | ||||
| NativeMediaSelection | | NativeMediaSelection | ||||
| { | | { | ||||
+step: 'web_selection', | +step: 'web_selection', | ||||
+filename: string, | +filename: string, | ||||
+size: number, // in bytes | +size: number, // in bytes | ||||
+mime: string, | +mime: string, | ||||
+selectTime: number, // ms timestamp | +selectTime: number, // ms timestamp | ||||
▲ Show 20 Lines • Show All 271 Lines • ▼ Show 20 Lines | export type Image = { | ||||
+id: string, | +id: string, | ||||
+uri: string, | +uri: string, | ||||
+type: 'photo', | +type: 'photo', | ||||
+dimensions: Dimensions, | +dimensions: Dimensions, | ||||
// stored on native only during creation in case retry needed after state lost | // stored on native only during creation in case retry needed after state lost | ||||
+localMediaSelection?: NativeMediaSelection, | +localMediaSelection?: NativeMediaSelection, | ||||
}; | }; | ||||
export const imageValidator: TInterface<Image> = tShape<Image>({ | |||||
id: tID, | |||||
uri: t.String, | |||||
type: tString('photo'), | |||||
dimensions: dimensionsValidator, | |||||
localMediaSelection: t.maybe(nativeMediaSelectionValidator), | |||||
}); | |||||
export type EncryptedImage = { | export type EncryptedImage = { | ||||
+id: string, | +id: string, | ||||
// a media URI for keyserver uploads / blob holder for Blob service uploads | // a media URI for keyserver uploads / blob holder for Blob service uploads | ||||
+holder: string, | +holder: string, | ||||
+encryptionKey: string, | +encryptionKey: string, | ||||
+type: 'encrypted_photo', | +type: 'encrypted_photo', | ||||
+dimensions: Dimensions, | +dimensions: Dimensions, | ||||
}; | }; | ||||
export const encryptedImageValidator: TInterface<EncryptedImage> = | |||||
tShape<EncryptedImage>({ | |||||
id: tID, | |||||
holder: t.String, | |||||
encryptionKey: t.String, | |||||
type: tString('encrypted_photo'), | |||||
dimensions: dimensionsValidator, | |||||
}); | |||||
export type Video = { | export type Video = { | ||||
+id: string, | +id: string, | ||||
+uri: string, | +uri: string, | ||||
+type: 'video', | +type: 'video', | ||||
+dimensions: Dimensions, | +dimensions: Dimensions, | ||||
+loop?: boolean, | +loop?: boolean, | ||||
+thumbnailID: string, | +thumbnailID: string, | ||||
+thumbnailURI: string, | +thumbnailURI: string, | ||||
// stored on native only during creation in case retry needed after state lost | // stored on native only during creation in case retry needed after state lost | ||||
+localMediaSelection?: NativeMediaSelection, | +localMediaSelection?: NativeMediaSelection, | ||||
}; | }; | ||||
export const videoValidator: TInterface<Video> = tShape<Video>({ | |||||
id: tID, | |||||
uri: t.String, | |||||
type: tString('video'), | |||||
dimensions: dimensionsValidator, | |||||
loop: t.maybe(t.Boolean), | |||||
thumbnailID: tID, | |||||
thumbnailURI: t.String, | |||||
localMediaSelection: t.maybe(nativeMediaSelectionValidator), | |||||
}); | |||||
export type EncryptedVideo = { | export type EncryptedVideo = { | ||||
+id: string, | +id: string, | ||||
// a media URI for keyserver uploads / blob holder for Blob service uploads | // a media URI for keyserver uploads / blob holder for Blob service uploads | ||||
+holder: string, | +holder: string, | ||||
+encryptionKey: string, | +encryptionKey: string, | ||||
+type: 'encrypted_video', | +type: 'encrypted_video', | ||||
+dimensions: Dimensions, | +dimensions: Dimensions, | ||||
+loop?: boolean, | +loop?: boolean, | ||||
+thumbnailID: string, | +thumbnailID: string, | ||||
+thumbnailHolder: string, | +thumbnailHolder: string, | ||||
+thumbnailEncryptionKey: string, | +thumbnailEncryptionKey: string, | ||||
}; | }; | ||||
export const encryptedVideoValidator: TInterface<EncryptedVideo> = | |||||
tShape<EncryptedVideo>({ | |||||
id: tID, | |||||
holder: t.String, | |||||
encryptionKey: t.String, | |||||
type: tString('encrypted_video'), | |||||
dimensions: dimensionsValidator, | |||||
loop: t.maybe(t.Boolean), | |||||
thumbnailID: tID, | |||||
thumbnailHolder: t.String, | |||||
thumbnailEncryptionKey: t.String, | |||||
}); | |||||
export type Media = Image | Video | EncryptedImage | EncryptedVideo; | export type Media = Image | Video | EncryptedImage | EncryptedVideo; | ||||
export const mediaValidator: TUnion<Media> = t.union([ | |||||
imageValidator, | |||||
videoValidator, | |||||
encryptedImageValidator, | |||||
encryptedVideoValidator, | |||||
]); |