@amityco/ts-sdk
Version:
Amity Social Cloud Typescript SDK
69 lines (58 loc) • 2.24 kB
text/typescript
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, isAmityVideoPost } from '../postTypePredicate';
export const postLinkedObject = (post: Amity.InternalPost): Amity.Post => {
return {
...post,
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;
},
};
};