@amityco/ts-sdk-react-native 6.30.4-48268a5.0 → 6.30.4

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 (78) hide show
  1. package/.env +26 -26
  2. package/dist/@types/core/events.d.ts +2 -0
  3. package/dist/@types/core/events.d.ts.map +1 -1
  4. package/dist/@types/core/payload.d.ts +2 -0
  5. package/dist/@types/core/payload.d.ts.map +1 -1
  6. package/dist/@types/domains/community.d.ts +8 -0
  7. package/dist/@types/domains/community.d.ts.map +1 -1
  8. package/dist/client/api/index.d.ts +2 -0
  9. package/dist/client/api/index.d.ts.map +1 -1
  10. package/dist/communityRepository/communityMembership/events/index.d.ts +2 -0
  11. package/dist/communityRepository/communityMembership/events/index.d.ts.map +1 -1
  12. package/dist/communityRepository/communityMembership/events/onLocalCommunityJoined.d.ts +2 -0
  13. package/dist/communityRepository/communityMembership/events/onLocalCommunityJoined.d.ts.map +1 -0
  14. package/dist/communityRepository/communityMembership/events/onLocalCommunityLeft.d.ts +2 -0
  15. package/dist/communityRepository/communityMembership/events/onLocalCommunityLeft.d.ts.map +1 -0
  16. package/dist/communityRepository/communityMembership/events/utils.d.ts.map +1 -1
  17. package/dist/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.d.ts.map +1 -1
  18. package/dist/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.d.ts.map +1 -1
  19. package/dist/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.d.ts.map +1 -1
  20. package/dist/communityRepository/observers/getCommunity.d.ts.map +1 -1
  21. package/dist/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesLiveCollectionController.d.ts +13 -0
  22. package/dist/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesLiveCollectionController.d.ts.map +1 -0
  23. package/dist/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesPaginationController.d.ts +9 -0
  24. package/dist/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesPaginationController.d.ts.map +1 -0
  25. package/dist/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesQueryStreamController.d.ts +16 -0
  26. package/dist/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesQueryStreamController.d.ts.map +1 -0
  27. package/dist/communityRepository/observers/getRecommendedCommunities/enums.d.ts +5 -0
  28. package/dist/communityRepository/observers/getRecommendedCommunities/enums.d.ts.map +1 -0
  29. package/dist/communityRepository/observers/getRecommendedCommunities.d.ts +1 -1
  30. package/dist/communityRepository/observers/getRecommendedCommunities.d.ts.map +1 -1
  31. package/dist/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesLiveCollectionController.d.ts +13 -0
  32. package/dist/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesLiveCollectionController.d.ts.map +1 -0
  33. package/dist/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesPaginationController.d.ts +9 -0
  34. package/dist/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesPaginationController.d.ts.map +1 -0
  35. package/dist/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesQueryStreamController.d.ts +16 -0
  36. package/dist/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesQueryStreamController.d.ts.map +1 -0
  37. package/dist/communityRepository/observers/getTrendingCommunities/enums.d.ts +5 -0
  38. package/dist/communityRepository/observers/getTrendingCommunities/enums.d.ts.map +1 -0
  39. package/dist/communityRepository/observers/getTrendingCommunities.d.ts +1 -1
  40. package/dist/communityRepository/observers/getTrendingCommunities.d.ts.map +1 -1
  41. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.d.ts.map +1 -1
  42. package/dist/communityRepository/utils/communityWithMembership.d.ts.map +1 -1
  43. package/dist/core/events.d.ts +3 -3
  44. package/dist/core/events.d.ts.map +1 -1
  45. package/dist/index.cjs.js +335 -222
  46. package/dist/index.esm.js +335 -222
  47. package/dist/index.umd.js +3 -3
  48. package/dist/report/api/isReportedByMe.d.ts.map +1 -1
  49. package/package.json +1 -1
  50. package/src/@types/core/events.ts +2 -0
  51. package/src/@types/core/payload.ts +3 -0
  52. package/src/@types/domains/community.ts +6 -0
  53. package/src/client/api/index.ts +3 -0
  54. package/src/communityRepository/api/joinCommunity.ts +1 -1
  55. package/src/communityRepository/api/leaveCommunity.ts +1 -1
  56. package/src/communityRepository/communityMembership/events/index.ts +2 -0
  57. package/src/communityRepository/communityMembership/events/onLocalCommunityJoined.ts +5 -0
  58. package/src/communityRepository/communityMembership/events/onLocalCommunityLeft.ts +5 -0
  59. package/src/communityRepository/communityMembership/events/utils.ts +0 -2
  60. package/src/communityRepository/communityMembership/observers/getMembers/CommunityMembersLiveCollectionController.ts +4 -0
  61. package/src/communityRepository/communityMembership/observers/searchMembers/SearchCommunityMembersLiveCollectionController.ts +4 -0
  62. package/src/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.ts +7 -2
  63. package/src/communityRepository/observers/getCommunity.ts +4 -0
  64. package/src/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesLiveCollectionController.ts +109 -0
  65. package/src/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesPaginationController.ts +31 -0
  66. package/src/communityRepository/observers/getRecommendedCommunities/RecommendedCommunitiesQueryStreamController.ts +88 -0
  67. package/src/communityRepository/observers/getRecommendedCommunities/enums.ts +4 -0
  68. package/src/communityRepository/observers/getRecommendedCommunities.ts +9 -81
  69. package/src/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesLiveCollectionController.ts +109 -0
  70. package/src/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesPaginationController.ts +28 -0
  71. package/src/communityRepository/observers/getTrendingCommunities/TrendingCommunitiesQueryStreamController.ts +86 -0
  72. package/src/communityRepository/observers/getTrendingCommunities/enums.ts +4 -0
  73. package/src/communityRepository/observers/getTrendingCommunities.ts +10 -82
  74. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.ts +7 -2
  75. package/src/communityRepository/utils/communityWithMembership.ts +7 -4
  76. package/src/report/api/createReport.ts +2 -2
  77. package/src/report/api/deleteReport.ts +2 -2
  78. package/src/report/api/isReportedByMe.ts +5 -3
@@ -1 +1 @@
1
- {"version":3,"file":"isReportedByMe.d.ts","sourceRoot":"","sources":["../../../src/report/api/isReportedByMe.ts"],"names":[],"mappings":"AA8DA;;;;;;;;;;;;KAYK;AACL,eAAO,MAAM,cAAc,kBACV,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,eACzC,MAAM,KAClB,QAAQ,OAAO,CAqBjB,CAAC"}
1
+ {"version":3,"file":"isReportedByMe.d.ts","sourceRoot":"","sources":["../../../src/report/api/isReportedByMe.ts"],"names":[],"mappings":"AAgEA;;;;;;;;;;;;KAYK;AACL,eAAO,MAAM,cAAc,kBACV,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,eACzC,MAAM,KAClB,QAAQ,OAAO,CAqBjB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amityco/ts-sdk-react-native",
3
- "version": "6.30.4-48268a5.0",
3
+ "version": "6.30.4",
4
4
  "license": "CC-BY-ND-4.0",
5
5
  "author": "amity.co <developers@amity.co> (https://amity.co)",
6
6
  "description": "Amity Social Cloud Typescript SDK",
@@ -181,6 +181,8 @@ declare global {
181
181
  };
182
182
 
183
183
  type LocalCommunityEvents = {
184
+ 'local.community.joined': Amity.CommunityMembershipPayload;
185
+ 'local.community.left': Amity.CommunityMembershipPayload;
184
186
  'local.community.roleAdded': Amity.ProcessedCommunityPayload;
185
187
  'local.community.roleRemoved': Amity.ProcessedCommunityPayload;
186
188
  'local.community.userAdded': Amity.CommunityMembershipPayload;
@@ -273,6 +273,9 @@ declare global {
273
273
  files: Amity.File[];
274
274
  };
275
275
 
276
+ type RecommendedCommunityPayload = Amity.CommunityPayload;
277
+ type TrendingCommunityPayload = Amity.CommunityPayload;
278
+
276
279
  /**
277
280
  * Items that extend from `Amity.CommunityPayload`
278
281
  * - communities: `Amity.RawCommunity` -> `Amity.Community` (Added Marker Service related props)
@@ -133,6 +133,9 @@ declare global {
133
133
 
134
134
  type CommunityLiveCollection = Amity.LiveCollectionParams<Omit<QueryCommunities, 'page'>>;
135
135
 
136
+ type RecommendedCommunityLiveCollection = Amity.LiveCollectionParams<{ limit?: number }>;
137
+ type TrendingCommunityLiveCollection = Amity.LiveCollectionParams<{ limit?: number }>;
138
+
136
139
  type SearchCommunityLiveCollection = Amity.LiveCollectionParams<
137
140
  Omit<SearchQueryCommunities, 'page'>
138
141
  >;
@@ -142,6 +145,9 @@ declare global {
142
145
  Pick<QueryCommunities, 'page'>
143
146
  >;
144
147
 
148
+ type RecommendedCommunityLiveCollectionCache = Amity.CommunityLiveCollectionCache;
149
+ type TrendingCommunityLiveCollectionCache = Amity.CommunityLiveCollectionCache;
150
+
145
151
  type SearchCommunityLiveCollectionCache = Amity.LiveCollectionCache<
146
152
  Amity.Community['communityId'],
147
153
  Pick<SearchQueryCommunities, 'page'>
@@ -18,3 +18,6 @@ export * from './markerSync';
18
18
  export * from './enableUnreadCount';
19
19
 
20
20
  export * from './setUploadedFileAccessType';
21
+
22
+ export * from './registerPushNotification';
23
+ export * from './unregisterPushNotification';
@@ -31,7 +31,7 @@ export const joinCommunity = async (
31
31
  `/api/v3/communities/${communityId}/join`,
32
32
  );
33
33
 
34
- fireEvent('community.joined', payload);
34
+ fireEvent('local.community.joined', payload);
35
35
 
36
36
  const data = prepareCommunityPayload(payload);
37
37
 
@@ -31,7 +31,7 @@ export const leaveCommunity = async (
31
31
  `/api/v3/communities/${communityId}/leave`,
32
32
  );
33
33
 
34
- fireEvent('community.left', payload);
34
+ fireEvent('local.community.left', payload);
35
35
 
36
36
  const data = prepareCommunityPayload(payload);
37
37
 
@@ -10,3 +10,5 @@ export * from './onLocalCommunityUserRemoved';
10
10
 
11
11
  export * from './onCommunityJoined';
12
12
  export * from './onCommunityLeft';
13
+ export * from './onLocalCommunityJoined';
14
+ export * from './onLocalCommunityLeft';
@@ -0,0 +1,5 @@
1
+ import { createLocalCommunityMemberEventSubscriber } from './utils';
2
+
3
+ export const onLocalCommunityJoined = (
4
+ callback: (community: Amity.Community, member: Amity.Membership<'community'>[]) => void,
5
+ ) => createLocalCommunityMemberEventSubscriber('local.community.joined', callback);
@@ -0,0 +1,5 @@
1
+ import { createLocalCommunityMemberEventSubscriber } from './utils';
2
+
3
+ export const onLocalCommunityLeft = (
4
+ callback: (community: Amity.Community, member: Amity.Membership<'community'>[]) => void,
5
+ ) => createLocalCommunityMemberEventSubscriber('local.community.left', callback);
@@ -35,8 +35,6 @@ function getEventRelatedMember(
35
35
  return payload.communityUsers;
36
36
  }
37
37
 
38
- // NOTE: backend returns the one who took the action and the one on whom
39
- // the action was taken. We need the 2nd one
40
38
  if (event === 'community.userRemoved' || event === 'local.community.userRemoved') {
41
39
  return payload.communityUsers!.filter(x => x.communityMembership === 'none');
42
40
  }
@@ -13,6 +13,8 @@ import {
13
13
  onCommunityUserUnbanned,
14
14
  onLocalCommunityUserAdded,
15
15
  onLocalCommunityUserRemoved,
16
+ onLocalCommunityLeft,
17
+ onLocalCommunityJoined,
16
18
  } from '~/communityRepository/communityMembership/events';
17
19
  import {
18
20
  filterByPropIntersection,
@@ -84,7 +86,9 @@ export class CommunityMembersLiveCollectionController extends LiveCollectionCont
84
86
  startSubscription() {
85
87
  return this.queryStreamController.subscribeRTE([
86
88
  { fn: onCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
89
+ { fn: onLocalCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
87
90
  { fn: onCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
91
+ { fn: onLocalCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
88
92
  { fn: onCommunityUserBanned, action: EnumCommunityMemberActions.OnCommunityUserBanned },
89
93
  { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnCommunityUserChanged },
90
94
  { fn: onCommunityUserRoleAdded, action: EnumCommunityMemberActions.OnCommunityUserRoleAdded },
@@ -11,6 +11,8 @@ import {
11
11
  onCommunityUserChanged,
12
12
  onCommunityUserRoleRemoved,
13
13
  onCommunityUserUnbanned,
14
+ onLocalCommunityLeft,
15
+ onLocalCommunityJoined,
14
16
  } from '~/communityRepository/communityMembership/events';
15
17
  import { filterByPropIntersection, filterBySearchTerm } from '~/core/query';
16
18
  import { prepareCommunityPayload } from '~/communityRepository/utils';
@@ -78,7 +80,9 @@ export class SearchCommunityMembersLiveCollectionController extends LiveCollecti
78
80
  startSubscription() {
79
81
  return this.queryStreamController.subscribeRTE([
80
82
  { fn: onCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
83
+ { fn: onLocalCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
81
84
  { fn: onCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
85
+ { fn: onLocalCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
82
86
  { fn: onCommunityUserBanned, action: EnumCommunityMemberActions.OnCommunityUserBanned },
83
87
  { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnCommunityUserChanged },
84
88
  {
@@ -22,8 +22,11 @@ import { EnumCommunityMemberActions } from '~/communityRepository/communityMembe
22
22
  import {
23
23
  onCommunityJoined,
24
24
  onCommunityLeft,
25
+ onLocalCommunityJoined,
26
+ onLocalCommunityLeft,
25
27
  onCommunityUserChanged,
26
28
  } from '~/communityRepository/communityMembership';
29
+ import { isNonNullable } from '~/utils';
27
30
 
28
31
  export class CommunityLiveCollectionController extends LiveCollectionController<
29
32
  'community',
@@ -87,6 +90,8 @@ export class CommunityLiveCollectionController extends LiveCollectionController<
87
90
  { fn: onCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
88
91
  { fn: onCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
89
92
  { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnMemberCountChanged },
93
+ { fn: onLocalCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
94
+ { fn: onLocalCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
90
95
  ]);
91
96
  }
92
97
 
@@ -96,8 +101,8 @@ export class CommunityLiveCollectionController extends LiveCollectionController<
96
101
 
97
102
  const data = this.applyFilter(
98
103
  collection.data
99
- .map(id => pullFromCache<Amity.Community>(['community', 'get', id])!)
100
- .filter(Boolean)
104
+ .map(id => pullFromCache<Amity.Community>(['community', 'get', id]))
105
+ .filter(isNonNullable)
101
106
  .map(({ data }) => data) ?? [],
102
107
  );
103
108
 
@@ -7,6 +7,8 @@ import {
7
7
  onCommunityUserChanged,
8
8
  onCommunityJoined,
9
9
  onCommunityLeft,
10
+ onLocalCommunityJoined,
11
+ onLocalCommunityLeft,
10
12
  } from '../communityMembership/events';
11
13
 
12
14
  /* begin_public_function
@@ -40,6 +42,8 @@ export const getCommunity = (
40
42
  onCommunityDeleted,
41
43
  onCommunityJoined,
42
44
  onCommunityLeft,
45
+ onLocalCommunityJoined,
46
+ onLocalCommunityLeft,
43
47
  onCommunityUserBanned,
44
48
  onCommunityUserUnbanned,
45
49
  onCommunityUserChanged,
@@ -0,0 +1,109 @@
1
+ import hash from 'object-hash';
2
+ import { pullFromCache, pushToCache } from '~/cache/api';
3
+ import { RecommendedCommunitiesPaginationController } from './RecommendedCommunitiesPaginationController';
4
+ import { RecommendedCommunitiesQueryStreamController } from './RecommendedCommunitiesQueryStreamController';
5
+ import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
6
+ import { prepareCommunityPayload } from '~/communityRepository/utils';
7
+ import { EnumCommunityActions } from './enums';
8
+ import { EnumCommunityMemberActions } from '~/communityRepository/communityMembership/observers/getMembers/enums';
9
+ import { onCommunityDeleted, onCommunityUpdated } from '~/communityRepository/events';
10
+ import {
11
+ onCommunityJoined,
12
+ onCommunityLeft,
13
+ onLocalCommunityJoined,
14
+ onLocalCommunityLeft,
15
+ onCommunityUserChanged,
16
+ } from '~/communityRepository/communityMembership';
17
+ import { isNonNullable } from '~/utils';
18
+
19
+ export class RecommendedCommunityLiveCollectionController extends LiveCollectionController<
20
+ 'community',
21
+ Amity.RecommendedCommunityLiveCollection,
22
+ Amity.Community,
23
+ RecommendedCommunitiesPaginationController
24
+ > {
25
+ private queryStreamController: RecommendedCommunitiesQueryStreamController;
26
+
27
+ private query: Amity.RecommendedCommunityLiveCollection;
28
+
29
+ constructor(
30
+ query: Amity.RecommendedCommunityLiveCollection,
31
+ callback: Amity.LiveCollectionCallback<Amity.Community>,
32
+ ) {
33
+ const queryStreamId = hash(query);
34
+ const cacheKey = ['community', 'collection', queryStreamId];
35
+ const paginationController = new RecommendedCommunitiesPaginationController(query);
36
+
37
+ super(paginationController, queryStreamId, cacheKey, callback);
38
+
39
+ this.query = query;
40
+ this.queryStreamController = new RecommendedCommunitiesQueryStreamController(
41
+ this.query,
42
+ this.cacheKey,
43
+ this.notifyChange.bind(this),
44
+ prepareCommunityPayload,
45
+ );
46
+
47
+ this.callback = callback.bind(this);
48
+ this.loadPage({ initial: true });
49
+ }
50
+
51
+ protected setup() {
52
+ const collection = pullFromCache<Amity.RecommendedCommunityLiveCollectionCache>(
53
+ this.cacheKey,
54
+ )?.data;
55
+ if (!collection) {
56
+ pushToCache(this.cacheKey, {
57
+ data: [],
58
+ params: {},
59
+ });
60
+ }
61
+ }
62
+
63
+ protected async persistModel(queryPayload: Amity.CommunityPayload & Amity.Pagination) {
64
+ await this.queryStreamController.saveToMainDB(queryPayload);
65
+ }
66
+
67
+ protected persistQueryStream({
68
+ response,
69
+ direction,
70
+ refresh,
71
+ }: Amity.LiveCollectionPersistQueryStreamParams<'community'>) {
72
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
73
+ }
74
+
75
+ startSubscription() {
76
+ return this.queryStreamController.subscribeRTE([
77
+ { fn: onCommunityDeleted, action: EnumCommunityActions.OnCommunityDeleted },
78
+ { fn: onCommunityUpdated, action: EnumCommunityActions.OnCommunityUpdated },
79
+ { fn: onCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
80
+ { fn: onCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
81
+ { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnMemberCountChanged },
82
+ { fn: onLocalCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
83
+ { fn: onLocalCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
84
+ ]);
85
+ }
86
+
87
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
88
+ const collection = pullFromCache<Amity.RecommendedCommunityLiveCollectionCache>(
89
+ this.cacheKey,
90
+ )?.data;
91
+ if (!collection) return;
92
+
93
+ const data =
94
+ collection.data
95
+ .map(id => pullFromCache<Amity.Community>(['community', 'get', id]))
96
+ .filter(isNonNullable)
97
+ .map(({ data }) => data) ?? [];
98
+
99
+ if (!this.shouldNotify(data) && origin === 'event') return;
100
+
101
+ this.callback({
102
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
103
+ data,
104
+ hasNextPage: !!this.paginationController.getNextToken(),
105
+ loading,
106
+ error,
107
+ });
108
+ }
109
+ }
@@ -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 RecommendedCommunitiesPaginationController extends PaginationController<
10
+ 'community',
11
+ Amity.RecommendedCommunityLiveCollection
12
+ > {
13
+ async getRequest(
14
+ queryParams: Amity.RecommendedCommunityLiveCollection,
15
+ token: string | undefined,
16
+ ) {
17
+ const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, ...params } = queryParams;
18
+ const options = token ? { token } : { limit };
19
+
20
+ const { data: queryResponse } = await this.http.get<Amity.CommunityPayload & Amity.Pagination>(
21
+ `/api/v3/communities/recommended`,
22
+ {
23
+ params: {
24
+ ...params,
25
+ options,
26
+ },
27
+ },
28
+ );
29
+ return queryResponse;
30
+ }
31
+ }
@@ -0,0 +1,88 @@
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 { saveCommunityUsers } from '~/communityRepository/utils/saveCommunityUsers';
7
+ import { EnumCommunityActions } from './enums';
8
+ import { EnumCommunityMemberActions } from '~/communityRepository/communityMembership/observers/getMembers/enums';
9
+
10
+ export class RecommendedCommunitiesQueryStreamController extends QueryStreamController<
11
+ Amity.RecommendedCommunityPayload,
12
+ Amity.RecommendedCommunityLiveCollection
13
+ > {
14
+ private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
15
+
16
+ private preparePayload: (
17
+ response: Amity.RecommendedCommunityPayload,
18
+ ) => Amity.ProcessedCommunityPayload;
19
+
20
+ constructor(
21
+ query: Amity.RecommendedCommunityLiveCollection,
22
+ cacheKey: string[],
23
+ notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
24
+ preparePayload: (
25
+ response: Amity.RecommendedCommunityPayload,
26
+ ) => Amity.ProcessedCommunityPayload,
27
+ ) {
28
+ super(query, cacheKey);
29
+ this.notifyChange = notifyChange;
30
+ this.preparePayload = preparePayload;
31
+ }
32
+
33
+ async saveToMainDB(response: Amity.RecommendedCommunityPayload) {
34
+ const processedPayload = await this.preparePayload(response);
35
+
36
+ const client = getActiveClient();
37
+ const cachedAt = client.cache && Date.now();
38
+
39
+ if (client.cache) {
40
+ ingestInCache(processedPayload, { cachedAt });
41
+ saveCommunityUsers(response.communities, response.communityUsers);
42
+ }
43
+ }
44
+
45
+ appendToQueryStream(
46
+ response: Amity.RecommendedCommunityPayload & Partial<Amity.Pagination>,
47
+ direction: Amity.LiveCollectionPageDirection,
48
+ refresh = false,
49
+ ) {
50
+ if (refresh) {
51
+ pushToCache(this.cacheKey, {
52
+ data: response.communities.map(getResolver('community')),
53
+ });
54
+ } else {
55
+ const collection = pullFromCache<Amity.RecommendedCommunityLiveCollectionCache>(
56
+ this.cacheKey,
57
+ )?.data;
58
+
59
+ const communities = collection?.data ?? [];
60
+
61
+ pushToCache(this.cacheKey, {
62
+ ...collection,
63
+ data: [...new Set([...communities, ...response.communities.map(getResolver('community'))])],
64
+ });
65
+ }
66
+ }
67
+
68
+ reactor(action: EnumCommunityActions | EnumCommunityMemberActions) {
69
+ return (community: Amity.Community) => {
70
+ const collection = pullFromCache<Amity.RecommendedCommunityLiveCollectionCache>(
71
+ this.cacheKey,
72
+ )?.data;
73
+ if (!collection) return;
74
+
75
+ pushToCache(this.cacheKey, collection);
76
+ this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
77
+ };
78
+ }
79
+
80
+ subscribeRTE(
81
+ createSubscriber: {
82
+ fn: (reactor: (channel: Amity.Community) => void) => Amity.Unsubscriber;
83
+ action: EnumCommunityActions | EnumCommunityMemberActions;
84
+ }[],
85
+ ) {
86
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
87
+ }
88
+ }
@@ -0,0 +1,4 @@
1
+ export enum EnumCommunityActions {
2
+ OnCommunityDeleted = 'onCommunityDeleted',
3
+ OnCommunityUpdated = 'onCommunityUpdated',
4
+ }
@@ -1,18 +1,9 @@
1
- /* eslint-disable no-use-before-define */
2
- import { getResolver } from '~/core/model';
3
1
  import { dropFromCache, pullFromCache, pushToCache } from '~/cache/api';
4
2
  import { getActiveClient } from '~/client/api';
5
- import { createQuery, queryOptions, runQuery } from '~/core/query';
6
- import {
7
- COLLECTION_DEFAULT_CACHING_POLICY,
8
- COLLECTION_DEFAULT_PAGINATION_LIMIT,
9
- ENABLE_CACHE_MESSAGE,
10
- } from '~/utils/constants';
3
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
11
4
 
12
- import { onCommunityUserChanged } from '../communityMembership/events';
13
5
  import { getRecommendedCommunities as _getRecommendedCommunities } from '../api/getRecommendedCommunities';
14
- import { CACHE_SHORTEN_LIFESPAN } from '~/cache/utils';
15
- import { communityQueryFilter } from '../utils/communityQueryFilter';
6
+ import { RecommendedCommunityLiveCollectionController } from './getRecommendedCommunities/RecommendedCommunitiesLiveCollectionController';
16
7
 
17
8
  /* begin_public_function
18
9
  id: community.query.recommended_communities
@@ -36,7 +27,7 @@ import { communityQueryFilter } from '../utils/communityQueryFilter';
36
27
  * @category Community Live Collection
37
28
  */
38
29
  export const getRecommendedCommunities = (
39
- params: Amity.PageLimit,
30
+ params: Amity.RecommendedCommunityLiveCollection,
40
31
  callback: Amity.LiveCollectionCallback<Amity.Community>,
41
32
  config?: Amity.LiveCollectionConfig,
42
33
  ) => {
@@ -49,77 +40,14 @@ export const getRecommendedCommunities = (
49
40
  const timestamp = Date.now();
50
41
  log(`getRecommendedCommunities(tmpid: ${timestamp}) > listen`);
51
42
 
52
- const { limit: queryLimit, ...queryParams } = params;
43
+ const recommendedCommunitiesLiveCollection = new RecommendedCommunityLiveCollectionController(
44
+ params,
45
+ callback,
46
+ );
47
+ const disposers = recommendedCommunitiesLiveCollection.startSubscription();
53
48
 
54
- const limit = queryLimit ?? COLLECTION_DEFAULT_PAGINATION_LIMIT;
55
- const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config ?? {};
49
+ const cacheKey = recommendedCommunitiesLiveCollection.getCacheKey();
56
50
 
57
- const disposers: Amity.Unsubscriber[] = [];
58
- const cacheKey = ['community', 'recommended', { params: { options: { limit } } }];
59
-
60
- const responder = (data: Amity.CommunityLiveCollectionCache, isEventModel = false) => {
61
- const communities: Amity.Community[] =
62
- data.data
63
- .map(communityId => pullFromCache<Amity.Community>(['community', 'get', communityId])!)
64
- .filter(Boolean)
65
- .map(({ data }) => data) ?? [];
66
-
67
- callback({
68
- onNextPage: onFetch,
69
- data: isEventModel ? communityQueryFilter(communities, params, userId!) : communities,
70
- hasNextPage: !!data.params?.page,
71
- loading: data.loading,
72
- error: data.error,
73
- });
74
- };
75
-
76
- const realtimeRouter = (_: Amity.CommunityActionType) => (community: Amity.Community) => {
77
- const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(cacheKey)?.data;
78
- if (!collection) return;
79
-
80
- /*
81
- * Simply update collection and let responder decide what to do with data
82
- */
83
- collection.data = [...new Set([community.communityId, ...collection.data])];
84
-
85
- pushToCache(cacheKey, collection);
86
- responder(collection);
87
- };
88
-
89
- const onFetch = (initial = false) => {
90
- const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(cacheKey)?.data;
91
-
92
- const communities = collection?.data ?? [];
93
-
94
- if (!initial && communities.length > 0 && !collection?.params.page) return;
95
-
96
- const query = createQuery(_getRecommendedCommunities, params);
97
-
98
- runQuery(
99
- query,
100
- ({ data: result, error, loading }) => {
101
- const data = {
102
- loading,
103
- error,
104
- params,
105
- data: communities,
106
- };
107
-
108
- if (result) {
109
- data.data = [...new Set([...communities, ...result.map(getResolver('community'))])];
110
- }
111
-
112
- pushToCache(cacheKey, data);
113
-
114
- responder(data);
115
- },
116
- queryOptions(policy, CACHE_SHORTEN_LIFESPAN),
117
- );
118
- };
119
-
120
- disposers.push(onCommunityUserChanged(realtimeRouter('onMemberCountChanged')));
121
-
122
- onFetch(true);
123
51
  disposers.push(() => dropFromCache(cacheKey));
124
52
 
125
53
  return () => {
@@ -0,0 +1,109 @@
1
+ import hash from 'object-hash';
2
+ import { pullFromCache, pushToCache } from '~/cache/api';
3
+ import { TrendingCommunitiesPaginationController } from './TrendingCommunitiesPaginationController';
4
+ import { TrendingCommunitiesQueryStreamController } from './TrendingCommunitiesQueryStreamController';
5
+ import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
6
+ import { onCommunityDeleted, onCommunityUpdated } from '~/communityRepository/events';
7
+ import { prepareCommunityPayload } from '~/communityRepository/utils';
8
+ import { EnumCommunityActions } from './enums';
9
+ import { EnumCommunityMemberActions } from '~/communityRepository/communityMembership/observers/getMembers/enums';
10
+ import {
11
+ onCommunityJoined,
12
+ onCommunityLeft,
13
+ onLocalCommunityJoined,
14
+ onLocalCommunityLeft,
15
+ onCommunityUserChanged,
16
+ } from '~/communityRepository/communityMembership';
17
+ import { isNonNullable } from '~/utils';
18
+
19
+ export class TrendingCommunityLiveCollectionController extends LiveCollectionController<
20
+ 'community',
21
+ Amity.TrendingCommunityLiveCollection,
22
+ Amity.Community,
23
+ TrendingCommunitiesPaginationController
24
+ > {
25
+ private queryStreamController: TrendingCommunitiesQueryStreamController;
26
+
27
+ private query: Amity.TrendingCommunityLiveCollection;
28
+
29
+ constructor(
30
+ query: Amity.TrendingCommunityLiveCollection,
31
+ callback: Amity.LiveCollectionCallback<Amity.Community>,
32
+ ) {
33
+ const queryStreamId = hash(query);
34
+ const cacheKey = ['trendingCommunity', 'collection', queryStreamId];
35
+ const paginationController = new TrendingCommunitiesPaginationController(query);
36
+
37
+ super(paginationController, queryStreamId, cacheKey, callback);
38
+
39
+ this.query = query;
40
+ this.queryStreamController = new TrendingCommunitiesQueryStreamController(
41
+ this.query,
42
+ this.cacheKey,
43
+ this.notifyChange.bind(this),
44
+ prepareCommunityPayload,
45
+ );
46
+
47
+ this.callback = callback.bind(this);
48
+ this.loadPage({ initial: true });
49
+ }
50
+
51
+ protected setup() {
52
+ const collection = pullFromCache<Amity.TrendingCommunityLiveCollectionCache>(
53
+ this.cacheKey,
54
+ )?.data;
55
+ if (!collection) {
56
+ pushToCache(this.cacheKey, {
57
+ data: [],
58
+ params: {},
59
+ });
60
+ }
61
+ }
62
+
63
+ protected async persistModel(queryPayload: Amity.TrendingCommunityPayload & Amity.Pagination) {
64
+ await this.queryStreamController.saveToMainDB(queryPayload);
65
+ }
66
+
67
+ protected persistQueryStream({
68
+ response,
69
+ direction,
70
+ refresh,
71
+ }: Amity.LiveCollectionPersistQueryStreamParams<'community'>) {
72
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
73
+ }
74
+
75
+ startSubscription() {
76
+ return this.queryStreamController.subscribeRTE([
77
+ { fn: onCommunityDeleted, action: EnumCommunityActions.OnCommunityDeleted },
78
+ { fn: onCommunityUpdated, action: EnumCommunityActions.OnCommunityUpdated },
79
+ { fn: onCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
80
+ { fn: onCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
81
+ { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnMemberCountChanged },
82
+ { fn: onLocalCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
83
+ { fn: onLocalCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
84
+ ]);
85
+ }
86
+
87
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
88
+ const collection = pullFromCache<Amity.TrendingCommunityLiveCollectionCache>(
89
+ this.cacheKey,
90
+ )?.data;
91
+ if (!collection) return;
92
+
93
+ const data =
94
+ collection.data
95
+ .map(id => pullFromCache<Amity.Community>(['community', 'get', id]))
96
+ .filter(isNonNullable)
97
+ .map(({ data }) => data) ?? [];
98
+
99
+ if (!this.shouldNotify(data) && origin === 'event') return;
100
+
101
+ this.callback({
102
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
103
+ data,
104
+ hasNextPage: !!this.paginationController.getNextToken(),
105
+ loading,
106
+ error,
107
+ });
108
+ }
109
+ }