@amityco/ts-sdk 6.28.0 → 6.28.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/@types/core/payload.d.ts +1 -1
  2. package/dist/@types/core/payload.d.ts.map +1 -1
  3. package/dist/commentRepository/observers/getComments.d.ts +1 -1
  4. package/dist/commentRepository/observers/getComments.d.ts.map +1 -1
  5. package/dist/communityRepository/utils/payload.d.ts +7 -0
  6. package/dist/communityRepository/utils/payload.d.ts.map +1 -1
  7. package/dist/index.cjs.js +284 -180
  8. package/dist/index.esm.js +284 -180
  9. package/dist/index.umd.js +2 -2
  10. package/dist/postRepository/internalApi/getPost.d.ts +5 -0
  11. package/dist/postRepository/internalApi/getPost.d.ts.map +1 -0
  12. package/dist/postRepository/internalApi/index.d.ts +13 -0
  13. package/dist/postRepository/internalApi/index.d.ts.map +1 -0
  14. package/dist/postRepository/observers/getPosts/PostLiveCollectionController.d.ts +14 -0
  15. package/dist/postRepository/observers/getPosts/PostLiveCollectionController.d.ts.map +1 -0
  16. package/dist/postRepository/observers/getPosts/PostPaginationController.d.ts +5 -0
  17. package/dist/postRepository/observers/getPosts/PostPaginationController.d.ts.map +1 -0
  18. package/dist/postRepository/observers/getPosts/PostQueryStreamController.d.ts +15 -0
  19. package/dist/postRepository/observers/getPosts/PostQueryStreamController.d.ts.map +1 -0
  20. package/dist/postRepository/observers/getPosts/enums.d.ts +12 -0
  21. package/dist/postRepository/observers/getPosts/enums.d.ts.map +1 -0
  22. package/dist/postRepository/observers/getPosts.d.ts.map +1 -1
  23. package/dist/postRepository/utils/payload.d.ts +2 -0
  24. package/dist/postRepository/utils/payload.d.ts.map +1 -0
  25. package/dist/utils/tests/dummy/post.d.ts +3 -3
  26. package/package.json +1 -1
  27. package/src/@types/core/payload.ts +1 -1
  28. package/src/commentRepository/observers/getComments.ts +1 -1
  29. package/src/communityRepository/utils/payload.ts +19 -3
  30. package/src/postRepository/internalApi/getPost.ts +65 -0
  31. package/src/postRepository/internalApi/index.ts +16 -0
  32. package/src/postRepository/observers/getPosts/PostLiveCollectionController.ts +190 -0
  33. package/src/postRepository/observers/getPosts/PostPaginationController.ts +45 -0
  34. package/src/postRepository/observers/getPosts/PostQueryStreamController.ts +102 -0
  35. package/src/postRepository/observers/getPosts/enums.ts +11 -0
  36. package/src/postRepository/observers/getPosts.ts +6 -197
  37. package/src/postRepository/utils/payload.ts +17 -0
package/dist/index.cjs.js CHANGED
@@ -102,8 +102,8 @@ const PostContentType = Object.freeze({
102
102
 
103
103
  function getVersion() {
104
104
  try {
105
- // the string ''v6.28.0-cjs'' should be replaced by actual value by @rollup/plugin-replace
106
- return 'v6.28.0-cjs';
105
+ // the string ''v6.28.1-cjs'' should be replaced by actual value by @rollup/plugin-replace
106
+ return 'v6.28.1-cjs';
107
107
  }
108
108
  catch (error) {
109
109
  return '__dev__';
@@ -12464,7 +12464,7 @@ getMessage$2.locally = (messageId) => {
12464
12464
  * @category Post API
12465
12465
  * @async
12466
12466
  */
12467
- const getPost$1 = async (postId) => {
12467
+ const getPost$2 = async (postId) => {
12468
12468
  const client = getActiveClient();
12469
12469
  client.log('post/getPost', postId);
12470
12470
  isInTombstone('post', postId);
@@ -12504,7 +12504,7 @@ const getPost$1 = async (postId) => {
12504
12504
  *
12505
12505
  * @category Post API
12506
12506
  */
12507
- getPost$1.locally = (postId) => {
12507
+ getPost$2.locally = (postId) => {
12508
12508
  const client = getActiveClient();
12509
12509
  client.log('post/getPost.locally', postId);
12510
12510
  if (!client.cache)
@@ -17904,7 +17904,7 @@ const convertCommunityUsersToUniqueObject = (communityUsers) => {
17904
17904
  });
17905
17905
  return result;
17906
17906
  };
17907
- const prepareCommunityPayload = (rawPayload) => {
17907
+ const convertCommunityPayload = (rawPayload) => {
17908
17908
  // Unpack community payload by mapping payload field to postSetting value.
17909
17909
  const communities = rawPayload.communities.map((_a) => {
17910
17910
  var { needApprovalOnPostCreation, onlyAdminCanPost } = _a, restCommunityPayload = __rest(_a, ["needApprovalOnPostCreation", "onlyAdminCanPost"]);
@@ -17928,6 +17928,11 @@ const prepareCommunityPayload = (rawPayload) => {
17928
17928
  const communityWithMembershipStatus = updateMembershipStatus(communities, communityUsers);
17929
17929
  return Object.assign(Object.assign({}, rawPayload), { communities: communityWithMembershipStatus, communityUsers });
17930
17930
  };
17931
+ const prepareCommunityPayload = (rawPayload) => {
17932
+ const { communities, communityUsers } = convertCommunityPayload(rawPayload);
17933
+ return Object.assign(Object.assign({}, rawPayload), { communities,
17934
+ communityUsers });
17935
+ };
17931
17936
  const prepareCommunityRequest = (params) => {
17932
17937
  const { postSetting = undefined, storySetting } = params, restParam = __rest(params, ["postSetting", "storySetting"]);
17933
17938
  return Object.assign(Object.assign(Object.assign({}, restParam), (postSetting ? CommunityPostSettingMaps[postSetting] : undefined)), {
@@ -20660,7 +20665,7 @@ const editPost = async (postId, patch) => {
20660
20665
  const deletePost = async (postId, permanent = false) => {
20661
20666
  var _a;
20662
20667
  const client = getActiveClient();
20663
- const post = await getPost$1(postId);
20668
+ const post = await getPost$2(postId);
20664
20669
  await client.http.delete(`/api/v4/posts/${encodeURIComponent(postId)}`, {
20665
20670
  params: {
20666
20671
  postId,
@@ -20999,7 +21004,7 @@ const observePost = (postId, callback, policy = 'cache_then_server') => {
20999
21004
  };
21000
21005
  const disposers = [];
21001
21006
  disposers.push(onPostUpdated(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onUpdate')), onPostDeleted(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onDelete')), onPostApproved(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onApproved')), onPostDeclined(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onDeclined')), onPostFlagged(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onFlagged')), onPostUnflagged(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onUnflagged')), onPostReactionAdded(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onReactionAdded')), onPostReactionRemoved(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onReactionRemoved')));
21002
- runQuery(createQuery(getPost$1, postId), result => (result.data || result.error) &&
21007
+ runQuery(createQuery(getPost$2, postId), result => (result.data || result.error) &&
21003
21008
  router(Object.assign(Object.assign({}, result), { data: result.data && LinkedObject.post(result.data) }), 'onFetch'), queryOptions(policy));
21004
21009
  return () => {
21005
21010
  log(`observePosts(tmpid: ${timestamp}) > dispose`);
@@ -21114,7 +21119,7 @@ const createComment = async (bundle) => {
21114
21119
  if (client.cache)
21115
21120
  ingestInCache(data, { cachedAt });
21116
21121
  if (['post', 'content'].includes(bundle.referenceType)) {
21117
- const post = await getPost$1(bundle.referenceId);
21122
+ const post = await getPost$2(bundle.referenceId);
21118
21123
  fireEvent('local.post.updated', {
21119
21124
  posts: [post.data],
21120
21125
  categories: [],
@@ -21308,7 +21313,7 @@ const deleteComment = async (commentId, permanent = false) => {
21308
21313
  });
21309
21314
  }
21310
21315
  else {
21311
- const post = await getPost$1(comment.data.referenceId);
21316
+ const post = await getPost$2(comment.data.referenceId);
21312
21317
  fireEvent('local.post.updated', {
21313
21318
  posts: [post.data],
21314
21319
  categories: [],
@@ -21912,12 +21917,12 @@ var index$6 = /*#__PURE__*/Object.freeze({
21912
21917
  *
21913
21918
  * @category Post Live Object
21914
21919
  */
21915
- const getPost = (postId, callback) => {
21920
+ const getPost$1 = (postId, callback) => {
21916
21921
  const responder = (snapshot) => {
21917
21922
  const { data } = snapshot;
21918
21923
  callback(Object.assign(Object.assign({}, snapshot), { data: data ? LinkedObject.post(snapshot.data) : data }));
21919
21924
  };
21920
- return liveObject(postId, responder, 'postId', getPost$1, [
21925
+ return liveObject(postId, responder, 'postId', getPost$2, [
21921
21926
  onPostApproved,
21922
21927
  onPostDeclined,
21923
21928
  (callback) => {
@@ -21957,7 +21962,7 @@ const getPost = (postId, callback) => {
21957
21962
  convertEventPayload((callback) => {
21958
21963
  return onCommentCreated(async (comment) => {
21959
21964
  if (comment.referenceId === postId) {
21960
- await getPost$1(postId);
21965
+ await getPost$2(postId);
21961
21966
  callback(comment);
21962
21967
  }
21963
21968
  });
@@ -21965,7 +21970,7 @@ const getPost = (postId, callback) => {
21965
21970
  convertEventPayload((callback) => {
21966
21971
  return onCommentDeleted(async (comment) => {
21967
21972
  if (comment.referenceId === postId) {
21968
- await getPost$1(postId);
21973
+ await getPost$2(postId);
21969
21974
  callback(comment);
21970
21975
  }
21971
21976
  });
@@ -21974,61 +21979,272 @@ const getPost = (postId, callback) => {
21974
21979
  };
21975
21980
  /* end_public_function */
21976
21981
 
21977
- /**
21978
- * ```js
21979
- * import { queryPosts } from '@amityco/ts-sdk'
21980
- * const { data: posts, prevPage, nextPage } = await queryPosts({ targetId, targetType })
21981
- * ```
21982
- *
21983
- * Queries a paginable list of {@link Amity.Post} objects
21984
- *
21985
- * @param query The query parameters
21986
- * @returns posts
21987
- *
21988
- * @category Post API
21989
- * @async
21990
- */
21991
- const queryPosts = async (query) => {
21992
- const client = getActiveClient();
21993
- client.log('post/queryPosts', query);
21994
- const { page, limit = 10, includeDeleted } = query, params = __rest(query, ["page", "limit", "includeDeleted"]);
21995
- const { dataTypes, matchingOnlyParentPost } = params;
21996
- const options = (() => {
21997
- if (page)
21998
- return { token: page };
21999
- if (limit)
22000
- return { limit };
22001
- return undefined;
22002
- })();
22003
- // API-FIX: parameters should be querystring. (1)
22004
- // API-FIX: backend should answer Amity.Response (2)
22005
- // const { data } = await client.http.get<Amity.Response<Amity.PagedResponse<Amity.PostPayload>>>(
22006
- const { data } = await client.http.get(`/api/v4/posts`, {
22007
- params: Object.assign(Object.assign({}, params), { isDeleted: inferIsDeleted(includeDeleted),
22008
- /*
22009
- * when creating post like image, file, video BE will create 2 posts
22010
- * 1. parent post to store text with dataType=text
22011
- * 2. child post to store dataTypes post data
22012
- *
22013
- * By default, BE queries only parent post
22014
- */
22015
- matchingOnlyParentPost: matchingOnlyParentPost !== null && matchingOnlyParentPost !== void 0 ? matchingOnlyParentPost : !(dataTypes === null || dataTypes === void 0 ? void 0 : dataTypes.length), options }),
21982
+ class PostPaginationController extends PaginationController {
21983
+ async getRequest(queryParams, token) {
21984
+ const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, includeDeleted, matchingOnlyParentPost } = queryParams, params = __rest(queryParams, ["limit", "includeDeleted", "matchingOnlyParentPost"]);
21985
+ const { dataTypes } = params;
21986
+ const baseOptions = {
21987
+ type: params.sortBy || queryParams.limit ? 'pagination' : undefined,
21988
+ };
21989
+ const options = token ? Object.assign(Object.assign({}, baseOptions), { token }) : Object.assign(Object.assign({}, baseOptions), { limit });
21990
+ const { data: queryResponse } = await this.http.get(`/api/v4/posts`, {
21991
+ params: Object.assign(Object.assign({}, params), { isDeleted: inferIsDeleted(includeDeleted),
21992
+ /*
21993
+ * when creating post like image, file, video BE will create 2 posts
21994
+ * 1. parent post to store text with dataType=text
21995
+ * 2. child post to store dataTypes post data
21996
+ *
21997
+ * By default, BE queries only parent post
21998
+ */
21999
+ matchingOnlyParentPost: matchingOnlyParentPost !== null && matchingOnlyParentPost !== void 0 ? matchingOnlyParentPost : !(dataTypes === null || dataTypes === void 0 ? void 0 : dataTypes.length), options }),
22000
+ });
22001
+ return queryResponse;
22002
+ }
22003
+ }
22004
+
22005
+ var EnumPostActions;
22006
+ (function (EnumPostActions) {
22007
+ EnumPostActions["OnPostCreated"] = "onPostCreated";
22008
+ EnumPostActions["OnPostUpdated"] = "onPostUpdated";
22009
+ EnumPostActions["OnPostDeleted"] = "onPostDeleted";
22010
+ EnumPostActions["OnPostFlagged"] = "onPostFlagged";
22011
+ EnumPostActions["OnPostUnflagged"] = "onPostUnflagged";
22012
+ EnumPostActions["OnPostReactionAdded"] = "onPostReactionAdded";
22013
+ EnumPostActions["OnPostReactionRemoved"] = "onPostReactionRemoved";
22014
+ EnumPostActions["OnPostApproved"] = "onPostApproved";
22015
+ EnumPostActions["OnPostDeclined"] = "onPostDeclined";
22016
+ })(EnumPostActions || (EnumPostActions = {}));
22017
+
22018
+ class PostQueryStreamController extends QueryStreamController {
22019
+ constructor(query, cacheKey, notifyChange, preparePayload) {
22020
+ super(query, cacheKey);
22021
+ this.notifyChange = notifyChange;
22022
+ this.preparePayload = preparePayload;
22023
+ }
22024
+ async saveToMainDB(response) {
22025
+ const processedPayload = await this.preparePayload(response);
22026
+ const client = getActiveClient();
22027
+ const cachedAt = client.cache && Date.now();
22028
+ if (client.cache) {
22029
+ ingestInCache(processedPayload, { cachedAt });
22030
+ }
22031
+ }
22032
+ appendToQueryStream(response, direction, refresh = false) {
22033
+ var _a, _b;
22034
+ if (refresh) {
22035
+ pushToCache(this.cacheKey, {
22036
+ data: response.posts.map(getResolver('post')),
22037
+ });
22038
+ }
22039
+ else {
22040
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22041
+ const posts = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
22042
+ pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: [...new Set([...posts, ...response.posts.map(getResolver('post'))])] }));
22043
+ }
22044
+ }
22045
+ reactor(action) {
22046
+ return (post) => {
22047
+ var _a, _b;
22048
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22049
+ if (!collection)
22050
+ return;
22051
+ if (post.parentPostId && post.isDeleted) {
22052
+ const parentPost = (_b = pullFromCache([
22053
+ 'post',
22054
+ 'get',
22055
+ post.parentPostId,
22056
+ ])) === null || _b === void 0 ? void 0 : _b.data;
22057
+ if (!parentPost || (parentPost === null || parentPost === void 0 ? void 0 : parentPost.targetId) !== this.query.targetId)
22058
+ return;
22059
+ parentPost.children = parentPost.children.filter(childId => childId !== post.postId);
22060
+ pushToCache(['post', 'get', parentPost.postId], parentPost);
22061
+ }
22062
+ else {
22063
+ if (this.query.targetId !== post.targetId)
22064
+ return;
22065
+ if (this.query.targetType !== post.targetType)
22066
+ return;
22067
+ }
22068
+ if (action === EnumPostActions.OnPostDeclined) {
22069
+ collection.data = collection.data.filter(postId => postId !== post.postId);
22070
+ }
22071
+ if (action === EnumPostActions.OnPostCreated || action === EnumPostActions.OnPostApproved) {
22072
+ collection.data = [...new Set([post.postId, ...collection.data])];
22073
+ }
22074
+ pushToCache(this.cacheKey, collection);
22075
+ this.notifyChange({ origin: "event" /* Amity.LiveDataOrigin.EVENT */, loading: false });
22076
+ };
22077
+ }
22078
+ subscribeRTE(createSubscriber) {
22079
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
22080
+ }
22081
+ }
22082
+
22083
+ const preparePostPayload = (postPayload) => {
22084
+ const { posts } = postPayload;
22085
+ const { communities, communityUsers } = convertCommunityPayload({
22086
+ communities: postPayload.communities,
22087
+ communityUsers: postPayload.communityUsers,
22016
22088
  });
22017
- // API-FIX: backend should answer Amity.Response (2)
22018
- // const { paging, posts } = unwrapPayload(data)
22019
- // unpacking
22020
- const { paging } = data, payload = __rest(data, ["paging"]);
22021
- const paperedPayload = prepareMembershipPayload(payload, 'communityUsers');
22022
- const { posts } = payload;
22023
- const cachedAt = client.cache && Date.now();
22024
- if (client.cache) {
22025
- ingestInCache(paperedPayload, { cachedAt });
22026
- const cacheKey = ['post', 'query', Object.assign(Object.assign({}, params), { options })];
22027
- pushToCache(cacheKey, { posts: posts.map(getResolver('post')), paging });
22089
+ return Object.assign(Object.assign({}, postPayload), { posts,
22090
+ communities,
22091
+ communityUsers });
22092
+ };
22093
+
22094
+ const getPost = async (postId) => {
22095
+ const client = getActiveClient();
22096
+ client.log('post/getPost', postId);
22097
+ isInTombstone('post', postId);
22098
+ let payload;
22099
+ try {
22100
+ // API-FIX: endpoint should not be /list, parameters should be querystring.
22101
+ const response = await client.http.get(`/api/v3/posts/${encodeURIComponent(postId)}`);
22102
+ payload = response.data;
22028
22103
  }
22029
- return { data: posts.map(LinkedObject.post), cachedAt, paging };
22104
+ catch (error) {
22105
+ if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
22106
+ pushToTombstone('post', postId);
22107
+ }
22108
+ throw error;
22109
+ }
22110
+ const data = prepareMembershipPayload(payload, 'communityUsers');
22111
+ const cachedAt = client.cache && Date.now();
22112
+ if (client.cache)
22113
+ ingestInCache(data, { cachedAt });
22114
+ const { posts } = data;
22115
+ const result = posts.find(post => post.postId === postId);
22116
+ return {
22117
+ data: result,
22118
+ cachedAt,
22119
+ };
22120
+ };
22121
+ getPost.locally = (postId) => {
22122
+ const client = getActiveClient();
22123
+ client.log('post/getPost.locally', postId);
22124
+ if (!client.cache)
22125
+ return;
22126
+ const cached = pullFromCache(['post', 'get', postId]);
22127
+ if (!cached)
22128
+ return;
22129
+ return {
22130
+ data: cached.data,
22131
+ cachedAt: cached.cachedAt,
22132
+ };
22030
22133
  };
22031
22134
 
22135
+ class PostLiveCollectionController extends LiveCollectionController {
22136
+ constructor(query, callback) {
22137
+ const queryStreamId = hash__default["default"](query);
22138
+ const cacheKey = ['posts', 'collection', queryStreamId];
22139
+ const paginationController = new PostPaginationController(query);
22140
+ super(paginationController, queryStreamId, cacheKey, callback);
22141
+ this.query = query;
22142
+ this.queryStreamController = new PostQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this), preparePostPayload);
22143
+ this.callback = callback.bind(this);
22144
+ this.loadPage({ initial: true });
22145
+ }
22146
+ setup() {
22147
+ var _a;
22148
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22149
+ if (!collection) {
22150
+ pushToCache(this.cacheKey, {
22151
+ data: [],
22152
+ params: {},
22153
+ });
22154
+ }
22155
+ }
22156
+ async persistModel(queryPayload) {
22157
+ await this.queryStreamController.saveToMainDB(queryPayload);
22158
+ }
22159
+ persistQueryStream({ response, direction, refresh, }) {
22160
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
22161
+ }
22162
+ startSubscription() {
22163
+ return this.queryStreamController.subscribeRTE([
22164
+ { fn: onPostCreated, action: EnumPostActions.OnPostCreated },
22165
+ { fn: onPostUpdated, action: EnumPostActions.OnPostUpdated },
22166
+ { fn: onPostDeleted, action: EnumPostActions.OnPostDeleted },
22167
+ { fn: onPostFlagged, action: EnumPostActions.OnPostFlagged },
22168
+ { fn: onPostUnflagged, action: EnumPostActions.OnPostUnflagged },
22169
+ { fn: onPostApproved, action: EnumPostActions.OnPostApproved },
22170
+ { fn: onPostDeclined, action: EnumPostActions.OnPostDeclined },
22171
+ { fn: onPostReactionAdded, action: EnumPostActions.OnPostReactionAdded },
22172
+ { fn: onPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved },
22173
+ {
22174
+ fn: convertEventPayload((callback) => {
22175
+ return onCommentCreated(async (comment) => {
22176
+ var _a;
22177
+ const currentCollection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22178
+ if (!currentCollection || currentCollection.data.includes(comment.referenceId))
22179
+ return;
22180
+ await getPost(comment.referenceId);
22181
+ callback(comment);
22182
+ });
22183
+ }, 'referenceId', 'post'),
22184
+ action: EnumPostActions.OnPostUpdated,
22185
+ },
22186
+ {
22187
+ fn: convertEventPayload((callback) => {
22188
+ return onCommentDeleted(async (comment) => {
22189
+ var _a;
22190
+ const currentCollection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22191
+ if (!currentCollection || currentCollection.data.includes(comment.referenceId))
22192
+ return;
22193
+ await getPost(comment.referenceId);
22194
+ callback(comment);
22195
+ });
22196
+ }, 'referenceId', 'post'),
22197
+ action: EnumPostActions.OnPostUpdated,
22198
+ },
22199
+ ]);
22200
+ }
22201
+ notifyChange({ origin, loading, error }) {
22202
+ var _a, _b;
22203
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22204
+ if (!collection)
22205
+ return;
22206
+ const data = this.applyFilter((_b = collection.data
22207
+ .map(id => pullFromCache(['post', 'get', id]))
22208
+ .filter(isNonNullable)
22209
+ .map(({ data }) => data)) !== null && _b !== void 0 ? _b : []).map(LinkedObject.post);
22210
+ if (!this.shouldNotify(data) && origin === 'event')
22211
+ return;
22212
+ this.callback({
22213
+ onNextPage: () => this.loadPage({ direction: "next" /* Amity.LiveCollectionPageDirection.NEXT */ }),
22214
+ data,
22215
+ hasNextPage: !!this.paginationController.getNextToken(),
22216
+ loading,
22217
+ error,
22218
+ });
22219
+ }
22220
+ applyFilter(data) {
22221
+ var _a;
22222
+ let posts = data;
22223
+ if (!this.query.includeDeleted) {
22224
+ posts = filterByPropEquality(posts, 'isDeleted', false);
22225
+ }
22226
+ if (this.query.tags) {
22227
+ posts = posts.filter(p => { var _a; return (_a = p.tags) === null || _a === void 0 ? void 0 : _a.some(t => { var _a; return (_a = this.query.tags) === null || _a === void 0 ? void 0 : _a.includes(t); }); });
22228
+ }
22229
+ if (this.query.targetType === 'community' && this.query.feedType) {
22230
+ posts = filterByFeedType(posts, this.query.feedType);
22231
+ }
22232
+ if ((_a = this.query.dataTypes) === null || _a === void 0 ? void 0 : _a.length) {
22233
+ posts = filterByPostDataTypes(posts, this.query.dataTypes);
22234
+ }
22235
+ switch (this.query.sortBy) {
22236
+ case 'firstCreated':
22237
+ posts = posts.sort(sortByFirstCreated);
22238
+ break;
22239
+ case 'lastCreated':
22240
+ default:
22241
+ posts = posts.sort(sortByLastCreated);
22242
+ break;
22243
+ }
22244
+ return posts;
22245
+ }
22246
+ }
22247
+
22032
22248
  /* begin_public_function
22033
22249
  id: post.query
22034
22250
  */
@@ -22060,121 +22276,9 @@ const getPosts = (params, callback, config) => {
22060
22276
  }
22061
22277
  const timestamp = Date.now();
22062
22278
  log(`getPosts(tmpid: ${timestamp}) > listen`);
22063
- const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
22064
- const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
22065
- const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
22066
- const disposers = [];
22067
- const cacheKey = [
22068
- 'post',
22069
- 'collection',
22070
- { targetId: params.targetId, targetType: params.targetType },
22071
- ];
22072
- const responder = (data) => {
22073
- var _a, _b, _c;
22074
- let posts = (_a = data.data
22075
- .map(postId => pullFromCache(['post', 'get', postId]))
22076
- .filter(Boolean)
22077
- .map(({ data }) => LinkedObject.post(data))) !== null && _a !== void 0 ? _a : [];
22078
- const sortBy = params.sortBy ? params.sortBy : 'lastCreated';
22079
- posts = posts.sort(sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
22080
- if (!params.includeDeleted) {
22081
- posts = filterByPropEquality(posts, 'isDeleted', false);
22082
- }
22083
- if (params.tags) {
22084
- posts = posts.filter(p => { var _a; return (_a = p.tags) === null || _a === void 0 ? void 0 : _a.some(t => { var _a; return (_a = params.tags) === null || _a === void 0 ? void 0 : _a.includes(t); }); });
22085
- }
22086
- if (params.targetType === 'community' && params.feedType) {
22087
- posts = filterByFeedType(posts, params.feedType);
22088
- }
22089
- if ((_b = params.dataTypes) === null || _b === void 0 ? void 0 : _b.length) {
22090
- posts = filterByPostDataTypes(posts, params.dataTypes);
22091
- }
22092
- callback({
22093
- onNextPage: onFetch,
22094
- data: posts,
22095
- hasNextPage: !!((_c = data.params) === null || _c === void 0 ? void 0 : _c.page),
22096
- loading: data.loading,
22097
- error: data.error,
22098
- });
22099
- };
22100
- const realtimeRouter = (action) => (post) => {
22101
- var _a, _b;
22102
- const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22103
- if (!collection)
22104
- return;
22105
- if (post.parentPostId && post.isDeleted) {
22106
- const parentPost = (_b = pullFromCache([
22107
- 'post',
22108
- 'get',
22109
- post.parentPostId,
22110
- ])) === null || _b === void 0 ? void 0 : _b.data;
22111
- if (!parentPost || (parentPost === null || parentPost === void 0 ? void 0 : parentPost.targetId) !== params.targetId)
22112
- return;
22113
- parentPost.children = parentPost.children.filter(childId => childId !== post.postId);
22114
- pushToCache(['post', 'get', parentPost.postId], parentPost);
22115
- }
22116
- else {
22117
- if (params.targetId !== post.targetId)
22118
- return;
22119
- if (params.targetType !== post.targetType)
22120
- return;
22121
- }
22122
- /*
22123
- * This is not ideal, but currently this is the only way to update the
22124
- * collection on post declined
22125
- */
22126
- if (action === 'onDeclined') {
22127
- collection.data = collection.data.filter(postId => postId !== post.postId);
22128
- }
22129
- else {
22130
- collection.data = [...new Set([post.postId, ...collection.data])];
22131
- }
22132
- pushToCache(cacheKey, collection);
22133
- responder(collection);
22134
- };
22135
- const onFetch = (initial = false) => {
22136
- var _a, _b, _c;
22137
- const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22138
- const posts = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
22139
- if (!initial && posts.length > 0 && !((_c = collection === null || collection === void 0 ? void 0 : collection.params) === null || _c === void 0 ? void 0 : _c.page))
22140
- return;
22141
- const query = createQuery(queryPosts, Object.assign(Object.assign({}, queryParams), { limit: initial ? limit : undefined, page: !initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined }));
22142
- runQuery(query, ({ data: result, error, loading, paging }) => {
22143
- const data = {
22144
- loading,
22145
- error,
22146
- params: { page: paging === null || paging === void 0 ? void 0 : paging.next },
22147
- data: posts,
22148
- };
22149
- if (result) {
22150
- data.data = initial
22151
- ? result.map(getResolver('post'))
22152
- : [...new Set([...posts, ...result.map(getResolver('post'))])];
22153
- }
22154
- pushToCache(cacheKey, data);
22155
- responder(data);
22156
- }, queryOptions(policy, CACHE_SHORTEN_LIFESPAN));
22157
- };
22158
- disposers.push(onPostCreated(realtimeRouter('onCreate')), onPostApproved(realtimeRouter('onApproved')), onPostDeclined(realtimeRouter('onDeclined')), onPostDeleted(realtimeRouter('onDelete')), onPostUpdated(realtimeRouter('onUpdate')), onPostFlagged(realtimeRouter('onFlagged')), onPostUnflagged(realtimeRouter('onUnflagged')), onPostReactionAdded(realtimeRouter('onReactionAdded')), onPostReactionRemoved(realtimeRouter('onReactionRemoved')), convertEventPayload((callback) => {
22159
- return onCommentCreated(async (comment) => {
22160
- var _a;
22161
- const currentCollection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22162
- if (!currentCollection || currentCollection.data.includes(comment.referenceId))
22163
- return;
22164
- await getPost$1(comment.referenceId);
22165
- callback(comment);
22166
- });
22167
- }, 'referenceId', 'post')(realtimeRouter('onUpdate')), convertEventPayload((callback) => {
22168
- return onCommentDeleted(async (comment) => {
22169
- var _a;
22170
- const currentCollection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22171
- if (!currentCollection || currentCollection.data.includes(comment.referenceId))
22172
- return;
22173
- await getPost$1(comment.referenceId);
22174
- callback(comment);
22175
- });
22176
- }, 'referenceId', 'post')(realtimeRouter('onUpdate')));
22177
- onFetch(true);
22279
+ const postsLiveCollection = new PostLiveCollectionController(params, callback);
22280
+ const disposers = postsLiveCollection.startSubscription();
22281
+ const cacheKey = postsLiveCollection.getCacheKey();
22178
22282
  disposers.push(() => dropFromCache(cacheKey));
22179
22283
  return () => {
22180
22284
  log(`getPosts(tmpid: ${timestamp}) > dispose`);
@@ -22208,7 +22312,7 @@ var index$5 = /*#__PURE__*/Object.freeze({
22208
22312
  onPostReactionRemoved: onPostReactionRemoved,
22209
22313
  observePosts: observePosts,
22210
22314
  observePost: observePost,
22211
- getPost: getPost,
22315
+ getPost: getPost$1,
22212
22316
  getPosts: getPosts
22213
22317
  });
22214
22318