@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
100 lines (81 loc) • 3.41 kB
text/typescript
import { QueryStreamController } from '~/core/liveCollection/QueryStreamController';
import { pullFromCache, pushToCache } from '~/cache/api';
import { ingestInCache } from '~/cache/api/ingestInCache';
import { getResolver } from '~/core/model';
import { getActiveClient } from '~/client';
import { EnumPostActions } from '~/postRepository/observers/enums';
export class GlobalFeedQueryStreamController extends QueryStreamController<
Amity.PostPayload,
Amity.GlobalFeedLiveCollection
> {
private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
private preparePayload: (response: Amity.PostPayload) => Amity.ProcessedPostPayload;
constructor(
query: Amity.GlobalFeedLiveCollection,
cacheKey: string[],
notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
preparePayload: (response: Amity.PostPayload) => Amity.ProcessedPostPayload,
) {
super(query, cacheKey);
this.notifyChange = notifyChange;
this.preparePayload = preparePayload;
}
async saveToMainDB(response: Amity.PostPayload) {
const processedPayload = await this.preparePayload(response);
const client = getActiveClient();
const cachedAt = client.cache && Date.now();
if (client.cache) {
ingestInCache(processedPayload, { cachedAt });
}
}
appendToQueryStream(
response: Amity.PostPayload & Partial<Amity.Pagination>,
direction: Amity.LiveCollectionPageDirection,
refresh = false,
) {
if (refresh) {
pushToCache(this.cacheKey, {
data: response.posts.map(getResolver('post')),
});
} else {
const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(this.cacheKey)?.data;
const posts = collection?.data ?? [];
pushToCache(this.cacheKey, {
...collection,
data: [...new Set([...posts, ...response.posts.map(getResolver('post'))])],
});
}
}
reactor(action: EnumPostActions) {
return (post: Amity.InternalPost) => {
const collection = pullFromCache<Amity.GlobalFeedLiveCollectionCache>(this.cacheKey)?.data;
if (!collection) return;
if (post.parentPostId && !collection.data.includes(post.parentPostId)) return;
// Delete Action
if (action === EnumPostActions.OnPostDeleted) {
// Parent Post - Remove from collection
collection.data = collection.data.filter(postId => postId !== post.postId);
}
if (action === EnumPostActions.OnPostCreated || action === EnumPostActions.OnPostApproved) {
// If the query has dataTypes array with value, check if post.dataType is include in the array
if (this.query.dataTypes && this.query.dataTypes.length > 0) {
if (!this.query.dataTypes.includes(post.dataType)) return;
}
collection.data = [...new Set([post.postId, ...collection.data])];
}
if (action === EnumPostActions.OnPostDeclined) {
collection.data = collection.data.filter(postId => postId !== post.postId);
}
pushToCache(this.cacheKey, collection);
this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
};
}
subscribeRTE(
createSubscriber: {
fn: (reactor: (post: Amity.InternalPost) => void) => Amity.Unsubscriber;
action: EnumPostActions;
}[],
) {
return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
}
}