@amityco/ts-sdk-react-native 6.23.1-d8b4c4c.0 → 6.24.1-13ac472.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 (147) hide show
  1. package/dist/@types/core/events.d.ts +0 -2
  2. package/dist/@types/core/events.d.ts.map +1 -1
  3. package/dist/@types/core/model.d.ts +2 -4
  4. package/dist/@types/core/model.d.ts.map +1 -1
  5. package/dist/@types/core/payload.d.ts +1 -2
  6. package/dist/@types/core/payload.d.ts.map +1 -1
  7. package/dist/@types/domains/community.d.ts +1 -15
  8. package/dist/@types/domains/community.d.ts.map +1 -1
  9. package/dist/@types/domains/stream.d.ts +3 -19
  10. package/dist/@types/domains/stream.d.ts.map +1 -1
  11. package/dist/client/api/secureLogout.d.ts +1 -1
  12. package/dist/communityRepository/api/createCommunity.d.ts.map +1 -1
  13. package/dist/communityRepository/api/getCommunities.d.ts.map +1 -1
  14. package/dist/communityRepository/api/getCommunity.d.ts.map +1 -1
  15. package/dist/communityRepository/api/queryCommunities.d.ts.map +1 -1
  16. package/dist/communityRepository/api/updateCommunity.d.ts.map +1 -1
  17. package/dist/communityRepository/communityMembership/observers/getMembers.d.ts.map +1 -1
  18. package/dist/communityRepository/communityMembership/observers/index.d.ts +0 -1
  19. package/dist/communityRepository/communityMembership/observers/index.d.ts.map +1 -1
  20. package/dist/communityRepository/observers/getCommunities.d.ts.map +1 -1
  21. package/dist/communityRepository/observers/searchCommunities.d.ts +1 -1
  22. package/dist/communityRepository/utils/payload.d.ts.map +1 -1
  23. package/dist/core/events.d.ts +3 -3
  24. package/dist/core/events.d.ts.map +1 -1
  25. package/dist/core/model/idResolvers.d.ts.map +1 -1
  26. package/dist/core/model/index.d.ts.map +1 -1
  27. package/dist/index.cjs.js +462 -841
  28. package/dist/index.esm.js +437 -816
  29. package/dist/index.umd.js +4 -4
  30. package/dist/streamRepository/api/createStream.d.ts +4 -4
  31. package/dist/streamRepository/api/createStream.d.ts.map +1 -1
  32. package/dist/streamRepository/api/deleteStream.d.ts +4 -4
  33. package/dist/streamRepository/api/deleteStream.d.ts.map +1 -1
  34. package/dist/streamRepository/api/disposeStream.d.ts +4 -4
  35. package/dist/streamRepository/api/disposeStream.d.ts.map +1 -1
  36. package/dist/streamRepository/api/getStreams.d.ts +39 -0
  37. package/dist/streamRepository/api/getStreams.d.ts.map +1 -0
  38. package/dist/streamRepository/api/queryStreams.d.ts +4 -4
  39. package/dist/streamRepository/api/queryStreams.d.ts.map +1 -1
  40. package/dist/streamRepository/api/updateStream.d.ts.map +1 -1
  41. package/dist/streamRepository/events/index.d.ts +0 -2
  42. package/dist/streamRepository/events/index.d.ts.map +1 -1
  43. package/dist/streamRepository/events/onStreamRecorded.d.ts +2 -2
  44. package/dist/streamRepository/events/onStreamRecorded.d.ts.map +1 -1
  45. package/dist/streamRepository/events/onStreamStarted.d.ts +2 -2
  46. package/dist/streamRepository/events/onStreamStarted.d.ts.map +1 -1
  47. package/dist/streamRepository/events/onStreamStopped.d.ts +2 -2
  48. package/dist/streamRepository/events/onStreamStopped.d.ts.map +1 -1
  49. package/dist/streamRepository/internalApi/queryStreams.d.ts +3 -3
  50. package/dist/streamRepository/internalApi/queryStreams.d.ts.map +1 -1
  51. package/dist/streamRepository/observers/getStreamById.d.ts +0 -1
  52. package/dist/streamRepository/observers/getStreamById.d.ts.map +1 -1
  53. package/dist/streamRepository/observers/getStreams.d.ts +17 -0
  54. package/dist/streamRepository/observers/getStreams.d.ts.map +1 -0
  55. package/dist/utils/linkedObject/index.d.ts +0 -1
  56. package/dist/utils/linkedObject/index.d.ts.map +1 -1
  57. package/dist/utils/liveObject.d.ts +0 -3
  58. package/dist/utils/liveObject.d.ts.map +1 -1
  59. package/package.json +1 -1
  60. package/src/@types/core/events.ts +1 -5
  61. package/src/@types/core/model.ts +2 -4
  62. package/src/@types/core/payload.ts +1 -2
  63. package/src/@types/domains/community.ts +1 -25
  64. package/src/@types/domains/stream.ts +3 -23
  65. package/src/client/api/secureLogout.ts +1 -1
  66. package/src/communityRepository/api/createCommunity.ts +2 -5
  67. package/src/communityRepository/api/getCommunities.ts +1 -5
  68. package/src/communityRepository/api/getCommunity.ts +1 -5
  69. package/src/communityRepository/api/queryCommunities.ts +2 -2
  70. package/src/communityRepository/api/updateCommunity.ts +1 -5
  71. package/src/communityRepository/communityMembership/observers/getMembers.ts +128 -15
  72. package/src/communityRepository/communityMembership/observers/index.ts +0 -1
  73. package/src/communityRepository/observers/getCommunities.ts +150 -7
  74. package/src/communityRepository/observers/searchCommunities.ts +1 -1
  75. package/src/communityRepository/utils/payload.ts +1 -35
  76. package/src/core/model/idResolvers.ts +0 -1
  77. package/src/core/model/index.ts +0 -1
  78. package/src/streamRepository/api/createStream.ts +5 -8
  79. package/src/streamRepository/api/deleteStream.ts +4 -6
  80. package/src/streamRepository/api/disposeStream.ts +5 -5
  81. package/src/streamRepository/api/getStreams.ts +81 -0
  82. package/src/streamRepository/api/queryStreams.ts +4 -4
  83. package/src/streamRepository/api/updateStream.ts +1 -2
  84. package/src/streamRepository/events/index.ts +0 -2
  85. package/src/streamRepository/events/onStreamRecorded.ts +2 -4
  86. package/src/streamRepository/events/onStreamStarted.ts +2 -4
  87. package/src/streamRepository/events/onStreamStopped.ts +2 -4
  88. package/src/streamRepository/internalApi/queryStreams.ts +3 -3
  89. package/src/streamRepository/observers/getStreamById.ts +5 -18
  90. package/src/streamRepository/observers/getStreams.ts +152 -0
  91. package/src/streamRepository/observers/tests/getStreamById.test.ts +1 -1
  92. package/src/utils/linkedObject/index.ts +0 -2
  93. package/src/utils/liveObject.ts +0 -3
  94. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.d.ts +0 -14
  95. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.d.ts.map +0 -1
  96. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.d.ts +0 -9
  97. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.d.ts.map +0 -1
  98. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.d.ts +0 -15
  99. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.d.ts.map +0 -1
  100. package/dist/communityRepository/communityMembership/observers/getMembers/enums.d.ts +0 -10
  101. package/dist/communityRepository/communityMembership/observers/getMembers/enums.d.ts.map +0 -1
  102. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.d.ts +0 -14
  103. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.d.ts.map +0 -1
  104. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.d.ts +0 -9
  105. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.d.ts.map +0 -1
  106. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.d.ts +0 -15
  107. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.d.ts.map +0 -1
  108. package/dist/communityRepository/communityMembership/observers/searchMembers/enums.d.ts +0 -10
  109. package/dist/communityRepository/communityMembership/observers/searchMembers/enums.d.ts.map +0 -1
  110. package/dist/communityRepository/communityMembership/observers/searchMembers.d.ts +0 -20
  111. package/dist/communityRepository/communityMembership/observers/searchMembers.d.ts.map +0 -1
  112. package/dist/communityRepository/utils/saveCommunityUsers.d.ts +0 -2
  113. package/dist/communityRepository/utils/saveCommunityUsers.d.ts.map +0 -1
  114. package/dist/streamRepository/events/onStreamFlagged.d.ts +0 -17
  115. package/dist/streamRepository/events/onStreamFlagged.d.ts.map +0 -1
  116. package/dist/streamRepository/events/onStreamTerminated.d.ts +0 -17
  117. package/dist/streamRepository/events/onStreamTerminated.d.ts.map +0 -1
  118. package/dist/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.d.ts +0 -14
  119. package/dist/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.d.ts.map +0 -1
  120. package/dist/streamRepository/observers/getStreams/GetStreamsPageController.d.ts +0 -14
  121. package/dist/streamRepository/observers/getStreams/GetStreamsPageController.d.ts.map +0 -1
  122. package/dist/streamRepository/observers/getStreams/GetStreamsQueryStreamController.d.ts +0 -15
  123. package/dist/streamRepository/observers/getStreams/GetStreamsQueryStreamController.d.ts.map +0 -1
  124. package/dist/streamRepository/observers/getStreams/getStreams.d.ts +0 -2
  125. package/dist/streamRepository/observers/getStreams/getStreams.d.ts.map +0 -1
  126. package/dist/streamRepository/observers/getStreams/index.d.ts +0 -2
  127. package/dist/streamRepository/observers/getStreams/index.d.ts.map +0 -1
  128. package/dist/utils/linkedObject/streamLinkedObject.d.ts +0 -2
  129. package/dist/utils/linkedObject/streamLinkedObject.d.ts.map +0 -1
  130. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.ts +0 -152
  131. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.ts +0 -26
  132. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.ts +0 -114
  133. package/src/communityRepository/communityMembership/observers/getMembers/enums.ts +0 -9
  134. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.ts +0 -130
  135. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.ts +0 -29
  136. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.ts +0 -105
  137. package/src/communityRepository/communityMembership/observers/searchMembers/enums.ts +0 -9
  138. package/src/communityRepository/communityMembership/observers/searchMembers.ts +0 -60
  139. package/src/communityRepository/utils/saveCommunityUsers.ts +0 -16
  140. package/src/streamRepository/events/onStreamFlagged.ts +0 -37
  141. package/src/streamRepository/events/onStreamTerminated.ts +0 -37
  142. package/src/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.ts +0 -114
  143. package/src/streamRepository/observers/getStreams/GetStreamsPageController.ts +0 -23
  144. package/src/streamRepository/observers/getStreams/GetStreamsQueryStreamController.ts +0 -83
  145. package/src/streamRepository/observers/getStreams/getStreams.ts +0 -32
  146. package/src/streamRepository/observers/getStreams/index.ts +0 -1
  147. package/src/utils/linkedObject/streamLinkedObject.ts +0 -11
package/dist/index.esm.js CHANGED
@@ -83,8 +83,8 @@ const PostContentType = Object.freeze({
83
83
 
84
84
  function getVersion() {
85
85
  try {
86
- // the string ''v6.23.0-esm'' should be replaced by actual value by @rollup/plugin-replace
87
- return 'v6.23.0-esm';
86
+ // the string ''v6.24.0-esm'' should be replaced by actual value by @rollup/plugin-replace
87
+ return 'v6.24.0-esm';
88
88
  }
89
89
  catch (error) {
90
90
  return '__dev__';
@@ -512,7 +512,6 @@ const idResolvers = {
512
512
  reaction: ({ referenceType, referenceId }) => `${referenceType}#${referenceId}`,
513
513
  reactor: ({ reactionId }) => reactionId,
514
514
  stream: ({ streamId }) => streamId,
515
- streamModeration: ({ streamId }) => streamId,
516
515
  follow: ({ from, to }) => `${from}#${to}`,
517
516
  followInfo: ({ userId }) => userId,
518
517
  followCount: ({ userId }) => userId,
@@ -562,7 +561,6 @@ const PAYLOAD2MODEL = {
562
561
  reactors: 'reactor',
563
562
  reactions: 'reaction',
564
563
  videoStreamings: 'stream',
565
- videoStreamModerations: 'streamModeration',
566
564
  follows: 'follow',
567
565
  followCounts: 'followCount',
568
566
  feeds: 'feed',
@@ -21342,13 +21340,6 @@ const storyLinkedObject = (story) => {
21342
21340
  } });
21343
21341
  };
21344
21342
 
21345
- const streamLinkedObject = (stream) => {
21346
- return Object.assign(Object.assign({}, stream), { get moderation() {
21347
- var _a;
21348
- return (_a = pullFromCache(['streamModeration', 'get', stream.streamId])) === null || _a === void 0 ? void 0 : _a.data;
21349
- } });
21350
- };
21351
-
21352
21343
  const categoryLinkedObject = (category) => {
21353
21344
  return Object.assign(Object.assign({}, category), { get avatar() {
21354
21345
  var _a;
@@ -21817,7 +21808,6 @@ const LinkedObject = {
21817
21808
  post: postLinkedObject,
21818
21809
  user: userLinkedObject,
21819
21810
  category: categoryLinkedObject,
21820
- stream: streamLinkedObject,
21821
21811
  story: storyLinkedObject,
21822
21812
  storyTarget: storyTargetLinkedObject,
21823
21813
  message: messageLinkedObject,
@@ -25187,7 +25177,7 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
25187
25177
  */
25188
25178
  /**
25189
25179
  * ```js
25190
- * import { Client } from '@amityco/ts-sdk-react-native'
25180
+ * import { Client } from '@amityco/ts-sdk'
25191
25181
  * const success = await Client.secureLogout()
25192
25182
  * ```
25193
25183
  *
@@ -25543,9 +25533,6 @@ function isEqual(x, y) {
25543
25533
  }));
25544
25534
  }
25545
25535
 
25546
- /**
25547
- * @deprecated This function is deprecated
25548
- */
25549
25536
  const liveObject = (id, callback, key, fetcher, eventHandlers, options) => {
25550
25537
  const { forceDispatch, callbackDataSelector, callbackFilter } = Object.assign({ forceDispatch: false, callbackDataSelector: (data) => data }, options);
25551
25538
  const { cache } = getActiveClient();
@@ -33620,7 +33607,7 @@ const getMembers$1 = (params, callback, config) => {
33620
33607
  *
33621
33608
  * @category Channel Live Collection
33622
33609
  */
33623
- const searchMembers$1 = (params, callback, config) => {
33610
+ const searchMembers = (params, callback, config) => {
33624
33611
  return getMembers$1(params, callback);
33625
33612
  };
33626
33613
  /* end_public_function */
@@ -33631,7 +33618,7 @@ var index$d = /*#__PURE__*/Object.freeze({
33631
33618
  removeMembers: removeMembers$1,
33632
33619
  applyFilter: applyFilter$1,
33633
33620
  getMembers: getMembers$1,
33634
- searchMembers: searchMembers$1
33621
+ searchMembers: searchMembers
33635
33622
  });
33636
33623
 
33637
33624
  /* begin_public_function
@@ -33874,30 +33861,12 @@ var index$b = /*#__PURE__*/Object.freeze({
33874
33861
  prepareChannelPayload: prepareChannelPayload
33875
33862
  });
33876
33863
 
33877
- const saveCommunityUsers = (communities, communityUsers) => {
33878
- if (communities.length === 0 || communityUsers.length === 0)
33879
- return;
33880
- communities.forEach(({ communityId }) => {
33881
- const collection = communityUsers.filter(({ communityId: userCommunityId }) => communityId === userCommunityId);
33882
- pushToCache(['communityUsers', 'collection', communityId], collection);
33883
- });
33884
- };
33885
-
33886
33864
  const getMatchPostSetting = (value) => {
33887
33865
  var _a;
33888
33866
  return (_a = Object.keys(CommunityPostSettingMaps).find(key => value.needApprovalOnPostCreation ===
33889
33867
  CommunityPostSettingMaps[key].needApprovalOnPostCreation &&
33890
33868
  value.onlyAdminCanPost === CommunityPostSettingMaps[key].onlyAdminCanPost)) !== null && _a !== void 0 ? _a : DefaultCommunityPostSetting;
33891
33869
  };
33892
- const convertCommunityUsersToUniqueObject = (communityUsers) => {
33893
- if (!communityUsers)
33894
- return communityUsers;
33895
- const result = {};
33896
- communityUsers.forEach(user => {
33897
- result[user.userId] = user;
33898
- });
33899
- return result;
33900
- };
33901
33870
  const prepareCommunityPayload = (rawPayload) => {
33902
33871
  // Unpack community payload by mapping payload field to postSetting value.
33903
33872
  const communities = rawPayload.communities.map((_a) => {
@@ -33907,18 +33876,7 @@ const prepareCommunityPayload = (rawPayload) => {
33907
33876
  onlyAdminCanPost,
33908
33877
  }) }, restCommunityPayload));
33909
33878
  });
33910
- const mergeCommunityUsers = communities.reduce((acc, { communityId }) => {
33911
- var _a;
33912
- const users = (_a = pullFromCache([
33913
- 'communityUsers',
33914
- 'collection',
33915
- communityId,
33916
- ])) === null || _a === void 0 ? void 0 : _a.data;
33917
- if (!users)
33918
- return acc;
33919
- return Object.assign(Object.assign({}, convertCommunityUsersToUniqueObject(users)), acc);
33920
- }, convertCommunityUsersToUniqueObject(rawPayload.communityUsers));
33921
- const communityUsers = withUsers(Object.values(mergeCommunityUsers));
33879
+ const communityUsers = withUsers(rawPayload.communityUsers);
33922
33880
  const communityWithMembershipStatus = updateMembershipStatus(communities, communityUsers);
33923
33881
  return Object.assign(Object.assign({}, rawPayload), { communities: communityWithMembershipStatus, communityUsers });
33924
33882
  };
@@ -33952,10 +33910,8 @@ const getCommunities$1 = async (communityIds) => {
33952
33910
  });
33953
33911
  const data = prepareCommunityPayload(payload);
33954
33912
  const cachedAt = client.cache && Date.now();
33955
- if (client.cache) {
33913
+ if (client.cache)
33956
33914
  ingestInCache(data, { cachedAt });
33957
- saveCommunityUsers(data.communities, data.communityUsers);
33958
- }
33959
33915
  return {
33960
33916
  data: data.communities,
33961
33917
  cachedAt,
@@ -34017,10 +33973,8 @@ const createCommunity = async (bundle) => {
34017
33973
  fireEvent('community.created', payload);
34018
33974
  const data = prepareCommunityPayload(payload);
34019
33975
  const cachedAt = client.cache && Date.now();
34020
- if (client.cache) {
33976
+ if (client.cache)
34021
33977
  ingestInCache(data, { cachedAt });
34022
- saveCommunityUsers(data.communities, data.communityUsers);
34023
- }
34024
33978
  const { communities } = data;
34025
33979
  return {
34026
33980
  data: communities[0],
@@ -34054,10 +34008,8 @@ const updateCommunity = async (communityId, patch) => {
34054
34008
  fireEvent('community.updated', payload);
34055
34009
  const data = prepareCommunityPayload(payload);
34056
34010
  const cachedAt = client.cache && Date.now();
34057
- if (client.cache) {
34011
+ if (client.cache)
34058
34012
  ingestInCache(data, { cachedAt });
34059
- saveCommunityUsers(data.communities, data.communityUsers);
34060
- }
34061
34013
  const { communities } = data;
34062
34014
  return {
34063
34015
  data: communities.find(community => community.communityId === communityId),
@@ -34087,10 +34039,8 @@ const getCommunity$1 = async (communityId) => {
34087
34039
  const { data: payload } = await client.http.get(`/api/v3/communities/${communityId}`);
34088
34040
  const data = prepareCommunityPayload(payload);
34089
34041
  const cachedAt = client.cache && Date.now();
34090
- if (client.cache) {
34042
+ if (client.cache)
34091
34043
  ingestInCache(data, { cachedAt });
34092
- saveCommunityUsers(data.communities, data.communityUsers);
34093
- }
34094
34044
  const { communities } = data;
34095
34045
  return {
34096
34046
  data: communities.find(community => community.communityId === communityId),
@@ -34633,7 +34583,7 @@ const observeCommunity = (communityId, callback) => {
34633
34583
  * TODO: handle cache receive cache option, and cache policy
34634
34584
  * TODO: check if querybyIds is supported
34635
34585
  */
34636
- class CommunitiesPaginationController$1 extends PaginationController {
34586
+ class CommunitiesPaginationController extends PaginationController {
34637
34587
  async getRequest(queryParams, token) {
34638
34588
  const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT } = queryParams, params = __rest(queryParams, ["limit"]);
34639
34589
  const options = token ? { token } : { limit };
@@ -34644,7 +34594,7 @@ class CommunitiesPaginationController$1 extends PaginationController {
34644
34594
  }
34645
34595
  }
34646
34596
 
34647
- class CommunitiesQueryStreamController$1 extends QueryStreamController {
34597
+ class CommunitiesQueryStreamController extends QueryStreamController {
34648
34598
  constructor(query, cacheKey, notifyChange, preparePayload) {
34649
34599
  super(query, cacheKey);
34650
34600
  this.notifyChange = notifyChange;
@@ -34686,22 +34636,22 @@ class CommunitiesQueryStreamController$1 extends QueryStreamController {
34686
34636
  }
34687
34637
  }
34688
34638
 
34689
- var EnumCommunityActions$1;
34639
+ var EnumCommunityActions;
34690
34640
  (function (EnumCommunityActions) {
34691
34641
  EnumCommunityActions["OnCommunityCreated"] = "onCommunityCreated";
34692
34642
  EnumCommunityActions["OnCommunityDeleted"] = "onCommunityDeleted";
34693
34643
  EnumCommunityActions["OnCommunityUpdated"] = "onCommunityUpdated";
34694
- })(EnumCommunityActions$1 || (EnumCommunityActions$1 = {}));
34644
+ })(EnumCommunityActions || (EnumCommunityActions = {}));
34695
34645
 
34696
34646
  /* eslint-disable no-use-before-define */
34697
34647
  class SearchCommunityLiveCollectionController extends LiveCollectionController {
34698
34648
  constructor(query, callback) {
34699
34649
  const queryStreamId = hash(query);
34700
34650
  const cacheKey = ['community', 'collection', queryStreamId];
34701
- const paginationController = new CommunitiesPaginationController$1(query);
34651
+ const paginationController = new CommunitiesPaginationController(query);
34702
34652
  super(paginationController, queryStreamId, cacheKey, callback);
34703
34653
  this.query = query;
34704
- this.queryStreamController = new CommunitiesQueryStreamController$1(this.query, this.cacheKey, this.notifyChange.bind(this), prepareCommunityPayload);
34654
+ this.queryStreamController = new CommunitiesQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this), prepareCommunityPayload);
34705
34655
  this.callback = callback.bind(this);
34706
34656
  this.loadPage({ initial: true });
34707
34657
  }
@@ -34723,8 +34673,8 @@ class SearchCommunityLiveCollectionController extends LiveCollectionController {
34723
34673
  }
34724
34674
  startSubscription() {
34725
34675
  return this.queryStreamController.subscribeRTE([
34726
- { fn: onCommunityDeleted, action: EnumCommunityActions$1.OnCommunityDeleted },
34727
- { fn: onCommunityUpdated, action: EnumCommunityActions$1.OnCommunityUpdated },
34676
+ { fn: onCommunityDeleted, action: EnumCommunityActions.OnCommunityDeleted },
34677
+ { fn: onCommunityUpdated, action: EnumCommunityActions.OnCommunityUpdated },
34728
34678
  ]);
34729
34679
  }
34730
34680
  notifyChange({ origin, loading, error }) {
@@ -34770,7 +34720,7 @@ class SearchCommunityLiveCollectionController extends LiveCollectionController {
34770
34720
  */
34771
34721
  /**
34772
34722
  * ```js
34773
- * import { CommunityRepository } from '@amityco/ts-sdk-react-native'
34723
+ * import { CommunityRepository } from '@amityco/ts-sdk'
34774
34724
  *
34775
34725
  * let communities = []
34776
34726
  * const unsub = CommunityRepository.searchCommunities({
@@ -34805,163 +34755,50 @@ const searchCommunities = (params, callback, config) => {
34805
34755
  /* end_public_function */
34806
34756
 
34807
34757
  /**
34808
- * TODO: handle cache receive cache option, and cache policy
34809
- * TODO: check if querybyIds is supported
34758
+ * ```js
34759
+ * import { queryCommunities } from '@amityco/ts-sdk-react-native'
34760
+ * const communities = await queryCommunities()
34761
+ * ```
34762
+ *
34763
+ * Queries a paginable list of {@link Amity.Community} objects
34764
+ * Search is performed by displayName such as `.startsWith(search)`
34765
+ *
34766
+ * @param query The query parameters
34767
+ * @returns A page of {@link Amity.Community} objects
34768
+ *
34769
+ * @category Community API
34770
+ * @async
34810
34771
  */
34811
- class CommunitiesPaginationController extends PaginationController {
34812
- async getRequest(queryParams, token) {
34813
- const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT } = queryParams, params = __rest(queryParams, ["limit"]);
34814
- const options = token ? { token } : { limit };
34815
- const { data: queryResponse } = await this.http.get(`/api/v3/communities`, {
34816
- params: Object.assign(Object.assign({}, params), { isDeleted: inferIsDeleted(params.includeDeleted), keyword: params.displayName, filter: params.membership, options }),
34817
- });
34818
- return queryResponse;
34819
- }
34820
- }
34821
-
34822
- var EnumCommunityActions;
34823
- (function (EnumCommunityActions) {
34824
- EnumCommunityActions["OnCommunityCreated"] = "onCommunityCreated";
34825
- EnumCommunityActions["OnCommunityDeleted"] = "onCommunityDeleted";
34826
- EnumCommunityActions["OnCommunityUpdated"] = "onCommunityUpdated";
34827
- })(EnumCommunityActions || (EnumCommunityActions = {}));
34828
-
34829
- class CommunitiesQueryStreamController extends QueryStreamController {
34830
- constructor(query, cacheKey, notifyChange, preparePayload) {
34831
- super(query, cacheKey);
34832
- this.notifyChange = notifyChange;
34833
- this.preparePayload = preparePayload;
34834
- }
34835
- async saveToMainDB(response) {
34836
- const processedPayload = await this.preparePayload(response);
34837
- const client = getActiveClient();
34838
- const cachedAt = client.cache && Date.now();
34839
- if (client.cache) {
34840
- ingestInCache(processedPayload, { cachedAt });
34841
- }
34842
- }
34843
- appendToQueryStream(response, direction, refresh = false) {
34844
- var _a, _b;
34845
- if (refresh) {
34846
- pushToCache(this.cacheKey, {
34847
- data: response.communities.map(getResolver('community')),
34848
- });
34849
- }
34850
- else {
34851
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
34852
- const communities = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
34853
- pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: [...new Set([...communities, ...response.communities.map(getResolver('community'))])] }));
34854
- }
34855
- }
34856
- reactor(action) {
34857
- return (community) => {
34858
- var _a;
34859
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
34860
- if (!collection)
34861
- return;
34862
- if (this.query.displayName && action === EnumCommunityActions.OnCommunityCreated) {
34863
- return;
34864
- }
34865
- /*
34866
- * Simply update collection and let responder decide what to do with data
34867
- */
34868
- collection.data = [...new Set([community.communityId, ...collection.data])];
34869
- pushToCache(this.cacheKey, collection);
34870
- this.notifyChange({ origin: "event" /* Amity.LiveDataOrigin.EVENT */, loading: false });
34871
- };
34872
- }
34873
- subscribeRTE(createSubscriber) {
34874
- return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
34875
- }
34876
- }
34877
-
34878
- /* eslint-disable no-use-before-define */
34879
- class CommunityLiveCollectionController extends LiveCollectionController {
34880
- constructor(query, callback) {
34881
- const queryStreamId = hash(query);
34882
- const cacheKey = ['community', 'collection', queryStreamId];
34883
- const paginationController = new CommunitiesPaginationController(query);
34884
- super(paginationController, queryStreamId, cacheKey, callback);
34885
- this.query = query;
34886
- this.queryStreamController = new CommunitiesQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this), prepareCommunityPayload);
34887
- this.callback = callback.bind(this);
34888
- this.loadPage({ initial: true });
34889
- }
34890
- setup() {
34891
- var _a;
34892
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
34893
- if (!collection) {
34894
- pushToCache(this.cacheKey, {
34895
- data: [],
34896
- params: {},
34897
- });
34898
- }
34899
- }
34900
- async persistModel(queryPayload) {
34901
- await this.queryStreamController.saveToMainDB(queryPayload);
34902
- }
34903
- persistQueryStream({ response, direction, refresh, }) {
34904
- this.queryStreamController.appendToQueryStream(response, direction, refresh);
34905
- }
34906
- startSubscription() {
34907
- return this.queryStreamController.subscribeRTE([
34908
- { fn: onCommunityCreated, action: EnumCommunityActions.OnCommunityCreated },
34909
- { fn: onCommunityDeleted, action: EnumCommunityActions.OnCommunityDeleted },
34910
- { fn: onCommunityUpdated, action: EnumCommunityActions.OnCommunityUpdated },
34911
- ]);
34912
- }
34913
- notifyChange({ origin, loading, error }) {
34914
- var _a, _b;
34915
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
34916
- if (!collection)
34917
- return;
34918
- const data = this.applyFilter((_b = collection.data
34919
- .map(id => pullFromCache(['community', 'get', id]))
34920
- .filter(Boolean)
34921
- .map(({ data }) => data)) !== null && _b !== void 0 ? _b : []);
34922
- if (!this.shouldNotify(data) && origin === 'event')
34923
- return;
34924
- this.callback({
34925
- onNextPage: () => this.loadPage({ direction: "next" /* Amity.LiveCollectionPageDirection.NEXT */ }),
34926
- data,
34927
- hasNextPage: !!this.paginationController.getNextToken(),
34928
- loading,
34929
- error,
34930
- });
34931
- }
34932
- applyFilter(data) {
34933
- const { userId } = getActiveClient();
34934
- let communities = data;
34935
- if (this.query.includeDeleted) {
34936
- communities = filterByPropEquality(communities, 'isDeleted', false);
34937
- }
34938
- if (this.query.categoryId) {
34939
- communities = communities.filter(c => { var _a; return (_a = c.categoryIds) === null || _a === void 0 ? void 0 : _a.includes(this.query.categoryId); });
34940
- }
34941
- if (this.query.tags) {
34942
- communities = communities.filter(c => { var _a; return (_a = c.tags) === null || _a === void 0 ? void 0 : _a.some(t => { var _a; return (_a = this.query.tags) === null || _a === void 0 ? void 0 : _a.includes(t); }); });
34943
- }
34944
- if (this.query.membership && userId) {
34945
- communities = filterByCommunityMembership(communities, this.query.membership, userId);
34946
- }
34947
- if (!this.query.displayName) {
34948
- const sortFn = (() => {
34949
- switch (this.query.sortBy) {
34950
- case 'firstCreated':
34951
- return sortByFirstCreated;
34952
- case 'lastCreated':
34953
- return sortByLastCreated;
34954
- case 'displayName':
34955
- return sortByDisplayName;
34956
- default:
34957
- return sortByLastCreated;
34958
- }
34959
- })();
34960
- communities = communities.sort(sortFn);
34961
- }
34962
- return communities;
34772
+ const queryCommunities = async (query) => {
34773
+ const client = getActiveClient();
34774
+ client.log('channel/queryCommunities', query);
34775
+ // safe decapsulation
34776
+ const _a = query !== null && query !== void 0 ? query : {}, { page, limit = 10 } = _a, params = __rest(_a, ["page", "limit"]);
34777
+ const _b = params !== null && params !== void 0 ? params : {}, { displayName, membership, includeDeleted } = _b, restParams = __rest(_b, ["displayName", "membership", "includeDeleted"]);
34778
+ const options = (() => {
34779
+ if (page)
34780
+ return { token: page };
34781
+ if (limit)
34782
+ return { limit };
34783
+ return undefined;
34784
+ })();
34785
+ // API-FIX: parameters should be querystring.
34786
+ // API-FIX: backend doesn't answer Amity.Response
34787
+ // const { data } = await client.http.get<Amity.Response<Amity.PagedResponse<CommunityPayload>>>(
34788
+ const { data } = await client.http.get(`/api/v3/communities`, {
34789
+ params: Object.assign(Object.assign({}, restParams), { isDeleted: inferIsDeleted(includeDeleted), keyword: displayName, filter: membership, options }),
34790
+ });
34791
+ const { paging } = data, payload = __rest(data, ["paging"]);
34792
+ const unpackedPayload = prepareCommunityPayload(payload);
34793
+ const { communities } = unpackedPayload;
34794
+ const cachedAt = client.cache && Date.now();
34795
+ if (client.cache) {
34796
+ ingestInCache(unpackedPayload, { cachedAt });
34797
+ const cacheKey = ['community', 'query', Object.assign(Object.assign({}, params), { options })];
34798
+ pushToCache(cacheKey, { communities: communities.map(getResolver('community')), paging });
34963
34799
  }
34964
- }
34800
+ return { data: communities, cachedAt, paging };
34801
+ };
34965
34802
 
34966
34803
  /* begin_public_function
34967
34804
  id: community.query
@@ -34985,15 +34822,98 @@ class CommunityLiveCollectionController extends LiveCollectionController {
34985
34822
  * @category Community Live Collection
34986
34823
  */
34987
34824
  const getCommunities = (params, callback, config) => {
34988
- const { log, cache } = getActiveClient();
34825
+ const { log, cache, userId } = getActiveClient();
34989
34826
  if (!cache) {
34990
34827
  console.log(ENABLE_CACHE_MESSAGE);
34991
34828
  }
34992
34829
  const timestamp = Date.now();
34993
34830
  log(`getCommunities(tmpid: ${timestamp}) > listen`);
34994
- const communitiesLiveCollection = new CommunityLiveCollectionController(params, callback);
34995
- const disposers = communitiesLiveCollection.startSubscription();
34996
- const cacheKey = communitiesLiveCollection.getCacheKey();
34831
+ const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
34832
+ const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
34833
+ const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
34834
+ const disposers = [];
34835
+ const cacheKey = ['community', 'collection', queryParams];
34836
+ const responder = (data) => {
34837
+ var _a, _b;
34838
+ let communities = (_a = data.data
34839
+ .map(communityId => pullFromCache(['community', 'get', communityId]))
34840
+ .filter(Boolean)
34841
+ .map(({ data }) => data)) !== null && _a !== void 0 ? _a : [];
34842
+ communities = filterByStringComparePartially(communities, 'displayName', params.displayName);
34843
+ if (!params.includeDeleted) {
34844
+ communities = filterByPropEquality(communities, 'isDeleted', false);
34845
+ }
34846
+ if (params.categoryId) {
34847
+ communities = communities.filter(c => { var _a; return (_a = c.categoryIds) === null || _a === void 0 ? void 0 : _a.includes(params.categoryId); });
34848
+ }
34849
+ if (params.tags) {
34850
+ 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); }); });
34851
+ }
34852
+ if (params.membership && userId) {
34853
+ communities = filterByCommunityMembership(communities, params.membership, userId);
34854
+ }
34855
+ const sortBy = params.sortBy || 'lastCreated';
34856
+ if (sortBy === 'lastCreated' || sortBy === 'firstCreated') {
34857
+ communities = communities.sort(sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
34858
+ }
34859
+ /*
34860
+ * The server returns communities with empty | null displayName's first before
34861
+ * returning sorted list of communities with displayNames
34862
+ *
34863
+ * This section needs to be updated as displayNames can be null as well
34864
+ */
34865
+ if (sortBy === 'displayName') {
34866
+ communities = communities
34867
+ // this needs to be aligned with the backend data type
34868
+ .map(c => (c.displayName ? c : Object.assign(Object.assign({}, c), { displayName: '' })))
34869
+ // @ts-ignore
34870
+ .sort(sortByDisplayName);
34871
+ }
34872
+ callback({
34873
+ onNextPage: onFetch,
34874
+ data: communities,
34875
+ hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
34876
+ loading: data.loading,
34877
+ error: data.error,
34878
+ });
34879
+ };
34880
+ const realtimeRouter = (_) => (community) => {
34881
+ var _a;
34882
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
34883
+ if (!collection)
34884
+ return;
34885
+ /*
34886
+ * Simply update collection and let responder decide what to do with data
34887
+ */
34888
+ collection.data = [...new Set([community.communityId, ...collection.data])];
34889
+ pushToCache(cacheKey, collection);
34890
+ responder(collection);
34891
+ };
34892
+ const onFetch = (initial = false) => {
34893
+ var _a, _b;
34894
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
34895
+ const communities = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
34896
+ if (!initial && communities.length > 0 && !(collection === null || collection === void 0 ? void 0 : collection.params.page))
34897
+ return;
34898
+ const query = createQuery(queryCommunities, Object.assign(Object.assign({}, queryParams), { limit: initial ? limit : undefined, page: !initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined }));
34899
+ runQuery(query, ({ data: result, error, loading, paging }) => {
34900
+ const data = {
34901
+ loading,
34902
+ error,
34903
+ params: { page: paging === null || paging === void 0 ? void 0 : paging.next },
34904
+ data: communities,
34905
+ };
34906
+ if (result) {
34907
+ data.data = initial
34908
+ ? result.map(getResolver('community'))
34909
+ : [...new Set([...communities, ...result.map(getResolver('community'))])];
34910
+ }
34911
+ pushToCache(cacheKey, data);
34912
+ responder(data);
34913
+ }, queryOptions(policy, CACHE_SHORTEN_LIFESPAN));
34914
+ };
34915
+ disposers.push(onCommunityCreated(realtimeRouter()), onCommunityDeleted(realtimeRouter()), onCommunityUpdated(realtimeRouter()), onCommunityJoined(realtimeRouter()), onCommunityLeft(realtimeRouter()), onCommunityUserChanged(realtimeRouter()));
34916
+ onFetch(true);
34997
34917
  disposers.push(() => dropFromCache(cacheKey));
34998
34918
  return () => {
34999
34919
  log(`getCommunities(tmpid: ${timestamp}) > dispose`);
@@ -35522,187 +35442,87 @@ const removeMembers = async (communityId, userIds) => {
35522
35442
  /* end_public_function */
35523
35443
 
35524
35444
  /**
35525
- * TODO: handle cache receive cache option, and cache policy
35526
- * TODO: check if querybyIds is supported
35527
- */
35528
- class CommunityMembersPaginationController extends PaginationController {
35529
- async getRequest(queryParams, token) {
35530
- const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT } = queryParams, params = __rest(queryParams, ["limit"]);
35531
- const options = token ? { token } : { limit };
35532
- const { data: queryResponse } = await this.http.get(`/api/v3/communities/${params.communityId}/users`, {
35533
- params: Object.assign(Object.assign({}, params), { options }),
35534
- });
35535
- return queryResponse;
35536
- }
35537
- }
35538
-
35539
- var EnumCommunityMemberActions$1;
35540
- (function (EnumCommunityMemberActions) {
35541
- EnumCommunityMemberActions["OnCommunityJoined"] = "onCommunityJoined";
35542
- EnumCommunityMemberActions["OnCommunityLeft"] = "onCommunityLeft";
35543
- EnumCommunityMemberActions["OnCommunityUserBanned"] = "onCommunityUserBanned";
35544
- EnumCommunityMemberActions["OnCommunityUserChanged"] = "onCommunityUserChanged";
35545
- EnumCommunityMemberActions["OnCommunityUserRoleAdded"] = "onCommunityUserRoleAdded";
35546
- EnumCommunityMemberActions["OnCommunityUserRoleRemoved"] = "onCommunityUserRoleRemoved";
35547
- EnumCommunityMemberActions["OnCommunityUserUnbanned"] = "onCommunityUserUnbanned";
35548
- })(EnumCommunityMemberActions$1 || (EnumCommunityMemberActions$1 = {}));
35549
-
35550
- class CommunityMembersQueryStreamController extends QueryStreamController {
35551
- constructor(query, cacheKey, notifyChange, preparePayload) {
35552
- super(query, cacheKey);
35553
- this.notifyChange = notifyChange;
35554
- this.preparePayload = preparePayload;
35555
- }
35556
- async saveToMainDB(response) {
35557
- const processedPayload = await this.preparePayload(response);
35558
- const client = getActiveClient();
35559
- const cachedAt = client.cache && Date.now();
35560
- if (client.cache) {
35561
- ingestInCache(processedPayload, { cachedAt });
35562
- }
35563
- }
35564
- appendToQueryStream(response, direction, refresh = false) {
35565
- var _a, _b;
35566
- if (refresh) {
35567
- pushToCache(this.cacheKey, {
35568
- data: response.communityUsers.map(({ communityId, userId }) => getResolver('communityUsers')({ communityId, userId })),
35569
- });
35570
- }
35571
- else {
35572
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35573
- const communityUsers = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
35574
- pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: [
35575
- ...new Set([
35576
- ...communityUsers,
35577
- ...response.communityUsers.map(({ communityId, userId }) => getResolver('communityUsers')({ communityId, userId })),
35578
- ]),
35579
- ] }));
35580
- }
35581
- }
35582
- reactor(action) {
35583
- return (community, communityMembers) => {
35584
- var _a;
35585
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35586
- if (!collection)
35587
- return;
35588
- if (action === EnumCommunityMemberActions$1.OnCommunityUserRoleAdded &&
35589
- this.query.sortBy === 'displayName') {
35590
- return;
35591
- }
35592
- communityMembers.forEach(communityMember => {
35593
- const communityMemberCacheId = getResolver('communityUsers')({
35594
- communityId: this.query.communityId,
35595
- userId: communityMember.userId,
35596
- });
35597
- if (communityMember.communityMembership === 'none') {
35598
- collection.data = collection.data.filter(m => m !== communityMemberCacheId);
35599
- }
35600
- else if (!collection.data.includes(communityMemberCacheId)) {
35601
- collection.data = [communityMemberCacheId, ...collection.data];
35602
- }
35603
- });
35604
- pushToCache(this.cacheKey, collection);
35605
- this.notifyChange({ origin: "event" /* Amity.LiveDataOrigin.EVENT */, loading: false });
35606
- };
35607
- }
35608
- subscribeRTE(createSubscriber) {
35609
- return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
35610
- }
35611
- }
35612
-
35613
- /* eslint-disable no-use-before-define */
35614
- class CommunityMembersLiveCollectionController extends LiveCollectionController {
35615
- constructor(query, callback) {
35616
- const queryStreamId = hash(query);
35617
- const cacheKey = ['communityUsers', 'collection', queryStreamId];
35618
- const paginationController = new CommunityMembersPaginationController(query);
35619
- super(paginationController, queryStreamId, cacheKey, callback);
35620
- this.query = query;
35621
- this.queryStreamController = new CommunityMembersQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this), prepareCommunityPayload);
35622
- this.callback = callback.bind(this);
35623
- this.loadPage({ initial: true });
35624
- }
35625
- setup() {
35626
- var _a;
35627
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35628
- if (!collection) {
35629
- pushToCache(this.cacheKey, {
35630
- data: [],
35631
- params: {},
35632
- });
35633
- }
35634
- }
35635
- async persistModel(queryPayload) {
35636
- await this.queryStreamController.saveToMainDB(queryPayload);
35637
- }
35638
- persistQueryStream({ response, direction, refresh, }) {
35639
- this.queryStreamController.appendToQueryStream(response, direction, refresh);
35640
- }
35641
- startSubscription() {
35642
- return this.queryStreamController.subscribeRTE([
35643
- { fn: onCommunityJoined, action: EnumCommunityMemberActions$1.OnCommunityJoined },
35644
- { fn: onCommunityLeft, action: EnumCommunityMemberActions$1.OnCommunityLeft },
35645
- { fn: onCommunityUserBanned, action: EnumCommunityMemberActions$1.OnCommunityUserBanned },
35646
- { fn: onCommunityUserChanged, action: EnumCommunityMemberActions$1.OnCommunityUserChanged },
35647
- { fn: onCommunityUserRoleAdded, action: EnumCommunityMemberActions$1.OnCommunityUserRoleAdded },
35648
- {
35649
- fn: onCommunityUserRoleRemoved,
35650
- action: EnumCommunityMemberActions$1.OnCommunityUserRoleRemoved,
35651
- },
35652
- { fn: onCommunityUserUnbanned, action: EnumCommunityMemberActions$1.OnCommunityUserUnbanned },
35653
- ]);
35654
- }
35655
- notifyChange({ origin, loading, error }) {
35656
- var _a, _b;
35657
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35658
- if (!collection)
35659
- return;
35660
- const data = this.applyFilter((_b = collection.data
35661
- .map(id => pullFromCache(['communityUsers', 'get', id]))
35662
- .filter(isNonNullable)
35663
- .map(({ data }) => data)) !== null && _b !== void 0 ? _b : []);
35664
- if (!this.shouldNotify(data) && origin === 'event')
35665
- return;
35666
- this.callback({
35667
- onNextPage: () => this.loadPage({ direction: "next" /* Amity.LiveCollectionPageDirection.NEXT */ }),
35668
- data,
35669
- hasNextPage: !!this.paginationController.getNextToken(),
35670
- loading,
35671
- error,
35445
+ * ```js
35446
+ * import { queryCommunityMembers } from '@amityco/ts-sdk-react-native'
35447
+ * const communityMembers = await queryCommunityMembers({ communityId: 'foo' })
35448
+ * ```
35449
+ *
35450
+ * Queries a paginable list of {@link Amity.CommunityUser} objects
35451
+ *
35452
+ * @param query The query parameters
35453
+ * @returns A page of {@link Amity.CommunityUser} objects
35454
+ *
35455
+ * @category Community API
35456
+ * @async
35457
+ * */
35458
+ const queryCommunityMembers = async (query) => {
35459
+ const client = getActiveClient();
35460
+ client.log('community/queryCommunityMembers', query);
35461
+ const _a = query !== null && query !== void 0 ? query : {}, { page, limit } = _a, params = __rest(_a, ["page", "limit"]);
35462
+ const options = (() => {
35463
+ if (page)
35464
+ return { token: page };
35465
+ if (limit)
35466
+ return { limit };
35467
+ return undefined;
35468
+ })();
35469
+ const { data: queryPayload } = await client.http.get(`/api/v3/communities/${params.communityId}/users`, {
35470
+ params: Object.assign(Object.assign({}, params), { options }),
35471
+ });
35472
+ const { paging } = queryPayload, payload = __rest(queryPayload, ["paging"]);
35473
+ const preparedPayload = prepareMembershipPayload(payload, 'communityUsers');
35474
+ const { communityUsers } = preparedPayload;
35475
+ const cachedAt = client.cache && Date.now();
35476
+ if (client.cache) {
35477
+ ingestInCache(preparedPayload, { cachedAt });
35478
+ const cacheKey = ['communityUsers', 'query', Object.assign(Object.assign({}, params), { options })];
35479
+ pushToCache(cacheKey, {
35480
+ communityUsers: communityUsers.map(({ communityId, userId }) => getResolver('communityUsers')({ communityId, userId })),
35481
+ paging,
35672
35482
  });
35673
35483
  }
35674
- applyFilter(data) {
35675
- let communityMembers = filterByPropIntersection(data, 'roles', this.query.roles);
35676
- if (this.query.memberships) {
35677
- communityMembers = communityMembers.filter(({ communityMembership }) => {
35678
- const memberships = this.query.memberships || [];
35679
- return memberships.includes(communityMembership);
35680
- });
35681
- }
35682
- if (this.query.search) {
35683
- communityMembers = filterBySearchTerm(communityMembers, this.query.search);
35684
- }
35685
- switch (this.query.sortBy) {
35686
- case 'firstCreated':
35687
- communityMembers = communityMembers.sort(sortByFirstCreated);
35688
- break;
35689
- case 'displayName':
35690
- communityMembers = communityMembers.sort((a, b) => {
35691
- var _a, _b, _c, _d;
35692
- if (((_a = a === null || a === void 0 ? void 0 : a.user) === null || _a === void 0 ? void 0 : _a.displayName) && ((_b = b === null || b === void 0 ? void 0 : b.user) === null || _b === void 0 ? void 0 : _b.displayName)) {
35693
- return (_c = a === null || a === void 0 ? void 0 : a.user) === null || _c === void 0 ? void 0 : _c.displayName.localeCompare((_d = b === null || b === void 0 ? void 0 : b.user) === null || _d === void 0 ? void 0 : _d.displayName);
35694
- }
35695
- return 0;
35696
- });
35697
- break;
35698
- case 'lastCreated':
35699
- default:
35700
- communityMembers = communityMembers.sort(sortByLastCreated);
35701
- break;
35702
- }
35703
- return communityMembers;
35704
- }
35705
- }
35484
+ return { data: communityUsers, cachedAt, paging };
35485
+ };
35486
+ /**
35487
+ * ```js
35488
+ * import { queryCommunityMembers } from '@amityco/ts-sdk-react-native'
35489
+ * const communityMembers = await queryCommunityMembers(query)
35490
+ * ```
35491
+ *
35492
+ * Queries a paginable list of {@link Amity.InternalPost} objects from cache
35493
+ *
35494
+ * @param query The query parameters
35495
+ * @returns posts
35496
+ *
35497
+ * @category Post API
35498
+ */
35499
+ queryCommunityMembers.locally = (query) => {
35500
+ var _a, _b;
35501
+ const client = getActiveClient();
35502
+ client.log('community/queryCommunityMembers', query);
35503
+ if (!client.cache)
35504
+ return;
35505
+ const _c = query !== null && query !== void 0 ? query : {}, { page, limit } = _c, params = __rest(_c, ["page", "limit"]);
35506
+ const options = (() => {
35507
+ if (page)
35508
+ return { token: page };
35509
+ if (limit)
35510
+ return { limit };
35511
+ return undefined;
35512
+ })();
35513
+ const cacheKey = ['communityUsers', 'query', Object.assign(Object.assign({}, params), { options })];
35514
+ const { data, cachedAt } = (_a = pullFromCache(cacheKey)) !== null && _a !== void 0 ? _a : {};
35515
+ if (!(data === null || data === void 0 ? void 0 : data.communityUsers.length))
35516
+ return;
35517
+ const communityUsers = data.communityUsers
35518
+ .map(key => pullFromCache(['communityUsers', 'get', key]))
35519
+ .filter(Boolean)
35520
+ .map(({ data }) => data);
35521
+ const { paging } = data;
35522
+ return communityUsers.length === ((_b = data === null || data === void 0 ? void 0 : data.communityUsers) === null || _b === void 0 ? void 0 : _b.length)
35523
+ ? { data: communityUsers, cachedAt, paging }
35524
+ : undefined;
35525
+ };
35706
35526
 
35707
35527
  /*
35708
35528
  * Exported for testing
@@ -35751,215 +35571,79 @@ const getMembers = (params, callback, config) => {
35751
35571
  }
35752
35572
  const timestamp = Date.now();
35753
35573
  log(`getMembers(tmpid: ${timestamp}) > listen`);
35754
- const communityMemberLiveCollection = new CommunityMembersLiveCollectionController(params, resp => {
35755
- console.log('resp', resp);
35756
- callback(resp);
35757
- });
35758
- const disposers = communityMemberLiveCollection.startSubscription();
35759
- const cacheKey = communityMemberLiveCollection.getCacheKey();
35760
- disposers.push(() => {
35761
- dropFromCache(cacheKey);
35762
- });
35763
- return () => {
35764
- log(`getMembers(tmpid: ${timestamp}) > dispose`);
35765
- disposers.forEach(fn => fn());
35766
- };
35767
- };
35768
- /* end_public_function */
35769
-
35770
- /**
35771
- * TODO: handle cache receive cache option, and cache policy
35772
- * TODO: check if querybyIds is supported
35773
- */
35774
- class SearchCommunityMembersPaginationController extends PaginationController {
35775
- async getRequest(queryParams, token) {
35776
- const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT } = queryParams, params = __rest(queryParams, ["limit"]);
35777
- const options = token ? { token } : { limit };
35778
- const { data: queryResponse } = await this.http.get(`/api/v3/communities/${params.communityId}/users`, {
35779
- params: Object.assign(Object.assign({}, params), { options }),
35780
- });
35781
- return queryResponse;
35782
- }
35783
- }
35784
-
35785
- class SearchCommunityMembersQueryStreamController extends QueryStreamController {
35786
- constructor(query, cacheKey, notifyChange, preparePayload) {
35787
- super(query, cacheKey);
35788
- this.notifyChange = notifyChange;
35789
- this.preparePayload = preparePayload;
35790
- }
35791
- async saveToMainDB(response) {
35792
- const processedPayload = await this.preparePayload(response);
35793
- const client = getActiveClient();
35794
- const cachedAt = client.cache && Date.now();
35795
- if (client.cache) {
35796
- ingestInCache(processedPayload, { cachedAt });
35797
- }
35798
- }
35799
- appendToQueryStream(response, direction, refresh = false) {
35574
+ const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
35575
+ const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
35576
+ const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
35577
+ const disposers = [];
35578
+ const cacheKey = ['communityUsers', 'collection', queryParams];
35579
+ const cacheByCommunityIdKey = [
35580
+ 'communityUsers',
35581
+ 'collection',
35582
+ { communityId: params.communityId },
35583
+ ];
35584
+ const responder = (data, isEventModel = false) => {
35800
35585
  var _a, _b;
35801
- if (refresh) {
35802
- pushToCache(this.cacheKey, {
35803
- data: response.communityUsers.map(({ communityId, userId }) => getResolver('communityUsers')({ communityId, userId })),
35804
- });
35805
- }
35806
- else {
35807
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35808
- const communityUsers = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
35809
- pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: [
35810
- ...new Set([
35811
- ...communityUsers,
35812
- ...response.communityUsers.map(({ communityId, userId }) => getResolver('communityUsers')({ communityId, userId })),
35813
- ]),
35814
- ] }));
35815
- }
35816
- }
35817
- reactor(action) {
35818
- return (community, communityMembers) => {
35819
- var _a;
35820
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35821
- if (!collection)
35822
- return;
35823
- communityMembers.forEach(communityMember => {
35824
- const communityMemberCacheId = getResolver('communityUsers')({
35825
- communityId: this.query.communityId,
35826
- userId: communityMember.userId,
35827
- });
35828
- if (communityMember.communityMembership === 'none') {
35829
- collection.data = collection.data.filter(m => m !== communityMemberCacheId);
35830
- }
35831
- });
35832
- pushToCache(this.cacheKey, collection);
35833
- this.notifyChange({ origin: "event" /* Amity.LiveDataOrigin.EVENT */, loading: false });
35834
- };
35835
- }
35836
- subscribeRTE(createSubscriber) {
35837
- return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
35838
- }
35839
- }
35840
-
35841
- var EnumCommunityMemberActions;
35842
- (function (EnumCommunityMemberActions) {
35843
- EnumCommunityMemberActions["OnCommunityJoined"] = "onCommunityJoined";
35844
- EnumCommunityMemberActions["OnCommunityLeft"] = "onCommunityLeft";
35845
- EnumCommunityMemberActions["OnCommunityUserBanned"] = "onCommunityUserBanned";
35846
- EnumCommunityMemberActions["OnCommunityUserChanged"] = "onCommunityUserChanged";
35847
- EnumCommunityMemberActions["OnCommunityUserRoleAdded"] = "onCommunityUserRoleAdded";
35848
- EnumCommunityMemberActions["OnCommunityUserRoleRemoved"] = "onCommunityUserRoleRemoved";
35849
- EnumCommunityMemberActions["OnCommunityUserUnbanned"] = "onCommunityUserUnbanned";
35850
- })(EnumCommunityMemberActions || (EnumCommunityMemberActions = {}));
35851
-
35852
- /* eslint-disable no-use-before-define */
35853
- class SearchCommunityMembersLiveCollectionController extends LiveCollectionController {
35854
- constructor(query, callback) {
35855
- const queryStreamId = hash(query);
35856
- const cacheKey = ['communityUsers', 'collection', queryStreamId];
35857
- const paginationController = new SearchCommunityMembersPaginationController(query);
35858
- super(paginationController, queryStreamId, cacheKey, callback);
35859
- this.query = query;
35860
- this.queryStreamController = new SearchCommunityMembersQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this), prepareCommunityPayload);
35861
- this.callback = callback.bind(this);
35862
- this.loadPage({ initial: true });
35863
- }
35864
- setup() {
35586
+ const communityMembers = (_a = data.data
35587
+ .map(id => pullFromCache(['communityUsers', 'get', id]))
35588
+ .filter(Boolean)
35589
+ .map(({ data }) => data)) !== null && _a !== void 0 ? _a : [];
35590
+ callback({
35591
+ onNextPage: onFetch,
35592
+ data: isEventModel ? applyFilter(communityMembers, params) : communityMembers,
35593
+ hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
35594
+ loading: data.loading,
35595
+ error: data.error,
35596
+ });
35597
+ };
35598
+ const realtimeRouter = (_) => (_, communityMembers) => {
35865
35599
  var _a;
35866
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35600
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35867
35601
  if (!collection) {
35868
- pushToCache(this.cacheKey, {
35869
- data: [],
35870
- params: {},
35871
- });
35602
+ return;
35872
35603
  }
35873
- }
35874
- async persistModel(queryPayload) {
35875
- await this.queryStreamController.saveToMainDB(queryPayload);
35876
- }
35877
- persistQueryStream({ response, direction, refresh, }) {
35878
- this.queryStreamController.appendToQueryStream(response, direction, refresh);
35879
- }
35880
- startSubscription() {
35881
- return this.queryStreamController.subscribeRTE([
35882
- { fn: onCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
35883
- { fn: onCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
35884
- { fn: onCommunityUserBanned, action: EnumCommunityMemberActions.OnCommunityUserBanned },
35885
- { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnCommunityUserChanged },
35886
- {
35887
- fn: onCommunityUserRoleRemoved,
35888
- action: EnumCommunityMemberActions.OnCommunityUserRoleRemoved,
35889
- },
35890
- { fn: onCommunityUserUnbanned, action: EnumCommunityMemberActions.OnCommunityUserUnbanned },
35891
- ]);
35892
- }
35893
- notifyChange({ origin, loading, error }) {
35604
+ const communityMemberCacheIds = communityMembers
35605
+ .map(communityMember => {
35606
+ if (params.communityId !== communityMember.communityId) {
35607
+ return;
35608
+ }
35609
+ const communityMemberCacheId = getResolver('communityUsers')({
35610
+ communityId: params.communityId,
35611
+ userId: communityMember.userId,
35612
+ });
35613
+ return communityMemberCacheId;
35614
+ })
35615
+ .filter(isNonNullable);
35616
+ collection.data = [...new Set([...communityMemberCacheIds, ...collection.data])];
35617
+ pushToCache(cacheKey, collection);
35618
+ pushToCache(cacheByCommunityIdKey, collection);
35619
+ responder(collection, true);
35620
+ };
35621
+ const onFetch = (initial = false) => {
35894
35622
  var _a, _b;
35895
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35896
- if (!collection)
35897
- return;
35898
- const data = this.applyFilter((_b = collection.data
35899
- .map(id => pullFromCache(['communityUsers', 'get', id]))
35900
- .filter(isNonNullable)
35901
- .map(({ data }) => data)) !== null && _b !== void 0 ? _b : []);
35902
- if (!this.shouldNotify(data) && origin === 'event')
35623
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
35624
+ const communityMembers = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
35625
+ if (!initial && communityMembers.length > 0 && !(collection === null || collection === void 0 ? void 0 : collection.params.page))
35903
35626
  return;
35904
- this.callback({
35905
- onNextPage: () => this.loadPage({ direction: "next" /* Amity.LiveCollectionPageDirection.NEXT */ }),
35906
- data,
35907
- hasNextPage: !!this.paginationController.getNextToken(),
35908
- loading,
35909
- error,
35910
- });
35911
- }
35912
- applyFilter(data) {
35913
- let communityMembers = filterByPropIntersection(data, 'roles', this.query.roles);
35914
- if (this.query.memberships) {
35915
- communityMembers = communityMembers.filter(({ communityMembership }) => {
35916
- const memberships = this.query.memberships || [];
35917
- return memberships.includes(communityMembership);
35918
- });
35919
- }
35920
- if (this.query.search) {
35921
- communityMembers = filterBySearchTerm(communityMembers, this.query.search);
35922
- }
35923
- return communityMembers;
35924
- }
35925
- }
35926
-
35927
- /* begin_public_function
35928
- id: community.membership.query
35929
- */
35930
- /**
35931
- * ```js
35932
- * import { searchMembers } from '@amityco/ts-sdk-react-native'
35933
- *
35934
- * let communityMembers = []
35935
- * const unsub = searchMembers({
35936
- * communityId: Amity.Community['communityId'],
35937
- * }, response => merge(communityMembers, response.data))
35938
- * ```
35939
- *
35940
- * Observe all mutations on a list of {@link Amity.CommunityUser}s
35941
- *
35942
- * @param params for querying community users
35943
- * @param callback the function to call when new data are available
35944
- * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the community users
35945
- *
35946
- * @category Community Live Collection
35947
- */
35948
- const searchMembers = (params, callback, config) => {
35949
- const { log, cache } = getActiveClient();
35950
- if (!cache) {
35951
- console.log(ENABLE_CACHE_MESSAGE);
35952
- }
35953
- const timestamp = Date.now();
35954
- log(`getMembers(tmpid: ${timestamp}) > listen`);
35955
- const searchCommunityMemberLiveCollection = new SearchCommunityMembersLiveCollectionController(params, resp => {
35956
- callback(resp);
35957
- });
35958
- const disposers = searchCommunityMemberLiveCollection.startSubscription();
35959
- const cacheKey = searchCommunityMemberLiveCollection.getCacheKey();
35960
- disposers.push(() => {
35961
- dropFromCache(cacheKey);
35962
- });
35627
+ const query = createQuery(queryCommunityMembers, Object.assign(Object.assign({}, queryParams), { limit: initial ? limit : undefined, page: !initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined }));
35628
+ runQuery(query, ({ data: result, error, loading, paging }) => {
35629
+ const data = {
35630
+ loading,
35631
+ error,
35632
+ params: { page: paging === null || paging === void 0 ? void 0 : paging.next },
35633
+ data: communityMembers,
35634
+ };
35635
+ if (result) {
35636
+ data.data = [
35637
+ ...new Set([...communityMembers, ...result.map(getResolver('communityUsers'))]),
35638
+ ];
35639
+ }
35640
+ pushToCache(cacheKey, data);
35641
+ pushToCache(cacheByCommunityIdKey, data);
35642
+ responder(data);
35643
+ }, queryOptions(policy));
35644
+ };
35645
+ disposers.push(onCommunityLeft(realtimeRouter()), onCommunityJoined(realtimeRouter()), onCommunityUserBanned(realtimeRouter()), onCommunityUserUnbanned(realtimeRouter()), onCommunityUserChanged(realtimeRouter()), onCommunityUserRoleAdded(realtimeRouter()), onCommunityUserRoleRemoved(realtimeRouter()));
35646
+ onFetch(true);
35963
35647
  return () => {
35964
35648
  log(`getMembers(tmpid: ${timestamp}) > dispose`);
35965
35649
  disposers.forEach(fn => fn());
@@ -35973,7 +35657,6 @@ var index$9 = /*#__PURE__*/Object.freeze({
35973
35657
  removeMembers: removeMembers,
35974
35658
  applyFilter: applyFilter,
35975
35659
  getMembers: getMembers,
35976
- searchMembers: searchMembers,
35977
35660
  onCommunityUserAdded: onCommunityUserAdded,
35978
35661
  onCommunityUserRemoved: onCommunityUserRemoved,
35979
35662
  onCommunityUserBanned: onCommunityUserBanned,
@@ -38131,10 +37814,10 @@ var index$4 = /*#__PURE__*/Object.freeze({
38131
37814
  * const created = await createStream({ title: 'my stream', 'thumbnailFileId': fileId })
38132
37815
  * ```
38133
37816
  *
38134
- * Creates an {@link Amity.InternalStream}
37817
+ * Creates an {@link Amity.Stream}
38135
37818
  *
38136
- * @param bundle The data necessary to create a new {@link Amity.InternalStream}
38137
- * @returns The newly created {@link Amity.InternalStream}
37819
+ * @param bundle The data necessary to create a new {@link Amity.Stream}
37820
+ * @returns The newly created {@link Amity.Stream}
38138
37821
  *
38139
37822
  * @category Stream API
38140
37823
  * @async
@@ -38148,7 +37831,7 @@ const createStream = async (bundle) => {
38148
37831
  ingestInCache(data, { cachedAt });
38149
37832
  const { videoStreamings } = data;
38150
37833
  return {
38151
- data: LinkedObject.stream(videoStreamings[0]),
37834
+ data: videoStreamings[0],
38152
37835
  cachedAt,
38153
37836
  };
38154
37837
  };
@@ -38181,7 +37864,7 @@ const updateStream = async (streamId, patch) => {
38181
37864
  ingestInCache(data, { cachedAt });
38182
37865
  const { videoStreamings } = data;
38183
37866
  return {
38184
- data: LinkedObject.stream(videoStreamings.find(stream => stream.streamId === streamId)),
37867
+ data: videoStreamings.find(stream => stream.streamId === streamId),
38185
37868
  cachedAt,
38186
37869
  };
38187
37870
  };
@@ -38255,10 +37938,10 @@ getStream.locally = (streamId) => {
38255
37938
  * const success = await deleteStream(streamId)
38256
37939
  * ```
38257
37940
  *
38258
- * Deletes a {@link Amity.InternalStream}
37941
+ * Deletes a {@link Amity.Stream}
38259
37942
  *
38260
- * @param streamId The {@link Amity.InternalStream} ID to delete
38261
- * @return A success boolean if the {@link Amity.InternalStream} was deleted
37943
+ * @param streamId The {@link Amity.Stream} ID to delete
37944
+ * @return A success boolean if the {@link Amity.Stream} was deleted
38262
37945
  *
38263
37946
  * @category Stream API
38264
37947
  * @async
@@ -38283,11 +37966,11 @@ const deleteStream = async (streamId) => {
38283
37966
  * const stream = await disposeStream(streamId)
38284
37967
  * ```
38285
37968
  *
38286
- * Dispose a {@link Amity.InternalStream}.
37969
+ * Dispose a {@link Amity.Stream}.
38287
37970
  * Streaming status will be updated to "ended" and streaming url will be invalidated
38288
37971
  *
38289
- * @param streamId The {@link Amity.InternalStream} ID to dispose
38290
- * @returns the associated {@link Amity.InternalStream} object
37972
+ * @param streamId The {@link Amity.Stream} ID to dispose
37973
+ * @returns the associated {@link Amity.Stream} object
38291
37974
  *
38292
37975
  * @category Stream API
38293
37976
  * @async
@@ -38318,15 +38001,15 @@ const disposeStream = async (streamId) => {
38318
38001
  * const streams = await getStreams()
38319
38002
  * ```
38320
38003
  *
38321
- * Queries a paginable list of {@link Amity.InternalStream} objects
38004
+ * Queries a paginable list of {@link Amity.Stream} objects
38322
38005
  *
38323
38006
  * @param query The query parameters
38324
- * @returns A page of {@link Amity.InternalStream} objects
38007
+ * @returns A page of {@link Amity.Stream} objects
38325
38008
  *
38326
38009
  * @category Stream API
38327
38010
  * @async
38328
38011
  */
38329
- const queryStreams = async (query) => {
38012
+ const queryStreams$1 = async (query) => {
38330
38013
  const client = getActiveClient();
38331
38014
  client.log('stream/queryStreams', query);
38332
38015
  const _a = query !== null && query !== void 0 ? query : {}, { page } = _a, params = __rest(_a, ["page"]);
@@ -38356,7 +38039,7 @@ const queryStreams = async (query) => {
38356
38039
  * })
38357
38040
  * ```
38358
38041
  *
38359
- * Fired when a {@link Amity.InternalStream} has started airing
38042
+ * Fired when a {@link Amity.Stream} has started airing
38360
38043
  *
38361
38044
  * @param callback The function to call when the event was fired
38362
38045
  * @returns an {@link Amity.Unsubscriber} function to stop listening
@@ -38380,7 +38063,7 @@ const onStreamStarted = (callback) => {
38380
38063
  * })
38381
38064
  * ```
38382
38065
  *
38383
- * Fired when a {@link Amity.InternalStream} has stopped airing
38066
+ * Fired when a {@link Amity.Stream} has stopped airing
38384
38067
  *
38385
38068
  * @param callback The function to call when the event was fired
38386
38069
  * @returns an {@link Amity.Unsubscriber} function to stop listening
@@ -38404,7 +38087,7 @@ const onStreamStopped = (callback) => {
38404
38087
  * })
38405
38088
  * ```
38406
38089
  *
38407
- * Fired when the recordings of a {@link Amity.InternalStream} are available
38090
+ * Fired when the recordings of a {@link Amity.Stream} are available
38408
38091
  *
38409
38092
  * @param callback The function to call when the event was fired
38410
38093
  * @returns an {@link Amity.Unsubscriber} function to stop listening
@@ -38420,54 +38103,6 @@ const onStreamRecorded = (callback) => {
38420
38103
  return createEventSubscriber(client, 'stream/onStreamRecorded', 'v3.video-streaming.didRecord', filter);
38421
38104
  };
38422
38105
 
38423
- /**
38424
- * ```js
38425
- * import { onStreamFlagged } from '@amityco/ts-sdk-react-native'
38426
- * const dispose = onStreamFlagged(stream => {
38427
- * // ...
38428
- * })
38429
- * ```
38430
- *
38431
- * Fired when a {@link Amity.InternalStream} has started airing
38432
- *
38433
- * @param callback The function to call when the event was fired
38434
- * @returns an {@link Amity.Unsubscriber} function to stop listening
38435
- *
38436
- * @category Stream Events
38437
- */
38438
- const onStreamFlagged = (callback) => {
38439
- const client = getActiveClient();
38440
- const filter = (payload) => {
38441
- ingestInCache(payload);
38442
- callback(payload.videoStreamings[0]);
38443
- };
38444
- return createEventSubscriber(client, 'stream/onStreamFlagged', 'v3.video-streaming.didFlag', filter);
38445
- };
38446
-
38447
- /**
38448
- * ```js
38449
- * import { onStreamTerminated } from '@amityco/ts-sdk-react-native'
38450
- * const dispose = onStreamTerminated(stream => {
38451
- * // ...
38452
- * })
38453
- * ```
38454
- *
38455
- * Fired when a {@link Amity.InternalStream} has started airing
38456
- *
38457
- * @param callback The function to call when the event was fired
38458
- * @returns an {@link Amity.Unsubscriber} function to stop listening
38459
- *
38460
- * @category Stream Events
38461
- */
38462
- const onStreamTerminated = (callback) => {
38463
- const client = getActiveClient();
38464
- const filter = (payload) => {
38465
- ingestInCache(payload);
38466
- callback(payload.videoStreamings[0]);
38467
- };
38468
- return createEventSubscriber(client, 'stream/onStreamTerminated', 'v3.video-streaming.didTerminate', filter);
38469
- };
38470
-
38471
38106
  /* eslint-disable no-use-before-define */
38472
38107
  /* begin_public_function
38473
38108
  id: stream.get
@@ -38482,22 +38117,15 @@ const onStreamTerminated = (callback) => {
38482
38117
  * Fetches a {@link Amity.Stream} object
38483
38118
  *
38484
38119
  * @param streamId the ID of the {@link Amity.Stream} to get
38485
- * @param callback
38486
38120
  * @returns the associated {@link Amity.Stream} object
38487
38121
  *
38488
38122
  * @category Stream Live Object
38489
38123
  */
38490
38124
  const getStreamById = (streamId, callback) => {
38491
- const reactor = (snapshot) => {
38492
- const { data } = snapshot;
38493
- callback(Object.assign(Object.assign({}, snapshot), { data: data ? LinkedObject.stream(snapshot.data) : data }));
38494
- };
38495
- return liveObject(streamId, reactor, 'streamId', getStream, [
38125
+ return liveObject(streamId, callback, 'streamId', getStream, [
38496
38126
  onStreamRecorded,
38497
38127
  onStreamStarted,
38498
38128
  onStreamStopped,
38499
- onStreamFlagged,
38500
- onStreamTerminated,
38501
38129
  ]);
38502
38130
  };
38503
38131
  /* end_public_function */
@@ -38523,150 +38151,145 @@ getStreamById.locally = (streamId) => {
38523
38151
  if (!cached)
38524
38152
  return;
38525
38153
  return {
38526
- data: LinkedObject.stream(cached.data),
38154
+ data: cached.data,
38527
38155
  cachedAt: cached.cachedAt,
38528
38156
  };
38529
38157
  };
38530
38158
 
38531
- class GetStreamsPageController extends PaginationController {
38532
- async getRequest(queryParams, token) {
38533
- const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT } = queryParams, params = __rest(queryParams, ["limit"]);
38534
- const options = token ? { token } : { limit };
38535
- const { data: queryResponse } = await this.http.get(`/api/v3/video-streaming`, {
38536
- params: Object.assign(Object.assign({}, params), { options }),
38537
- });
38538
- return Object.assign(Object.assign({}, queryResponse.results), { paging: queryResponse.paging });
38539
- }
38540
- }
38541
-
38542
- class GetStreamsQueryStreamController extends QueryStreamController {
38543
- constructor(query, cacheKey, notifyChange, paginationController) {
38544
- super(query, cacheKey);
38545
- this.notifyChange = notifyChange;
38546
- this.paginationController = paginationController;
38547
- }
38548
- // eslint-disable-next-line class-methods-use-this
38549
- saveToMainDB(response) {
38550
- const client = getActiveClient();
38551
- const cachedAt = client.cache && Date.now();
38552
- if (client.cache) {
38553
- ingestInCache(response, { cachedAt });
38554
- }
38555
- }
38556
- appendToQueryStream(response, direction, refresh = false) {
38557
- var _a, _b;
38558
- if (refresh) {
38559
- pushToCache(this.cacheKey, {
38560
- data: response.videoStreamings.map(getResolver('stream')),
38561
- query: this.query,
38562
- });
38563
- }
38564
- else {
38565
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
38566
- const messages = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
38567
- pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: direction === 'next'
38568
- ? [...new Set([...messages, ...response.videoStreamings.map(getResolver('stream'))])]
38569
- : [...new Set([...response.videoStreamings.map(getResolver('stream')), ...messages])] }));
38570
- }
38571
- }
38572
- reactor(action) {
38573
- return (payload) => {
38574
- var _a;
38575
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
38576
- if (!collection)
38577
- return;
38578
- collection.data = [...new Set([payload.streamId, ...collection.data])];
38579
- pushToCache(this.cacheKey, collection);
38580
- this.notifyChange({ origin: "event" /* Amity.LiveDataOrigin.EVENT */, loading: false });
38581
- };
38582
- }
38583
- subscribeRTE(createSubscriber) {
38584
- return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
38585
- }
38586
- }
38587
-
38588
- class GetStreamsLiveCollectionController extends LiveCollectionController {
38589
- constructor(query, callback) {
38590
- const queryStreamId = hash(query);
38591
- const cacheKey = ['streams', 'collection', queryStreamId];
38592
- const paginationController = new GetStreamsPageController(query);
38593
- super(paginationController, queryStreamId, cacheKey, callback);
38594
- this.applyFilter = (data) => {
38595
- let streams = filterByPropEquality(data, 'isDeleted', this.query.isDeleted);
38596
- streams = streams.sort(this.query.sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
38597
- return streams;
38598
- };
38599
- this.query = query;
38600
- this.queryStreamController = new GetStreamsQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this), paginationController);
38601
- this.paginationController = paginationController;
38602
- this.callback = callback.bind(this);
38603
- this.loadPage({ initial: true });
38604
- }
38605
- notifyChange({ origin, loading, error }) {
38606
- var _a;
38607
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
38608
- if (!collection)
38609
- return;
38610
- let data = collection.data
38611
- .map(streamId => pullFromCache(['stream', 'get', streamId]))
38612
- .filter(Boolean)
38613
- .map(stream => LinkedObject.stream(stream.data));
38614
- if (!this.shouldNotify(data) && origin === 'event')
38615
- return;
38616
- data = this.applyFilter(data);
38617
- this.callback({
38618
- onNextPage: () => this.loadPage({ initial: false, direction: "next" /* Amity.LiveCollectionPageDirection.NEXT */ }),
38619
- data,
38620
- hasNextPage: !!this.paginationController.getNextToken(),
38621
- loading,
38622
- error,
38623
- });
38624
- }
38625
- startSubscription() {
38626
- return this.queryStreamController.subscribeRTE([
38627
- { fn: onStreamRecorded, action: 'onStreamRecorded' },
38628
- { fn: onStreamStarted, action: 'onStreamStarted' },
38629
- { fn: onStreamStopped, action: 'onStreamStopped' },
38630
- { fn: onStreamFlagged, action: 'onStreamFlagged' },
38631
- { fn: onStreamTerminated, action: 'onStreamTerminated' },
38632
- ]);
38633
- }
38634
- setup() {
38635
- var _a;
38636
- const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
38637
- if (!collection) {
38638
- pushToCache(this.cacheKey, {
38639
- data: [],
38640
- params: {},
38641
- });
38642
- }
38643
- }
38644
- persistModel(response) {
38645
- this.queryStreamController.saveToMainDB(response);
38646
- }
38647
- persistQueryStream({ response, direction, refresh, }) {
38648
- this.queryStreamController.appendToQueryStream(response, direction, refresh);
38649
- }
38650
- }
38159
+ /* begin_public_function
38160
+ id: stream.query
38161
+ */
38162
+ /**
38163
+ * ```js
38164
+ * import { getStreams } from '@amityco/ts-sdk-react-native'
38165
+ * const streams = await getStreams()
38166
+ * ```
38167
+ *
38168
+ * Queries a paginable list of {@link Amity.Stream} objects
38169
+ *
38170
+ * @param query The query parameters
38171
+ * @returns A page of {@link Amity.Stream} objects
38172
+ *
38173
+ * @category Stream API
38174
+ * @async
38175
+ */
38176
+ const queryStreams = async (query) => {
38177
+ const client = getActiveClient();
38178
+ client.log('stream/queryStreams', query);
38179
+ const _a = query !== null && query !== void 0 ? query : {}, { page, limit } = _a, params = __rest(_a, ["page", "limit"]);
38180
+ const options = (() => {
38181
+ if (page)
38182
+ return { token: page };
38183
+ if (limit)
38184
+ return { limit };
38185
+ return undefined;
38186
+ })();
38187
+ const { data } = await client.http.get(`/api/v3/video-streaming`, {
38188
+ params: Object.assign(Object.assign({}, params), { options }),
38189
+ });
38190
+ // API-FIX: backend to response Amity.Response: const { paging, videoStreamings } = unwrapPayload(data)
38191
+ // API-FIX: seems returned data has a results identifier on top of data, like no other apis, and this is beautiful
38192
+ const { paging, results: payload } = data;
38193
+ const { videoStreamings } = payload;
38194
+ const cachedAt = client.cache && Date.now();
38195
+ if (client.cache)
38196
+ ingestInCache(payload, { cachedAt });
38197
+ return { data: videoStreamings, cachedAt, paging };
38198
+ };
38199
+ /* end_public_function */
38651
38200
 
38201
+ /* begin_public_function
38202
+ id: stream.query
38203
+ */
38204
+ /**
38205
+ * ```js
38206
+ * import { StreamRepository } from '@amityco/ts-sdk-react-native'
38207
+ * const streams = await StreamRepository.getStreams()
38208
+ * ```
38209
+ *
38210
+ * Observe all mutations on a list of {@link Amity.Stream}s
38211
+ *
38212
+ * @param params for querying streams
38213
+ * @param callback the function to call when new data are available
38214
+ * @param config
38215
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the streams
38216
+ *
38217
+ * @category Stream Live Collection
38218
+ */
38652
38219
  const getStreams = (params, callback, config) => {
38653
- const { log, cache, userId } = getActiveClient();
38220
+ const { log, cache } = getActiveClient();
38654
38221
  if (!cache) {
38655
38222
  console.log(ENABLE_CACHE_MESSAGE);
38656
38223
  }
38657
38224
  const timestamp = Date.now();
38658
38225
  log(`getStreams(tmpid: ${timestamp}) > listen`);
38659
- const liveCollection = new GetStreamsLiveCollectionController(params, callback);
38660
- const disposers = liveCollection.startSubscription();
38661
- const cacheKey = liveCollection.getCacheKey();
38662
- disposers.push(() => {
38663
- dropFromCache(cacheKey);
38664
- });
38226
+ const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
38227
+ const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
38228
+ const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
38229
+ const disposers = [];
38230
+ const cacheKey = ['streams', 'collection', params];
38231
+ const applyFilter = (data) => {
38232
+ let streams = filterByPropEquality(data, 'isDeleted', params.isDeleted);
38233
+ streams = streams.sort(params.sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
38234
+ return streams;
38235
+ };
38236
+ const responder = (data, isEventModel = false) => {
38237
+ var _a, _b;
38238
+ const streams = (_a = data.data
38239
+ .map(streamId => {
38240
+ return pullFromCache(['stream', 'get', streamId]);
38241
+ })
38242
+ .filter(Boolean)
38243
+ .map(({ data }) => data)) !== null && _a !== void 0 ? _a : [];
38244
+ callback({
38245
+ onNextPage: onFetch,
38246
+ /*
38247
+ * Only apply filter to RTE Model
38248
+ */
38249
+ data: isEventModel ? applyFilter(streams) : streams,
38250
+ hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
38251
+ loading: data.loading,
38252
+ error: data.error,
38253
+ });
38254
+ };
38255
+ const realtimeRouter = (_) => (stream) => {
38256
+ var _a;
38257
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
38258
+ if (!collection)
38259
+ return;
38260
+ collection.data = [...new Set([stream.streamId, ...collection.data])];
38261
+ pushToCache(cacheKey, collection);
38262
+ responder(collection, true);
38263
+ };
38264
+ const onFetch = (initial = false) => {
38265
+ var _a, _b;
38266
+ const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
38267
+ const streams = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
38268
+ if (!initial && streams.length > 0 && !(collection === null || collection === void 0 ? void 0 : collection.params.page))
38269
+ return;
38270
+ const query = createQuery(queryStreams, Object.assign(Object.assign({}, queryParams), { limit: initial ? limit : undefined, page: !initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined }));
38271
+ runQuery(query, ({ data: result, error, loading, paging }) => {
38272
+ const data = {
38273
+ loading,
38274
+ error,
38275
+ params: { page: paging === null || paging === void 0 ? void 0 : paging.next },
38276
+ data: streams,
38277
+ };
38278
+ if (result) {
38279
+ data.data = [...new Set([...streams, ...result.map(getResolver('stream'))])];
38280
+ }
38281
+ pushToCache(cacheKey, data);
38282
+ responder(data);
38283
+ }, queryOptions(policy));
38284
+ };
38285
+ disposers.push(onStreamRecorded(realtimeRouter()), onStreamStarted(realtimeRouter()), onStreamStopped(realtimeRouter()));
38286
+ onFetch(true);
38665
38287
  return () => {
38666
38288
  log(`getStreams(tmpid: ${timestamp}) > dispose`);
38667
38289
  disposers.forEach(fn => fn());
38668
38290
  };
38669
- };
38291
+ };
38292
+ /* end_public_function */
38670
38293
 
38671
38294
  var index$3 = /*#__PURE__*/Object.freeze({
38672
38295
  __proto__: null,
@@ -38674,13 +38297,11 @@ var index$3 = /*#__PURE__*/Object.freeze({
38674
38297
  updateStream: updateStream,
38675
38298
  deleteStream: deleteStream,
38676
38299
  disposeStream: disposeStream,
38677
- queryStreams: queryStreams,
38300
+ queryStreams: queryStreams$1,
38678
38301
  getStream: getStream,
38679
38302
  onStreamStarted: onStreamStarted,
38680
38303
  onStreamStopped: onStreamStopped,
38681
38304
  onStreamRecorded: onStreamRecorded,
38682
- onStreamFlagged: onStreamFlagged,
38683
- onStreamTerminated: onStreamTerminated,
38684
38305
  getStreamById: getStreamById,
38685
38306
  getStreams: getStreams
38686
38307
  });