UNPKG

stream-chat

Version:

JS SDK for the Stream Chat API

132 lines (112 loc) 4.63 kB
import type { Attachment, SharedLocationResponse } from '../types'; import type { AttachmentLoadingState, AudioAttachment, FileAttachment, GiphyAttachment, ImageAttachment, LocalAttachment, LocalAudioAttachment, LocalFileAttachment, LocalImageAttachment, LocalUploadAttachment, LocalVideoAttachment, LocalVoiceRecordingAttachment, UploadedAttachment, VideoAttachment, VoiceRecordingAttachment, } from './types'; export const isScrapedContent = (attachment: Attachment) => !!attachment?.og_scrape_url || !!attachment?.title_link; export const isLocalAttachment = (attachment: unknown): attachment is LocalAttachment => !!(attachment as LocalAttachment)?.localMetadata?.id; export const isLocalUploadAttachment = ( attachment: unknown, ): attachment is LocalUploadAttachment => !!(attachment as LocalAttachment)?.localMetadata?.uploadState; /** * Upload states meaning "the bytes are not on the CDN yet, but they are on their way". * `blocked` and `failed` are excluded - those never resolve on their own. * * Typed against {@link AttachmentLoadingState} rather than `string`, so renaming a state fails * the build instead of silently making this list match nothing - it decides what gets sent. */ const PENDING_UPLOAD_STATES: ReadonlyArray<AttachmentLoadingState> = [ 'pending', 'uploading', ]; /** Whether an upload for this attachment is still expected to resolve. */ export const isPendingUpload = ( attachment: unknown, ): attachment is LocalUploadAttachment => isLocalUploadAttachment(attachment) && PENDING_UPLOAD_STATES.includes(attachment.localMetadata.uploadState); /** Whether this attachment's upload already resolved to a URL. */ export const isFinishedUpload = ( attachment: unknown, ): attachment is LocalUploadAttachment => isLocalUploadAttachment(attachment) && attachment.localMetadata.uploadState === 'finished'; export const isFileAttachment = ( attachment: Attachment | LocalAttachment, supportedVideoFormat: string[] = [], ): attachment is FileAttachment => attachment.type === 'file' || !!( attachment.mime_type && supportedVideoFormat.indexOf(attachment.mime_type) === -1 && attachment.type !== 'video' ); export const isLocalFileAttachment = ( attachment: Attachment | LocalAttachment, ): attachment is LocalFileAttachment => isFileAttachment(attachment) && isLocalAttachment(attachment); export const isImageAttachment = ( attachment: Attachment, ): attachment is ImageAttachment => attachment.type === 'image' && !isScrapedContent(attachment); export const isLocalImageAttachment = ( attachment: Attachment | LocalAttachment, ): attachment is LocalImageAttachment => isImageAttachment(attachment) && isLocalAttachment(attachment); export const isAudioAttachment = ( attachment: Attachment | LocalAttachment, ): attachment is AudioAttachment => attachment.type === 'audio'; export const isLocalAudioAttachment = ( attachment: Attachment | LocalAttachment, ): attachment is LocalAudioAttachment => isAudioAttachment(attachment) && isLocalAttachment(attachment); export const isVoiceRecordingAttachment = ( attachment: Attachment | LocalAttachment, ): attachment is VoiceRecordingAttachment => attachment.type === 'voiceRecording'; export const isLocalVoiceRecordingAttachment = ( attachment: Attachment | LocalAttachment, ): attachment is LocalVoiceRecordingAttachment => isVoiceRecordingAttachment(attachment) && isLocalAttachment(attachment); export const isVideoAttachment = ( attachment: Attachment | LocalAttachment, supportedVideoFormat: string[] = [], ): attachment is VideoAttachment => attachment.type === 'video' || !!(attachment.mime_type && supportedVideoFormat.indexOf(attachment.mime_type) !== -1); export const isLocalVideoAttachment = ( attachment: Attachment | LocalAttachment, ): attachment is LocalVideoAttachment => isVideoAttachment(attachment) && isLocalAttachment(attachment); export const isUploadedAttachment = ( attachment: Attachment, ): attachment is UploadedAttachment => isAudioAttachment(attachment) || isFileAttachment(attachment) || isImageAttachment(attachment) || isVideoAttachment(attachment) || isVoiceRecordingAttachment(attachment); export const isSharedLocationResponse = ( location: unknown, ): location is SharedLocationResponse => !!(location as SharedLocationResponse).latitude && !!(location as SharedLocationResponse).longitude && !!(location as SharedLocationResponse).channel_cid; export const isGiphyAttachment = ( attachment: Attachment, ): attachment is GiphyAttachment => attachment.type === 'giphy';