@amityco/ts-sdk 0.0.1-beta.37 → 0.0.1-beta.39
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 +3 -5
- package/dist/@types/core/events.d.ts +1 -0
- package/dist/@types/core/events.d.ts.map +1 -1
- package/dist/@types/core/model.d.ts +2 -0
- package/dist/@types/core/model.d.ts.map +1 -1
- package/dist/@types/domains/category.d.ts +7 -0
- package/dist/@types/domains/category.d.ts.map +1 -1
- package/dist/@types/domains/channel.d.ts +2 -2
- package/dist/@types/domains/channel.d.ts.map +1 -1
- package/dist/@types/domains/community.d.ts +12 -0
- package/dist/@types/domains/community.d.ts.map +1 -1
- package/dist/@types/domains/feed.d.ts +1 -2
- package/dist/@types/domains/feed.d.ts.map +1 -1
- package/dist/@types/domains/message.d.ts +1 -1
- package/dist/@types/domains/message.d.ts.map +1 -1
- package/dist/@types/domains/post.d.ts +1 -1
- package/dist/@types/domains/post.d.ts.map +1 -1
- package/dist/@types/domains/user.d.ts +8 -0
- package/dist/@types/domains/user.d.ts.map +1 -1
- package/dist/@types/global.d.ts +5 -0
- package/dist/@types/global.d.ts.map +1 -1
- package/dist/category/api/queryCategories.d.ts +1 -5
- package/dist/category/api/queryCategories.d.ts.map +1 -1
- package/dist/category/observers/index.d.ts +2 -0
- package/dist/category/observers/index.d.ts.map +1 -0
- package/dist/category/observers/liveCategories.d.ts +18 -0
- package/dist/category/observers/liveCategories.d.ts.map +1 -0
- package/dist/category/observers/tests/liveCategories.test.d.ts +2 -0
- package/dist/category/observers/tests/liveCategories.test.d.ts.map +1 -0
- package/dist/channel/api/tests/getChannels.test.d.ts +2 -0
- package/dist/channel/api/tests/getChannels.test.d.ts.map +1 -0
- package/dist/channel/api/tests/leaveChannel.test.d.ts +2 -0
- package/dist/channel/api/tests/leaveChannel.test.d.ts.map +1 -0
- package/dist/channel/observers/liveChannel.d.ts.map +1 -1
- package/dist/channel/observers/liveChannels.d.ts.map +1 -1
- package/dist/client/api/accessTokenExpiryWatcher.d.ts +4 -1
- package/dist/client/api/accessTokenExpiryWatcher.d.ts.map +1 -1
- package/dist/client/api/connectClient.d.ts.map +1 -1
- package/dist/client/api/getToken.d.ts +3 -1
- package/dist/client/api/getToken.d.ts.map +1 -1
- package/dist/client/utils/setClientToken.d.ts +3 -3
- package/dist/comment/api/deleteComment.d.ts.map +1 -1
- package/dist/comment/api/getComment.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/liveComment.d.ts +21 -0
- package/dist/comment/observers/liveComment.d.ts.map +1 -0
- package/dist/comment/observers/tests/liveComment.test.d.ts +2 -0
- package/dist/comment/observers/tests/liveComment.test.d.ts.map +1 -0
- package/dist/community/api/queryCommunities.d.ts +1 -10
- package/dist/community/api/queryCommunities.d.ts.map +1 -1
- package/dist/community/events/index.d.ts +1 -0
- package/dist/community/events/index.d.ts.map +1 -1
- package/dist/community/events/onCommunityUserChanged.d.ts +17 -0
- package/dist/community/events/onCommunityUserChanged.d.ts.map +1 -0
- package/dist/community/events/utils.d.ts +1 -0
- package/dist/community/events/utils.d.ts.map +1 -1
- package/dist/community/observers/index.d.ts +1 -0
- package/dist/community/observers/index.d.ts.map +1 -1
- package/dist/community/observers/liveCommunities.d.ts +20 -0
- package/dist/community/observers/liveCommunities.d.ts.map +1 -0
- package/dist/community/observers/tests/liveCommunities.test.d.ts +2 -0
- package/dist/community/observers/tests/liveCommunities.test.d.ts.map +1 -0
- package/dist/core/events.d.ts +3 -3
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/model/idResolvers.d.ts.map +1 -1
- package/dist/core/model/identifyModel.d.ts.map +1 -1
- package/dist/core/model/index.d.ts.map +1 -1
- package/dist/core/query/filtering.d.ts +10 -0
- package/dist/core/query/filtering.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/sorting.d.ts +6 -0
- package/dist/core/query/sorting.d.ts.map +1 -1
- package/dist/core/query/tests/paging.test.d.ts +2 -0
- package/dist/core/query/tests/paging.test.d.ts.map +1 -0
- package/dist/core/transports/http.d.ts +0 -5
- package/dist/core/transports/http.d.ts.map +1 -1
- package/dist/file/api/test/createImage.test.d.ts +2 -0
- package/dist/file/api/test/createImage.test.d.ts.map +1 -0
- package/dist/file/api/test/createVideo.test.d.ts +2 -0
- package/dist/file/api/test/createVideo.test.d.ts.map +1 -0
- package/dist/index.cjs.js +607 -252
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +599 -251
- package/dist/index.umd.js +4 -4
- package/dist/message/api/queryMessages.d.ts +2 -2
- package/dist/message/api/queryMessages.d.ts.map +1 -1
- package/dist/message/observers/liveMessage.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/livePost.d.ts +21 -0
- package/dist/post/observers/livePost.d.ts.map +1 -0
- package/dist/post/observers/livePosts.d.ts +2 -2
- package/dist/post/observers/livePosts.d.ts.map +1 -1
- package/dist/post/observers/tests/livePost.test.d.ts +2 -0
- package/dist/post/observers/tests/livePost.test.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/report/api/tests/createReport.test.d.ts +2 -0
- package/dist/report/api/tests/createReport.test.d.ts.map +1 -0
- package/dist/report/api/tests/deleteReport.test.d.ts +2 -0
- package/dist/report/api/tests/deleteReport.test.d.ts.map +1 -0
- package/dist/subChannel/api/tests/deleteSubChannel.test.d.ts +2 -0
- package/dist/subChannel/api/tests/deleteSubChannel.test.d.ts.map +1 -0
- package/dist/subChannel/api/tests/getSubChannel.test.d.ts +2 -0
- package/dist/subChannel/api/tests/getSubChannel.test.d.ts.map +1 -0
- package/dist/subChannel/api/tests/getSubChannels.test.d.ts +2 -0
- package/dist/subChannel/api/tests/getSubChannels.test.d.ts.map +1 -0
- package/dist/subChannel/api/tests/updateSubChannel.test.d.ts +2 -0
- package/dist/subChannel/api/tests/updateSubChannel.test.d.ts.map +1 -0
- package/dist/subChannel/observers/liveSubChannel.d.ts.map +1 -1
- package/dist/user/api/queryUsers.d.ts +1 -6
- package/dist/user/api/queryUsers.d.ts.map +1 -1
- package/dist/user/api/tests/updateUser.test.d.ts +2 -0
- package/dist/user/api/tests/updateUser.test.d.ts.map +1 -0
- package/dist/user/observers/index.d.ts +1 -0
- package/dist/user/observers/index.d.ts.map +1 -1
- package/dist/user/observers/liveUser.d.ts.map +1 -1
- package/dist/user/observers/liveUsers.d.ts +18 -0
- package/dist/user/observers/liveUsers.d.ts.map +1 -0
- package/dist/user/observers/tests/liveUsers.test.d.ts +2 -0
- package/dist/user/observers/tests/liveUsers.test.d.ts.map +1 -0
- package/dist/utils/liveObject.d.ts +2 -0
- package/dist/utils/liveObject.d.ts.map +1 -0
- package/dist/utils/tests/dummy/category.d.ts +64 -0
- package/dist/utils/tests/dummy/category.d.ts.map +1 -0
- package/dist/utils/tests/dummy/channel.d.ts +50 -0
- package/dist/utils/tests/dummy/channel.d.ts.map +1 -1
- package/dist/utils/tests/dummy/community.d.ts +132 -0
- package/dist/utils/tests/dummy/community.d.ts.map +1 -1
- package/dist/utils/tests/dummy/feed.d.ts +21 -0
- package/dist/utils/tests/dummy/feed.d.ts.map +1 -0
- package/dist/utils/tests/dummy/file.d.ts +3 -0
- package/dist/utils/tests/dummy/file.d.ts.map +1 -1
- package/dist/utils/tests/dummy/index.d.ts +2 -0
- package/dist/utils/tests/dummy/index.d.ts.map +1 -1
- package/dist/utils/tests/dummy/post.d.ts +11 -0
- package/dist/utils/tests/dummy/post.d.ts.map +1 -1
- package/dist/utils/tests/dummy/subChannel.d.ts +0 -20
- package/dist/utils/tests/dummy/subChannel.d.ts.map +1 -1
- package/dist/utils/tests/dummy/user.d.ts +81 -0
- package/dist/utils/tests/dummy/user.d.ts.map +1 -1
- package/dist/utils/tests/liveObject.test.d.ts +2 -0
- package/dist/utils/tests/liveObject.test.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/@types/core/events.ts +1 -0
- package/src/@types/core/model.ts +4 -0
- package/src/@types/domains/category.ts +13 -0
- package/src/@types/domains/channel.ts +2 -2
- package/src/@types/domains/community.ts +25 -0
- package/src/@types/domains/feed.ts +1 -3
- package/src/@types/domains/message.ts +4 -1
- package/src/@types/domains/post.ts +1 -1
- package/src/@types/domains/user.ts +14 -0
- package/src/@types/global.ts +5 -0
- package/src/cache/api/tests/ingestInCache.test.ts +2 -0
- package/src/category/api/queryCategories.ts +3 -5
- package/src/category/observers/index.ts +1 -0
- package/src/category/observers/liveCategories.ts +141 -0
- package/src/category/observers/tests/liveCategories.test.ts +147 -0
- package/src/channel/api/tests/createChannel.test.ts +30 -6
- package/src/channel/api/tests/getChannel.test.ts +3 -1
- package/src/channel/api/tests/getChannels.test.ts +97 -0
- package/src/channel/api/tests/joinChannel.test.ts +62 -3
- package/src/channel/api/tests/leaveChannel.test.ts +90 -0
- package/src/channel/api/tests/queryChannels.test.ts +38 -1
- package/src/channel/observers/liveChannel.ts +8 -80
- package/src/channel/observers/liveChannels.ts +29 -24
- package/src/client/api/accessTokenExpiryWatcher.ts +7 -5
- package/src/client/api/connectClient.ts +0 -2
- package/src/client/api/tests/accessTokenExpiryWatcher.test.ts +8 -5
- package/src/comment/api/deleteComment.ts +5 -5
- package/src/comment/api/getComment.ts +8 -3
- package/src/comment/observers/index.ts +1 -0
- package/src/comment/observers/liveComment.ts +43 -0
- package/src/comment/observers/tests/liveComment.test.ts +95 -0
- package/src/community/api/queryCommunities.ts +3 -10
- package/src/community/api/test/addCommunityMember.test.ts +14 -2
- package/src/community/api/test/joinCommunity.test.ts +14 -2
- package/src/community/api/test/leaveCommunity.test.ts +11 -1
- package/src/community/api/test/removeCommunityMembers.test.ts +11 -1
- package/src/community/events/index.ts +1 -0
- package/src/community/events/onCommunityUserChanged.ts +20 -0
- package/src/community/events/test/onCommunityJoined.test.ts +1 -1
- package/src/community/events/test/onCommunityUserAdded.test.ts +1 -1
- package/src/community/events/test/onCommunityUserBanned.test.ts +1 -1
- package/src/community/events/test/onCommunityUserUnbaned.test.ts +1 -1
- package/src/community/events/test/utils.test.ts +52 -3
- package/src/community/events/utils.ts +49 -0
- package/src/community/observers/index.ts +1 -0
- package/src/community/observers/liveCommunities.ts +188 -0
- package/src/community/observers/tests/liveCommunities.test.ts +284 -0
- package/src/core/model/idResolvers.ts +2 -0
- package/src/core/model/identifyModel.ts +1 -0
- package/src/core/model/index.ts +2 -0
- package/src/core/query/filtering.ts +33 -3
- package/src/core/query/paging.ts +5 -4
- package/src/core/query/sorting.ts +8 -0
- package/src/core/query/tests/paging.test.ts +66 -0
- package/src/core/transports/http.ts +17 -13
- package/src/core/transports/test/http.test.ts +49 -60
- package/src/file/api/createFile.ts +1 -1
- package/src/file/api/createImage.ts +1 -1
- package/src/file/api/createVideo.ts +1 -1
- package/src/file/api/test/createImage.test.ts +69 -0
- package/src/file/api/test/createVideo.test.ts +81 -0
- package/src/index.ts +1 -0
- package/src/message/api/queryMessages.ts +7 -7
- package/src/message/api/tests/queryMessages.test.ts +6 -6
- package/src/message/observers/liveMessage.ts +11 -83
- package/src/message/tests/observers/liveMessage.test.ts +1 -143
- package/src/message/tests/observers/liveMessages.test.ts +4 -4
- package/src/post/observers/index.ts +1 -0
- package/src/post/observers/livePost.ts +47 -0
- package/src/post/observers/livePosts.ts +14 -22
- package/src/post/observers/tests/livePost.test.ts +97 -0
- package/src/post/observers/tests/livePosts.test.ts +65 -8
- package/src/report/api/createReport.ts +1 -6
- package/src/report/api/deleteReport.ts +2 -6
- package/src/report/api/tests/createReport.test.ts +93 -0
- package/src/report/api/tests/deleteReport.test.ts +93 -0
- package/src/stream/api/tests/createStream.test.ts +3 -11
- package/src/stream/api/tests/deleteStream.test.ts +5 -3
- package/src/subChannel/api/tests/createSubChannel.test.ts +21 -14
- package/src/subChannel/api/tests/deleteSubChannel.test.ts +112 -0
- package/src/subChannel/api/tests/getSubChannel.test.ts +97 -0
- package/src/subChannel/api/tests/getSubChannels.test.ts +97 -0
- package/src/subChannel/api/tests/updateSubChannel.test.ts +86 -0
- package/src/subChannel/observers/liveSubChannel.ts +6 -56
- package/src/subChannel/observers/tests/liveSubChannel.test.ts +0 -82
- package/src/user/api/queryUsers.ts +1 -6
- package/src/user/api/tests/updateUser.test.ts +60 -0
- package/src/user/observers/index.ts +1 -0
- package/src/user/observers/liveUser.ts +9 -61
- package/src/user/observers/liveUsers.ts +162 -0
- package/src/user/observers/tests/liveUsers.test.ts +202 -0
- package/src/user/tests/observers/liveUser.test.ts +1 -154
- package/src/utils/liveObject.ts +86 -0
- package/src/utils/tests/dummy/category.ts +57 -0
- package/src/utils/tests/dummy/channel.ts +52 -1
- package/src/utils/tests/dummy/community.ts +94 -0
- package/src/utils/tests/dummy/feed.ts +13 -0
- package/src/utils/tests/dummy/file.ts +32 -0
- package/src/utils/tests/dummy/index.ts +3 -0
- package/src/utils/tests/dummy/post.ts +62 -47
- package/src/utils/tests/dummy/stream.ts +73 -73
- package/src/utils/tests/dummy/subChannel.ts +0 -22
- package/src/utils/tests/dummy/user.ts +57 -0
- package/src/utils/tests/liveObject.test.ts +172 -0
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { getActiveClient } from '~/client/api';
|
|
2
2
|
|
|
3
|
-
import { pullFromCache
|
|
3
|
+
import { pullFromCache } from '~/cache/api';
|
|
4
4
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
5
|
+
import { isInTombstone } from '~/cache/api/isInTombstone';
|
|
6
|
+
import { checkIfShouldGoesToTombstone } from '~/cache/utils';
|
|
7
|
+
import { pushToTombstone } from '~/cache/api/pushToTombstone';
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
10
|
* ```js
|
|
@@ -23,6 +26,8 @@ export const getComment = async (
|
|
|
23
26
|
const client = getActiveClient();
|
|
24
27
|
client.log('comment/getComment', commentId);
|
|
25
28
|
|
|
29
|
+
isInTombstone('comment', commentId);
|
|
30
|
+
|
|
26
31
|
let data: Amity.CommentPayload;
|
|
27
32
|
|
|
28
33
|
try {
|
|
@@ -33,8 +38,8 @@ export const getComment = async (
|
|
|
33
38
|
|
|
34
39
|
data = response.data;
|
|
35
40
|
} catch (error) {
|
|
36
|
-
if ((error as Amity.ErrorResponse)?.code
|
|
37
|
-
|
|
41
|
+
if (checkIfShouldGoesToTombstone((error as Amity.ErrorResponse)?.code)) {
|
|
42
|
+
pushToTombstone('comment', commentId);
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
throw error;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { liveObject } from '~/utils/liveObject';
|
|
2
|
+
import { getComment } from '../api';
|
|
3
|
+
import {
|
|
4
|
+
onCommentDeleted,
|
|
5
|
+
onCommentFlagged,
|
|
6
|
+
onCommentReactionAdded,
|
|
7
|
+
onCommentReactionRemoved,
|
|
8
|
+
onCommentUnflagged,
|
|
9
|
+
onCommentUpdated,
|
|
10
|
+
} from '../events';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* ```js
|
|
14
|
+
* import { liveComment } from '@amityco/ts-sdk';
|
|
15
|
+
*
|
|
16
|
+
* let comment;
|
|
17
|
+
*
|
|
18
|
+
* const unsubscribe = liveComment(commentId, response => {
|
|
19
|
+
* comment = response.data;
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Observe all mutation on a given {@link Amity.Comment}
|
|
24
|
+
*
|
|
25
|
+
* @param commentId the ID of the comment to observe
|
|
26
|
+
* @param callback the function to call when new data are available
|
|
27
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the comment
|
|
28
|
+
*
|
|
29
|
+
* @category Comment Live Object
|
|
30
|
+
*/
|
|
31
|
+
export const liveComment = (
|
|
32
|
+
commentId: Amity.Comment['commentId'],
|
|
33
|
+
callback: Amity.LiveObjectCallback<Amity.Comment>,
|
|
34
|
+
): Amity.Unsubscriber => {
|
|
35
|
+
return liveObject(commentId, callback, 'commentId', getComment, [
|
|
36
|
+
onCommentDeleted,
|
|
37
|
+
onCommentFlagged,
|
|
38
|
+
onCommentReactionAdded,
|
|
39
|
+
onCommentReactionRemoved,
|
|
40
|
+
onCommentUnflagged,
|
|
41
|
+
onCommentUpdated,
|
|
42
|
+
]);
|
|
43
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { disableCache, enableCache } from '~/cache/api';
|
|
2
|
+
import {
|
|
3
|
+
pause,
|
|
4
|
+
client,
|
|
5
|
+
connectClient,
|
|
6
|
+
disconnectClient,
|
|
7
|
+
generateComment,
|
|
8
|
+
user12,
|
|
9
|
+
} from '~/utils/tests';
|
|
10
|
+
import { getFutureDate } from '~/core/model';
|
|
11
|
+
import { liveComment } from '../liveComment';
|
|
12
|
+
|
|
13
|
+
describe('liveComment', () => {
|
|
14
|
+
const comment = generateComment();
|
|
15
|
+
const response = { data: { comments: [comment] } };
|
|
16
|
+
|
|
17
|
+
beforeAll(async () => {
|
|
18
|
+
await connectClient();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterAll(async () => {
|
|
22
|
+
await disconnectClient();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
beforeEach(enableCache);
|
|
26
|
+
|
|
27
|
+
afterEach(disableCache);
|
|
28
|
+
|
|
29
|
+
const events: [string, keyof Amity.MqttCommentEvents][] = [
|
|
30
|
+
['should get update on comment.updated event', 'comment.updated'],
|
|
31
|
+
['should get update on comment.deleted event', 'comment.deleted'],
|
|
32
|
+
['should get update on comment.flagged event', 'comment.flagged'],
|
|
33
|
+
['should get update on comment.unflagged event', 'comment.unflagged'],
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
test.each(events)('%s', async (test, event) => {
|
|
37
|
+
const update = {
|
|
38
|
+
...comment,
|
|
39
|
+
data: { text: 'new-text' },
|
|
40
|
+
updatedAt: getFutureDate(comment.updatedAt),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const callback = jest.fn();
|
|
44
|
+
client.http.get = jest.fn().mockResolvedValue(response);
|
|
45
|
+
|
|
46
|
+
liveComment(comment.commentId, callback);
|
|
47
|
+
await pause();
|
|
48
|
+
client.emitter.emit(event, { comments: [update] });
|
|
49
|
+
await pause();
|
|
50
|
+
|
|
51
|
+
expect(callback).toHaveBeenCalledTimes(3);
|
|
52
|
+
expect(callback).toHaveBeenNthCalledWith(
|
|
53
|
+
3,
|
|
54
|
+
expect.objectContaining({
|
|
55
|
+
data: update,
|
|
56
|
+
loading: false,
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test.each([
|
|
62
|
+
['should get update on comment.addReaction event', 'comment.addReaction'],
|
|
63
|
+
['should get update on comment.removeReaction event', 'comment.removeReaction'],
|
|
64
|
+
] as [string, keyof Amity.MqttCommentEvents][])('%s', async (test, event) => {
|
|
65
|
+
const update = {
|
|
66
|
+
...comment,
|
|
67
|
+
data: { text: 'new-text' },
|
|
68
|
+
updatedAt: getFutureDate(comment.updatedAt),
|
|
69
|
+
};
|
|
70
|
+
const reactor: Amity.Reaction = {
|
|
71
|
+
createdAt: getFutureDate(comment.updatedAt),
|
|
72
|
+
reactionId: 'like',
|
|
73
|
+
reactionName: 'like',
|
|
74
|
+
referenceId: comment.commentId,
|
|
75
|
+
referenceType: 'comment',
|
|
76
|
+
userId: user12.userId,
|
|
77
|
+
};
|
|
78
|
+
const callback = jest.fn();
|
|
79
|
+
client.http.get = jest.fn().mockResolvedValue(response);
|
|
80
|
+
|
|
81
|
+
liveComment(comment.commentId, callback);
|
|
82
|
+
await pause();
|
|
83
|
+
client.emitter.emit(event, { comments: [update], reactor });
|
|
84
|
+
await pause();
|
|
85
|
+
|
|
86
|
+
expect(callback).toHaveBeenCalledTimes(3);
|
|
87
|
+
expect(callback).toHaveBeenNthCalledWith(
|
|
88
|
+
3,
|
|
89
|
+
expect.objectContaining({
|
|
90
|
+
data: update,
|
|
91
|
+
loading: false,
|
|
92
|
+
}),
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -22,16 +22,9 @@ import { unpackCommunityPayload } from '../utils';
|
|
|
22
22
|
* @category Community API
|
|
23
23
|
* @async
|
|
24
24
|
*/
|
|
25
|
-
export const queryCommunities = async (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
categoryId?: Amity.Category['categoryId'];
|
|
29
|
-
isDeleted?: Amity.Community['isDeleted'];
|
|
30
|
-
hasFlag?: boolean;
|
|
31
|
-
tags?: Amity.Taggable['tags'];
|
|
32
|
-
sortBy?: 'displayName' | 'firstCreated' | 'lastCreated';
|
|
33
|
-
page?: Amity.Page;
|
|
34
|
-
}): Promise<Amity.Cached<Amity.Paged<Amity.Community>>> => {
|
|
25
|
+
export const queryCommunities = async (
|
|
26
|
+
query?: Amity.QueryCommunities,
|
|
27
|
+
): Promise<Amity.Cached<Amity.Paged<Amity.Community>>> => {
|
|
35
28
|
const client = getActiveClient();
|
|
36
29
|
client.log('channel/queryCommunities', query);
|
|
37
30
|
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { disableCache, enableCache, pullFromCache } from '~/cache/api';
|
|
2
2
|
import { getResolver } from '~/core/model';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
client,
|
|
6
|
+
connectClient,
|
|
7
|
+
disconnectClient,
|
|
8
|
+
community11,
|
|
9
|
+
communityUser11,
|
|
10
|
+
user11,
|
|
11
|
+
} from '~/utils/tests';
|
|
5
12
|
|
|
6
13
|
import { onCommunityUserAdded } from '../../events';
|
|
7
14
|
import { addCommunityMembers } from '../addCommunityMembers';
|
|
@@ -21,6 +28,9 @@ const resolvedPostValue = {
|
|
|
21
28
|
};
|
|
22
29
|
|
|
23
30
|
describe('addCommunityMembers', () => {
|
|
31
|
+
beforeAll(connectClient);
|
|
32
|
+
afterAll(disconnectClient);
|
|
33
|
+
|
|
24
34
|
test('should return true value', async () => {
|
|
25
35
|
client.http.post = jest.fn().mockResolvedValue(resolvedPostValue);
|
|
26
36
|
|
|
@@ -66,6 +76,8 @@ describe('addCommunityMembers', () => {
|
|
|
66
76
|
|
|
67
77
|
await addCommunityMembers(communityToAddUserTo.communityId, [userToAdd.userId]);
|
|
68
78
|
|
|
69
|
-
await expect(callback).resolves.toEqual(
|
|
79
|
+
await expect(callback).resolves.toEqual(
|
|
80
|
+
expect.objectContaining({ ...communityToAddUserTo, isJoined: true }),
|
|
81
|
+
);
|
|
70
82
|
});
|
|
71
83
|
});
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { disableCache, enableCache, pullFromCache } from '~/cache/api';
|
|
2
2
|
import { getResolver } from '~/core/model';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
client,
|
|
6
|
+
connectClient,
|
|
7
|
+
disconnectClient,
|
|
8
|
+
community11,
|
|
9
|
+
communityUser11,
|
|
10
|
+
user11,
|
|
11
|
+
} from '~/utils/tests';
|
|
5
12
|
|
|
6
13
|
import { onCommunityJoined } from '../../events';
|
|
7
14
|
import { joinCommunity } from '../joinCommunity';
|
|
@@ -21,6 +28,9 @@ const resolvedPostValue = {
|
|
|
21
28
|
};
|
|
22
29
|
|
|
23
30
|
describe('joinCommunity', () => {
|
|
31
|
+
beforeAll(connectClient);
|
|
32
|
+
afterAll(disconnectClient);
|
|
33
|
+
|
|
24
34
|
test('should return true value', async () => {
|
|
25
35
|
client.http.post = jest.fn().mockResolvedValue(resolvedPostValue);
|
|
26
36
|
|
|
@@ -65,6 +75,8 @@ describe('joinCommunity', () => {
|
|
|
65
75
|
|
|
66
76
|
await joinCommunity(communityToJoin.communityId);
|
|
67
77
|
|
|
68
|
-
await expect(callback).resolves.toEqual(
|
|
78
|
+
await expect(callback).resolves.toEqual(
|
|
79
|
+
expect.objectContaining({ ...communityToJoin, isJoined: true }),
|
|
80
|
+
);
|
|
69
81
|
});
|
|
70
82
|
});
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { disableCache, enableCache, pullFromCache, pushToCache } from '~/cache/api';
|
|
2
2
|
import { getResolver } from '~/core/model';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
client,
|
|
5
|
+
connectClient,
|
|
6
|
+
disconnectClient,
|
|
7
|
+
community11,
|
|
8
|
+
communityUser11,
|
|
9
|
+
user11,
|
|
10
|
+
} from '~/utils/tests';
|
|
4
11
|
|
|
5
12
|
import { leaveCommunity } from '../leaveCommunity';
|
|
6
13
|
import { onCommunityLeft } from '../../events';
|
|
@@ -21,6 +28,9 @@ const resolvedDeleteValue = {
|
|
|
21
28
|
};
|
|
22
29
|
|
|
23
30
|
describe('leaveCommunity', () => {
|
|
31
|
+
beforeAll(connectClient);
|
|
32
|
+
afterAll(disconnectClient);
|
|
33
|
+
|
|
24
34
|
test('should return true value', async () => {
|
|
25
35
|
client.http.delete = jest.fn().mockResolvedValueOnce(resolvedDeleteValue);
|
|
26
36
|
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { disableCache, enableCache, pullFromCache } from '~/cache/api';
|
|
2
2
|
import { getResolver } from '~/core/model';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
client,
|
|
6
|
+
community11,
|
|
7
|
+
communityUser11,
|
|
8
|
+
user11,
|
|
9
|
+
connectClient,
|
|
10
|
+
disconnectClient,
|
|
11
|
+
} from '~/utils/tests';
|
|
5
12
|
|
|
6
13
|
import { onCommunityUserRemoved } from '../../events';
|
|
7
14
|
import { removeCommunityMembers } from '../removeCommunityMembers';
|
|
@@ -22,6 +29,9 @@ const resolvedDeleteValue = {
|
|
|
22
29
|
};
|
|
23
30
|
|
|
24
31
|
describe('removeCommunityMembers', () => {
|
|
32
|
+
beforeAll(connectClient);
|
|
33
|
+
afterAll(disconnectClient);
|
|
34
|
+
|
|
25
35
|
test('should return true value', async () => {
|
|
26
36
|
client.http.delete = jest.fn().mockResolvedValue(resolvedDeleteValue);
|
|
27
37
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createCommunityMemberEventSubscriber } from './utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ```js
|
|
5
|
+
* import { onCommunityUserChanged } from '@amityco/ts-sdk'
|
|
6
|
+
* const dispose = onCommunityUserChanged((community, member) => {
|
|
7
|
+
* // ...
|
|
8
|
+
* })
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Fired when a user has been added or removed from a {@link Amity.Community}
|
|
12
|
+
*
|
|
13
|
+
* @param callback The function to call when the event was fired
|
|
14
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
15
|
+
*
|
|
16
|
+
* @category Community Events
|
|
17
|
+
*/
|
|
18
|
+
export const onCommunityUserChanged = (
|
|
19
|
+
callback: (community: Amity.Community, member: Amity.Membership<'community'>) => void,
|
|
20
|
+
) => createCommunityMemberEventSubscriber('community.userChanged', callback);
|
|
@@ -35,7 +35,7 @@ describe('onCommunityJoined', () => {
|
|
|
35
35
|
unsub();
|
|
36
36
|
|
|
37
37
|
expect(callback).toHaveBeenCalled();
|
|
38
|
-
expect(callback).toHaveBeenCalledWith(communityToJoin, joinedUser);
|
|
38
|
+
expect(callback).toHaveBeenCalledWith({ ...communityToJoin, isJoined: true }, joinedUser);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
test('it should got nothing if we did unsubscribe before got event', () => {
|
|
@@ -35,7 +35,7 @@ describe('onCommunityUserAdded', () => {
|
|
|
35
35
|
unsub();
|
|
36
36
|
|
|
37
37
|
expect(callback).toHaveBeenCalled();
|
|
38
|
-
expect(callback).toHaveBeenCalledWith(communityToAddUserTo, userToAdd);
|
|
38
|
+
expect(callback).toHaveBeenCalledWith({ ...communityToAddUserTo, isJoined: true }, userToAdd);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
test('it should got nothing if we did unsubscribe before got event', () => {
|
|
@@ -35,7 +35,7 @@ describe('onCommunityUserBanned', () => {
|
|
|
35
35
|
unsub();
|
|
36
36
|
|
|
37
37
|
expect(callback).toHaveBeenCalled();
|
|
38
|
-
expect(callback).toHaveBeenCalledWith(community, banedUser);
|
|
38
|
+
expect(callback).toHaveBeenCalledWith({ ...community, isJoined: true }, banedUser);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
test('it should got nothing if we did unsubscribe before got event', () => {
|
|
@@ -35,7 +35,7 @@ describe('onCommunityUserUnbanned', () => {
|
|
|
35
35
|
unsub();
|
|
36
36
|
|
|
37
37
|
expect(callback).toHaveBeenCalled();
|
|
38
|
-
expect(callback).toHaveBeenCalledWith(community, unbannedUser);
|
|
38
|
+
expect(callback).toHaveBeenCalledWith({ ...community, isJoined: true }, unbannedUser);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
test('it should got nothing if we did unsubscribe before got event', () => {
|
|
@@ -10,7 +10,11 @@ import {
|
|
|
10
10
|
user11,
|
|
11
11
|
} from '~/utils/tests';
|
|
12
12
|
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
createCommunityEventSubscriber,
|
|
15
|
+
createCommunityMemberEventSubscriber,
|
|
16
|
+
updateMembershipStatus,
|
|
17
|
+
} from '../utils';
|
|
14
18
|
|
|
15
19
|
const event = 'community.created';
|
|
16
20
|
const communityToCreate = community11;
|
|
@@ -115,7 +119,7 @@ describe('createCommunityMemberEventSubscriber', () => {
|
|
|
115
119
|
unsub();
|
|
116
120
|
|
|
117
121
|
expect(callback).toHaveBeenCalled();
|
|
118
|
-
expect(callback).toHaveBeenCalledWith(communityToAddUser, addedUser);
|
|
122
|
+
expect(callback).toHaveBeenCalledWith({ ...communityToAddUser, isJoined: true }, addedUser);
|
|
119
123
|
});
|
|
120
124
|
|
|
121
125
|
test('it should trigger callback with correctly params when cache is enabled', () => {
|
|
@@ -128,7 +132,7 @@ describe('createCommunityMemberEventSubscriber', () => {
|
|
|
128
132
|
unsub();
|
|
129
133
|
|
|
130
134
|
expect(callback).toHaveBeenCalled();
|
|
131
|
-
expect(callback).toHaveBeenCalledWith(
|
|
135
|
+
expect(callback).toHaveBeenCalledWith({ ...communityToCreate, isJoined: true }, addedUser);
|
|
132
136
|
|
|
133
137
|
disableCache();
|
|
134
138
|
});
|
|
@@ -156,3 +160,48 @@ describe('createCommunityMemberEventSubscriber', () => {
|
|
|
156
160
|
disableCache();
|
|
157
161
|
});
|
|
158
162
|
});
|
|
163
|
+
|
|
164
|
+
describe('updateMembershipStatus', () => {
|
|
165
|
+
beforeAll(connectClient);
|
|
166
|
+
afterAll(disconnectClient);
|
|
167
|
+
|
|
168
|
+
const communities = [
|
|
169
|
+
{
|
|
170
|
+
communityId: 'test-membership',
|
|
171
|
+
} as Amity.Community,
|
|
172
|
+
];
|
|
173
|
+
|
|
174
|
+
const getCommunityUsers = (membership = 'member') => [
|
|
175
|
+
{
|
|
176
|
+
communityId: 'test-membership',
|
|
177
|
+
communityMembership: membership,
|
|
178
|
+
userId: client?.userId || 'test',
|
|
179
|
+
} as Amity.Membership<'community'>,
|
|
180
|
+
{
|
|
181
|
+
communityId: 'test-membership',
|
|
182
|
+
communityMembership: 'member',
|
|
183
|
+
userId: 'some-other-user',
|
|
184
|
+
} as Amity.Membership<'community'>,
|
|
185
|
+
];
|
|
186
|
+
|
|
187
|
+
test('it should update isJoined status to true if membership member', () => {
|
|
188
|
+
const expected = { ...communities[0], isJoined: true };
|
|
189
|
+
const recieved = updateMembershipStatus(communities, getCommunityUsers())[0];
|
|
190
|
+
|
|
191
|
+
expect(recieved).toStrictEqual(expected);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('it should update isJoined status to true if membership banned', () => {
|
|
195
|
+
const expected = { ...communities[0], isJoined: true };
|
|
196
|
+
const recieved = updateMembershipStatus(communities, getCommunityUsers('banned'))[0];
|
|
197
|
+
|
|
198
|
+
expect(recieved).toStrictEqual(expected);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test('it should update isJoined status to false if membership none', () => {
|
|
202
|
+
const expected = { ...communities[0], isJoined: false };
|
|
203
|
+
const recieved = updateMembershipStatus(communities, getCommunityUsers('none'))[0];
|
|
204
|
+
|
|
205
|
+
expect(recieved).toStrictEqual(expected);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getActiveClient } from '~/client/api';
|
|
2
|
+
import { getActiveUser } from '~/client/api/activeUser';
|
|
2
3
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
3
4
|
import { createEventSubscriber } from '~/core/events';
|
|
4
5
|
import { pullFromCache } from '~/cache/api';
|
|
@@ -35,6 +36,43 @@ export const createCommunityEventSubscriber = (
|
|
|
35
36
|
return createEventSubscriber(client, event, event, filter);
|
|
36
37
|
};
|
|
37
38
|
|
|
39
|
+
/*
|
|
40
|
+
* verifies membership status
|
|
41
|
+
*/
|
|
42
|
+
function isMember(membership: Amity.Membership<'community'>['communityMembership']): boolean {
|
|
43
|
+
return membership !== 'none';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/*
|
|
47
|
+
* checks if currently logged in user is part of the community
|
|
48
|
+
*/
|
|
49
|
+
function isCurrentUserPartOfCommunity(
|
|
50
|
+
c: Amity.Community,
|
|
51
|
+
m: Amity.Membership<'community'>,
|
|
52
|
+
): boolean {
|
|
53
|
+
const { userId } = getActiveUser();
|
|
54
|
+
|
|
55
|
+
return c.communityId === m.communityId && m.userId === userId;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/*
|
|
59
|
+
* For mqtt events server will not send user specific data as it's broadcasted
|
|
60
|
+
* to multiple users. Althought this can be calculated, it's not scalable. Hence
|
|
61
|
+
* the client SDK needs to calculate this info
|
|
62
|
+
*/
|
|
63
|
+
export function updateMembershipStatus(
|
|
64
|
+
communities: Amity.Community[],
|
|
65
|
+
communityUsers: Amity.Membership<'community'>[],
|
|
66
|
+
): Amity.Community[] {
|
|
67
|
+
return communities.map(c => {
|
|
68
|
+
const isJoined = communityUsers.some(
|
|
69
|
+
m => isCurrentUserPartOfCommunity(c, m) && isMember(m.communityMembership),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return { ...c, isJoined };
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
38
76
|
function hasPermission(
|
|
39
77
|
member: Amity.Membership<'community'>,
|
|
40
78
|
payload: {
|
|
@@ -88,7 +126,18 @@ export const createCommunityMemberEventSubscriber = (
|
|
|
88
126
|
|
|
89
127
|
const filter = (payload: Amity.CommunityMembershipPayload) => {
|
|
90
128
|
const communityPayload = unpackCommunityPayload(payload);
|
|
129
|
+
const { communities } = communityPayload;
|
|
130
|
+
|
|
91
131
|
const preparedPayload = prepareMembershipPayload(communityPayload, 'communityUsers');
|
|
132
|
+
const { communityUsers } = preparedPayload;
|
|
133
|
+
|
|
134
|
+
/*
|
|
135
|
+
* community.isJoined is not part of the communityMembership payload, and needs
|
|
136
|
+
* to be calculated based on the communityMembership value
|
|
137
|
+
*/
|
|
138
|
+
const communitiesWithMembership = updateMembershipStatus(communities, communityUsers);
|
|
139
|
+
|
|
140
|
+
preparedPayload.communities = communitiesWithMembership;
|
|
92
141
|
|
|
93
142
|
if (!client.cache) {
|
|
94
143
|
// TODO: here we are missing specific properties here!
|