@amityco/ts-sdk 0.0.1-beta.36 → 0.0.1-beta.38
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/errors.d.ts +1 -0
- package/dist/@types/core/errors.d.ts.map +1 -1
- package/dist/@types/core/events.d.ts +5 -0
- package/dist/@types/core/events.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/client.d.ts +21 -1
- package/dist/@types/domains/client.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/global.d.ts +12 -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/client/api/accessTokenExpiryWatcher.d.ts +7 -0
- package/dist/client/api/accessTokenExpiryWatcher.d.ts.map +1 -0
- package/dist/client/api/activeUser.d.ts +1 -1
- package/dist/client/api/activeUser.d.ts.map +1 -1
- 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/client/api/disconnectClient.d.ts +0 -1
- package/dist/client/api/disconnectClient.d.ts.map +1 -1
- package/dist/client/api/getToken.d.ts +7 -5
- package/dist/client/api/getToken.d.ts.map +1 -1
- package/dist/client/api/index.d.ts +1 -0
- package/dist/client/api/index.d.ts.map +1 -1
- package/dist/client/api/renewal.d.ts +2 -0
- package/dist/client/api/renewal.d.ts.map +1 -0
- package/dist/client/api/setSessionState.d.ts +12 -0
- package/dist/client/api/setSessionState.d.ts.map +1 -0
- package/dist/client/api/terminateClient.d.ts +9 -0
- package/dist/client/api/terminateClient.d.ts.map +1 -0
- package/dist/client/api/tests/accessTokenExpiryWatcher.test.d.ts +2 -0
- package/dist/client/api/tests/accessTokenExpiryWatcher.test.d.ts.map +1 -0
- package/dist/client/api/tests/connectClient.test.d.ts +2 -0
- package/dist/client/api/tests/connectClient.test.d.ts.map +1 -0
- package/dist/client/api/tests/disconnectClient.test.d.ts +2 -0
- package/dist/client/api/tests/disconnectClient.test.d.ts.map +1 -0
- package/dist/client/api/tests/getToken.test.d.ts +2 -0
- package/dist/client/api/tests/getToken.test.d.ts.map +1 -0
- package/dist/client/api/tests/renewal.test.d.ts +2 -0
- package/dist/client/api/tests/renewal.test.d.ts.map +1 -0
- package/dist/client/api/tests/setSessionState.test.d.ts +2 -0
- package/dist/client/api/tests/setSessionState.test.d.ts.map +1 -0
- package/dist/client/api/tests/terminateClient.test.d.ts +2 -0
- package/dist/client/api/tests/terminateClient.test.d.ts.map +1 -0
- package/dist/client/events/index.d.ts +1 -0
- package/dist/client/events/index.d.ts.map +1 -1
- package/dist/client/events/onClientBanned.d.ts +1 -1
- package/dist/client/events/onClientBanned.d.ts.map +1 -1
- package/dist/client/events/onSessionStateChange.d.ts +17 -0
- package/dist/client/events/onSessionStateChange.d.ts.map +1 -0
- package/dist/client/events/onTokenExpired.d.ts +17 -0
- package/dist/client/events/onTokenExpired.d.ts.map +1 -0
- package/dist/client/events/onTokenTerminated.d.ts +11 -0
- package/dist/client/events/onTokenTerminated.d.ts.map +1 -0
- package/dist/client/events/tests/onSessionStateChange.test.d.ts +2 -0
- package/dist/client/events/tests/onSessionStateChange.test.d.ts.map +1 -0
- package/dist/client/events/tests/onTokenTerminated.test.d.ts +2 -0
- package/dist/client/events/tests/onTokenTerminated.test.d.ts.map +1 -0
- package/dist/client/utils/setClientToken.d.ts +19 -0
- package/dist/client/utils/setClientToken.d.ts.map +1 -0
- package/dist/comment/api/deleteComment.d.ts.map +1 -1
- package/dist/comment/api/getComment.d.ts.map +1 -1
- package/dist/comment/events/tests/onCommentDeleted.test.d.ts +2 -0
- package/dist/comment/events/tests/onCommentDeleted.test.d.ts.map +1 -0
- 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/query/sorting.d.ts +6 -0
- package/dist/core/query/sorting.d.ts.map +1 -1
- package/dist/core/subscription.d.ts.map +1 -1
- 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 +944 -332
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +937 -332
- package/dist/index.umd.js +4 -4
- 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/tests/livePost.test.d.ts +2 -0
- package/dist/post/observers/tests/livePost.test.d.ts.map +1 -0
- package/dist/stream/api/tests/createStream.test.d.ts +2 -0
- package/dist/stream/api/tests/createStream.test.d.ts.map +1 -0
- package/dist/stream/api/tests/deleteStream.test.d.ts +2 -0
- package/dist/stream/api/tests/deleteStream.test.d.ts.map +1 -0
- package/dist/stream/api/tests/getStream.test.d.ts +2 -0
- package/dist/stream/api/tests/getStream.test.d.ts.map +1 -0
- package/dist/subChannel/api/tests/createSubChannel.test.d.ts +2 -0
- package/dist/subChannel/api/tests/createSubChannel.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/tests/updateUser.test.d.ts +2 -0
- package/dist/user/api/tests/updateUser.test.d.ts.map +1 -0
- package/dist/user/observers/liveUser.d.ts.map +1 -1
- package/dist/utils/constants.d.ts +2 -0
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/liveObject.d.ts +2 -0
- package/dist/utils/liveObject.d.ts.map +1 -0
- package/dist/utils/tests/client.d.ts.map +1 -1
- 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/file.d.ts +3 -0
- package/dist/utils/tests/dummy/file.d.ts.map +1 -1
- package/dist/utils/tests/dummy/index.d.ts +3 -0
- package/dist/utils/tests/dummy/index.d.ts.map +1 -1
- package/dist/utils/tests/dummy/post.d.ts +2 -0
- package/dist/utils/tests/dummy/post.d.ts.map +1 -1
- package/dist/utils/tests/dummy/stream.d.ts +83 -0
- package/dist/utils/tests/dummy/stream.d.ts.map +1 -0
- package/dist/utils/tests/dummy/token.d.ts +11 -0
- package/dist/utils/tests/dummy/token.d.ts.map +1 -0
- package/dist/utils/tests/dummy/user.d.ts +18 -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/errors.ts +1 -0
- package/src/@types/core/events.ts +7 -0
- package/src/@types/domains/category.ts +13 -0
- package/src/@types/domains/client.ts +45 -1
- package/src/@types/domains/community.ts +25 -0
- package/src/@types/global.ts +19 -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/client/api/accessTokenExpiryWatcher.ts +85 -0
- package/src/client/api/activeUser.ts +1 -1
- package/src/client/api/connectClient.ts +105 -24
- package/src/client/api/createClient.ts +9 -1
- package/src/client/api/disconnectClient.ts +26 -2
- package/src/client/api/getToken.ts +6 -6
- package/src/client/api/index.ts +2 -0
- package/src/client/api/renewal.ts +74 -0
- package/src/client/api/setSessionState.ts +75 -0
- package/src/client/api/terminateClient.ts +20 -0
- package/src/client/api/tests/accessTokenExpiryWatcher.test.ts +132 -0
- package/src/client/api/tests/connectClient.test.ts +84 -0
- package/src/client/api/tests/createClient.test.ts +8 -0
- package/src/client/api/tests/disconnectClient.test.ts +78 -0
- package/src/client/api/tests/getToken.test.ts +36 -0
- package/src/client/api/tests/renewal.test.ts +67 -0
- package/src/client/api/tests/setSessionState.test.ts +62 -0
- package/src/client/api/tests/terminateClient.test.ts +29 -0
- package/src/client/events/index.ts +2 -0
- package/src/client/events/onClientBanned.ts +12 -5
- package/src/client/events/onSessionStateChange.ts +31 -0
- package/src/client/events/onTokenExpired.ts +26 -0
- package/src/client/events/onTokenTerminated.ts +20 -0
- package/src/client/events/tests/onSessionStateChange.test.ts +37 -0
- package/src/client/events/tests/onTokenTerminated.test.ts +37 -0
- package/src/client/tests/connectClient.test.ts +6 -1
- package/src/client/utils/setClientToken.ts +39 -0
- package/src/comment/api/deleteComment.ts +5 -5
- package/src/comment/api/getComment.ts +8 -3
- package/src/comment/events/tests/onCommentDeleted.test.ts +27 -0
- 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/events.ts +5 -3
- package/src/core/query/sorting.ts +8 -0
- package/src/core/subscription.ts +3 -1
- package/src/core/transports/http.ts +52 -13
- package/src/core/transports/mqtt.ts +1 -1
- package/src/core/transports/test/http.test.ts +49 -60
- package/src/external/api/createUserToken.ts +2 -2
- 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/observers/liveMessage.ts +11 -83
- package/src/message/tests/observers/liveMessage.test.ts +1 -143
- package/src/post/observers/index.ts +1 -0
- package/src/post/observers/livePost.ts +47 -0
- package/src/post/observers/tests/livePost.test.ts +97 -0
- package/src/stream/api/tests/createStream.test.ts +22 -0
- package/src/stream/api/tests/deleteStream.test.ts +50 -0
- package/src/stream/api/tests/getStream.test.ts +23 -0
- package/src/subChannel/api/tests/createSubChannel.test.ts +110 -0
- 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/tests/updateUser.test.ts +60 -0
- package/src/user/observers/liveUser.ts +9 -61
- package/src/user/tests/observers/liveUser.test.ts +1 -154
- package/src/utils/constants.ts +2 -0
- package/src/utils/liveObject.ts +86 -0
- package/src/utils/tests/client.ts +18 -2
- package/src/utils/tests/dummy/category.ts +57 -0
- package/src/utils/tests/dummy/channel.ts +51 -1
- package/src/utils/tests/dummy/community.ts +94 -0
- package/src/utils/tests/dummy/file.ts +32 -0
- package/src/utils/tests/dummy/index.ts +4 -0
- package/src/utils/tests/dummy/post.ts +56 -45
- package/src/utils/tests/dummy/stream.ts +86 -0
- package/src/utils/tests/dummy/token.ts +12 -0
- package/src/utils/tests/dummy/user.ts +21 -0
- package/src/utils/tests/liveObject.test.ts +172 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { disableCache, enableCache, pushToCache, pullFromCache } from '~/cache/api';
|
|
2
|
+
import { client, getChannelsResponse } from '~/utils/tests';
|
|
3
|
+
|
|
4
|
+
import { getChannels } from '../getChannels';
|
|
5
|
+
import { prepareChannelPayload } from '~/channel/utils';
|
|
6
|
+
import { ASCApiError } from '~/core/errors';
|
|
7
|
+
|
|
8
|
+
const channelIds = [
|
|
9
|
+
getChannelsResponse.data.channels[0].channelId,
|
|
10
|
+
getChannelsResponse.data.channels[1].channelId,
|
|
11
|
+
getChannelsResponse.data.channels[2].channelId,
|
|
12
|
+
];
|
|
13
|
+
const channel = prepareChannelPayload(getChannelsResponse.data).channels;
|
|
14
|
+
|
|
15
|
+
describe('getChannels', () => {
|
|
16
|
+
// integration_test_id: ced0cd3b-1714-499d-862e-0284da1c8bb1
|
|
17
|
+
test('it should return a valid channels', async () => {
|
|
18
|
+
client.http.get = jest.fn().mockResolvedValueOnce(getChannelsResponse);
|
|
19
|
+
|
|
20
|
+
const expected = channel;
|
|
21
|
+
const { data: received } = await getChannels(channelIds);
|
|
22
|
+
|
|
23
|
+
expect(received).toStrictEqual(expected);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('it should update cache after fetch from server', async () => {
|
|
27
|
+
enableCache();
|
|
28
|
+
client.http.get = jest.fn().mockResolvedValue(getChannelsResponse);
|
|
29
|
+
await getChannels(channelIds);
|
|
30
|
+
|
|
31
|
+
const data = getChannels.locally(channelIds);
|
|
32
|
+
|
|
33
|
+
expect(data).toBeDefined();
|
|
34
|
+
expect(data?.data).toEqual(channel);
|
|
35
|
+
|
|
36
|
+
disableCache();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// integration_test_id: 344a8f3d-0e54-4eb9-850f-80283e638352
|
|
40
|
+
test('it should return an error 400400 when try to get channels by invalid channel id', async () => {
|
|
41
|
+
const error = new ASCApiError(
|
|
42
|
+
'Some channel not found',
|
|
43
|
+
Amity.ServerError.ITEM_NOT_FOUND,
|
|
44
|
+
Amity.ErrorLevel.ERROR,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
client.http.get = jest.fn().mockRejectedValueOnce(error);
|
|
48
|
+
|
|
49
|
+
await expect(getChannels(channelIds)).rejects.toThrow(error);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('getChannels locally', () => {
|
|
54
|
+
test('it should return data from cache', () => {
|
|
55
|
+
enableCache();
|
|
56
|
+
|
|
57
|
+
const cachedAt = Date.now();
|
|
58
|
+
|
|
59
|
+
pushToCache(['channel', 'get', channelIds[0]], channel[0], { cachedAt });
|
|
60
|
+
pushToCache(['channel', 'get', channelIds[1]], channel[1], { cachedAt });
|
|
61
|
+
pushToCache(['channel', 'get', channelIds[2]], channel[2], { cachedAt });
|
|
62
|
+
|
|
63
|
+
const data = getChannels.locally(channelIds);
|
|
64
|
+
|
|
65
|
+
expect(data).toBeDefined();
|
|
66
|
+
expect(data?.data).toEqual(channel);
|
|
67
|
+
expect(data?.cachedAt).toEqual(cachedAt);
|
|
68
|
+
|
|
69
|
+
disableCache();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('should return undefined if channel not in cache', () => {
|
|
73
|
+
expect(
|
|
74
|
+
getChannels.locally(['non-existent-channel-1', 'non-existent-channel-2']),
|
|
75
|
+
).toBeUndefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('should return undefined if cache not enabled', () => {
|
|
79
|
+
expect(getChannels.locally(channelIds)).toBeUndefined();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('it should return cachedAt same date with oldest record', () => {
|
|
83
|
+
enableCache();
|
|
84
|
+
|
|
85
|
+
const cachedAt = Date.now();
|
|
86
|
+
|
|
87
|
+
pushToCache(['channel', 'get', channelIds[0]], channel[0], { cachedAt });
|
|
88
|
+
pushToCache(['channel', 'get', channelIds[1]], channel[1], { cachedAt: cachedAt + 1 });
|
|
89
|
+
pushToCache(['channel', 'get', channelIds[2]], channel[2], { cachedAt: cachedAt + 2 });
|
|
90
|
+
|
|
91
|
+
const data = getChannels.locally(channelIds);
|
|
92
|
+
|
|
93
|
+
expect(data?.cachedAt).toEqual(cachedAt);
|
|
94
|
+
|
|
95
|
+
disableCache();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { pullFromCache, enableCache, disableCache } from '~/cache/api';
|
|
2
|
+
import { ASCApiError } from '~/core/errors';
|
|
2
3
|
import { getResolver } from '~/core/model';
|
|
3
4
|
|
|
4
5
|
import {
|
|
@@ -19,6 +20,7 @@ describe('joinChannel', () => {
|
|
|
19
20
|
|
|
20
21
|
const { channelId } = channel1;
|
|
21
22
|
|
|
23
|
+
// integration_test_id: efef33a8-bf09-4dfe-8ed9-e7c3615ad74d
|
|
22
24
|
test('it should return channelUser', async () => {
|
|
23
25
|
client.http.post = jest.fn().mockResolvedValueOnce(channelUserQueryResponse);
|
|
24
26
|
|
|
@@ -44,9 +46,66 @@ describe('joinChannel', () => {
|
|
|
44
46
|
disableCache();
|
|
45
47
|
});
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
// integration_test_id: 402a8cd8-6522-42d0-8f40-ccccae0093a1
|
|
50
|
+
test('it should throw error 400400 when user joins channel which does not exist', async () => {
|
|
51
|
+
const apiError = new ASCApiError(
|
|
52
|
+
'ChannelId Not Found',
|
|
53
|
+
Amity.ServerError.ITEM_NOT_FOUND,
|
|
54
|
+
Amity.ErrorLevel.ERROR,
|
|
55
|
+
);
|
|
49
56
|
|
|
50
|
-
|
|
57
|
+
client.http.post = jest.fn().mockRejectedValueOnce({
|
|
58
|
+
status: 403,
|
|
59
|
+
data: apiError,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
await joinChannel(channelId);
|
|
64
|
+
} catch ({ status = null, data = null }) {
|
|
65
|
+
expect(status).toEqual(403);
|
|
66
|
+
expect(data).toEqual(apiError);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// integration_test_id: dcad7f35-586c-400f-8eca-2334d6d7b3e0
|
|
71
|
+
test("it should throw error 400300 when user tries to join channel in which he doesn't have permission", async () => {
|
|
72
|
+
const apiError = new ASCApiError(
|
|
73
|
+
"Can't join the conversation channel",
|
|
74
|
+
Amity.ServerError.FORBIDDEN,
|
|
75
|
+
Amity.ErrorLevel.ERROR,
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
client.http.post = jest.fn().mockRejectedValueOnce({
|
|
79
|
+
status: 403,
|
|
80
|
+
data: apiError,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
await joinChannel(channelId);
|
|
85
|
+
} catch ({ status = null, data = null }) {
|
|
86
|
+
expect(status).toEqual(403);
|
|
87
|
+
expect(data).toEqual(apiError);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// integration_test_id: 8ccb81b7-a89c-486a-81ed-7371b3ef1c4f
|
|
92
|
+
test('it should throw error 400304 when user tries to join channel in which he is banned', async () => {
|
|
93
|
+
const apiError = new ASCApiError(
|
|
94
|
+
'User is banned from this channel',
|
|
95
|
+
Amity.ServerError.CHANNEL_BAN,
|
|
96
|
+
Amity.ErrorLevel.ERROR,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
client.http.post = jest.fn().mockRejectedValueOnce({
|
|
100
|
+
status: 403,
|
|
101
|
+
data: apiError,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
await joinChannel(channelId);
|
|
106
|
+
} catch ({ status = null, data = null }) {
|
|
107
|
+
expect(status).toEqual(403);
|
|
108
|
+
expect(data).toEqual(apiError);
|
|
109
|
+
}
|
|
51
110
|
});
|
|
52
111
|
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { pullFromCache, enableCache, disableCache } from '~/cache/api';
|
|
2
|
+
import { ASCApiError } from '~/core/errors';
|
|
3
|
+
import { getResolver } from '~/core/model';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
client,
|
|
7
|
+
connectClient,
|
|
8
|
+
disconnectClient,
|
|
9
|
+
channel1,
|
|
10
|
+
user11,
|
|
11
|
+
rawChannelUser,
|
|
12
|
+
channelUserQueryResponse,
|
|
13
|
+
} from '~/utils/tests';
|
|
14
|
+
|
|
15
|
+
import { leaveChannel } from '../leaveChannel';
|
|
16
|
+
|
|
17
|
+
describe('leaveChannel', () => {
|
|
18
|
+
beforeAll(connectClient);
|
|
19
|
+
afterAll(disconnectClient);
|
|
20
|
+
|
|
21
|
+
const { channelId } = channel1;
|
|
22
|
+
|
|
23
|
+
// integration_test_id: 3b4408dd-0f45-4270-9384-73d9ddac4511
|
|
24
|
+
test('it should return channelUser', async () => {
|
|
25
|
+
client.http.delete = jest.fn().mockResolvedValueOnce(channelUserQueryResponse);
|
|
26
|
+
|
|
27
|
+
const recieved = await leaveChannel(channelId);
|
|
28
|
+
|
|
29
|
+
expect(recieved).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('it should update cache', async () => {
|
|
33
|
+
enableCache();
|
|
34
|
+
|
|
35
|
+
const expected = rawChannelUser;
|
|
36
|
+
client.http.delete = jest.fn().mockResolvedValueOnce(channelUserQueryResponse);
|
|
37
|
+
|
|
38
|
+
const didleaveChannel = await leaveChannel(channelId);
|
|
39
|
+
|
|
40
|
+
const resolverId = getResolver('channelUsers')({ channelId, userId: user11.userId });
|
|
41
|
+
const recieved = pullFromCache(['channelUsers', 'get', resolverId]);
|
|
42
|
+
|
|
43
|
+
expect(didleaveChannel).toBe(true);
|
|
44
|
+
expect(recieved?.data).toStrictEqual(expected);
|
|
45
|
+
|
|
46
|
+
disableCache();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// integration_test_id: 4f56790e-5fa0-4d04-8c20-ab0c39d0776f
|
|
50
|
+
test('it should throw error 400301 when user try to leave a channel which user is not a member', async () => {
|
|
51
|
+
const apiError = new ASCApiError(
|
|
52
|
+
'User is not member of channel',
|
|
53
|
+
Amity.ServerError.PERMISSION_DENIED,
|
|
54
|
+
Amity.ErrorLevel.ERROR,
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
client.http.delete = jest.fn().mockRejectedValueOnce({
|
|
58
|
+
status: 403,
|
|
59
|
+
data: apiError,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
await leaveChannel(channelId);
|
|
64
|
+
} catch ({ status = null, data = null }) {
|
|
65
|
+
expect(status).toEqual(403);
|
|
66
|
+
expect(data).toEqual(apiError);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// integration_test_id: 818d2317-e138-4d66-bdc0-bcc858580df5
|
|
71
|
+
test('it should throw error 400304 when user try to leave channel which he got banned', async () => {
|
|
72
|
+
const apiError = new ASCApiError(
|
|
73
|
+
'User is banned from this channel',
|
|
74
|
+
Amity.ServerError.CHANNEL_BAN,
|
|
75
|
+
Amity.ErrorLevel.ERROR,
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
client.http.delete = jest.fn().mockRejectedValueOnce({
|
|
79
|
+
status: 403,
|
|
80
|
+
data: apiError,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
await leaveChannel(channelId);
|
|
85
|
+
} catch ({ status = null, data = null }) {
|
|
86
|
+
expect(status).toEqual(403);
|
|
87
|
+
expect(data).toEqual(apiError);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -4,11 +4,17 @@ import { disableCache, enableCache, pushToCache } from '~/cache/api';
|
|
|
4
4
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
5
5
|
|
|
6
6
|
import { prepareChannelPayload } from '~/channel/utils';
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
client,
|
|
9
|
+
channelQueryResponse,
|
|
10
|
+
channelTagQueryResponse,
|
|
11
|
+
channelExcludeTagQueryResponse,
|
|
12
|
+
} from '~/utils/tests';
|
|
8
13
|
|
|
9
14
|
import { queryChannels } from '../queryChannels';
|
|
10
15
|
|
|
11
16
|
describe('queryChannels', () => {
|
|
17
|
+
// integration_test_id: 06c14dab-4799-4f26-9e52-be7c12b3bdb3
|
|
12
18
|
test('it should return channels', async () => {
|
|
13
19
|
const expected = prepareChannelPayload(channelQueryResponse.data).channels;
|
|
14
20
|
client.http.get = jest.fn().mockResolvedValue(channelQueryResponse);
|
|
@@ -18,6 +24,37 @@ describe('queryChannels', () => {
|
|
|
18
24
|
expect(data).toEqual(expected);
|
|
19
25
|
});
|
|
20
26
|
|
|
27
|
+
// integration_test_id: 253fb0ac-f868-4570-9b1e-98b08861aa53
|
|
28
|
+
test('it should return channels match with membership type', async () => {
|
|
29
|
+
// Actually there is no any flag to tell about this list is relate to membership type
|
|
30
|
+
// But need to implement this case to match with our test list
|
|
31
|
+
const expected = prepareChannelPayload(channelQueryResponse.data).channels;
|
|
32
|
+
client.http.get = jest.fn().mockResolvedValue(channelQueryResponse);
|
|
33
|
+
const { data } = await queryChannels({ membership: 'member' });
|
|
34
|
+
expect(data).toEqual(expected);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// integration_test_id: 92329d41-9403-44e7-979f-ed227e6ab120
|
|
38
|
+
test('it should return channels match with tags query type', async () => {
|
|
39
|
+
const expected = prepareChannelPayload(channelTagQueryResponse.data).channels;
|
|
40
|
+
client.http.get = jest.fn().mockResolvedValue(channelTagQueryResponse);
|
|
41
|
+
const { data } = await queryChannels({ tags: ['tag1'] });
|
|
42
|
+
expect(data).toEqual(expected);
|
|
43
|
+
expect(data[0].tags).toContain('tag1');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// integration_test_id: 5249778c-6d99-4e2f-9b80-2f4ea6ae1833
|
|
47
|
+
test('it should return channels without tags which defined in exclude options', async () => {
|
|
48
|
+
const expected = prepareChannelPayload(channelExcludeTagQueryResponse.data).channels;
|
|
49
|
+
client.http.get = jest.fn().mockResolvedValue(channelExcludeTagQueryResponse);
|
|
50
|
+
const { data } = await queryChannels({ excludeTags: ['tag1'] });
|
|
51
|
+
expect(data).toEqual(expected);
|
|
52
|
+
|
|
53
|
+
data.forEach(item => {
|
|
54
|
+
expect(item.tags).not.toContain('tag1');
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
21
58
|
test('it should throw error', async () => {
|
|
22
59
|
client.http.get = jest.fn().mockRejectedValueOnce(new Error('error'));
|
|
23
60
|
|
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getActiveClient } from '~/client/api';
|
|
3
|
-
import { createQuery, runQuery } from '~/core/query';
|
|
4
|
-
import {
|
|
5
|
-
LIVE_OBJECT_ENABLE_CACHE_MESSAGE,
|
|
6
|
-
UNSYNCED_OBJECT_CACHED_AT_MESSAGE,
|
|
7
|
-
UNSYNCED_OBJECT_CACHED_AT_VALUE,
|
|
8
|
-
} from '~/utils/constants';
|
|
9
|
-
import { hasUpdates } from '~/core/model';
|
|
1
|
+
import { liveObject } from '~/utils/liveObject';
|
|
10
2
|
import { onMessageCreated } from '~/message/events';
|
|
11
3
|
import { getChannel } from '../api';
|
|
12
4
|
import { onChannelDeleted, onChannelJoined, onChannelMuted, onChannelUpdated } from '../events';
|
|
@@ -34,75 +26,11 @@ export const liveChannel = (
|
|
|
34
26
|
channelId: Amity.Channel['channelId'],
|
|
35
27
|
callback: Amity.LiveObjectCallback<Amity.Channel>,
|
|
36
28
|
): Amity.Unsubscriber => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
let channel: Amity.Channel;
|
|
46
|
-
let isUnsyncedObject = false;
|
|
47
|
-
const disposers: Amity.Unsubscriber[] = [];
|
|
48
|
-
|
|
49
|
-
const dispatcher = (data: Amity.LiveObject<Amity.Channel>) => {
|
|
50
|
-
channel = data.data;
|
|
51
|
-
callback(data);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const realtimeRouter = (eventChannel: Amity.Channel) => {
|
|
55
|
-
if (channelId !== eventChannel.channelId) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (channel && !hasUpdates(channel, eventChannel)) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
dispatcher({ loading: false, data: eventChannel, origin: 'event' });
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const onFetch = () => {
|
|
67
|
-
const query = createQuery(getChannel, channelId);
|
|
68
|
-
|
|
69
|
-
runQuery(query, ({ loading, data, error, origin, cachedAt }) => {
|
|
70
|
-
if (cachedAt === UNSYNCED_OBJECT_CACHED_AT_VALUE) {
|
|
71
|
-
dispatcher({
|
|
72
|
-
data,
|
|
73
|
-
origin,
|
|
74
|
-
loading: false,
|
|
75
|
-
error: new ASCApiError(
|
|
76
|
-
UNSYNCED_OBJECT_CACHED_AT_MESSAGE,
|
|
77
|
-
Amity.ClientError.DISALOOW_UNSYNCED_OBJECT,
|
|
78
|
-
Amity.ErrorLevel.ERROR,
|
|
79
|
-
),
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
isUnsyncedObject = true;
|
|
83
|
-
disposers.forEach(fn => fn());
|
|
84
|
-
} else if (!isUnsyncedObject) {
|
|
85
|
-
dispatcher({ loading, data, origin, error });
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (error) {
|
|
89
|
-
disposers.forEach(fn => fn());
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
disposers.push(
|
|
95
|
-
onChannelUpdated(realtimeRouter),
|
|
96
|
-
onChannelDeleted(realtimeRouter),
|
|
97
|
-
onChannelJoined(realtimeRouter),
|
|
98
|
-
onChannelMuted(realtimeRouter),
|
|
99
|
-
onMessageCreated(() => null), // watch unread count
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
onFetch();
|
|
103
|
-
|
|
104
|
-
return () => {
|
|
105
|
-
log(`liveChannel(tmpid: ${timestamp}) > dispose`);
|
|
106
|
-
disposers.forEach(fn => fn());
|
|
107
|
-
};
|
|
29
|
+
return liveObject(channelId, callback, 'channelId', getChannel, [
|
|
30
|
+
onChannelUpdated,
|
|
31
|
+
onChannelDeleted,
|
|
32
|
+
onChannelJoined,
|
|
33
|
+
onChannelMuted,
|
|
34
|
+
() => onMessageCreated(() => null), // watch unread count
|
|
35
|
+
]);
|
|
108
36
|
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { fireEvent } from '~/core/events';
|
|
2
|
+
import { scheduleTask } from '~/core/microtasks';
|
|
3
|
+
import { MINUTE, ACCESS_TOKEN_WATCHER_INTERVAL } from '~/utils/constants';
|
|
4
|
+
|
|
5
|
+
import { renewal } from './renewal';
|
|
6
|
+
|
|
7
|
+
const ABOUT_TO_EXPIRE_THRESHOLD = 80 / 100;
|
|
8
|
+
const COMPENSATED_DELAY = 5 * MINUTE;
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* a helper function to check if the token has expires
|
|
12
|
+
*
|
|
13
|
+
* @param token to be checked
|
|
14
|
+
* @returns boolean indicating if token expires
|
|
15
|
+
*
|
|
16
|
+
* @category private
|
|
17
|
+
*/
|
|
18
|
+
export const isExpired = (expiresAt: Amity.Tokens['expiresAt']): boolean =>
|
|
19
|
+
Date.now() > Date.parse(expiresAt) - COMPENSATED_DELAY;
|
|
20
|
+
|
|
21
|
+
/*
|
|
22
|
+
* a helper function to check if the token is about to expire
|
|
23
|
+
*
|
|
24
|
+
* @param token to be checked
|
|
25
|
+
* @returns boolean indicating if token is aboutToExpire
|
|
26
|
+
*
|
|
27
|
+
* @category private
|
|
28
|
+
*/
|
|
29
|
+
export const isAboutToExpire = (params: {
|
|
30
|
+
expiresAt: Amity.Tokens['expiresAt'];
|
|
31
|
+
issuedAt: Amity.Tokens['issuedAt'];
|
|
32
|
+
}): boolean => {
|
|
33
|
+
const { expiresAt, issuedAt } = params;
|
|
34
|
+
|
|
35
|
+
const expires = Date.parse(expiresAt);
|
|
36
|
+
const issued = Date.parse(issuedAt);
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
|
|
39
|
+
const duration = expires - issued - COMPENSATED_DELAY;
|
|
40
|
+
const aboutToExpireAt = issued + duration * ABOUT_TO_EXPIRE_THRESHOLD;
|
|
41
|
+
|
|
42
|
+
return now > aboutToExpireAt && now < expires;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/*
|
|
46
|
+
* Monitors time to expire of token and updates session state to aboutToExpire
|
|
47
|
+
*
|
|
48
|
+
* @returns intervalId to be cleared after trigger
|
|
49
|
+
*
|
|
50
|
+
* @category private
|
|
51
|
+
*/
|
|
52
|
+
export const accessTokenExpiryWatcher = (
|
|
53
|
+
expiresAt: Amity.Tokens['expiresAt'],
|
|
54
|
+
issuedAt: Amity.Tokens['issuedAt'],
|
|
55
|
+
sessionHandler: Amity.SessionHandler,
|
|
56
|
+
): Amity.Unsubscriber => {
|
|
57
|
+
const interval: ReturnType<typeof setInterval> = setInterval(() => {
|
|
58
|
+
if (isExpired(expiresAt)) {
|
|
59
|
+
/*
|
|
60
|
+
* the event handler will take care of updating session state
|
|
61
|
+
* Note, this will also clear the interval id, so this event will only be
|
|
62
|
+
* fired once
|
|
63
|
+
*/
|
|
64
|
+
fireEvent('tokenExpired', Amity.SessionStates.TOKEN_EXPIRED);
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
* https://ekoapp.atlassian.net/wiki/spaces/UP/pages/2082537485/ASC+Core+-+Session+Management+3.0#Automatically-initiate-renewal-flow
|
|
68
|
+
*
|
|
69
|
+
* Why sechduled task?
|
|
70
|
+
* Since fireEvent is scheduled, it will be called
|
|
71
|
+
* after sessionHandler leading to an invalid state change from
|
|
72
|
+
* establishing to tokenExpired
|
|
73
|
+
*/
|
|
74
|
+
scheduleTask(() => sessionHandler.sessionWillRenewAccessToken(renewal()));
|
|
75
|
+
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (isAboutToExpire({ expiresAt, issuedAt })) {
|
|
80
|
+
sessionHandler.sessionWillRenewAccessToken(renewal());
|
|
81
|
+
}
|
|
82
|
+
}, ACCESS_TOKEN_WATCHER_INTERVAL);
|
|
83
|
+
|
|
84
|
+
return (): void => clearInterval(interval);
|
|
85
|
+
};
|