@amityco/ts-sdk 6.4.6-e12bdf9.0 → 6.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/dist/@types/core/events.d.ts +4 -4
  2. package/dist/@types/core/events.d.ts.map +1 -1
  3. package/dist/@types/core/model.d.ts +3 -1
  4. package/dist/@types/core/model.d.ts.map +1 -1
  5. package/dist/@types/core/payload.d.ts +1 -1
  6. package/dist/@types/core/payload.d.ts.map +1 -1
  7. package/dist/@types/domains/post.d.ts +1 -1
  8. package/dist/@types/domains/post.d.ts.map +1 -1
  9. package/dist/@types/domains/reaction.d.ts +4 -5
  10. package/dist/@types/domains/reaction.d.ts.map +1 -1
  11. package/dist/channelRepsitory/channelMembership/observers/getMembers.d.ts +1 -0
  12. package/dist/channelRepsitory/channelMembership/observers/getMembers.d.ts.map +1 -1
  13. package/dist/client/observers/getTotalUnreadCount.d.ts.map +1 -1
  14. package/dist/client/utils/markerSyncEngine.d.ts.map +1 -1
  15. package/dist/commentRepository/observers/getComment.d.ts +2 -2
  16. package/dist/communityRepository/api/getCommunity.d.ts +4 -4
  17. package/dist/communityRepository/api/getRecommendedCommunities.d.ts +4 -19
  18. package/dist/communityRepository/api/getRecommendedCommunities.d.ts.map +1 -1
  19. package/dist/communityRepository/api/getTopTrendingCommunities.d.ts +5 -2
  20. package/dist/communityRepository/api/getTopTrendingCommunities.d.ts.map +1 -1
  21. package/dist/communityRepository/api/getTrendingCommunities.d.ts +17 -0
  22. package/dist/communityRepository/api/getTrendingCommunities.d.ts.map +1 -0
  23. package/dist/communityRepository/api/index.d.ts +0 -1
  24. package/dist/communityRepository/api/index.d.ts.map +1 -1
  25. package/dist/communityRepository/api/test/getTrendingCommunities.test.d.ts +2 -0
  26. package/dist/communityRepository/api/test/getTrendingCommunities.test.d.ts.map +1 -0
  27. package/dist/communityRepository/communityMembership/observers/getMembers.d.ts +1 -0
  28. package/dist/communityRepository/communityMembership/observers/getMembers.d.ts.map +1 -1
  29. package/dist/communityRepository/observers/getCommunities.d.ts +2 -2
  30. package/dist/communityRepository/observers/getCommunity.d.ts +2 -2
  31. package/dist/communityRepository/observers/getRecommendedCommunities.d.ts +20 -0
  32. package/dist/communityRepository/observers/getRecommendedCommunities.d.ts.map +1 -0
  33. package/dist/communityRepository/observers/getTrendingCommunities.d.ts +20 -0
  34. package/dist/communityRepository/observers/getTrendingCommunities.d.ts.map +1 -0
  35. package/dist/communityRepository/observers/index.d.ts +2 -0
  36. package/dist/communityRepository/observers/index.d.ts.map +1 -1
  37. package/dist/communityRepository/utils/communityQueryFilter.d.ts +2 -0
  38. package/dist/communityRepository/utils/communityQueryFilter.d.ts.map +1 -0
  39. package/dist/core/model/idResolvers.d.ts.map +1 -1
  40. package/dist/core/model/identifyModel.d.ts.map +1 -1
  41. package/dist/index.cjs.js +554 -237
  42. package/dist/index.esm.js +554 -237
  43. package/dist/index.umd.js +4 -4
  44. package/dist/pollRepository/api/index.d.ts +0 -1
  45. package/dist/pollRepository/api/index.d.ts.map +1 -1
  46. package/dist/pollRepository/observers/getPoll.d.ts +21 -0
  47. package/dist/pollRepository/observers/getPoll.d.ts.map +1 -0
  48. package/dist/pollRepository/observers/index.d.ts +1 -0
  49. package/dist/pollRepository/observers/index.d.ts.map +1 -1
  50. package/dist/postRepository/observers/getPost.d.ts +2 -2
  51. package/dist/reactionRepository/api/addReaction.d.ts +2 -2
  52. package/dist/reactionRepository/api/addReaction.d.ts.map +1 -1
  53. package/dist/reactionRepository/api/queryReactions.d.ts +2 -2
  54. package/dist/reactionRepository/api/queryReactions.d.ts.map +1 -1
  55. package/dist/reactionRepository/api/queryReactor.d.ts +19 -0
  56. package/dist/reactionRepository/api/queryReactor.d.ts.map +1 -0
  57. package/dist/reactionRepository/api/removeReaction.d.ts +2 -2
  58. package/dist/reactionRepository/api/removeReaction.d.ts.map +1 -1
  59. package/dist/reactionRepository/observers/getReactions.d.ts +3 -3
  60. package/dist/reactionRepository/observers/getReactions.d.ts.map +1 -1
  61. package/dist/streamRepository/observers/getStreamById.d.ts +19 -2
  62. package/dist/streamRepository/observers/getStreamById.d.ts.map +1 -1
  63. package/dist/streamRepository/observers/getStreams.d.ts.map +1 -1
  64. package/dist/streamRepository/observers/tests/getStreamById.test.d.ts +2 -0
  65. package/dist/streamRepository/observers/tests/getStreamById.test.d.ts.map +1 -0
  66. package/dist/userRepository/observers/getUsers.d.ts +1 -0
  67. package/dist/userRepository/observers/getUsers.d.ts.map +1 -1
  68. package/dist/utils/tests/dummy/reaction.d.ts +1 -1
  69. package/dist/utils/tests/dummy/reaction.d.ts.map +1 -1
  70. package/dist/utils/tests/dummy/stream.d.ts +22 -0
  71. package/dist/utils/tests/dummy/stream.d.ts.map +1 -1
  72. package/package.json +1 -1
  73. package/src/@types/core/events.ts +4 -4
  74. package/src/@types/core/model.ts +3 -1
  75. package/src/@types/core/payload.ts +1 -1
  76. package/src/@types/domains/post.ts +1 -1
  77. package/src/@types/domains/reaction.ts +8 -9
  78. package/src/categoryRepository/api/queryCategories.ts +1 -1
  79. package/src/channelRepsitory/channelMembership/observers/getMembers.ts +47 -39
  80. package/src/channelRepsitory/channelMembership/observers/tests/getMembers.test.ts +82 -5
  81. package/src/client/observers/getTotalUnreadCount.ts +1 -7
  82. package/src/client/observers/tests/getTotalUnreadCount.test.ts +3 -10
  83. package/src/client/utils/markerSyncEngine.ts +13 -2
  84. package/src/commentRepository/events/onCommentReactionAdded.ts +1 -1
  85. package/src/commentRepository/events/onCommentReactionRemoved.ts +1 -1
  86. package/src/commentRepository/observers/getComment.ts +2 -2
  87. package/src/commentRepository/observers/tests/getComment.test.ts +1 -3
  88. package/src/communityRepository/api/getCommunity.ts +4 -4
  89. package/src/communityRepository/api/getRecommendedCommunities.ts +7 -48
  90. package/src/communityRepository/api/getTopTrendingCommunities.ts +5 -2
  91. package/src/communityRepository/api/getTrendingCommunities.ts +50 -0
  92. package/src/communityRepository/api/index.ts +1 -1
  93. package/src/communityRepository/api/test/getTrendingCommunities.test.ts +102 -0
  94. package/src/communityRepository/communityMembership/observers/getMembers.ts +31 -23
  95. package/src/communityRepository/communityMembership/observers/tests/getMembers.test.ts +80 -2
  96. package/src/communityRepository/observers/getCommunities.ts +2 -2
  97. package/src/communityRepository/observers/getCommunity.ts +2 -2
  98. package/src/communityRepository/observers/getRecommendedCommunities.ts +130 -0
  99. package/src/communityRepository/observers/getTrendingCommunities.ts +130 -0
  100. package/src/communityRepository/observers/index.ts +2 -0
  101. package/src/communityRepository/utils/communityQueryFilter.ts +55 -0
  102. package/src/core/model/idResolvers.ts +2 -1
  103. package/src/core/model/identifyModel.ts +5 -4
  104. package/src/pollRepository/api/index.ts +0 -2
  105. package/src/pollRepository/observers/getPoll.ts +33 -0
  106. package/src/pollRepository/observers/index.ts +1 -0
  107. package/src/pollRepository/observers/observePoll.ts +2 -2
  108. package/src/postRepository/events/tests/onPostReactionAdded.test.ts +6 -5
  109. package/src/postRepository/events/tests/onPostReactionRemoved.test.ts +5 -4
  110. package/src/postRepository/observers/getPost.ts +2 -2
  111. package/src/postRepository/observers/getPosts.ts +1 -1
  112. package/src/postRepository/observers/tests/getPost.test.ts +1 -3
  113. package/src/postRepository/observers/tests/getPosts.test.ts +1 -5
  114. package/src/reactionRepository/api/addReaction.ts +2 -2
  115. package/src/reactionRepository/api/queryReactions.ts +2 -2
  116. package/src/reactionRepository/api/queryReactor.ts +31 -0
  117. package/src/reactionRepository/api/removeReaction.ts +2 -2
  118. package/src/reactionRepository/observers/getReactions.ts +6 -8
  119. package/src/streamRepository/observers/getStreamById.ts +35 -1
  120. package/src/streamRepository/observers/getStreams.ts +6 -5
  121. package/src/streamRepository/observers/tests/getStreamById.test.ts +44 -0
  122. package/src/userRepository/observers/getUsers.ts +32 -24
  123. package/src/userRepository/observers/tests/getUsers.test.ts +46 -1
  124. package/src/utils/tests/dummy/reaction.ts +6 -10
  125. package/src/utils/tests/dummy/stream.ts +47 -0
@@ -0,0 +1,130 @@
1
+ /* eslint-disable no-use-before-define */
2
+ import { getResolver } from '~/core/model';
3
+ import { dropFromCache, pullFromCache, pushToCache } from '~/cache/api';
4
+ import { getActiveClient } from '~/client/api';
5
+ import { createQuery, queryOptions, runQuery } from '~/core/query';
6
+ import {
7
+ COLLECTION_DEFAULT_CACHING_POLICY,
8
+ COLLECTION_DEFAULT_PAGINATION_LIMIT,
9
+ ENABLE_CACHE_MESSAGE,
10
+ } from '~/utils/constants';
11
+
12
+ import { onCommunityUserChanged } from '../communityMembership/events';
13
+ import { getRecommendedCommunities as _getRecommendedCommunities } from '../api/getRecommendedCommunities';
14
+ import { CACHE_SHORTEN_LIFESPAN } from '~/cache/utils';
15
+ import { communityQueryFilter } from '../utils/communityQueryFilter';
16
+
17
+ /* begin_public_function
18
+ id: community.query.recommended_communities
19
+ */
20
+ /**
21
+ * ```js
22
+ * import { CommunityRepository } from '@amityco/ts-sdk'
23
+ *
24
+ * let communities = []
25
+ * const unsub = CommunityRepository.getCommunities({
26
+ * displayName: Amity.Community['displayName'],
27
+ * }, response => merge(communities, response.data))
28
+ * ```
29
+ *
30
+ * Observe all mutations on a list of {@link Amity.Community}s
31
+ *
32
+ * @param params for querying communities
33
+ * @param callback the function to call when new data are available
34
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the communities
35
+ *
36
+ * @category Community Live Collection
37
+ */
38
+ export const getRecommendedCommunities = (
39
+ params: Amity.PageLimit,
40
+ callback: Amity.LiveCollectionCallback<Amity.Community>,
41
+ config?: Amity.LiveCollectionConfig,
42
+ ) => {
43
+ const { log, cache, userId } = getActiveClient();
44
+
45
+ if (!cache) {
46
+ console.log(ENABLE_CACHE_MESSAGE);
47
+ }
48
+
49
+ const timestamp = Date.now();
50
+ log(`getRecommendedCommunities(tmpid: ${timestamp}) > listen`);
51
+
52
+ const { limit: queryLimit, ...queryParams } = params;
53
+
54
+ const limit = queryLimit ?? COLLECTION_DEFAULT_PAGINATION_LIMIT;
55
+ const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config ?? {};
56
+
57
+ const disposers: Amity.Unsubscriber[] = [];
58
+ const cacheKey = ['community', 'recommended', { params: { options: { limit } } }];
59
+
60
+ const responder = (data: Amity.CommunityLiveCollectionCache, isEventModel = false) => {
61
+ const communities: Amity.Community[] =
62
+ data.data
63
+ .map(communityId => pullFromCache<Amity.Community>(['community', 'get', communityId])!)
64
+ .filter(Boolean)
65
+ .map(({ data }) => data) ?? [];
66
+
67
+ callback({
68
+ onNextPage: onFetch,
69
+ data: isEventModel ? communityQueryFilter(communities, params, userId!) : communities,
70
+ hasNextPage: !!data.params?.page,
71
+ loading: data.loading,
72
+ error: data.error,
73
+ });
74
+ };
75
+
76
+ const realtimeRouter = (_: Amity.CommunityActionType) => (community: Amity.Community) => {
77
+ const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(cacheKey)?.data;
78
+ if (!collection) return;
79
+
80
+ /*
81
+ * Simply update collection and let responder decide what to do with data
82
+ */
83
+ collection.data = [...new Set([community.communityId, ...collection.data])];
84
+
85
+ pushToCache(cacheKey, collection);
86
+ responder(collection);
87
+ };
88
+
89
+ const onFetch = (initial = false) => {
90
+ const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(cacheKey)?.data;
91
+
92
+ const communities = collection?.data ?? [];
93
+
94
+ if (!initial && communities.length > 0 && !collection?.params.page) return;
95
+
96
+ const query = createQuery(_getRecommendedCommunities, params);
97
+
98
+ runQuery(
99
+ query,
100
+ ({ data: result, error, loading }) => {
101
+ const data = {
102
+ loading,
103
+ error,
104
+ params,
105
+ data: communities,
106
+ };
107
+
108
+ if (result) {
109
+ data.data = [...new Set([...communities, ...result.map(getResolver('community'))])];
110
+ }
111
+
112
+ pushToCache(cacheKey, data);
113
+
114
+ responder(data);
115
+ },
116
+ queryOptions(policy, CACHE_SHORTEN_LIFESPAN),
117
+ );
118
+ };
119
+
120
+ disposers.push(onCommunityUserChanged(realtimeRouter('onMemberCountChanged')));
121
+
122
+ onFetch(true);
123
+ disposers.push(() => dropFromCache(cacheKey));
124
+
125
+ return () => {
126
+ log(`getRecommendedCommunities(tmpid: ${timestamp}) > dispose`);
127
+ disposers.forEach(fn => fn());
128
+ };
129
+ };
130
+ /* end_public_function */
@@ -0,0 +1,130 @@
1
+ /* eslint-disable no-use-before-define */
2
+ import { getResolver } from '~/core/model';
3
+ import { dropFromCache, pullFromCache, pushToCache } from '~/cache/api';
4
+ import { getActiveClient } from '~/client/api';
5
+ import { createQuery, queryOptions, runQuery } from '~/core/query';
6
+ import {
7
+ COLLECTION_DEFAULT_CACHING_POLICY,
8
+ COLLECTION_DEFAULT_PAGINATION_LIMIT,
9
+ ENABLE_CACHE_MESSAGE,
10
+ } from '~/utils/constants';
11
+
12
+ import { onCommunityUserChanged } from '../communityMembership/events';
13
+ import { getTrendingCommunities as _getTrendingCommunities } from '../api/getTrendingCommunities';
14
+ import { CACHE_SHORTEN_LIFESPAN } from '~/cache/utils';
15
+ import { communityQueryFilter } from '../utils/communityQueryFilter';
16
+
17
+ /* begin_public_function
18
+ id: community.query.trending_communities
19
+ */
20
+ /**
21
+ * ```js
22
+ * import { CommunityRepository } from '@amityco/ts-sdk'
23
+ *
24
+ * let communities = []
25
+ * const unsub = CommunityRepository.getCommunities({
26
+ * displayName: Amity.Community['displayName'],
27
+ * }, response => merge(communities, response.data))
28
+ * ```
29
+ *
30
+ * Observe all mutations on a list of {@link Amity.Community}s
31
+ *
32
+ * @param params for querying communities
33
+ * @param callback the function to call when new data are available
34
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the communities
35
+ *
36
+ * @category Community Live Collection
37
+ */
38
+ export const getTrendingCommunities = (
39
+ params: Amity.PageLimit,
40
+ callback: Amity.LiveCollectionCallback<Amity.Community>,
41
+ config?: Amity.LiveCollectionConfig,
42
+ ) => {
43
+ const { log, cache, userId } = getActiveClient();
44
+
45
+ if (!cache) {
46
+ console.log(ENABLE_CACHE_MESSAGE);
47
+ }
48
+
49
+ const timestamp = Date.now();
50
+ log(`getTrendingCommunities(tmpid: ${timestamp}) > listen`);
51
+
52
+ const { limit: queryLimit } = params;
53
+
54
+ const limit = queryLimit ?? COLLECTION_DEFAULT_PAGINATION_LIMIT;
55
+ const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config ?? {};
56
+
57
+ const disposers: Amity.Unsubscriber[] = [];
58
+ const cacheKey = ['community', 'top-trending', { params: { options: { limit } } }];
59
+
60
+ const responder = (data: Amity.CommunityLiveCollectionCache, isEventModel = false) => {
61
+ const communities: Amity.Community[] =
62
+ data.data
63
+ .map(communityId => pullFromCache<Amity.Community>(['community', 'get', communityId])!)
64
+ .filter(Boolean)
65
+ .map(({ data }) => data) ?? [];
66
+
67
+ callback({
68
+ onNextPage: onFetch,
69
+ data: isEventModel ? communityQueryFilter(communities, params, userId!) : communities,
70
+ hasNextPage: !!data.params?.page,
71
+ loading: data.loading,
72
+ error: data.error,
73
+ });
74
+ };
75
+
76
+ const realtimeRouter = (_: Amity.CommunityActionType) => (community: Amity.Community) => {
77
+ const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(cacheKey)?.data;
78
+ if (!collection) return;
79
+
80
+ /*
81
+ * Simply update collection and let responder decide what to do with data
82
+ */
83
+ collection.data = [...new Set([community.communityId, ...collection.data])];
84
+
85
+ pushToCache(cacheKey, collection);
86
+ responder(collection);
87
+ };
88
+
89
+ const onFetch = (initial = false) => {
90
+ const collection = pullFromCache<Amity.CommunityLiveCollectionCache>(cacheKey)?.data;
91
+
92
+ const communities = collection?.data ?? [];
93
+
94
+ if (!initial && communities.length > 0 && !collection?.params.page) return;
95
+
96
+ const query = createQuery(_getTrendingCommunities, params);
97
+
98
+ runQuery(
99
+ query,
100
+ ({ data: result, error, loading }) => {
101
+ const data = {
102
+ loading,
103
+ error,
104
+ params,
105
+ data: communities,
106
+ };
107
+
108
+ if (result) {
109
+ data.data = [...new Set([...communities, ...result.map(getResolver('community'))])];
110
+ }
111
+
112
+ pushToCache(cacheKey, data);
113
+
114
+ responder(data);
115
+ },
116
+ queryOptions(policy, CACHE_SHORTEN_LIFESPAN),
117
+ );
118
+ };
119
+
120
+ disposers.push(onCommunityUserChanged(realtimeRouter('onMemberCountChanged')));
121
+
122
+ onFetch(true);
123
+ disposers.push(() => dropFromCache(cacheKey));
124
+
125
+ return () => {
126
+ log(`getTrendingCommunities(tmpid: ${timestamp}) > dispose`);
127
+ disposers.forEach(fn => fn());
128
+ };
129
+ };
130
+ /* end_public_function */
@@ -2,3 +2,5 @@ export * from './observeCommunity';
2
2
 
3
3
  export * from './getCommunities';
4
4
  export * from './getCommunity';
5
+ export * from './getTrendingCommunities';
6
+ export * from './getRecommendedCommunities';
@@ -0,0 +1,55 @@
1
+ import {
2
+ filterByCommunityMembership,
3
+ filterByPropEquality,
4
+ sortByDisplayName,
5
+ sortByFirstCreated,
6
+ sortByLastCreated,
7
+ } from '~/core/query';
8
+
9
+ export const communityQueryFilter = (
10
+ data: Amity.Community[],
11
+ params: Amity.CommunityLiveCollection,
12
+ userId: string,
13
+ ): Amity.Community[] => {
14
+ let communities = data;
15
+ communities = filterByPropEquality(communities, 'displayName', params.displayName);
16
+
17
+ if (!params.includeDeleted) {
18
+ communities = filterByPropEquality(communities, 'isDeleted', false);
19
+ }
20
+
21
+ if (params.categoryId) {
22
+ communities = communities.filter(c => c.categoryIds?.includes(params.categoryId!));
23
+ }
24
+
25
+ if (params.tags) {
26
+ communities = communities.filter(c => c.tags?.some(t => params.tags?.includes(t)));
27
+ }
28
+
29
+ if (params.membership && userId) {
30
+ communities = filterByCommunityMembership(communities, params.membership, userId);
31
+ }
32
+
33
+ const sortBy = params.sortBy || 'lastCreated';
34
+
35
+ if (sortBy === 'lastCreated' || sortBy === 'firstCreated') {
36
+ communities = communities.sort(
37
+ sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated,
38
+ );
39
+ }
40
+
41
+ /*
42
+ * The server returns communities with empty | null displayName's first before
43
+ * returning sorted list of communities with displayNames
44
+ *
45
+ * This section needs to be updated as displayNames can be null as well
46
+ */
47
+ if (sortBy === 'displayName') {
48
+ communities = communities
49
+ // this needs to be aligned with the backend data type
50
+ .map(c => (c.displayName ? c : { ...c, displayName: '' }))
51
+ // @ts-ignore
52
+ .sort(sortByDisplayName);
53
+ }
54
+ return communities;
55
+ };
@@ -33,7 +33,8 @@ const idResolvers: Resolvers = {
33
33
  post: ({ postId }) => postId,
34
34
  comment: ({ commentId }) => commentId,
35
35
  poll: ({ pollId }) => pollId,
36
- reaction: ({ reactionId }) => reactionId,
36
+ reaction: ({ referenceType, referenceId }) => `${referenceType}#${referenceId}`,
37
+ reactor: ({ reactionId }) => reactionId,
37
38
 
38
39
  stream: ({ streamId }) => streamId,
39
40
 
@@ -21,10 +21,10 @@ type Criterias<T extends Amity.Domain = Amity.Domain> = {
21
21
  */
22
22
  const CRITERIAS: Criterias = {
23
23
  file: ['fileId', 'attributes'],
24
- user: ['userId', 'avatarFileId'],
24
+ user: ['userId', 'avatarFileId', 'roles', 'permissions'],
25
25
  role: ['roleId'],
26
- channel: ['channelId', 'rateLimit'],
27
- subChannel: ['subChannelId'],
26
+ channel: ['channelId', 'defaultSubChannelId'],
27
+ subChannel: ['subChannelId', 'latestMessageId'],
28
28
  channelUsers: ['channelId', 'userId', 'membership'],
29
29
  message: ['messageId'],
30
30
  community: ['communityId', 'postSetting'],
@@ -33,7 +33,8 @@ const CRITERIAS: Criterias = {
33
33
  post: ['postId', 'feedId'],
34
34
  comment: ['commentId', 'referenceId'],
35
35
  poll: ['pollId'],
36
- reaction: ['reactionId'],
36
+ reaction: ['referenceId', 'referenceType', 'reactors'],
37
+ reactor: ['reactionId'],
37
38
  stream: ['streamId'],
38
39
  follow: ['from', 'to'],
39
40
  feed: ['feedId', 'feedType'],
@@ -1,5 +1,3 @@
1
- export * from './getPoll';
2
-
3
1
  export * from './createPoll';
4
2
  export * from './closePoll';
5
3
  export * from './deletePoll';
@@ -0,0 +1,33 @@
1
+ import { liveObject } from '~/utils/liveObject';
2
+ import { getPoll as _getPoll } from '../api/getPoll';
3
+ import { onPollDeleted, onPollUpdated } from '../events';
4
+
5
+ /* begin_public_function
6
+ id: poll.get
7
+ */
8
+ /**
9
+ * ```js
10
+ * import { PollRepository } from '@amityco/ts-sdk';
11
+ *
12
+ * let poll;
13
+ *
14
+ * const unsub = PollRepository.getPoll(commentId, response => {
15
+ * poll = response.data;
16
+ * });
17
+ * ```
18
+ *
19
+ * Observe all mutation on a given {@link Amity.Poll}
20
+ *
21
+ * @param pollId the ID of the poll to observe
22
+ * @param callback the function to call when new data are available
23
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the poll
24
+ *
25
+ * @category Poll Live Object
26
+ */
27
+ export const getPoll = (
28
+ pollId: Amity.Poll['pollId'],
29
+ callback: Amity.LiveObjectCallback<Amity.Poll>,
30
+ ): Amity.Unsubscriber => {
31
+ return liveObject(pollId, callback, 'pollId', _getPoll, [onPollUpdated, onPollDeleted]);
32
+ };
33
+ /* end_public_function */
@@ -1 +1,2 @@
1
1
  export * from './observePoll';
2
+ export * from './getPoll';
@@ -2,7 +2,7 @@ import { getActiveClient } from '~/client/api';
2
2
 
3
3
  import { createQuery, runQuery, queryOptions } from '~/core/query';
4
4
 
5
- import { getPoll } from '../api';
5
+ import { getPoll as _getPoll } from '../api/getPoll';
6
6
 
7
7
  import { onPollUpdated, onPollDeleted } from '../events';
8
8
 
@@ -55,7 +55,7 @@ export const observePoll = <Events extends ['onFetch', 'onUpdate', 'onDelete']>(
55
55
  );
56
56
 
57
57
  runQuery(
58
- createQuery(getPoll, pollId),
58
+ createQuery(_getPoll, pollId),
59
59
  result => (result.data || result.error) && router(result, 'onFetch'),
60
60
  queryOptions(policy),
61
61
  );
@@ -1,13 +1,14 @@
1
- import { onPostReactionAdded } from '../onPostReactionAdded';
2
1
  import {
3
2
  client,
4
3
  connectClient,
5
4
  disconnectClient,
6
5
  postPayload,
7
6
  post11,
8
- reaction11,
7
+ reactor11,
9
8
  } from '~/utils/tests';
10
9
 
10
+ import { onPostReactionAdded } from '../onPostReactionAdded';
11
+
11
12
  describe('onPostReactionAdded', () => {
12
13
  beforeAll(async () => {
13
14
  await connectClient();
@@ -20,12 +21,12 @@ describe('onPostReactionAdded', () => {
20
21
  test('it should got event after add reaction to post', () => {
21
22
  const callback = jest.fn();
22
23
  const unsub = onPostReactionAdded(callback);
23
- client.emitter.emit('post.addReaction', { ...postPayload, reactor: reaction11 });
24
+ client.emitter.emit('post.addReaction', { ...postPayload, reactor: reactor11 });
24
25
 
25
26
  unsub();
26
27
 
27
28
  expect(callback).toHaveBeenCalled();
28
- expect(callback).toHaveBeenCalledWith({ ...post11, myReactions: [reaction11.reactionName] });
29
+ expect(callback).toHaveBeenCalledWith({ ...post11, myReactions: [reactor11.reactionName] });
29
30
  });
30
31
 
31
32
  test('it should got nothing if we did unsubscribe before got event', () => {
@@ -34,7 +35,7 @@ describe('onPostReactionAdded', () => {
34
35
 
35
36
  unsub();
36
37
 
37
- client.emitter.emit('post.addReaction', { ...postPayload, reactor: reaction11 });
38
+ client.emitter.emit('post.addReaction', { ...postPayload, reactor: reactor11 });
38
39
  expect(callback).not.toHaveBeenCalled();
39
40
  });
40
41
  });
@@ -1,13 +1,14 @@
1
- import { onPostReactionRemoved } from '../onPostReactionRemoved';
2
1
  import {
3
2
  client,
4
3
  connectClient,
5
4
  disconnectClient,
6
5
  postPayload,
7
6
  post11,
8
- reaction11,
7
+ reactor11,
9
8
  } from '~/utils/tests';
10
9
 
10
+ import { onPostReactionRemoved } from '../onPostReactionRemoved';
11
+
11
12
  describe('onPostReactionRemoved', () => {
12
13
  beforeAll(async () => {
13
14
  await connectClient();
@@ -20,7 +21,7 @@ describe('onPostReactionRemoved', () => {
20
21
  test('it should got event after reaction got removed from post', () => {
21
22
  const callback = jest.fn();
22
23
  const unsub = onPostReactionRemoved(callback);
23
- client.emitter.emit('post.removeReaction', { ...postPayload, reactor: reaction11 });
24
+ client.emitter.emit('post.removeReaction', { ...postPayload, reactor: reactor11 });
24
25
 
25
26
  unsub();
26
27
 
@@ -34,7 +35,7 @@ describe('onPostReactionRemoved', () => {
34
35
 
35
36
  unsub();
36
37
 
37
- client.emitter.emit('post.removeReaction', { ...postPayload, reactor: reaction11 });
38
+ client.emitter.emit('post.removeReaction', { ...postPayload, reactor: reactor11 });
38
39
  expect(callback).not.toHaveBeenCalled();
39
40
  });
40
41
  });
@@ -16,11 +16,11 @@ import {
16
16
  */
17
17
  /**
18
18
  * ```js
19
- * import { getPost } from '@amityco/ts-sdk';
19
+ * import { PostRepository } from '@amityco/ts-sdk';
20
20
  *
21
21
  * let post;
22
22
  *
23
- * const unsubscribe = getPost(postId, response => {
23
+ * const unsub = PostRepository.getPost(postId, response => {
24
24
  * post = response.data;
25
25
  * });
26
26
  * ```
@@ -35,7 +35,7 @@ import {
35
35
  import { queryPosts } from '../api/queryPosts';
36
36
 
37
37
  /* begin_public_function
38
- id: post.query, feed.query.community_feed, feed.query.user_feed
38
+ id: post.query
39
39
  */
40
40
  /**
41
41
  * ```js
@@ -76,12 +76,10 @@ describe('getPost', () => {
76
76
  data: { text: 'new-text' },
77
77
  updatedAt: getFutureDate(post.updatedAt),
78
78
  };
79
- const reactor: Amity.Reaction = {
79
+ const reactor: Amity.Reactor = {
80
80
  createdAt: getFutureDate(post.updatedAt),
81
81
  reactionId: 'like',
82
82
  reactionName: 'like',
83
- referenceId: post.postId,
84
- referenceType: 'post',
85
83
  userId: user12.userId,
86
84
  };
87
85
  const callback = jest.fn();
@@ -201,7 +201,7 @@ describe('getPosts', () => {
201
201
  const reactionEvents: [
202
202
  string,
203
203
  keyof Amity.Events,
204
- Amity.PostPayload & { reactor: Amity.Reaction },
204
+ Amity.PostPayload & { reactor: Amity.Reactor },
205
205
  ][] = [
206
206
  [
207
207
  'it should update post in collection onReactionAdded',
@@ -210,8 +210,6 @@ describe('getPosts', () => {
210
210
  ...emptyPostPayload,
211
211
  posts: [{ ...post11, reactionsCount: 1 }],
212
212
  reactor: {
213
- referenceId: post11.postId,
214
- referenceType: 'post',
215
213
  reactionId: 'test-reaction-id',
216
214
  reactionName: 'like',
217
215
  userId: 'test',
@@ -226,8 +224,6 @@ describe('getPosts', () => {
226
224
  ...emptyPostPayload,
227
225
  posts: [{ ...post11, reactionsCount: 0 }],
228
226
  reactor: {
229
- referenceId: post11.postId,
230
- referenceType: 'post',
231
227
  reactionId: 'test-reaction-id',
232
228
  reactionName: 'like',
233
229
  userId: 'test',
@@ -27,7 +27,7 @@ import { dispatchReactable } from '../utils';
27
27
  export const addReaction = async (
28
28
  referenceType: Amity.Reaction['referenceType'],
29
29
  referenceId: Amity.Reaction['referenceId'],
30
- reactionName: Amity.Reaction['reactionName'],
30
+ reactionName: Amity.Reactor['reactionName'],
31
31
  ): Promise<boolean> => {
32
32
  const client = getActiveClient();
33
33
  client.log('reaction/createReaction', {
@@ -76,7 +76,7 @@ export const addReaction = async (
76
76
  addReaction.optimistically = (
77
77
  referenceType: Amity.ReactableType,
78
78
  referenceId: Amity.Reaction['referenceId'],
79
- reactionName: Amity.Reaction['reactionName'],
79
+ reactionName: Amity.Reactor['reactionName'],
80
80
  ): boolean | undefined => {
81
81
  const client = getActiveClient();
82
82
  client.log('reaction/createReaction.optimistically', {
@@ -12,7 +12,7 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
12
12
  * })
13
13
  * ```
14
14
  *
15
- * Queries a paginable list of {@link Amity.ReactionQuery} objects
15
+ * Queries a paginable list of {@link Amity.Reaction} objects
16
16
  *
17
17
  * @param query The query parameters
18
18
  * @returns A page of {@link Amity.Reaction} objects
@@ -22,7 +22,7 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
22
22
  * */
23
23
  export const queryReactions = async (
24
24
  query: Amity.QueryReactions,
25
- ): Promise<Amity.Paged<Amity.ReactionQuery, Amity.Page<string>>> => {
25
+ ): Promise<Amity.Paged<Amity.Reaction, Amity.Page<string>>> => {
26
26
  const client = getActiveClient();
27
27
  client.log('reaction/queryReactions', query);
28
28
 
@@ -0,0 +1,31 @@
1
+ import { getActiveClient } from '~/client/api/activeClient';
2
+
3
+ import { queryReactions } from './queryReactions';
4
+
5
+ /**
6
+ * ```js
7
+ * import { queryReactor } from '@amityco/ts-sdk'
8
+ * const { data: reactions, prevPage, nextPage } = await queryReactor({
9
+ * referenceId: 'postId',
10
+ * referenceType: 'post',
11
+ * })
12
+ * ```
13
+ *
14
+ * Queries a paginable list of {@link Amity.Reactor} objects
15
+ *
16
+ * @param query The query parameters
17
+ * @returns A page of {@link Amity.Reactor} objects
18
+ *
19
+ * @reaction Reaction API
20
+ * @async
21
+ * */
22
+ export const queryReactor = async (
23
+ query: Amity.QueryReactions,
24
+ ): Promise<Amity.Paged<Amity.Reactor, Amity.Page<string>>> => {
25
+ const client = getActiveClient();
26
+ client.log('reaction/queryReactor', query);
27
+
28
+ const { data, ...response } = await queryReactions(query);
29
+
30
+ return { ...response, data: data[0].reactors };
31
+ };
@@ -28,7 +28,7 @@ import { dispatchReactable } from '../utils';
28
28
  export const removeReaction = async (
29
29
  referenceType: Amity.Reaction['referenceType'],
30
30
  referenceId: Amity.Reaction['referenceId'],
31
- reactionName: Amity.Reaction['reactionName'],
31
+ reactionName: Amity.Reactor['reactionName'],
32
32
  ): Promise<boolean> => {
33
33
  const client = getActiveClient();
34
34
  client.log('reaction/removeReaction', {
@@ -79,7 +79,7 @@ export const removeReaction = async (
79
79
  removeReaction.optimistically = (
80
80
  referenceType: Amity.ReactableType,
81
81
  referenceId: Amity.Reaction['referenceId'],
82
- reactionName: Amity.Reaction['reactionName'],
82
+ reactionName: Amity.Reactor['reactionName'],
83
83
  ): boolean | undefined => {
84
84
  const client = getActiveClient();
85
85
  client.log('reaction/removeReaction.optimistically', {