@amityco/ts-sdk 7.4.1-0e170b02.0 → 7.4.1-108e44a8.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 (63) hide show
  1. package/dist/@types/core/events.d.ts +1 -1
  2. package/dist/@types/core/events.d.ts.map +1 -1
  3. package/dist/@types/domains/community.d.ts +12 -1
  4. package/dist/@types/domains/community.d.ts.map +1 -1
  5. package/dist/@types/domains/joinRequest.d.ts +7 -7
  6. package/dist/@types/domains/joinRequest.d.ts.map +1 -1
  7. package/dist/@types/domains/notification.d.ts +4 -2
  8. package/dist/@types/domains/notification.d.ts.map +1 -1
  9. package/dist/communityRepository/api/getCommunities.d.ts.map +1 -1
  10. package/dist/communityRepository/communityMembership/events/index.d.ts +1 -0
  11. package/dist/communityRepository/communityMembership/events/index.d.ts.map +1 -1
  12. package/dist/communityRepository/communityMembership/events/onLocalCommunityJoin.d.ts +2 -0
  13. package/dist/communityRepository/communityMembership/events/onLocalCommunityJoin.d.ts.map +1 -0
  14. package/dist/communityRepository/internalAPI/joinCommunity.d.ts.map +1 -1
  15. package/dist/communityRepository/joinRequest/internalAPI/approveJoinRequest.d.ts.map +1 -1
  16. package/dist/communityRepository/joinRequest/internalAPI/cancelJoinRequest.d.ts.map +1 -1
  17. package/dist/communityRepository/joinRequest/internalAPI/rejectJoinRequest.d.ts.map +1 -1
  18. package/dist/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.d.ts.map +1 -1
  19. package/dist/communityRepository/observers/getCommunity.d.ts.map +1 -1
  20. package/dist/communityRepository/observers/getJoinRequestList/JoinRequestListLiveCollectionController.d.ts +14 -0
  21. package/dist/communityRepository/observers/getJoinRequestList/JoinRequestListLiveCollectionController.d.ts.map +1 -0
  22. package/dist/communityRepository/observers/getJoinRequestList/JoinRequestListPaginationController.d.ts +9 -0
  23. package/dist/communityRepository/observers/getJoinRequestList/JoinRequestListPaginationController.d.ts.map +1 -0
  24. package/dist/communityRepository/observers/getJoinRequestList/JoinRequestListQueryStreamController.d.ts +15 -0
  25. package/dist/communityRepository/observers/getJoinRequestList/JoinRequestListQueryStreamController.d.ts.map +1 -0
  26. package/dist/communityRepository/observers/getJoinRequestList/enum.d.ts +6 -0
  27. package/dist/communityRepository/observers/getJoinRequestList/enum.d.ts.map +1 -0
  28. package/dist/communityRepository/observers/getJoinRequestList.d.ts +12 -0
  29. package/dist/communityRepository/observers/getJoinRequestList.d.ts.map +1 -0
  30. package/dist/communityRepository/observers/index.d.ts +1 -0
  31. package/dist/communityRepository/observers/index.d.ts.map +1 -1
  32. package/dist/core/events.d.ts +3 -3
  33. package/dist/core/events.d.ts.map +1 -1
  34. package/dist/core/model/index.d.ts.map +1 -1
  35. package/dist/index.cjs.js +254 -36
  36. package/dist/index.esm.js +254 -36
  37. package/dist/index.umd.js +2 -2
  38. package/dist/utils/linkedObject/communityLinkedObject.d.ts.map +1 -1
  39. package/dist/utils/linkedObject/joinRequestLinkedObject.d.ts.map +1 -1
  40. package/dist/utils/tests/dummy/comment.d.ts +1 -1
  41. package/package.json +1 -1
  42. package/src/@types/core/events.ts +2 -2
  43. package/src/@types/domains/community.ts +18 -1
  44. package/src/@types/domains/joinRequest.ts +7 -7
  45. package/src/@types/domains/notification.ts +2 -0
  46. package/src/communityRepository/api/getCommunities.ts +3 -1
  47. package/src/communityRepository/communityMembership/events/index.ts +1 -0
  48. package/src/communityRepository/communityMembership/events/onLocalCommunityJoin.ts +20 -0
  49. package/src/communityRepository/internalAPI/joinCommunity.ts +21 -4
  50. package/src/communityRepository/joinRequest/internalAPI/approveJoinRequest.ts +1 -5
  51. package/src/communityRepository/joinRequest/internalAPI/cancelJoinRequest.ts +0 -5
  52. package/src/communityRepository/joinRequest/internalAPI/rejectJoinRequest.ts +3 -0
  53. package/src/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.ts +6 -0
  54. package/src/communityRepository/observers/getCommunity.ts +3 -0
  55. package/src/communityRepository/observers/getJoinRequestList/JoinRequestListLiveCollectionController.ts +130 -0
  56. package/src/communityRepository/observers/getJoinRequestList/JoinRequestListPaginationController.ts +30 -0
  57. package/src/communityRepository/observers/getJoinRequestList/JoinRequestListQueryStreamController.ts +112 -0
  58. package/src/communityRepository/observers/getJoinRequestList/enum.ts +5 -0
  59. package/src/communityRepository/observers/getJoinRequestList.ts +51 -0
  60. package/src/communityRepository/observers/index.ts +1 -0
  61. package/src/core/model/index.ts +1 -0
  62. package/src/utils/linkedObject/communityLinkedObject.ts +1 -3
  63. package/src/utils/linkedObject/joinRequestLinkedObject.ts +11 -13
@@ -1 +1 @@
1
- {"version":3,"file":"communityLinkedObject.d.ts","sourceRoot":"","sources":["../../../src/utils/linkedObject/communityLinkedObject.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,qBAAqB,cAAe,MAAM,iBAAiB,KAAG,MAAM,SAiDhF,CAAC"}
1
+ {"version":3,"file":"communityLinkedObject.d.ts","sourceRoot":"","sources":["../../../src/utils/linkedObject/communityLinkedObject.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,qBAAqB,cAAe,MAAM,iBAAiB,KAAG,MAAM,SA+ChF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"joinRequestLinkedObject.d.ts","sourceRoot":"","sources":["../../../src/utils/linkedObject/joinRequestLinkedObject.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,uBAAuB,gBACrB,MAAM,mBAAmB,KACrC,MAAM,WAkBR,CAAC"}
1
+ {"version":3,"file":"joinRequestLinkedObject.d.ts","sourceRoot":"","sources":["../../../src/utils/linkedObject/joinRequestLinkedObject.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,uBAAuB,gBACrB,MAAM,mBAAmB,KACrC,MAAM,WAgBR,CAAC"}
@@ -23,7 +23,7 @@ export declare const generateComment: (params?: Partial<Amity.InternalComment>)
23
23
  referenceType: Amity.CommentReferenceType;
24
24
  dataType?: any;
25
25
  dataTypes?: any[] | undefined;
26
- data?: string | Amity.ContentDataText | Amity.ContentDataFile | Amity.ContentDataImage | Amity.ContentDataVideo | Amity.ContentDataPoll | Record<string, unknown> | undefined;
26
+ data?: string | Record<string, unknown> | Amity.ContentDataText | Amity.ContentDataFile | Amity.ContentDataImage | Amity.ContentDataVideo | Amity.ContentDataPoll | undefined;
27
27
  metadata?: Record<string, any> | undefined;
28
28
  flagCount: number;
29
29
  hashFlag: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amityco/ts-sdk",
3
- "version": "7.4.1-0e170b02.0",
3
+ "version": "7.4.1-108e44a8.0",
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",
@@ -188,7 +188,7 @@ declare global {
188
188
  };
189
189
 
190
190
  type LocalCommunityEvents = {
191
- 'local.community.joined': Amity.CommunityMembershipPayload; // v3 join community
191
+ 'local.community.joined': Amity.CommunityMembershipPayload;
192
192
  'local.community.left': Amity.CommunityMembershipPayload;
193
193
  'local.community.roleAdded': Amity.ProcessedCommunityPayload;
194
194
  'local.community.roleRemoved': Amity.ProcessedCommunityPayload;
@@ -265,7 +265,7 @@ declare global {
265
265
  'local.invitation.updated': Amity.InternalInvitation[];
266
266
  'local.invitation.deleted': Amity.InternalInvitation[];
267
267
 
268
- 'local.community.join': Amity.InternalJoinRequest[]; // v4 join community with status
268
+ 'v4.local.community.joined': Amity.InternalJoinRequest[];
269
269
 
270
270
  'local.joinRequest.created': Amity.InternalJoinRequest[];
271
271
  'local.joinRequest.updated': Amity.InternalJoinRequest[];
@@ -159,6 +159,16 @@ declare global {
159
159
  page?: string;
160
160
  };
161
161
 
162
+ type QueryJoinRequestList = {
163
+ communityIds: string[];
164
+ sortBy?: Amity.CommunitySortBy | Amity.CommunitySortByEnum;
165
+ options?: {
166
+ limit?: number;
167
+ token?: string;
168
+ };
169
+ page?: string;
170
+ };
171
+
162
172
  type CommunityLiveCollection = Amity.LiveCollectionParams<Omit<QueryCommunities, 'page'>>;
163
173
 
164
174
  type RecommendedCommunityLiveCollection = Amity.LiveCollectionParams<{ limit?: number }>;
@@ -166,6 +176,8 @@ declare global {
166
176
 
167
177
  type JoinRequestLiveCollection = Amity.LiveCollectionParams<QueryJoinRequest>;
168
178
 
179
+ type JoinRequestListLiveCollection = Amity.LiveCollectionParams<QueryJoinRequestList>;
180
+
169
181
  type SearchCommunityLiveCollection = Amity.LiveCollectionParams<
170
182
  Omit<SearchQueryCommunities, 'page'>
171
183
  >;
@@ -188,6 +200,11 @@ declare global {
188
200
  Pick<QueryJoinRequest, 'page'>
189
201
  >;
190
202
 
203
+ type JoinRequestListLiveCollectionCache = Amity.LiveCollectionCache<
204
+ Amity.JoinRequest['joinRequestId'],
205
+ Pick<QueryJoinRequestList, 'page'>
206
+ >;
207
+
191
208
  type QueryCommunityMembers = {
192
209
  communityId: string;
193
210
  memberships?: ('banned' | 'member')[];
@@ -257,7 +274,7 @@ declare global {
257
274
  callback: Amity.LiveCollectionCallback<Amity.Invitation>,
258
275
  ) => Amity.Unsubscriber;
259
276
  getInvitation: () => Promise<Amity.Invitation | undefined>;
260
- join: () => Promise<Amity.JoinResult>;
277
+ join: Promise<Amity.JoinResult>;
261
278
  getJoinRequests: (
262
279
  params: QueryJoinRequest,
263
280
  callback: Amity.LiveCollectionCallback<Amity.JoinRequest>,
@@ -35,10 +35,10 @@ declare global {
35
35
  requestorInternalId: string;
36
36
  requestorPublicId: string;
37
37
 
38
- respondedAt: Amity.timestamp;
39
- responderId: string;
40
- responderInternalId: string;
41
- responderPublicId: string;
38
+ respondedAt: Amity.timestamp | null;
39
+ responderId: string | null;
40
+ responderInternalId: string | null;
41
+ responderPublicId: string | null;
42
42
 
43
43
  status: Amity.JoinRequestStatus;
44
44
  targetId: string;
@@ -58,9 +58,9 @@ declare global {
58
58
  };
59
59
 
60
60
  type JoinRequestLinkedObject = {
61
- cancel: () => Promise<void>;
62
- approve: () => Promise<void>;
63
- reject: () => Promise<void>;
61
+ cancel: () => void;
62
+ approve: () => void;
63
+ reject: () => void;
64
64
  };
65
65
  }
66
66
  }
@@ -10,6 +10,7 @@ declare global {
10
10
  POST_REACT = 'reaction_on_post',
11
11
  COMMENT_REACT = 'reaction_on_comment',
12
12
  REPLY_REACT = 'reaction_on_reply',
13
+ RESPONSE_JOIN_REQUEST = 'respond_on_join_request',
13
14
  }
14
15
 
15
16
  const enum TrayAction {
@@ -19,6 +20,7 @@ declare global {
19
20
  REACTION = 'reaction',
20
21
  MENTION = 'mention',
21
22
  REPLY = 'reply',
23
+ JOIN_REQUEST = 'join_request',
22
24
  }
23
25
 
24
26
  type RawNotificationTrayItem = {
@@ -28,12 +28,14 @@ export const getCommunities = async (
28
28
  const client = getActiveClient();
29
29
  client.log('community/getCommunities', communityIds);
30
30
 
31
+ const encodedCommunityIds = communityIds.map(communityId => encodeURIComponent(communityId));
32
+
31
33
  // API-FIX: endpoint should not be /list, parameters should be querystring.
32
34
  const { data: payload } = await client.http.get<Amity.CommunityPayload>(
33
35
  `/api/v3/communities/list`,
34
36
  {
35
37
  params: {
36
- communityIds,
38
+ communityIds: encodedCommunityIds,
37
39
  includeDiscoverablePrivateCommunity: includeDiscoverablePrivateCommunity ?? true,
38
40
  },
39
41
  },
@@ -12,3 +12,4 @@ export * from './onCommunityJoined';
12
12
  export * from './onCommunityLeft';
13
13
  export * from './onLocalCommunityJoined';
14
14
  export * from './onLocalCommunityLeft';
15
+ export * from './onLocalCommunityJoin';
@@ -0,0 +1,20 @@
1
+ import { getActiveClient } from '~/client/api/activeClient';
2
+ import { createEventSubscriber } from '~/core/events';
3
+
4
+ export const onLocalCommunityJoin = (
5
+ callback: (joinRequest: Amity.InternalJoinRequest) => void,
6
+ ) => {
7
+ const client = getActiveClient();
8
+
9
+ const filter = async (rawPayload: Amity.InternalJoinRequest[]) => {
10
+ callback(rawPayload[0]);
11
+ };
12
+
13
+ const disposers = [
14
+ createEventSubscriber(client, 'onCommunityUpdate', 'v4.local.community.joined', filter),
15
+ ];
16
+
17
+ return () => {
18
+ disposers.forEach(fn => fn());
19
+ };
20
+ };
@@ -1,8 +1,10 @@
1
1
  import { getActiveClient } from '~/client/api/activeClient';
2
2
  import { ingestInCache } from '~/cache/api/ingestInCache';
3
+ import { pullFromCache } from '~/cache/api/pullFromCache';
4
+ import { upsertInCache } from '~/cache/api/upsertInCache';
3
5
  import { prepareCommunityJoinRequestPayload } from '~/communityRepository/utils';
4
6
  import { fireEvent } from '~/core/events';
5
- import { JoinResultStatusEnum } from '~/@types/domains/joinRequest';
7
+ import { JoinRequestStatusEnum, JoinResultStatusEnum } from '~/@types/domains/joinRequest';
6
8
  import { joinRequestLinkedObject } from '~/utils/linkedObject/joinRequestLinkedObject';
7
9
 
8
10
  /* begin_public_function
@@ -37,10 +39,25 @@ export const joinRequest = async (
37
39
  const cachedAt = client.cache && Date.now();
38
40
  if (client.cache) ingestInCache(data, { cachedAt });
39
41
 
40
- fireEvent('local.community.join', data.joinRequests);
41
-
42
42
  const status =
43
- data.joinRequests.length === 0 ? JoinResultStatusEnum.Success : JoinResultStatusEnum.Pending;
43
+ data.joinRequests[0].status === JoinRequestStatusEnum.Approved
44
+ ? JoinResultStatusEnum.Success
45
+ : JoinResultStatusEnum.Pending;
46
+
47
+ if (status === JoinResultStatusEnum.Success && client.cache) {
48
+ const community = pullFromCache<Amity.Community>(['community', 'get', communityId])?.data;
49
+
50
+ if (community) {
51
+ const updatedCommunity = {
52
+ ...community,
53
+ isJoined: true,
54
+ };
55
+
56
+ upsertInCache(['community', 'get', communityId], updatedCommunity);
57
+ }
58
+ }
59
+
60
+ fireEvent('v4.local.community.joined', data.joinRequests);
44
61
 
45
62
  return status === JoinResultStatusEnum.Success
46
63
  ? { status }
@@ -29,14 +29,10 @@ export const approveJoinRequest = async (
29
29
  const { data } = await client.http.post<{ success: boolean }>(
30
30
  `/api/v4/communities/${joinRequest.targetId}/join/approve`,
31
31
  {
32
- params: {
33
- userId: joinRequest.userId,
34
- },
32
+ userId: joinRequest.requestorInternalId,
35
33
  },
36
34
  );
37
35
 
38
- // TODO : Check cache id
39
-
40
36
  const joinRequestCache = pullFromCache<Amity.InternalJoinRequest>([
41
37
  'joinRequest',
42
38
  'get',
@@ -27,11 +27,6 @@ export const cancelJoinRequest = async (
27
27
 
28
28
  const { data } = await client.http.delete<{ success: boolean }>(
29
29
  `/api/v4/communities/${joinRequest.targetId}/join`,
30
- {
31
- params: {
32
- userId: joinRequest.userId,
33
- },
34
- },
35
30
  );
36
31
 
37
32
  const joinRequestCache = pullFromCache<Amity.InternalJoinRequest>([
@@ -28,6 +28,9 @@ export const rejectJoinRequest = async (
28
28
 
29
29
  const { data } = await client.http.post<{ success: boolean }>(
30
30
  `/api/v4/communities/${joinRequest.targetId}/join/reject`,
31
+ {
32
+ userId: joinRequest.requestorInternalId,
33
+ },
31
34
  );
32
35
 
33
36
  const joinRequestCache = pullFromCache<Amity.InternalJoinRequest>([
@@ -25,8 +25,10 @@ import {
25
25
  onLocalCommunityJoined,
26
26
  onLocalCommunityLeft,
27
27
  onCommunityUserChanged,
28
+ onLocalCommunityJoin,
28
29
  } from '~/communityRepository/communityMembership';
29
30
  import { isNonNullable } from '~/utils';
31
+ import { convertEventPayload } from '~/utils/event';
30
32
 
31
33
  export class CommunityLiveCollectionController extends LiveCollectionController<
32
34
  'community',
@@ -92,6 +94,10 @@ export class CommunityLiveCollectionController extends LiveCollectionController<
92
94
  { fn: onCommunityUserChanged, action: EnumCommunityMemberActions.OnMemberCountChanged },
93
95
  { fn: onLocalCommunityJoined, action: EnumCommunityMemberActions.OnCommunityJoined },
94
96
  { fn: onLocalCommunityLeft, action: EnumCommunityMemberActions.OnCommunityLeft },
97
+ {
98
+ fn: convertEventPayload(onLocalCommunityJoin, 'targetId', 'community'),
99
+ action: EnumCommunityActions.OnCommunityUpdated,
100
+ },
95
101
  ]);
96
102
  }
97
103
 
@@ -9,7 +9,9 @@ import {
9
9
  onCommunityLeft,
10
10
  onLocalCommunityJoined,
11
11
  onLocalCommunityLeft,
12
+ onLocalCommunityJoin,
12
13
  } from '../communityMembership/events';
14
+ import { convertEventPayload } from '~/utils/event';
13
15
 
14
16
  /* begin_public_function
15
17
  id: community.get
@@ -47,6 +49,7 @@ export const getCommunity = (
47
49
  onCommunityUserBanned,
48
50
  onCommunityUserUnbanned,
49
51
  onCommunityUserChanged,
52
+ convertEventPayload(onLocalCommunityJoin, 'targetId', 'community'),
50
53
  ]);
51
54
  };
52
55
  /* end_public_function */
@@ -0,0 +1,130 @@
1
+ import hash from 'object-hash';
2
+ import { pullFromCache, pushToCache } from '~/cache/api';
3
+ import { JoinRequestListPaginationController } from './JoinRequestListPaginationController';
4
+ import { JoinRequestListQueryStreamController } from './JoinRequestListQueryStreamController';
5
+ import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
6
+ import { sortByFirstCreated, sortByLastCreated } from '~/core/query';
7
+ import { prepareCommunityJoinRequestPayload } from '~/communityRepository/utils';
8
+ import { EnumJoinRequestAction } from './enum';
9
+ import { isNonNullable } from '~/utils';
10
+ import { joinRequestLinkedObject } from '~/utils/linkedObject/joinRequestLinkedObject';
11
+ import {
12
+ onJoinRequestCreated,
13
+ onJoinRequestDeleted,
14
+ onJoinRequestUpdated,
15
+ } from '~/communityRepository/joinRequest/events/';
16
+
17
+ export class JoinRequestListLiveCollectionController extends LiveCollectionController<
18
+ 'joinRequest',
19
+ Amity.JoinRequestListLiveCollection,
20
+ Amity.JoinRequest,
21
+ JoinRequestListPaginationController
22
+ > {
23
+ private queryStreamController: JoinRequestListQueryStreamController;
24
+
25
+ private query: Amity.JoinRequestListLiveCollection;
26
+
27
+ constructor(
28
+ query: Amity.JoinRequestListLiveCollection,
29
+ callback: Amity.LiveCollectionCallback<Amity.JoinRequest>,
30
+ ) {
31
+ const queryStreamId = hash(query);
32
+ const cacheKey = ['joinRequestList', 'collection', queryStreamId];
33
+ const paginationController = new JoinRequestListPaginationController(query);
34
+
35
+ super(paginationController, queryStreamId, cacheKey, callback);
36
+
37
+ this.query = query;
38
+ this.queryStreamController = new JoinRequestListQueryStreamController(
39
+ this.query,
40
+ this.cacheKey,
41
+ this.notifyChange.bind(this),
42
+ prepareCommunityJoinRequestPayload,
43
+ );
44
+
45
+ this.callback = callback.bind(this);
46
+ this.loadPage({ initial: true });
47
+ }
48
+
49
+ protected setup() {
50
+ const collection = pullFromCache<Amity.JoinRequestListLiveCollectionCache>(this.cacheKey)?.data;
51
+
52
+ if (!collection) {
53
+ pushToCache(this.cacheKey, {
54
+ data: [],
55
+ params: this.query,
56
+ });
57
+ }
58
+ }
59
+
60
+ protected async persistModel(queryPayload: Amity.CommunityJoinRequestPayload & Amity.Pagination) {
61
+ await this.queryStreamController.saveToMainDB(queryPayload);
62
+ }
63
+
64
+ protected persistQueryStream({
65
+ response,
66
+ direction,
67
+ refresh,
68
+ }: Amity.LiveCollectionPersistQueryStreamParams<'joinRequest'>) {
69
+ const joinRequestResponse = response as unknown as Amity.CommunityJoinRequestPayload &
70
+ Partial<Amity.Pagination>;
71
+ this.queryStreamController.appendToQueryStream(joinRequestResponse, direction, refresh);
72
+ }
73
+
74
+ startSubscription() {
75
+ return this.queryStreamController.subscribeRTE([
76
+ { fn: onJoinRequestCreated, action: EnumJoinRequestAction.OnLocalJoinRequestCreated },
77
+ { fn: onJoinRequestUpdated, action: EnumJoinRequestAction.OnLocalJoinRequestUpdated },
78
+ { fn: onJoinRequestDeleted, action: EnumJoinRequestAction.OnLocalJoinRequestDeleted },
79
+ ]);
80
+ }
81
+
82
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
83
+ const collection = pullFromCache<Amity.JoinRequestListLiveCollectionCache>(this.cacheKey)?.data;
84
+
85
+ if (!collection) return;
86
+
87
+ const data = this.applyFilter(
88
+ collection.data
89
+ .map(id => pullFromCache<Amity.InternalJoinRequest>(['joinRequest', 'get', id])!)
90
+ .filter(isNonNullable)
91
+ .map(({ data }) => data)
92
+ .map(joinRequestLinkedObject),
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.JoinRequest[]) {
107
+ let joinRequest = data;
108
+
109
+ if (this.query.communityIds && this.query.communityIds.length > 0) {
110
+ joinRequest = joinRequest.filter(joinRequest =>
111
+ this.query.communityIds.some(id => id === joinRequest.targetId),
112
+ );
113
+ }
114
+
115
+ const sortFn = (() => {
116
+ switch (this.query.sortBy) {
117
+ case 'firstCreated':
118
+ return sortByFirstCreated;
119
+ case 'lastCreated':
120
+ return sortByLastCreated;
121
+ default:
122
+ return sortByLastCreated;
123
+ }
124
+ })();
125
+
126
+ joinRequest = joinRequest.sort(sortFn);
127
+
128
+ return joinRequest;
129
+ }
130
+ }
@@ -0,0 +1,30 @@
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 JoinRequestListPaginationController extends PaginationController<
9
+ 'joinRequest',
10
+ Amity.JoinRequestListLiveCollection
11
+ > {
12
+ async getRequest(queryParams: Amity.JoinRequestListLiveCollection, token: string | undefined) {
13
+ const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, communityIds, ...params } = queryParams;
14
+ const options = token ? { token } : { limit };
15
+
16
+ const encodedChannelIds = communityIds.map(communityId => encodeURIComponent(communityId));
17
+
18
+ const { data: queryResponse } = await this.http.get<
19
+ Amity.CommunityJoinRequestPayload & Amity.Pagination
20
+ >(`/api/v4/communities/join/requests`, {
21
+ params: {
22
+ ...params,
23
+ options,
24
+ communityIds: encodedChannelIds,
25
+ },
26
+ });
27
+
28
+ return queryResponse;
29
+ }
30
+ }
@@ -0,0 +1,112 @@
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/api/activeClient';
6
+ import { EnumJoinRequestAction } from './enum';
7
+
8
+ export class JoinRequestListQueryStreamController extends QueryStreamController<
9
+ Amity.CommunityJoinRequestPayload,
10
+ Amity.JoinRequestListLiveCollection
11
+ > {
12
+ private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
13
+
14
+ private preparePayload: (
15
+ response: Amity.CommunityJoinRequestPayload,
16
+ ) => Amity.ProcessedJoinRequestPayload;
17
+
18
+ constructor(
19
+ query: Amity.JoinRequestListLiveCollection,
20
+ cacheKey: string[],
21
+ notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
22
+ preparePayload: (
23
+ response: Amity.CommunityJoinRequestPayload,
24
+ ) => Amity.ProcessedJoinRequestPayload,
25
+ ) {
26
+ super(query, cacheKey);
27
+ this.notifyChange = notifyChange;
28
+ this.preparePayload = preparePayload;
29
+ }
30
+
31
+ async saveToMainDB(response: Amity.CommunityJoinRequestPayload) {
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.CommunityJoinRequestPayload & Partial<Amity.Pagination>,
44
+ direction: Amity.LiveCollectionPageDirection,
45
+ refresh = false,
46
+ ) {
47
+ if (refresh) {
48
+ pushToCache(this.cacheKey, {
49
+ data: response.joinRequests.map(joinRequest =>
50
+ getResolver('joinRequest')({ joinRequestId: joinRequest._id }),
51
+ ),
52
+ });
53
+ } else {
54
+ const collection = pullFromCache<Amity.JoinRequestListLiveCollectionCache>(
55
+ this.cacheKey,
56
+ )?.data;
57
+
58
+ const joinRequests = collection?.data ?? [];
59
+
60
+ pushToCache(this.cacheKey, {
61
+ ...collection,
62
+ data: [
63
+ ...new Set([
64
+ ...joinRequests,
65
+ ...response.joinRequests.map(joinRequest =>
66
+ getResolver('joinRequest')({ joinRequestId: joinRequest._id }),
67
+ ),
68
+ ]),
69
+ ],
70
+ });
71
+ }
72
+ }
73
+
74
+ reactor(action: EnumJoinRequestAction) {
75
+ return (joinRequest: Amity.InternalJoinRequest[]) => {
76
+ const collection = pullFromCache<Amity.JoinRequestListLiveCollectionCache>(
77
+ this.cacheKey,
78
+ )?.data;
79
+ if (!collection) return;
80
+
81
+ if (action === EnumJoinRequestAction.OnLocalJoinRequestUpdated) {
82
+ const isExist = collection.data.find(id => id === joinRequest[0].joinRequestId);
83
+ if (!isExist) return;
84
+ }
85
+
86
+ if (action === EnumJoinRequestAction.OnLocalJoinRequestCreated) {
87
+ collection.data = [
88
+ ...new Set([
89
+ ...joinRequest.map(joinRequest => joinRequest.joinRequestId),
90
+ ...collection.data,
91
+ ]),
92
+ ];
93
+ }
94
+
95
+ if (action === EnumJoinRequestAction.OnLocalJoinRequestDeleted) {
96
+ collection.data = collection.data.filter(id => id !== joinRequest[0].joinRequestId);
97
+ }
98
+
99
+ pushToCache(this.cacheKey, collection);
100
+ this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
101
+ };
102
+ }
103
+
104
+ subscribeRTE(
105
+ createSubscriber: {
106
+ fn: (reactor: Amity.Listener<Amity.InternalJoinRequest[]>) => Amity.Unsubscriber;
107
+ action: EnumJoinRequestAction;
108
+ }[],
109
+ ) {
110
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
111
+ }
112
+ }
@@ -0,0 +1,5 @@
1
+ export enum EnumJoinRequestAction {
2
+ OnLocalJoinRequestCreated = 'OnLocalJoinRequestCreated',
3
+ OnLocalJoinRequestUpdated = 'OnLocalJoinRequestUpdated',
4
+ OnLocalJoinRequestDeleted = 'OnLocalJoinRequestDeleted',
5
+ }
@@ -0,0 +1,51 @@
1
+ import { dropFromCache } from '~/cache/api/dropFromCache';
2
+ import { getActiveClient } from '~/client/api/activeClient';
3
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
4
+ import { JoinRequestListLiveCollectionController } from './getJoinRequestList/JoinRequestListLiveCollectionController';
5
+
6
+ /* begin_public_function
7
+ id: community.getJoinRequestList
8
+ */
9
+ /**
10
+ * Get Join Requests
11
+ *
12
+ * @param params the query parameters
13
+ * @param callback the callback to be called when the join request are updated
14
+ * @returns joinRequest[]
15
+ *
16
+ * @category joinRequestList Live Collection
17
+ *
18
+ */
19
+ export const getJoinRequestList = (
20
+ params: Amity.JoinRequestListLiveCollection,
21
+ callback: Amity.LiveCollectionCallback<Amity.JoinRequest>,
22
+ config?: Amity.LiveCollectionConfig,
23
+ ) => {
24
+ const { log, cache } = getActiveClient();
25
+
26
+ if (!cache) {
27
+ console.log(ENABLE_CACHE_MESSAGE);
28
+ }
29
+
30
+ const timestamp = Date.now();
31
+ log(`getJoinRequestList: (tmpid: ${timestamp}) > listen`);
32
+
33
+ const joinRequestListLiveCollection = new JoinRequestListLiveCollectionController(
34
+ params,
35
+ callback,
36
+ );
37
+
38
+ const disposers = joinRequestListLiveCollection.startSubscription();
39
+
40
+ const cacheKey = joinRequestListLiveCollection.getCacheKey();
41
+
42
+ disposers.push(() => {
43
+ dropFromCache(cacheKey);
44
+ });
45
+
46
+ return () => {
47
+ log(`getJoinRequestList (tmpid: ${timestamp}) > dispose`);
48
+ disposers.forEach(fn => fn());
49
+ };
50
+ };
51
+ /* end_public_function */
@@ -5,3 +5,4 @@ export * from './getTrendingCommunities';
5
5
  export * from './getRecommendedCommunities';
6
6
 
7
7
  export * from './semanticSearchCommunities';
8
+ export * from './getJoinRequestList';
@@ -56,6 +56,7 @@ export const PAYLOAD2MODEL: Record<string, Amity.Domain> = {
56
56
  notificationTrayItems: 'notificationTrayItem',
57
57
 
58
58
  invitations: 'invitation',
59
+ joinRequests: 'joinRequest',
59
60
  };
60
61
 
61
62
  /** hidden */
@@ -35,9 +35,7 @@ export const communityLinkedObject = (community: Amity.InternalCommunity): Amity
35
35
  const { data } = await getInvitation('community', community.communityId);
36
36
  return data;
37
37
  },
38
- join: async (): Promise<Amity.JoinResult> => {
39
- return joinRequest(community.communityId);
40
- },
38
+ join: joinRequest(community.communityId),
41
39
  getJoinRequests: (
42
40
  params: Omit<Amity.QueryJoinRequest, 'communityId'>,
43
41
  callback: Amity.LiveCollectionCallback<Amity.JoinRequest>,