@amityco/ts-sdk 7.6.1-bba8c29.0 → 7.6.1-ce05a481.0

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 (102) 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/@types/domains/content.d.ts +8 -2
  4. package/dist/@types/domains/content.d.ts.map +1 -1
  5. package/dist/@types/domains/feed.d.ts +6 -1
  6. package/dist/@types/domains/feed.d.ts.map +1 -1
  7. package/dist/@types/domains/file.d.ts +3 -1
  8. package/dist/@types/domains/file.d.ts.map +1 -1
  9. package/dist/@types/domains/post.d.ts +3 -0
  10. package/dist/@types/domains/post.d.ts.map +1 -1
  11. package/dist/communityRepository/api/getCommunity.d.ts +1 -1
  12. package/dist/communityRepository/api/getCommunity.d.ts.map +1 -1
  13. package/dist/core/model/index.d.ts.map +1 -1
  14. package/dist/feedRepository/api/getCustomRankingGlobalFeed.d.ts +1 -1
  15. package/dist/feedRepository/api/getCustomRankingGlobalFeed.d.ts.map +1 -1
  16. package/dist/feedRepository/api/queryGlobalFeed.d.ts.map +1 -1
  17. package/dist/feedRepository/index.d.ts +1 -1
  18. package/dist/feedRepository/index.d.ts.map +1 -1
  19. package/dist/feedRepository/observers/getCustomRankingGlobalFeed/LiveCollectionController.d.ts +13 -0
  20. package/dist/feedRepository/observers/getCustomRankingGlobalFeed/LiveCollectionController.d.ts.map +1 -0
  21. package/dist/feedRepository/observers/getCustomRankingGlobalFeed/PaginationController.d.ts +5 -0
  22. package/dist/feedRepository/observers/getCustomRankingGlobalFeed/PaginationController.d.ts.map +1 -0
  23. package/dist/feedRepository/observers/getCustomRankingGlobalFeed/QueryStreamController.d.ts +15 -0
  24. package/dist/feedRepository/observers/getCustomRankingGlobalFeed/QueryStreamController.d.ts.map +1 -0
  25. package/dist/feedRepository/observers/getCustomRankingGlobalFeed.d.ts +23 -0
  26. package/dist/feedRepository/observers/getCustomRankingGlobalFeed.d.ts.map +1 -0
  27. package/dist/feedRepository/observers/getGlobalFeed/LiveCollectionController.d.ts +13 -0
  28. package/dist/feedRepository/observers/getGlobalFeed/LiveCollectionController.d.ts.map +1 -0
  29. package/dist/feedRepository/observers/getGlobalFeed/PaginationController.d.ts +5 -0
  30. package/dist/feedRepository/observers/getGlobalFeed/PaginationController.d.ts.map +1 -0
  31. package/dist/feedRepository/observers/getGlobalFeed/QueryStreamController.d.ts +15 -0
  32. package/dist/feedRepository/observers/getGlobalFeed/QueryStreamController.d.ts.map +1 -0
  33. package/dist/feedRepository/observers/getGlobalFeed.d.ts +22 -0
  34. package/dist/feedRepository/observers/getGlobalFeed.d.ts.map +1 -0
  35. package/dist/feedRepository/observers/index.d.ts +3 -0
  36. package/dist/feedRepository/observers/index.d.ts.map +1 -0
  37. package/dist/feedRepository/observers/utils.d.ts +6 -0
  38. package/dist/feedRepository/observers/utils.d.ts.map +1 -0
  39. package/dist/fileRepository/api/index.d.ts +1 -0
  40. package/dist/fileRepository/api/index.d.ts.map +1 -1
  41. package/dist/fileRepository/api/uploadClip.d.ts +17 -0
  42. package/dist/fileRepository/api/uploadClip.d.ts.map +1 -0
  43. package/dist/index.cjs.js +1565 -1195
  44. package/dist/index.esm.js +1657 -1287
  45. package/dist/index.umd.js +2 -2
  46. package/dist/messagePreview/utils/getSubChannelMessagePreviewWithUser.d.ts +1 -1
  47. package/dist/postRepository/api/createClipPost.d.ts +32 -0
  48. package/dist/postRepository/api/createClipPost.d.ts.map +1 -0
  49. package/dist/postRepository/api/index.d.ts +1 -0
  50. package/dist/postRepository/api/index.d.ts.map +1 -1
  51. package/dist/postRepository/events/utils.d.ts.map +1 -1
  52. package/dist/postRepository/observers/getGlobalPinnedPosts/GlobalPinnedPostLiveCollectionController.d.ts.map +1 -1
  53. package/dist/postRepository/observers/getPinnedPosts/PinnedPostLiveCollectionController.d.ts.map +1 -1
  54. package/dist/postRepository/observers/getPinnedPosts/PinnedPostQueryStreamController.d.ts +6 -0
  55. package/dist/postRepository/observers/getPinnedPosts/PinnedPostQueryStreamController.d.ts.map +1 -1
  56. package/dist/postRepository/observers/getPosts/PostLiveCollectionController.d.ts.map +1 -1
  57. package/dist/postRepository/observers/utils.d.ts +13 -0
  58. package/dist/postRepository/observers/utils.d.ts.map +1 -0
  59. package/dist/reactionRepository/api/addReaction.d.ts.map +1 -1
  60. package/dist/reactionRepository/api/removeReaction.d.ts.map +1 -1
  61. package/dist/utils/linkedObject/postLinkedObject.d.ts.map +1 -1
  62. package/dist/utils/postTypePredicate.d.ts +1 -0
  63. package/dist/utils/postTypePredicate.d.ts.map +1 -1
  64. package/dist/utils/tests/dummy/comment.d.ts +1 -1
  65. package/package.json +1 -1
  66. package/src/@types/core/payload.ts +1 -1
  67. package/src/@types/domains/content.ts +10 -0
  68. package/src/@types/domains/feed.ts +18 -1
  69. package/src/@types/domains/file.ts +5 -0
  70. package/src/@types/domains/post.ts +4 -0
  71. package/src/commentRepository/api/deleteComment.ts +6 -6
  72. package/src/communityRepository/api/getCommunity.ts +0 -9
  73. package/src/core/model/index.ts +2 -1
  74. package/src/feedRepository/api/getCustomRankingGlobalFeed.ts +1 -1
  75. package/src/feedRepository/api/queryGlobalFeed.ts +3 -1
  76. package/src/feedRepository/index.ts +1 -1
  77. package/src/feedRepository/observers/getCustomRankingGlobalFeed/LiveCollectionController.ts +95 -0
  78. package/src/feedRepository/observers/getCustomRankingGlobalFeed/PaginationController.ts +25 -0
  79. package/src/feedRepository/observers/getCustomRankingGlobalFeed/QueryStreamController.ts +101 -0
  80. package/src/feedRepository/observers/getCustomRankingGlobalFeed.ts +57 -0
  81. package/src/feedRepository/observers/getGlobalFeed/LiveCollectionController.ts +91 -0
  82. package/src/feedRepository/observers/getGlobalFeed/PaginationController.ts +23 -0
  83. package/src/feedRepository/observers/getGlobalFeed/QueryStreamController.ts +99 -0
  84. package/src/feedRepository/observers/getGlobalFeed.ts +56 -0
  85. package/src/feedRepository/observers/index.ts +2 -0
  86. package/src/feedRepository/observers/utils.ts +85 -0
  87. package/src/fileRepository/api/index.ts +1 -0
  88. package/src/fileRepository/api/uploadClip.ts +71 -0
  89. package/src/postRepository/api/createClipPost.ts +62 -0
  90. package/src/postRepository/api/index.ts +2 -0
  91. package/src/postRepository/api/queryPosts.ts +1 -1
  92. package/src/postRepository/events/utils.ts +35 -1
  93. package/src/postRepository/observers/getGlobalPinnedPosts/GlobalPinnedPostLiveCollectionController.ts +5 -0
  94. package/src/postRepository/observers/getPinnedPosts/PinnedPostLiveCollectionController.ts +7 -1
  95. package/src/postRepository/observers/getPinnedPosts/PinnedPostQueryStreamController.ts +27 -0
  96. package/src/postRepository/observers/getPosts/PostLiveCollectionController.ts +2 -74
  97. package/src/postRepository/observers/getPosts/PostPaginationController.ts +1 -1
  98. package/src/postRepository/observers/utils.ts +97 -0
  99. package/src/reactionRepository/api/addReaction.ts +1 -0
  100. package/src/reactionRepository/api/removeReaction.ts +1 -0
  101. package/src/utils/linkedObject/postLinkedObject.ts +15 -5
  102. package/src/utils/postTypePredicate.ts +9 -0
package/dist/index.cjs.js CHANGED
@@ -35,6 +35,7 @@ const FileType = Object.freeze({
35
35
  FILE: 'file',
36
36
  IMAGE: 'image',
37
37
  VIDEO: 'video',
38
+ CLIP: 'clip',
38
39
  });
39
40
  const VideoResolution = Object.freeze({
40
41
  '1080P': '1080p',
@@ -118,6 +119,7 @@ const PostContentType = Object.freeze({
118
119
  VIDEO: 'video',
119
120
  LIVESTREAM: 'liveStream',
120
121
  POLL: 'poll',
122
+ CLIP: 'clip',
121
123
  });
122
124
 
123
125
  exports.InvitationTypeEnum = void 0;
@@ -656,7 +658,8 @@ const PAYLOAD2MODEL = {
656
658
  };
657
659
  /** hidden */
658
660
  const isOutdated = (prevData, nextData) => {
659
- if ('updatedAt' in nextData && 'updatedAt' in nextData) {
661
+ // Check if the new value is outdated.
662
+ if ('updatedAt' in nextData && 'updatedAt' in prevData) {
660
663
  return new Date(nextData.updatedAt) < new Date(prevData.updatedAt);
661
664
  }
662
665
  return false;
@@ -6693,15 +6696,23 @@ function isAmityVideoPost(post) {
6693
6696
  'videoFileId' in post.data &&
6694
6697
  'thumbnailFileId' in post.data &&
6695
6698
  post.dataType === 'video');
6699
+ }
6700
+ function isAmityClipPost(post) {
6701
+ return !!(post.data &&
6702
+ typeof post.data !== 'string' &&
6703
+ 'fileId' in post.data &&
6704
+ post.dataType === 'clip');
6696
6705
  }
6697
6706
 
6698
6707
  const postLinkedObject = (post) => {
6699
- return Object.assign(Object.assign({}, post), { analytics: {
6708
+ return shallowClone(post, {
6709
+ analytics: {
6700
6710
  markAsViewed: () => {
6701
6711
  const analyticsEngineInstance = AnalyticsEngine$1.getInstance();
6702
6712
  analyticsEngineInstance.markPostAsViewed(post.postId);
6703
6713
  },
6704
- }, get latestComments() {
6714
+ },
6715
+ get latestComments() {
6705
6716
  if (!post.comments)
6706
6717
  return [];
6707
6718
  return (post.comments
@@ -6747,7 +6758,14 @@ const postLinkedObject = (post) => {
6747
6758
  return isAmityFilePost(post)
6748
6759
  ? (_b = pullFromCache(['file', 'get', (_a = post === null || post === void 0 ? void 0 : post.data) === null || _a === void 0 ? void 0 : _a.fileId])) === null || _b === void 0 ? void 0 : _b.data
6749
6760
  : undefined;
6750
- } });
6761
+ },
6762
+ getClipInfo() {
6763
+ var _a, _b;
6764
+ return isAmityClipPost(post)
6765
+ ? (_b = pullFromCache(['file', 'get', (_a = post === null || post === void 0 ? void 0 : post.data) === null || _a === void 0 ? void 0 : _a.fileId])) === null || _b === void 0 ? void 0 : _b.data
6766
+ : undefined;
6767
+ },
6768
+ });
6751
6769
  };
6752
6770
 
6753
6771
  const getCachedMarker = (message) => {
@@ -13915,6 +13933,57 @@ const updateAltText = async (fileId, altText) => {
13915
13933
  };
13916
13934
  /* end_public_function */
13917
13935
 
13936
+ /* begin_public_function
13937
+ id: file.upload.clip
13938
+ */
13939
+ /**
13940
+ * ```js
13941
+ * import { FileRepository } from '@amityco/ts-sdk'
13942
+ * const created = await FileRepository.uploadClip(formData)
13943
+ * ```
13944
+ *
13945
+ * Creates an {@link Amity.File}
13946
+ *
13947
+ * @param formData The data necessary to create a new {@link Amity.File}
13948
+ * @param onProgress The callback to track the upload progress
13949
+ * @returns The newly created {@link Amity.File}
13950
+ *
13951
+ * @category File API
13952
+ * @async
13953
+ */
13954
+ const uploadClip = async (formData, feedType, onProgress) => {
13955
+ const client = getActiveClient();
13956
+ client.log('file/uploadClip', formData);
13957
+ const file = formData.get('files');
13958
+ if (!file)
13959
+ throw new Error('The formData object must have a `files` key.');
13960
+ const accessType = GlobalFileAccessType$1.getInstance().getFileAccessType();
13961
+ formData.append('accessType', accessType);
13962
+ formData.append('preferredFilename', file.name);
13963
+ if (feedType) {
13964
+ formData.append('feedType', feedType);
13965
+ }
13966
+ const headers = 'getHeaders' in formData
13967
+ ? formData.getHeaders()
13968
+ : { 'content-type': 'multipart/form-data' };
13969
+ const { data } = await client.upload.post('/api/v4/clips', formData, {
13970
+ headers,
13971
+ onUploadProgress({ loaded, total = 100 }) {
13972
+ onProgress && onProgress(Math.round((loaded * 100) / total));
13973
+ },
13974
+ });
13975
+ // API-FIX: payload should be serialized properly
13976
+ // const { files } = data
13977
+ const cachedAt = client.cache && Date.now();
13978
+ if (client.cache)
13979
+ ingestInCache({ files: data }, { cachedAt });
13980
+ return {
13981
+ data,
13982
+ cachedAt,
13983
+ };
13984
+ };
13985
+ /* end_public_function */
13986
+
13918
13987
  var index$k = /*#__PURE__*/Object.freeze({
13919
13988
  __proto__: null,
13920
13989
  getFile: getFile,
@@ -13923,7 +13992,8 @@ var index$k = /*#__PURE__*/Object.freeze({
13923
13992
  fileUrlWithSize: fileUrlWithSize,
13924
13993
  uploadVideo: uploadVideo,
13925
13994
  uploadImage: uploadImage,
13926
- updateAltText: updateAltText
13995
+ updateAltText: updateAltText,
13996
+ uploadClip: uploadClip
13927
13997
  });
13928
13998
 
13929
13999
  /**
@@ -14377,7 +14447,7 @@ const addReaction = async (referenceType, referenceId, reactionName) => {
14377
14447
  ]);
14378
14448
  if (!model || ((_a = model.data.myReactions) === null || _a === void 0 ? void 0 : _a.includes(reactionName)))
14379
14449
  return true;
14380
- const updatedModel = Object.assign(Object.assign({}, model.data), { reactionsCount: model.data.reactionsCount + 1, myReactions: [...((_b = model.data.myReactions) !== null && _b !== void 0 ? _b : []), reactionName], reactions: Object.assign(Object.assign({}, model.data.reactions), { [reactionName]: ((_c = model.data.reactions[reactionName]) !== null && _c !== void 0 ? _c : 0) + 1 }) });
14450
+ const updatedModel = Object.assign(Object.assign({}, model.data), { reactionsCount: model.data.reactionsCount + 1, myReactions: [...((_b = model.data.myReactions) !== null && _b !== void 0 ? _b : []), reactionName], reactions: Object.assign(Object.assign({}, model.data.reactions), { [reactionName]: ((_c = model.data.reactions[reactionName]) !== null && _c !== void 0 ? _c : 0) + 1 }), updatedAt: new Date().toISOString() });
14381
14451
  if (referenceType === 'comment') {
14382
14452
  fireEvent('local.comment.addReaction', {
14383
14453
  comment: updatedModel,
@@ -14500,7 +14570,7 @@ const removeReaction = async (referenceType, referenceId, reactionName) => {
14500
14570
  ]);
14501
14571
  if (!model)
14502
14572
  return true;
14503
- const updatedModel = Object.assign(Object.assign({}, model.data), { reactionsCount: Math.max(0, model.data.reactionsCount - 1), myReactions: ((_a = model.data.myReactions) !== null && _a !== void 0 ? _a : []).filter(item => item !== reactionName), reactions: Object.assign(Object.assign({}, model.data.reactions), { [reactionName]: Math.max(0, ((_b = model.data.reactions[reactionName]) !== null && _b !== void 0 ? _b : 0) - 1) }) });
14573
+ const updatedModel = Object.assign(Object.assign({}, model.data), { reactionsCount: Math.max(0, model.data.reactionsCount - 1), myReactions: ((_a = model.data.myReactions) !== null && _a !== void 0 ? _a : []).filter(item => item !== reactionName), reactions: Object.assign(Object.assign({}, model.data.reactions), { [reactionName]: Math.max(0, ((_b = model.data.reactions[reactionName]) !== null && _b !== void 0 ? _b : 0) - 1) }), updatedAt: new Date().toISOString() });
14504
14574
  if (referenceType === 'comment') {
14505
14575
  fireEvent('local.comment.removeReaction', {
14506
14576
  comment: updatedModel,
@@ -14602,6 +14672,26 @@ const prepareSemanticSearchPostPayload = (_a) => {
14602
14672
  return Object.assign(Object.assign({}, processedPostPayload), { polls });
14603
14673
  };
14604
14674
 
14675
+ const processDeleteChildPost = (payload) => {
14676
+ var _a;
14677
+ const post = payload.posts[0];
14678
+ if (!post.parentId)
14679
+ return;
14680
+ const parentPost = (_a = pullFromCache(['post', 'get', post.parentPostId])) === null || _a === void 0 ? void 0 : _a.data;
14681
+ if (!parentPost)
14682
+ return;
14683
+ pushToCache(['post', 'get', parentPost.postId], Object.assign(Object.assign({}, parentPost), { children: parentPost.children.filter(childId => childId !== post.postId) }));
14684
+ };
14685
+ const processCreateChildPost = (payload) => {
14686
+ var _a;
14687
+ const post = payload.posts[0];
14688
+ if (!post.parentId)
14689
+ return;
14690
+ const parentPost = (_a = pullFromCache(['post', 'get', post.parentPostId])) === null || _a === void 0 ? void 0 : _a.data;
14691
+ if (!parentPost)
14692
+ return;
14693
+ pushToCache(['post', 'get', parentPost.postId], Object.assign(Object.assign({}, parentPost), { children: [...new Set([post.postId, ...parentPost.children])] }));
14694
+ };
14605
14695
  const createPostEventSubscriber = (event, callback) => {
14606
14696
  const client = getActiveClient();
14607
14697
  const filter = (payload) => {
@@ -14626,6 +14716,7 @@ const createPostEventSubscriber = (event, callback) => {
14626
14716
  }
14627
14717
  if (event === 'post.deleted') {
14628
14718
  const { postId, postedUserId } = payload.posts[0];
14719
+ processDeleteChildPost(payload);
14629
14720
  try {
14630
14721
  isInTombstone('post', postId);
14631
14722
  }
@@ -14652,6 +14743,8 @@ const createPostEventSubscriber = (event, callback) => {
14652
14743
  }
14653
14744
  queries === null || queries === void 0 ? void 0 : queries.map(({ key, data }) => upsertInCache(key, data, { cachedAt: -1 }));
14654
14745
  }
14746
+ if (event === 'post.created')
14747
+ processCreateChildPost(payload);
14655
14748
  callback(post.data);
14656
14749
  }
14657
14750
  };
@@ -14678,6 +14771,8 @@ const createLocalPostEventSubscriber = (event, callback) => {
14678
14771
  });
14679
14772
  }
14680
14773
  const post = pullFromCache(['post', 'get', payload.posts[0].postId]);
14774
+ if (event === 'local.post.deleted')
14775
+ processDeleteChildPost(payload);
14681
14776
  callback(post.data);
14682
14777
  }
14683
14778
  };
@@ -19791,16 +19886,11 @@ const updateCommunity = async (communityId, patch) => {
19791
19886
  * @category Community API
19792
19887
  * @async
19793
19888
  */
19794
- const getCommunity$1 = async (communityId, type, includeDiscoverablePrivateCommunity) => {
19889
+ const getCommunity$1 = async (communityId) => {
19795
19890
  const client = getActiveClient();
19796
19891
  client.log('community/getCommunity', communityId);
19797
19892
  // API-FIX: endpoint should not be /list, parameters should be querystring.
19798
- const { data: payload } = await client.http.get(`/api/v3/communities/${communityId}`, {
19799
- params: {
19800
- type: 'communityJoinRequest',
19801
- includeDiscoverablePrivateCommunity: includeDiscoverablePrivateCommunity !== null && includeDiscoverablePrivateCommunity !== void 0 ? includeDiscoverablePrivateCommunity : true,
19802
- },
19803
- });
19893
+ const { data: payload } = await client.http.get(`/api/v3/communities/${communityId}`);
19804
19894
  const data = prepareCommunityPayload(payload);
19805
19895
  const cachedAt = client.cache && Date.now();
19806
19896
  if (client.cache) {
@@ -22504,1249 +22594,720 @@ var index$a = /*#__PURE__*/Object.freeze({
22504
22594
  getCategories: getCategories
22505
22595
  });
22506
22596
 
22597
+ class CustomRankingGlobalFeedPaginationController extends PaginationController {
22598
+ async getRequest(queryParams, token) {
22599
+ const { limit } = queryParams, params = __rest(queryParams, ["limit"]);
22600
+ const options = token ? { token } : { limit };
22601
+ const { data: queryResponse } = await this.http.get(`/api/v5/me/global-feeds`, {
22602
+ params: Object.assign(Object.assign({}, params), { options }),
22603
+ });
22604
+ return queryResponse;
22605
+ }
22606
+ }
22607
+
22608
+ var EnumPostActions;
22609
+ (function (EnumPostActions) {
22610
+ EnumPostActions["OnPostCreated"] = "onPostCreated";
22611
+ EnumPostActions["OnPostUpdated"] = "onPostUpdated";
22612
+ EnumPostActions["OnPostDeleted"] = "onPostDeleted";
22613
+ EnumPostActions["OnPostFlagged"] = "onPostFlagged";
22614
+ EnumPostActions["OnPostUnflagged"] = "onPostUnflagged";
22615
+ EnumPostActions["OnPostReactionAdded"] = "onPostReactionAdded";
22616
+ EnumPostActions["OnPostReactionRemoved"] = "onPostReactionRemoved";
22617
+ EnumPostActions["OnPostApproved"] = "onPostApproved";
22618
+ EnumPostActions["OnPostDeclined"] = "onPostDeclined";
22619
+ })(EnumPostActions || (EnumPostActions = {}));
22620
+
22621
+ class CustomRankingGlobalFeedQueryStreamController extends QueryStreamController {
22622
+ constructor(query, cacheKey, notifyChange, preparePayload) {
22623
+ super(query, cacheKey);
22624
+ this.notifyChange = notifyChange;
22625
+ this.preparePayload = preparePayload;
22626
+ }
22627
+ async saveToMainDB(response) {
22628
+ const processedPayload = await this.preparePayload(response);
22629
+ const client = getActiveClient();
22630
+ const cachedAt = client.cache && Date.now();
22631
+ if (client.cache) {
22632
+ ingestInCache(processedPayload, { cachedAt });
22633
+ }
22634
+ }
22635
+ appendToQueryStream(response, direction, refresh = false) {
22636
+ var _a, _b;
22637
+ if (refresh) {
22638
+ pushToCache(this.cacheKey, {
22639
+ data: response.posts.map(getResolver('post')),
22640
+ });
22641
+ }
22642
+ else {
22643
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22644
+ const posts = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
22645
+ pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: [...new Set([...posts, ...response.posts.map(getResolver('post'))])] }));
22646
+ }
22647
+ }
22648
+ reactor(action) {
22649
+ return (post) => {
22650
+ var _a;
22651
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
22652
+ if (!collection)
22653
+ return;
22654
+ if (post.parentPostId && !collection.data.includes(post.parentPostId))
22655
+ return;
22656
+ // Delete Action
22657
+ if (action === EnumPostActions.OnPostDeleted) {
22658
+ // Parent Post - Remove from collection
22659
+ collection.data = collection.data.filter(postId => postId !== post.postId);
22660
+ }
22661
+ if (action === EnumPostActions.OnPostCreated || action === EnumPostActions.OnPostApproved) {
22662
+ // If the query has dataTypes array with value, check if post.dataType is include in the array
22663
+ if (this.query.dataTypes && this.query.dataTypes.length > 0) {
22664
+ if (!this.query.dataTypes.includes(post.dataType))
22665
+ return;
22666
+ }
22667
+ collection.data = [...new Set([post.postId, ...collection.data])];
22668
+ }
22669
+ if (action === EnumPostActions.OnPostDeclined) {
22670
+ collection.data = collection.data.filter(postId => postId !== post.postId);
22671
+ }
22672
+ pushToCache(this.cacheKey, collection);
22673
+ this.notifyChange({ origin: "event" /* Amity.LiveDataOrigin.EVENT */, loading: false });
22674
+ };
22675
+ }
22676
+ subscribeRTE(createSubscriber) {
22677
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
22678
+ }
22679
+ }
22680
+
22507
22681
  /* begin_public_function
22508
- id: feed.query.global_feed
22682
+ id: comment.get_by_ids
22509
22683
  */
22510
22684
  /**
22511
22685
  * ```js
22512
- * import { queryGlobalFeed } from '@amityco/ts-sdk'
22513
- * const posts = await queryGlobalFeed()
22686
+ * import { CommentRepository } from '@amityco/ts-sdk'
22687
+ * const comments = await CommentRepository.getCommentByIds(['foo', 'bar'])
22514
22688
  * ```
22515
22689
  *
22516
- * Queries a paginable list of {@link Amity.Post} objects
22690
+ * Fetches a collection of {@link Amity.Comment} objects
22517
22691
  *
22518
- * @param query The query parameters
22519
- * @returns A page of {@link Amity.Post} objects
22692
+ * @param commentIds the IDs of the {@link Amity.Comment} to fetch
22693
+ * @returns the associated collection of {@link Amity.Comment} objects
22520
22694
  *
22521
- * @category Feed API
22695
+ * @category Comment API
22522
22696
  * @async
22523
- * */
22524
- const queryGlobalFeed = async (query) => {
22697
+ */
22698
+ const getCommentByIds = async (commentIds) => {
22525
22699
  const client = getActiveClient();
22526
- client.log('feed/queryGlobalFeed', query);
22527
- const _a = query !== null && query !== void 0 ? query : {}, { queryToken } = _a, params = __rest(_a, ["queryToken"]);
22528
- const options = (() => {
22529
- if (queryToken)
22530
- return { token: queryToken };
22531
- return undefined;
22532
- })();
22533
- const { data: queryPayload } = await client.http.get(`/api/v4/me/global-feeds`, {
22534
- params: Object.assign(Object.assign({}, params), { options }),
22535
- });
22536
- const { paging } = queryPayload, payload = __rest(queryPayload, ["paging"]);
22537
- const data = prepareMembershipPayload(payload, 'communityUsers');
22538
- const { posts } = data;
22539
- const { communities: processedCommunity } = prepareCommunityPayload(data);
22540
- const cachedAt = client.cache && Date.now();
22541
- if (client.cache) {
22542
- ingestInCache(Object.assign(Object.assign({}, data), { communitis: processedCommunity }));
22543
- const cacheKey = ['globalFeed', 'query', Object.assign(Object.assign({}, params), { options })];
22544
- pushToCache(cacheKey, { posts: posts.map(getResolver('post')), paging });
22700
+ client.log('comment/getCommentByIds', commentIds);
22701
+ const encodedCommentIds = commentIds.map(commentId => encodeURIComponent(commentId));
22702
+ let data;
22703
+ try {
22704
+ // API-FIX: endpoint should not be /list, parameters should be querystring.
22705
+ const response = await client.http.get(`/api/v3/comments/list`, {
22706
+ params: { commentIds: encodedCommentIds },
22707
+ });
22708
+ data = response.data;
22709
+ }
22710
+ catch (error) {
22711
+ commentIds.forEach(commentId => {
22712
+ if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
22713
+ pushToTombstone('comment', commentId);
22714
+ }
22715
+ });
22716
+ throw error;
22545
22717
  }
22718
+ const cachedAt = client.cache && Date.now();
22719
+ if (client.cache)
22720
+ ingestInCache(data, { cachedAt });
22546
22721
  return {
22547
- data: posts.map(LinkedObject.post),
22722
+ data: data.comments.map(comment => LinkedObject.comment(comment)),
22548
22723
  cachedAt,
22549
- paging,
22550
22724
  };
22551
22725
  };
22552
22726
  /* end_public_function */
22553
22727
  /**
22554
22728
  * ```js
22555
- * import { queryGlobalFeed } from '@amityco/ts-sdk'
22556
- * const posts = await queryGlobalFeed.locally()
22729
+ * import { getCommentByIds } from '@amityco/ts-sdk'
22730
+ * const comments = getCommentByIds.locally(['foo', 'bar'])
22557
22731
  * ```
22558
22732
  *
22559
- * Queries a paginable list of {@link Amity.Post} objects from cache
22733
+ * Fetches a collection of {@link Amity.Comment} objects from cache
22560
22734
  *
22561
- * @param query The query parameters
22562
- * @returns A page of {@link Amity.Post} objects
22735
+ * @param commentIds the IDs of the {@link Amity.Comment} to fetch
22736
+ * @returns the associated collection of {@link Amity.Comment} objects
22563
22737
  *
22564
- * @category Feed API
22565
- * @async
22566
- * */
22567
- queryGlobalFeed.locally = (query) => {
22568
- var _a, _b;
22738
+ * @category Comment API
22739
+ */
22740
+ getCommentByIds.locally = (commentIds) => {
22741
+ var _a;
22569
22742
  const client = getActiveClient();
22570
- client.log('post/queryGlobalFeed.locally', query);
22743
+ client.log('comment/getCommentByIds.locally', commentIds);
22571
22744
  if (!client.cache)
22572
22745
  return;
22573
- const params = __rest(query !== null && query !== void 0 ? query : {}, []);
22574
- const queryKey = ['globalFeed', 'query', Object.assign({}, params)];
22575
- const { data, cachedAt } = (_a = pullFromCache(queryKey)) !== null && _a !== void 0 ? _a : {};
22576
- if (!(data === null || data === void 0 ? void 0 : data.posts.length))
22746
+ const cached = commentIds
22747
+ .map(commentId => pullFromCache(['comment', 'get', commentId]))
22748
+ .filter(Boolean);
22749
+ const comments = cached.map(({ data }) => data);
22750
+ const oldest = (_a = cached.sort((a, b) => (a.cachedAt < b.cachedAt ? -1 : 1))) === null || _a === void 0 ? void 0 : _a[0];
22751
+ if ((cached === null || cached === void 0 ? void 0 : cached.length) < commentIds.length)
22577
22752
  return;
22578
- const posts = data.posts
22579
- .map(postId => pullFromCache(['post', 'get', postId]))
22580
- .filter(Boolean)
22581
- .map(({ data }) => data)
22582
- .map(LinkedObject.post);
22583
- const { paging } = data;
22584
- return posts.length === ((_b = data === null || data === void 0 ? void 0 : data.posts) === null || _b === void 0 ? void 0 : _b.length)
22585
- ? {
22586
- data: posts,
22587
- cachedAt,
22588
- paging,
22589
- }
22590
- : undefined;
22753
+ return {
22754
+ data: comments.map(comment => LinkedObject.comment(comment)),
22755
+ cachedAt: oldest.cachedAt,
22756
+ };
22591
22757
  };
22592
22758
 
22593
22759
  /* begin_public_function
22594
- id: feed.query.custom_ranking_feed
22760
+ id: comment.create
22595
22761
  */
22596
22762
  /**
22597
22763
  * ```js
22598
- * import { FeedRepository } from '@amityco/ts-sdk'
22599
- * const posts = await FeedRepository.getCustomRankingGlobalFeed()
22764
+ * import { CommentRepository } from '@amityco/ts-sdk'
22765
+ * const newComment = await CommentRepository.createComment(bundle)
22600
22766
  * ```
22601
22767
  *
22602
- * Queries a paginable list of {@link Amity.Post} objects
22768
+ * Creates an {@link Amity.Comment}
22603
22769
  *
22604
- * @param query The query parameters
22605
- * @returns A page of {@link Amity.Post} objects
22770
+ * @param bundle The data necessary to create a new {@link Amity.Comment}
22771
+ * @returns The newly created {@link Amity.Comment}
22606
22772
  *
22607
- * @category Feed API
22773
+ * @category Comment API
22608
22774
  * @async
22609
- * */
22610
- const getCustomRankingGlobalFeed = async (query) => {
22775
+ */
22776
+ const createComment = async (bundle) => {
22777
+ var _a;
22611
22778
  const client = getActiveClient();
22612
- client.log('feed/getCustomRankingGlobalFeed', query);
22613
- const _a = query !== null && query !== void 0 ? query : {}, { queryToken, limit } = _a, params = __rest(_a, ["queryToken", "limit"]);
22614
- const options = (() => {
22615
- if (queryToken)
22616
- return { token: queryToken };
22617
- return undefined;
22618
- })();
22619
- const { data: queryPayload } = await client.http.get(`/api/v5/me/global-feeds`, {
22620
- params: Object.assign(Object.assign({}, params), { limit: !queryToken ? limit : undefined, options }),
22621
- });
22622
- const { paging } = queryPayload, payload = __rest(queryPayload, ["paging"]);
22623
- const data = prepareMembershipPayload(payload, 'communityUsers');
22624
- const { posts } = data;
22779
+ client.log('comment/createComment', bundle);
22780
+ const { data } = await client.http.post('/api/v3/comments', bundle);
22781
+ const { comments } = data;
22782
+ // BE always returns an array of comments If it got record 0 from BE it might have a problem on creation logic
22783
+ if (comments.length === 0)
22784
+ throw new Error('Comment not created');
22625
22785
  const cachedAt = client.cache && Date.now();
22626
- if (client.cache) {
22627
- ingestInCache(data);
22628
- const cacheKey = ['customGlobalFeed', 'query', Object.assign(Object.assign({}, params), { options })];
22629
- pushToCache(cacheKey, { posts: posts.map(getResolver('post')), paging });
22630
- }
22631
- return { data: posts.map(LinkedObject.post), cachedAt, paging };
22632
- };
22633
- /* end_public_function */
22634
- /**
22635
- * ```js
22636
- * import { FeedRepository } from '@amityco/ts-sdk'
22637
- * const posts = await FeedRepository.getCustomRankingGlobalFeed.locally()
22638
- * ```
22639
- *
22640
- * Queries a paginable list of {@link Amity.Post} objects from cache
22641
- *
22642
- * @param query The query parameters
22643
- * @returns A page of {@link Amity.Post} objects
22644
- *
22645
- * @category Feed API
22646
- * @async
22647
- * */
22648
- getCustomRankingGlobalFeed.locally = (query) => {
22649
- var _a, _b;
22650
- const client = getActiveClient();
22651
- client.log('post/getCustomRankingGlobalFeed.locally', query);
22652
- if (!client.cache)
22653
- return;
22654
- const params = __rest(query !== null && query !== void 0 ? query : {}, []);
22655
- const queryKey = ['customGlobalFeed', 'query', Object.assign({}, params)];
22656
- const { data, cachedAt } = (_a = pullFromCache(queryKey)) !== null && _a !== void 0 ? _a : {};
22657
- if (!(data === null || data === void 0 ? void 0 : data.posts.length))
22658
- return;
22659
- const posts = data.posts
22660
- .map(postId => pullFromCache(['post', 'get', postId]))
22661
- .filter(Boolean)
22662
- .map(({ data }) => data);
22663
- const { paging } = data;
22664
- return posts.length === ((_b = data === null || data === void 0 ? void 0 : data.posts) === null || _b === void 0 ? void 0 : _b.length)
22665
- ? { data: posts.map(LinkedObject.post), cachedAt, paging }
22666
- : undefined;
22667
- };
22668
-
22669
- var index$9 = /*#__PURE__*/Object.freeze({
22670
- __proto__: null,
22671
- queryGlobalFeed: queryGlobalFeed,
22672
- getCustomRankingGlobalFeed: getCustomRankingGlobalFeed
22673
- });
22786
+ if (client.cache)
22787
+ ingestInCache(data, { cachedAt });
22788
+ if (['post', 'content'].includes(bundle.referenceType)) {
22789
+ const post = (_a = pullFromCache(['post', 'get', bundle.referenceId])) === null || _a === void 0 ? void 0 : _a.data;
22790
+ if (post) {
22791
+ post.commentsCount += 1;
22792
+ fireEvent('local.post.updated', {
22793
+ posts: [post],
22794
+ categories: [],
22795
+ comments: [],
22796
+ communities: [],
22797
+ communityUsers: data.communityUsers,
22798
+ feeds: [],
22799
+ files: data.files,
22800
+ postChildren: [],
22801
+ users: data.users,
22802
+ videoStreamings: [],
22803
+ });
22804
+ }
22805
+ }
22806
+ else if (bundle.referenceType === 'story') {
22807
+ const storyIndex = pullFromCache([
22808
+ "story-reference" /* STORY_KEY_CACHE.STORY_ID_TO_REFERENCE_ID */,
22809
+ bundle.referenceId,
22810
+ ]);
22811
+ if (storyIndex === null || storyIndex === void 0 ? void 0 : storyIndex.data) {
22812
+ const cacheStory = pullFromCache([
22813
+ "story" /* STORY_KEY_CACHE.STORY */,
22814
+ 'get',
22815
+ storyIndex.data,
22816
+ ]);
22817
+ if (cacheStory === null || cacheStory === void 0 ? void 0 : cacheStory.data) {
22818
+ fireEvent('story.updated', {
22819
+ stories: [
22820
+ Object.assign(Object.assign({}, cacheStory.data), { commentsCount: cacheStory.data.commentsCount + 1, comments: [...new Set([...cacheStory.data.comments, comments[0].commentId])] }),
22821
+ ],
22822
+ categories: [],
22823
+ comments,
22824
+ communities: [],
22825
+ communityUsers: data.communityUsers,
22826
+ files: data.files,
22827
+ users: data.users,
22828
+ });
22829
+ }
22830
+ }
22831
+ }
22832
+ fireEvent('local.comment.created', data);
22833
+ return {
22834
+ data: LinkedObject.comment(comments[0]),
22835
+ cachedAt,
22836
+ };
22837
+ };
22838
+ /* end_public_function */
22674
22839
 
22675
22840
  /* begin_public_function
22676
- id: post.get_by_ids
22841
+ id: comment.update_comment
22677
22842
  */
22678
22843
  /**
22679
22844
  * ```js
22680
- * import { getPostByIds } from '@amityco/ts-sdk'
22681
- * const { data: posts } = await getPostByIds(['foo', 'bar'])
22845
+ * import { CommentRepository } from '@amityco/ts-sdk'
22846
+ * const updated = await CommentRepository.updateComment(commentId, {
22847
+ * data: { text: 'hello world' }
22848
+ * })
22682
22849
  * ```
22683
22850
  *
22684
- * Fetches a collection of {@link Amity.Post} objects
22851
+ * Updates an {@link Amity.Comment}
22685
22852
  *
22686
- * @param postIds the IDs of the {@link Amity.Post} to fetch
22687
- * @returns the associated collection of {@link Amity.Post} objects
22853
+ * @param commentId The ID of the {@link Amity.Comment} to edit
22854
+ * @param patch The patch data to apply
22855
+ * @returns the updated {@link Amity.Comment} object
22688
22856
  *
22689
- * @category Post API
22857
+ * @category Comment API
22690
22858
  * @async
22691
22859
  */
22692
- const getPostByIds = async (postIds) => {
22860
+ const updateComment = async (commentId, patch) => {
22693
22861
  const client = getActiveClient();
22694
- client.log('post/getPostByIds', postIds);
22695
- const encodedPostIds = postIds.map(postId => encodeURIComponent(postId));
22696
- let payload;
22862
+ client.log('user/updateComment', patch);
22863
+ const { data } = await client.http.put(`/api/v3/comments/${encodeURIComponent(commentId)}`, patch);
22864
+ const cachedAt = client.cache && Date.now();
22865
+ if (client.cache)
22866
+ ingestInCache(data, { cachedAt });
22867
+ fireEvent('comment.updated', data);
22868
+ const { comments } = data;
22869
+ return {
22870
+ data: LinkedObject.comment(comments.find(comment => comment.commentId === commentId)),
22871
+ cachedAt,
22872
+ };
22873
+ };
22874
+ /* end_public_function */
22875
+
22876
+ // Due to we have optimistic logic, we will use referenceId as a id in SDK instead of storyId
22877
+ const applyMissingField = (rawData, isCreated = false) => {
22878
+ const { storyId, referenceId } = rawData;
22879
+ if (!isCreated) {
22880
+ if (referenceId)
22881
+ return Object.assign(Object.assign({}, rawData), { syncState: "synced" /* Amity.SyncState.Synced */ });
22882
+ }
22883
+ return Object.assign(Object.assign({}, rawData), { syncState: "synced" /* Amity.SyncState.Synced */, referenceId: storyId });
22884
+ };
22885
+ const convertRawStoryToInternal = (data, isCreated = false) => {
22886
+ const { stories } = data;
22887
+ const storiesData = stories.map(story => applyMissingField(story, isCreated));
22888
+ return Object.assign(Object.assign({}, data), { stories: storiesData });
22889
+ };
22890
+
22891
+ const getStoryByStoryId$1 = async (storyId) => {
22892
+ const client = getActiveClient();
22893
+ client.log('story/getStoryByStoryId', storyId);
22894
+ // Get story referenceId from cache
22895
+ const cacheReferenceId = pullFromCache([
22896
+ "story-reference" /* STORY_KEY_CACHE.STORY_ID_TO_REFERENCE_ID */,
22897
+ storyId,
22898
+ ]);
22899
+ if (cacheReferenceId === null || cacheReferenceId === void 0 ? void 0 : cacheReferenceId.data) {
22900
+ const { data: referenceId } = cacheReferenceId;
22901
+ isInTombstone('story', referenceId);
22902
+ }
22903
+ let data;
22697
22904
  try {
22698
- // API-FIX: endpoint should not be /list, parameters should be querystring.
22699
- const response = await client.http.get(`/api/v3/posts/list`, {
22700
- params: { postIds: encodedPostIds },
22701
- });
22702
- payload = response.data;
22905
+ const response = await client.http.get(`/api/v4/stories/${storyId}`);
22906
+ data = convertRawStoryToInternal(response.data);
22703
22907
  }
22704
22908
  catch (error) {
22705
- postIds.forEach(postId => {
22706
- if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
22707
- pushToTombstone('post', postId);
22708
- }
22709
- });
22909
+ if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
22910
+ pushToTombstone('story', storyId);
22911
+ }
22710
22912
  throw error;
22711
22913
  }
22712
- const data = prepareMembershipPayload(payload, 'communityUsers');
22713
22914
  const cachedAt = client.cache && Date.now();
22714
- if (client.cache)
22915
+ if (client.cache) {
22715
22916
  ingestInCache(data, { cachedAt });
22917
+ }
22716
22918
  return {
22717
- data: data.posts.map(LinkedObject.post),
22919
+ data: data.stories[0],
22718
22920
  cachedAt,
22719
22921
  };
22720
22922
  };
22721
- /* end_public_function */
22722
- /**
22723
- * ```js
22724
- * import { getPostByIds } from '@amityco/ts-sdk'
22725
- * const { data: posts } = getPostByIds.locally(['foo', 'bar'])
22726
- * ```
22727
- *
22728
- * Fetches a collection of {@link Amity.Post} objects from cache
22729
- *
22730
- * @param postIds the IDs of the {@link Amity.Post} to fetch
22731
- * @returns the associated collection of {@link Amity.Post} objects
22732
- *
22733
- * @category Post API
22734
- */
22735
- getPostByIds.locally = (postIds) => {
22736
- var _a;
22923
+ getStoryByStoryId$1.locally = (storyId) => {
22737
22924
  const client = getActiveClient();
22738
- client.log('post/getPostByIds.locally', postIds);
22739
- if (!client.cache)
22740
- return;
22741
- const cached = postIds
22742
- .map(postId => pullFromCache(['post', 'get', postId]))
22743
- .filter(Boolean);
22744
- const posts = cached.map(({ data }) => data);
22745
- const oldest = (_a = cached.sort((a, b) => (a.cachedAt < b.cachedAt ? -1 : 1))) === null || _a === void 0 ? void 0 : _a[0];
22746
- if ((cached === null || cached === void 0 ? void 0 : cached.length) < postIds.length)
22925
+ client.log('story/getStorybyStoryId', storyId);
22926
+ // Get story referenceId from cache
22927
+ const cacheReferenceId = pullFromCache([
22928
+ "story-reference" /* STORY_KEY_CACHE.STORY_ID_TO_REFERENCE_ID */,
22929
+ storyId,
22930
+ ]);
22931
+ if (cacheReferenceId === null || cacheReferenceId === void 0 ? void 0 : cacheReferenceId.data) {
22932
+ const { data: referenceId } = cacheReferenceId;
22933
+ isInTombstone('story', referenceId);
22934
+ }
22935
+ const cachedAt = client.cache && Date.now();
22936
+ const storyCache = pullFromCache(['story', 'get', storyId]);
22937
+ if (!storyCache)
22747
22938
  return;
22748
22939
  return {
22749
- data: posts.map(LinkedObject.post),
22750
- cachedAt: oldest.cachedAt,
22940
+ data: storyCache.data,
22941
+ cachedAt,
22751
22942
  };
22752
22943
  };
22753
22944
 
22754
22945
  /* begin_public_function
22755
- id: post.create.text_post, post.create.image_post, post.create.file_post, post.create.video_post, post.create.poll_post, post.create.livestream_post, post.create.custom_post
22946
+ id: comment.soft_delete, comment.hard_delete
22756
22947
  */
22757
22948
  /**
22758
22949
  * ```js
22759
- * import { PostRepository } from '@amityco/ts-sdk'
22760
- * const created = await PostRepository.createPost({
22761
- * targetType: 'user',
22762
- * targetId: 'foobar',
22763
- * data: { text: 'hello world' }
22764
- * }))
22950
+ * import { CommentRepository } from '@amityco/ts-sdk'
22951
+ * const success = await CommentRepository.deleteComment('foobar')
22765
22952
  * ```
22766
22953
  *
22767
- * Creates an {@link Amity.Post}
22954
+ * Deletes a {@link Amity.Comment}
22768
22955
  *
22769
- * @param bundle The data necessary to create a new {@link Amity.Post}
22770
- * @returns The newly created {@link Amity.Post}
22956
+ * @param commentId The {@link Amity.Comment} ID to delete
22957
+ * @return A success boolean if the {@link Amity.Comment} was deleted
22771
22958
  *
22772
- * @category Post API
22959
+ * @category Comment API
22773
22960
  * @async
22774
22961
  */
22775
- const createPost = async (bundle) => {
22962
+ const deleteComment = async (commentId, permanent = false) => {
22963
+ var _a;
22776
22964
  const client = getActiveClient();
22777
- client.log('post/createPost', bundle);
22778
- if (!bundle.dataType || ['text', 'image', 'file', 'video'].includes(bundle.dataType)) {
22779
- // eslint-disable-next-line no-param-reassign
22780
- delete bundle.dataType;
22965
+ const comment = await getComment$2(commentId);
22966
+ // API-FIX: This endpoint has not been implemented yet.
22967
+ await client.http.delete(`/api/v4/comments/${encodeURIComponent(commentId)}`, {
22968
+ params: {
22969
+ commentId,
22970
+ permanent,
22971
+ },
22972
+ });
22973
+ // to support hard deletion
22974
+ const deleted = Object.assign(Object.assign({}, comment.data), { isDeleted: true });
22975
+ if (permanent) {
22976
+ scheduleTask(() => pushToTombstone('comment', commentId));
22781
22977
  }
22782
- const { data: payload } = await client.http.post('/api/v4/posts', bundle);
22783
- fireEvent('post.created', payload);
22784
- const data = prepareMembershipPayload(payload, 'communityUsers');
22785
- const cachedAt = client.cache && Date.now();
22786
- if (client.cache)
22787
- ingestInCache(data, { cachedAt });
22788
- const { posts } = data;
22789
- return {
22790
- data: LinkedObject.post(posts[0]),
22791
- cachedAt,
22792
- };
22793
- };
22794
- /* end_public_function */
22795
-
22796
- /* begin_public_function
22797
- id: post.edit, post.edit.custom_post
22798
- */
22799
- /**
22800
- * ```js
22801
- * import { PostRepository } from '@amityco/ts-sdk'
22802
- * const updated = await PostRepository.editPost(postId, {
22803
- * data: { text: 'hello world' }
22804
- * })
22805
- * ```
22806
- *
22807
- * Updates an {@link Amity.Post}
22808
- *
22809
- * @param postId The ID of the {@link Amity.Post} to edit
22810
- * @param patch The patch data to apply
22811
- * @returns the updated {@link Amity.Post} object
22812
- *
22813
- * @category Post API
22814
- * @async
22815
- */
22816
- const editPost = async (postId, patch) => {
22817
- const client = getActiveClient();
22818
- client.log('user/editPost', patch);
22819
- const { data: payload } = await client.http.put(`/api/v4/posts/${encodeURIComponent(postId)}`, patch);
22820
- const data = prepareMembershipPayload(payload, 'communityUsers');
22821
- const cachedAt = client.cache && Date.now();
22822
- if (client.cache)
22823
- ingestInCache(data, { cachedAt });
22824
- fireEvent('local.post.updated', data);
22825
- const { posts } = data;
22826
- return {
22827
- data: LinkedObject.post(posts.find(post => post.postId === postId)),
22828
- cachedAt,
22829
- };
22830
- };
22831
- /* end_public_function */
22832
-
22833
- /**
22834
- * ```js
22835
- * import { deletePost } from '@amityco/ts-sdk'
22836
- * const success = await deletePost('foobar')
22837
- * ```
22838
- *
22839
- * Deletes a {@link Amity.Post}
22840
- *
22841
- * @param postId The {@link Amity.Post} ID to delete
22842
- * @return A success boolean if the {@link Amity.Post} was deleted
22843
- *
22844
- * @private
22845
- * @async
22846
- */
22847
- const deletePost = async (postId, permanent = false) => {
22848
- var _a;
22849
- const client = getActiveClient();
22850
- const post = await getPost$2(postId);
22851
- await client.http.delete(`/api/v4/posts/${encodeURIComponent(postId)}`, {
22852
- params: {
22853
- postId,
22854
- permanent,
22855
- },
22856
- });
22857
- // there is currently a limitation which doesn't allow us to fire event to tell that community
22858
- // has been updated. reason is that, when the object is deleted, we don't have its `communityId`
22859
- // and so we cannot refetch the community or advertise on events. hopefully this should be solved
22860
- // later when realtime events covers that for us.
22861
- if (post.data.targetType === 'community') {
22862
- const community = await getCommunity$1(post.data.targetId);
22863
- const communityUsersCache = (_a = queryCache(['communityUsers', 'get'])) !== null && _a !== void 0 ? _a : [];
22864
- const communityUsers = communityUsersCache
22865
- .filter(({ key }) => {
22866
- // cache key is ['communityUsers', 'get', `${communityId}#`${userId}`}]
22867
- if (key[0] !== 'communityUsers')
22868
- return false;
22869
- if (key[1] !== 'get')
22870
- return false;
22871
- if (typeof key[2] === 'string')
22872
- return key[2].includes(community.data.communityId);
22873
- return false;
22874
- })
22875
- .map(({ data }) => data);
22876
- fireEvent('community.updated', {
22877
- communities: [community.data],
22978
+ else {
22979
+ upsertInCache(['comment', 'get', commentId], { isDeleted: true });
22980
+ }
22981
+ if (comment.data.referenceType === 'story') {
22982
+ const story = await getStoryByStoryId$1(comment.data.referenceId);
22983
+ fireEvent('local.story.updated', {
22984
+ stories: [story.data],
22878
22985
  categories: [],
22879
- communityUsers,
22880
- feeds: [],
22986
+ comments: [],
22987
+ communities: [],
22988
+ communityUsers: [],
22881
22989
  files: [],
22882
22990
  users: [],
22883
22991
  });
22884
22992
  }
22885
- // to support hard deletion
22886
- const deleted = Object.assign(Object.assign({}, post.data), { isDeleted: true });
22887
- if (permanent) {
22888
- setTimeout(() => {
22889
- pushToTombstone('post', postId);
22890
- }, 0);
22891
- }
22892
22993
  else {
22893
- upsertInCache(['post', 'get', postId], { isDeleted: true });
22994
+ const post = (_a = pullFromCache(['post', 'get', comment.data.referenceId])) === null || _a === void 0 ? void 0 : _a.data;
22995
+ if (post) {
22996
+ let removeCount;
22997
+ if (!deleted.parentId) {
22998
+ // NOTE: delete the parent comment will remove all children comments
22999
+ removeCount = deleted.childrenNumber + 1;
23000
+ }
23001
+ else
23002
+ removeCount = 1;
23003
+ post.commentsCount -= removeCount;
23004
+ fireEvent('local.post.updated', {
23005
+ posts: [post],
23006
+ categories: [],
23007
+ comments: [],
23008
+ communities: [],
23009
+ communityUsers: [],
23010
+ feeds: [],
23011
+ files: [],
23012
+ postChildren: [],
23013
+ users: [],
23014
+ videoStreamings: [],
23015
+ });
23016
+ }
22894
23017
  }
22895
- fireEvent('local.post.deleted', {
22896
- posts: [deleted],
22897
- categories: [],
22898
- comments: [],
22899
- communities: [],
22900
- communityUsers: [],
22901
- feeds: [],
23018
+ fireEvent('local.comment.deleted', {
23019
+ comments: [deleted],
23020
+ commentChildren: [],
22902
23021
  files: [],
22903
- postChildren: [],
22904
23022
  users: [],
22905
- videoStreamings: [],
23023
+ communityUsers: [],
22906
23024
  });
22907
- return LinkedObject.post(deleted);
22908
- };
23025
+ return deleted;
23026
+ };
23027
+ /* end_public_function */
22909
23028
 
22910
23029
  /* begin_public_function
22911
- id: post.soft_delete
23030
+ id: comment.soft_delete
22912
23031
  */
22913
23032
  /**
22914
23033
  * ```js
22915
- * import { PostRepository } from '@amityco/ts-sdk'
22916
- * const success = await PostRepository.softDeletePost('foobar')
23034
+ * import { CommentRepository } from '@amityco/ts-sdk'
23035
+ * const success = await CommentRepository.softDeleteComment('foobar')
22917
23036
  * ```
22918
23037
  *
22919
- * Soft deletes a {@link Amity.Post}
23038
+ * Deletes a {@link Amity.Comment}
22920
23039
  *
22921
- * @param postId The {@link Amity.Post} ID to soft delete
22922
- * @return A success boolean if the {@link Amity.Post} was deleted
23040
+ * @param commentId The {@link Amity.Comment} ID to delete
23041
+ * @return A success boolean if the {@link Amity.Comment} was deleted
22923
23042
  *
22924
- * @category Post API
23043
+ * @category Comment API
22925
23044
  * @async
22926
23045
  */
22927
- const softDeletePost = async (postId) => {
23046
+ const softDeleteComment = async (commentId) => {
22928
23047
  const client = getActiveClient();
22929
- client.log('post/softDeletePost', postId);
22930
- const softDeleted = await deletePost(postId, false);
22931
- return LinkedObject.post(softDeleted);
23048
+ client.log('comment/softDeleteComment', commentId);
23049
+ const softDeleted = deleteComment(commentId);
23050
+ return softDeleted;
22932
23051
  };
22933
23052
  /* end_public_function */
22934
23053
 
22935
23054
  /* begin_public_function
22936
- id: post.hard_delete
23055
+ id: comment.hard_delete
22937
23056
  */
22938
23057
  /**
22939
23058
  * ```js
22940
- * import { hardDeletePost } from '@amityco/ts-sdk'
22941
- * const success = await hardDeletePost('foobar')
23059
+ * import { CommentRepository } from '@amityco/ts-sdk'
23060
+ * const success = await CommentRepository.hardDeleteComment('foobar')
22942
23061
  * ```
22943
23062
  *
22944
- * Hard deletes a {@link Amity.Post}
23063
+ * Deletes a {@link Amity.Comment}
22945
23064
  *
22946
- * @param postId The {@link Amity.Post} ID to be hard delete
22947
- * @return A success boolean if the {@link Amity.Post} was deleted
23065
+ * @param commentId The {@link Amity.Comment} ID to delete
23066
+ * @return A success boolean if the {@link Amity.Comment} was deleted
22948
23067
  *
22949
- * @category Post API
23068
+ * @category Comment API
22950
23069
  * @async
22951
23070
  */
22952
- const hardDeletePost = async (postId) => {
23071
+ const hardDeleteComment = async (commentId) => {
22953
23072
  const client = getActiveClient();
22954
- client.log('post/hardDeletePost', postId);
22955
- const hardDeleted = await deletePost(postId, true);
22956
- return LinkedObject.post(hardDeleted);
23073
+ client.log('comment/hardDeleteComment', commentId);
23074
+ const hardDeleted = deleteComment(commentId, true);
23075
+ return hardDeleted;
22957
23076
  };
22958
23077
  /* end_public_function */
22959
23078
 
22960
23079
  /* begin_public_function
22961
- id: post.approve
23080
+ id: comment.flag
22962
23081
  */
22963
23082
  /**
22964
23083
  * ```js
22965
- * import { approvePost } from '@amityco/ts-sdk'
22966
- *
22967
- * const { data: post } = await approvePost('postId')
23084
+ * import { CommentRepository } from '@amityco/ts-sdk'
23085
+ * const flagged = await CommentRepository.flagComment(commentId, reason)
22968
23086
  * ```
22969
23087
  *
22970
- * Approves a {@link Amity.Post}
22971
- *
22972
- * @param postId The {@link Amity.Post} ID to be approved
22973
- * @return A {@link Amity.Post} that was approved
23088
+ * @param commentId The ID of the comment to flag
23089
+ * @param reason the reason to flag the comment
23090
+ * @returns the created report result
22974
23091
  *
22975
- * @category Post API
23092
+ * @category Comment API
22976
23093
  * @async
22977
- */
22978
- const approvePost = async (postId) => {
23094
+ * */
23095
+ const flagComment = async (commentId, reason) => {
22979
23096
  const client = getActiveClient();
22980
- client.log('post/approvePost', postId);
22981
- const { data: payload } = await client.http.post(`/api/v3/posts/${encodeURIComponent(postId)}/approve`);
22982
- fireEvent('post.approved', payload);
22983
- // fire virtual event for community update
22984
- if (payload.posts[0].targetType === 'community') {
22985
- fireEvent('community.updated', payload);
22986
- }
22987
- const data = prepareMembershipPayload(payload, 'communityUsers');
22988
- const cachedAt = client.cache && Date.now();
22989
- if (client.cache)
22990
- ingestInCache(data, { cachedAt });
22991
- return {
22992
- data: LinkedObject.post(data.posts.find(post => post.postId === postId)),
22993
- cachedAt,
23097
+ client.log('comment/flagComment', commentId);
23098
+ const isPredefinedReason = reason &&
23099
+ Object.entries(exports.ContentFlagReasonEnum).some(([key, value]) => key !== exports.ContentFlagReasonEnum.Others && value === reason);
23100
+ const body = {
23101
+ reason: reason && isPredefinedReason ? reason : exports.ContentFlagReasonEnum.Others,
23102
+ detail: reason && !isPredefinedReason ? reason : '',
22994
23103
  };
23104
+ const { data: payload } = await client.http.post(`/api/v3/comments/${encodeURIComponent(commentId)}/flag`, body);
23105
+ if (client.cache) {
23106
+ ingestInCache(payload);
23107
+ }
23108
+ fireEvent('comment.flagged', payload);
23109
+ return !!payload;
22995
23110
  };
22996
23111
  /* end_public_function */
22997
23112
 
22998
23113
  /* begin_public_function
22999
- id: post.decline
23114
+ id: comment.unflag
23000
23115
  */
23001
23116
  /**
23002
23117
  * ```js
23003
- * import { declinePost } from '@amityco/ts-sdk'
23004
- *
23005
- * const {data: post} = await declinePost('postId')
23118
+ * import { CommentRepository } from '@amityco/ts-sdk'
23119
+ * const unflagged = await CommentRepository.unflagComment('commentId')
23006
23120
  * ```
23007
23121
  *
23008
- * Declines a {@link Amity.Post}
23009
- *
23010
- * @param postId The {@link Amity.Post} ID to be declined
23011
- * @return A {@link Amity.Post} that was declined
23122
+ * @param commentId The ID of comment to unflag
23123
+ * @returns the unflagged result
23012
23124
  *
23013
- * @category Post API
23125
+ * @category Comment API
23014
23126
  * @async
23015
- */
23016
- const declinePost = async (postId) => {
23127
+ * */
23128
+ const unflagComment = async (commentId) => {
23017
23129
  const client = getActiveClient();
23018
- client.log('post/declinePost', postId);
23019
- const { data: payload } = await client.http.post(`/api/v3/posts/${encodeURIComponent(postId)}/decline`);
23020
- // fire virtual event
23021
- if (payload.posts[0].targetType === 'community') {
23022
- fireEvent('community.updated', payload);
23023
- }
23024
- fireEvent('post.declined', payload);
23025
- const data = prepareMembershipPayload(payload, 'communityUsers');
23026
- const cachedAt = client.cache && Date.now();
23027
- if (client.cache)
23028
- ingestInCache(data, { cachedAt });
23029
- return {
23030
- data: LinkedObject.post(payload.posts.find(post => post.postId === postId)),
23031
- cachedAt,
23032
- };
23033
- };
23034
- /* end_public_function */
23035
-
23036
- /* begin_public_function
23037
- id: post.flag
23038
- */
23039
- /**
23040
- * ```js
23041
- * import { PostRepository } from '@amityco/ts-sdk'
23042
- * const flagged = await PostRepository.flagPost(postId, reason)
23043
- * ```
23044
- *
23045
- * @param postId of the post to flag
23046
- * @param reason the reason to flag the post
23047
- * @returns a boolean
23048
- *
23049
- * @category Post API
23050
- * @async
23051
- * */
23052
- const flagPost = async (postId, reason) => {
23053
- const client = getActiveClient();
23054
- client.log('post/flagPost', postId);
23055
- const isPredefinedReason = reason &&
23056
- Object.entries(exports.ContentFlagReasonEnum).some(([key, value]) => key !== exports.ContentFlagReasonEnum.Others && value === reason);
23057
- const body = {
23058
- reason: reason && isPredefinedReason ? reason : exports.ContentFlagReasonEnum.Others,
23059
- detail: reason && !isPredefinedReason ? reason : '',
23060
- };
23061
- const { data: payload } = await client.http.post(`/api/v3/posts/${encodeURIComponent(postId)}/flag`, body);
23062
- if (client.cache) {
23063
- ingestInCache(prepareMembershipPayload(payload, 'communityUsers'));
23064
- }
23065
- fireEvent('post.flagged', payload);
23066
- return !!payload;
23067
- };
23068
- /* end_public_function */
23069
-
23070
- /* begin_public_function
23071
- id: post.unflag
23072
- */
23073
- /**
23074
- * ```js
23075
- * import { PostRepository } from '@amityco/ts-sdk'
23076
- * const unflagged = await PostRepository.unflagPost(postId)
23077
- * ```
23078
- *
23079
- * @param postId of the post to unflag
23080
- * @returns the unflag post result
23081
- *
23082
- * @category Post API
23083
- * @async
23084
- * */
23085
- const unflagPost = async (postId) => {
23086
- const client = getActiveClient();
23087
- client.log('post/unflagPost', postId);
23088
- const { data: payload } = await client.http.delete(`/api/v3/posts/${encodeURIComponent(postId)}/unflag`);
23130
+ client.log('comment/unflagComment', commentId);
23131
+ const { data: payload } = await client.http.delete(`/api/v3/comments/${encodeURIComponent(commentId)}/unflag`);
23089
23132
  if (client.cache) {
23090
- ingestInCache(prepareMembershipPayload(payload, 'communityUsers'));
23133
+ ingestInCache(payload);
23091
23134
  }
23092
- fireEvent('post.unflagged', payload);
23135
+ fireEvent('comment.unflagged', payload);
23093
23136
  return !!payload;
23094
23137
  };
23095
23138
  /* end_public_function */
23096
23139
 
23097
23140
  /* begin_public_function
23098
- id: post.check_flag_by_me
23141
+ id: comment.check_flag_by_me
23099
23142
  */
23100
23143
  /**
23101
23144
  * ```js
23102
- * import { PostRepository } from '@amityco/ts-sdk'
23103
- * const isReported = await PostRepository.isPostFlaggedByMe('post', postId)
23145
+ * import { CommentRepository } from '@amityco/ts-sdk'
23146
+ * const isReported = await CommentRepository.isCommentFlaggedByMe('commentId')
23104
23147
  * ```
23105
23148
  *
23106
- * @param postId of the post to check if flagged by current user
23107
- * @returns `true` if the post is flagged by me, `false` if doesn't.
23149
+ * @param commentId The ID of the comment to check if flagged by current user
23150
+ * @returns `true` if the comment is flagged by me, `false` if doesn't.
23108
23151
  *
23109
- * @category Post API
23152
+ * @category Comment API
23110
23153
  * @async
23111
23154
  * */
23112
- const isPostFlaggedByMe = async (postId) => {
23155
+ const isCommentFlaggedByMe = async (commentId) => {
23113
23156
  const client = getActiveClient();
23114
- client.log('post/isPostFlaggedByMe', postId);
23115
- const { data: { result }, } = await client.http.get(`/api/v3/posts/${postId}/isflagbyme`);
23157
+ client.log('comment/isCommentFlaggedByMe', commentId);
23158
+ const { data: { result }, } = await client.http.get(`/api/v3/comments/${commentId}/isflagbyme`);
23116
23159
  return result;
23117
23160
  };
23118
23161
  /* end_public_function */
23119
23162
 
23120
- /* begin_public_function
23121
- id: comment.get_by_ids
23122
- */
23123
- /**
23124
- * ```js
23125
- * import { CommentRepository } from '@amityco/ts-sdk'
23126
- * const comments = await CommentRepository.getCommentByIds(['foo', 'bar'])
23127
- * ```
23128
- *
23129
- * Fetches a collection of {@link Amity.Comment} objects
23130
- *
23131
- * @param commentIds the IDs of the {@link Amity.Comment} to fetch
23132
- * @returns the associated collection of {@link Amity.Comment} objects
23133
- *
23134
- * @category Comment API
23135
- * @async
23136
- */
23137
- const getCommentByIds = async (commentIds) => {
23163
+ const getComment$1 = async (commentId) => {
23138
23164
  const client = getActiveClient();
23139
- client.log('comment/getCommentByIds', commentIds);
23140
- const encodedCommentIds = commentIds.map(commentId => encodeURIComponent(commentId));
23165
+ client.log('comment/getComment', commentId);
23166
+ isInTombstone('comment', commentId);
23141
23167
  let data;
23142
23168
  try {
23143
23169
  // API-FIX: endpoint should not be /list, parameters should be querystring.
23144
- const response = await client.http.get(`/api/v3/comments/list`, {
23145
- params: { commentIds: encodedCommentIds },
23146
- });
23170
+ const response = await client.http.get(`/api/v3/comments/${encodeURIComponent(commentId)}`);
23147
23171
  data = response.data;
23148
23172
  }
23149
23173
  catch (error) {
23150
- commentIds.forEach(commentId => {
23151
- if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
23152
- pushToTombstone('comment', commentId);
23153
- }
23154
- });
23174
+ if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
23175
+ pushToTombstone('comment', commentId);
23176
+ }
23155
23177
  throw error;
23156
23178
  }
23157
23179
  const cachedAt = client.cache && Date.now();
23158
23180
  if (client.cache)
23159
23181
  ingestInCache(data, { cachedAt });
23182
+ const { comments } = data;
23160
23183
  return {
23161
- data: data.comments.map(comment => LinkedObject.comment(comment)),
23184
+ data: comments.find(comment => comment.commentId === commentId),
23162
23185
  cachedAt,
23163
23186
  };
23164
23187
  };
23165
- /* end_public_function */
23166
- /**
23167
- * ```js
23168
- * import { getCommentByIds } from '@amityco/ts-sdk'
23169
- * const comments = getCommentByIds.locally(['foo', 'bar'])
23170
- * ```
23171
- *
23172
- * Fetches a collection of {@link Amity.Comment} objects from cache
23173
- *
23174
- * @param commentIds the IDs of the {@link Amity.Comment} to fetch
23175
- * @returns the associated collection of {@link Amity.Comment} objects
23176
- *
23177
- * @category Comment API
23178
- */
23179
- getCommentByIds.locally = (commentIds) => {
23180
- var _a;
23188
+ getComment$1.locally = (commentId) => {
23181
23189
  const client = getActiveClient();
23182
- client.log('comment/getCommentByIds.locally', commentIds);
23190
+ client.log('comment/getComment.locally', commentId);
23183
23191
  if (!client.cache)
23184
23192
  return;
23185
- const cached = commentIds
23186
- .map(commentId => pullFromCache(['comment', 'get', commentId]))
23187
- .filter(Boolean);
23188
- const comments = cached.map(({ data }) => data);
23189
- const oldest = (_a = cached.sort((a, b) => (a.cachedAt < b.cachedAt ? -1 : 1))) === null || _a === void 0 ? void 0 : _a[0];
23190
- if ((cached === null || cached === void 0 ? void 0 : cached.length) < commentIds.length)
23193
+ const cached = pullFromCache(['comment', 'get', commentId]);
23194
+ if (!cached)
23191
23195
  return;
23192
23196
  return {
23193
- data: comments.map(comment => LinkedObject.comment(comment)),
23194
- cachedAt: oldest.cachedAt,
23197
+ data: cached.data,
23198
+ cachedAt: cached.cachedAt,
23195
23199
  };
23196
23200
  };
23197
23201
 
23198
- /* begin_public_function
23199
- id: comment.create
23200
- */
23201
23202
  /**
23202
23203
  * ```js
23203
- * import { CommentRepository } from '@amityco/ts-sdk'
23204
- * const newComment = await CommentRepository.createComment(bundle)
23204
+ * import { onCommentDeleteLocal } from '@amityco/ts-sdk'
23205
+ * const dispose = onCommentDeleteLocal(comment => {
23206
+ * // ...
23207
+ * })
23205
23208
  * ```
23206
23209
  *
23207
- * Creates an {@link Amity.Comment}
23210
+ * Fired when a {@link Amity.InternalComment} has been deleted
23208
23211
  *
23209
- * @param bundle The data necessary to create a new {@link Amity.Comment}
23210
- * @returns The newly created {@link Amity.Comment}
23212
+ * @param callback The function to call when the event was fired
23213
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
23211
23214
  *
23212
- * @category Comment API
23213
- * @async
23215
+ * @category Comment Events
23214
23216
  */
23215
- const createComment = async (bundle) => {
23216
- var _a;
23217
- const client = getActiveClient();
23218
- client.log('comment/createComment', bundle);
23219
- const { data } = await client.http.post('/api/v3/comments', bundle);
23220
- const { comments } = data;
23221
- // BE always returns an array of comments If it got record 0 from BE it might have a problem on creation logic
23222
- if (comments.length === 0)
23223
- throw new Error('Comment not created');
23224
- const cachedAt = client.cache && Date.now();
23225
- if (client.cache)
23226
- ingestInCache(data, { cachedAt });
23227
- if (['post', 'content'].includes(bundle.referenceType)) {
23228
- const post = (_a = pullFromCache(['post', 'get', bundle.referenceId])) === null || _a === void 0 ? void 0 : _a.data;
23229
- if (post) {
23230
- post.commentsCount += 1;
23231
- fireEvent('local.post.updated', {
23232
- posts: [post],
23233
- categories: [],
23234
- comments: [],
23235
- communities: [],
23236
- communityUsers: data.communityUsers,
23237
- feeds: [],
23238
- files: data.files,
23239
- postChildren: [],
23240
- users: data.users,
23241
- videoStreamings: [],
23242
- });
23243
- }
23244
- }
23245
- else if (bundle.referenceType === 'story') {
23246
- const storyIndex = pullFromCache([
23247
- "story-reference" /* STORY_KEY_CACHE.STORY_ID_TO_REFERENCE_ID */,
23248
- bundle.referenceId,
23249
- ]);
23250
- if (storyIndex === null || storyIndex === void 0 ? void 0 : storyIndex.data) {
23251
- const cacheStory = pullFromCache([
23252
- "story" /* STORY_KEY_CACHE.STORY */,
23253
- 'get',
23254
- storyIndex.data,
23255
- ]);
23256
- if (cacheStory === null || cacheStory === void 0 ? void 0 : cacheStory.data) {
23257
- fireEvent('story.updated', {
23258
- stories: [
23259
- Object.assign(Object.assign({}, cacheStory.data), { commentsCount: cacheStory.data.commentsCount + 1, comments: [...new Set([...cacheStory.data.comments, comments[0].commentId])] }),
23260
- ],
23261
- categories: [],
23262
- comments,
23263
- communities: [],
23264
- communityUsers: data.communityUsers,
23265
- files: data.files,
23266
- users: data.users,
23267
- });
23268
- }
23269
- }
23270
- }
23271
- fireEvent('local.comment.created', data);
23272
- return {
23273
- data: LinkedObject.comment(comments[0]),
23274
- cachedAt,
23275
- };
23276
- };
23277
- /* end_public_function */
23217
+ const onCommentDeleteLocal = (callback) => createLocalCommentEventSubscriber('local.comment.deleted', callback);
23278
23218
 
23279
- /* begin_public_function
23280
- id: comment.update_comment
23281
- */
23282
23219
  /**
23283
23220
  * ```js
23284
- * import { CommentRepository } from '@amityco/ts-sdk'
23285
- * const updated = await CommentRepository.updateComment(commentId, {
23286
- * data: { text: 'hello world' }
23221
+ * import { onLocalCommentReactionAdded } from '@amityco/ts-sdk'
23222
+ * const dispose = onLocalCommentReactionAdded(comment => {
23223
+ * // ...
23287
23224
  * })
23288
23225
  * ```
23289
23226
  *
23290
- * Updates an {@link Amity.Comment}
23227
+ * Fired when a {@link Amity.InternalComment} has been reacted
23291
23228
  *
23292
- * @param commentId The ID of the {@link Amity.Comment} to edit
23293
- * @param patch The patch data to apply
23294
- * @returns the updated {@link Amity.Comment} object
23229
+ * @param callback The function to call when the event was fired
23230
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
23295
23231
  *
23296
- * @category Comment API
23297
- * @async
23232
+ * @category Comment Events
23298
23233
  */
23299
- const updateComment = async (commentId, patch) => {
23300
- const client = getActiveClient();
23301
- client.log('user/updateComment', patch);
23302
- const { data } = await client.http.put(`/api/v3/comments/${encodeURIComponent(commentId)}`, patch);
23303
- const cachedAt = client.cache && Date.now();
23304
- if (client.cache)
23305
- ingestInCache(data, { cachedAt });
23306
- fireEvent('comment.updated', data);
23307
- const { comments } = data;
23308
- return {
23309
- data: LinkedObject.comment(comments.find(comment => comment.commentId === commentId)),
23310
- cachedAt,
23311
- };
23312
- };
23313
- /* end_public_function */
23314
-
23315
- // Due to we have optimistic logic, we will use referenceId as a id in SDK instead of storyId
23316
- const applyMissingField = (rawData, isCreated = false) => {
23317
- const { storyId, referenceId } = rawData;
23318
- if (!isCreated) {
23319
- if (referenceId)
23320
- return Object.assign(Object.assign({}, rawData), { syncState: "synced" /* Amity.SyncState.Synced */ });
23321
- }
23322
- return Object.assign(Object.assign({}, rawData), { syncState: "synced" /* Amity.SyncState.Synced */, referenceId: storyId });
23323
- };
23324
- const convertRawStoryToInternal = (data, isCreated = false) => {
23325
- const { stories } = data;
23326
- const storiesData = stories.map(story => applyMissingField(story, isCreated));
23327
- return Object.assign(Object.assign({}, data), { stories: storiesData });
23328
- };
23329
-
23330
- const getStoryByStoryId$1 = async (storyId) => {
23234
+ const onLocalCommentReactionAdded = (callback) => {
23331
23235
  const client = getActiveClient();
23332
- client.log('story/getStoryByStoryId', storyId);
23333
- // Get story referenceId from cache
23334
- const cacheReferenceId = pullFromCache([
23335
- "story-reference" /* STORY_KEY_CACHE.STORY_ID_TO_REFERENCE_ID */,
23336
- storyId,
23337
- ]);
23338
- if (cacheReferenceId === null || cacheReferenceId === void 0 ? void 0 : cacheReferenceId.data) {
23339
- const { data: referenceId } = cacheReferenceId;
23340
- isInTombstone('story', referenceId);
23341
- }
23342
- let data;
23343
- try {
23344
- const response = await client.http.get(`/api/v4/stories/${storyId}`);
23345
- data = convertRawStoryToInternal(response.data);
23346
- }
23347
- catch (error) {
23348
- if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
23349
- pushToTombstone('story', storyId);
23236
+ const filter = ({ comment }) => {
23237
+ if (!client.cache) {
23238
+ callback(comment);
23239
+ }
23240
+ else {
23241
+ upsertInCache(['comment', 'get', comment.commentId], comment);
23242
+ callback(commentLinkedObject(comment));
23350
23243
  }
23351
- throw error;
23352
- }
23353
- const cachedAt = client.cache && Date.now();
23354
- if (client.cache) {
23355
- ingestInCache(data, { cachedAt });
23356
- }
23357
- return {
23358
- data: data.stories[0],
23359
- cachedAt,
23360
- };
23361
- };
23362
- getStoryByStoryId$1.locally = (storyId) => {
23363
- const client = getActiveClient();
23364
- client.log('story/getStorybyStoryId', storyId);
23365
- // Get story referenceId from cache
23366
- const cacheReferenceId = pullFromCache([
23367
- "story-reference" /* STORY_KEY_CACHE.STORY_ID_TO_REFERENCE_ID */,
23368
- storyId,
23369
- ]);
23370
- if (cacheReferenceId === null || cacheReferenceId === void 0 ? void 0 : cacheReferenceId.data) {
23371
- const { data: referenceId } = cacheReferenceId;
23372
- isInTombstone('story', referenceId);
23373
- }
23374
- const cachedAt = client.cache && Date.now();
23375
- const storyCache = pullFromCache(['story', 'get', storyId]);
23376
- if (!storyCache)
23377
- return;
23378
- return {
23379
- data: storyCache.data,
23380
- cachedAt,
23381
23244
  };
23245
+ return createEventSubscriber(client, 'local.comment.addReaction', 'local.comment.addReaction', filter);
23382
23246
  };
23383
23247
 
23384
- /* begin_public_function
23385
- id: comment.soft_delete, comment.hard_delete
23386
- */
23387
23248
  /**
23388
23249
  * ```js
23389
- * import { CommentRepository } from '@amityco/ts-sdk'
23390
- * const success = await CommentRepository.deleteComment('foobar')
23250
+ * import { onLocalCommentReactionRemoved } from '@amityco/ts-sdk'
23251
+ * const dispose = onLocalCommentReactionRemoved(comment => {
23252
+ * // ...
23253
+ * })
23391
23254
  * ```
23392
23255
  *
23393
- * Deletes a {@link Amity.Comment}
23256
+ * Fired when a {@link Amity.InternalComment} has been reacted
23394
23257
  *
23395
- * @param commentId The {@link Amity.Comment} ID to delete
23396
- * @return A success boolean if the {@link Amity.Comment} was deleted
23258
+ * @param callback The function to call when the event was fired
23259
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
23397
23260
  *
23398
- * @category Comment API
23399
- * @async
23261
+ * @category Comment Events
23400
23262
  */
23401
- const deleteComment = async (commentId, permanent = false) => {
23402
- var _a;
23263
+ const onLocalCommentReactionRemoved = (callback) => {
23403
23264
  const client = getActiveClient();
23404
- const comment = await getComment$2(commentId);
23405
- // API-FIX: This endpoint has not been implemented yet.
23406
- await client.http.delete(`/api/v4/comments/${encodeURIComponent(commentId)}`, {
23407
- params: {
23408
- commentId,
23409
- permanent,
23410
- },
23411
- });
23412
- // to support hard deletion
23413
- const deleted = Object.assign(Object.assign({}, comment.data), { isDeleted: true });
23414
- if (comment.data.referenceType === 'story') {
23415
- const story = await getStoryByStoryId$1(comment.data.referenceId);
23416
- fireEvent('local.story.updated', {
23417
- stories: [story.data],
23418
- categories: [],
23419
- comments: [],
23420
- communities: [],
23421
- communityUsers: [],
23422
- files: [],
23423
- users: [],
23424
- });
23425
- }
23426
- else {
23427
- const post = (_a = pullFromCache(['post', 'get', comment.data.referenceId])) === null || _a === void 0 ? void 0 : _a.data;
23428
- if (post) {
23429
- let removeCount;
23430
- if (!deleted.parentId) {
23431
- // NOTE: delete the parent comment will remove all children comments
23432
- removeCount = deleted.childrenNumber + 1;
23433
- }
23434
- else
23435
- removeCount = 1;
23436
- post.commentsCount -= removeCount;
23437
- fireEvent('local.post.updated', {
23438
- posts: [post],
23439
- categories: [],
23440
- comments: [],
23441
- communities: [],
23442
- communityUsers: [],
23443
- feeds: [],
23444
- files: [],
23445
- postChildren: [],
23446
- users: [],
23447
- videoStreamings: [],
23448
- });
23265
+ const filter = ({ comment }) => {
23266
+ if (!client.cache) {
23267
+ callback(comment);
23449
23268
  }
23450
- }
23451
- fireEvent('local.comment.deleted', {
23452
- comments: [deleted],
23453
- commentChildren: [],
23454
- files: [],
23455
- users: [],
23456
- communityUsers: [],
23457
- });
23458
- if (permanent) {
23459
- scheduleTask(() => pushToTombstone('comment', commentId));
23460
- }
23461
- else {
23462
- upsertInCache(['comment', 'get', commentId], { isDeleted: true });
23463
- }
23464
- return deleted;
23465
- };
23466
- /* end_public_function */
23269
+ else {
23270
+ upsertInCache(['comment', 'get', comment.commentId], comment);
23271
+ callback(commentLinkedObject(comment));
23272
+ }
23273
+ };
23274
+ return createEventSubscriber(client, 'local.comment.removeReaction', 'local.comment.removeReaction', filter);
23275
+ };
23467
23276
 
23468
23277
  /* begin_public_function
23469
- id: comment.soft_delete
23278
+ id: comment.get
23470
23279
  */
23471
23280
  /**
23472
23281
  * ```js
23473
- * import { CommentRepository } from '@amityco/ts-sdk'
23474
- * const success = await CommentRepository.softDeleteComment('foobar')
23475
- * ```
23476
- *
23477
- * Deletes a {@link Amity.Comment}
23282
+ * import { CommentRepository } from '@amityco/ts-sdk';
23478
23283
  *
23479
- * @param commentId The {@link Amity.Comment} ID to delete
23480
- * @return A success boolean if the {@link Amity.Comment} was deleted
23284
+ * let comment;
23481
23285
  *
23482
- * @category Comment API
23483
- * @async
23484
- */
23485
- const softDeleteComment = async (commentId) => {
23486
- const client = getActiveClient();
23487
- client.log('comment/softDeleteComment', commentId);
23488
- const softDeleted = deleteComment(commentId);
23489
- return softDeleted;
23490
- };
23491
- /* end_public_function */
23492
-
23493
- /* begin_public_function
23494
- id: comment.hard_delete
23495
- */
23496
- /**
23497
- * ```js
23498
- * import { CommentRepository } from '@amityco/ts-sdk'
23499
- * const success = await CommentRepository.hardDeleteComment('foobar')
23286
+ * const unsub = CommentRepository.getComment(commentId, response => {
23287
+ * comment = response.data;
23288
+ * });
23500
23289
  * ```
23501
23290
  *
23502
- * Deletes a {@link Amity.Comment}
23291
+ * Observe all mutation on a given {@link Amity.Comment}
23503
23292
  *
23504
- * @param commentId The {@link Amity.Comment} ID to delete
23505
- * @return A success boolean if the {@link Amity.Comment} was deleted
23293
+ * @param commentId the ID of the comment to observe
23294
+ * @param callback the function to call when new data are available
23295
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the comment
23506
23296
  *
23507
- * @category Comment API
23508
- * @async
23297
+ * @category Comment Live Object
23509
23298
  */
23510
- const hardDeleteComment = async (commentId) => {
23511
- const client = getActiveClient();
23512
- client.log('comment/hardDeleteComment', commentId);
23513
- const hardDeleted = deleteComment(commentId, true);
23514
- return hardDeleted;
23515
- };
23516
- /* end_public_function */
23517
-
23518
- /* begin_public_function
23519
- id: comment.flag
23520
- */
23521
- /**
23522
- * ```js
23523
- * import { CommentRepository } from '@amityco/ts-sdk'
23524
- * const flagged = await CommentRepository.flagComment(commentId, reason)
23525
- * ```
23526
- *
23527
- * @param commentId The ID of the comment to flag
23528
- * @param reason the reason to flag the comment
23529
- * @returns the created report result
23530
- *
23531
- * @category Comment API
23532
- * @async
23533
- * */
23534
- const flagComment = async (commentId, reason) => {
23535
- const client = getActiveClient();
23536
- client.log('comment/flagComment', commentId);
23537
- const isPredefinedReason = reason &&
23538
- Object.entries(exports.ContentFlagReasonEnum).some(([key, value]) => key !== exports.ContentFlagReasonEnum.Others && value === reason);
23539
- const body = {
23540
- reason: reason && isPredefinedReason ? reason : exports.ContentFlagReasonEnum.Others,
23541
- detail: reason && !isPredefinedReason ? reason : '',
23542
- };
23543
- const { data: payload } = await client.http.post(`/api/v3/comments/${encodeURIComponent(commentId)}/flag`, body);
23544
- if (client.cache) {
23545
- ingestInCache(payload);
23546
- }
23547
- fireEvent('comment.flagged', payload);
23548
- return !!payload;
23549
- };
23550
- /* end_public_function */
23551
-
23552
- /* begin_public_function
23553
- id: comment.unflag
23554
- */
23555
- /**
23556
- * ```js
23557
- * import { CommentRepository } from '@amityco/ts-sdk'
23558
- * const unflagged = await CommentRepository.unflagComment('commentId')
23559
- * ```
23560
- *
23561
- * @param commentId The ID of comment to unflag
23562
- * @returns the unflagged result
23563
- *
23564
- * @category Comment API
23565
- * @async
23566
- * */
23567
- const unflagComment = async (commentId) => {
23568
- const client = getActiveClient();
23569
- client.log('comment/unflagComment', commentId);
23570
- const { data: payload } = await client.http.delete(`/api/v3/comments/${encodeURIComponent(commentId)}/unflag`);
23571
- if (client.cache) {
23572
- ingestInCache(payload);
23573
- }
23574
- fireEvent('comment.unflagged', payload);
23575
- return !!payload;
23576
- };
23577
- /* end_public_function */
23578
-
23579
- /* begin_public_function
23580
- id: comment.check_flag_by_me
23581
- */
23582
- /**
23583
- * ```js
23584
- * import { CommentRepository } from '@amityco/ts-sdk'
23585
- * const isReported = await CommentRepository.isCommentFlaggedByMe('commentId')
23586
- * ```
23587
- *
23588
- * @param commentId The ID of the comment to check if flagged by current user
23589
- * @returns `true` if the comment is flagged by me, `false` if doesn't.
23590
- *
23591
- * @category Comment API
23592
- * @async
23593
- * */
23594
- const isCommentFlaggedByMe = async (commentId) => {
23595
- const client = getActiveClient();
23596
- client.log('comment/isCommentFlaggedByMe', commentId);
23597
- const { data: { result }, } = await client.http.get(`/api/v3/comments/${commentId}/isflagbyme`);
23598
- return result;
23599
- };
23600
- /* end_public_function */
23601
-
23602
- const getComment$1 = async (commentId) => {
23603
- const client = getActiveClient();
23604
- client.log('comment/getComment', commentId);
23605
- isInTombstone('comment', commentId);
23606
- let data;
23607
- try {
23608
- // API-FIX: endpoint should not be /list, parameters should be querystring.
23609
- const response = await client.http.get(`/api/v3/comments/${encodeURIComponent(commentId)}`);
23610
- data = response.data;
23611
- }
23612
- catch (error) {
23613
- if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
23614
- pushToTombstone('comment', commentId);
23615
- }
23616
- throw error;
23617
- }
23618
- const cachedAt = client.cache && Date.now();
23619
- if (client.cache)
23620
- ingestInCache(data, { cachedAt });
23621
- const { comments } = data;
23622
- return {
23623
- data: comments.find(comment => comment.commentId === commentId),
23624
- cachedAt,
23625
- };
23626
- };
23627
- getComment$1.locally = (commentId) => {
23628
- const client = getActiveClient();
23629
- client.log('comment/getComment.locally', commentId);
23630
- if (!client.cache)
23631
- return;
23632
- const cached = pullFromCache(['comment', 'get', commentId]);
23633
- if (!cached)
23634
- return;
23635
- return {
23636
- data: cached.data,
23637
- cachedAt: cached.cachedAt,
23638
- };
23639
- };
23640
-
23641
- /**
23642
- * ```js
23643
- * import { onCommentDeleteLocal } from '@amityco/ts-sdk'
23644
- * const dispose = onCommentDeleteLocal(comment => {
23645
- * // ...
23646
- * })
23647
- * ```
23648
- *
23649
- * Fired when a {@link Amity.InternalComment} has been deleted
23650
- *
23651
- * @param callback The function to call when the event was fired
23652
- * @returns an {@link Amity.Unsubscriber} function to stop listening
23653
- *
23654
- * @category Comment Events
23655
- */
23656
- const onCommentDeleteLocal = (callback) => createLocalCommentEventSubscriber('local.comment.deleted', callback);
23657
-
23658
- /**
23659
- * ```js
23660
- * import { onLocalCommentReactionAdded } from '@amityco/ts-sdk'
23661
- * const dispose = onLocalCommentReactionAdded(comment => {
23662
- * // ...
23663
- * })
23664
- * ```
23665
- *
23666
- * Fired when a {@link Amity.InternalComment} has been reacted
23667
- *
23668
- * @param callback The function to call when the event was fired
23669
- * @returns an {@link Amity.Unsubscriber} function to stop listening
23670
- *
23671
- * @category Comment Events
23672
- */
23673
- const onLocalCommentReactionAdded = (callback) => {
23674
- const client = getActiveClient();
23675
- const filter = ({ comment }) => {
23676
- if (!client.cache) {
23677
- callback(comment);
23678
- }
23679
- else {
23680
- upsertInCache(['comment', 'get', comment.commentId], comment);
23681
- callback(commentLinkedObject(comment));
23682
- }
23683
- };
23684
- return createEventSubscriber(client, 'local.comment.addReaction', 'local.comment.addReaction', filter);
23685
- };
23686
-
23687
- /**
23688
- * ```js
23689
- * import { onLocalCommentReactionRemoved } from '@amityco/ts-sdk'
23690
- * const dispose = onLocalCommentReactionRemoved(comment => {
23691
- * // ...
23692
- * })
23693
- * ```
23694
- *
23695
- * Fired when a {@link Amity.InternalComment} has been reacted
23696
- *
23697
- * @param callback The function to call when the event was fired
23698
- * @returns an {@link Amity.Unsubscriber} function to stop listening
23699
- *
23700
- * @category Comment Events
23701
- */
23702
- const onLocalCommentReactionRemoved = (callback) => {
23703
- const client = getActiveClient();
23704
- const filter = ({ comment }) => {
23705
- if (!client.cache) {
23706
- callback(comment);
23707
- }
23708
- else {
23709
- upsertInCache(['comment', 'get', comment.commentId], comment);
23710
- callback(commentLinkedObject(comment));
23711
- }
23712
- };
23713
- return createEventSubscriber(client, 'local.comment.removeReaction', 'local.comment.removeReaction', filter);
23714
- };
23715
-
23716
- /* begin_public_function
23717
- id: comment.get
23718
- */
23719
- /**
23720
- * ```js
23721
- * import { CommentRepository } from '@amityco/ts-sdk';
23722
- *
23723
- * let comment;
23724
- *
23725
- * const unsub = CommentRepository.getComment(commentId, response => {
23726
- * comment = response.data;
23727
- * });
23728
- * ```
23729
- *
23730
- * Observe all mutation on a given {@link Amity.Comment}
23731
- *
23732
- * @param commentId the ID of the comment to observe
23733
- * @param callback the function to call when new data are available
23734
- * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the comment
23735
- *
23736
- * @category Comment Live Object
23737
- */
23738
- const getComment = (commentId, callback) => {
23739
- return liveObject(commentId, callback, 'commentId', getComment$1, [
23740
- onCommentDeleteLocal,
23741
- onCommentDeleted,
23742
- onCommentFlagged,
23743
- onCommentReactionAdded,
23744
- onCommentReactionRemoved,
23745
- onCommentUnflagged,
23746
- onCommentUpdated,
23747
- onLocalCommentReactionAdded,
23748
- onLocalCommentReactionRemoved,
23749
- ]);
23299
+ const getComment = (commentId, callback) => {
23300
+ return liveObject(commentId, callback, 'commentId', getComment$1, [
23301
+ onCommentDeleteLocal,
23302
+ onCommentDeleted,
23303
+ onCommentFlagged,
23304
+ onCommentReactionAdded,
23305
+ onCommentReactionRemoved,
23306
+ onCommentUnflagged,
23307
+ onCommentUpdated,
23308
+ onLocalCommentReactionAdded,
23309
+ onLocalCommentReactionRemoved,
23310
+ ]);
23750
23311
  };
23751
23312
  /* end_public_function */
23752
23313
 
@@ -23995,7 +23556,7 @@ const getComments = (params, callback, config) => {
23995
23556
  };
23996
23557
  /* end_public_function */
23997
23558
 
23998
- var index$8 = /*#__PURE__*/Object.freeze({
23559
+ var index$9 = /*#__PURE__*/Object.freeze({
23999
23560
  __proto__: null,
24000
23561
  getCommentByIds: getCommentByIds,
24001
23562
  createComment: createComment,
@@ -24017,97 +23578,930 @@ var index$8 = /*#__PURE__*/Object.freeze({
24017
23578
  getComments: getComments
24018
23579
  });
24019
23580
 
24020
- /**
24021
- * ```js
24022
- * import { onPostUpdatedLocal } from '@amityco/ts-sdk'
24023
- * const dispose = onPostUpdatedLocal(post => {
24024
- * // ...
24025
- * })
23581
+ const getPost$1 = async (postId) => {
23582
+ const client = getActiveClient();
23583
+ client.log('post/getPost', postId);
23584
+ isInTombstone('post', postId);
23585
+ let payload;
23586
+ try {
23587
+ // API-FIX: endpoint should not be /list, parameters should be querystring.
23588
+ const response = await client.http.get(`/api/v3/posts/${encodeURIComponent(postId)}`);
23589
+ payload = response.data;
23590
+ }
23591
+ catch (error) {
23592
+ if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
23593
+ pushToTombstone('post', postId);
23594
+ }
23595
+ throw error;
23596
+ }
23597
+ const data = prepareMembershipPayload(payload, 'communityUsers');
23598
+ const cachedAt = client.cache && Date.now();
23599
+ if (client.cache)
23600
+ ingestInCache(data, { cachedAt });
23601
+ const { posts } = data;
23602
+ const result = posts.find(post => post.postId === postId);
23603
+ return {
23604
+ data: result,
23605
+ cachedAt,
23606
+ };
23607
+ };
23608
+ getPost$1.locally = (postId) => {
23609
+ const client = getActiveClient();
23610
+ client.log('post/getPost.locally', postId);
23611
+ if (!client.cache)
23612
+ return;
23613
+ const cached = pullFromCache(['post', 'get', postId]);
23614
+ if (!cached)
23615
+ return;
23616
+ return {
23617
+ data: cached.data,
23618
+ cachedAt: cached.cachedAt,
23619
+ };
23620
+ };
23621
+
23622
+ /**
23623
+ * ```js
23624
+ * import { onLocalPostDeleted } from '@amityco/ts-sdk'
23625
+ * const dispose = onLocalPostDeleted(post => {
23626
+ * // ...
23627
+ * })
23628
+ * ```
23629
+ *
23630
+ * Fired when a {@link Amity.InternalPost} has been deleted
23631
+ *
23632
+ * @param callback The function to call when the event was fired
23633
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
23634
+ *
23635
+ * @category Post Events
23636
+ */
23637
+ const onLocalPostDeleted = (callback) => createLocalPostEventSubscriber('local.post.deleted', callback);
23638
+
23639
+ /**
23640
+ * ```js
23641
+ * import { onLocalPostReactionAdded } from '@amityco/ts-sdk'
23642
+ * const dispose = onPostReactionAdded(post => {
23643
+ * // ...
23644
+ * })
23645
+ * ```
23646
+ *
23647
+ * Fired when a {@link Amity.InternalPost} has been reacted
23648
+ *
23649
+ * @param callback The function to call when the event was fired
23650
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
23651
+ *
23652
+ * @category Post Events
23653
+ */
23654
+ const onLocalPostReactionAdded = (callback) => {
23655
+ const client = getActiveClient();
23656
+ const filter = ({ post }) => {
23657
+ if (!client.cache) {
23658
+ callback(post);
23659
+ }
23660
+ else {
23661
+ upsertInCache(['post', 'get', post.postId], post);
23662
+ callback(post);
23663
+ }
23664
+ };
23665
+ return createEventSubscriber(client, 'local.post.addReaction', 'local.post.addReaction', filter);
23666
+ };
23667
+
23668
+ /**
23669
+ * ```js
23670
+ * import { onLocalPostReactionRemoved } from '@amityco/ts-sdk'
23671
+ * const dispose = onPostReactionRemoved(post => {
23672
+ * // ...
23673
+ * })
23674
+ * ```
23675
+ *
23676
+ * Fired when a {@link Amity.InternalPost} has been reacted
23677
+ *
23678
+ * @param callback The function to call when the event was fired
23679
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
23680
+ *
23681
+ * @category Post Events
23682
+ */
23683
+ const onLocalPostReactionRemoved = (callback) => {
23684
+ const client = getActiveClient();
23685
+ const filter = ({ post }) => {
23686
+ if (!client.cache) {
23687
+ callback(post);
23688
+ }
23689
+ else {
23690
+ upsertInCache(['post', 'get', post.postId], post);
23691
+ callback(post);
23692
+ }
23693
+ };
23694
+ return createEventSubscriber(client, 'local.post.removeReaction', 'local.post.removeReaction', filter);
23695
+ };
23696
+
23697
+ /**
23698
+ * ```js
23699
+ * import { onPostUpdatedLocal } from '@amityco/ts-sdk'
23700
+ * const dispose = onPostUpdatedLocal(post => {
23701
+ * // ...
23702
+ * })
23703
+ * ```
23704
+ *
23705
+ * Fired when a {@link Amity.InternalPost} has been updated
23706
+ *
23707
+ * @param callback The function to call when the event was fired
23708
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
23709
+ *
23710
+ * @category Post Events
23711
+ */
23712
+ const onPostUpdatedLocal = (callback) => createLocalPostEventSubscriber('local.post.updated', callback);
23713
+
23714
+ const commentEventHanlder$1 = (callback, eventHandler, cacheKey) => {
23715
+ return eventHandler(async (comment) => {
23716
+ var _a;
23717
+ const currentCollection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
23718
+ if (!currentCollection || !currentCollection.data.includes(comment.referenceId))
23719
+ return;
23720
+ await getPost$1(comment.referenceId);
23721
+ callback(comment);
23722
+ });
23723
+ };
23724
+ const generateCommentSubscriptions$1 = (cacheKey) => {
23725
+ const eventHandlers = [
23726
+ onCommentCreated,
23727
+ onCommentDeleted,
23728
+ onCommentReactionAdded,
23729
+ onCommentReactionRemoved,
23730
+ onCommentCreatedLocal,
23731
+ onCommentDeleteLocal,
23732
+ onLocalCommentReactionAdded,
23733
+ onLocalCommentReactionRemoved,
23734
+ ];
23735
+ return eventHandlers.map(handler => ({
23736
+ fn: convertEventPayload((callback) => commentEventHanlder$1(callback, handler, cacheKey), 'referenceId', 'post'),
23737
+ action: EnumPostActions.OnPostUpdated,
23738
+ }));
23739
+ };
23740
+ const getGlobalFeedSubscriptions = (cacheKey) => {
23741
+ return [
23742
+ { fn: onPostCreated, action: EnumPostActions.OnPostCreated },
23743
+ { fn: onPostUpdated, action: EnumPostActions.OnPostUpdated },
23744
+ { fn: onPostUpdatedLocal, action: EnumPostActions.OnPostUpdated },
23745
+ { fn: onPostDeleted, action: EnumPostActions.OnPostDeleted },
23746
+ { fn: onPostFlagged, action: EnumPostActions.OnPostFlagged },
23747
+ { fn: onPostUnflagged, action: EnumPostActions.OnPostUnflagged },
23748
+ { fn: onPostApproved, action: EnumPostActions.OnPostApproved },
23749
+ { fn: onPostDeclined, action: EnumPostActions.OnPostDeclined },
23750
+ { fn: onPostReactionAdded, action: EnumPostActions.OnPostReactionAdded },
23751
+ { fn: onPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved },
23752
+ { fn: onLocalPostReactionAdded, action: EnumPostActions.OnPostReactionAdded },
23753
+ { fn: onLocalPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved },
23754
+ { fn: onLocalPostDeleted, action: EnumPostActions.OnPostDeleted },
23755
+ ...generateCommentSubscriptions$1(cacheKey),
23756
+ ];
23757
+ };
23758
+
23759
+ class CustomRankingGlobalFeedLiveCollectionController extends LiveCollectionController {
23760
+ constructor(query, callback) {
23761
+ const queryStreamId = hash__default["default"](query);
23762
+ const cacheKey = ['customRankingGlobalFeed', 'collection', queryStreamId];
23763
+ const paginationController = new CustomRankingGlobalFeedPaginationController(query);
23764
+ super(paginationController, queryStreamId, cacheKey, callback);
23765
+ this.query = query;
23766
+ this.queryStreamController = new CustomRankingGlobalFeedQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this), preparePostPayload);
23767
+ this.callback = callback.bind(this);
23768
+ this.loadPage({ initial: true });
23769
+ }
23770
+ setup() {
23771
+ var _a;
23772
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
23773
+ if (!collection) {
23774
+ pushToCache(this.cacheKey, {
23775
+ data: [],
23776
+ params: {},
23777
+ });
23778
+ }
23779
+ }
23780
+ async persistModel(queryPayload) {
23781
+ await this.queryStreamController.saveToMainDB(queryPayload);
23782
+ }
23783
+ persistQueryStream({ response, direction, refresh, }) {
23784
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
23785
+ }
23786
+ startSubscription() {
23787
+ return this.queryStreamController.subscribeRTE(getGlobalFeedSubscriptions(this.cacheKey));
23788
+ }
23789
+ notifyChange({ origin, loading, error }) {
23790
+ var _a, _b;
23791
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
23792
+ if (!collection)
23793
+ return;
23794
+ const data = ((_b = collection.data
23795
+ .map(id => pullFromCache(['post', 'get', id]))
23796
+ .filter(isNonNullable)
23797
+ .map(({ data }) => data)) !== null && _b !== void 0 ? _b : []).map(LinkedObject.post);
23798
+ if (!this.shouldNotify(data) && origin === 'event')
23799
+ return;
23800
+ this.callback({
23801
+ onNextPage: () => this.loadPage({ direction: "next" /* Amity.LiveCollectionPageDirection.NEXT */ }),
23802
+ data,
23803
+ hasNextPage: !!this.paginationController.getNextToken(),
23804
+ loading,
23805
+ error,
23806
+ });
23807
+ }
23808
+ }
23809
+
23810
+ /* begin_public_function
23811
+ id: post.query
23812
+ */
23813
+ /**
23814
+ * ```js
23815
+ * import { PostRepository } from '@amityco/ts-sdk'
23816
+ *
23817
+ * let posts = []
23818
+ * const unsub = PostRepository.getPosts({
23819
+ * targetType: Amity.PostTargetType,
23820
+ * targetId: Amity.Post['targetId'],
23821
+ * }, response => merge(posts, response.data))
23822
+ * ```
23823
+ *
23824
+ * Observe all mutations on a list of {@link Amity.Post} for a given target object
23825
+ *
23826
+ * @param params.targetType the type of the target
23827
+ * @param params.targetId the ID of the target
23828
+ * @param callback the function to call when new data are available
23829
+ * @param config
23830
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
23831
+ *
23832
+ * @category Posts Live Collection
23833
+ */
23834
+ const getCustomRankingGlobalFeed = (params, callback, config) => {
23835
+ const { log, cache } = getActiveClient();
23836
+ if (!cache) {
23837
+ console.log(ENABLE_CACHE_MESSAGE);
23838
+ }
23839
+ const timestamp = Date.now();
23840
+ log(`getCustomRankingGlobalFeed(tmpid: ${timestamp}) > listen`);
23841
+ const liveCollection = new CustomRankingGlobalFeedLiveCollectionController(params, callback);
23842
+ const disposers = liveCollection.startSubscription();
23843
+ const cacheKey = liveCollection.getCacheKey();
23844
+ disposers.push(() => dropFromCache(cacheKey));
23845
+ return () => {
23846
+ log(`getCustomRankingGlobalFeed(tmpid: ${timestamp}) > dispose`);
23847
+ disposers.forEach(fn => fn());
23848
+ };
23849
+ };
23850
+ /* end_public_function */
23851
+
23852
+ class GlobalFeedPaginationController extends PaginationController {
23853
+ async getRequest(queryParams, token) {
23854
+ const { limit, resolveParent } = queryParams, params = __rest(queryParams, ["limit", "resolveParent"]);
23855
+ const options = token ? { token } : { limit };
23856
+ const { data: queryResponse } = await this.http.get(`/api/v4/me/global-feeds`, {
23857
+ params: Object.assign(Object.assign({}, params), { resolveParent: resolveParent !== null && resolveParent !== void 0 ? resolveParent : true, options }),
23858
+ });
23859
+ return queryResponse;
23860
+ }
23861
+ }
23862
+
23863
+ class GlobalFeedQueryStreamController extends QueryStreamController {
23864
+ constructor(query, cacheKey, notifyChange, preparePayload) {
23865
+ super(query, cacheKey);
23866
+ this.notifyChange = notifyChange;
23867
+ this.preparePayload = preparePayload;
23868
+ }
23869
+ async saveToMainDB(response) {
23870
+ const processedPayload = await this.preparePayload(response);
23871
+ const client = getActiveClient();
23872
+ const cachedAt = client.cache && Date.now();
23873
+ if (client.cache) {
23874
+ ingestInCache(processedPayload, { cachedAt });
23875
+ }
23876
+ }
23877
+ appendToQueryStream(response, direction, refresh = false) {
23878
+ var _a, _b;
23879
+ if (refresh) {
23880
+ pushToCache(this.cacheKey, {
23881
+ data: response.posts.map(getResolver('post')),
23882
+ });
23883
+ }
23884
+ else {
23885
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
23886
+ const posts = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
23887
+ pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: [...new Set([...posts, ...response.posts.map(getResolver('post'))])] }));
23888
+ }
23889
+ }
23890
+ reactor(action) {
23891
+ return (post) => {
23892
+ var _a;
23893
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
23894
+ if (!collection)
23895
+ return;
23896
+ if (post.parentPostId && !collection.data.includes(post.parentPostId))
23897
+ return;
23898
+ // Delete Action
23899
+ if (action === EnumPostActions.OnPostDeleted) {
23900
+ // Parent Post - Remove from collection
23901
+ collection.data = collection.data.filter(postId => postId !== post.postId);
23902
+ }
23903
+ if (action === EnumPostActions.OnPostCreated || action === EnumPostActions.OnPostApproved) {
23904
+ // If the query has dataTypes array with value, check if post.dataType is include in the array
23905
+ if (this.query.dataTypes && this.query.dataTypes.length > 0) {
23906
+ if (!this.query.dataTypes.includes(post.dataType))
23907
+ return;
23908
+ }
23909
+ collection.data = [...new Set([post.postId, ...collection.data])];
23910
+ }
23911
+ if (action === EnumPostActions.OnPostDeclined) {
23912
+ collection.data = collection.data.filter(postId => postId !== post.postId);
23913
+ }
23914
+ pushToCache(this.cacheKey, collection);
23915
+ this.notifyChange({ origin: "event" /* Amity.LiveDataOrigin.EVENT */, loading: false });
23916
+ };
23917
+ }
23918
+ subscribeRTE(createSubscriber) {
23919
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
23920
+ }
23921
+ }
23922
+
23923
+ class GlobalFeedLiveCollectionController extends LiveCollectionController {
23924
+ constructor(query, callback) {
23925
+ const queryStreamId = hash__default["default"](query);
23926
+ const cacheKey = ['globalFeed', 'collection', queryStreamId];
23927
+ const paginationController = new GlobalFeedPaginationController(query);
23928
+ super(paginationController, queryStreamId, cacheKey, callback);
23929
+ this.query = query;
23930
+ this.queryStreamController = new GlobalFeedQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this), preparePostPayload);
23931
+ this.callback = callback.bind(this);
23932
+ this.loadPage({ initial: true });
23933
+ }
23934
+ setup() {
23935
+ var _a;
23936
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
23937
+ if (!collection) {
23938
+ pushToCache(this.cacheKey, {
23939
+ data: [],
23940
+ params: {},
23941
+ });
23942
+ }
23943
+ }
23944
+ async persistModel(queryPayload) {
23945
+ await this.queryStreamController.saveToMainDB(queryPayload);
23946
+ }
23947
+ persistQueryStream({ response, direction, refresh, }) {
23948
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
23949
+ }
23950
+ startSubscription() {
23951
+ return this.queryStreamController.subscribeRTE(getGlobalFeedSubscriptions(this.cacheKey));
23952
+ }
23953
+ notifyChange({ origin, loading, error }) {
23954
+ var _a, _b;
23955
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
23956
+ if (!collection)
23957
+ return;
23958
+ const data = ((_b = collection.data
23959
+ .map(id => pullFromCache(['post', 'get', id]))
23960
+ .filter(isNonNullable)
23961
+ .map(({ data }) => data)) !== null && _b !== void 0 ? _b : []).map(LinkedObject.post);
23962
+ if (!this.shouldNotify(data) && origin === 'event')
23963
+ return;
23964
+ this.callback({
23965
+ onNextPage: () => this.loadPage({ direction: "next" /* Amity.LiveCollectionPageDirection.NEXT */ }),
23966
+ data,
23967
+ hasNextPage: !!this.paginationController.getNextToken(),
23968
+ loading,
23969
+ error,
23970
+ });
23971
+ }
23972
+ }
23973
+
23974
+ /* begin_public_function
23975
+ id: post.query
23976
+ */
23977
+ /**
23978
+ * ```js
23979
+ * import { PostRepository } from '@amityco/ts-sdk'
23980
+ *
23981
+ * let posts = []
23982
+ * const unsub = PostRepository.getPosts({
23983
+ * targetType: Amity.PostTargetType,
23984
+ * targetId: Amity.Post['targetId'],
23985
+ * }, response => merge(posts, response.data))
23986
+ * ```
23987
+ *
23988
+ * Observe all mutations on a list of {@link Amity.Post} for a given target object
23989
+ * @param params.targetType the type of the target
23990
+ * @param params.targetId the ID of the target
23991
+ * @param callback the function to call when new data are available
23992
+ * @param config
23993
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
23994
+ *
23995
+ * @category Posts Live Collection
23996
+ */
23997
+ const getGlobalFeed = (params, callback, config) => {
23998
+ const { log, cache } = getActiveClient();
23999
+ if (!cache) {
24000
+ console.log(ENABLE_CACHE_MESSAGE);
24001
+ }
24002
+ const timestamp = Date.now();
24003
+ log(`getGlobalFeed(tmpid: ${timestamp}) > listen`);
24004
+ const liveCollection = new GlobalFeedLiveCollectionController(params, callback);
24005
+ const disposers = liveCollection.startSubscription();
24006
+ const cacheKey = liveCollection.getCacheKey();
24007
+ disposers.push(() => dropFromCache(cacheKey));
24008
+ return () => {
24009
+ log(`getGlobalFeed(tmpid: ${timestamp}) > dispose`);
24010
+ disposers.forEach(fn => fn());
24011
+ };
24012
+ };
24013
+ /* end_public_function */
24014
+
24015
+ var index$8 = /*#__PURE__*/Object.freeze({
24016
+ __proto__: null,
24017
+ getCustomRankingGlobalFeed: getCustomRankingGlobalFeed,
24018
+ getGlobalFeed: getGlobalFeed
24019
+ });
24020
+
24021
+ /* begin_public_function
24022
+ id: post.get_by_ids
24023
+ */
24024
+ /**
24025
+ * ```js
24026
+ * import { getPostByIds } from '@amityco/ts-sdk'
24027
+ * const { data: posts } = await getPostByIds(['foo', 'bar'])
24028
+ * ```
24029
+ *
24030
+ * Fetches a collection of {@link Amity.Post} objects
24031
+ *
24032
+ * @param postIds the IDs of the {@link Amity.Post} to fetch
24033
+ * @returns the associated collection of {@link Amity.Post} objects
24034
+ *
24035
+ * @category Post API
24036
+ * @async
24037
+ */
24038
+ const getPostByIds = async (postIds) => {
24039
+ const client = getActiveClient();
24040
+ client.log('post/getPostByIds', postIds);
24041
+ const encodedPostIds = postIds.map(postId => encodeURIComponent(postId));
24042
+ let payload;
24043
+ try {
24044
+ // API-FIX: endpoint should not be /list, parameters should be querystring.
24045
+ const response = await client.http.get(`/api/v3/posts/list`, {
24046
+ params: { postIds: encodedPostIds },
24047
+ });
24048
+ payload = response.data;
24049
+ }
24050
+ catch (error) {
24051
+ postIds.forEach(postId => {
24052
+ if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
24053
+ pushToTombstone('post', postId);
24054
+ }
24055
+ });
24056
+ throw error;
24057
+ }
24058
+ const data = prepareMembershipPayload(payload, 'communityUsers');
24059
+ const cachedAt = client.cache && Date.now();
24060
+ if (client.cache)
24061
+ ingestInCache(data, { cachedAt });
24062
+ return {
24063
+ data: data.posts.map(LinkedObject.post),
24064
+ cachedAt,
24065
+ };
24066
+ };
24067
+ /* end_public_function */
24068
+ /**
24069
+ * ```js
24070
+ * import { getPostByIds } from '@amityco/ts-sdk'
24071
+ * const { data: posts } = getPostByIds.locally(['foo', 'bar'])
24072
+ * ```
24073
+ *
24074
+ * Fetches a collection of {@link Amity.Post} objects from cache
24075
+ *
24076
+ * @param postIds the IDs of the {@link Amity.Post} to fetch
24077
+ * @returns the associated collection of {@link Amity.Post} objects
24078
+ *
24079
+ * @category Post API
24080
+ */
24081
+ getPostByIds.locally = (postIds) => {
24082
+ var _a;
24083
+ const client = getActiveClient();
24084
+ client.log('post/getPostByIds.locally', postIds);
24085
+ if (!client.cache)
24086
+ return;
24087
+ const cached = postIds
24088
+ .map(postId => pullFromCache(['post', 'get', postId]))
24089
+ .filter(Boolean);
24090
+ const posts = cached.map(({ data }) => data);
24091
+ const oldest = (_a = cached.sort((a, b) => (a.cachedAt < b.cachedAt ? -1 : 1))) === null || _a === void 0 ? void 0 : _a[0];
24092
+ if ((cached === null || cached === void 0 ? void 0 : cached.length) < postIds.length)
24093
+ return;
24094
+ return {
24095
+ data: posts.map(LinkedObject.post),
24096
+ cachedAt: oldest.cachedAt,
24097
+ };
24098
+ };
24099
+
24100
+ /* begin_public_function
24101
+ id: post.create.text_post, post.create.image_post, post.create.file_post, post.create.video_post, post.create.poll_post, post.create.livestream_post, post.create.custom_post
24102
+ */
24103
+ /**
24104
+ * ```js
24105
+ * import { PostRepository } from '@amityco/ts-sdk'
24106
+ * const created = await PostRepository.createPost({
24107
+ * targetType: 'user',
24108
+ * targetId: 'foobar',
24109
+ * data: { text: 'hello world' }
24110
+ * }))
24111
+ * ```
24112
+ *
24113
+ * Creates an {@link Amity.Post}
24114
+ *
24115
+ * @param bundle The data necessary to create a new {@link Amity.Post}
24116
+ * @returns The newly created {@link Amity.Post}
24117
+ *
24118
+ * @category Post API
24119
+ * @async
24120
+ */
24121
+ const createPost = async (bundle) => {
24122
+ const client = getActiveClient();
24123
+ client.log('post/createPost', bundle);
24124
+ if (!bundle.dataType || ['text', 'image', 'file', 'video'].includes(bundle.dataType)) {
24125
+ // eslint-disable-next-line no-param-reassign
24126
+ delete bundle.dataType;
24127
+ }
24128
+ const { data: payload } = await client.http.post('/api/v4/posts', bundle);
24129
+ fireEvent('post.created', payload);
24130
+ const data = prepareMembershipPayload(payload, 'communityUsers');
24131
+ const cachedAt = client.cache && Date.now();
24132
+ if (client.cache)
24133
+ ingestInCache(data, { cachedAt });
24134
+ const { posts } = data;
24135
+ return {
24136
+ data: LinkedObject.post(posts[0]),
24137
+ cachedAt,
24138
+ };
24139
+ };
24140
+ /* end_public_function */
24141
+
24142
+ /* begin_public_function
24143
+ id: post.edit, post.edit.custom_post
24144
+ */
24145
+ /**
24146
+ * ```js
24147
+ * import { PostRepository } from '@amityco/ts-sdk'
24148
+ * const updated = await PostRepository.editPost(postId, {
24149
+ * data: { text: 'hello world' }
24150
+ * })
24151
+ * ```
24152
+ *
24153
+ * Updates an {@link Amity.Post}
24154
+ *
24155
+ * @param postId The ID of the {@link Amity.Post} to edit
24156
+ * @param patch The patch data to apply
24157
+ * @returns the updated {@link Amity.Post} object
24158
+ *
24159
+ * @category Post API
24160
+ * @async
24161
+ */
24162
+ const editPost = async (postId, patch) => {
24163
+ const client = getActiveClient();
24164
+ client.log('user/editPost', patch);
24165
+ const { data: payload } = await client.http.put(`/api/v4/posts/${encodeURIComponent(postId)}`, patch);
24166
+ const data = prepareMembershipPayload(payload, 'communityUsers');
24167
+ const cachedAt = client.cache && Date.now();
24168
+ if (client.cache)
24169
+ ingestInCache(data, { cachedAt });
24170
+ fireEvent('local.post.updated', data);
24171
+ const { posts } = data;
24172
+ return {
24173
+ data: LinkedObject.post(posts.find(post => post.postId === postId)),
24174
+ cachedAt,
24175
+ };
24176
+ };
24177
+ /* end_public_function */
24178
+
24179
+ /**
24180
+ * ```js
24181
+ * import { deletePost } from '@amityco/ts-sdk'
24182
+ * const success = await deletePost('foobar')
24183
+ * ```
24184
+ *
24185
+ * Deletes a {@link Amity.Post}
24186
+ *
24187
+ * @param postId The {@link Amity.Post} ID to delete
24188
+ * @return A success boolean if the {@link Amity.Post} was deleted
24189
+ *
24190
+ * @private
24191
+ * @async
24192
+ */
24193
+ const deletePost = async (postId, permanent = false) => {
24194
+ var _a;
24195
+ const client = getActiveClient();
24196
+ const post = await getPost$2(postId);
24197
+ await client.http.delete(`/api/v4/posts/${encodeURIComponent(postId)}`, {
24198
+ params: {
24199
+ postId,
24200
+ permanent,
24201
+ },
24202
+ });
24203
+ // there is currently a limitation which doesn't allow us to fire event to tell that community
24204
+ // has been updated. reason is that, when the object is deleted, we don't have its `communityId`
24205
+ // and so we cannot refetch the community or advertise on events. hopefully this should be solved
24206
+ // later when realtime events covers that for us.
24207
+ if (post.data.targetType === 'community') {
24208
+ const community = await getCommunity$1(post.data.targetId);
24209
+ const communityUsersCache = (_a = queryCache(['communityUsers', 'get'])) !== null && _a !== void 0 ? _a : [];
24210
+ const communityUsers = communityUsersCache
24211
+ .filter(({ key }) => {
24212
+ // cache key is ['communityUsers', 'get', `${communityId}#`${userId}`}]
24213
+ if (key[0] !== 'communityUsers')
24214
+ return false;
24215
+ if (key[1] !== 'get')
24216
+ return false;
24217
+ if (typeof key[2] === 'string')
24218
+ return key[2].includes(community.data.communityId);
24219
+ return false;
24220
+ })
24221
+ .map(({ data }) => data);
24222
+ fireEvent('community.updated', {
24223
+ communities: [community.data],
24224
+ categories: [],
24225
+ communityUsers,
24226
+ feeds: [],
24227
+ files: [],
24228
+ users: [],
24229
+ });
24230
+ }
24231
+ // to support hard deletion
24232
+ const deleted = Object.assign(Object.assign({}, post.data), { isDeleted: true });
24233
+ if (permanent) {
24234
+ setTimeout(() => {
24235
+ pushToTombstone('post', postId);
24236
+ }, 0);
24237
+ }
24238
+ else {
24239
+ upsertInCache(['post', 'get', postId], { isDeleted: true });
24240
+ }
24241
+ fireEvent('local.post.deleted', {
24242
+ posts: [deleted],
24243
+ categories: [],
24244
+ comments: [],
24245
+ communities: [],
24246
+ communityUsers: [],
24247
+ feeds: [],
24248
+ files: [],
24249
+ postChildren: [],
24250
+ users: [],
24251
+ videoStreamings: [],
24252
+ });
24253
+ return LinkedObject.post(deleted);
24254
+ };
24255
+
24256
+ /* begin_public_function
24257
+ id: post.soft_delete
24258
+ */
24259
+ /**
24260
+ * ```js
24261
+ * import { PostRepository } from '@amityco/ts-sdk'
24262
+ * const success = await PostRepository.softDeletePost('foobar')
24263
+ * ```
24264
+ *
24265
+ * Soft deletes a {@link Amity.Post}
24266
+ *
24267
+ * @param postId The {@link Amity.Post} ID to soft delete
24268
+ * @return A success boolean if the {@link Amity.Post} was deleted
24269
+ *
24270
+ * @category Post API
24271
+ * @async
24272
+ */
24273
+ const softDeletePost = async (postId) => {
24274
+ const client = getActiveClient();
24275
+ client.log('post/softDeletePost', postId);
24276
+ const softDeleted = await deletePost(postId, false);
24277
+ return LinkedObject.post(softDeleted);
24278
+ };
24279
+ /* end_public_function */
24280
+
24281
+ /* begin_public_function
24282
+ id: post.hard_delete
24283
+ */
24284
+ /**
24285
+ * ```js
24286
+ * import { hardDeletePost } from '@amityco/ts-sdk'
24287
+ * const success = await hardDeletePost('foobar')
24288
+ * ```
24289
+ *
24290
+ * Hard deletes a {@link Amity.Post}
24291
+ *
24292
+ * @param postId The {@link Amity.Post} ID to be hard delete
24293
+ * @return A success boolean if the {@link Amity.Post} was deleted
24294
+ *
24295
+ * @category Post API
24296
+ * @async
24297
+ */
24298
+ const hardDeletePost = async (postId) => {
24299
+ const client = getActiveClient();
24300
+ client.log('post/hardDeletePost', postId);
24301
+ const hardDeleted = await deletePost(postId, true);
24302
+ return LinkedObject.post(hardDeleted);
24303
+ };
24304
+ /* end_public_function */
24305
+
24306
+ /* begin_public_function
24307
+ id: post.approve
24308
+ */
24309
+ /**
24310
+ * ```js
24311
+ * import { approvePost } from '@amityco/ts-sdk'
24312
+ *
24313
+ * const { data: post } = await approvePost('postId')
24314
+ * ```
24315
+ *
24316
+ * Approves a {@link Amity.Post}
24317
+ *
24318
+ * @param postId The {@link Amity.Post} ID to be approved
24319
+ * @return A {@link Amity.Post} that was approved
24320
+ *
24321
+ * @category Post API
24322
+ * @async
24323
+ */
24324
+ const approvePost = async (postId) => {
24325
+ const client = getActiveClient();
24326
+ client.log('post/approvePost', postId);
24327
+ const { data: payload } = await client.http.post(`/api/v3/posts/${encodeURIComponent(postId)}/approve`);
24328
+ fireEvent('post.approved', payload);
24329
+ // fire virtual event for community update
24330
+ if (payload.posts[0].targetType === 'community') {
24331
+ fireEvent('community.updated', payload);
24332
+ }
24333
+ const data = prepareMembershipPayload(payload, 'communityUsers');
24334
+ const cachedAt = client.cache && Date.now();
24335
+ if (client.cache)
24336
+ ingestInCache(data, { cachedAt });
24337
+ return {
24338
+ data: LinkedObject.post(data.posts.find(post => post.postId === postId)),
24339
+ cachedAt,
24340
+ };
24341
+ };
24342
+ /* end_public_function */
24343
+
24344
+ /* begin_public_function
24345
+ id: post.decline
24346
+ */
24347
+ /**
24348
+ * ```js
24349
+ * import { declinePost } from '@amityco/ts-sdk'
24350
+ *
24351
+ * const {data: post} = await declinePost('postId')
24352
+ * ```
24353
+ *
24354
+ * Declines a {@link Amity.Post}
24355
+ *
24356
+ * @param postId The {@link Amity.Post} ID to be declined
24357
+ * @return A {@link Amity.Post} that was declined
24358
+ *
24359
+ * @category Post API
24360
+ * @async
24361
+ */
24362
+ const declinePost = async (postId) => {
24363
+ const client = getActiveClient();
24364
+ client.log('post/declinePost', postId);
24365
+ const { data: payload } = await client.http.post(`/api/v3/posts/${encodeURIComponent(postId)}/decline`);
24366
+ // fire virtual event
24367
+ if (payload.posts[0].targetType === 'community') {
24368
+ fireEvent('community.updated', payload);
24369
+ }
24370
+ fireEvent('post.declined', payload);
24371
+ const data = prepareMembershipPayload(payload, 'communityUsers');
24372
+ const cachedAt = client.cache && Date.now();
24373
+ if (client.cache)
24374
+ ingestInCache(data, { cachedAt });
24375
+ return {
24376
+ data: LinkedObject.post(payload.posts.find(post => post.postId === postId)),
24377
+ cachedAt,
24378
+ };
24379
+ };
24380
+ /* end_public_function */
24381
+
24382
+ /* begin_public_function
24383
+ id: post.flag
24384
+ */
24385
+ /**
24386
+ * ```js
24387
+ * import { PostRepository } from '@amityco/ts-sdk'
24388
+ * const flagged = await PostRepository.flagPost(postId, reason)
24026
24389
  * ```
24027
24390
  *
24028
- * Fired when a {@link Amity.InternalPost} has been updated
24029
- *
24030
- * @param callback The function to call when the event was fired
24031
- * @returns an {@link Amity.Unsubscriber} function to stop listening
24391
+ * @param postId of the post to flag
24392
+ * @param reason the reason to flag the post
24393
+ * @returns a boolean
24032
24394
  *
24033
- * @category Post Events
24034
- */
24035
- const onPostUpdatedLocal = (callback) => createLocalPostEventSubscriber('local.post.updated', callback);
24395
+ * @category Post API
24396
+ * @async
24397
+ * */
24398
+ const flagPost = async (postId, reason) => {
24399
+ const client = getActiveClient();
24400
+ client.log('post/flagPost', postId);
24401
+ const isPredefinedReason = reason &&
24402
+ Object.entries(exports.ContentFlagReasonEnum).some(([key, value]) => key !== exports.ContentFlagReasonEnum.Others && value === reason);
24403
+ const body = {
24404
+ reason: reason && isPredefinedReason ? reason : exports.ContentFlagReasonEnum.Others,
24405
+ detail: reason && !isPredefinedReason ? reason : '',
24406
+ };
24407
+ const { data: payload } = await client.http.post(`/api/v3/posts/${encodeURIComponent(postId)}/flag`, body);
24408
+ if (client.cache) {
24409
+ ingestInCache(prepareMembershipPayload(payload, 'communityUsers'));
24410
+ }
24411
+ fireEvent('post.flagged', payload);
24412
+ return !!payload;
24413
+ };
24414
+ /* end_public_function */
24036
24415
 
24416
+ /* begin_public_function
24417
+ id: post.unflag
24418
+ */
24037
24419
  /**
24038
24420
  * ```js
24039
- * import { onLocalPostReactionAdded } from '@amityco/ts-sdk'
24040
- * const dispose = onPostReactionAdded(post => {
24041
- * // ...
24042
- * })
24421
+ * import { PostRepository } from '@amityco/ts-sdk'
24422
+ * const unflagged = await PostRepository.unflagPost(postId)
24043
24423
  * ```
24044
24424
  *
24045
- * Fired when a {@link Amity.InternalPost} has been reacted
24046
- *
24047
- * @param callback The function to call when the event was fired
24048
- * @returns an {@link Amity.Unsubscriber} function to stop listening
24425
+ * @param postId of the post to unflag
24426
+ * @returns the unflag post result
24049
24427
  *
24050
- * @category Post Events
24051
- */
24052
- const onLocalPostReactionAdded = (callback) => {
24428
+ * @category Post API
24429
+ * @async
24430
+ * */
24431
+ const unflagPost = async (postId) => {
24053
24432
  const client = getActiveClient();
24054
- const filter = ({ post }) => {
24055
- if (!client.cache) {
24056
- callback(post);
24057
- }
24058
- else {
24059
- upsertInCache(['post', 'get', post.postId], post);
24060
- callback(post);
24061
- }
24062
- };
24063
- return createEventSubscriber(client, 'local.post.addReaction', 'local.post.addReaction', filter);
24064
- };
24433
+ client.log('post/unflagPost', postId);
24434
+ const { data: payload } = await client.http.delete(`/api/v3/posts/${encodeURIComponent(postId)}/unflag`);
24435
+ if (client.cache) {
24436
+ ingestInCache(prepareMembershipPayload(payload, 'communityUsers'));
24437
+ }
24438
+ fireEvent('post.unflagged', payload);
24439
+ return !!payload;
24440
+ };
24441
+ /* end_public_function */
24065
24442
 
24443
+ /* begin_public_function
24444
+ id: post.check_flag_by_me
24445
+ */
24066
24446
  /**
24067
24447
  * ```js
24068
- * import { onLocalPostReactionRemoved } from '@amityco/ts-sdk'
24069
- * const dispose = onPostReactionRemoved(post => {
24070
- * // ...
24071
- * })
24448
+ * import { PostRepository } from '@amityco/ts-sdk'
24449
+ * const isReported = await PostRepository.isPostFlaggedByMe('post', postId)
24072
24450
  * ```
24073
24451
  *
24074
- * Fired when a {@link Amity.InternalPost} has been reacted
24075
- *
24076
- * @param callback The function to call when the event was fired
24077
- * @returns an {@link Amity.Unsubscriber} function to stop listening
24452
+ * @param postId of the post to check if flagged by current user
24453
+ * @returns `true` if the post is flagged by me, `false` if doesn't.
24078
24454
  *
24079
- * @category Post Events
24080
- */
24081
- const onLocalPostReactionRemoved = (callback) => {
24455
+ * @category Post API
24456
+ * @async
24457
+ * */
24458
+ const isPostFlaggedByMe = async (postId) => {
24082
24459
  const client = getActiveClient();
24083
- const filter = ({ post }) => {
24084
- if (!client.cache) {
24085
- callback(post);
24086
- }
24087
- else {
24088
- upsertInCache(['post', 'get', post.postId], post);
24089
- callback(post);
24090
- }
24091
- };
24092
- return createEventSubscriber(client, 'local.post.removeReaction', 'local.post.removeReaction', filter);
24093
- };
24460
+ client.log('post/isPostFlaggedByMe', postId);
24461
+ const { data: { result }, } = await client.http.get(`/api/v3/posts/${postId}/isflagbyme`);
24462
+ return result;
24463
+ };
24464
+ /* end_public_function */
24094
24465
 
24466
+ /* begin_public_function
24467
+ id: post.create.clip_post
24468
+ */
24095
24469
  /**
24096
24470
  * ```js
24097
- * import { onLocalPostDeleted } from '@amityco/ts-sdk'
24098
- * const dispose = onLocalPostDeleted(post => {
24099
- * // ...
24100
- * })
24471
+ * import { PostRepository } from '@amityco/ts-sdk'
24472
+ * const created = await PostRepository.createClipPost({
24473
+ * targetType: 'user',
24474
+ * targetId: 'foobar',
24475
+ * dataType: 'clip',
24476
+ * data: { text: 'hello world' },
24477
+ * attachments: [{ type: 'clip', fileId: 'fileId123', displayMode: 'fill', isMuted: false }]
24478
+ * }))
24101
24479
  * ```
24102
24480
  *
24103
- * Fired when a {@link Amity.InternalPost} has been deleted
24481
+ * Creates an {@link Amity.Post}
24104
24482
  *
24105
- * @param callback The function to call when the event was fired
24106
- * @returns an {@link Amity.Unsubscriber} function to stop listening
24483
+ * @param bundle The data necessary to create a new {@link Amity.Post}
24484
+ * @returns The newly created {@link Amity.Post}
24107
24485
  *
24108
- * @category Post Events
24486
+ * @category Post API
24487
+ * @async
24109
24488
  */
24110
- const onLocalPostDeleted = (callback) => createLocalPostEventSubscriber('local.post.deleted', callback);
24489
+ const createClipPost = async (bundle) => {
24490
+ const client = getActiveClient();
24491
+ client.log('post/createPost', bundle);
24492
+ const { data: payload } = await client.http.post('/api/v4/posts', bundle);
24493
+ fireEvent('post.created', payload);
24494
+ const data = prepareMembershipPayload(payload, 'communityUsers');
24495
+ const cachedAt = client.cache && Date.now();
24496
+ if (client.cache)
24497
+ ingestInCache(data, { cachedAt });
24498
+ const { posts } = data;
24499
+ return {
24500
+ data: LinkedObject.post(posts[0]),
24501
+ cachedAt,
24502
+ };
24503
+ };
24504
+ /* end_public_function */
24111
24505
 
24112
24506
  /* begin_public_function
24113
24507
  id: post.get
@@ -24131,7 +24525,7 @@ const onLocalPostDeleted = (callback) => createLocalPostEventSubscriber('local.p
24131
24525
  *
24132
24526
  * @category Post Live Object
24133
24527
  */
24134
- const getPost$1 = (postId, callback) => {
24528
+ const getPost = (postId, callback) => {
24135
24529
  const responder = (snapshot) => {
24136
24530
  const { data } = snapshot;
24137
24531
  callback(Object.assign(Object.assign({}, snapshot), { data: data ? LinkedObject.post(snapshot.data) : data }));
@@ -24205,7 +24599,7 @@ class PostPaginationController extends PaginationController {
24205
24599
  type: params.sortBy || queryParams.limit ? 'pagination' : undefined,
24206
24600
  };
24207
24601
  const options = token ? Object.assign(Object.assign({}, baseOptions), { token }) : Object.assign(Object.assign({}, baseOptions), { limit });
24208
- const { data: queryResponse } = await this.http.get(`/api/v4/posts`, {
24602
+ const { data: queryResponse } = await this.http.get(`/api/v5/posts`, {
24209
24603
  params: Object.assign(Object.assign({}, params), { isDeleted: inferIsDeleted(includeDeleted),
24210
24604
  /*
24211
24605
  * when creating post like image, file, video BE will create 2 posts
@@ -24220,19 +24614,6 @@ class PostPaginationController extends PaginationController {
24220
24614
  }
24221
24615
  }
24222
24616
 
24223
- var EnumPostActions;
24224
- (function (EnumPostActions) {
24225
- EnumPostActions["OnPostCreated"] = "onPostCreated";
24226
- EnumPostActions["OnPostUpdated"] = "onPostUpdated";
24227
- EnumPostActions["OnPostDeleted"] = "onPostDeleted";
24228
- EnumPostActions["OnPostFlagged"] = "onPostFlagged";
24229
- EnumPostActions["OnPostUnflagged"] = "onPostUnflagged";
24230
- EnumPostActions["OnPostReactionAdded"] = "onPostReactionAdded";
24231
- EnumPostActions["OnPostReactionRemoved"] = "onPostReactionRemoved";
24232
- EnumPostActions["OnPostApproved"] = "onPostApproved";
24233
- EnumPostActions["OnPostDeclined"] = "onPostDeclined";
24234
- })(EnumPostActions || (EnumPostActions = {}));
24235
-
24236
24617
  class PostQueryStreamController extends QueryStreamController {
24237
24618
  constructor(query, cacheKey, notifyChange, preparePayload) {
24238
24619
  super(query, cacheKey);
@@ -24298,45 +24679,50 @@ class PostQueryStreamController extends QueryStreamController {
24298
24679
  }
24299
24680
  }
24300
24681
 
24301
- const getPost = async (postId) => {
24302
- const client = getActiveClient();
24303
- client.log('post/getPost', postId);
24304
- isInTombstone('post', postId);
24305
- let payload;
24306
- try {
24307
- // API-FIX: endpoint should not be /list, parameters should be querystring.
24308
- const response = await client.http.get(`/api/v3/posts/${encodeURIComponent(postId)}`);
24309
- payload = response.data;
24310
- }
24311
- catch (error) {
24312
- if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
24313
- pushToTombstone('post', postId);
24314
- }
24315
- throw error;
24316
- }
24317
- const data = prepareMembershipPayload(payload, 'communityUsers');
24318
- const cachedAt = client.cache && Date.now();
24319
- if (client.cache)
24320
- ingestInCache(data, { cachedAt });
24321
- const { posts } = data;
24322
- const result = posts.find(post => post.postId === postId);
24323
- return {
24324
- data: result,
24325
- cachedAt,
24326
- };
24682
+ const commentEventHanlder = (callback, eventHandler, cacheKey, resolveId) => {
24683
+ return eventHandler(async (comment) => {
24684
+ var _a;
24685
+ const currentCollection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
24686
+ if (!currentCollection ||
24687
+ !currentCollection.data.includes(resolveId ? resolveId(comment.referenceId) : comment.referenceId))
24688
+ return;
24689
+ await getPost$1(comment.referenceId);
24690
+ callback(comment);
24691
+ });
24327
24692
  };
24328
- getPost.locally = (postId) => {
24329
- const client = getActiveClient();
24330
- client.log('post/getPost.locally', postId);
24331
- if (!client.cache)
24332
- return;
24333
- const cached = pullFromCache(['post', 'get', postId]);
24334
- if (!cached)
24335
- return;
24336
- return {
24337
- data: cached.data,
24338
- cachedAt: cached.cachedAt,
24339
- };
24693
+ const generateCommentSubscriptions = ({ cacheKey, resolveId, }) => {
24694
+ const eventHandlers = [
24695
+ onCommentCreated,
24696
+ onCommentDeleted,
24697
+ onCommentReactionAdded,
24698
+ onCommentReactionRemoved,
24699
+ onCommentCreatedLocal,
24700
+ onCommentDeleteLocal,
24701
+ onLocalCommentReactionAdded,
24702
+ onLocalCommentReactionRemoved,
24703
+ ];
24704
+ return eventHandlers.map(handler => ({
24705
+ fn: convertEventPayload((callback) => commentEventHanlder(callback, handler, cacheKey, resolveId), 'referenceId', 'post'),
24706
+ action: EnumPostActions.OnPostUpdated,
24707
+ }));
24708
+ };
24709
+ const getPostSubscription = (cacheKey) => {
24710
+ return [
24711
+ { fn: onPostCreated, action: EnumPostActions.OnPostCreated },
24712
+ { fn: onPostUpdated, action: EnumPostActions.OnPostUpdated },
24713
+ { fn: onPostUpdatedLocal, action: EnumPostActions.OnPostUpdated },
24714
+ { fn: onPostDeleted, action: EnumPostActions.OnPostDeleted },
24715
+ { fn: onPostFlagged, action: EnumPostActions.OnPostFlagged },
24716
+ { fn: onPostUnflagged, action: EnumPostActions.OnPostUnflagged },
24717
+ { fn: onPostApproved, action: EnumPostActions.OnPostApproved },
24718
+ { fn: onPostDeclined, action: EnumPostActions.OnPostDeclined },
24719
+ { fn: onPostReactionAdded, action: EnumPostActions.OnPostReactionAdded },
24720
+ { fn: onPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved },
24721
+ { fn: onLocalPostReactionAdded, action: EnumPostActions.OnPostReactionAdded },
24722
+ { fn: onLocalPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved },
24723
+ { fn: onLocalPostDeleted, action: EnumPostActions.OnPostDeleted },
24724
+ ...generateCommentSubscriptions({ cacheKey }),
24725
+ ];
24340
24726
  };
24341
24727
 
24342
24728
  class PostLiveCollectionController extends LiveCollectionController {
@@ -24367,47 +24753,7 @@ class PostLiveCollectionController extends LiveCollectionController {
24367
24753
  this.queryStreamController.appendToQueryStream(response, direction, refresh);
24368
24754
  }
24369
24755
  startSubscription() {
24370
- return this.queryStreamController.subscribeRTE([
24371
- { fn: onPostCreated, action: EnumPostActions.OnPostCreated },
24372
- { fn: onPostUpdated, action: EnumPostActions.OnPostUpdated },
24373
- { fn: onPostUpdatedLocal, action: EnumPostActions.OnPostUpdated },
24374
- { fn: onPostDeleted, action: EnumPostActions.OnPostDeleted },
24375
- { fn: onPostFlagged, action: EnumPostActions.OnPostFlagged },
24376
- { fn: onPostUnflagged, action: EnumPostActions.OnPostUnflagged },
24377
- { fn: onPostApproved, action: EnumPostActions.OnPostApproved },
24378
- { fn: onPostDeclined, action: EnumPostActions.OnPostDeclined },
24379
- { fn: onPostReactionAdded, action: EnumPostActions.OnPostReactionAdded },
24380
- { fn: onPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved },
24381
- { fn: onLocalPostReactionAdded, action: EnumPostActions.OnPostReactionAdded },
24382
- { fn: onLocalPostReactionRemoved, action: EnumPostActions.OnPostReactionRemoved },
24383
- { fn: onLocalPostDeleted, action: EnumPostActions.OnPostDeleted },
24384
- {
24385
- fn: convertEventPayload((callback) => {
24386
- return onCommentCreated(async (comment) => {
24387
- var _a;
24388
- const currentCollection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
24389
- if (!currentCollection || currentCollection.data.includes(comment.referenceId))
24390
- return;
24391
- await getPost(comment.referenceId);
24392
- callback(comment);
24393
- });
24394
- }, 'referenceId', 'post'),
24395
- action: EnumPostActions.OnPostUpdated,
24396
- },
24397
- {
24398
- fn: convertEventPayload((callback) => {
24399
- return onCommentDeleted(async (comment) => {
24400
- var _a;
24401
- const currentCollection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
24402
- if (!currentCollection || currentCollection.data.includes(comment.referenceId))
24403
- return;
24404
- await getPost(comment.referenceId);
24405
- callback(comment);
24406
- });
24407
- }, 'referenceId', 'post'),
24408
- action: EnumPostActions.OnPostUpdated,
24409
- },
24410
- ]);
24756
+ return this.queryStreamController.subscribeRTE(getPostSubscription(this.cacheKey));
24411
24757
  }
24412
24758
  notifyChange({ origin, loading, error }) {
24413
24759
  var _a, _b;
@@ -24542,6 +24888,22 @@ class PinnedPostQueryStreamController extends QueryStreamController {
24542
24888
  });
24543
24889
  }
24544
24890
  }
24891
+ reactor(action) {
24892
+ return (post) => {
24893
+ var _a;
24894
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
24895
+ if (!collection)
24896
+ return;
24897
+ if (action === EnumPostActions.OnPostDeleted) {
24898
+ collection.data = collection.data.filter(referenceId => referenceId !== `global#${post.postId}`);
24899
+ }
24900
+ pushToCache(this.cacheKey, collection);
24901
+ this.notifyChange({ origin: "event" /* Amity.LiveDataOrigin.EVENT */, loading: false });
24902
+ };
24903
+ }
24904
+ subscribeRTE(createSubscriber) {
24905
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
24906
+ }
24545
24907
  }
24546
24908
 
24547
24909
  class PinnedPostLiveCollectionController extends LiveCollectionController {
@@ -24573,7 +24935,10 @@ class PinnedPostLiveCollectionController extends LiveCollectionController {
24573
24935
  }
24574
24936
  // eslint-disable-next-line class-methods-use-this
24575
24937
  startSubscription() {
24576
- return [];
24938
+ return this.queryStreamController.subscribeRTE(generateCommentSubscriptions({
24939
+ cacheKey: this.cacheKey,
24940
+ resolveId: id => `${this.query.placement}#${id}`,
24941
+ }));
24577
24942
  }
24578
24943
  notifyChange({ origin, loading, error }) {
24579
24944
  var _a, _b;
@@ -24725,6 +25090,10 @@ class GlobalPinnedPostLiveCollectionController extends LiveCollectionController
24725
25090
  fn: onPostDeleted,
24726
25091
  action: EnumPostActions.OnPostDeleted,
24727
25092
  },
25093
+ ...generateCommentSubscriptions({
25094
+ cacheKey: this.cacheKey,
25095
+ resolveId: id => `global#${id}`,
25096
+ }),
24728
25097
  ]);
24729
25098
  }
24730
25099
  notifyChange({ origin, loading, error }) {
@@ -25017,6 +25386,7 @@ var index$7 = /*#__PURE__*/Object.freeze({
25017
25386
  flagPost: flagPost,
25018
25387
  unflagPost: unflagPost,
25019
25388
  isPostFlaggedByMe: isPostFlaggedByMe,
25389
+ createClipPost: createClipPost,
25020
25390
  onPostCreated: onPostCreated,
25021
25391
  onPostUpdated: onPostUpdated,
25022
25392
  onPostDeleted: onPostDeleted,
@@ -25026,7 +25396,7 @@ var index$7 = /*#__PURE__*/Object.freeze({
25026
25396
  onPostUnflagged: onPostUnflagged,
25027
25397
  onPostReactionAdded: onPostReactionAdded,
25028
25398
  onPostReactionRemoved: onPostReactionRemoved,
25029
- getPost: getPost$1,
25399
+ getPost: getPost,
25030
25400
  getPosts: getPosts,
25031
25401
  getPinnedPosts: getPinnedPosts,
25032
25402
  getGlobalPinnedPosts: getGlobalPinnedPosts,
@@ -28011,13 +28381,13 @@ exports.AdRepository = index$2;
28011
28381
  exports.CategoryRepository = index$a;
28012
28382
  exports.ChannelRepository = index$e;
28013
28383
  exports.Client = index$n;
28014
- exports.CommentRepository = index$8;
28384
+ exports.CommentRepository = index$9;
28015
28385
  exports.CommunityPostSettingMaps = CommunityPostSettingMaps;
28016
28386
  exports.CommunityPostSettings = CommunityPostSettings;
28017
28387
  exports.CommunityRepository = index$b;
28018
28388
  exports.ContentFeedType = ContentFeedType;
28019
28389
  exports.DefaultCommunityPostSetting = DefaultCommunityPostSetting;
28020
- exports.FeedRepository = index$9;
28390
+ exports.FeedRepository = index$8;
28021
28391
  exports.FileRepository = index$k;
28022
28392
  exports.FileType = FileType;
28023
28393
  exports.InvitationRepository = index;