@amityco/ts-sdk-react-native 6.24.1-13ac472.0 → 6.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/.env +26 -26
  2. package/dist/@types/domains/community.d.ts +15 -1
  3. package/dist/@types/domains/community.d.ts.map +1 -1
  4. package/dist/client/api/secureLogout.d.ts +1 -1
  5. package/dist/client/utils/markerSyncEngine.d.ts.map +1 -1
  6. package/dist/commentRepository/events/utils.d.ts.map +1 -1
  7. package/dist/communityRepository/api/createCommunity.d.ts.map +1 -1
  8. package/dist/communityRepository/api/getCommunities.d.ts.map +1 -1
  9. package/dist/communityRepository/api/getCommunity.d.ts.map +1 -1
  10. package/dist/communityRepository/api/queryCommunities.d.ts.map +1 -1
  11. package/dist/communityRepository/api/updateCommunity.d.ts.map +1 -1
  12. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.d.ts +14 -0
  13. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.d.ts.map +1 -0
  14. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.d.ts +9 -0
  15. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.d.ts.map +1 -0
  16. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.d.ts +15 -0
  17. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.d.ts.map +1 -0
  18. package/dist/communityRepository/communityMembership/observers/getMembers/enums.d.ts +11 -0
  19. package/dist/communityRepository/communityMembership/observers/getMembers/enums.d.ts.map +1 -0
  20. package/dist/communityRepository/communityMembership/observers/getMembers.d.ts.map +1 -1
  21. package/dist/communityRepository/communityMembership/observers/index.d.ts +1 -0
  22. package/dist/communityRepository/communityMembership/observers/index.d.ts.map +1 -1
  23. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.d.ts +14 -0
  24. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.d.ts.map +1 -0
  25. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.d.ts +9 -0
  26. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.d.ts.map +1 -0
  27. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.d.ts +15 -0
  28. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.d.ts.map +1 -0
  29. package/dist/communityRepository/communityMembership/observers/searchMembers/enums.d.ts +10 -0
  30. package/dist/communityRepository/communityMembership/observers/searchMembers/enums.d.ts.map +1 -0
  31. package/dist/communityRepository/communityMembership/observers/searchMembers.d.ts +20 -0
  32. package/dist/communityRepository/communityMembership/observers/searchMembers.d.ts.map +1 -0
  33. package/dist/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.d.ts.map +1 -1
  34. package/dist/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.d.ts +3 -2
  35. package/dist/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.d.ts.map +1 -1
  36. package/dist/communityRepository/observers/getCommunities.d.ts.map +1 -1
  37. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.d.ts.map +1 -1
  38. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.d.ts +3 -2
  39. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.d.ts.map +1 -1
  40. package/dist/communityRepository/observers/searchCommunities.d.ts +1 -1
  41. package/dist/communityRepository/utils/payload.d.ts.map +1 -1
  42. package/dist/communityRepository/utils/saveCommunityUsers.d.ts +2 -0
  43. package/dist/communityRepository/utils/saveCommunityUsers.d.ts.map +1 -0
  44. package/dist/fileRepository/api/createFile.d.ts.map +1 -1
  45. package/dist/fileRepository/api/createImage.d.ts.map +1 -1
  46. package/dist/fileRepository/api/createVideo.d.ts.map +1 -1
  47. package/dist/fileRepository/api/uploadFile.d.ts.map +1 -1
  48. package/dist/fileRepository/api/uploadImage.d.ts.map +1 -1
  49. package/dist/fileRepository/api/uploadVideo.d.ts.map +1 -1
  50. package/dist/index.cjs.js +803 -475
  51. package/dist/index.esm.js +779 -451
  52. package/dist/index.umd.js +4 -4
  53. package/dist/streamRepository/api/getStreams.d.ts +2 -2
  54. package/dist/utils/liveObject.d.ts +3 -0
  55. package/dist/utils/liveObject.d.ts.map +1 -1
  56. package/package.json +1 -1
  57. package/src/@types/domains/community.ts +25 -1
  58. package/src/client/api/secureLogout.ts +1 -1
  59. package/src/client/utils/markerSyncEngine.ts +4 -1
  60. package/src/commentRepository/events/utils.ts +8 -5
  61. package/src/communityRepository/api/createCommunity.ts +5 -2
  62. package/src/communityRepository/api/getCommunities.ts +5 -1
  63. package/src/communityRepository/api/getCommunity.ts +5 -1
  64. package/src/communityRepository/api/queryCommunities.ts +2 -2
  65. package/src/communityRepository/api/updateCommunity.ts +5 -1
  66. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.ts +151 -0
  67. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersPaginationController.ts +26 -0
  68. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersQueryStreamController.ts +114 -0
  69. package/src/communityRepository/communityMembership/observers/getMembers/enums.ts +10 -0
  70. package/src/communityRepository/communityMembership/observers/getMembers.ts +15 -128
  71. package/src/communityRepository/communityMembership/observers/index.ts +1 -0
  72. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.ts +130 -0
  73. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersPaginationController.ts +29 -0
  74. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersQueryStreamController.ts +105 -0
  75. package/src/communityRepository/communityMembership/observers/searchMembers/enums.ts +9 -0
  76. package/src/communityRepository/communityMembership/observers/searchMembers.ts +60 -0
  77. package/src/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.ts +9 -1
  78. package/src/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.ts +6 -3
  79. package/src/communityRepository/observers/getCommunities.ts +7 -150
  80. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.ts +11 -13
  81. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.ts +3 -2
  82. package/src/communityRepository/observers/searchCommunities.ts +1 -1
  83. package/src/communityRepository/utils/payload.ts +35 -1
  84. package/src/communityRepository/utils/saveCommunityUsers.ts +16 -0
  85. package/src/fileRepository/api/createFile.ts +5 -2
  86. package/src/fileRepository/api/createImage.ts +6 -2
  87. package/src/fileRepository/api/createVideo.ts +5 -2
  88. package/src/fileRepository/api/uploadFile.ts +5 -2
  89. package/src/fileRepository/api/uploadImage.ts +5 -2
  90. package/src/fileRepository/api/uploadVideo.ts +5 -2
  91. package/src/streamRepository/api/getStreams.ts +2 -2
  92. package/src/utils/liveObject.ts +3 -0
@@ -1,32 +1,13 @@
1
- /* eslint-disable no-use-before-define */
2
- import { getResolver } from '~/core/model';
3
- import { pullFromCache, pushToCache } from '~/cache/api';
4
1
  import { getActiveClient } from '~/client/api';
5
2
  import {
6
- createQuery,
7
3
  filterByPropIntersection,
8
4
  filterBySearchTerm,
9
- queryOptions,
10
- runQuery,
11
5
  sortByFirstCreated,
12
6
  sortByLastCreated,
13
7
  } from '~/core/query';
14
- import {
15
- COLLECTION_DEFAULT_CACHING_POLICY,
16
- COLLECTION_DEFAULT_PAGINATION_LIMIT,
17
- ENABLE_CACHE_MESSAGE,
18
- } from '~/utils/constants';
19
- import { isNonNullable } from '~/utils';
20
- import {
21
- onCommunityJoined,
22
- onCommunityLeft,
23
- onCommunityUserBanned,
24
- onCommunityUserChanged,
25
- onCommunityUserRoleAdded,
26
- onCommunityUserRoleRemoved,
27
- onCommunityUserUnbanned,
28
- } from '../events';
29
- import { queryCommunityMembers } from '../api/queryCommunityMembers';
8
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
9
+ import { CommunityMembersLiveCollectionController } from './getMembers/CommunityMembersLiveCollectionController';
10
+ import { dropFromCache } from '~/cache/api';
30
11
 
31
12
  /*
32
13
  * Exported for testing
@@ -92,114 +73,20 @@ export const getMembers = (
92
73
  const timestamp = Date.now();
93
74
  log(`getMembers(tmpid: ${timestamp}) > listen`);
94
75
 
95
- const { limit: queryLimit, ...queryParams } = params;
96
-
97
- const limit = queryLimit ?? COLLECTION_DEFAULT_PAGINATION_LIMIT;
98
- const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config ?? {};
99
-
100
- const disposers: Amity.Unsubscriber[] = [];
101
- const cacheKey = ['communityUsers', 'collection', queryParams as unknown as Amity.Serializable];
102
- const cacheByCommunityIdKey = [
103
- 'communityUsers',
104
- 'collection',
105
- { communityId: params.communityId },
106
- ];
107
-
108
- const responder = (data: Amity.CommunityMemberLiveCollectionCache, isEventModel = false) => {
109
- const communityMembers: Amity.Membership<'community'>[] =
110
- data.data
111
- .map(id => pullFromCache<Amity.Membership<'community'>>(['communityUsers', 'get', id])!)
112
- .filter(Boolean)
113
- .map(({ data }) => data) ?? [];
114
-
115
- callback({
116
- onNextPage: onFetch,
117
- data: isEventModel ? applyFilter(communityMembers, params) : communityMembers,
118
- hasNextPage: !!data.params?.page,
119
- loading: data.loading,
120
- error: data.error,
121
- });
122
- };
123
-
124
- const realtimeRouter =
125
- (_: Amity.CommunityMemberActionType) =>
126
- (_: Amity.Community, communityMembers: Amity.Membership<'community'>[]) => {
127
- const collection = pullFromCache<Amity.CommunityMemberLiveCollectionCache>(cacheKey)?.data;
128
- if (!collection) {
129
- return;
130
- }
131
-
132
- const communityMemberCacheIds = communityMembers
133
- .map(communityMember => {
134
- if (params.communityId !== communityMember.communityId) {
135
- return;
136
- }
137
-
138
- const communityMemberCacheId = getResolver('communityUsers')({
139
- communityId: params.communityId,
140
- userId: communityMember.userId,
141
- });
142
-
143
- return communityMemberCacheId;
144
- })
145
- .filter(isNonNullable);
146
-
147
- collection.data = [...new Set([...communityMemberCacheIds, ...collection.data])];
148
-
149
- pushToCache(cacheKey, collection);
150
- pushToCache(cacheByCommunityIdKey, collection);
151
- responder(collection, true);
152
- };
153
-
154
- const onFetch = (initial = false) => {
155
- const collection = pullFromCache<Amity.CommunityMemberLiveCollectionCache>(cacheKey)?.data;
156
-
157
- const communityMembers = collection?.data ?? [];
158
-
159
- if (!initial && communityMembers.length > 0 && !collection?.params.page) return;
160
-
161
- const query = createQuery(queryCommunityMembers, {
162
- ...queryParams,
163
- limit: initial ? limit : undefined,
164
- page: !initial ? collection?.params.page : undefined,
165
- });
166
-
167
- runQuery(
168
- query,
169
- ({ data: result, error, loading, paging }) => {
170
- const data = {
171
- loading,
172
- error,
173
- params: { page: paging?.next },
174
- data: communityMembers,
175
- };
176
-
177
- if (result) {
178
- data.data = [
179
- ...new Set([...communityMembers, ...result.map(getResolver('communityUsers'))]),
180
- ];
181
- }
182
-
183
- pushToCache(cacheKey, data);
184
- pushToCache(cacheByCommunityIdKey, data);
185
-
186
- responder(data);
187
- },
188
- queryOptions(policy),
189
- );
190
- };
191
-
192
- disposers.push(
193
- onCommunityLeft(realtimeRouter('onLeft')),
194
- onCommunityJoined(realtimeRouter('onJoin')),
195
- onCommunityUserBanned(realtimeRouter('onBan')),
196
- onCommunityUserUnbanned(realtimeRouter('onUnban')),
197
- onCommunityUserChanged(realtimeRouter('onMemberCountChanged')),
198
- onCommunityUserRoleAdded(realtimeRouter('onMemberCountChanged')),
199
- onCommunityUserRoleRemoved(realtimeRouter('onMemberCountChanged')),
76
+ const communityMemberLiveCollection = new CommunityMembersLiveCollectionController(
77
+ params,
78
+ resp => {
79
+ console.log('resp', resp);
80
+ callback(resp);
81
+ },
200
82
  );
83
+ const disposers = communityMemberLiveCollection.startSubscription();
84
+
85
+ const cacheKey = communityMemberLiveCollection.getCacheKey();
201
86
 
202
- onFetch(true);
87
+ disposers.push(() => {
88
+ dropFromCache(cacheKey);
89
+ });
203
90
 
204
91
  return () => {
205
92
  log(`getMembers(tmpid: ${timestamp}) > dispose`);
@@ -1 +1,2 @@
1
1
  export * from './getMembers';
2
+ export * from './searchMembers';
@@ -0,0 +1,130 @@
1
+ /* eslint-disable no-use-before-define */
2
+ import hash from 'object-hash';
3
+ import { pullFromCache, pushToCache } from '~/cache/api';
4
+ import { SearchCommunityMembersPaginationController } from './SearchCommunityMembersPaginationController';
5
+ import { SearchCommunityMembersQueryStreamController } from './SearchCommunityMembersQueryStreamController';
6
+ import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
7
+ import {
8
+ onCommunityJoined,
9
+ onCommunityLeft,
10
+ onCommunityUserBanned,
11
+ onCommunityUserChanged,
12
+ onCommunityUserRoleRemoved,
13
+ onCommunityUserUnbanned,
14
+ } from '~/communityRepository/communityMembership/events';
15
+ import { filterByPropIntersection, filterBySearchTerm } from '~/core/query';
16
+ import { prepareCommunityPayload } from '~/communityRepository/utils';
17
+ import { isNonNullable } from '~/utils';
18
+ import { EnumCommunityMemberActions } from './enums';
19
+
20
+ export class SearchCommunityMembersLiveCollectionController extends LiveCollectionController<
21
+ 'communityUser',
22
+ Amity.SearchCommunityMemberLiveCollection,
23
+ Amity.Membership<'community'>,
24
+ SearchCommunityMembersPaginationController
25
+ > {
26
+ private queryStreamController: SearchCommunityMembersQueryStreamController;
27
+
28
+ private query: Amity.SearchCommunityMemberLiveCollection;
29
+
30
+ constructor(
31
+ query: Amity.SearchCommunityMemberLiveCollection,
32
+ callback: Amity.LiveCollectionCallback<Amity.Membership<'community'>>,
33
+ ) {
34
+ const queryStreamId = hash(query);
35
+ const cacheKey = ['communityUsers', 'collection', queryStreamId];
36
+ const paginationController = new SearchCommunityMembersPaginationController(query);
37
+
38
+ super(paginationController, queryStreamId, cacheKey, callback);
39
+
40
+ this.query = query;
41
+ this.queryStreamController = new SearchCommunityMembersQueryStreamController(
42
+ this.query,
43
+ this.cacheKey,
44
+ this.notifyChange.bind(this),
45
+ prepareCommunityPayload,
46
+ );
47
+
48
+ this.callback = callback.bind(this);
49
+ this.loadPage({ initial: true });
50
+ }
51
+
52
+ protected setup() {
53
+ const collection = pullFromCache<Amity.SearchCommunityMemberLiveCollectionCache>(
54
+ this.cacheKey,
55
+ )?.data;
56
+ if (!collection) {
57
+ pushToCache(this.cacheKey, {
58
+ data: [],
59
+ params: {},
60
+ });
61
+ }
62
+ }
63
+
64
+ protected async persistModel(queryPayload: Amity.CommunityMembershipPayload & Amity.Pagination) {
65
+ await this.queryStreamController.saveToMainDB(queryPayload);
66
+ }
67
+
68
+ protected persistQueryStream({
69
+ response,
70
+ direction,
71
+ refresh,
72
+ }: Amity.LiveCollectionPersistQueryStreamParams<'communityUser'>) {
73
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
74
+ }
75
+
76
+ startSubscription() {
77
+ return this.queryStreamController.subscribeRTE([
78
+ { fn: onCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
79
+ { fn: onCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
80
+ { fn: onCommunityUserBanned, action: EnumCommunityMemberActions.OnCommunityUserBanned },
81
+ { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnCommunityUserChanged },
82
+ {
83
+ fn: onCommunityUserRoleRemoved,
84
+ action: EnumCommunityMemberActions.OnCommunityUserRoleRemoved,
85
+ },
86
+ { fn: onCommunityUserUnbanned, action: EnumCommunityMemberActions.OnCommunityUserUnbanned },
87
+ ]);
88
+ }
89
+
90
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
91
+ const collection = pullFromCache<Amity.SearchCommunityMemberLiveCollectionCache>(
92
+ this.cacheKey,
93
+ )?.data;
94
+ if (!collection) return;
95
+
96
+ const data = this.applyFilter(
97
+ collection.data
98
+ .map(id => pullFromCache<Amity.Membership<'community'>>(['communityUsers', 'get', id])!)
99
+ .filter(isNonNullable)
100
+ .map(({ data }) => data) ?? [],
101
+ );
102
+
103
+ if (!this.shouldNotify(data) && origin === 'event') return;
104
+
105
+ this.callback({
106
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
107
+ data,
108
+ hasNextPage: !!this.paginationController.getNextToken(),
109
+ loading,
110
+ error,
111
+ });
112
+ }
113
+
114
+ applyFilter(data: Amity.Membership<'community'>[]) {
115
+ let communityMembers = filterByPropIntersection(data, 'roles', this.query.roles);
116
+
117
+ if (this.query.memberships) {
118
+ communityMembers = communityMembers.filter(({ communityMembership }) => {
119
+ const memberships: Amity.GroupMembership[] = this.query.memberships || [];
120
+ return memberships.includes(communityMembership);
121
+ });
122
+ }
123
+
124
+ if (this.query.search) {
125
+ communityMembers = filterBySearchTerm(communityMembers, this.query.search);
126
+ }
127
+
128
+ return communityMembers;
129
+ }
130
+ }
@@ -0,0 +1,29 @@
1
+ import { PaginationController } from '~/core/liveCollection/PaginationController';
2
+ import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';
3
+
4
+ /**
5
+ * TODO: handle cache receive cache option, and cache policy
6
+ * TODO: check if querybyIds is supported
7
+ */
8
+ export class SearchCommunityMembersPaginationController extends PaginationController<
9
+ 'communityUser',
10
+ Amity.CommunityMemberLiveCollection
11
+ > {
12
+ async getRequest(
13
+ queryParams: Amity.SearchCommunityMemberLiveCollection,
14
+ token: string | undefined,
15
+ ) {
16
+ const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, ...params } = queryParams;
17
+ const options = token ? { token } : { limit };
18
+
19
+ const { data: queryResponse } = await this.http.get<
20
+ Amity.CommunityMembershipPayload & Amity.Pagination
21
+ >(`/api/v3/communities/${params.communityId}/users`, {
22
+ params: {
23
+ ...params,
24
+ options,
25
+ },
26
+ });
27
+ return queryResponse;
28
+ }
29
+ }
@@ -0,0 +1,105 @@
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 { EnumCommunityMemberActions } from './enums';
7
+
8
+ export class SearchCommunityMembersQueryStreamController extends QueryStreamController<
9
+ Amity.CommunityMembershipPayload,
10
+ Amity.SearchCommunityMemberLiveCollection
11
+ > {
12
+ private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
13
+
14
+ private preparePayload: (
15
+ response: Amity.CommunityMembershipPayload,
16
+ ) => Amity.ProcessedCommunityMembershipPayload;
17
+
18
+ constructor(
19
+ query: Amity.SearchCommunityMemberLiveCollection,
20
+ cacheKey: string[],
21
+ notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
22
+ preparePayload: (
23
+ response: Amity.CommunityMembershipPayload,
24
+ ) => Amity.ProcessedCommunityMembershipPayload,
25
+ ) {
26
+ super(query, cacheKey);
27
+ this.notifyChange = notifyChange;
28
+ this.preparePayload = preparePayload;
29
+ }
30
+
31
+ async saveToMainDB(response: Amity.CommunityMembershipPayload) {
32
+ const processedPayload = await this.preparePayload(response);
33
+
34
+ const client = getActiveClient();
35
+ const cachedAt = client.cache && Date.now();
36
+
37
+ if (client.cache) {
38
+ ingestInCache(processedPayload, { cachedAt });
39
+ }
40
+ }
41
+
42
+ appendToQueryStream(
43
+ response: Amity.CommunityMembershipPayload & Partial<Amity.Pagination>,
44
+ direction: Amity.LiveCollectionPageDirection,
45
+ refresh = false,
46
+ ) {
47
+ if (refresh) {
48
+ pushToCache(this.cacheKey, {
49
+ data: response.communityUsers.map(({ communityId, userId }) =>
50
+ getResolver('communityUsers')({ communityId, userId }),
51
+ ),
52
+ });
53
+ } else {
54
+ const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(this.cacheKey)?.data;
55
+
56
+ const communityUsers = collection?.data ?? [];
57
+
58
+ pushToCache(this.cacheKey, {
59
+ ...collection,
60
+ data: [
61
+ ...new Set([
62
+ ...communityUsers,
63
+ ...response.communityUsers.map(({ communityId, userId }) =>
64
+ getResolver('communityUsers')({ communityId, userId }),
65
+ ),
66
+ ]),
67
+ ],
68
+ });
69
+ }
70
+ }
71
+
72
+ reactor(action: EnumCommunityMemberActions) {
73
+ return (community: Amity.Community, communityMembers: Amity.Membership<'community'>[]) => {
74
+ const collection = pullFromCache<Amity.SearchCommunityMemberLiveCollectionCache>(
75
+ this.cacheKey,
76
+ )?.data;
77
+ if (!collection) return;
78
+
79
+ communityMembers.forEach(communityMember => {
80
+ const communityMemberCacheId = getResolver('communityUsers')({
81
+ communityId: this.query.communityId,
82
+ userId: communityMember.userId,
83
+ });
84
+
85
+ if (communityMember.communityMembership === 'none') {
86
+ collection.data = collection.data.filter(m => m !== communityMemberCacheId);
87
+ }
88
+ });
89
+
90
+ pushToCache(this.cacheKey, collection);
91
+ this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
92
+ };
93
+ }
94
+
95
+ subscribeRTE(
96
+ createSubscriber: {
97
+ fn: (
98
+ reactor: (channel: Amity.Community, communityUser: Amity.Membership<'community'>[]) => void,
99
+ ) => Amity.Unsubscriber;
100
+ action: EnumCommunityMemberActions;
101
+ }[],
102
+ ) {
103
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
104
+ }
105
+ }
@@ -0,0 +1,9 @@
1
+ export enum EnumCommunityMemberActions {
2
+ OnCommunityJoined = 'onCommunityJoined',
3
+ OnCommunityLeft = 'onCommunityLeft',
4
+ OnCommunityUserBanned = 'onCommunityUserBanned',
5
+ OnCommunityUserChanged = 'onCommunityUserChanged',
6
+ OnCommunityUserRoleAdded = 'onCommunityUserRoleAdded',
7
+ OnCommunityUserRoleRemoved = 'onCommunityUserRoleRemoved',
8
+ OnCommunityUserUnbanned = 'onCommunityUserUnbanned',
9
+ }
@@ -0,0 +1,60 @@
1
+ import { getActiveClient } from '~/client/api';
2
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
3
+ import { dropFromCache } from '~/cache/api';
4
+ import { SearchCommunityMembersLiveCollectionController } from './searchMembers/SearchCommunityMembersLiveCollectionController';
5
+
6
+ /* begin_public_function
7
+ id: community.membership.query
8
+ */
9
+ /**
10
+ * ```js
11
+ * import { searchMembers } from '@amityco/ts-sdk-react-native'
12
+ *
13
+ * let communityMembers = []
14
+ * const unsub = searchMembers({
15
+ * communityId: Amity.Community['communityId'],
16
+ * }, response => merge(communityMembers, response.data))
17
+ * ```
18
+ *
19
+ * Observe all mutations on a list of {@link Amity.CommunityUser}s
20
+ *
21
+ * @param params for querying community users
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 community users
24
+ *
25
+ * @category Community Live Collection
26
+ */
27
+ export const searchMembers = (
28
+ params: Amity.SearchCommunityMemberLiveCollection,
29
+ callback: Amity.LiveCollectionCallback<Amity.Membership<'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(`getMembers(tmpid: ${timestamp}) > listen`);
40
+
41
+ const searchCommunityMemberLiveCollection = new SearchCommunityMembersLiveCollectionController(
42
+ params,
43
+ resp => {
44
+ callback(resp);
45
+ },
46
+ );
47
+ const disposers = searchCommunityMemberLiveCollection.startSubscription();
48
+
49
+ const cacheKey = searchCommunityMemberLiveCollection.getCacheKey();
50
+
51
+ disposers.push(() => {
52
+ dropFromCache(cacheKey);
53
+ });
54
+
55
+ return () => {
56
+ log(`getMembers(tmpid: ${timestamp}) > dispose`);
57
+ disposers.forEach(fn => fn());
58
+ };
59
+ };
60
+ /* end_public_function */
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-use-before-define */
2
1
  import hash from 'object-hash';
3
2
  import { pullFromCache, pushToCache } from '~/cache/api';
4
3
  import { CommunitiesPaginationController } from './CommunitiesPaginationController';
@@ -19,6 +18,12 @@ import {
19
18
  import { prepareCommunityPayload } from '~/communityRepository/utils';
20
19
  import { getActiveClient } from '~/client';
21
20
  import { EnumCommunityActions } from './enums';
21
+ import { EnumCommunityMemberActions } from '~/communityRepository/communityMembership/observers/getMembers/enums';
22
+ import {
23
+ onCommunityJoined,
24
+ onCommunityLeft,
25
+ onCommunityUserChanged,
26
+ } from '~/communityRepository/communityMembership';
22
27
 
23
28
  export class CommunityLiveCollectionController extends LiveCollectionController<
24
29
  'community',
@@ -79,6 +84,9 @@ export class CommunityLiveCollectionController extends LiveCollectionController<
79
84
  { fn: onCommunityCreated, action: EnumCommunityActions.OnCommunityCreated },
80
85
  { fn: onCommunityDeleted, action: EnumCommunityActions.OnCommunityDeleted },
81
86
  { fn: onCommunityUpdated, action: EnumCommunityActions.OnCommunityUpdated },
87
+ { fn: onCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
88
+ { fn: onCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
89
+ { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnMemberCountChanged },
82
90
  ]);
83
91
  }
84
92
 
@@ -3,7 +3,9 @@ import { pullFromCache, pushToCache } from '~/cache/api';
3
3
  import { ingestInCache } from '~/cache/api/ingestInCache';
4
4
  import { getResolver } from '~/core/model';
5
5
  import { getActiveClient } from '~/client';
6
+ import { saveCommunityUsers } from '~/communityRepository/utils/saveCommunityUsers';
6
7
  import { EnumCommunityActions } from './enums';
8
+ import { EnumCommunityMemberActions } from '~/communityRepository/communityMembership/observers/getMembers/enums';
7
9
 
8
10
  export class CommunitiesQueryStreamController extends QueryStreamController<
9
11
  Amity.CommunityPayload,
@@ -32,6 +34,7 @@ export class CommunitiesQueryStreamController extends QueryStreamController<
32
34
 
33
35
  if (client.cache) {
34
36
  ingestInCache(processedPayload, { cachedAt });
37
+ saveCommunityUsers(response.communities, response.communityUsers);
35
38
  }
36
39
  }
37
40
 
@@ -56,7 +59,7 @@ export class CommunitiesQueryStreamController extends QueryStreamController<
56
59
  }
57
60
  }
58
61
 
59
- reactor(action: EnumCommunityActions) {
62
+ reactor(action: EnumCommunityActions | EnumCommunityMemberActions) {
60
63
  return (community: Amity.Community) => {
61
64
  const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(this.cacheKey)?.data;
62
65
  if (!collection) return;
@@ -66,7 +69,7 @@ export class CommunitiesQueryStreamController extends QueryStreamController<
66
69
  }
67
70
 
68
71
  /*
69
- * Simply update collection and let responder decide what to do with data
72
+ * Simply update a collection and let responder decide what to do with data
70
73
  */
71
74
  collection.data = [...new Set([community.communityId, ...collection.data])];
72
75
 
@@ -78,7 +81,7 @@ export class CommunitiesQueryStreamController extends QueryStreamController<
78
81
  subscribeRTE(
79
82
  createSubscriber: {
80
83
  fn: (reactor: (channel: Amity.Community) => void) => Amity.Unsubscriber;
81
- action: EnumCommunityActions;
84
+ action: EnumCommunityActions | EnumCommunityMemberActions;
82
85
  }[],
83
86
  ) {
84
87
  return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));