UNPKG

@amityco/ts-sdk-react-native

Version:

Amity Social Cloud Typescript SDK

163 lines (144 loc) 5.98 kB
import hash from 'object-hash'; import { pullFromCache, pushToCache } from '~/cache/api'; import { UserFeedPaginationController } from './PaginationController'; import { UserFeedQueryStreamController } from './QueryStreamController'; import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController'; import { onPostCreated, onPostUpdated, onPostDeleted, onPostFlagged, onPostUnflagged, onPostReactionAdded, onPostReactionRemoved, onPostApproved, onPostDeclined, } from '~/postRepository/events'; import { isNonNullable } from '~/utils'; import { EnumPostActions } from '~/postRepository/observers/enums'; import { LinkedObject } from '~/utils/linkedObject'; import { preparePostPayload } from '~/postRepository/utils/payload'; import { convertEventPayload } from '~/utils/event'; import { onCommentCreated, onCommentDeleted } from '~/commentRepository'; import { getPost } from '~/postRepository/internalApi/getPost'; import { onPostUpdatedLocal } from '~/postRepository/events/onPostUpdatedLocal'; import { onLocalPostReactionAdded } from '~/postRepository/events/onLocalPostReactionAdded'; import { onLocalPostReactionRemoved } from '~/postRepository/events/onLocalPostReactionRemoved'; import { onLocalPostDeleted } from '~/postRepository/events/onLocalPostDeleted'; export class UserFeedLiveCollectionController extends LiveCollectionController< 'post', Amity.UserFeedLiveCollection, Amity.Post, UserFeedPaginationController > { private queryStreamController: UserFeedQueryStreamController; private query: Amity.UserFeedLiveCollection; constructor( query: Amity.UserFeedLiveCollection, callback: Amity.LiveCollectionCallback<Amity.Post>, ) { const queryStreamId = hash(query); const cacheKey = ['userFeed', 'collection', queryStreamId]; const paginationController = new UserFeedPaginationController(query); super(paginationController, queryStreamId, cacheKey, callback); this.query = query; this.queryStreamController = new UserFeedQueryStreamController( this.query, this.cacheKey, this.notifyChange.bind(this), preparePostPayload, ); this.callback = callback.bind(this); this.loadPage({ initial: true }); } protected setup() { const collection = pullFromCache<Amity.UserFeedLiveCollectionCache>(this.cacheKey)?.data; if (!collection) { pushToCache(this.cacheKey, { data: [], params: {}, }); } } protected async persistModel(queryPayload: Amity.PostPayload & Amity.Pagination) { await this.queryStreamController.saveToMainDB(queryPayload); } protected persistQueryStream({ response, direction, refresh, }: Amity.LiveCollectionPersistQueryStreamParams<'post'>) { this.queryStreamController.appendToQueryStream(response, direction, refresh); } startSubscription() { return this.queryStreamController.subscribeRTE([ { 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 }, { fn: convertEventPayload( (callback: Amity.Listener<Amity.InternalComment>) => { return onCommentCreated(async (comment: Amity.InternalComment) => { const currentCollection = pullFromCache<Amity.UserFeedLiveCollectionCache>( this.cacheKey, )?.data; if (!currentCollection || currentCollection.data.includes(comment.referenceId)) return; await getPost(comment.referenceId); callback(comment); }); }, 'referenceId', 'post', ), action: EnumPostActions.OnPostUpdated, }, { fn: convertEventPayload( (callback: Amity.Listener<Amity.InternalComment>) => { return onCommentDeleted(async (comment: Amity.InternalComment) => { const currentCollection = pullFromCache<Amity.UserFeedLiveCollectionCache>( this.cacheKey, )?.data; if (!currentCollection || currentCollection.data.includes(comment.referenceId)) return; await getPost(comment.referenceId); callback(comment); }); }, 'referenceId', 'post', ), action: EnumPostActions.OnPostUpdated, }, ]); } notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) { const collection = pullFromCache<Amity.UserFeedLiveCollectionCache>(this.cacheKey)?.data; if (!collection) return; const data = ( collection.data .map(id => pullFromCache<Amity.InternalPost>(['post', 'get', id])!) .filter(isNonNullable) .map(({ data }) => data) ?? [] ).map(LinkedObject.post); if (!this.shouldNotify(data) && origin === 'event') return; this.callback({ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }), data, hasNextPage: !!this.paginationController.getNextToken(), loading, error, }); } }