UNPKG

@amityco/ts-sdk-react-native

Version:

Amity Social Cloud Typescript SDK

127 lines (105 loc) 3.82 kB
import hash from 'object-hash'; import { pullFromCache, pushToCache } from '~/cache/api'; import { PostPaginationController } from './PostPaginationController'; import { PostQueryStreamController } from './PostQueryStreamController'; import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController'; import { filterByFeedType, filterByPostDataTypes, filterByPropEquality, sortByFirstCreated, sortByLastCreated, } from '~/core/query'; import { isNonNullable } from '~/utils'; import { LinkedObject } from '~/utils/linkedObject'; import { preparePostPayload } from '~/postRepository/utils/payload'; import { getPostSubscription } from '../utils'; export class PostLiveCollectionController extends LiveCollectionController< 'post', Amity.PostLiveCollection, Amity.Post, PostPaginationController > { private queryStreamController: PostQueryStreamController; private query: Amity.PostLiveCollection; constructor(query: Amity.PostLiveCollection, callback: Amity.LiveCollectionCallback<Amity.Post>) { const queryStreamId = hash(query); const cacheKey = ['posts', 'collection', queryStreamId]; const paginationController = new PostPaginationController(query); super(paginationController, queryStreamId, cacheKey, callback); this.query = query; this.queryStreamController = new PostQueryStreamController( this.query, this.cacheKey, this.notifyChange.bind(this), preparePostPayload, ); this.callback = callback.bind(this); this.loadPage({ initial: true }); } protected setup() { const collection = pullFromCache<Amity.PostLiveCollectionCache>(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(getPostSubscription(this.cacheKey)); } notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) { const collection = pullFromCache<Amity.PostLiveCollectionCache>(this.cacheKey)?.data; if (!collection) return; const data = this.applyFilter( 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, }); } applyFilter(data: Amity.InternalPost[]) { let posts = data; if (!this.query.includeDeleted) { posts = filterByPropEquality(posts, 'isDeleted', false); } if (this.query.tags) { posts = posts.filter(p => p.tags?.some(t => this.query.tags?.includes(t))); } if (this.query.targetType === 'community' && this.query.feedType) { posts = filterByFeedType(posts, this.query.feedType); } if (this.query.dataTypes?.length) { posts = filterByPostDataTypes(posts, this.query.dataTypes); } switch (this.query.sortBy) { case 'firstCreated': posts = posts.sort(sortByFirstCreated); break; case 'lastCreated': default: posts = posts.sort(sortByLastCreated); break; } return posts; } }