@amityco/ts-sdk 0.0.1-beta.24 → 0.0.1-beta.27
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/.eslintrc.json +4 -1
- package/dist/@types/core/events.d.ts +12 -8
- package/dist/@types/core/events.d.ts.map +1 -1
- package/dist/@types/core/live.d.ts +23 -0
- package/dist/@types/core/live.d.ts.map +1 -0
- package/dist/@types/core/paging.d.ts +1 -1
- package/dist/@types/core/paging.d.ts.map +1 -1
- package/dist/@types/core/payload.d.ts +3 -5
- package/dist/@types/core/payload.d.ts.map +1 -1
- package/dist/@types/domains/comment.d.ts +14 -0
- package/dist/@types/domains/comment.d.ts.map +1 -1
- package/dist/@types/domains/follow.d.ts +2 -1
- package/dist/@types/domains/follow.d.ts.map +1 -1
- package/dist/@types/domains/post.d.ts +17 -0
- package/dist/@types/domains/post.d.ts.map +1 -1
- package/dist/@types/domains/reaction.d.ts +10 -0
- package/dist/@types/domains/reaction.d.ts.map +1 -1
- package/dist/@types/index.d.ts +1 -0
- package/dist/@types/index.d.ts.map +1 -1
- package/dist/cache/api/ingestInCache.d.ts +1 -1
- package/dist/cache/api/ingestInCache.d.ts.map +1 -1
- package/dist/cache/api/mergeInCache.d.ts +1 -1
- package/dist/cache/api/mergeInCache.d.ts.map +1 -1
- package/dist/category/api/queryCategories.d.ts +3 -3
- package/dist/category/api/queryCategories.d.ts.map +1 -1
- package/dist/channel/api/banChannelMembers.d.ts +16 -0
- package/dist/channel/api/banChannelMembers.d.ts.map +1 -0
- package/dist/channel/api/index.d.ts +2 -0
- package/dist/channel/api/index.d.ts.map +1 -1
- package/dist/channel/api/queryChannels.d.ts +6 -6
- package/dist/channel/api/queryChannels.d.ts.map +1 -1
- package/dist/channel/api/unbanChannelMembers.d.ts +16 -0
- package/dist/channel/api/unbanChannelMembers.d.ts.map +1 -0
- package/dist/client/api/connectClient.d.ts +1 -1
- package/dist/client/api/connectClient.d.ts.map +1 -1
- package/dist/client/api/createClient.d.ts +1 -1
- package/dist/client/api/createClient.d.ts.map +1 -1
- package/dist/comment/api/queryComments.d.ts +1 -9
- package/dist/comment/api/queryComments.d.ts.map +1 -1
- package/dist/comment/observers/index.d.ts +1 -0
- package/dist/comment/observers/index.d.ts.map +1 -1
- package/dist/comment/observers/liveComments.d.ts +22 -0
- package/dist/comment/observers/liveComments.d.ts.map +1 -0
- package/dist/comment/observers/observeComments.d.ts.map +1 -1
- package/dist/community/api/getRecommendedCommunities.d.ts +1 -1
- package/dist/community/api/getRecommendedCommunities.d.ts.map +1 -1
- package/dist/community/api/getTopTrendingCommunities.d.ts +1 -1
- package/dist/community/api/getTopTrendingCommunities.d.ts.map +1 -1
- package/dist/community/api/queryCommunities.d.ts +7 -7
- package/dist/community/api/queryCommunities.d.ts.map +1 -1
- package/dist/core/debug.d.ts +1 -1
- package/dist/core/debug.d.ts.map +1 -1
- package/dist/core/events.d.ts +3 -3
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/query/paging.d.ts +2 -2
- package/dist/core/query/paging.d.ts.map +1 -1
- package/dist/core/query/query.d.ts +1 -1
- package/dist/core/query/query.d.ts.map +1 -1
- package/dist/core/tests/query/filtering.test.d.ts +2 -0
- package/dist/core/tests/query/filtering.test.d.ts.map +1 -0
- package/dist/core/tests/query/query.test.d.ts +2 -0
- package/dist/core/tests/query/query.test.d.ts.map +1 -0
- package/dist/core/transports/ws.d.ts +1 -1
- package/dist/core/transports/ws.d.ts.map +1 -1
- package/dist/external/api/createUserToken.d.ts +1 -1
- package/dist/external/api/createUserToken.d.ts.map +1 -1
- package/dist/feed/api/queryGlobalFeed.d.ts +4 -4
- package/dist/feed/api/queryGlobalFeed.d.ts.map +1 -1
- package/dist/follow/api/follow.d.ts.map +1 -1
- package/dist/follow/api/getFollowInfo.d.ts.map +1 -1
- package/dist/follow/api/queryFollowers.d.ts +4 -4
- package/dist/follow/api/queryFollowers.d.ts.map +1 -1
- package/dist/follow/api/queryFollowings.d.ts +4 -4
- package/dist/follow/api/queryFollowings.d.ts.map +1 -1
- package/dist/follow/api/utils.d.ts +4 -4
- package/dist/follow/api/utils.d.ts.map +1 -1
- package/dist/follow/events/utils.d.ts.map +1 -1
- package/dist/index.cjs.js +18341 -17625
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +18329 -17626
- package/dist/index.umd.js +4 -19
- package/dist/poll/api/closePoll.d.ts.map +1 -1
- package/dist/poll/api/deletePoll.d.ts.map +1 -1
- package/dist/poll/api/votePoll.d.ts.map +1 -1
- package/dist/poll/events/index.d.ts +3 -0
- package/dist/poll/events/index.d.ts.map +1 -0
- package/dist/poll/events/onPollDeleted.d.ts +17 -0
- package/dist/poll/events/onPollDeleted.d.ts.map +1 -0
- package/dist/poll/events/onPollUpdated.d.ts +17 -0
- package/dist/poll/events/onPollUpdated.d.ts.map +1 -0
- package/dist/poll/observers/index.d.ts +2 -0
- package/dist/poll/observers/index.d.ts.map +1 -0
- package/dist/poll/observers/observePoll.d.ts +18 -0
- package/dist/poll/observers/observePoll.d.ts.map +1 -0
- package/dist/post/api/approvePost.d.ts +17 -0
- package/dist/post/api/approvePost.d.ts.map +1 -0
- package/dist/post/api/declinePost.d.ts +17 -0
- package/dist/post/api/declinePost.d.ts.map +1 -0
- package/dist/post/api/index.d.ts +2 -0
- package/dist/post/api/index.d.ts.map +1 -1
- package/dist/post/api/queryPosts.d.ts +1 -12
- package/dist/post/api/queryPosts.d.ts.map +1 -1
- package/dist/post/observers/index.d.ts +1 -0
- package/dist/post/observers/index.d.ts.map +1 -1
- package/dist/post/observers/livePosts.d.ts +22 -0
- package/dist/post/observers/livePosts.d.ts.map +1 -0
- package/dist/post/observers/observePosts.d.ts.map +1 -1
- package/dist/post/tests/api/getPost.test.d.ts +2 -0
- package/dist/post/tests/api/getPost.test.d.ts.map +1 -0
- package/dist/post/tests/api/queryPosts.test.d.ts +2 -0
- package/dist/post/tests/api/queryPosts.test.d.ts.map +1 -0
- package/dist/reaction/api/queryReactions.d.ts +1 -6
- package/dist/reaction/api/queryReactions.d.ts.map +1 -1
- package/dist/reaction/events/index.d.ts +3 -0
- package/dist/reaction/events/index.d.ts.map +1 -1
- package/dist/reaction/events/onReactorAdded.d.ts +19 -0
- package/dist/reaction/events/onReactorAdded.d.ts.map +1 -0
- package/dist/reaction/events/onReactorRemoved.d.ts +19 -0
- package/dist/reaction/events/onReactorRemoved.d.ts.map +1 -0
- package/dist/reaction/observers/index.d.ts +2 -0
- package/dist/reaction/observers/index.d.ts.map +1 -0
- package/dist/reaction/observers/liveReactions.d.ts +22 -0
- package/dist/reaction/observers/liveReactions.d.ts.map +1 -0
- package/dist/report/api/createReport.d.ts.map +1 -1
- package/dist/report/api/deleteReport.d.ts.map +1 -1
- package/dist/role/api/queryRoles.d.ts +4 -4
- package/dist/role/api/queryRoles.d.ts.map +1 -1
- package/dist/stream/api/queryStreams.d.ts +6 -6
- package/dist/stream/api/queryStreams.d.ts.map +1 -1
- package/dist/utils/constants.d.ts +3 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/tests/client.d.ts +3 -0
- package/dist/utils/tests/client.d.ts.map +1 -0
- package/dist/utils/tests/dummy.d.ts +8 -0
- package/dist/utils/tests/dummy.d.ts.map +1 -0
- package/dist/utils/tests/index.d.ts +3 -0
- package/dist/utils/tests/index.d.ts.map +1 -0
- package/jest.config.ts +15 -0
- package/package.json +21 -16
- package/src/@types/core/events.ts +13 -8
- package/src/@types/core/live.ts +28 -0
- package/src/@types/core/paging.ts +1 -1
- package/src/@types/core/payload.ts +5 -6
- package/src/@types/domains/comment.ts +31 -0
- package/src/@types/domains/follow.ts +3 -1
- package/src/@types/domains/post.ts +37 -0
- package/src/@types/domains/reaction.ts +17 -0
- package/src/@types/index.ts +1 -0
- package/src/channel/api/banChannelMembers.ts +41 -0
- package/src/channel/api/index.ts +3 -0
- package/src/channel/api/unbanChannelMembers.ts +41 -0
- package/src/comment/api/queryComments.ts +3 -9
- package/src/comment/observers/index.ts +1 -0
- package/src/comment/observers/liveComments.ts +172 -0
- package/src/comment/observers/observeComments.ts +1 -11
- package/src/core/query/query.ts +15 -2
- package/src/core/tests/query/filtering.test.ts +11 -0
- package/src/core/tests/query/query.test.ts +19 -0
- package/src/follow/api/acceptFollower.ts +1 -1
- package/src/follow/api/declineFollower.ts +1 -1
- package/src/follow/api/follow.ts +1 -4
- package/src/follow/api/getFollowInfo.ts +8 -5
- package/src/follow/api/queryFollowers.ts +5 -4
- package/src/follow/api/queryFollowings.ts +5 -4
- package/src/follow/api/unfollow.ts +1 -1
- package/src/follow/api/utils.ts +10 -10
- package/src/follow/events/utils.ts +9 -6
- package/src/index.ts +3 -0
- package/src/poll/api/closePoll.ts +3 -0
- package/src/poll/api/deletePoll.ts +3 -0
- package/src/poll/api/votePoll.ts +3 -0
- package/src/poll/events/index.ts +2 -0
- package/src/poll/events/onPollDeleted.ts +31 -0
- package/src/poll/events/onPollUpdated.ts +31 -0
- package/src/poll/observers/index.ts +1 -0
- package/src/poll/observers/observePoll.ts +67 -0
- package/src/post/api/approvePost.ts +48 -0
- package/src/post/api/declinePost.ts +48 -0
- package/src/post/api/index.ts +3 -0
- package/src/post/api/queryPosts.ts +3 -12
- package/src/post/observers/index.ts +1 -0
- package/src/post/observers/livePosts.ts +170 -0
- package/src/post/observers/observePosts.ts +1 -13
- package/src/post/tests/api/getPost.test.ts +88 -0
- package/src/post/tests/api/queryPosts.test.ts +23 -0
- package/src/reaction/api/queryReactions.ts +14 -14
- package/src/reaction/events/index.ts +4 -0
- package/src/reaction/events/onReactorAdded.ts +80 -0
- package/src/reaction/events/onReactorRemoved.ts +85 -0
- package/src/reaction/observers/index.ts +1 -0
- package/src/reaction/observers/liveReactions.ts +142 -0
- package/src/report/api/createReport.ts +7 -1
- package/src/report/api/deleteReport.ts +7 -1
- package/src/utils/constants.ts +2 -0
- package/src/utils/tests/client.ts +5 -0
- package/src/utils/tests/dummy.ts +7 -0
- package/src/utils/tests/index.ts +2 -0
- package/tsconfig.json +23 -22
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ASCApiError } from '~/core/errors';
|
|
2
|
+
import { client, userPosts } from '~/utils/tests';
|
|
3
|
+
import { pushToCache, enableCache, disableCache } from '~/cache/api';
|
|
4
|
+
|
|
5
|
+
import { getPost } from '~/post/api';
|
|
6
|
+
|
|
7
|
+
describe('post/api', () => {
|
|
8
|
+
const post = { postId: userPosts.page1[0] };
|
|
9
|
+
const post2 = { postId: userPosts.page1[1] };
|
|
10
|
+
|
|
11
|
+
describe('getPost.locally', () => {
|
|
12
|
+
test('should return undefined if cache is turned off', () => {
|
|
13
|
+
expect(getPost.locally(post.postId)).toBeUndefined();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('should return undefined if post not in cache', () => {
|
|
17
|
+
enableCache();
|
|
18
|
+
|
|
19
|
+
const cachedAt = Date.now();
|
|
20
|
+
|
|
21
|
+
pushToCache(['post', 'get', post.postId], post, { cachedAt });
|
|
22
|
+
|
|
23
|
+
expect(getPost.locally(post2.postId)).toBeUndefined();
|
|
24
|
+
|
|
25
|
+
disableCache();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should return post if post in cache', () => {
|
|
29
|
+
enableCache();
|
|
30
|
+
|
|
31
|
+
const cachedAt = Date.now();
|
|
32
|
+
|
|
33
|
+
pushToCache(['post', 'get', post.postId], post, { cachedAt });
|
|
34
|
+
|
|
35
|
+
const data = getPost.locally(post.postId);
|
|
36
|
+
|
|
37
|
+
expect(data).toBeDefined();
|
|
38
|
+
expect(data?.data).toEqual(post);
|
|
39
|
+
expect(data?.cachedAt).toEqual(cachedAt);
|
|
40
|
+
|
|
41
|
+
disableCache();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('getPost', () => {
|
|
46
|
+
test('should return a post', async () => {
|
|
47
|
+
client.http.get = jest.fn().mockReturnValueOnce({ data: { posts: [post] } });
|
|
48
|
+
|
|
49
|
+
const data = await getPost(post.postId);
|
|
50
|
+
|
|
51
|
+
expect(data).toBeDefined();
|
|
52
|
+
expect(data?.data).toEqual(post);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('should put post into the cache after fetch', async () => {
|
|
56
|
+
enableCache();
|
|
57
|
+
client.http.get = jest.fn().mockReturnValueOnce({ data: { posts: [post] } });
|
|
58
|
+
|
|
59
|
+
await getPost(post.postId);
|
|
60
|
+
|
|
61
|
+
expect(getPost.locally(post.postId)?.data).toEqual(post);
|
|
62
|
+
|
|
63
|
+
disableCache();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('should return an error', async () => {
|
|
67
|
+
client.http.get = jest.fn().mockRejectedValueOnce(new Error('error'));
|
|
68
|
+
|
|
69
|
+
await expect(getPost(post.postId)).rejects.toThrow('error');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('should return an error if post in cache but api throws not found', async () => {
|
|
73
|
+
enableCache();
|
|
74
|
+
pushToCache(['post', 'get', post.postId], post, { cachedAt: Date.now() });
|
|
75
|
+
|
|
76
|
+
client.http.get = jest
|
|
77
|
+
.fn()
|
|
78
|
+
.mockRejectedValueOnce(
|
|
79
|
+
new ASCApiError('not found!', Amity.ServerError.ITEM_NOT_FOUND, Amity.ErrorLevel.ERROR),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
await expect(getPost(post.postId)).rejects.toThrow();
|
|
83
|
+
|
|
84
|
+
expect(getPost.locally(post.postId)).toBeUndefined();
|
|
85
|
+
disableCache();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { queryPosts } from '~/post/api';
|
|
2
|
+
import { client, userPosts } from '~/utils/tests';
|
|
3
|
+
|
|
4
|
+
describe('post/api', () => {
|
|
5
|
+
test('should return a post query', async () => {
|
|
6
|
+
const returnValue = [{ postId: userPosts.page1[0] }];
|
|
7
|
+
|
|
8
|
+
client.http.get = jest.fn().mockResolvedValue({
|
|
9
|
+
data: {
|
|
10
|
+
posts: returnValue,
|
|
11
|
+
paging: {},
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const data = await queryPosts({
|
|
16
|
+
targetId: userPosts.targetId,
|
|
17
|
+
targetType: userPosts.targetType,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
expect(data).toBeDefined();
|
|
21
|
+
expect(data?.data).toEqual(returnValue);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getActiveClient } from '~/client/api/activeClient';
|
|
2
2
|
|
|
3
|
-
import { toToken,
|
|
3
|
+
import { toToken, toPageRaw } from '~/core/query';
|
|
4
|
+
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* ```js
|
|
@@ -19,34 +20,33 @@ import { toToken, toPage } from '~/core/query';
|
|
|
19
20
|
* @reaction Reaction API
|
|
20
21
|
* @async
|
|
21
22
|
* */
|
|
22
|
-
export const queryReactions = async (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
reactionName?: Amity.Reaction['reactionName'];
|
|
26
|
-
page?: Amity.Page;
|
|
27
|
-
}): Promise<Amity.Paged<Amity.ReactionQuery, Amity.Page>> => {
|
|
23
|
+
export const queryReactions = async (
|
|
24
|
+
query: Amity.QueryReactions,
|
|
25
|
+
): Promise<Amity.Paged<Amity.ReactionQuery, Amity.Page<string>>> => {
|
|
28
26
|
const client = getActiveClient();
|
|
29
27
|
client.log('reaction/queryReactions', query);
|
|
30
28
|
|
|
31
29
|
const { page = { limit: 10 }, ...params } = query ?? {};
|
|
32
30
|
|
|
33
|
-
const { data } = await client.http.get<
|
|
34
|
-
`/api/
|
|
31
|
+
const { data } = await client.http.get<Amity.ReactionPayload & Amity.Pagination>(
|
|
32
|
+
`/api/v3/reactions`,
|
|
35
33
|
{
|
|
36
34
|
params: {
|
|
37
35
|
...params,
|
|
38
36
|
options: {
|
|
39
|
-
token: toToken(page, '
|
|
37
|
+
token: toToken(page, 'afterbeforeraw'),
|
|
40
38
|
},
|
|
41
39
|
},
|
|
42
40
|
},
|
|
43
41
|
);
|
|
44
42
|
|
|
45
|
-
const { paging,
|
|
46
|
-
const { reactions } =
|
|
43
|
+
const { paging, ...payload } = data;
|
|
44
|
+
const { reactions } = payload;
|
|
47
45
|
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
ingestInCache({ ...payload, reactions: reactions[0].reactors });
|
|
47
|
+
|
|
48
|
+
const nextPage = toPageRaw(paging.next);
|
|
49
|
+
const prevPage = toPageRaw(paging.previous);
|
|
50
50
|
|
|
51
51
|
return { data: reactions, prevPage, nextPage };
|
|
52
52
|
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { getActiveClient } from '~/client/api';
|
|
2
|
+
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
3
|
+
import { createEventSubscriber } from '~/core/events';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ```js
|
|
7
|
+
* import { onReactorAdded } from '@amityco/ts-sdk'
|
|
8
|
+
* const dispose = onReactorAdded('post', postId, reactor => {
|
|
9
|
+
* // ...
|
|
10
|
+
* })
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Fired when an {@link Amity.Reactor} has been added
|
|
14
|
+
*
|
|
15
|
+
* @param {@link Amity.ReactableType} referenceType
|
|
16
|
+
* @param {string} referenceId
|
|
17
|
+
* @param callback The function to call when the event was fired
|
|
18
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
19
|
+
*
|
|
20
|
+
* @category Events
|
|
21
|
+
* */
|
|
22
|
+
export const onReactorAdded = (
|
|
23
|
+
referenceType: Amity.ReactableType,
|
|
24
|
+
referenceId: Amity.Reaction['referenceId'],
|
|
25
|
+
callback: Amity.Listener<Amity.Reactor>,
|
|
26
|
+
): Amity.Unsubscriber => {
|
|
27
|
+
const client = getActiveClient();
|
|
28
|
+
|
|
29
|
+
const callbackWrapper = (
|
|
30
|
+
referenceType_: Amity.ReactableType,
|
|
31
|
+
referenceId_: Amity.Reaction['referenceId'],
|
|
32
|
+
reaction: Amity.Reactor,
|
|
33
|
+
) => {
|
|
34
|
+
if (referenceType_ === referenceType && referenceId_ === referenceId) {
|
|
35
|
+
callback(reaction);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (referenceType === 'message') {
|
|
40
|
+
const filter = (payload: Amity.MessagePayload) => {
|
|
41
|
+
if (!payload.messages[0].latestReaction) return;
|
|
42
|
+
|
|
43
|
+
const { eventName, referenceId, referenceType, userDisplayName, ...reactor } =
|
|
44
|
+
payload.messages[0].latestReaction;
|
|
45
|
+
|
|
46
|
+
if (eventName !== 'add') return;
|
|
47
|
+
|
|
48
|
+
ingestInCache(payload);
|
|
49
|
+
ingestInCache({ reactions: [reactor as Amity.Reactor] });
|
|
50
|
+
|
|
51
|
+
callbackWrapper('message', payload.messages[0].messageId, reactor);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return createEventSubscriber(client, 'reaction/onReactorAdded', 'v3.message.didUpdate', filter);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (referenceType === 'post') {
|
|
58
|
+
const filter = (payload: Amity.PostPayload & { reactor: Amity.Reactor }) => {
|
|
59
|
+
const { reactor, ...rest } = payload;
|
|
60
|
+
|
|
61
|
+
ingestInCache(rest as Amity.PostPayload);
|
|
62
|
+
ingestInCache({ reactions: [reactor] });
|
|
63
|
+
|
|
64
|
+
callbackWrapper('post', payload.posts[0].postId, reactor);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return createEventSubscriber(client, 'post.addReaction', 'post.addReaction', filter);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const filter = (payload: Amity.CommentPayload & { reactor: Amity.Reactor }) => {
|
|
71
|
+
const { reactor, ...rest } = payload;
|
|
72
|
+
|
|
73
|
+
ingestInCache(rest as Amity.CommentPayload);
|
|
74
|
+
ingestInCache({ reactions: [reactor] });
|
|
75
|
+
|
|
76
|
+
callbackWrapper('comment', payload.comments[0].commentId, reactor);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return createEventSubscriber(client, 'comment.addReaction', 'comment.addReaction', filter);
|
|
80
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { getActiveClient } from '~/client/api';
|
|
2
|
+
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
3
|
+
import { createEventSubscriber } from '~/core/events';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ```js
|
|
7
|
+
* import { onReactorRemoved } from '@amityco/ts-sdk'
|
|
8
|
+
* const dispose = onReactorRemoved('post', postId, reactor => {
|
|
9
|
+
* // ...
|
|
10
|
+
* })
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Fired when an {@link Amity.Reactor} has been removed
|
|
14
|
+
*
|
|
15
|
+
* @param {@link Amity.ReactableType} referenceType
|
|
16
|
+
* @param {string} referenceId
|
|
17
|
+
* @param callback The function to call when the event was fired
|
|
18
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
19
|
+
*
|
|
20
|
+
* @category Events
|
|
21
|
+
* */
|
|
22
|
+
export const onReactorRemoved = (
|
|
23
|
+
referenceType: Amity.ReactableType,
|
|
24
|
+
referenceId: Amity.Reaction['referenceId'],
|
|
25
|
+
callback: Amity.Listener<Amity.Reactor>,
|
|
26
|
+
): Amity.Unsubscriber => {
|
|
27
|
+
const client = getActiveClient();
|
|
28
|
+
|
|
29
|
+
const callbackWrapper = (
|
|
30
|
+
referenceType_: Amity.ReactableType,
|
|
31
|
+
referenceId_: Amity.Reaction['referenceId'],
|
|
32
|
+
reaction: Amity.Reactor,
|
|
33
|
+
) => {
|
|
34
|
+
if (referenceType_ === referenceType && referenceId_ === referenceId) {
|
|
35
|
+
callback(reaction);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (referenceType === 'message') {
|
|
40
|
+
const filter = (payload: Amity.MessagePayload) => {
|
|
41
|
+
if (!payload.messages[0].latestReaction) return;
|
|
42
|
+
|
|
43
|
+
const { eventName, referenceId, referenceType, ...reactor } =
|
|
44
|
+
payload.messages[0].latestReaction;
|
|
45
|
+
|
|
46
|
+
if (eventName !== 'remove') return;
|
|
47
|
+
|
|
48
|
+
ingestInCache(payload);
|
|
49
|
+
ingestInCache({ reactions: [reactor as Amity.Reactor] });
|
|
50
|
+
|
|
51
|
+
callbackWrapper('message', payload.messages[0].messageId, reactor);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return createEventSubscriber(
|
|
55
|
+
client,
|
|
56
|
+
'reaction/onReactorRemoved',
|
|
57
|
+
'v3.message.didUpdate',
|
|
58
|
+
filter,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (referenceType === 'post') {
|
|
63
|
+
const filter = (payload: Amity.PostPayload & { reactor: Amity.Reactor }) => {
|
|
64
|
+
const { reactor, ...rest } = payload;
|
|
65
|
+
|
|
66
|
+
ingestInCache(rest as Amity.PostPayload);
|
|
67
|
+
ingestInCache({ reactions: [reactor] });
|
|
68
|
+
|
|
69
|
+
callbackWrapper('post', payload.posts[0].postId, reactor);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return createEventSubscriber(client, 'post.removeReaction', 'post.removeReaction', filter);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const filter = (payload: Amity.CommentPayload & { reactor: Amity.Reactor }) => {
|
|
76
|
+
const { reactor, ...rest } = payload;
|
|
77
|
+
|
|
78
|
+
ingestInCache(rest as Amity.CommentPayload);
|
|
79
|
+
ingestInCache({ reactions: [reactor] });
|
|
80
|
+
|
|
81
|
+
callbackWrapper('comment', payload.comments[0].commentId, reactor);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return createEventSubscriber(client, 'comment.removeReaction', 'comment.removeReaction', filter);
|
|
85
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './liveReactions';
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/* eslint-disable no-use-before-define */
|
|
2
|
+
import { getResolver } from '~/core/model';
|
|
3
|
+
import { getActiveClient } from '~/client/api';
|
|
4
|
+
import { pushToCache, pullFromCache } from '~/cache/api';
|
|
5
|
+
import { createQuery, runQuery, queryOptions } from '~/core/query';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
COLLECTION_DEFAULT_CACHING_POLICY,
|
|
9
|
+
COLLECTION_DEFAULT_PAGINATION_LIMIT,
|
|
10
|
+
} from '~/utils/constants';
|
|
11
|
+
|
|
12
|
+
import { onReactorAdded, onReactorRemoved } from '../events';
|
|
13
|
+
import { queryReactions } from '../api';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* ```js
|
|
17
|
+
* import { liveReactions } from '@amityco/ts-sdk'
|
|
18
|
+
*
|
|
19
|
+
* let reactions = []
|
|
20
|
+
* const unsub = liveReactions({
|
|
21
|
+
* referenceId: Amity.Reaction['referenceId'],
|
|
22
|
+
* referenceType: Amity.Reaction['referenceType'],
|
|
23
|
+
* }, response => merge(reactions, response.data))
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Observe all mutations on a list of {@link Amity.Reaction} for a given target object
|
|
27
|
+
*
|
|
28
|
+
* @param referenceType the type of the target
|
|
29
|
+
* @param referenceId the ID of the target
|
|
30
|
+
* @param callback the function to call when new data are available
|
|
31
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
32
|
+
*
|
|
33
|
+
* @category Reactions Live Collection
|
|
34
|
+
*/
|
|
35
|
+
export const liveReactions = (
|
|
36
|
+
params: Amity.ReactionLiveCollection,
|
|
37
|
+
callback: Amity.LiveCollectionCallback<Amity.Reactor>,
|
|
38
|
+
config?: Amity.LiveCollectionConfig,
|
|
39
|
+
): Amity.Unsubscriber => {
|
|
40
|
+
const { log, cache } = getActiveClient();
|
|
41
|
+
|
|
42
|
+
if (!cache) {
|
|
43
|
+
console.log('For using Live Collection feature you need to enable Cache!');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const timestamp = Date.now();
|
|
47
|
+
log(`liveReactions(tmpid: ${timestamp}) > listen`);
|
|
48
|
+
|
|
49
|
+
const { limit: queryLimit, ...queryParams } = params;
|
|
50
|
+
|
|
51
|
+
const limit = queryLimit ?? COLLECTION_DEFAULT_PAGINATION_LIMIT;
|
|
52
|
+
const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config ?? {};
|
|
53
|
+
|
|
54
|
+
const disposers: Amity.Unsubscriber[] = [];
|
|
55
|
+
const cacheKey = [
|
|
56
|
+
'reaction',
|
|
57
|
+
'collection',
|
|
58
|
+
{ referenceId: params.referenceId, referenceType: params.referenceType },
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
const responder = (data: Amity.ReactionLiveCollectionCache) => {
|
|
62
|
+
const reactions: Amity.Reactor[] =
|
|
63
|
+
data.data
|
|
64
|
+
.map(reactorId => pullFromCache<Amity.Reactor>(['reaction', 'get', reactorId])!)
|
|
65
|
+
.filter(Boolean)
|
|
66
|
+
.map(({ data }) => data) ?? [];
|
|
67
|
+
|
|
68
|
+
callback({
|
|
69
|
+
onNextPage: onFetch,
|
|
70
|
+
data: reactions,
|
|
71
|
+
hasNextPage: !!data.params?.page,
|
|
72
|
+
loading: data.loading,
|
|
73
|
+
error: data.error,
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const realtimeRouter = (reaction: Amity.Reactor, action: Amity.ReactionActionType) => {
|
|
78
|
+
const collection = pullFromCache<Amity.ReactionLiveCollectionCache>(cacheKey)?.data;
|
|
79
|
+
if (!collection) return;
|
|
80
|
+
|
|
81
|
+
if (action === 'onAdded') {
|
|
82
|
+
collection.data = [...new Set([reaction.reactionId, ...collection.data])];
|
|
83
|
+
} else if (action === 'onRemoved') {
|
|
84
|
+
collection.data = collection.data.filter(p => p !== reaction.reactionId);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
pushToCache(cacheKey, collection);
|
|
88
|
+
responder(collection);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const onFetch = () => {
|
|
92
|
+
const collection = pullFromCache<Amity.ReactionLiveCollectionCache>(cacheKey)?.data;
|
|
93
|
+
|
|
94
|
+
const reactions = collection?.data ?? [];
|
|
95
|
+
|
|
96
|
+
if (reactions.length > 0 && !collection?.params?.page) return;
|
|
97
|
+
|
|
98
|
+
const query = createQuery(queryReactions, {
|
|
99
|
+
...queryParams,
|
|
100
|
+
page: collection?.params?.page ?? { limit },
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
runQuery(
|
|
104
|
+
query,
|
|
105
|
+
({ data: result, error, loading, nextPage: page }) => {
|
|
106
|
+
const data = {
|
|
107
|
+
loading,
|
|
108
|
+
error,
|
|
109
|
+
params: { page },
|
|
110
|
+
data: reactions,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
if (result) {
|
|
114
|
+
data.data = [
|
|
115
|
+
...new Set([...reactions, ...result[0].reactors.map(getResolver('reaction'))]),
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
pushToCache(cacheKey, data);
|
|
120
|
+
|
|
121
|
+
responder(data);
|
|
122
|
+
},
|
|
123
|
+
queryOptions(policy),
|
|
124
|
+
);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
disposers.push(
|
|
128
|
+
onReactorAdded(queryParams.referenceType, queryParams.referenceId, reaction =>
|
|
129
|
+
realtimeRouter(reaction, 'onAdded'),
|
|
130
|
+
),
|
|
131
|
+
onReactorRemoved(queryParams.referenceType, queryParams.referenceId, reaction =>
|
|
132
|
+
realtimeRouter(reaction, 'onRemoved'),
|
|
133
|
+
),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
onFetch();
|
|
137
|
+
|
|
138
|
+
return () => {
|
|
139
|
+
log(`liveReactions(tmpid: ${timestamp}) > dispose`);
|
|
140
|
+
disposers.forEach(fn => fn());
|
|
141
|
+
};
|
|
142
|
+
};
|
|
@@ -29,7 +29,13 @@ export const createReport = async (
|
|
|
29
29
|
|
|
30
30
|
const { data } = await client.http.post<
|
|
31
31
|
Amity.PostPayload | Amity.CommentPayload | Amity.MessagePayload | Amity.UserPayload
|
|
32
|
-
>(
|
|
32
|
+
>(
|
|
33
|
+
`/api/${
|
|
34
|
+
referenceType === 'user'
|
|
35
|
+
? `v4/me/flags/${encodeURIComponent(referenceId)}`
|
|
36
|
+
: `v3/${domainName}/${encodeURIComponent(referenceId)}/flag`
|
|
37
|
+
}`,
|
|
38
|
+
);
|
|
33
39
|
|
|
34
40
|
if (client.cache) ingestInCache(data);
|
|
35
41
|
|
|
@@ -29,7 +29,13 @@ export const deleteReport = async (
|
|
|
29
29
|
|
|
30
30
|
const { data } = await client.http.delete<
|
|
31
31
|
Amity.PostPayload | Amity.CommentPayload | Amity.MessagePayload | Amity.UserPayload
|
|
32
|
-
>(
|
|
32
|
+
>(
|
|
33
|
+
`/api/${
|
|
34
|
+
referenceType === 'user'
|
|
35
|
+
? `v4/me/flags/${encodeURIComponent(referenceId)}`
|
|
36
|
+
: `v3/${domainName}/${encodeURIComponent(referenceId)}/unflag`
|
|
37
|
+
}`,
|
|
38
|
+
);
|
|
33
39
|
|
|
34
40
|
if (client.cache) ingestInCache(data);
|
|
35
41
|
|
package/tsconfig.json
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
"include": ["./src"],
|
|
5
|
+
"exclude": ["node_modules", "**/*.spec.ts"],
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
"compilerOptions": {
|
|
8
|
+
"baseUrl": "./src",
|
|
9
|
+
"paths": {
|
|
10
|
+
"~/*": ["./*"]
|
|
11
|
+
},
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
"rootDir": "./src",
|
|
14
|
+
"outDir": "./dist",
|
|
15
|
+
"declaration": true,
|
|
16
|
+
"declarationDir": "./dist",
|
|
17
|
+
"noEmit": true,
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
"module": "es2020",
|
|
20
|
+
"target": "es2017",
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
"lib": [
|
|
23
|
+
"esnext",
|
|
24
|
+
"es7",
|
|
25
|
+
"es6",
|
|
26
|
+
"dom"
|
|
27
|
+
],
|
|
28
|
+
"resolveJsonModule": true
|
|
29
|
+
}
|
|
29
30
|
}
|