@amityco/ts-sdk 0.0.1-e32b438.0 → 0.0.1-e556efe.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 (130) hide show
  1. package/.eslintrc.json +4 -1
  2. package/dist/@types/core/events.d.ts +7 -7
  3. package/dist/@types/core/events.d.ts.map +1 -1
  4. package/dist/@types/core/live.d.ts +23 -0
  5. package/dist/@types/core/live.d.ts.map +1 -0
  6. package/dist/@types/core/paging.d.ts +1 -1
  7. package/dist/@types/core/paging.d.ts.map +1 -1
  8. package/dist/@types/core/payload.d.ts +3 -5
  9. package/dist/@types/core/payload.d.ts.map +1 -1
  10. package/dist/@types/domains/comment.d.ts +14 -0
  11. package/dist/@types/domains/comment.d.ts.map +1 -1
  12. package/dist/@types/domains/follow.d.ts +2 -1
  13. package/dist/@types/domains/follow.d.ts.map +1 -1
  14. package/dist/@types/domains/post.d.ts +17 -0
  15. package/dist/@types/domains/post.d.ts.map +1 -1
  16. package/dist/@types/domains/reaction.d.ts +10 -0
  17. package/dist/@types/domains/reaction.d.ts.map +1 -1
  18. package/dist/@types/index.d.ts +1 -0
  19. package/dist/@types/index.d.ts.map +1 -1
  20. package/dist/channel/api/banChannelMembers.d.ts +16 -0
  21. package/dist/channel/api/banChannelMembers.d.ts.map +1 -0
  22. package/dist/channel/api/index.d.ts +2 -0
  23. package/dist/channel/api/index.d.ts.map +1 -1
  24. package/dist/channel/api/unbanChannelMembers.d.ts +16 -0
  25. package/dist/channel/api/unbanChannelMembers.d.ts.map +1 -0
  26. package/dist/comment/api/queryComments.d.ts +1 -9
  27. package/dist/comment/api/queryComments.d.ts.map +1 -1
  28. package/dist/comment/observers/index.d.ts +1 -0
  29. package/dist/comment/observers/index.d.ts.map +1 -1
  30. package/dist/comment/observers/liveComments.d.ts +22 -0
  31. package/dist/comment/observers/liveComments.d.ts.map +1 -0
  32. package/dist/comment/observers/observeComments.d.ts.map +1 -1
  33. package/dist/core/query/query.d.ts.map +1 -1
  34. package/dist/core/tests/query/filtering.test.d.ts +2 -0
  35. package/dist/core/tests/query/filtering.test.d.ts.map +1 -0
  36. package/dist/core/tests/query/query.test.d.ts +2 -0
  37. package/dist/core/tests/query/query.test.d.ts.map +1 -0
  38. package/dist/follow/api/follow.d.ts.map +1 -1
  39. package/dist/follow/api/getFollowInfo.d.ts.map +1 -1
  40. package/dist/follow/api/queryFollowers.d.ts +4 -4
  41. package/dist/follow/api/queryFollowers.d.ts.map +1 -1
  42. package/dist/follow/api/queryFollowings.d.ts +4 -4
  43. package/dist/follow/api/queryFollowings.d.ts.map +1 -1
  44. package/dist/follow/api/utils.d.ts +4 -4
  45. package/dist/follow/api/utils.d.ts.map +1 -1
  46. package/dist/follow/events/utils.d.ts.map +1 -1
  47. package/dist/index.cjs.js +561 -23
  48. package/dist/index.d.ts +1 -0
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.esm.js +554 -24
  51. package/dist/index.umd.js +4 -4
  52. package/dist/post/api/queryPosts.d.ts +1 -12
  53. package/dist/post/api/queryPosts.d.ts.map +1 -1
  54. package/dist/post/observers/index.d.ts +1 -0
  55. package/dist/post/observers/index.d.ts.map +1 -1
  56. package/dist/post/observers/livePosts.d.ts +22 -0
  57. package/dist/post/observers/livePosts.d.ts.map +1 -0
  58. package/dist/post/observers/observePosts.d.ts.map +1 -1
  59. package/dist/post/tests/api/getPost.test.d.ts +2 -0
  60. package/dist/post/tests/api/getPost.test.d.ts.map +1 -0
  61. package/dist/post/tests/api/queryPosts.test.d.ts +2 -0
  62. package/dist/post/tests/api/queryPosts.test.d.ts.map +1 -0
  63. package/dist/reaction/api/queryReactions.d.ts +1 -6
  64. package/dist/reaction/api/queryReactions.d.ts.map +1 -1
  65. package/dist/reaction/events/index.d.ts +3 -0
  66. package/dist/reaction/events/index.d.ts.map +1 -1
  67. package/dist/reaction/events/onReactorAdded.d.ts +19 -0
  68. package/dist/reaction/events/onReactorAdded.d.ts.map +1 -0
  69. package/dist/reaction/events/onReactorRemoved.d.ts +19 -0
  70. package/dist/reaction/events/onReactorRemoved.d.ts.map +1 -0
  71. package/dist/reaction/observers/index.d.ts +2 -0
  72. package/dist/reaction/observers/index.d.ts.map +1 -0
  73. package/dist/reaction/observers/liveReactions.d.ts +22 -0
  74. package/dist/reaction/observers/liveReactions.d.ts.map +1 -0
  75. package/dist/utils/constants.d.ts +3 -0
  76. package/dist/utils/constants.d.ts.map +1 -0
  77. package/dist/utils/tests/client.d.ts +3 -0
  78. package/dist/utils/tests/client.d.ts.map +1 -0
  79. package/dist/utils/tests/dummy.d.ts +8 -0
  80. package/dist/utils/tests/dummy.d.ts.map +1 -0
  81. package/dist/utils/tests/index.d.ts +3 -0
  82. package/dist/utils/tests/index.d.ts.map +1 -0
  83. package/jest.config.ts +15 -0
  84. package/package.json +10 -3
  85. package/src/@types/core/events.ts +7 -7
  86. package/src/@types/core/live.ts +28 -0
  87. package/src/@types/core/paging.ts +1 -1
  88. package/src/@types/core/payload.ts +5 -6
  89. package/src/@types/domains/comment.ts +31 -0
  90. package/src/@types/domains/follow.ts +3 -1
  91. package/src/@types/domains/post.ts +37 -0
  92. package/src/@types/domains/reaction.ts +17 -0
  93. package/src/@types/index.ts +1 -0
  94. package/src/channel/api/banChannelMembers.ts +41 -0
  95. package/src/channel/api/index.ts +3 -0
  96. package/src/channel/api/unbanChannelMembers.ts +41 -0
  97. package/src/comment/api/queryComments.ts +3 -9
  98. package/src/comment/observers/index.ts +1 -0
  99. package/src/comment/observers/liveComments.ts +172 -0
  100. package/src/comment/observers/observeComments.ts +1 -11
  101. package/src/core/query/query.ts +15 -2
  102. package/src/core/tests/query/filtering.test.ts +11 -0
  103. package/src/core/tests/query/query.test.ts +19 -0
  104. package/src/follow/api/acceptFollower.ts +1 -1
  105. package/src/follow/api/declineFollower.ts +1 -1
  106. package/src/follow/api/follow.ts +1 -4
  107. package/src/follow/api/getFollowInfo.ts +8 -5
  108. package/src/follow/api/queryFollowers.ts +5 -4
  109. package/src/follow/api/queryFollowings.ts +5 -4
  110. package/src/follow/api/unfollow.ts +1 -1
  111. package/src/follow/api/utils.ts +10 -10
  112. package/src/follow/events/utils.ts +9 -6
  113. package/src/index.ts +1 -0
  114. package/src/post/api/queryPosts.ts +3 -12
  115. package/src/post/observers/index.ts +1 -0
  116. package/src/post/observers/livePosts.ts +170 -0
  117. package/src/post/observers/observePosts.ts +1 -13
  118. package/src/post/tests/api/getPost.test.ts +88 -0
  119. package/src/post/tests/api/queryPosts.test.ts +23 -0
  120. package/src/reaction/api/queryReactions.ts +14 -14
  121. package/src/reaction/events/index.ts +4 -0
  122. package/src/reaction/events/onReactorAdded.ts +80 -0
  123. package/src/reaction/events/onReactorRemoved.ts +85 -0
  124. package/src/reaction/observers/index.ts +1 -0
  125. package/src/reaction/observers/liveReactions.ts +142 -0
  126. package/src/utils/constants.ts +2 -0
  127. package/src/utils/tests/client.ts +5 -0
  128. package/src/utils/tests/dummy.ts +7 -0
  129. package/src/utils/tests/index.ts +2 -0
  130. package/tsconfig.json +23 -22
@@ -19,15 +19,9 @@ import { getResolver } from '~/core/model';
19
19
  * @category Comment API
20
20
  * @async
21
21
  */
22
- export const queryComments = async (query: {
23
- referenceType: Amity.Comment['referenceType'];
24
- referenceId: Amity.Comment['referenceId'];
25
- sortBy?: 'lastCreated' | 'firstCreated' | 'lastUpdated' | 'firstUpdated';
26
- parentId?: Amity.Comment['commentId'] | null;
27
- hasFlag?: boolean;
28
- isDeleted?: Amity.Comment['isDeleted'];
29
- page?: Amity.Page;
30
- }): Promise<Amity.Cached<Amity.Paged<Amity.Comment>>> => {
22
+ export const queryComments = async (
23
+ query: Amity.QueryComments,
24
+ ): Promise<Amity.Cached<Amity.Paged<Amity.Comment>>> => {
31
25
  const client = getActiveClient();
32
26
  client.log('comment/queryComments', query);
33
27
 
@@ -1,2 +1,3 @@
1
1
  export * from './observeComments';
2
2
  export * from './observeComment';
3
+ export * from './liveComments';
@@ -0,0 +1,172 @@
1
+ /* eslint-disable no-use-before-define */
2
+ import { getResolver } from '~/core/model';
3
+ import { getActiveClient } from '~/client/api';
4
+ import { pushToCache, pullFromCache } from '~/cache/api';
5
+ import {
6
+ createQuery,
7
+ runQuery,
8
+ queryOptions,
9
+ filterByPropEquality,
10
+ sortByFirstCreated,
11
+ sortByLastCreated,
12
+ } from '~/core/query';
13
+
14
+ import {
15
+ COLLECTION_DEFAULT_CACHING_POLICY,
16
+ COLLECTION_DEFAULT_PAGINATION_LIMIT,
17
+ } from '~/utils/constants';
18
+
19
+ import {
20
+ onCommentCreated,
21
+ onCommentUpdated,
22
+ onCommentDeleted,
23
+ onCommentFlagged,
24
+ onCommentUnflagged,
25
+ onCommentReactionAdded,
26
+ onCommentReactionRemoved,
27
+ } from '../events';
28
+ import { queryComments } from '../api';
29
+
30
+ /**
31
+ * ```js
32
+ * import { liveComments } from '@amityco/ts-sdk'
33
+ *
34
+ * let comments = []
35
+ * const unsub = liveComments({
36
+ * referenceType: Amity.Comment['referenceType'];
37
+ * referenceId: Amity.Comment['referenceId'];
38
+ * }, response => merge(comments, response.data))
39
+ * ```
40
+ *
41
+ * Observe all mutations on a list of {@link Amity.Comment} for a given target object
42
+ *
43
+ * @param referenceType the type of the target
44
+ * @param referenceId the ID of the target
45
+ * @param callback the function to call when new data are available
46
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
47
+ *
48
+ * @category Comments Live Collection
49
+ */
50
+ export const liveComments = (
51
+ params: Amity.CommentLiveCollection,
52
+ callback: Amity.LiveCollectionCallback<Amity.Comment>,
53
+ config?: Amity.LiveCollectionConfig,
54
+ ): Amity.Unsubscriber => {
55
+ const { log, cache } = getActiveClient();
56
+
57
+ if (!cache) {
58
+ console.log('For using Live Collection feature you need to enable Cache!');
59
+ }
60
+
61
+ const timestamp = Date.now();
62
+ log(`liveComments(tmpid: ${timestamp}) > listen`);
63
+
64
+ const { limit: queryLimit, ...queryParams } = params;
65
+
66
+ const limit = queryLimit ?? COLLECTION_DEFAULT_PAGINATION_LIMIT;
67
+ const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config ?? {};
68
+
69
+ const disposers: Amity.Unsubscriber[] = [];
70
+ const cacheKey = [
71
+ 'comment',
72
+ 'collection',
73
+ { referenceId: params.referenceType, referenceType: params.referenceId },
74
+ ];
75
+
76
+ const responder = (data: Amity.CommentLiveCollectionCache) => {
77
+ let comments: Amity.Comment[] =
78
+ data.data
79
+ .map(commentId => pullFromCache<Amity.Comment>(['comment', 'get', commentId])!)
80
+ .filter(Boolean)
81
+ .map(({ data }) => data) ?? [];
82
+
83
+ comments = filterByPropEquality(comments, 'isDeleted', params.isDeleted);
84
+
85
+ const sortBy = params.sortBy ? params.sortBy : 'lastCreated';
86
+
87
+ comments = comments.sort(sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
88
+
89
+ callback({
90
+ onNextPage: onFetch,
91
+ data: comments,
92
+ hasNextPage: !!data.params?.page,
93
+ loading: data.loading,
94
+ error: data.error,
95
+ });
96
+ };
97
+
98
+ const realtimeRouter = (comment: Amity.Comment, action: Amity.CommentActionType) => {
99
+ const collection = pullFromCache<Amity.CommentLiveCollectionCache>(cacheKey)?.data;
100
+
101
+ if (
102
+ params.referenceId !== comment.referenceId ||
103
+ params.referenceType !== comment.referenceType ||
104
+ !collection
105
+ )
106
+ return;
107
+
108
+ if (action === 'onCreate') {
109
+ collection.data = [...new Set([comment.commentId, ...collection.data])];
110
+ } else if (action === 'onDelete') {
111
+ collection.data = collection.data.filter(p => p !== comment.commentId);
112
+ }
113
+
114
+ pushToCache(cacheKey, collection);
115
+
116
+ responder(collection);
117
+ };
118
+
119
+ const onFetch = () => {
120
+ const collection = pullFromCache<Amity.CommentLiveCollectionCache>(cacheKey)?.data;
121
+
122
+ const comments = collection?.data ?? [];
123
+
124
+ if (comments.length > 0 && !collection?.params?.page) return;
125
+
126
+ const query = createQuery(queryComments, {
127
+ ...queryParams,
128
+ page: collection?.params?.page ?? { limit },
129
+ });
130
+
131
+ runQuery(
132
+ query,
133
+ ({ data: result, error, loading, prevPage, nextPage }) => {
134
+ // depend on sortBy value we have two different pagination type
135
+ const page = queryParams.sortBy ? nextPage : prevPage;
136
+
137
+ const data = {
138
+ loading,
139
+ error,
140
+ params: { page },
141
+ data: comments,
142
+ };
143
+
144
+ if (result) {
145
+ data.data = [...new Set([...comments, ...result.map(getResolver('comment'))])];
146
+ }
147
+
148
+ pushToCache(cacheKey, data);
149
+
150
+ responder(data);
151
+ },
152
+ queryOptions(policy),
153
+ );
154
+ };
155
+
156
+ disposers.push(
157
+ onCommentCreated(comment => realtimeRouter(comment, 'onCreate')),
158
+ onCommentUpdated(comment => realtimeRouter(comment, 'onUpdate')),
159
+ onCommentDeleted(comment => realtimeRouter(comment, 'onDelete')),
160
+ onCommentFlagged(comment => realtimeRouter(comment, 'onFlagged')),
161
+ onCommentUnflagged(comment => realtimeRouter(comment, 'onUnflagged')),
162
+ onCommentReactionAdded(comment => realtimeRouter(comment, 'onReactionAdded')),
163
+ onCommentReactionRemoved(comment => realtimeRouter(comment, 'onReactionRemoved')),
164
+ );
165
+
166
+ onFetch();
167
+
168
+ return () => {
169
+ log(`liveComments(tmpid: ${timestamp}) > dispose`);
170
+ disposers.forEach(fn => fn());
171
+ };
172
+ };
@@ -48,17 +48,7 @@ export const observeComments = (
48
48
 
49
49
  const disposers: Amity.Unsubscriber[] = [];
50
50
 
51
- const router = (
52
- comment: Amity.Comment,
53
- action:
54
- | 'onCreate'
55
- | 'onUpdate'
56
- | 'onDelete'
57
- | 'onFlagged'
58
- | 'onUnflagged'
59
- | 'onReactionAdded'
60
- | 'onReactionRemoved',
61
- ) => {
51
+ const router = (comment: Amity.Comment, action: Exclude<Amity.CommentActionType, 'onFetch'>) => {
62
52
  if (comment.referenceId !== postId) return;
63
53
 
64
54
  if (callback instanceof Function) return callback(comment);
@@ -215,8 +215,21 @@ export const runQuery = <Args extends any[], Returned extends any>(
215
215
  if (isMutator(func)) {
216
216
  // @ts-ignore
217
217
  const modelKey = local && local.data && identifyModelKey(local.data);
218
- // @ts-ignore
219
- optimisticArgs = modelKey ? [...args, { [modelKey]: local.data[modelKey] }] : args;
218
+ if (modelKey) {
219
+ /*
220
+ * modal key is only required for create as update and delete queries pass
221
+ * the modal key in the param itself
222
+ * For update & delete the first argument is always an id which is of type
223
+ * string, whereas for create the first and only argument is the param object
224
+ *
225
+ * When creating an AmityObject optimistically we need to pass the modal key
226
+ * on to the server. ex: for create message modal key will be 'messageId`
227
+ */
228
+ if (typeof args[0] !== 'string') {
229
+ // @ts-ignore
230
+ optimisticArgs = [{ ...args[0], [modelKey]: local.data[modelKey] }];
231
+ }
232
+ }
220
233
  }
221
234
 
222
235
  func(...optimisticArgs)
@@ -0,0 +1,11 @@
1
+ import { filterByPropEquality } from '~/core/query';
2
+
3
+ describe('Core/Query/Filtering', () => {
4
+ test('check filterByPropEquality', () => {
5
+ const prop = 'prop';
6
+ const collection = [{ [prop]: 'value' }];
7
+
8
+ expect(filterByPropEquality(collection, prop, 'value')).toStrictEqual(collection);
9
+ expect(filterByPropEquality(collection, prop, 'otherValue')).toStrictEqual([]);
10
+ });
11
+ });
@@ -0,0 +1,19 @@
1
+ import { isFetcher } from '~/core/query';
2
+
3
+ type isFetcherMockType = jest.Mock<any, any> & {
4
+ locally?: jest.Mock<any, any>;
5
+ };
6
+
7
+ describe('Core/Query/Query', () => {
8
+ const isFetcherMock: isFetcherMockType = jest.fn();
9
+
10
+ test('isFetcher without locally', () => {
11
+ expect(isFetcher(isFetcherMock)).toBe(false);
12
+ });
13
+
14
+ test('isFetcher with locally', () => {
15
+ isFetcherMock.locally = jest.fn();
16
+
17
+ expect(isFetcher(isFetcherMock)).toBe(true);
18
+ });
19
+ });
@@ -28,7 +28,7 @@ export const acceptFollower = async (userId: Amity.User['userId']): Promise<bool
28
28
  ingestInCache(data);
29
29
  }
30
30
 
31
- fireEvent('follow.accepted', data.follows[0]);
31
+ fireEvent('follow.accepted', data);
32
32
 
33
33
  return true;
34
34
  };
@@ -28,7 +28,7 @@ export const declineFollower = async (userId: Amity.User['userId']): Promise<boo
28
28
  ingestInCache(data);
29
29
  }
30
30
 
31
- fireEvent('follow.requestDeclined', data.follows[0]);
31
+ fireEvent('follow.requestDeclined', data);
32
32
 
33
33
  return true;
34
34
  };
@@ -32,10 +32,7 @@ export const follow = async (
32
32
  ingestInCache(data, { cachedAt });
33
33
  }
34
34
 
35
- fireEvent(
36
- data.follows[0].status === 'accepted' ? 'follow.created' : 'follow.requested',
37
- data.follows[0],
38
- );
35
+ fireEvent(data.follows[0].status === 'accepted' ? 'follow.created' : 'follow.requested', data);
39
36
 
40
37
  return {
41
38
  data: data.follows[0],
@@ -27,15 +27,18 @@ export const getFollowInfo = async (
27
27
  const client = getActiveClient();
28
28
  client.log('follow/getFollowInfo', userId);
29
29
 
30
- const { data } = await client.http.get<Amity.FollowInfoPayload>(
30
+ const { data } = await client.http.get<Amity.FollowInfoMePayload | Amity.FollowInfoPayload>(
31
31
  client.userId === userId ? `/api/v4/me/followInfo` : `/api/v4/users/${userId}/followInfo`,
32
32
  );
33
33
 
34
34
  const cachedAt = client.cache && Date.now();
35
- const followInfo = {
36
- ...data.followCounts[0],
37
- status: data.follows?.[0]?.status,
38
- };
35
+ const followInfo =
36
+ 'follows' in data
37
+ ? {
38
+ ...data.followCounts[0],
39
+ status: data.follows?.[0]?.status,
40
+ }
41
+ : data.followCounts[0];
39
42
 
40
43
  if (client.cache) {
41
44
  pushToCache(getCacheKey(userId), followInfo, { cachedAt });
@@ -16,9 +16,10 @@ import { queryFollows } from './utils';
16
16
  */
17
17
  export const queryFollowers = async (query: {
18
18
  userId: Amity.User['userId'];
19
- status?: 'all' | Exclude<Amity.FollowStatus['status'], 'none'>;
20
- page?: Amity.Page;
21
- }): Promise<Amity.Cached<Amity.Paged<Amity.FollowStatus>>> => queryFollows('followers', query);
19
+ status?: Exclude<Amity.FollowStatusType, 'none'>;
20
+ page?: Amity.PageRaw;
21
+ }): Promise<Amity.Cached<Amity.Paged<Amity.FollowStatus, Amity.PageRaw>>> =>
22
+ queryFollows('followers', query);
22
23
 
23
24
  /**
24
25
  * ```js
@@ -35,5 +36,5 @@ export const queryFollowers = async (query: {
35
36
  */
36
37
  queryFollowers.locally = (
37
38
  query: Parameters<typeof queryFollowers>[0],
38
- ): Amity.Cached<Amity.Paged<Amity.FollowStatus>> | undefined =>
39
+ ): Amity.Cached<Amity.Paged<Amity.FollowStatus, Amity.PageRaw>> | undefined =>
39
40
  queryFollows.locally('followers', query);
@@ -16,9 +16,10 @@ import { queryFollows } from './utils';
16
16
  */
17
17
  export const queryFollowings = async (query: {
18
18
  userId: Amity.User['userId'];
19
- status?: Exclude<Amity.FollowStatus['status'], 'none'>;
20
- page?: Amity.Page;
21
- }): Promise<Amity.Cached<Amity.Paged<Amity.FollowStatus>>> => queryFollows('following', query);
19
+ status?: Exclude<Amity.FollowStatusType, 'none'>;
20
+ page?: Amity.PageRaw;
21
+ }): Promise<Amity.Cached<Amity.Paged<Amity.FollowStatus, Amity.PageRaw>>> =>
22
+ queryFollows('following', query);
22
23
 
23
24
  /**
24
25
  * ```js
@@ -35,5 +36,5 @@ export const queryFollowings = async (query: {
35
36
  */
36
37
  queryFollowings.locally = (
37
38
  query: Parameters<typeof queryFollowings>[0],
38
- ): Amity.Cached<Amity.Paged<Amity.FollowStatus>> | undefined =>
39
+ ): Amity.Cached<Amity.Paged<Amity.FollowStatus, Amity.PageRaw>> | undefined =>
39
40
  queryFollows.locally('following', query);
@@ -28,7 +28,7 @@ export const unfollow = async (userId: Amity.User['userId']): Promise<boolean> =
28
28
  ingestInCache(data);
29
29
  }
30
30
 
31
- fireEvent('follow.unfollowed', data.follows[0]);
31
+ fireEvent('follow.unfollowed', data);
32
32
 
33
33
  return true;
34
34
  };
@@ -1,5 +1,5 @@
1
1
  import { getActiveClient } from '~/client/api';
2
- import { toPage, toToken } from '~/core/query';
2
+ import { toPageRaw, toToken } from '~/core/query';
3
3
  import { ingestInCache } from '~/cache/api/ingestInCache';
4
4
  import { pullFromCache, pushToCache } from '~/cache/api';
5
5
  import { getResolver } from '~/core/model';
@@ -8,10 +8,10 @@ export const queryFollows = async (
8
8
  key: 'followers' | 'following',
9
9
  query: {
10
10
  userId: Amity.User['userId'];
11
- status?: 'all' | Exclude<Amity.FollowStatus['status'], 'none'>;
12
- page?: Amity.Page;
11
+ status?: Exclude<Amity.FollowStatusType, 'none'>;
12
+ page?: Amity.PageRaw;
13
13
  },
14
- ): Promise<Amity.Cached<Amity.Paged<Amity.FollowStatus>>> => {
14
+ ): Promise<Amity.Cached<Amity.Paged<Amity.FollowStatus, Amity.PageRaw>>> => {
15
15
  const client = getActiveClient();
16
16
  client.log(`follow/queryF${key.substring(1)}`, query);
17
17
 
@@ -22,7 +22,7 @@ export const queryFollows = async (
22
22
  {
23
23
  params: {
24
24
  ...params,
25
- token: toToken(page, 'skiplimit'),
25
+ token: toToken(page, 'afterbeforeraw'),
26
26
  },
27
27
  },
28
28
  );
@@ -43,8 +43,8 @@ export const queryFollows = async (
43
43
  pushToCache(cacheKey, { follows: follows.map(getResolver('follow')), paging });
44
44
  }
45
45
 
46
- const nextPage = toPage(paging.next);
47
- const prevPage = toPage(paging.previous);
46
+ const nextPage = toPageRaw(paging.next);
47
+ const prevPage = toPageRaw(paging.previous);
48
48
 
49
49
  return { data: follows, cachedAt, prevPage, nextPage };
50
50
  };
@@ -52,7 +52,7 @@ export const queryFollows = async (
52
52
  queryFollows.locally = (
53
53
  key: Parameters<typeof queryFollows>[0],
54
54
  query: Parameters<typeof queryFollows>[1],
55
- ): Amity.Cached<Amity.Paged<Amity.FollowStatus>> | undefined => {
55
+ ): Amity.Cached<Amity.Paged<Amity.FollowStatus, Amity.PageRaw>> | undefined => {
56
56
  const client = getActiveClient();
57
57
  client.log(`follow/queryF${key.substring(1)}.locally`, query);
58
58
 
@@ -73,8 +73,8 @@ queryFollows.locally = (
73
73
  .filter(Boolean)
74
74
  .map(({ data }) => data);
75
75
 
76
- const prevPage = toPage(data?.paging.previous);
77
- const nextPage = toPage(data?.paging.next);
76
+ const prevPage = toPageRaw(data?.paging.previous);
77
+ const nextPage = toPageRaw(data?.paging.next);
78
78
 
79
79
  return follows.length === data?.follows?.length
80
80
  ? { data: follows, cachedAt, prevPage, nextPage }
@@ -9,11 +9,11 @@ export const createFollowEventSubscriber = (
9
9
  ) => {
10
10
  const client = getActiveClient();
11
11
 
12
- const filter = (payload: Amity.FollowStatus) => {
12
+ const filter = (payload: Amity.FollowersPayload | Amity.FollowStatusPayload) => {
13
13
  if (!client.cache) {
14
- callback(payload);
14
+ callback(payload.follows[0]);
15
15
  } else {
16
- ingestInCache({ follows: [payload] });
16
+ ingestInCache(payload);
17
17
 
18
18
  const queries = [
19
19
  ...(queryCache(['followers', 'query']) ?? []),
@@ -21,11 +21,14 @@ export const createFollowEventSubscriber = (
21
21
  ];
22
22
 
23
23
  queries
24
- // @ts-ignore
25
- .filter(({ key }) => key[2].userId === payload.from || key[2].userId === payload.to)
24
+ .filter(
25
+ ({ key }) =>
26
+ // @ts-ignore
27
+ key[2].userId === payload.follows[0].from || key[2].userId === payload.follows[0].to,
28
+ )
26
29
  .forEach(({ key, data }) => upsertInCache(key, data as any, { cachedAt: -1 }));
27
30
 
28
- callback(payload);
31
+ callback(payload.follows[0]);
29
32
  }
30
33
  };
31
34
 
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ export * from './role/api';
25
25
  // // asc partials
26
26
  export * from './reaction/api';
27
27
  export * from './reaction/events';
28
+ export * from './reaction/observers';
28
29
 
29
30
  export * from './report/api';
30
31
 
@@ -20,18 +20,9 @@ import { getResolver } from '~/core/model';
20
20
  * @category Post API
21
21
  * @async
22
22
  */
23
- export const queryPosts = async (query: {
24
- targetId: string;
25
- targetType: Amity.Post['targetType'];
26
- sortBy?: 'lastCreated' | 'firstCreated' | 'lastUpdated' | 'firstUpdated';
27
- dataType?: string;
28
- isDeleted?: Amity.Post['isDeleted'];
29
- hasFlag?: boolean;
30
- feedType?: 'reviewing' | 'published';
31
- tags?: Amity.Taggable['tags'];
32
- matchingOnlyParentPost?: boolean;
33
- page?: Amity.PageRaw;
34
- }): Promise<Amity.Cached<Amity.Paged<Amity.Post, Amity.PageRaw>>> => {
23
+ export const queryPosts = async (
24
+ query: Amity.QueryPosts,
25
+ ): Promise<Amity.Cached<Amity.Paged<Amity.Post, Amity.PageRaw>>> => {
35
26
  const client = getActiveClient();
36
27
  client.log('post/queryPosts', query);
37
28
 
@@ -1,2 +1,3 @@
1
1
  export * from './observePosts';
2
2
  export * from './observePost';
3
+ export * from './livePosts';