UNPKG

@amityco/ts-sdk-react-native

Version:

Amity Social Cloud Typescript SDK

106 lines (92 loc) 3.38 kB
import { pullFromCache } from '~/cache/api'; import { commentLinkedObject } from '~/utils/linkedObject/commentLinkedObject'; import AnalyticsEngine from '../../analytic/service/analytic/AnalyticsEngine'; import { userLinkedObject } from './userLinkedObject'; import { isAmityFilePost, isAmityImagePost, isAmityLivestreamPost, isAmityPollPost, isAmityVideoPost, isAmityClipPost, } from '../postTypePredicate'; import { shallowClone } from '../shallowClone'; import { streamLinkedObject } from './streamLinkedObject'; import { isNonNullable } from '..'; export const postLinkedObject = (post: Amity.InternalPost): Amity.Post => { return shallowClone(post, { childrenPosts: post.children .map(childPost => pullFromCache<Amity.InternalPost>(['post', 'get', childPost])?.data) .filter(isNonNullable) .map(postLinkedObject), analytics: { markAsViewed: () => { const analyticsEngineInstance = AnalyticsEngine.getInstance(); analyticsEngineInstance.markPostAsViewed(post.postId); }, }, get latestComments(): (Amity.Comment | null)[] { if (!post.comments) return []; return ( post.comments .map(commentId => { const commentCached = pullFromCache<Amity.InternalComment>([ 'comment', 'get', commentId, ])?.data; if (!commentCached) return null; return commentLinkedObject(commentCached); }) .filter(Boolean) || [] ); }, get creator(): Amity.User | undefined { const cacheData = pullFromCache<Amity.User>(['user', 'get', post.postedUserId]); if (!cacheData?.data) return; return userLinkedObject(cacheData.data); }, getImageInfo(): Amity.File<'image'> | undefined { return isAmityImagePost(post) ? pullFromCache<Amity.File<'image'>>(['file', 'get', post?.data?.fileId])?.data : undefined; }, getVideoInfo(): Amity.File<'video'> | undefined { return isAmityVideoPost(post) ? pullFromCache<Amity.File<'video'>>(['file', 'get', post?.data?.videoFileId?.original]) ?.data : undefined; }, getVideoThumbnailInfo(): Amity.File<'image'> | undefined { return isAmityVideoPost(post) ? pullFromCache<Amity.File<'image'>>(['file', 'get', post?.data?.thumbnailFileId])?.data : undefined; }, getFileInfo(): Amity.File<'file'> | undefined { return isAmityFilePost(post) ? pullFromCache<Amity.File<'file'>>(['file', 'get', post?.data?.fileId])?.data : undefined; }, getLivestreamInfo(): Amity.Stream | undefined { if (!isAmityLivestreamPost(post)) return; const cache = pullFromCache<Amity.InternalStream>([ 'stream', 'get', post?.data?.streamId, ])?.data; if (!cache) return; return streamLinkedObject(cache); }, getPollInfo(): Amity.Poll | undefined { if (!isAmityPollPost(post)) return; const cache = pullFromCache<Amity.Poll>(['poll', 'get', post?.data?.pollId])?.data; if (!cache) return; return cache; }, getClipInfo(): Amity.File<'clip'> | undefined { return isAmityClipPost(post) ? pullFromCache<Amity.File<'clip'>>(['file', 'get', post?.data?.fileId])?.data : undefined; }, }); };