@amityco/ts-sdk-react-native 6.23.0 → 6.23.1-d8b4c4c.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 (214) hide show
  1. package/.env +26 -26
  2. package/dist/@types/core/events.d.ts +7 -0
  3. package/dist/@types/core/events.d.ts.map +1 -1
  4. package/dist/@types/core/model.d.ts +4 -2
  5. package/dist/@types/core/model.d.ts.map +1 -1
  6. package/dist/@types/core/payload.d.ts +2 -1
  7. package/dist/@types/core/payload.d.ts.map +1 -1
  8. package/dist/@types/domains/community.d.ts +30 -1
  9. package/dist/@types/domains/community.d.ts.map +1 -1
  10. package/dist/@types/domains/stream.d.ts +19 -3
  11. package/dist/@types/domains/stream.d.ts.map +1 -1
  12. package/dist/client/api/secureLogout.d.ts +1 -1
  13. package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts.map +1 -1
  14. package/dist/commentRepository/api/deleteComment.d.ts.map +1 -1
  15. package/dist/commentRepository/internalApi/createComment.d.ts.map +1 -1
  16. package/dist/communityRepository/api/createCommunity.d.ts.map +1 -1
  17. package/dist/communityRepository/api/getCommunities.d.ts.map +1 -1
  18. package/dist/communityRepository/api/getCommunity.d.ts.map +1 -1
  19. package/dist/communityRepository/api/queryCommunities.d.ts.map +1 -1
  20. package/dist/communityRepository/api/updateCommunity.d.ts.map +1 -1
  21. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.d.ts +14 -0
  22. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.d.ts.map +1 -0
  23. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.d.ts +9 -0
  24. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.d.ts.map +1 -0
  25. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.d.ts +15 -0
  26. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.d.ts.map +1 -0
  27. package/dist/communityRepository/communityMembership/observers/getMembers/enums.d.ts +10 -0
  28. package/dist/communityRepository/communityMembership/observers/getMembers/enums.d.ts.map +1 -0
  29. package/dist/communityRepository/communityMembership/observers/getMembers.d.ts.map +1 -1
  30. package/dist/communityRepository/communityMembership/observers/index.d.ts +1 -0
  31. package/dist/communityRepository/communityMembership/observers/index.d.ts.map +1 -1
  32. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.d.ts +14 -0
  33. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.d.ts.map +1 -0
  34. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.d.ts +9 -0
  35. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.d.ts.map +1 -0
  36. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.d.ts +15 -0
  37. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.d.ts.map +1 -0
  38. package/dist/communityRepository/communityMembership/observers/searchMembers/enums.d.ts +10 -0
  39. package/dist/communityRepository/communityMembership/observers/searchMembers/enums.d.ts.map +1 -0
  40. package/dist/communityRepository/communityMembership/observers/searchMembers.d.ts +20 -0
  41. package/dist/communityRepository/communityMembership/observers/searchMembers.d.ts.map +1 -0
  42. package/dist/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.d.ts +14 -0
  43. package/dist/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.d.ts.map +1 -0
  44. package/dist/communityRepository/observers/getCommunities/CommunitiesPaginationController.d.ts +9 -0
  45. package/dist/communityRepository/observers/getCommunities/CommunitiesPaginationController.d.ts.map +1 -0
  46. package/dist/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.d.ts +15 -0
  47. package/dist/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.d.ts.map +1 -0
  48. package/dist/communityRepository/observers/getCommunities/enums.d.ts +6 -0
  49. package/dist/communityRepository/observers/getCommunities/enums.d.ts.map +1 -0
  50. package/dist/communityRepository/observers/getCommunities.d.ts.map +1 -1
  51. package/dist/communityRepository/observers/index.d.ts +1 -0
  52. package/dist/communityRepository/observers/index.d.ts.map +1 -1
  53. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.d.ts +14 -0
  54. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.d.ts.map +1 -0
  55. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesPaginationController.d.ts +9 -0
  56. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesPaginationController.d.ts.map +1 -0
  57. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.d.ts +15 -0
  58. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.d.ts.map +1 -0
  59. package/dist/communityRepository/observers/searchCommunities/enums.d.ts +6 -0
  60. package/dist/communityRepository/observers/searchCommunities/enums.d.ts.map +1 -0
  61. package/dist/communityRepository/observers/searchCommunities.d.ts +20 -0
  62. package/dist/communityRepository/observers/searchCommunities.d.ts.map +1 -0
  63. package/dist/communityRepository/utils/payload.d.ts.map +1 -1
  64. package/dist/communityRepository/utils/saveCommunityUsers.d.ts +2 -0
  65. package/dist/communityRepository/utils/saveCommunityUsers.d.ts.map +1 -0
  66. package/dist/core/events.d.ts +3 -3
  67. package/dist/core/events.d.ts.map +1 -1
  68. package/dist/core/liveCollection/LiveCollectionController.d.ts +4 -1
  69. package/dist/core/liveCollection/LiveCollectionController.d.ts.map +1 -1
  70. package/dist/core/model/idResolvers.d.ts.map +1 -1
  71. package/dist/core/model/index.d.ts.map +1 -1
  72. package/dist/index.cjs.js +1604 -977
  73. package/dist/index.esm.js +1372 -745
  74. package/dist/index.umd.js +4 -4
  75. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts +12 -0
  76. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts.map +1 -0
  77. package/dist/marker/events/onSubChannelUnreadUpdatedLocal.d.ts +12 -0
  78. package/dist/marker/events/onSubChannelUnreadUpdatedLocal.d.ts.map +1 -0
  79. package/dist/marker/events/onUserFeedMarkerUpdated.d.ts.map +1 -1
  80. package/dist/marker/utils/reCalculateChannelUnreadInfo.d.ts +1 -1
  81. package/dist/marker/utils/reCalculateChannelUnreadInfo.d.ts.map +1 -1
  82. package/dist/postRepository/observers/getPost.d.ts.map +1 -1
  83. package/dist/postRepository/observers/getPosts.d.ts.map +1 -1
  84. package/dist/streamRepository/api/createStream.d.ts +4 -4
  85. package/dist/streamRepository/api/createStream.d.ts.map +1 -1
  86. package/dist/streamRepository/api/deleteStream.d.ts +4 -4
  87. package/dist/streamRepository/api/deleteStream.d.ts.map +1 -1
  88. package/dist/streamRepository/api/disposeStream.d.ts +4 -4
  89. package/dist/streamRepository/api/disposeStream.d.ts.map +1 -1
  90. package/dist/streamRepository/api/queryStreams.d.ts +4 -4
  91. package/dist/streamRepository/api/queryStreams.d.ts.map +1 -1
  92. package/dist/streamRepository/api/updateStream.d.ts.map +1 -1
  93. package/dist/streamRepository/events/index.d.ts +2 -0
  94. package/dist/streamRepository/events/index.d.ts.map +1 -1
  95. package/dist/streamRepository/events/onStreamFlagged.d.ts +17 -0
  96. package/dist/streamRepository/events/onStreamFlagged.d.ts.map +1 -0
  97. package/dist/streamRepository/events/onStreamRecorded.d.ts +2 -2
  98. package/dist/streamRepository/events/onStreamRecorded.d.ts.map +1 -1
  99. package/dist/streamRepository/events/onStreamStarted.d.ts +2 -2
  100. package/dist/streamRepository/events/onStreamStarted.d.ts.map +1 -1
  101. package/dist/streamRepository/events/onStreamStopped.d.ts +2 -2
  102. package/dist/streamRepository/events/onStreamStopped.d.ts.map +1 -1
  103. package/dist/streamRepository/events/onStreamTerminated.d.ts +17 -0
  104. package/dist/streamRepository/events/onStreamTerminated.d.ts.map +1 -0
  105. package/dist/streamRepository/internalApi/queryStreams.d.ts +3 -3
  106. package/dist/streamRepository/internalApi/queryStreams.d.ts.map +1 -1
  107. package/dist/streamRepository/observers/getStreamById.d.ts +1 -0
  108. package/dist/streamRepository/observers/getStreamById.d.ts.map +1 -1
  109. package/dist/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.d.ts +14 -0
  110. package/dist/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.d.ts.map +1 -0
  111. package/dist/streamRepository/observers/getStreams/GetStreamsPageController.d.ts +14 -0
  112. package/dist/streamRepository/observers/getStreams/GetStreamsPageController.d.ts.map +1 -0
  113. package/dist/streamRepository/observers/getStreams/GetStreamsQueryStreamController.d.ts +15 -0
  114. package/dist/streamRepository/observers/getStreams/GetStreamsQueryStreamController.d.ts.map +1 -0
  115. package/dist/streamRepository/observers/getStreams/getStreams.d.ts +2 -0
  116. package/dist/streamRepository/observers/getStreams/getStreams.d.ts.map +1 -0
  117. package/dist/streamRepository/observers/getStreams/index.d.ts +2 -0
  118. package/dist/streamRepository/observers/getStreams/index.d.ts.map +1 -0
  119. package/dist/utils/linkedObject/index.d.ts +1 -0
  120. package/dist/utils/linkedObject/index.d.ts.map +1 -1
  121. package/dist/utils/linkedObject/streamLinkedObject.d.ts +2 -0
  122. package/dist/utils/linkedObject/streamLinkedObject.d.ts.map +1 -0
  123. package/dist/utils/liveObject.d.ts +3 -0
  124. package/dist/utils/liveObject.d.ts.map +1 -1
  125. package/package.json +1 -1
  126. package/src/@types/core/events.ts +12 -0
  127. package/src/@types/core/model.ts +4 -2
  128. package/src/@types/core/payload.ts +2 -1
  129. package/src/@types/domains/community.ts +50 -1
  130. package/src/@types/domains/stream.ts +23 -3
  131. package/src/channelRepository/channelMembership/observers/getMembers/ChannelMemberLiveCollectionController.ts +2 -2
  132. package/src/channelRepository/observers/getChannel.ts +2 -2
  133. package/src/channelRepository/observers/getChannels/ChannelLiveCollectionController.ts +7 -7
  134. package/src/client/api/secureLogout.ts +1 -1
  135. package/src/client/utils/ReadReceiptSync/readReceiptSyncEngine.ts +6 -3
  136. package/src/commentRepository/api/createComment.ts +2 -2
  137. package/src/commentRepository/api/deleteComment.ts +2 -4
  138. package/src/commentRepository/events/utils.ts +1 -1
  139. package/src/commentRepository/internalApi/createComment.ts +3 -2
  140. package/src/commentRepository/internalApi/deleteComment.ts +2 -2
  141. package/src/communityRepository/api/createCommunity.ts +5 -2
  142. package/src/communityRepository/api/getCommunities.ts +5 -1
  143. package/src/communityRepository/api/getCommunity.ts +5 -1
  144. package/src/communityRepository/api/queryCommunities.ts +2 -2
  145. package/src/communityRepository/api/updateCommunity.ts +5 -1
  146. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.ts +152 -0
  147. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.ts +26 -0
  148. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.ts +114 -0
  149. package/src/communityRepository/communityMembership/observers/getMembers/enums.ts +9 -0
  150. package/src/communityRepository/communityMembership/observers/getMembers.ts +15 -128
  151. package/src/communityRepository/communityMembership/observers/index.ts +1 -0
  152. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.ts +130 -0
  153. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.ts +29 -0
  154. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.ts +105 -0
  155. package/src/communityRepository/communityMembership/observers/searchMembers/enums.ts +9 -0
  156. package/src/communityRepository/communityMembership/observers/searchMembers.ts +60 -0
  157. package/src/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.ts +147 -0
  158. package/src/communityRepository/observers/getCommunities/CommunitiesPaginationController.ts +31 -0
  159. package/src/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.ts +86 -0
  160. package/src/communityRepository/observers/getCommunities/enums.ts +5 -0
  161. package/src/communityRepository/observers/getCommunities.ts +7 -150
  162. package/src/communityRepository/observers/index.ts +1 -0
  163. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.ts +129 -0
  164. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesPaginationController.ts +31 -0
  165. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.ts +81 -0
  166. package/src/communityRepository/observers/searchCommunities/enums.ts +5 -0
  167. package/src/communityRepository/observers/searchCommunities.ts +56 -0
  168. package/src/communityRepository/utils/payload.ts +35 -1
  169. package/src/communityRepository/utils/saveCommunityUsers.ts +16 -0
  170. package/src/core/liveCollection/LiveCollectionController.ts +6 -3
  171. package/src/core/model/idResolvers.ts +1 -0
  172. package/src/core/model/index.ts +1 -0
  173. package/src/marker/events/onChannelUnreadUpdatedLocal.ts +29 -0
  174. package/src/marker/events/onSubChannelMarkerFetched.ts +1 -1
  175. package/src/marker/events/onSubChannelUnreadUpdatedLocal.ts +29 -0
  176. package/src/marker/events/onUserFeedMarkerUpdated.ts +3 -4
  177. package/src/marker/utils/reCalculateChannelUnreadInfo.ts +7 -3
  178. package/src/messageRepository/observers/getMessages/MessageLiveCollectionController.ts +3 -3
  179. package/src/postRepository/api/editPost.ts +1 -1
  180. package/src/postRepository/api/updatePost.ts +1 -1
  181. package/src/postRepository/observers/getPost.ts +26 -0
  182. package/src/postRepository/observers/getPosts.ts +31 -0
  183. package/src/storyRepository/observers/getGlobalStoryTargets/GlobalStoryLiveCollectionController.ts +2 -2
  184. package/src/storyRepository/observers/getStoriesByTargetIds/StoryLiveCollectionController.ts +1 -1
  185. package/src/streamRepository/api/createStream.ts +8 -5
  186. package/src/streamRepository/api/deleteStream.ts +6 -4
  187. package/src/streamRepository/api/disposeStream.ts +5 -5
  188. package/src/streamRepository/api/queryStreams.ts +4 -4
  189. package/src/streamRepository/api/updateStream.ts +2 -1
  190. package/src/streamRepository/events/index.ts +2 -0
  191. package/src/streamRepository/events/onStreamFlagged.ts +37 -0
  192. package/src/streamRepository/events/onStreamRecorded.ts +4 -2
  193. package/src/streamRepository/events/onStreamStarted.ts +4 -2
  194. package/src/streamRepository/events/onStreamStopped.ts +4 -2
  195. package/src/streamRepository/events/onStreamTerminated.ts +37 -0
  196. package/src/streamRepository/internalApi/queryStreams.ts +3 -3
  197. package/src/streamRepository/observers/getStreamById.ts +18 -5
  198. package/src/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.ts +114 -0
  199. package/src/streamRepository/observers/getStreams/GetStreamsPageController.ts +23 -0
  200. package/src/streamRepository/observers/getStreams/GetStreamsQueryStreamController.ts +83 -0
  201. package/src/streamRepository/observers/getStreams/getStreams.ts +32 -0
  202. package/src/streamRepository/observers/getStreams/index.ts +1 -0
  203. package/src/streamRepository/observers/tests/getStreamById.test.ts +1 -1
  204. package/src/subChannelRepository/observers/getSubChannel.ts +2 -2
  205. package/src/subChannelRepository/observers/getSubChannels/SubChannelLiveCollectionController.ts +4 -4
  206. package/src/utils/linkedObject/index.ts +2 -0
  207. package/src/utils/linkedObject/streamLinkedObject.ts +11 -0
  208. package/src/utils/liveObject.ts +3 -0
  209. package/dist/streamRepository/api/getStreams.d.ts +0 -39
  210. package/dist/streamRepository/api/getStreams.d.ts.map +0 -1
  211. package/dist/streamRepository/observers/getStreams.d.ts +0 -17
  212. package/dist/streamRepository/observers/getStreams.d.ts.map +0 -1
  213. package/src/streamRepository/api/getStreams.ts +0 -81
  214. package/src/streamRepository/observers/getStreams.ts +0 -152
@@ -0,0 +1,86 @@
1
+ import { QueryStreamController } from '~/core/liveCollection/QueryStreamController';
2
+ import { pullFromCache, pushToCache } from '~/cache/api';
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+ import { getResolver } from '~/core/model';
5
+ import { getActiveClient } from '~/client';
6
+ import { EnumCommunityActions } from './enums';
7
+
8
+ export class CommunitiesQueryStreamController extends QueryStreamController<
9
+ Amity.CommunityPayload,
10
+ Amity.CommunityLiveCollection
11
+ > {
12
+ private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
13
+
14
+ private preparePayload: (response: Amity.CommunityPayload) => Amity.ProcessedCommunityPayload;
15
+
16
+ constructor(
17
+ query: Amity.CommunityLiveCollection,
18
+ cacheKey: string[],
19
+ notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
20
+ preparePayload: (response: Amity.CommunityPayload) => Amity.ProcessedCommunityPayload,
21
+ ) {
22
+ super(query, cacheKey);
23
+ this.notifyChange = notifyChange;
24
+ this.preparePayload = preparePayload;
25
+ }
26
+
27
+ async saveToMainDB(response: Amity.CommunityPayload) {
28
+ const processedPayload = await this.preparePayload(response);
29
+
30
+ const client = getActiveClient();
31
+ const cachedAt = client.cache && Date.now();
32
+
33
+ if (client.cache) {
34
+ ingestInCache(processedPayload, { cachedAt });
35
+ }
36
+ }
37
+
38
+ appendToQueryStream(
39
+ response: Amity.CommunityPayload & Partial<Amity.Pagination>,
40
+ direction: Amity.LiveCollectionPageDirection,
41
+ refresh = false,
42
+ ) {
43
+ if (refresh) {
44
+ pushToCache(this.cacheKey, {
45
+ data: response.communities.map(getResolver('community')),
46
+ });
47
+ } else {
48
+ const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(this.cacheKey)?.data;
49
+
50
+ const communities = collection?.data ?? [];
51
+
52
+ pushToCache(this.cacheKey, {
53
+ ...collection,
54
+ data: [...new Set([...communities, ...response.communities.map(getResolver('community'))])],
55
+ });
56
+ }
57
+ }
58
+
59
+ reactor(action: EnumCommunityActions) {
60
+ return (community: Amity.Community) => {
61
+ const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(this.cacheKey)?.data;
62
+ if (!collection) return;
63
+
64
+ if (this.query.displayName && action === EnumCommunityActions.OnCommunityCreated) {
65
+ return;
66
+ }
67
+
68
+ /*
69
+ * Simply update collection and let responder decide what to do with data
70
+ */
71
+ collection.data = [...new Set([community.communityId, ...collection.data])];
72
+
73
+ pushToCache(this.cacheKey, collection);
74
+ this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
75
+ };
76
+ }
77
+
78
+ subscribeRTE(
79
+ createSubscriber: {
80
+ fn: (reactor: (channel: Amity.Community) => void) => Amity.Unsubscriber;
81
+ action: EnumCommunityActions;
82
+ }[],
83
+ ) {
84
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
85
+ }
86
+ }
@@ -0,0 +1,5 @@
1
+ export enum EnumCommunityActions {
2
+ OnCommunityCreated = 'onCommunityCreated',
3
+ OnCommunityDeleted = 'onCommunityDeleted',
4
+ OnCommunityUpdated = 'onCommunityUpdated',
5
+ }
@@ -1,31 +1,7 @@
1
- /* eslint-disable no-use-before-define */
2
- import { getResolver } from '~/core/model';
3
- import { dropFromCache, pullFromCache, pushToCache } from '~/cache/api';
1
+ import { dropFromCache } from '~/cache/api';
4
2
  import { getActiveClient } from '~/client/api';
5
- import {
6
- createQuery,
7
- filterByCommunityMembership,
8
- filterByPropEquality,
9
- filterByStringComparePartially,
10
- queryOptions,
11
- runQuery,
12
- sortByDisplayName,
13
- sortByFirstCreated,
14
- sortByLastCreated,
15
- } from '~/core/query';
16
- import {
17
- COLLECTION_DEFAULT_CACHING_POLICY,
18
- COLLECTION_DEFAULT_PAGINATION_LIMIT,
19
- ENABLE_CACHE_MESSAGE,
20
- } from '~/utils/constants';
21
- import { CACHE_SHORTEN_LIFESPAN } from '~/cache/utils';
22
- import { onCommunityCreated, onCommunityDeleted, onCommunityUpdated } from '../events';
23
- import {
24
- onCommunityJoined,
25
- onCommunityLeft,
26
- onCommunityUserChanged,
27
- } from '../communityMembership/events';
28
- import { queryCommunities } from '../api/queryCommunities';
3
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
4
+ import { CommunityLiveCollectionController } from './getCommunities/CommunitiesLiveCollectionController';
29
5
 
30
6
  /* begin_public_function
31
7
  id: community.query
@@ -53,7 +29,7 @@ export const getCommunities = (
53
29
  callback: Amity.LiveCollectionCallback<Amity.Community>,
54
30
  config?: Amity.LiveCollectionConfig,
55
31
  ) => {
56
- const { log, cache, userId } = getActiveClient();
32
+ const { log, cache } = getActiveClient();
57
33
 
58
34
  if (!cache) {
59
35
  console.log(ENABLE_CACHE_MESSAGE);
@@ -62,130 +38,11 @@ export const getCommunities = (
62
38
  const timestamp = Date.now();
63
39
  log(`getCommunities(tmpid: ${timestamp}) > listen`);
64
40
 
65
- const { limit: queryLimit, ...queryParams } = params;
41
+ const communitiesLiveCollection = new CommunityLiveCollectionController(params, callback);
42
+ const disposers = communitiesLiveCollection.startSubscription();
66
43
 
67
- const limit = queryLimit ?? COLLECTION_DEFAULT_PAGINATION_LIMIT;
68
- const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config ?? {};
44
+ const cacheKey = communitiesLiveCollection.getCacheKey();
69
45
 
70
- const disposers: Amity.Unsubscriber[] = [];
71
- const cacheKey = ['community', 'collection', queryParams as Amity.Serializable];
72
-
73
- const responder = (data: Amity.CommunityLiveCollectionCache) => {
74
- let communities: Amity.Community[] =
75
- data.data
76
- .map(communityId => pullFromCache<Amity.Community>(['community', 'get', communityId])!)
77
- .filter(Boolean)
78
- .map(({ data }) => data) ?? [];
79
-
80
- communities = filterByStringComparePartially(communities, 'displayName', params.displayName);
81
-
82
- if (!params.includeDeleted) {
83
- communities = filterByPropEquality(communities, 'isDeleted', false);
84
- }
85
-
86
- if (params.categoryId) {
87
- communities = communities.filter(c => c.categoryIds?.includes(params.categoryId!));
88
- }
89
-
90
- if (params.tags) {
91
- communities = communities.filter(c => c.tags?.some(t => params.tags?.includes(t)));
92
- }
93
-
94
- if (params.membership && userId) {
95
- communities = filterByCommunityMembership(communities, params.membership, userId);
96
- }
97
-
98
- const sortBy = params.sortBy || 'lastCreated';
99
-
100
- if (sortBy === 'lastCreated' || sortBy === 'firstCreated') {
101
- communities = communities.sort(
102
- sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated,
103
- );
104
- }
105
-
106
- /*
107
- * The server returns communities with empty | null displayName's first before
108
- * returning sorted list of communities with displayNames
109
- *
110
- * This section needs to be updated as displayNames can be null as well
111
- */
112
- if (sortBy === 'displayName') {
113
- communities = communities
114
- // this needs to be aligned with the backend data type
115
- .map(c => (c.displayName ? c : { ...c, displayName: '' }))
116
- // @ts-ignore
117
- .sort(sortByDisplayName);
118
- }
119
-
120
- callback({
121
- onNextPage: onFetch,
122
- data: communities,
123
- hasNextPage: !!data.params?.page,
124
- loading: data.loading,
125
- error: data.error,
126
- });
127
- };
128
-
129
- const realtimeRouter = (_: Amity.CommunityActionType) => (community: Amity.Community) => {
130
- const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(cacheKey)?.data;
131
- if (!collection) return;
132
-
133
- /*
134
- * Simply update collection and let responder decide what to do with data
135
- */
136
- collection.data = [...new Set([community.communityId, ...collection.data])];
137
-
138
- pushToCache(cacheKey, collection);
139
- responder(collection);
140
- };
141
-
142
- const onFetch = (initial = false) => {
143
- const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(cacheKey)?.data;
144
-
145
- const communities = collection?.data ?? [];
146
-
147
- if (!initial && communities.length > 0 && !collection?.params.page) return;
148
-
149
- const query = createQuery(queryCommunities, {
150
- ...queryParams,
151
- limit: initial ? limit : undefined,
152
- page: !initial ? collection?.params.page : undefined,
153
- });
154
-
155
- runQuery(
156
- query,
157
- ({ data: result, error, loading, paging }) => {
158
- const data = {
159
- loading,
160
- error,
161
- params: { page: paging?.next },
162
- data: communities,
163
- };
164
-
165
- if (result) {
166
- data.data = initial
167
- ? result.map(getResolver('community'))
168
- : [...new Set([...communities, ...result.map(getResolver('community'))])];
169
- }
170
-
171
- pushToCache(cacheKey, data);
172
-
173
- responder(data);
174
- },
175
- queryOptions(policy, CACHE_SHORTEN_LIFESPAN),
176
- );
177
- };
178
-
179
- disposers.push(
180
- onCommunityCreated(realtimeRouter('onCreate')),
181
- onCommunityDeleted(realtimeRouter('onDelete')),
182
- onCommunityUpdated(realtimeRouter('onUpdate')),
183
- onCommunityJoined(realtimeRouter('onJoin')),
184
- onCommunityLeft(realtimeRouter('onLeft')),
185
- onCommunityUserChanged(realtimeRouter('onMemberCountChanged')),
186
- );
187
-
188
- onFetch(true);
189
46
  disposers.push(() => dropFromCache(cacheKey));
190
47
 
191
48
  return () => {
@@ -1,5 +1,6 @@
1
1
  export * from './observeCommunity';
2
2
 
3
+ export * from './searchCommunities';
3
4
  export * from './getCommunities';
4
5
  export * from './getCommunity';
5
6
  export * from './getTrendingCommunities';
@@ -0,0 +1,129 @@
1
+ /* eslint-disable no-use-before-define */
2
+ import hash from 'object-hash';
3
+ import { pullFromCache, pushToCache } from '~/cache/api';
4
+ import { CommunitiesPaginationController } from './SearchCommunitiesPaginationController';
5
+ import { CommunitiesQueryStreamController } from './SearchCommunitiesQueryStreamController';
6
+ import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
7
+ import {
8
+ onCommunityCreated,
9
+ onCommunityDeleted,
10
+ onCommunityUpdated,
11
+ } from '~/communityRepository/events';
12
+ import {
13
+ filterByCommunityMembership,
14
+ filterByPropEquality,
15
+ sortByDisplayName,
16
+ sortByFirstCreated,
17
+ sortByLastCreated,
18
+ } from '~/core/query';
19
+ import { prepareCommunityPayload } from '~/communityRepository/utils';
20
+ import { getActiveClient } from '~/client';
21
+ import { EnumCommunityActions } from './enums';
22
+
23
+ export class SearchCommunityLiveCollectionController extends LiveCollectionController<
24
+ 'community',
25
+ Amity.SearchCommunityLiveCollection,
26
+ Amity.Community,
27
+ CommunitiesPaginationController
28
+ > {
29
+ private queryStreamController: CommunitiesQueryStreamController;
30
+
31
+ private query: Amity.SearchCommunityLiveCollection;
32
+
33
+ constructor(
34
+ query: Amity.SearchCommunityLiveCollection,
35
+ callback: Amity.LiveCollectionCallback<Amity.Community>,
36
+ ) {
37
+ const queryStreamId = hash(query);
38
+ const cacheKey = ['community', 'collection', queryStreamId];
39
+ const paginationController = new CommunitiesPaginationController(query);
40
+
41
+ super(paginationController, queryStreamId, cacheKey, callback);
42
+
43
+ this.query = query;
44
+ this.queryStreamController = new CommunitiesQueryStreamController(
45
+ this.query,
46
+ this.cacheKey,
47
+ this.notifyChange.bind(this),
48
+ prepareCommunityPayload,
49
+ );
50
+
51
+ this.callback = callback.bind(this);
52
+ this.loadPage({ initial: true });
53
+ }
54
+
55
+ protected setup() {
56
+ const collection = pullFromCache<Amity.SearchCommunityLiveCollectionCache>(this.cacheKey)?.data;
57
+ if (!collection) {
58
+ pushToCache(this.cacheKey, {
59
+ data: [],
60
+ params: {},
61
+ });
62
+ }
63
+ }
64
+
65
+ protected async persistModel(queryPayload: Amity.CommunityPayload & Amity.Pagination) {
66
+ await this.queryStreamController.saveToMainDB(queryPayload);
67
+ }
68
+
69
+ protected persistQueryStream({
70
+ response,
71
+ direction,
72
+ refresh,
73
+ }: Amity.LiveCollectionPersistQueryStreamParams<'community'>) {
74
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
75
+ }
76
+
77
+ startSubscription() {
78
+ return this.queryStreamController.subscribeRTE([
79
+ { fn: onCommunityDeleted, action: EnumCommunityActions.OnCommunityDeleted },
80
+ { fn: onCommunityUpdated, action: EnumCommunityActions.OnCommunityUpdated },
81
+ ]);
82
+ }
83
+
84
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
85
+ const collection = pullFromCache<Amity.SearchCommunityLiveCollectionCache>(this.cacheKey)?.data;
86
+ if (!collection) return;
87
+
88
+ const data = this.applyFilter(
89
+ collection.data
90
+ .map(id => pullFromCache<Amity.Community>(['community', 'get', id])!)
91
+ .filter(Boolean)
92
+ .map(({ data }) => data) ?? [],
93
+ );
94
+
95
+ if (!this.shouldNotify(data) && origin === 'event') return;
96
+
97
+ this.callback({
98
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
99
+ data,
100
+ hasNextPage: !!this.paginationController.getNextToken(),
101
+ loading,
102
+ error,
103
+ });
104
+ }
105
+
106
+ applyFilter(data: Amity.Community[]) {
107
+ const { userId } = getActiveClient();
108
+
109
+ let communities = data;
110
+
111
+ if (this.query.includeDeleted) {
112
+ communities = filterByPropEquality(communities, 'isDeleted', false);
113
+ }
114
+
115
+ if (this.query.categoryId) {
116
+ communities = communities.filter(c => c.categoryIds?.includes(this.query.categoryId!));
117
+ }
118
+
119
+ if (this.query.tags) {
120
+ communities = communities.filter(c => c.tags?.some(t => this.query.tags?.includes(t)));
121
+ }
122
+
123
+ if (this.query.membership && userId) {
124
+ communities = filterByCommunityMembership(communities, this.query.membership, userId);
125
+ }
126
+
127
+ return communities;
128
+ }
129
+ }
@@ -0,0 +1,31 @@
1
+ import { PaginationController } from '~/core/liveCollection/PaginationController';
2
+ import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';
3
+ import { inferIsDeleted } from '~/utils/inferIsDeleted';
4
+
5
+ /**
6
+ * TODO: handle cache receive cache option, and cache policy
7
+ * TODO: check if querybyIds is supported
8
+ */
9
+ export class CommunitiesPaginationController extends PaginationController<
10
+ 'community',
11
+ Amity.SearchCommunityLiveCollection
12
+ > {
13
+ async getRequest(queryParams: Amity.SearchCommunityLiveCollection, token: string | undefined) {
14
+ const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, ...params } = queryParams;
15
+ const options = token ? { token } : { limit };
16
+
17
+ const { data: queryResponse } = await this.http.get<Amity.CommunityPayload & Amity.Pagination>(
18
+ `/api/v3/communities`,
19
+ {
20
+ params: {
21
+ ...params,
22
+ isDeleted: inferIsDeleted(params.includeDeleted),
23
+ keyword: params.displayName,
24
+ filter: params.membership,
25
+ options,
26
+ },
27
+ },
28
+ );
29
+ return queryResponse;
30
+ }
31
+ }
@@ -0,0 +1,81 @@
1
+ import { QueryStreamController } from '~/core/liveCollection/QueryStreamController';
2
+ import { pullFromCache, pushToCache } from '~/cache/api';
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+ import { getResolver } from '~/core/model';
5
+ import { getActiveClient } from '~/client';
6
+ import { EnumCommunityActions } from './enums';
7
+
8
+ export class CommunitiesQueryStreamController extends QueryStreamController<
9
+ Amity.CommunityPayload,
10
+ Amity.SearchCommunityLiveCollection
11
+ > {
12
+ private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
13
+
14
+ private preparePayload: (response: Amity.CommunityPayload) => Amity.ProcessedCommunityPayload;
15
+
16
+ constructor(
17
+ query: Amity.SearchCommunityLiveCollection,
18
+ cacheKey: string[],
19
+ notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
20
+ preparePayload: (response: Amity.CommunityPayload) => Amity.ProcessedCommunityPayload,
21
+ ) {
22
+ super(query, cacheKey);
23
+ this.notifyChange = notifyChange;
24
+ this.preparePayload = preparePayload;
25
+ }
26
+
27
+ async saveToMainDB(response: Amity.CommunityPayload) {
28
+ const processedPayload = await this.preparePayload(response);
29
+
30
+ const client = getActiveClient();
31
+ const cachedAt = client.cache && Date.now();
32
+
33
+ if (client.cache) {
34
+ ingestInCache(processedPayload, { cachedAt });
35
+ }
36
+ }
37
+
38
+ appendToQueryStream(
39
+ response: Amity.CommunityPayload & Partial<Amity.Pagination>,
40
+ direction: Amity.LiveCollectionPageDirection,
41
+ refresh = false,
42
+ ) {
43
+ if (refresh) {
44
+ pushToCache(this.cacheKey, {
45
+ data: response.communities.map(getResolver('community')),
46
+ });
47
+ } else {
48
+ const collection = pullFromCache<Amity.SearchCommunityLiveCollectionCache>(
49
+ this.cacheKey,
50
+ )?.data;
51
+
52
+ const communities = collection?.data ?? [];
53
+
54
+ pushToCache(this.cacheKey, {
55
+ ...collection,
56
+ data: [...new Set([...communities, ...response.communities.map(getResolver('community'))])],
57
+ });
58
+ }
59
+ }
60
+
61
+ reactor(action: EnumCommunityActions) {
62
+ return (community: Amity.Community) => {
63
+ const collection = pullFromCache<Amity.SearchCommunityLiveCollectionCache>(
64
+ this.cacheKey,
65
+ )?.data;
66
+ if (!collection) return;
67
+
68
+ pushToCache(this.cacheKey, collection);
69
+ this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
70
+ };
71
+ }
72
+
73
+ subscribeRTE(
74
+ createSubscriber: {
75
+ fn: (reactor: (channel: Amity.Community) => void) => Amity.Unsubscriber;
76
+ action: EnumCommunityActions;
77
+ }[],
78
+ ) {
79
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
80
+ }
81
+ }
@@ -0,0 +1,5 @@
1
+ export enum EnumCommunityActions {
2
+ OnCommunityCreated = 'onCommunityCreated',
3
+ OnCommunityDeleted = 'onCommunityDeleted',
4
+ OnCommunityUpdated = 'onCommunityUpdated',
5
+ }
@@ -0,0 +1,56 @@
1
+ import { dropFromCache } from '~/cache/api';
2
+ import { getActiveClient } from '~/client/api';
3
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
4
+ import { SearchCommunityLiveCollectionController } from './searchCommunities/SearchCommunitiesLiveCollectionController';
5
+
6
+ /* begin_public_function
7
+ id: community.query
8
+ */
9
+ /**
10
+ * ```js
11
+ * import { CommunityRepository } from '@amityco/ts-sdk-react-native'
12
+ *
13
+ * let communities = []
14
+ * const unsub = CommunityRepository.searchCommunities({
15
+ * displayName: Amity.Community['displayName'],
16
+ * }, response => merge(communities, response.data))
17
+ * ```
18
+ *
19
+ * Observe all mutations on a list of {@link Amity.Community}s
20
+ *
21
+ * @param params for querying communities
22
+ * @param callback the function to call when new data are available
23
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the communities
24
+ *
25
+ * @category Community Live Collection
26
+ */
27
+ export const searchCommunities = (
28
+ params: Amity.CommunityLiveCollection,
29
+ callback: Amity.LiveCollectionCallback<Amity.Community>,
30
+ config?: Amity.LiveCollectionConfig,
31
+ ) => {
32
+ const { log, cache } = getActiveClient();
33
+
34
+ if (!cache) {
35
+ console.log(ENABLE_CACHE_MESSAGE);
36
+ }
37
+
38
+ const timestamp = Date.now();
39
+ log(`searchCommunities(tmpid: ${timestamp}) > listen`);
40
+
41
+ const searchCommunitiesLiveCollection = new SearchCommunityLiveCollectionController(
42
+ params,
43
+ callback,
44
+ );
45
+ const disposers = searchCommunitiesLiveCollection.startSubscription();
46
+
47
+ const cacheKey = searchCommunitiesLiveCollection.getCacheKey();
48
+
49
+ disposers.push(() => dropFromCache(cacheKey));
50
+
51
+ return () => {
52
+ log(`searchCommunities(tmpid: ${timestamp}) > dispose`);
53
+ disposers.forEach(fn => fn());
54
+ };
55
+ };
56
+ /* end_public_function */
@@ -1,4 +1,5 @@
1
1
  import { CommunityPostSettingMaps, DefaultCommunityPostSetting } from '~/@types';
2
+ import { pullFromCache } from '~/cache/api';
2
3
  import { withUsers } from '~/group/utils';
3
4
  import { updateMembershipStatus } from './communityWithMembership';
4
5
 
@@ -13,6 +14,22 @@ const getMatchPostSetting = (value: {
13
14
  value.onlyAdminCanPost === CommunityPostSettingMaps[key].onlyAdminCanPost,
14
15
  ) ?? DefaultCommunityPostSetting;
15
16
 
17
+ const convertCommunityUsersToUniqueObject = (
18
+ communityUsers: Amity.RawMembership<'community'>[],
19
+ ) => {
20
+ if (!communityUsers) return communityUsers;
21
+
22
+ const result: {
23
+ [key: Amity.RawMembership<'community'>['userId']]: Amity.RawMembership<'community'>;
24
+ } = {};
25
+
26
+ communityUsers.forEach(user => {
27
+ result[user.userId] = user;
28
+ });
29
+
30
+ return result;
31
+ };
32
+
16
33
  export const prepareCommunityPayload = (
17
34
  rawPayload: Amity.CommunityPayload,
18
35
  ): Amity.ProcessedCommunityPayload => {
@@ -27,7 +44,24 @@ export const prepareCommunityPayload = (
27
44
  }),
28
45
  );
29
46
 
30
- const communityUsers = withUsers(rawPayload.communityUsers);
47
+ const mergeCommunityUsers = communities.reduce<{
48
+ [key: string]: Amity.RawMembership<'community'>;
49
+ }>((acc, { communityId }) => {
50
+ const users = pullFromCache<Amity.RawMembership<'community'>[]>([
51
+ 'communityUsers',
52
+ 'collection',
53
+ communityId,
54
+ ])?.data;
55
+
56
+ if (!users) return acc;
57
+
58
+ return {
59
+ ...convertCommunityUsersToUniqueObject(users),
60
+ ...acc,
61
+ };
62
+ }, convertCommunityUsersToUniqueObject(rawPayload.communityUsers));
63
+
64
+ const communityUsers = withUsers(Object.values(mergeCommunityUsers));
31
65
 
32
66
  const communityWithMembershipStatus = updateMembershipStatus(communities, communityUsers);
33
67
 
@@ -0,0 +1,16 @@
1
+ import { pushToCache } from '~/cache/api';
2
+
3
+ export const saveCommunityUsers = (
4
+ communities: Amity.CommunityPayload['communities'],
5
+ communityUsers: Amity.CommunityPayload['communityUsers'],
6
+ ) => {
7
+ if (communities.length === 0 || communityUsers.length === 0) return;
8
+
9
+ communities.forEach(({ communityId }) => {
10
+ const collection = communityUsers.filter(
11
+ ({ communityId: userCommunityId }) => communityId === userCommunityId,
12
+ );
13
+
14
+ pushToCache(['communityUsers', 'collection', communityId], collection);
15
+ });
16
+ };
@@ -57,10 +57,13 @@ export abstract class LiveCollectionController<
57
57
  }
58
58
  }
59
59
 
60
- protected loadPage(
60
+ protected loadPage({
61
61
  initial = false,
62
- direction: Amity.LiveCollectionPageDirection = Amity.LiveCollectionPageDirection.NEXT,
63
- ) {
62
+ direction = Amity.LiveCollectionPageDirection.NEXT,
63
+ }: {
64
+ initial?: boolean;
65
+ direction?: Amity.LiveCollectionPageDirection;
66
+ }) {
64
67
  this.setup();
65
68
 
66
69
  this.notifyChange({ origin: Amity.LiveDataOrigin.LOCAL, loading: true });
@@ -44,6 +44,7 @@ const idResolvers: Resolvers = {
44
44
  reactor: ({ reactionId }) => reactionId,
45
45
 
46
46
  stream: ({ streamId }) => streamId,
47
+ streamModeration: ({ streamId }) => streamId,
47
48
 
48
49
  follow: ({ from, to }) => `${from}#${to}`,
49
50
  followInfo: ({ userId }) => userId,
@@ -41,6 +41,7 @@ export const PAYLOAD2MODEL: Record<string, Amity.Domain> = {
41
41
  reactions: 'reaction',
42
42
 
43
43
  videoStreamings: 'stream',
44
+ videoStreamModerations: 'streamModeration',
44
45
 
45
46
  follows: 'follow',
46
47
  followCounts: 'followCount',