@amityco/ts-sdk 7.4.1-d23fab2f.0 → 7.4.1-e8631ca7.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.
- package/dist/@types/core/events.d.ts +3 -3
- package/dist/@types/core/events.d.ts.map +1 -1
- package/dist/@types/core/payload.d.ts +14 -0
- package/dist/@types/core/payload.d.ts.map +1 -1
- package/dist/@types/domains/community.d.ts +4 -0
- package/dist/@types/domains/community.d.ts.map +1 -1
- package/dist/@types/domains/invitation.d.ts +48 -3
- package/dist/@types/domains/invitation.d.ts.map +1 -1
- package/dist/communityRepository/api/getCommunities.d.ts +1 -1
- package/dist/communityRepository/api/getCommunities.d.ts.map +1 -1
- package/dist/communityRepository/api/getRecommendedCommunities.d.ts +3 -1
- package/dist/communityRepository/api/getRecommendedCommunities.d.ts.map +1 -1
- package/dist/communityRepository/api/getTrendingCommunities.d.ts +3 -1
- package/dist/communityRepository/api/getTrendingCommunities.d.ts.map +1 -1
- package/dist/communityRepository/api/updateCommunity.d.ts +2 -2
- package/dist/communityRepository/api/updateCommunity.d.ts.map +1 -1
- package/dist/index.cjs.js +1849 -1325
- package/dist/index.esm.js +2067 -1543
- package/dist/index.umd.js +4 -4
- package/dist/invitationRepository/events/index.d.ts +2 -0
- package/dist/invitationRepository/events/index.d.ts.map +1 -1
- package/dist/invitationRepository/events/onLocalInvitationCreated.d.ts +1 -1
- package/dist/invitationRepository/events/onLocalInvitationCreated.d.ts.map +1 -1
- package/dist/invitationRepository/events/onLocalInvitationDeleted.d.ts +1 -1
- package/dist/invitationRepository/events/onLocalInvitationDeleted.d.ts.map +1 -1
- package/dist/invitationRepository/events/onLocalInvitationUpdated.d.ts +1 -1
- package/dist/invitationRepository/events/onLocalInvitationUpdated.d.ts.map +1 -1
- package/dist/invitationRepository/index.d.ts +1 -0
- package/dist/invitationRepository/index.d.ts.map +1 -1
- package/dist/invitationRepository/internalApi/createInvitations.d.ts.map +1 -1
- package/dist/invitationRepository/internalApi/getInvitation.d.ts +17 -0
- package/dist/invitationRepository/internalApi/getInvitation.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getInvitations/InvitationsLiveCollectionController.d.ts +14 -0
- package/dist/invitationRepository/observers/getInvitations/InvitationsLiveCollectionController.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getInvitations/InvitationsPaginationController.d.ts +5 -0
- package/dist/invitationRepository/observers/getInvitations/InvitationsPaginationController.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getInvitations/InvitationsQueryStreamController.d.ts +15 -0
- package/dist/invitationRepository/observers/getInvitations/InvitationsQueryStreamController.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getInvitations/enums.d.ts +6 -0
- package/dist/invitationRepository/observers/getInvitations/enums.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getInvitations.d.ts +12 -0
- package/dist/invitationRepository/observers/getInvitations.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getMyCommunityInvitations.d.ts +12 -0
- package/dist/invitationRepository/observers/getMyCommunityInvitations.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getMyInvitations/MyInvitationsLiveCollectionController.d.ts +14 -0
- package/dist/invitationRepository/observers/getMyInvitations/MyInvitationsLiveCollectionController.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getMyInvitations/MyInvitationsPaginationController.d.ts +5 -0
- package/dist/invitationRepository/observers/getMyInvitations/MyInvitationsPaginationController.d.ts.map +1 -0
- package/dist/invitationRepository/observers/getMyInvitations/MyInvitationsQueryStreamController.d.ts +15 -0
- package/dist/invitationRepository/observers/getMyInvitations/MyInvitationsQueryStreamController.d.ts.map +1 -0
- package/dist/invitationRepository/observers/index.d.ts +2 -0
- package/dist/invitationRepository/observers/index.d.ts.map +1 -0
- package/dist/invitationRepository/utils/convertRawInvitationToInternalInvitation.d.ts +2 -0
- package/dist/invitationRepository/utils/convertRawInvitationToInternalInvitation.d.ts.map +1 -0
- package/dist/invitationRepository/utils/index.d.ts +2 -0
- package/dist/invitationRepository/utils/index.d.ts.map +1 -0
- package/dist/invitationRepository/utils/prepareInvitationPayload.d.ts +2 -0
- package/dist/invitationRepository/utils/prepareInvitationPayload.d.ts.map +1 -0
- package/dist/invitationRepository/utils/prepareMyInvitationsPayload.d.ts +2 -0
- package/dist/invitationRepository/utils/prepareMyInvitationsPayload.d.ts.map +1 -0
- package/dist/utils/linkedObject/communityLinkedObject.d.ts +1 -1
- package/dist/utils/linkedObject/communityLinkedObject.d.ts.map +1 -1
- package/dist/utils/linkedObject/index.d.ts +2 -2
- package/dist/utils/linkedObject/invitationLinkedObject.d.ts +1 -1
- package/dist/utils/linkedObject/invitationLinkedObject.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/@types/core/events.ts +3 -3
- package/src/@types/core/payload.ts +17 -0
- package/src/@types/domains/community.ts +7 -0
- package/src/@types/domains/invitation.ts +67 -3
- package/src/communityRepository/api/getCommunities.ts +5 -1
- package/src/communityRepository/api/getCommunity.ts +1 -1
- package/src/communityRepository/api/getRecommendedCommunities.ts +9 -3
- package/src/communityRepository/api/getTrendingCommunities.ts +9 -3
- package/src/communityRepository/api/updateCommunity.ts +9 -2
- package/src/core/liveCollection/PaginationController.ts +1 -1
- package/src/core/liveCollection/PaginationNoPageController.ts +1 -1
- package/src/invitationRepository/events/index.ts +2 -0
- package/src/invitationRepository/events/onLocalInvitationCreated.ts +1 -1
- package/src/invitationRepository/events/onLocalInvitationDeleted.ts +1 -1
- package/src/invitationRepository/events/onLocalInvitationUpdated.ts +1 -1
- package/src/invitationRepository/index.ts +1 -0
- package/src/invitationRepository/internalApi/acceptInvitation.ts +1 -1
- package/src/invitationRepository/internalApi/cancelInvitation.ts +1 -1
- package/src/invitationRepository/internalApi/createInvitations.ts +8 -2
- package/src/invitationRepository/internalApi/getInvitation.ts +47 -0
- package/src/invitationRepository/internalApi/rejectInvitation.ts +1 -1
- package/src/invitationRepository/observers/getInvitations/InvitationsLiveCollectionController.ts +148 -0
- package/src/invitationRepository/observers/getInvitations/InvitationsPaginationController.ts +19 -0
- package/src/invitationRepository/observers/getInvitations/InvitationsQueryStreamController.ts +97 -0
- package/src/invitationRepository/observers/getInvitations/enums.ts +5 -0
- package/src/invitationRepository/observers/getInvitations.ts +44 -0
- package/src/invitationRepository/observers/getMyCommunityInvitations.ts +48 -0
- package/src/invitationRepository/observers/getMyInvitations/MyInvitationsLiveCollectionController.ts +148 -0
- package/src/invitationRepository/observers/getMyInvitations/MyInvitationsPaginationController.ts +22 -0
- package/src/invitationRepository/observers/getMyInvitations/MyInvitationsQueryStreamController.ts +105 -0
- package/src/invitationRepository/observers/index.ts +1 -0
- package/src/invitationRepository/utils/convertRawInvitationToInternalInvitation.ts +8 -0
- package/src/invitationRepository/utils/index.ts +1 -0
- package/src/invitationRepository/utils/prepareInvitationPayload.ts +12 -0
- package/src/invitationRepository/utils/prepareMyInvitationsPayload.ts +12 -0
- package/src/messagePreview/utils/getChannelMessagePreviewWithUser.ts +3 -3
- package/src/utils/linkedObject/communityLinkedObject.ts +21 -1
- package/src/utils/linkedObject/invitationLinkedObject.ts +25 -1
|
@@ -17,7 +17,7 @@ import { createEventSubscriber } from '~/core/events';
|
|
|
17
17
|
* @category Invitation Events
|
|
18
18
|
*/
|
|
19
19
|
export const onLocalInvitationCreated = (
|
|
20
|
-
callback: Amity.Listener<Amity.
|
|
20
|
+
callback: Amity.Listener<Amity.InternalInvitation[]>,
|
|
21
21
|
): Amity.Unsubscriber => {
|
|
22
22
|
const client = getActiveClient();
|
|
23
23
|
|
|
@@ -17,7 +17,7 @@ import { getActiveClient } from '~/client/api/activeClient';
|
|
|
17
17
|
* @category Invitation Events
|
|
18
18
|
*/
|
|
19
19
|
export const onLocalInvitationDeleted = (
|
|
20
|
-
callback: Amity.Listener<Amity.
|
|
20
|
+
callback: Amity.Listener<Amity.InternalInvitation[]>,
|
|
21
21
|
): Amity.Unsubscriber => {
|
|
22
22
|
const client = getActiveClient();
|
|
23
23
|
|
|
@@ -17,7 +17,7 @@ import { createEventSubscriber } from '~/core/events';
|
|
|
17
17
|
* @category Invitation Events
|
|
18
18
|
*/
|
|
19
19
|
export const onLocalInvitationUpdated = (
|
|
20
|
-
callback: Amity.Listener<Amity.
|
|
20
|
+
callback: Amity.Listener<Amity.InternalInvitation[]>,
|
|
21
21
|
): Amity.Unsubscriber => {
|
|
22
22
|
const client = getActiveClient();
|
|
23
23
|
|
|
@@ -37,7 +37,7 @@ export const acceptInvitation = async (invitationId: Amity.Invitation['_id']): P
|
|
|
37
37
|
if (invitation) {
|
|
38
38
|
upsertInCache(['invitation', 'get', invitationId], { status: InvitationStatusEnum.Approved });
|
|
39
39
|
|
|
40
|
-
fireEvent('local.invitation.updated',
|
|
40
|
+
fireEvent('local.invitation.updated', [invitation]);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
return data.success;
|
|
@@ -36,7 +36,7 @@ export const cancelInvitation = async (invitationId: Amity.Invitation['_id']): P
|
|
|
36
36
|
if (invitation) {
|
|
37
37
|
dropFromCache(['invitation', 'get', invitationId]);
|
|
38
38
|
|
|
39
|
-
fireEvent('local.invitation.deleted',
|
|
39
|
+
fireEvent('local.invitation.deleted', [invitation]);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
return data.success;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { fireEvent } from '~/core/events';
|
|
2
2
|
import { getActiveClient } from '~/client/api/activeClient';
|
|
3
3
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
4
|
+
import { prepareInvitationPayload } from '../utils';
|
|
4
5
|
|
|
5
6
|
/* begin_public_function
|
|
6
7
|
id: invitation.createInvitations
|
|
@@ -30,13 +31,18 @@ export const createInvitations = async (
|
|
|
30
31
|
const client = getActiveClient();
|
|
31
32
|
client.log('invitation/createInvitations', bundle);
|
|
32
33
|
|
|
33
|
-
const { data } = await client.http.post<Amity.InvitationPayload>(
|
|
34
|
+
const { data: payload } = await client.http.post<Amity.InvitationPayload>(
|
|
35
|
+
'/api/v1/invitations',
|
|
36
|
+
bundle,
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const data = prepareInvitationPayload(payload);
|
|
34
40
|
|
|
35
41
|
const cachedAt = client.cache && Date.now();
|
|
36
42
|
|
|
37
43
|
if (client.cache) ingestInCache(data, { cachedAt });
|
|
38
44
|
|
|
39
|
-
fireEvent('local.invitation.created', data);
|
|
45
|
+
fireEvent('local.invitation.created', data.invitations);
|
|
40
46
|
|
|
41
47
|
return {
|
|
42
48
|
data: data.invitations,
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { getActiveClient } from '~/client/api/activeClient';
|
|
2
|
+
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
3
|
+
import { prepareMyInvitationsPayload } from '../utils/prepareMyInvitationsPayload';
|
|
4
|
+
import { invitationLinkedObject } from '~/utils/linkedObject/invitationLinkedObject';
|
|
5
|
+
|
|
6
|
+
/* begin_public_function
|
|
7
|
+
id: invitation.get
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* ```js
|
|
11
|
+
* import { getInvitation } from '@amityco/ts-sdk'
|
|
12
|
+
* const { invitation } = await getInvitation(targetType, targetId)
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Get a {@link Amity.Invitation} object
|
|
16
|
+
*
|
|
17
|
+
* @param targetType The type of the target of the {@link Amity.Invitation}
|
|
18
|
+
* @param targetId The ID of the target of the {@link Amity.Invitation}
|
|
19
|
+
* @returns A {@link Amity.Invitation} object
|
|
20
|
+
*
|
|
21
|
+
* @category Invitation API
|
|
22
|
+
* @async
|
|
23
|
+
*/
|
|
24
|
+
export const getInvitation = async (
|
|
25
|
+
targetType: Amity.Invitation['targetType'],
|
|
26
|
+
targetId: Amity.Invitation['targetId'],
|
|
27
|
+
): Promise<Amity.Cached<Amity.Invitation | undefined>> => {
|
|
28
|
+
const client = getActiveClient();
|
|
29
|
+
client.log('invitation/getInvitation', targetType, targetId);
|
|
30
|
+
|
|
31
|
+
const { data: payload } = await client.http.get<Amity.MyInvitationsPayload>(
|
|
32
|
+
`/api/v1/invitations/me`,
|
|
33
|
+
{ params: { targetType, targetId } },
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const data = prepareMyInvitationsPayload(payload);
|
|
37
|
+
|
|
38
|
+
const cachedAt = client.cache && Date.now();
|
|
39
|
+
|
|
40
|
+
if (client.cache) ingestInCache(data, { cachedAt });
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
data: data.invitations[0] ? invitationLinkedObject(data.invitations[0]) : undefined,
|
|
44
|
+
cachedAt,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/* end_public_function */
|
|
@@ -37,7 +37,7 @@ export const rejectInvitation = async (invitationId: Amity.Invitation['_id']): P
|
|
|
37
37
|
if (invitation) {
|
|
38
38
|
upsertInCache(['invitation', 'get', invitationId], { status: InvitationStatusEnum.Rejected });
|
|
39
39
|
|
|
40
|
-
fireEvent('local.invitation.updated',
|
|
40
|
+
fireEvent('local.invitation.updated', [invitation]);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
return data.success;
|
package/src/invitationRepository/observers/getInvitations/InvitationsLiveCollectionController.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import hash from 'object-hash';
|
|
2
|
+
import { isNonNullable } from '~/utils';
|
|
3
|
+
import { InvitationActionsEnum } from './enums';
|
|
4
|
+
import { invitationLinkedObject } from '~/utils/linkedObject/invitationLinkedObject';
|
|
5
|
+
import { pullFromCache, pushToCache } from '~/cache/api';
|
|
6
|
+
import { sortByFirstCreated, sortByLastCreated } from '~/core/query';
|
|
7
|
+
import { InvitationsPaginationController } from './InvitationsPaginationController';
|
|
8
|
+
import { InvitationsQueryStreamController } from './InvitationsQueryStreamController';
|
|
9
|
+
import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
|
|
10
|
+
import { prepareInvitationPayload } from '~/invitationRepository/utils';
|
|
11
|
+
import {
|
|
12
|
+
onLocalInvitationCreated,
|
|
13
|
+
onLocalInvitationDeleted,
|
|
14
|
+
onLocalInvitationUpdated,
|
|
15
|
+
} from '~/invitationRepository/events';
|
|
16
|
+
|
|
17
|
+
export class InvitationsLiveCollectionController extends LiveCollectionController<
|
|
18
|
+
'invitation',
|
|
19
|
+
Amity.InvitationLiveCollection,
|
|
20
|
+
Amity.Invitation,
|
|
21
|
+
InvitationsPaginationController
|
|
22
|
+
> {
|
|
23
|
+
private queryStreamController: InvitationsQueryStreamController;
|
|
24
|
+
|
|
25
|
+
private query: Amity.InvitationLiveCollection;
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
query: Amity.InvitationLiveCollection,
|
|
29
|
+
callback: Amity.LiveCollectionCallback<Amity.Invitation>,
|
|
30
|
+
) {
|
|
31
|
+
const queryStreamId = hash(query);
|
|
32
|
+
const cacheKey = ['invitation', 'collection', queryStreamId];
|
|
33
|
+
const paginationController = new InvitationsPaginationController(query);
|
|
34
|
+
|
|
35
|
+
super(paginationController, queryStreamId, cacheKey, callback);
|
|
36
|
+
|
|
37
|
+
this.query = query;
|
|
38
|
+
this.queryStreamController = new InvitationsQueryStreamController(
|
|
39
|
+
this.query,
|
|
40
|
+
this.cacheKey,
|
|
41
|
+
this.notifyChange.bind(this),
|
|
42
|
+
prepareInvitationPayload,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
this.callback = callback.bind(this);
|
|
46
|
+
this.loadPage({ initial: true });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected setup() {
|
|
50
|
+
const collection = pullFromCache<Amity.InvitationLiveCollectionCache>(this.cacheKey)?.data;
|
|
51
|
+
if (!collection) {
|
|
52
|
+
pushToCache(this.cacheKey, {
|
|
53
|
+
data: [],
|
|
54
|
+
params: this.query,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected async persistModel(queryPayload: Amity.InvitationPayload & Amity.Pagination) {
|
|
60
|
+
await this.queryStreamController.saveToMainDB(queryPayload);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected persistQueryStream({
|
|
64
|
+
response,
|
|
65
|
+
direction,
|
|
66
|
+
refresh,
|
|
67
|
+
}: Amity.LiveCollectionPersistQueryStreamParams<'invitation'>) {
|
|
68
|
+
this.queryStreamController.appendToQueryStream(response, direction, refresh);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
startSubscription() {
|
|
72
|
+
return this.queryStreamController.subscribeRTE([
|
|
73
|
+
{
|
|
74
|
+
fn: onLocalInvitationCreated,
|
|
75
|
+
action: InvitationActionsEnum.OnLocalInvitationCreated,
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
fn: onLocalInvitationUpdated,
|
|
79
|
+
action: InvitationActionsEnum.OnLocalInvitationUpdated,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
fn: onLocalInvitationDeleted,
|
|
83
|
+
action: InvitationActionsEnum.OnLocalInvitationDeleted,
|
|
84
|
+
},
|
|
85
|
+
]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
|
|
89
|
+
const collection = pullFromCache<Amity.InvitationLiveCollectionCache>(this.cacheKey)?.data;
|
|
90
|
+
if (!collection) return;
|
|
91
|
+
|
|
92
|
+
const data = this.applyFilter(
|
|
93
|
+
collection.data
|
|
94
|
+
.map(id => pullFromCache<Amity.InternalInvitation>(['invitation', 'get', id])!)
|
|
95
|
+
.filter(isNonNullable)
|
|
96
|
+
.map(({ data }) => invitationLinkedObject(data)) ?? [],
|
|
97
|
+
);
|
|
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
|
+
|
|
110
|
+
applyFilter(data: Amity.Invitation[]) {
|
|
111
|
+
let invitations = data;
|
|
112
|
+
|
|
113
|
+
if (this.query.targetId) {
|
|
114
|
+
invitations = invitations.filter(invitation => invitation.targetId === this.query.targetId);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (this.query.statuses) {
|
|
118
|
+
invitations = invitations.filter(invitation =>
|
|
119
|
+
this.query.statuses?.includes(invitation.status),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (this.query.targetType) {
|
|
124
|
+
invitations = invitations.filter(
|
|
125
|
+
invitation => invitation.targetType === this.query.targetType,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (this.query.type) {
|
|
130
|
+
invitations = invitations.filter(invitation => invitation.type === this.query.type);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const sortFn = (() => {
|
|
134
|
+
switch (this.query.sortBy) {
|
|
135
|
+
case 'firstCreated':
|
|
136
|
+
return sortByFirstCreated;
|
|
137
|
+
case 'lastCreated':
|
|
138
|
+
return sortByLastCreated;
|
|
139
|
+
default:
|
|
140
|
+
return sortByLastCreated;
|
|
141
|
+
}
|
|
142
|
+
})();
|
|
143
|
+
|
|
144
|
+
invitations = invitations.sort(sortFn);
|
|
145
|
+
|
|
146
|
+
return invitations;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';
|
|
2
|
+
import { PaginationController } from '~/core/liveCollection/PaginationController';
|
|
3
|
+
|
|
4
|
+
export class InvitationsPaginationController extends PaginationController<
|
|
5
|
+
'invitation',
|
|
6
|
+
Amity.InvitationLiveCollection
|
|
7
|
+
> {
|
|
8
|
+
async getRequest(queryParams: Amity.InvitationLiveCollection, token: string | undefined) {
|
|
9
|
+
const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, ...params } = queryParams;
|
|
10
|
+
const options = token ? { token } : { limit };
|
|
11
|
+
|
|
12
|
+
const { data } = await this.http.get<Amity.InvitationPayload & Amity.Pagination>(
|
|
13
|
+
'/api/v1/invitations',
|
|
14
|
+
{ params: { ...params, options } },
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
return data;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { getActiveClient } from '~/client/api/activeClient';
|
|
2
|
+
import { getResolver } from '~/core/model';
|
|
3
|
+
import { InvitationActionsEnum } from './enums';
|
|
4
|
+
import { pullFromCache, pushToCache } from '~/cache/api';
|
|
5
|
+
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
6
|
+
import { QueryStreamController } from '~/core/liveCollection/QueryStreamController';
|
|
7
|
+
|
|
8
|
+
export class InvitationsQueryStreamController extends QueryStreamController<
|
|
9
|
+
Amity.InvitationPayload,
|
|
10
|
+
Amity.InvitationLiveCollection
|
|
11
|
+
> {
|
|
12
|
+
private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
|
|
13
|
+
|
|
14
|
+
private preparePayload: (response: Amity.InvitationPayload) => Amity.ProcessedInvitationPayload;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
query: Amity.InvitationLiveCollection,
|
|
18
|
+
cacheKey: string[],
|
|
19
|
+
notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
|
|
20
|
+
preparePayload: (response: Amity.InvitationPayload) => Amity.ProcessedInvitationPayload,
|
|
21
|
+
) {
|
|
22
|
+
super(query, cacheKey);
|
|
23
|
+
this.notifyChange = notifyChange;
|
|
24
|
+
this.preparePayload = preparePayload;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async saveToMainDB(response: Amity.InvitationPayload) {
|
|
28
|
+
const processedPayload = await this.preparePayload(response);
|
|
29
|
+
|
|
30
|
+
const client = getActiveClient();
|
|
31
|
+
const cachedAt = client.cache && Date.now();
|
|
32
|
+
|
|
33
|
+
if (client.cache) {
|
|
34
|
+
ingestInCache(processedPayload, { cachedAt });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
appendToQueryStream(
|
|
39
|
+
response: Amity.InvitationPayload & Partial<Amity.Pagination>,
|
|
40
|
+
direction: Amity.LiveCollectionPageDirection,
|
|
41
|
+
refresh = false,
|
|
42
|
+
) {
|
|
43
|
+
if (refresh) {
|
|
44
|
+
pushToCache(this.cacheKey, {
|
|
45
|
+
data: response.invitations.map(getResolver('invitation')),
|
|
46
|
+
});
|
|
47
|
+
} else {
|
|
48
|
+
const collection = pullFromCache<Amity.InvitationLiveCollectionCache>(this.cacheKey)?.data;
|
|
49
|
+
|
|
50
|
+
const invitations = collection?.data ?? [];
|
|
51
|
+
|
|
52
|
+
pushToCache(this.cacheKey, {
|
|
53
|
+
...collection,
|
|
54
|
+
data: [
|
|
55
|
+
...new Set([...invitations, ...response.invitations.map(getResolver('invitation'))]),
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
reactor(action: InvitationActionsEnum) {
|
|
62
|
+
return (invitations: Amity.InternalInvitation[]) => {
|
|
63
|
+
const collection = pullFromCache<Amity.InvitationLiveCollectionCache>(this.cacheKey)?.data;
|
|
64
|
+
if (!collection) return;
|
|
65
|
+
|
|
66
|
+
if (action === InvitationActionsEnum.OnLocalInvitationUpdated) {
|
|
67
|
+
const isExist = collection.data.find(id => id === invitations[0].invitationId);
|
|
68
|
+
if (!isExist) return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (action === InvitationActionsEnum.OnLocalInvitationCreated) {
|
|
72
|
+
collection.data = [
|
|
73
|
+
...new Set([
|
|
74
|
+
...invitations.map(invitation => invitation.invitationId),
|
|
75
|
+
...collection.data,
|
|
76
|
+
]),
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (action === InvitationActionsEnum.OnLocalInvitationDeleted) {
|
|
81
|
+
collection.data = collection.data.filter(id => id !== invitations[0].invitationId);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
pushToCache(this.cacheKey, collection);
|
|
85
|
+
this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
subscribeRTE(
|
|
90
|
+
createSubscriber: {
|
|
91
|
+
fn: (reactor: Amity.Listener<Amity.InternalInvitation[]>) => Amity.Unsubscriber;
|
|
92
|
+
action: InvitationActionsEnum;
|
|
93
|
+
}[],
|
|
94
|
+
) {
|
|
95
|
+
return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { dropFromCache } from '~/cache/api';
|
|
2
|
+
import { getActiveClient } from '~/client/api/activeClient';
|
|
3
|
+
import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
|
|
4
|
+
import { InvitationsLiveCollectionController } from './getInvitations/InvitationsLiveCollectionController';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get invitations
|
|
8
|
+
*
|
|
9
|
+
* @param params the query parameters
|
|
10
|
+
* @param callback the callback to be called when the invitations are updated
|
|
11
|
+
* @returns invitations
|
|
12
|
+
*
|
|
13
|
+
* @category Invitation Live Collection
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
export const getInvitations = (
|
|
17
|
+
params: Amity.InvitationLiveCollection,
|
|
18
|
+
callback: Amity.LiveCollectionCallback<Amity.Invitation>,
|
|
19
|
+
config?: Amity.LiveCollectionConfig,
|
|
20
|
+
) => {
|
|
21
|
+
const { log, cache } = getActiveClient();
|
|
22
|
+
|
|
23
|
+
if (!cache) {
|
|
24
|
+
console.log(ENABLE_CACHE_MESSAGE);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const timestamp = Date.now();
|
|
28
|
+
log(`getInvitations: (tmpid: ${timestamp}) > listen`);
|
|
29
|
+
|
|
30
|
+
const invitationsLiveCollection = new InvitationsLiveCollectionController(params, callback);
|
|
31
|
+
|
|
32
|
+
const disposers = invitationsLiveCollection.startSubscription();
|
|
33
|
+
|
|
34
|
+
const cacheKey = invitationsLiveCollection.getCacheKey();
|
|
35
|
+
|
|
36
|
+
disposers.push(() => {
|
|
37
|
+
dropFromCache(cacheKey);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return () => {
|
|
41
|
+
log(`getInvitations (tmpid: ${timestamp}) > dispose`);
|
|
42
|
+
disposers.forEach(fn => fn());
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { dropFromCache } from '~/cache/api';
|
|
2
|
+
import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
|
|
3
|
+
import { getActiveClient } from '~/client/api/activeClient';
|
|
4
|
+
import { MyInvitationsLiveCollectionController } from './getMyInvitations/MyInvitationsLiveCollectionController';
|
|
5
|
+
import { InvitationStatusEnum } from '~/@types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get my community invitations
|
|
9
|
+
*
|
|
10
|
+
* @param params the query parameters
|
|
11
|
+
* @param callback the callback to be called when the invitations are fetched
|
|
12
|
+
* @returns invitations
|
|
13
|
+
*
|
|
14
|
+
* @category My Community Invitations Live Collection
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
export const getMyCommunityInvitations = (
|
|
18
|
+
params: Amity.MyInvitationsLiveCollection,
|
|
19
|
+
callback: Amity.LiveCollectionCallback<Amity.Invitation>,
|
|
20
|
+
config?: Amity.LiveCollectionConfig,
|
|
21
|
+
) => {
|
|
22
|
+
const { log, cache } = getActiveClient();
|
|
23
|
+
|
|
24
|
+
if (!cache) {
|
|
25
|
+
console.log(ENABLE_CACHE_MESSAGE);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const timestamp = Date.now();
|
|
29
|
+
log(`getMyCommunityInvitations: (tmpid: ${timestamp}) > listen`);
|
|
30
|
+
|
|
31
|
+
const invitationsLiveCollection = new MyInvitationsLiveCollectionController(
|
|
32
|
+
{ ...params, targetType: 'community', statuses: [InvitationStatusEnum.Pending] },
|
|
33
|
+
callback,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const disposers = invitationsLiveCollection.startSubscription();
|
|
37
|
+
|
|
38
|
+
const cacheKey = invitationsLiveCollection.getCacheKey();
|
|
39
|
+
|
|
40
|
+
disposers.push(() => {
|
|
41
|
+
dropFromCache(cacheKey);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return () => {
|
|
45
|
+
log(`getInvitations (tmpid: ${timestamp}) > dispose`);
|
|
46
|
+
disposers.forEach(fn => fn());
|
|
47
|
+
};
|
|
48
|
+
};
|
package/src/invitationRepository/observers/getMyInvitations/MyInvitationsLiveCollectionController.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import hash from 'object-hash';
|
|
2
|
+
import { isNonNullable } from '~/utils';
|
|
3
|
+
import { invitationLinkedObject } from '~/utils/linkedObject/invitationLinkedObject';
|
|
4
|
+
import { pullFromCache, pushToCache } from '~/cache/api';
|
|
5
|
+
import { sortByFirstCreated, sortByLastCreated } from '~/core/query';
|
|
6
|
+
import { MyInvitationsPaginationController } from './MyInvitationsPaginationController';
|
|
7
|
+
import { MyInvitationsQueryStreamController } from './MyInvitationsQueryStreamController';
|
|
8
|
+
import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
|
|
9
|
+
import {
|
|
10
|
+
onLocalInvitationCreated,
|
|
11
|
+
onLocalInvitationDeleted,
|
|
12
|
+
onLocalInvitationUpdated,
|
|
13
|
+
} from '~/invitationRepository/events';
|
|
14
|
+
import { prepareMyInvitationsPayload } from '~/invitationRepository/utils/prepareMyInvitationsPayload';
|
|
15
|
+
import { InvitationActionsEnum } from '../getInvitations/enums';
|
|
16
|
+
|
|
17
|
+
export class MyInvitationsLiveCollectionController extends LiveCollectionController<
|
|
18
|
+
'myInvitation',
|
|
19
|
+
Amity.MyInvitationsLiveCollection,
|
|
20
|
+
Amity.Invitation,
|
|
21
|
+
MyInvitationsPaginationController
|
|
22
|
+
> {
|
|
23
|
+
private queryStreamController: MyInvitationsQueryStreamController;
|
|
24
|
+
|
|
25
|
+
private query: Amity.MyInvitationsLiveCollection;
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
query: Amity.MyInvitationsLiveCollection,
|
|
29
|
+
callback: Amity.LiveCollectionCallback<Amity.Invitation>,
|
|
30
|
+
) {
|
|
31
|
+
const queryStreamId = hash(query);
|
|
32
|
+
const cacheKey = ['invitation', 'collection', queryStreamId];
|
|
33
|
+
const paginationController = new MyInvitationsPaginationController(query);
|
|
34
|
+
|
|
35
|
+
super(paginationController, queryStreamId, cacheKey, callback);
|
|
36
|
+
|
|
37
|
+
this.query = query;
|
|
38
|
+
this.queryStreamController = new MyInvitationsQueryStreamController(
|
|
39
|
+
this.query,
|
|
40
|
+
this.cacheKey,
|
|
41
|
+
this.notifyChange.bind(this),
|
|
42
|
+
prepareMyInvitationsPayload,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
this.callback = callback.bind(this);
|
|
46
|
+
this.loadPage({ initial: true });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected setup() {
|
|
50
|
+
const collection = pullFromCache<Amity.MyInvitationsLiveCollectionCache>(this.cacheKey)?.data;
|
|
51
|
+
if (!collection) {
|
|
52
|
+
pushToCache(this.cacheKey, {
|
|
53
|
+
data: [],
|
|
54
|
+
params: this.query,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected async persistModel(queryPayload: Amity.MyInvitationsPayload & Amity.Pagination) {
|
|
60
|
+
await this.queryStreamController.saveToMainDB(queryPayload);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected persistQueryStream({
|
|
64
|
+
response,
|
|
65
|
+
direction,
|
|
66
|
+
refresh,
|
|
67
|
+
}: Amity.LiveCollectionPersistQueryStreamParams<'myInvitation'>) {
|
|
68
|
+
this.queryStreamController.appendToQueryStream(response, direction, refresh);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
startSubscription() {
|
|
72
|
+
return this.queryStreamController.subscribeRTE([
|
|
73
|
+
{
|
|
74
|
+
fn: onLocalInvitationCreated,
|
|
75
|
+
action: InvitationActionsEnum.OnLocalInvitationCreated,
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
fn: onLocalInvitationUpdated,
|
|
79
|
+
action: InvitationActionsEnum.OnLocalInvitationUpdated,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
fn: onLocalInvitationDeleted,
|
|
83
|
+
action: InvitationActionsEnum.OnLocalInvitationDeleted,
|
|
84
|
+
},
|
|
85
|
+
]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
|
|
89
|
+
const collection = pullFromCache<Amity.MyInvitationsLiveCollectionCache>(this.cacheKey)?.data;
|
|
90
|
+
if (!collection) return;
|
|
91
|
+
|
|
92
|
+
const data = this.applyFilter(
|
|
93
|
+
collection.data
|
|
94
|
+
.map(id => pullFromCache<Amity.InternalInvitation>(['invitation', 'get', id])!)
|
|
95
|
+
.filter(isNonNullable)
|
|
96
|
+
.map(({ data }) => invitationLinkedObject(data)) ?? [],
|
|
97
|
+
);
|
|
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
|
+
|
|
110
|
+
applyFilter(data: Amity.Invitation[]) {
|
|
111
|
+
let invitations = data;
|
|
112
|
+
|
|
113
|
+
if (this.query.targetId) {
|
|
114
|
+
invitations = invitations.filter(invitation => invitation.targetId === this.query.targetId);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (this.query.statuses) {
|
|
118
|
+
invitations = invitations.filter(invitation =>
|
|
119
|
+
this.query.statuses?.includes(invitation.status),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (this.query.targetType) {
|
|
124
|
+
invitations = invitations.filter(
|
|
125
|
+
invitation => invitation.targetType === this.query.targetType,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (this.query.type) {
|
|
130
|
+
invitations = invitations.filter(invitation => invitation.type === this.query.type);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const sortFn = (() => {
|
|
134
|
+
switch (this.query.sortBy) {
|
|
135
|
+
case 'firstCreated':
|
|
136
|
+
return sortByFirstCreated;
|
|
137
|
+
case 'lastCreated':
|
|
138
|
+
return sortByLastCreated;
|
|
139
|
+
default:
|
|
140
|
+
return sortByLastCreated;
|
|
141
|
+
}
|
|
142
|
+
})();
|
|
143
|
+
|
|
144
|
+
invitations = invitations.sort(sortFn);
|
|
145
|
+
|
|
146
|
+
return invitations;
|
|
147
|
+
}
|
|
148
|
+
}
|