UNPKG

@amityco/ts-sdk-react-native

Version:

Amity Social Cloud Typescript SDK

98 lines (90 loc) 3.83 kB
import { pullFromCache } from '~/cache/api'; import { onCommentCreated, onCommentDeleted } from '~/commentRepository'; import { getPost } from '~/postRepository/internalApi'; import { onPostCreated, onPostUpdated, onPostDeleted, onPostFlagged, onPostUnflagged, onPostReactionAdded, onPostReactionRemoved, onPostApproved, onPostDeclined, } from '~/postRepository/events'; import { onLocalPostDeleted } from '~/postRepository/events/onLocalPostDeleted'; import { onLocalPostReactionAdded } from '~/postRepository/events/onLocalPostReactionAdded'; import { onLocalPostReactionRemoved } from '~/postRepository/events/onLocalPostReactionRemoved'; import { onPostUpdatedLocal } from '~/postRepository/events/onPostUpdatedLocal'; import { EnumPostActions } from '~/postRepository/observers/enums'; import { convertEventPayload } from '~/utils/event'; import { onCommentCreatedLocal } from '~/commentRepository/events/onCommentCreatedLocal'; import { onCommentDeleteLocal } from '~/commentRepository/events/onCommentDeletedLocal'; import { onCommentReactionAdded, onCommentReactionRemoved } from '~/commentRepository/events'; import { onLocalCommentReactionAdded } from '~/commentRepository/events/onLocalCommentReactionAdded'; import { onLocalCommentReactionRemoved } from '~/commentRepository/events/onLocalCommentReactionRemoved'; type CommentEventHandler = (callback: Amity.Listener<Amity.InternalComment>) => Amity.Unsubscriber; const commentEventHandler = ( callback: Amity.Listener<Amity.InternalComment>, eventHandler: CommentEventHandler, cacheKey: Amity.CacheKey, resolveId?: (id: string) => string, ) => { return eventHandler(async (comment: Amity.InternalComment) => { const currentCollection = pullFromCache<Amity.PinnedPostLiveCollectionCache>(cacheKey)?.data; if ( !currentCollection || !currentCollection.data.includes( resolveId ? resolveId(comment.referenceId) : comment.referenceId, ) ) return; await getPost(comment.referenceId); callback(comment); }); }; export const generateCommentSubscriptions = ({ cacheKey, resolveId, }: { cacheKey: Amity.CacheKey; resolveId?: (id: string) => string; }) => { const eventHandlers = [ onCommentCreated, onCommentDeleted, onCommentReactionAdded, onCommentReactionRemoved, onCommentCreatedLocal, onCommentDeleteLocal, onLocalCommentReactionAdded, onLocalCommentReactionRemoved, ]; return eventHandlers.map(handler => ({ fn: convertEventPayload( (callback: Amity.Listener<Amity.InternalComment>) => commentEventHandler(callback, handler, cacheKey, resolveId), 'referenceId', 'post', ), action: EnumPostActions.OnPostUpdated, })); }; export const getPostSubscription = (cacheKey: Amity.CacheKey) => { return [ { fn: onPostCreated, action: EnumPostActions.OnPostCreated }, { fn: onPostUpdated, action: EnumPostActions.OnPostUpdated }, { fn: onPostUpdatedLocal, action: EnumPostActions.OnPostUpdated }, { fn: onPostDeleted, action: EnumPostActions.OnPostDeleted }, { fn: onPostFlagged, action: EnumPostActions.OnPostFlagged }, { fn: onPostUnflagged, action: EnumPostActions.OnPostUnflagged }, { fn: onPostApproved, action: EnumPostActions.OnPostApproved }, { fn: onPostDeclined, action: EnumPostActions.OnPostDeclined }, { fn: onPostReactionAdded, action: EnumPostActions.OnPostReactionAdded }, { fn: onPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved }, { fn: onLocalPostReactionAdded, action: EnumPostActions.OnPostReactionAdded }, { fn: onLocalPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved }, { fn: onLocalPostDeleted, action: EnumPostActions.OnPostDeleted }, ...generateCommentSubscriptions({ cacheKey }), ]; };