@amityco/ts-sdk 6.4.6-bdc04db.0 → 6.4.6-be7e4df.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 (101) hide show
  1. package/dist/@types/core/events.d.ts +4 -4
  2. package/dist/@types/core/events.d.ts.map +1 -1
  3. package/dist/@types/core/model.d.ts +3 -1
  4. package/dist/@types/core/model.d.ts.map +1 -1
  5. package/dist/@types/core/payload.d.ts +1 -1
  6. package/dist/@types/core/payload.d.ts.map +1 -1
  7. package/dist/@types/domains/post.d.ts +1 -1
  8. package/dist/@types/domains/post.d.ts.map +1 -1
  9. package/dist/@types/domains/reaction.d.ts +4 -5
  10. package/dist/@types/domains/reaction.d.ts.map +1 -1
  11. package/dist/client/observers/getTotalUnreadCount.d.ts.map +1 -1
  12. package/dist/client/utils/markerSyncEngine.d.ts.map +1 -1
  13. package/dist/commentRepository/observers/getComment.d.ts +2 -2
  14. package/dist/communityRepository/api/getCommunity.d.ts +4 -4
  15. package/dist/communityRepository/api/getRecommendedCommunities.d.ts +4 -4
  16. package/dist/communityRepository/api/getTopTrendingCommunities.d.ts +5 -2
  17. package/dist/communityRepository/api/getTopTrendingCommunities.d.ts.map +1 -1
  18. package/dist/communityRepository/api/getTrendingCommunities.d.ts +32 -0
  19. package/dist/communityRepository/api/getTrendingCommunities.d.ts.map +1 -0
  20. package/dist/communityRepository/api/index.d.ts +0 -1
  21. package/dist/communityRepository/api/index.d.ts.map +1 -1
  22. package/dist/communityRepository/api/test/getTrendingCommunities.test.d.ts +2 -0
  23. package/dist/communityRepository/api/test/getTrendingCommunities.test.d.ts.map +1 -0
  24. package/dist/communityRepository/observers/getCommunities.d.ts +2 -2
  25. package/dist/communityRepository/observers/getCommunity.d.ts +2 -2
  26. package/dist/communityRepository/observers/getRecommendedCommunities.d.ts +20 -0
  27. package/dist/communityRepository/observers/getRecommendedCommunities.d.ts.map +1 -0
  28. package/dist/communityRepository/observers/getTrendingCommunities.d.ts +20 -0
  29. package/dist/communityRepository/observers/getTrendingCommunities.d.ts.map +1 -0
  30. package/dist/communityRepository/observers/index.d.ts +2 -0
  31. package/dist/communityRepository/observers/index.d.ts.map +1 -1
  32. package/dist/communityRepository/utils/communityQueryFilter.d.ts +2 -0
  33. package/dist/communityRepository/utils/communityQueryFilter.d.ts.map +1 -0
  34. package/dist/core/model/idResolvers.d.ts.map +1 -1
  35. package/dist/core/model/identifyModel.d.ts.map +1 -1
  36. package/dist/index.cjs.js +371 -166
  37. package/dist/index.esm.js +371 -166
  38. package/dist/index.umd.js +4 -4
  39. package/dist/pollRepository/api/index.d.ts +0 -1
  40. package/dist/pollRepository/api/index.d.ts.map +1 -1
  41. package/dist/pollRepository/observers/getPoll.d.ts +21 -0
  42. package/dist/pollRepository/observers/getPoll.d.ts.map +1 -0
  43. package/dist/pollRepository/observers/index.d.ts +1 -0
  44. package/dist/pollRepository/observers/index.d.ts.map +1 -1
  45. package/dist/postRepository/observers/getPost.d.ts +2 -2
  46. package/dist/reactionRepository/api/addReaction.d.ts +2 -2
  47. package/dist/reactionRepository/api/addReaction.d.ts.map +1 -1
  48. package/dist/reactionRepository/api/queryReactions.d.ts +2 -2
  49. package/dist/reactionRepository/api/queryReactions.d.ts.map +1 -1
  50. package/dist/reactionRepository/api/queryReactor.d.ts +19 -0
  51. package/dist/reactionRepository/api/queryReactor.d.ts.map +1 -0
  52. package/dist/reactionRepository/api/removeReaction.d.ts +2 -2
  53. package/dist/reactionRepository/api/removeReaction.d.ts.map +1 -1
  54. package/dist/reactionRepository/observers/getReactions.d.ts +3 -3
  55. package/dist/reactionRepository/observers/getReactions.d.ts.map +1 -1
  56. package/dist/utils/tests/dummy/reaction.d.ts +1 -1
  57. package/dist/utils/tests/dummy/reaction.d.ts.map +1 -1
  58. package/package.json +1 -1
  59. package/src/@types/core/events.ts +4 -4
  60. package/src/@types/core/model.ts +3 -1
  61. package/src/@types/core/payload.ts +1 -1
  62. package/src/@types/domains/post.ts +1 -1
  63. package/src/@types/domains/reaction.ts +8 -9
  64. package/src/categoryRepository/api/queryCategories.ts +1 -1
  65. package/src/client/observers/getTotalUnreadCount.ts +1 -7
  66. package/src/client/observers/tests/getTotalUnreadCount.test.ts +3 -10
  67. package/src/client/utils/markerSyncEngine.ts +13 -2
  68. package/src/commentRepository/events/onCommentReactionAdded.ts +1 -1
  69. package/src/commentRepository/events/onCommentReactionRemoved.ts +1 -1
  70. package/src/commentRepository/observers/getComment.ts +2 -2
  71. package/src/commentRepository/observers/tests/getComment.test.ts +1 -3
  72. package/src/communityRepository/api/getCommunity.ts +4 -4
  73. package/src/communityRepository/api/getRecommendedCommunities.ts +4 -4
  74. package/src/communityRepository/api/getTopTrendingCommunities.ts +5 -2
  75. package/src/communityRepository/api/getTrendingCommunities.ts +97 -0
  76. package/src/communityRepository/api/index.ts +1 -1
  77. package/src/communityRepository/api/test/getTrendingCommunities.test.ts +102 -0
  78. package/src/communityRepository/observers/getCommunities.ts +2 -2
  79. package/src/communityRepository/observers/getCommunity.ts +2 -2
  80. package/src/communityRepository/observers/getRecommendedCommunities.ts +133 -0
  81. package/src/communityRepository/observers/getTrendingCommunities.ts +133 -0
  82. package/src/communityRepository/observers/index.ts +2 -0
  83. package/src/communityRepository/utils/communityQueryFilter.ts +55 -0
  84. package/src/core/model/idResolvers.ts +2 -1
  85. package/src/core/model/identifyModel.ts +5 -4
  86. package/src/pollRepository/api/index.ts +0 -2
  87. package/src/pollRepository/observers/getPoll.ts +33 -0
  88. package/src/pollRepository/observers/index.ts +1 -0
  89. package/src/pollRepository/observers/observePoll.ts +2 -2
  90. package/src/postRepository/events/tests/onPostReactionAdded.test.ts +6 -5
  91. package/src/postRepository/events/tests/onPostReactionRemoved.test.ts +5 -4
  92. package/src/postRepository/observers/getPost.ts +2 -2
  93. package/src/postRepository/observers/getPosts.ts +1 -1
  94. package/src/postRepository/observers/tests/getPost.test.ts +1 -3
  95. package/src/postRepository/observers/tests/getPosts.test.ts +1 -5
  96. package/src/reactionRepository/api/addReaction.ts +2 -2
  97. package/src/reactionRepository/api/queryReactions.ts +2 -2
  98. package/src/reactionRepository/api/queryReactor.ts +31 -0
  99. package/src/reactionRepository/api/removeReaction.ts +2 -2
  100. package/src/reactionRepository/observers/getReactions.ts +6 -8
  101. package/src/utils/tests/dummy/reaction.ts +6 -10
package/dist/index.cjs.js CHANGED
@@ -524,7 +524,8 @@ const idResolvers = {
524
524
  post: ({ postId }) => postId,
525
525
  comment: ({ commentId }) => commentId,
526
526
  poll: ({ pollId }) => pollId,
527
- reaction: ({ reactionId }) => reactionId,
527
+ reaction: ({ referenceType, referenceId }) => `${referenceType}#${referenceId}`,
528
+ reactor: ({ reactionId }) => reactionId,
528
529
  stream: ({ streamId }) => streamId,
529
530
  follow: ({ from, to }) => `${from}#${to}`,
530
531
  followInfo: ({ userId }) => userId,
@@ -551,10 +552,10 @@ const getResolver = (name) => idResolvers[name];
551
552
  */
552
553
  const CRITERIAS = {
553
554
  file: ['fileId', 'attributes'],
554
- user: ['userId', 'avatarFileId'],
555
+ user: ['userId', 'avatarFileId', 'roles', 'permissions'],
555
556
  role: ['roleId'],
556
- channel: ['channelId', 'rateLimit'],
557
- subChannel: ['subChannelId'],
557
+ channel: ['channelId', 'defaultSubChannelId'],
558
+ subChannel: ['subChannelId', 'latestMessageId'],
558
559
  channelUsers: ['channelId', 'userId', 'membership'],
559
560
  message: ['messageId'],
560
561
  community: ['communityId', 'postSetting'],
@@ -563,7 +564,8 @@ const CRITERIAS = {
563
564
  post: ['postId', 'feedId'],
564
565
  comment: ['commentId', 'referenceId'],
565
566
  poll: ['pollId'],
566
- reaction: ['reactionId'],
567
+ reaction: ['referenceId', 'referenceType', 'reactors'],
568
+ reactor: ['reactionId'],
567
569
  stream: ['streamId'],
568
570
  follow: ['from', 'to'],
569
571
  feed: ['feedId', 'feedType'],
@@ -22564,12 +22566,8 @@ const getTotalUnreadCount = (callback) => {
22564
22566
  if (!userId)
22565
22567
  throw new ASCError('The _id has not been defined in ActiveUser', 800000 /* Amity.ClientError.UNKNOWN_ERROR */, "error" /* Amity.ErrorLevel.ERROR */);
22566
22568
  const callbackDataSelector = (data) => { var _a; return (_a = data === null || data === void 0 ? void 0 : data.unreadCount) !== null && _a !== void 0 ? _a : 0; };
22567
- // based on the mobile specs, unreadCount will not trigger observer when userMarker
22568
- // is re-fetched, so apply this filter to ensure the behavior is the same.
22569
- const callbackFilter = (newModel, oldModel) => (newModel === null || newModel === void 0 ? void 0 : newModel.unreadCount) !== (oldModel === null || oldModel === void 0 ? void 0 : oldModel.unreadCount);
22570
22569
  return liveObject(userId, callback, 'userId', getUserMarker, [onUserMarkerFetched], {
22571
22570
  callbackDataSelector,
22572
- callbackFilter,
22573
22571
  });
22574
22572
  };
22575
22573
 
@@ -24319,6 +24317,7 @@ const onSubChannelDeleted = (callback) => {
24319
24317
  };
24320
24318
 
24321
24319
  const SYNC_TRIGGER_INTERVAL_TIME = 2000;
24320
+ const ON_SUB_CHANNEL_DELETE_SYNC_TRIGGER_DELAY = 2000;
24322
24321
  let clearSyncTrigger;
24323
24322
  let disposers = [];
24324
24323
  let isWaitingForResponse = false;
@@ -24409,7 +24408,13 @@ const registerEventListeners = () => {
24409
24408
  },
24410
24409
  // only trigger sync from remote events to prevent an unread count equal `-1`
24411
24410
  // when an optimistic message creation is performed.
24412
- false), onChannelCreated(() => events.push("subchannel is created" /* Amity.MarkerSyncEvent.CHANNEL_CREATED */)), onChannelDeleted(() => events.push("subchannel is deleted" /* Amity.MarkerSyncEvent.CHANNEL_DELETED */)), onChannelJoined(() => events.push("subchannel is joined" /* Amity.MarkerSyncEvent.CHANNEL_JOINED */)), onChannelLeft(() => events.push("subchannel is left" /* Amity.MarkerSyncEvent.CHANNEL_LEFT */)), onSubChannelCreated(() => events.push("subchannel is created" /* Amity.MarkerSyncEvent.SUB_CHANNEL_CREATED */)), onSubChannelDeleted(() => events.push("subchannel is deleted" /* Amity.MarkerSyncEvent.SUBCHANNEL_IS_DELETED */)), onFeedMarkerUpdated(() => events.push("feed marker updated" /* Amity.MarkerSyncEvent.MARKER_UPDATED */)), onUserMarkerSync(() => events.push("feed marker updated" /* Amity.MarkerSyncEvent.MARKER_UPDATED */)));
24411
+ false), onChannelCreated(() => events.push("subchannel is created" /* Amity.MarkerSyncEvent.CHANNEL_CREATED */)), onChannelDeleted(() => events.push("subchannel is deleted" /* Amity.MarkerSyncEvent.CHANNEL_DELETED */)), onChannelJoined(() => events.push("subchannel is joined" /* Amity.MarkerSyncEvent.CHANNEL_JOINED */)), onChannelLeft(() => events.push("subchannel is left" /* Amity.MarkerSyncEvent.CHANNEL_LEFT */)), onSubChannelCreated(() => events.push("subchannel is created" /* Amity.MarkerSyncEvent.SUB_CHANNEL_CREATED */)), onSubChannelDeleted(() =>
24412
+ /*
24413
+ workaround: when receiving the event for sub-channel deletion,
24414
+ before triggering marker update, the SDK will have to add a 2-second delay.
24415
+ so that the unread count is calculated correctly.
24416
+ */
24417
+ setTimeout(() => events.push("subchannel is deleted" /* Amity.MarkerSyncEvent.SUBCHANNEL_IS_DELETED */), ON_SUB_CHANNEL_DELETE_SYNC_TRIGGER_DELAY)), onFeedMarkerUpdated(() => events.push("feed marker updated" /* Amity.MarkerSyncEvent.MARKER_UPDATED */)), onUserMarkerSync(() => events.push("feed marker updated" /* Amity.MarkerSyncEvent.MARKER_UPDATED */)));
24413
24418
  };
24414
24419
  const unRegisterEventListeners = () => {
24415
24420
  disposers.forEach(fn => fn());
@@ -27867,7 +27872,7 @@ const onReactorRemoved = (referenceType, referenceId, callback) => {
27867
27872
  * })
27868
27873
  * ```
27869
27874
  *
27870
- * Queries a paginable list of {@link Amity.ReactionQuery} objects
27875
+ * Queries a paginable list of {@link Amity.Reaction} objects
27871
27876
  *
27872
27877
  * @param query The query parameters
27873
27878
  * @returns A page of {@link Amity.Reaction} objects
@@ -27894,6 +27899,30 @@ const queryReactions = async (query) => {
27894
27899
  return { data: reactions, prevPage, nextPage };
27895
27900
  };
27896
27901
 
27902
+ /**
27903
+ * ```js
27904
+ * import { queryReactor } from '@amityco/ts-sdk'
27905
+ * const { data: reactions, prevPage, nextPage } = await queryReactor({
27906
+ * referenceId: 'postId',
27907
+ * referenceType: 'post',
27908
+ * })
27909
+ * ```
27910
+ *
27911
+ * Queries a paginable list of {@link Amity.Reactor} objects
27912
+ *
27913
+ * @param query The query parameters
27914
+ * @returns A page of {@link Amity.Reactor} objects
27915
+ *
27916
+ * @reaction Reaction API
27917
+ * @async
27918
+ * */
27919
+ const queryReactor = async (query) => {
27920
+ const client = getActiveClient();
27921
+ client.log('reaction/queryReactor', query);
27922
+ const _a = await queryReactions(query), { data } = _a, response = __rest(_a, ["data"]);
27923
+ return Object.assign(Object.assign({}, response), { data: data[0].reactors });
27924
+ };
27925
+
27897
27926
  /* begin_public_function
27898
27927
  id: reaction.query
27899
27928
  */
@@ -27908,11 +27937,11 @@ const queryReactions = async (query) => {
27908
27937
  * }, response => merge(reactions, response.data))
27909
27938
  * ```
27910
27939
  *
27911
- * Observe all mutations on a list of {@link Amity.Reaction} for a given target object
27940
+ * Observe all mutations on a list of {@link Amity.Reactor} for a given target object
27912
27941
  *
27913
- * @param params.referenceType the type of the target
27914
- * @param params.referenceId the ID of the target
27942
+ * @param params for querying reactions
27915
27943
  * @param callback the function to call when new data are available
27944
+ * @param config the live collection configuration
27916
27945
  * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
27917
27946
  *
27918
27947
  * @category Reactions Live Collection
@@ -27967,7 +27996,7 @@ const getReactions = (params, callback, config) => {
27967
27996
  const reactions = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
27968
27997
  if (!initial && reactions.length > 0 && !((_c = collection === null || collection === void 0 ? void 0 : collection.params) === null || _c === void 0 ? void 0 : _c.page))
27969
27998
  return;
27970
- const query = createQuery(queryReactions, Object.assign(Object.assign({}, queryParams), { page: (_d = (!initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined)) !== null && _d !== void 0 ? _d : { limit } }));
27999
+ const query = createQuery(queryReactor, Object.assign(Object.assign({}, queryParams), { page: (_d = (!initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined)) !== null && _d !== void 0 ? _d : { limit } }));
27971
28000
  runQuery(query, ({ data: result, error, loading, nextPage: page }) => {
27972
28001
  const data = {
27973
28002
  loading,
@@ -27976,9 +28005,7 @@ const getReactions = (params, callback, config) => {
27976
28005
  data: reactions,
27977
28006
  };
27978
28007
  if (result) {
27979
- data.data = [
27980
- ...new Set([...reactions, ...result[0].reactors.map(getResolver('reaction'))]),
27981
- ];
28008
+ data.data = [...new Set([...reactions, ...result.map(getResolver('reactor'))])];
27982
28009
  }
27983
28010
  pushToCache(cacheKey, data);
27984
28011
  responder(data);
@@ -29738,8 +29765,8 @@ const updateCommunity = async (communityId, patch) => {
29738
29765
 
29739
29766
  /**
29740
29767
  * ```js
29741
- * import { getCommunity } from '@amityco/ts-sdk'
29742
- * const community = await getCommunity('foobar')
29768
+ * import { CommunityRepository } from '@amityco/ts-sdk'
29769
+ * const community = await CommunityRepository.getCommunity('foobar')
29743
29770
  * ```
29744
29771
  *
29745
29772
  * Fetches a {@link Amity.Community} object
@@ -29767,8 +29794,8 @@ const getCommunity$1 = async (communityId) => {
29767
29794
  };
29768
29795
  /**
29769
29796
  * ```js
29770
- * import { getCommunity } from '@amityco/ts-sdk'
29771
- * const community = getCommunity.locally('foobar')
29797
+ * import { CommunityRepository } from '@amityco/ts-sdk'
29798
+ * const community = CommunityRepository.getCommunity.locally('foobar')
29772
29799
  * ```
29773
29800
  *
29774
29801
  * Fetches a {@link Amity.Community} object from cache
@@ -29893,9 +29920,12 @@ const leaveCommunity = async (communityId) => {
29893
29920
  id: community.query.trending_communities
29894
29921
  */
29895
29922
  /**
29923
+ * @deprecated This API renamed to `getTrendingCommunities()`.
29924
+ * Please use getTrendingCommunities() instead.
29925
+ *
29896
29926
  * ```js
29897
- * import { getTopTrendingCommunities } from '@amityco/ts-sdk'
29898
- * const trendingCommunities = await getTopTrendingCommunities()
29927
+ * import { CommunityRepository } from '@amityco/ts-sdk'
29928
+ * const trendingCommunities = await CommunityRepository.getTopTrendingCommunities()
29899
29929
  * ```
29900
29930
  *
29901
29931
  * Gets a list of top trending {@link Amity.Community} objects
@@ -29957,74 +29987,6 @@ getTopTrendingCommunities.locally = (query) => {
29957
29987
  : undefined;
29958
29988
  };
29959
29989
 
29960
- /* begin_public_function
29961
- id: community.query.recommended_communities
29962
- */
29963
- /**
29964
- * ```js
29965
- * import { getRecommendedCommunities } from '@amityco/ts-sdk'
29966
- * const communities = await getRecommendedCommunities()
29967
- * ```
29968
- *
29969
- * Gets a list of recommended {@link Amity.Community} objects
29970
- *
29971
- * @param query The query parameters
29972
- * @returns A list of {@link Amity.Community} objects
29973
- *
29974
- * @category Community API
29975
- * @async
29976
- */
29977
- const getRecommendedCommunities = async (query) => {
29978
- const client = getActiveClient();
29979
- client.log('channel/getRecommendedCommunities', query);
29980
- const { limit = 5 } = query !== null && query !== void 0 ? query : {};
29981
- // API-FIX: backend doesnt answer Amity.Response
29982
- // const { data: payload } = await client.http.get<Amity.Response<CommunityPayload>>(
29983
- const { data: payload } = await client.http.get(`/api/v3/communities/recommended`, { params: { options: { limit } } });
29984
- const data = prepareCommunityPayload(payload);
29985
- const { communities } = data;
29986
- const cachedAt = client.cache && Date.now();
29987
- if (client.cache) {
29988
- ingestInCache(data, { cachedAt });
29989
- const cacheKey = ['community', 'recommended', { params: { options: { limit } } }];
29990
- pushToCache(cacheKey, { communities: communities.map(getResolver('community')) });
29991
- }
29992
- return { data: communities, cachedAt };
29993
- };
29994
- /* end_public_function */
29995
- /**
29996
- * ```js
29997
- * import { getRecommendedCommunities } from '@amityco/ts-sdk'
29998
- * const communities = getRecommendedCommunities.locally()
29999
- * ```
30000
- *
30001
- * Gets a list of recommended {@link Amity.Community} objects from cache
30002
- *
30003
- * @param query The query parameters
30004
- * @returns communities
30005
- *
30006
- * @category Community API
30007
- */
30008
- getRecommendedCommunities.locally = (query) => {
30009
- var _a, _b;
30010
- const client = getActiveClient();
30011
- client.log('community/getRecommendedCommunities.locally', query);
30012
- if (!client.cache)
30013
- return;
30014
- const { limit = 5 } = query !== null && query !== void 0 ? query : {};
30015
- const queryKey = ['community', 'recommended', { params: { options: { limit } } }];
30016
- const { data, cachedAt } = (_a = pullFromCache(queryKey)) !== null && _a !== void 0 ? _a : {};
30017
- if (!(data === null || data === void 0 ? void 0 : data.communities.length))
30018
- return;
30019
- const communities = data.communities
30020
- .map(communityId => pullFromCache(['community', 'get', communityId]))
30021
- .filter(Boolean)
30022
- .map(({ data }) => data);
30023
- return communities.length === ((_b = data === null || data === void 0 ? void 0 : data.communities) === null || _b === void 0 ? void 0 : _b.length)
30024
- ? { data: communities, cachedAt }
30025
- : undefined;
30026
- };
30027
-
30028
29990
  const createCommunityEventSubscriber = (event, callback) => {
30029
29991
  const client = getActiveClient();
30030
29992
  const filter = (payload) => {
@@ -30429,10 +30391,10 @@ const queryCommunities = async (query) => {
30429
30391
  */
30430
30392
  /**
30431
30393
  * ```js
30432
- * import { getCommunities } from '@amityco/ts-sdk'
30394
+ * import { CommunityRepository } from '@amityco/ts-sdk'
30433
30395
  *
30434
30396
  * let communities = []
30435
- * const unsub = getCommunities({
30397
+ * const unsub = CommunityRepository.getCommunities({
30436
30398
  * displayName: Amity.Community['displayName'],
30437
30399
  * }, response => merge(communities, response.data))
30438
30400
  * ```
@@ -30549,11 +30511,11 @@ const getCommunities = (params, callback, config) => {
30549
30511
  */
30550
30512
  /**
30551
30513
  * ```js
30552
- * import { getCommunity } from '@amityco/ts-sdk';
30514
+ * import { CommunityRepository } from '@amityco/ts-sdk';
30553
30515
  *
30554
30516
  * let community;
30555
30517
  *
30556
- * const unsubscribe = getCommunity(communityId, response => {
30518
+ * const unsub = CommunityRepository.getCommunity(communityId, response => {
30557
30519
  * community = response.data;
30558
30520
  * });
30559
30521
  * ```
@@ -30579,6 +30541,221 @@ const getCommunity = (communityId, callback) => {
30579
30541
  };
30580
30542
  /* end_public_function */
30581
30543
 
30544
+ const communityQueryFilter = (data, params, userId) => {
30545
+ let communities = data;
30546
+ communities = filterByPropEquality(communities, 'displayName', params.displayName);
30547
+ if (!params.includeDeleted) {
30548
+ communities = filterByPropEquality(communities, 'isDeleted', false);
30549
+ }
30550
+ if (params.categoryId) {
30551
+ communities = communities.filter(c => { var _a; return (_a = c.categoryIds) === null || _a === void 0 ? void 0 : _a.includes(params.categoryId); });
30552
+ }
30553
+ if (params.tags) {
30554
+ communities = communities.filter(c => { var _a; return (_a = c.tags) === null || _a === void 0 ? void 0 : _a.some(t => { var _a; return (_a = params.tags) === null || _a === void 0 ? void 0 : _a.includes(t); }); });
30555
+ }
30556
+ if (params.membership && userId) {
30557
+ communities = filterByCommunityMembership(communities, params.membership, userId);
30558
+ }
30559
+ const sortBy = params.sortBy || 'lastCreated';
30560
+ if (sortBy === 'lastCreated' || sortBy === 'firstCreated') {
30561
+ communities = communities.sort(sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
30562
+ }
30563
+ /*
30564
+ * The server returns communities with empty | null displayName's first before
30565
+ * returning sorted list of communities with displayNames
30566
+ *
30567
+ * This section needs to be updated as displayNames can be null as well
30568
+ */
30569
+ if (sortBy === 'displayName') {
30570
+ communities = communities
30571
+ // this needs to be aligned with the backend data type
30572
+ .map(c => (c.displayName ? c : Object.assign(Object.assign({}, c), { displayName: '' })))
30573
+ // @ts-ignore
30574
+ .sort(sortByDisplayName);
30575
+ }
30576
+ return communities;
30577
+ };
30578
+
30579
+ /* begin_public_function
30580
+ id: community.query
30581
+ */
30582
+ /**
30583
+ * ```js
30584
+ * import { CommunityRepository } from '@amityco/ts-sdk'
30585
+ *
30586
+ * let communities = []
30587
+ * const unsub = CommunityRepository.getCommunities({
30588
+ * displayName: Amity.Community['displayName'],
30589
+ * }, response => merge(communities, response.data))
30590
+ * ```
30591
+ *
30592
+ * Observe all mutations on a list of {@link Amity.Community}s
30593
+ *
30594
+ * @param params for querying communities
30595
+ * @param callback the function to call when new data are available
30596
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the communities
30597
+ *
30598
+ * @category Community Live Collection
30599
+ */
30600
+ const getTrendingCommunities = (params, callback, config) => {
30601
+ const { log, cache, userId } = getActiveClient();
30602
+ if (!cache) {
30603
+ console.log(ENABLE_CACHE_MESSAGE);
30604
+ }
30605
+ const timestamp = Date.now();
30606
+ log(`getTrendingCommunities(tmpid: ${timestamp}) > listen`);
30607
+ const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
30608
+ const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
30609
+ const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
30610
+ const disposers = [];
30611
+ const cacheKey = ['community', 'top-trending', { params: { options: { limit } } }];
30612
+ const responder = (data, isEventModel = false) => {
30613
+ var _a, _b;
30614
+ const communities = (_a = data.data
30615
+ .map(communityId => pullFromCache(['community', 'get', communityId]))
30616
+ .filter(Boolean)
30617
+ .map(({ data }) => data)) !== null && _a !== void 0 ? _a : [];
30618
+ callback({
30619
+ onNextPage: onFetch,
30620
+ data: isEventModel ? communityQueryFilter(communities, params, userId) : communities,
30621
+ hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
30622
+ loading: data.loading,
30623
+ error: data.error,
30624
+ });
30625
+ };
30626
+ const realtimeRouter = (_) => (community) => {
30627
+ var _a;
30628
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30629
+ if (!collection)
30630
+ return;
30631
+ /*
30632
+ * Simply update collection and let responder decide what to do with data
30633
+ */
30634
+ collection.data = [...new Set([community.communityId, ...collection.data])];
30635
+ pushToCache(cacheKey, collection);
30636
+ responder(collection);
30637
+ };
30638
+ const onFetch = (initial = false) => {
30639
+ var _a, _b, _c;
30640
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30641
+ const communities = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
30642
+ if (!initial && communities.length > 0 && !(collection === null || collection === void 0 ? void 0 : collection.params.page))
30643
+ return;
30644
+ const query = createQuery(queryCommunities, Object.assign(Object.assign({}, queryParams), { page: (_c = (!initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined)) !== null && _c !== void 0 ? _c : { limit } }));
30645
+ runQuery(query, ({ data: result, error, loading, nextPage: page }) => {
30646
+ const data = {
30647
+ loading,
30648
+ error,
30649
+ params: { page },
30650
+ data: communities,
30651
+ };
30652
+ if (result) {
30653
+ data.data = [...new Set([...communities, ...result.map(getResolver('community'))])];
30654
+ }
30655
+ pushToCache(cacheKey, data);
30656
+ responder(data);
30657
+ }, queryOptions(policy, CACHE_SHORTEN_LIFESPAN));
30658
+ };
30659
+ disposers.push(onCommunityUserChanged(realtimeRouter()));
30660
+ onFetch(true);
30661
+ disposers.push(() => dropFromCache(cacheKey));
30662
+ return () => {
30663
+ log(`getTrendingCommunities(tmpid: ${timestamp}) > dispose`);
30664
+ disposers.forEach(fn => fn());
30665
+ };
30666
+ };
30667
+ /* end_public_function */
30668
+
30669
+ /* begin_public_function
30670
+ id: community.query
30671
+ */
30672
+ /**
30673
+ * ```js
30674
+ * import { CommunityRepository } from '@amityco/ts-sdk'
30675
+ *
30676
+ * let communities = []
30677
+ * const unsub = CommunityRepository.getCommunities({
30678
+ * displayName: Amity.Community['displayName'],
30679
+ * }, response => merge(communities, response.data))
30680
+ * ```
30681
+ *
30682
+ * Observe all mutations on a list of {@link Amity.Community}s
30683
+ *
30684
+ * @param params for querying communities
30685
+ * @param callback the function to call when new data are available
30686
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the communities
30687
+ *
30688
+ * @category Community Live Collection
30689
+ */
30690
+ const getRecommendedCommunities = (params, callback, config) => {
30691
+ const { log, cache, userId } = getActiveClient();
30692
+ if (!cache) {
30693
+ console.log(ENABLE_CACHE_MESSAGE);
30694
+ }
30695
+ const timestamp = Date.now();
30696
+ log(`getRecommendedCommunities(tmpid: ${timestamp}) > listen`);
30697
+ const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
30698
+ const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
30699
+ const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
30700
+ const disposers = [];
30701
+ const cacheKey = ['community', 'recommended', { params: { options: { limit } } }];
30702
+ const responder = (data, isEventModel = false) => {
30703
+ var _a, _b;
30704
+ const communities = (_a = data.data
30705
+ .map(communityId => pullFromCache(['community', 'get', communityId]))
30706
+ .filter(Boolean)
30707
+ .map(({ data }) => data)) !== null && _a !== void 0 ? _a : [];
30708
+ callback({
30709
+ onNextPage: onFetch,
30710
+ data: isEventModel ? communityQueryFilter(communities, params, userId) : communities,
30711
+ hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
30712
+ loading: data.loading,
30713
+ error: data.error,
30714
+ });
30715
+ };
30716
+ const realtimeRouter = (_) => (community) => {
30717
+ var _a;
30718
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30719
+ if (!collection)
30720
+ return;
30721
+ /*
30722
+ * Simply update collection and let responder decide what to do with data
30723
+ */
30724
+ collection.data = [...new Set([community.communityId, ...collection.data])];
30725
+ pushToCache(cacheKey, collection);
30726
+ responder(collection);
30727
+ };
30728
+ const onFetch = (initial = false) => {
30729
+ var _a, _b, _c;
30730
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30731
+ const communities = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
30732
+ if (!initial && communities.length > 0 && !(collection === null || collection === void 0 ? void 0 : collection.params.page))
30733
+ return;
30734
+ const query = createQuery(queryCommunities, Object.assign(Object.assign({}, queryParams), { page: (_c = (!initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined)) !== null && _c !== void 0 ? _c : { limit } }));
30735
+ runQuery(query, ({ data: result, error, loading, nextPage: page }) => {
30736
+ const data = {
30737
+ loading,
30738
+ error,
30739
+ params: { page },
30740
+ data: communities,
30741
+ };
30742
+ if (result) {
30743
+ data.data = [...new Set([...communities, ...result.map(getResolver('community'))])];
30744
+ }
30745
+ pushToCache(cacheKey, data);
30746
+ responder(data);
30747
+ }, queryOptions(policy, CACHE_SHORTEN_LIFESPAN));
30748
+ };
30749
+ disposers.push(onCommunityUserChanged(realtimeRouter()));
30750
+ onFetch(true);
30751
+ disposers.push(() => dropFromCache(cacheKey));
30752
+ return () => {
30753
+ log(`getRecommendedCommunities(tmpid: ${timestamp}) > dispose`);
30754
+ disposers.forEach(fn => fn());
30755
+ };
30756
+ };
30757
+ /* end_public_function */
30758
+
30582
30759
  /* begin_public_function
30583
30760
  id: community.moderation.add_roles
30584
30761
  */
@@ -31007,13 +31184,14 @@ var index$6 = /*#__PURE__*/Object.freeze({
31007
31184
  joinCommunity: joinCommunity,
31008
31185
  leaveCommunity: leaveCommunity,
31009
31186
  getTopTrendingCommunities: getTopTrendingCommunities,
31010
- getRecommendedCommunities: getRecommendedCommunities,
31011
31187
  onCommunityCreated: onCommunityCreated,
31012
31188
  onCommunityUpdated: onCommunityUpdated,
31013
31189
  onCommunityDeleted: onCommunityDeleted,
31014
31190
  observeCommunity: observeCommunity,
31015
31191
  getCommunities: getCommunities,
31016
- getCommunity: getCommunity
31192
+ getCommunity: getCommunity,
31193
+ getTrendingCommunities: getTrendingCommunities,
31194
+ getRecommendedCommunities: getRecommendedCommunities
31017
31195
  });
31018
31196
 
31019
31197
  /* begin_public_function
@@ -31105,7 +31283,7 @@ const queryCategories = async (query) => {
31105
31283
  const cacheKey = [
31106
31284
  'category',
31107
31285
  'query',
31108
- Object.assign(Object.assign({}, params), { options: Object.assign({}, page) }),
31286
+ Object.assign(Object.assign({}, params), { includeDeleted, options: Object.assign({}, page) }),
31109
31287
  ];
31110
31288
  pushToCache(cacheKey, { categories: categories.map(getResolver('category')), paging });
31111
31289
  }
@@ -31984,11 +32162,11 @@ const observePost = (postId, callback, policy = 'cache_then_server') => {
31984
32162
  */
31985
32163
  /**
31986
32164
  * ```js
31987
- * import { getPost } from '@amityco/ts-sdk';
32165
+ * import { PostRepository } from '@amityco/ts-sdk';
31988
32166
  *
31989
32167
  * let post;
31990
32168
  *
31991
- * const unsubscribe = getPost(postId, response => {
32169
+ * const unsub = PostRepository.getPost(postId, response => {
31992
32170
  * post = response.data;
31993
32171
  * });
31994
32172
  * ```
@@ -32068,7 +32246,7 @@ const queryPosts = async (query) => {
32068
32246
  };
32069
32247
 
32070
32248
  /* begin_public_function
32071
- id: post.query, feed.query.community_feed, feed.query.user_feed
32249
+ id: post.query
32072
32250
  */
32073
32251
  /**
32074
32252
  * ```js
@@ -32644,11 +32822,11 @@ const observeComment = (commentId, callback, policy = 'cache_then_server') => {
32644
32822
  */
32645
32823
  /**
32646
32824
  * ```js
32647
- * import { getComment } from '@amityco/ts-sdk';
32825
+ * import { CommentRepository } from '@amityco/ts-sdk';
32648
32826
  *
32649
32827
  * let comment;
32650
32828
  *
32651
- * const unsubscribe = getComment(commentId, response => {
32829
+ * const unsub = CommentRepository.getComment(commentId, response => {
32652
32830
  * comment = response.data;
32653
32831
  * });
32654
32832
  * ```
@@ -33281,64 +33459,6 @@ var index$1 = /*#__PURE__*/Object.freeze({
33281
33459
  getStreams: getStreams
33282
33460
  });
33283
33461
 
33284
- /* begin_public_function
33285
- id: poll.get
33286
- */
33287
- /**
33288
- * ```js
33289
- * import { getPoll } from '@amityco/ts-sdk'
33290
- * const poll = await getPoll('foobar')
33291
- * ```
33292
- *
33293
- * Fetches a {@link Amity.Poll} object
33294
- *
33295
- * @param pollId the ID of the {@link Amity.Poll} to fetch
33296
- * @returns the associated {@link Amity.Poll} object
33297
- *
33298
- * @category Poll API
33299
- * @async
33300
- */
33301
- const getPoll = async (pollId) => {
33302
- const client = getActiveClient();
33303
- client.log('poll/getPoll', pollId);
33304
- const { data } = await client.http.get(`/api/v3/polls/${pollId}`);
33305
- const cachedAt = client.cache && Date.now();
33306
- if (client.cache)
33307
- ingestInCache(data, { cachedAt });
33308
- const { polls } = data;
33309
- return {
33310
- data: polls.find(poll => poll.pollId === pollId),
33311
- cachedAt,
33312
- };
33313
- };
33314
- /* end_public_function */
33315
- /**
33316
- * ```js
33317
- * import { getPoll } from '@amityco/ts-sdk'
33318
- * const poll = getPoll.locally('foobar')
33319
- * ```
33320
- *
33321
- * Fetches a {@link Amity.Poll} object
33322
- *
33323
- * @param pollId the ID of the {@link Amity.Poll} to fetch
33324
- * @returns the associated {@link Amity.Poll} object
33325
- *
33326
- * @category Poll API
33327
- */
33328
- getPoll.locally = (pollId) => {
33329
- const client = getActiveClient();
33330
- client.log('poll/getPoll', pollId);
33331
- if (!client.cache)
33332
- return;
33333
- const cached = pullFromCache(['poll', 'get', pollId]);
33334
- if (!cached)
33335
- return;
33336
- return {
33337
- data: cached.data,
33338
- cachedAt: cached.cachedAt,
33339
- };
33340
- };
33341
-
33342
33462
  /* begin_public_function
33343
33463
  id: poll.create
33344
33464
  */
@@ -33411,6 +33531,64 @@ const closePoll = async (pollId) => {
33411
33531
  };
33412
33532
  /* end_public_function */
33413
33533
 
33534
+ /* begin_public_function
33535
+ id: poll.get
33536
+ */
33537
+ /**
33538
+ * ```js
33539
+ * import { getPoll } from '@amityco/ts-sdk'
33540
+ * const poll = await getPoll('foobar')
33541
+ * ```
33542
+ *
33543
+ * Fetches a {@link Amity.Poll} object
33544
+ *
33545
+ * @param pollId the ID of the {@link Amity.Poll} to fetch
33546
+ * @returns the associated {@link Amity.Poll} object
33547
+ *
33548
+ * @category Poll API
33549
+ * @async
33550
+ */
33551
+ const getPoll$1 = async (pollId) => {
33552
+ const client = getActiveClient();
33553
+ client.log('poll/getPoll', pollId);
33554
+ const { data } = await client.http.get(`/api/v3/polls/${pollId}`);
33555
+ const cachedAt = client.cache && Date.now();
33556
+ if (client.cache)
33557
+ ingestInCache(data, { cachedAt });
33558
+ const { polls } = data;
33559
+ return {
33560
+ data: polls.find(poll => poll.pollId === pollId),
33561
+ cachedAt,
33562
+ };
33563
+ };
33564
+ /* end_public_function */
33565
+ /**
33566
+ * ```js
33567
+ * import { getPoll } from '@amityco/ts-sdk'
33568
+ * const poll = getPoll.locally('foobar')
33569
+ * ```
33570
+ *
33571
+ * Fetches a {@link Amity.Poll} object
33572
+ *
33573
+ * @param pollId the ID of the {@link Amity.Poll} to fetch
33574
+ * @returns the associated {@link Amity.Poll} object
33575
+ *
33576
+ * @category Poll API
33577
+ */
33578
+ getPoll$1.locally = (pollId) => {
33579
+ const client = getActiveClient();
33580
+ client.log('poll/getPoll', pollId);
33581
+ if (!client.cache)
33582
+ return;
33583
+ const cached = pullFromCache(['poll', 'get', pollId]);
33584
+ if (!cached)
33585
+ return;
33586
+ return {
33587
+ data: cached.data,
33588
+ cachedAt: cached.cachedAt,
33589
+ };
33590
+ };
33591
+
33414
33592
  /* begin_public_function
33415
33593
  id: poll.delete
33416
33594
  */
@@ -33431,7 +33609,7 @@ const closePoll = async (pollId) => {
33431
33609
  const deletePoll = async (pollId) => {
33432
33610
  const client = getActiveClient();
33433
33611
  client.log('poll/deletePoll', pollId);
33434
- const poll = await getPoll(pollId);
33612
+ const poll = await getPoll$1(pollId);
33435
33613
  // API-FIX: it returns { success: boolean } but seems it should be Amity.Response<{ success: boolean }
33436
33614
  const { data } = await client.http.delete(`/api/v3/polls/${pollId}`);
33437
33615
  const { success } = data; // unwrapPayload(data)
@@ -33563,23 +33741,50 @@ const observePoll = (pollId, callback, policy = 'cache_then_server') => {
33563
33741
  };
33564
33742
  const disposers = [];
33565
33743
  disposers.push(onPollUpdated(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onUpdate')), onPollDeleted(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onDelete')));
33566
- runQuery(createQuery(getPoll, pollId), result => (result.data || result.error) && router(result, 'onFetch'), queryOptions(policy));
33744
+ runQuery(createQuery(getPoll$1, pollId), result => (result.data || result.error) && router(result, 'onFetch'), queryOptions(policy));
33567
33745
  return () => {
33568
33746
  log(`observePoll(tmpid: ${timestamp}) > dispose`);
33569
33747
  disposers.forEach(fn => fn());
33570
33748
  };
33571
33749
  };
33572
33750
 
33751
+ /* begin_public_function
33752
+ id: poll.get
33753
+ */
33754
+ /**
33755
+ * ```js
33756
+ * import { PollRepository } from '@amityco/ts-sdk';
33757
+ *
33758
+ * let poll;
33759
+ *
33760
+ * const unsub = PollRepository.getPoll(commentId, response => {
33761
+ * poll = response.data;
33762
+ * });
33763
+ * ```
33764
+ *
33765
+ * Observe all mutation on a given {@link Amity.Poll}
33766
+ *
33767
+ * @param pollId the ID of the poll to observe
33768
+ * @param callback the function to call when new data are available
33769
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the poll
33770
+ *
33771
+ * @category Poll Live Object
33772
+ */
33773
+ const getPoll = (pollId, callback) => {
33774
+ return liveObject(pollId, callback, 'pollId', getPoll$1, [onPollUpdated, onPollDeleted]);
33775
+ };
33776
+ /* end_public_function */
33777
+
33573
33778
  var index = /*#__PURE__*/Object.freeze({
33574
33779
  __proto__: null,
33575
- getPoll: getPoll,
33576
33780
  createPoll: createPoll,
33577
33781
  closePoll: closePoll,
33578
33782
  deletePoll: deletePoll,
33579
33783
  votePoll: votePoll,
33580
33784
  onPollUpdated: onPollUpdated,
33581
33785
  onPollDeleted: onPollDeleted,
33582
- observePoll: observePoll
33786
+ observePoll: observePoll,
33787
+ getPoll: getPoll
33583
33788
  });
33584
33789
 
33585
33790
  /**