@amityco/ts-sdk 0.0.1-beta.43 → 0.0.1-beta.44.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 (48) hide show
  1. package/dist/category/api/queryCategories.d.ts.map +1 -1
  2. package/dist/comment/api/queryComments.d.ts.map +1 -1
  3. package/dist/community/api/queryCommunities.d.ts.map +1 -1
  4. package/dist/community/observers/liveCommunities.d.ts.map +1 -1
  5. package/dist/core/query/filtering.d.ts +21 -0
  6. package/dist/core/query/filtering.d.ts.map +1 -1
  7. package/dist/index.cjs.js +116 -62
  8. package/dist/index.esm.js +115 -63
  9. package/dist/index.umd.js +4 -4
  10. package/dist/message/api/queryMessages.d.ts +1 -17
  11. package/dist/message/api/queryMessages.d.ts.map +1 -1
  12. package/dist/message/utils/prepareMessagePayload.d.ts.map +1 -1
  13. package/dist/post/api/queryPosts.d.ts.map +1 -1
  14. package/dist/post/observers/livePosts.d.ts.map +1 -1
  15. package/dist/subChannel/api/querySubChannels.d.ts.map +1 -1
  16. package/dist/utils/inferIsDeleted.d.ts +2 -0
  17. package/dist/utils/inferIsDeleted.d.ts.map +1 -0
  18. package/dist/utils/tests/inferIsDeleted.test.d.ts +2 -0
  19. package/dist/utils/tests/inferIsDeleted.test.d.ts.map +1 -0
  20. package/package.json +1 -1
  21. package/src/category/api/queryCategories.ts +3 -1
  22. package/src/category/observers/liveCategories.ts +1 -1
  23. package/src/category/observers/tests/liveCategories.test.ts +7 -5
  24. package/src/channel/api/tests/removeChannelMembers.test.ts +3 -4
  25. package/src/channel/observers/tests/liveChannels.test.ts +6 -6
  26. package/src/client/api/connectClient.ts +1 -1
  27. package/src/comment/api/queryComments.ts +3 -1
  28. package/src/comment/observers/liveComments.ts +1 -1
  29. package/src/community/api/queryCommunities.ts +2 -1
  30. package/src/community/api/test/queryCommunity.test.ts +2 -2
  31. package/src/community/observers/liveCommunities.ts +5 -8
  32. package/src/community/observers/tests/liveCommunities.test.ts +7 -94
  33. package/src/community/observers/tests/liveCommunity.test.ts +4 -4
  34. package/src/core/query/filtering.ts +79 -0
  35. package/src/core/query/query.ts +4 -4
  36. package/src/message/api/queryMessages.ts +7 -44
  37. package/src/message/api/tests/queryMessages.test.ts +2 -37
  38. package/src/message/observers/liveMessages.ts +2 -2
  39. package/src/message/tests/observers/liveMessage.test.ts +1 -1
  40. package/src/message/tests/observers/liveMessages.test.ts +1 -1
  41. package/src/message/utils/prepareMessagePayload.ts +3 -1
  42. package/src/post/api/queryPosts.ts +3 -1
  43. package/src/post/observers/livePosts.ts +7 -2
  44. package/src/post/observers/tests/livePosts.test.ts +10 -4
  45. package/src/subChannel/api/querySubChannels.ts +3 -1
  46. package/src/subChannel/observers/liveSubChannels.ts +1 -1
  47. package/src/utils/inferIsDeleted.ts +16 -0
  48. package/src/utils/tests/inferIsDeleted.test.ts +15 -0
@@ -12,21 +12,5 @@
12
12
  * @category Message API
13
13
  * @async
14
14
  */
15
- export declare const queryMessages: {
16
- (query: Amity.QueryMessages): Promise<Amity.Cached<Amity.Paged<Amity.Message, Amity.Page<string>>>>;
17
- /**
18
- * ```js
19
- * import { queryMessages } from '@amityco/ts-sdk'
20
- * const messages = queryMessages.locally!({ channelId })
21
- * ```
22
- *
23
- * Queries a paginable list of {@link Amity.Message} objects from cache
24
- *
25
- * @param query The query parameters
26
- * @returns A page of {@link Amity.Message} objects
27
- *
28
- * @category Message API
29
- */
30
- locally(query: Parameters<typeof queryMessages>[0]): Amity.Cached<Amity.Paged<Amity.Message, Amity.Page<string>>> | undefined;
31
- };
15
+ export declare const queryMessages: (query: Amity.QueryMessages) => Promise<Amity.Cached<Amity.Paged<Amity.Message, Amity.Page<string>>>>;
32
16
  //# sourceMappingURL=queryMessages.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"queryMessages.d.ts","sourceRoot":"","sources":["../../../src/message/api/queryMessages.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa;YACjB,MAAM,aAAa,GACzB,QAAQ,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,OAAO,EAAE,MAAM,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAuCxE;;;;;;;;;;;;OAYG;mBAEM,WAAW,oBAAoB,CAAC,CAAC,CAAC,CAAC,GACzC,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,OAAO,EAAE,MAAM,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS;CAlB1E,CAAC"}
1
+ {"version":3,"file":"queryMessages.d.ts","sourceRoot":"","sources":["../../../src/message/api/queryMessages.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa,UACjB,MAAM,aAAa,KACzB,QAAQ,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,OAAO,EAAE,MAAM,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CA0CtE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"prepareMessagePayload.d.ts","sourceRoot":"","sources":["../../../src/message/utils/prepareMessagePayload.ts"],"names":[],"mappings":"AA0BA,wBAAgB,cAAc,CAC5B,OAAO,EAAE,KAAK,CAAC,UAAU,EACzB,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,GAC3B,KAAK,CAAC,OAAO,CA8Df;AAED,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,KAAK,CAAC,OAAO,EACtB,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,GAC3B,KAAK,CAAC,OAAO,CASf;AAED,eAAO,MAAM,qBAAqB,YACvB,MAAM,cAAc,KAC5B,QAAQ,MAAM,uBAAuB,CAsBvC,CAAC;AAEF,aAAK,gBAAgB,GAAG,IAAI,CAC1B,KAAK,CAAC,aAAa,EAInB,MAAM,GAAG,QAAQ,GAAG,cAAc,GAAG,MAAM,GAAG,gBAAgB,CAC/D,GAAG;IACF,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IAC1C,SAAS,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACvC,aAAa,EAAE,KAAK,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IACnD,OAAO,EAAE;QACP,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACvC,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;CACH,CAAC;AAEF,wBAAgB,aAAa,CAAC,EAC5B,YAAY,EACZ,UAAU,EACV,GAAG,IAAI,EACR,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAE9C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,IAAI,EACJ,MAAM,EACN,YAAY,EACZ,IAAI,EACJ,cAAc,EACd,GAAG,IAAI,EACR,EAAE,KAAK,CAAC,aAAa,GAAG,gBAAgB,CAiBxC"}
1
+ {"version":3,"file":"prepareMessagePayload.d.ts","sourceRoot":"","sources":["../../../src/message/utils/prepareMessagePayload.ts"],"names":[],"mappings":"AA4BA,wBAAgB,cAAc,CAC5B,OAAO,EAAE,KAAK,CAAC,UAAU,EACzB,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,GAC3B,KAAK,CAAC,OAAO,CA8Df;AAED,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,KAAK,CAAC,OAAO,EACtB,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,GAC3B,KAAK,CAAC,OAAO,CASf;AAED,eAAO,MAAM,qBAAqB,YACvB,MAAM,cAAc,KAC5B,QAAQ,MAAM,uBAAuB,CAsBvC,CAAC;AAEF,aAAK,gBAAgB,GAAG,IAAI,CAC1B,KAAK,CAAC,aAAa,EAInB,MAAM,GAAG,QAAQ,GAAG,cAAc,GAAG,MAAM,GAAG,gBAAgB,CAC/D,GAAG;IACF,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IAC1C,SAAS,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACvC,aAAa,EAAE,KAAK,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IACnD,OAAO,EAAE;QACP,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACvC,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;CACH,CAAC;AAEF,wBAAgB,aAAa,CAAC,EAC5B,YAAY,EACZ,UAAU,EACV,GAAG,IAAI,EACR,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAE9C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,IAAI,EACJ,MAAM,EACN,YAAY,EACZ,IAAI,EACJ,cAAc,EACd,GAAG,IAAI,EACR,EAAE,KAAK,CAAC,aAAa,GAAG,gBAAgB,CAiBxC"}
@@ -1 +1 @@
1
- {"version":3,"file":"queryPosts.d.ts","sourceRoot":"","sources":["../../../src/post/api/queryPosts.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,UAAU;YACd,MAAM,UAAU,GACtB,QAAQ,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAmDhE;;;;;;;;;;;;OAYG;mBAEM,WAAW,iBAAiB,CAAC,CAAC,CAAC,CAAC,GACtC,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,GAAG,SAAS;CAlBlE,CAAC"}
1
+ {"version":3,"file":"queryPosts.d.ts","sourceRoot":"","sources":["../../../src/post/api/queryPosts.ts"],"names":[],"mappings":"AAcA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,UAAU;YACd,MAAM,UAAU,GACtB,QAAQ,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAmDhE;;;;;;;;;;;;OAYG;mBAEM,WAAW,iBAAiB,CAAC,CAAC,CAAC,CAAC,GACtC,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,GAAG,SAAS;CAlBlE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"livePosts.d.ts","sourceRoot":"","sources":["../../../src/post/observers/livePosts.ts"],"names":[],"mappings":"AAmCA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,SAAS,WACZ,MAAM,kBAAkB,YACtB,MAAM,sBAAsB,CAAC,MAAM,IAAI,CAAC,WACzC,MAAM,oBAAoB,KAClC,MAAM,YAgIR,CAAC"}
1
+ {"version":3,"file":"livePosts.d.ts","sourceRoot":"","sources":["../../../src/post/observers/livePosts.ts"],"names":[],"mappings":"AAoCA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,SAAS,WACZ,MAAM,kBAAkB,YACtB,MAAM,sBAAsB,CAAC,MAAM,IAAI,CAAC,WACzC,MAAM,oBAAoB,KAClC,MAAM,YAoIR,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"querySubChannels.d.ts","sourceRoot":"","sources":["../../../src/subChannel/api/querySubChannels.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,gBAAgB;YACpB,MAAM,gBAAgB,GAC5B,QAAQ,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,UAAU,CAAC,CAAC,CAAC;IA4CvD;;;;;;;;;;;;OAYG;mBAEM,WAAW,uBAAuB,CAAC,CAAC,CAAC,CAAC,GAC5C,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,UAAU,CAAC,CAAC,GAAG,SAAS;CAlBzD,CAAC"}
1
+ {"version":3,"file":"querySubChannels.d.ts","sourceRoot":"","sources":["../../../src/subChannel/api/querySubChannels.ts"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,gBAAgB;YACpB,MAAM,gBAAgB,GAC5B,QAAQ,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,UAAU,CAAC,CAAC,CAAC;IA4CvD;;;;;;;;;;;;OAYG;mBAEM,WAAW,uBAAuB,CAAC,CAAC,CAAC,CAAC,GAC5C,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,UAAU,CAAC,CAAC,GAAG,SAAS;CAlBzD,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const inferIsDeleted: (includeDeleted?: boolean) => boolean | undefined;
2
+ //# sourceMappingURL=inferIsDeleted.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inferIsDeleted.d.ts","sourceRoot":"","sources":["../../src/utils/inferIsDeleted.ts"],"names":[],"mappings":"AAcA,eAAO,MAAM,cAAc,oBAAqB,OAAO,KAAG,OAAO,GAAG,SACvB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=inferIsDeleted.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inferIsDeleted.test.d.ts","sourceRoot":"","sources":["../../../src/utils/tests/inferIsDeleted.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amityco/ts-sdk",
3
- "version": "0.0.1-beta.43",
3
+ "version": "0.0.1-beta.44.0",
4
4
  "license": "CC-BY-ND-4.0",
5
5
  "author": "amity.co <developers@amity.co> (https://amity.co)",
6
6
  "description": "Amity Social Cloud Typescript SDK",
@@ -6,6 +6,8 @@ import { pullFromCache, pushToCache } from '~/cache/api';
6
6
  import { ingestInCache } from '~/cache/api/ingestInCache';
7
7
  import { getResolver } from '~/core/model';
8
8
 
9
+ import { inferIsDeleted } from '~/utils/inferIsDeleted';
10
+
9
11
  /* begin_public_function
10
12
  id: community.category.query
11
13
  */
@@ -36,7 +38,7 @@ export const queryCategories = async (
36
38
  {
37
39
  params: {
38
40
  ...params,
39
- isDeleted: includeDeleted === false ? false : undefined,
41
+ isDeleted: inferIsDeleted(includeDeleted),
40
42
  options: {
41
43
  token: toToken(page, 'skiplimit'),
42
44
  },
@@ -63,7 +63,7 @@ export const liveCategories = (
63
63
  .filter(Boolean)
64
64
  .map(({ data }) => data) ?? [];
65
65
 
66
- if (params.includeDeleted === false) {
66
+ if (!params.includeDeleted) {
67
67
  categories = filterByPropEquality(categories, 'isDeleted', false);
68
68
  }
69
69
 
@@ -22,7 +22,9 @@ const getSnapshot = (params?: Record<string, any>) => {
22
22
  };
23
23
  };
24
24
 
25
- describe('live Caategories', () => {
25
+ const params = { includeDeleted: true };
26
+
27
+ describe('live Categories', () => {
26
28
  beforeAll(connectClient);
27
29
  afterAll(disconnectClient);
28
30
 
@@ -37,7 +39,7 @@ describe('live Caategories', () => {
37
39
  const callback = jest.fn();
38
40
  client.http.get = jest.fn().mockResolvedValue(categoryQueryResponse);
39
41
 
40
- liveCategories({}, callback);
42
+ liveCategories(params, callback);
41
43
  await pause();
42
44
 
43
45
  expect(callback).toHaveBeenCalledTimes(2);
@@ -57,9 +59,9 @@ describe('live Caategories', () => {
57
59
  const callback = jest.fn();
58
60
  client.http.get = jest.fn().mockResolvedValue(categoryQueryResponse);
59
61
 
60
- liveCategories({}, () => undefined);
62
+ liveCategories(params, () => undefined);
61
63
  await pause();
62
- liveCategories({}, callback);
64
+ liveCategories(params, callback);
63
65
  await pause();
64
66
 
65
67
  expect(callback).toHaveBeenCalledTimes(2);
@@ -116,7 +118,7 @@ describe('live Caategories', () => {
116
118
  .mockResolvedValue(categoryQueryResponse)
117
119
  .mockResolvedValueOnce(categoryQueryResponsePage2);
118
120
 
119
- liveCategories({}, callback);
121
+ liveCategories(params, callback);
120
122
  await pause();
121
123
 
122
124
  expect(callback).toHaveBeenCalled();
@@ -91,11 +91,10 @@ describe('removeChannelMembers', () => {
91
91
  await pause();
92
92
 
93
93
  /*
94
- * Assertion below is required to check if the event handler is called with the right
95
- * params. It's not enough to check if the handler does not throw
94
+ * Removed assertion for checking what the callback was called with as it's
95
+ * impossible to guarantee the order in which the events will be fired
96
96
  */
97
- expect(callback).toHaveBeenNthCalledWith(1, channel1, channelUser);
98
- expect(callback).toHaveBeenNthCalledWith(2, channel1, channelUser3);
97
+ expect(callback).toHaveBeenCalledTimes(2);
99
98
 
100
99
  unsub();
101
100
  });
@@ -9,6 +9,7 @@ import {
9
9
  client,
10
10
  connectClient,
11
11
  convertRawChannelPayload,
12
+ convertChannelFromRaw,
12
13
  disconnectClient,
13
14
  generateRawChannel,
14
15
  generateRawMessage,
@@ -20,7 +21,6 @@ import {
20
21
  } from '~/utils/tests';
21
22
 
22
23
  import { liveChannels } from '../liveChannels';
23
- import { convertFromRaw } from '../../utils';
24
24
  import * as getChannelMarkers from '../../../marker/api/getChannelMarkers';
25
25
 
26
26
  const getSnapshot = (params?: Record<string, any>) => {
@@ -99,17 +99,17 @@ describe('liveChannels', () => {
99
99
  channelId: 'deleted-channel-id',
100
100
  isDeleted: true,
101
101
  });
102
- const deletedChannel = convertFromRaw(rawDeletedChannel);
102
+ const deletedChannel = convertChannelFromRaw(rawDeletedChannel);
103
103
  const rawTaggedChannel = generateRawChannel({
104
104
  channelId: 'tagged-channel-id',
105
105
  tags: ['tag1', 'tag2'],
106
106
  });
107
- const taggedChannel = convertFromRaw(rawTaggedChannel);
107
+ const taggedChannel = convertChannelFromRaw(rawTaggedChannel);
108
108
  const rawTaggedChannel2 = generateRawChannel({
109
109
  channelId: 'tagged-channel-id-2',
110
110
  tags: ['tag2'],
111
111
  });
112
- const taggedChannel2 = convertFromRaw(rawTaggedChannel2);
112
+ const taggedChannel2 = convertChannelFromRaw(rawTaggedChannel2);
113
113
 
114
114
  const filters: [string, Amity.ChannelLiveCollection, Amity.Channel[]][] = [
115
115
  ['deleted', { isDeleted: false }, [channel1, taggedChannel, taggedChannel2]],
@@ -200,13 +200,13 @@ describe('liveChannels', () => {
200
200
  'it should add new channel to collection onCreate',
201
201
  'channel.created',
202
202
  rawNewChannel,
203
- [convertFromRaw(rawNewChannel), channel1, channel2],
203
+ [convertChannelFromRaw(rawNewChannel), channel1, channel2],
204
204
  ],
205
205
  [
206
206
  'it should add new channel to collection onJoin',
207
207
  'channel.joined',
208
208
  rawNewChannel,
209
- [convertFromRaw(rawNewChannel), channel1, channel2],
209
+ [convertChannelFromRaw(rawNewChannel), channel1, channel2],
210
210
  ],
211
211
  [
212
212
  'it should update channel in collection onUpdate',
@@ -107,8 +107,8 @@ export const connectClient = async (
107
107
  * token expires
108
108
  */
109
109
  unsubWatcher = client.accessTokenExpiryWatcher(
110
- client.token?.issuedAt!,
111
110
  client.token?.expiresAt!,
111
+ client.token?.issuedAt!,
112
112
  sessionHandler!,
113
113
  );
114
114
 
@@ -5,6 +5,8 @@ import { pullFromCache, pushToCache } from '~/cache/api';
5
5
  import { ingestInCache } from '~/cache/api/ingestInCache';
6
6
  import { getResolver } from '~/core/model';
7
7
 
8
+ import { inferIsDeleted } from '~/utils/inferIsDeleted';
9
+
8
10
  /* begin_public_function
9
11
  id: comment.query
10
12
  */
@@ -44,7 +46,7 @@ export const queryComments = async (
44
46
  {
45
47
  params: {
46
48
  ...params,
47
- isDeleted: includeDeleted === false ? false : undefined,
49
+ isDeleted: inferIsDeleted(includeDeleted),
48
50
  // filterByParentId, API-FIX: backend does not support this boolean LOL. what the hell seriously.
49
51
  options,
50
52
  },
@@ -82,7 +82,7 @@ export const liveComments = (
82
82
  .filter(Boolean)
83
83
  .map(({ data }) => data) ?? [];
84
84
 
85
- if (params.includeDeleted === false) {
85
+ if (!params.includeDeleted) {
86
86
  comments = filterByPropEquality(comments, 'isDeleted', false);
87
87
  }
88
88
 
@@ -5,6 +5,7 @@ import { pushToCache } from '~/cache/api';
5
5
  import { ingestInCache } from '~/cache/api/ingestInCache';
6
6
  import { getResolver } from '~/core/model';
7
7
 
8
+ import { inferIsDeleted } from '~/utils/inferIsDeleted';
8
9
  import { prepareCommunityPayload } from '../utils';
9
10
 
10
11
  /* begin_public_function
@@ -43,7 +44,7 @@ export const queryCommunities = async (
43
44
  {
44
45
  params: {
45
46
  ...restParams,
46
- isDeleted: includeDeleted === false ? false : undefined,
47
+ isDeleted: inferIsDeleted(includeDeleted),
47
48
  keyword: displayName,
48
49
  filter: membership,
49
50
  options: {
@@ -89,14 +89,14 @@ describe('queryCommunities', () => {
89
89
  expect(mockFn.mock.lastCall[1].params.isDeleted).toBe(false);
90
90
  });
91
91
 
92
- test('it should set isDeleted to undefined if includeDeleted is undefined', async () => {
92
+ test('it should set isDeleted to false if includeDeleted is undefined', async () => {
93
93
  const mockFn = jest.fn();
94
94
  client.http.get = mockFn.mockResolvedValue(resolvedGetValue);
95
95
 
96
96
  await queryCommunities(queryCriteriaWithoutPage);
97
97
 
98
98
  expect(mockFn.mock.lastCall).toHaveLength(2);
99
- expect(mockFn.mock.lastCall[1].params.isDeleted).toBeUndefined();
99
+ expect(mockFn.mock.lastCall[1].params.isDeleted).toBe(false);
100
100
  });
101
101
  });
102
102
 
@@ -5,6 +5,7 @@ import { getActiveClient } from '~/client/api';
5
5
  import {
6
6
  createQuery,
7
7
  filterByPropEquality,
8
+ filterByCommunityMembership,
8
9
  queryOptions,
9
10
  runQuery,
10
11
  sortByDisplayName,
@@ -51,7 +52,7 @@ export const liveCommunities = (
51
52
  callback: Amity.LiveCollectionCallback<Amity.Community>,
52
53
  config?: Amity.LiveCollectionConfig,
53
54
  ) => {
54
- const { log, cache } = getActiveClient();
55
+ const { log, cache, userId } = getActiveClient();
55
56
 
56
57
  if (!cache) {
57
58
  console.log(ENABLE_CACHE_MESSAGE);
@@ -77,7 +78,7 @@ export const liveCommunities = (
77
78
 
78
79
  communities = filterByPropEquality(communities, 'displayName', params.displayName);
79
80
 
80
- if (params.includeDeleted === false) {
81
+ if (!params.includeDeleted) {
81
82
  communities = filterByPropEquality(communities, 'isDeleted', false);
82
83
  }
83
84
 
@@ -89,12 +90,8 @@ export const liveCommunities = (
89
90
  communities = communities.filter(c => c.tags?.some(t => params.tags?.includes(t)));
90
91
  }
91
92
 
92
- if (params.membership === 'member') {
93
- communities = filterByPropEquality(communities, 'isJoined', true);
94
- }
95
-
96
- if (params.membership === 'notMember') {
97
- communities = filterByPropEquality(communities, 'isJoined', false);
93
+ if (params.membership && userId) {
94
+ communities = filterByCommunityMembership(communities, params.membership, userId);
98
95
  }
99
96
 
100
97
  const sortBy = params.sortBy || 'lastCreated';
@@ -39,7 +39,7 @@ describe('liveCommunities', () => {
39
39
  const callback = jest.fn();
40
40
  client.http.get = jest.fn().mockResolvedValue(communityQueryResponse);
41
41
 
42
- liveCommunities({}, callback);
42
+ liveCommunities({ includeDeleted: true }, callback);
43
43
  await pause();
44
44
 
45
45
  expect(callback).toHaveBeenCalledTimes(2);
@@ -59,9 +59,9 @@ describe('liveCommunities', () => {
59
59
  const callback = jest.fn();
60
60
  client.http.get = jest.fn().mockResolvedValue(communityQueryResponse);
61
61
 
62
- liveCommunities({}, () => undefined);
62
+ liveCommunities({ includeDeleted: true }, () => undefined);
63
63
  await pause();
64
- liveCommunities({}, callback);
64
+ liveCommunities({ includeDeleted: true }, callback);
65
65
  await pause();
66
66
 
67
67
  expect(callback).toHaveBeenCalledTimes(2);
@@ -86,12 +86,9 @@ describe('liveCommunities', () => {
86
86
 
87
87
  const filters: [string, Amity.CommunityLiveCollection, Amity.Community[]][] = [
88
88
  ['not deleted', { includeDeleted: false }, [community2]],
89
- ['categoryId', { categoryId: 'test-category-id' }, [community1]],
90
- ['displayName', { displayName: 'community 1' }, [community1]],
91
- ['tagged', { tags: ['test-community-tag'] }, [community1]],
92
- ['member', { membership: 'member' }, [community1]],
93
- ['not member', { membership: 'notMember' }, [community2]],
94
- ['all membership', { membership: 'all' }, [community1, community2]],
89
+ ['categoryId', { categoryId: 'test-category-id', includeDeleted: true }, [community1]],
90
+ ['displayName', { displayName: 'community 1', includeDeleted: true }, [community1]],
91
+ ['tagged', { tags: ['test-community-tag'], includeDeleted: true }, [community1]],
95
92
  ];
96
93
 
97
94
  test.each(filters)('it should filter by %s communities', async (filter, params, expected) => {
@@ -124,7 +121,7 @@ describe('liveCommunities', () => {
124
121
  .mockResolvedValue(communityQueryResponse)
125
122
  .mockResolvedValueOnce(communityQueryResponsePage2);
126
123
 
127
- liveCommunities({}, callback);
124
+ liveCommunities({ includeDeleted: true }, callback);
128
125
  await pause();
129
126
 
130
127
  expect(callback).toHaveBeenCalled();
@@ -216,89 +213,5 @@ describe('liveCommunities', () => {
216
213
  ),
217
214
  );
218
215
  });
219
-
220
- const joinCommunityPayload = {
221
- ...communityQueryResponse.data.communities[0],
222
- communityId: 'joining-community',
223
- isDeleted: false,
224
- };
225
-
226
- const joinedCommunity = prepareCommunityPayload({
227
- ...emptyCommunityPayload,
228
- communities: [joinCommunityPayload],
229
- }).communities[0];
230
-
231
- const joiningUser: Amity.RawMembership<'community'> = {
232
- ...communityUser11,
233
- userId: 'test',
234
- communityId: 'joining-community',
235
- communityMembership: 'member',
236
- };
237
-
238
- const memberEvents: [
239
- string,
240
- Amity.CommunityLiveCollection,
241
- keyof Amity.Events,
242
- Amity.CommunityPayload,
243
- Amity.Community[],
244
- ][] = [
245
- [
246
- 'it should add new community to collection onJoin',
247
- { membership: 'member' },
248
- 'community.joined',
249
- {
250
- ...emptyCommunityPayload,
251
- communities: [joinCommunityPayload],
252
- communityUsers: [joiningUser],
253
- },
254
- [joinedCommunity, community1],
255
- ],
256
- [
257
- 'it should not add new member to community collection onJoin if membership:not member',
258
- { membership: 'notMember' },
259
- 'community.joined',
260
- {
261
- ...emptyCommunityPayload,
262
- communities: [joinCommunityPayload],
263
- communityUsers: [joiningUser],
264
- },
265
- [],
266
- ],
267
- [
268
- 'it should update community in collection onLeft',
269
- { membership: 'member' },
270
- 'community.left',
271
- { ...communityPayload, communities: [{ ...community1, isJoined: false }] },
272
- [],
273
- ],
274
- [
275
- 'it should add new community in collection onLeft and if membership:not member',
276
- { membership: 'notMember' },
277
- 'community.left',
278
- { ...communityPayload, communities: [{ ...community1, isJoined: false }] },
279
- [{ ...community1, isJoined: false }],
280
- ],
281
- ];
282
-
283
- test.each(memberEvents)('%s', async (test, params, event, communityPayload, expected) => {
284
- const callback = jest.fn();
285
- client.http.get = jest.fn().mockResolvedValue(communityQueryResponsePage2);
286
-
287
- liveCommunities(params, callback);
288
- await pause();
289
-
290
- client.emitter.emit(event, communityPayload);
291
- await pause();
292
-
293
- expect(callback).toHaveBeenNthCalledWith(
294
- 3,
295
- expect.objectContaining(
296
- getSnapshot({
297
- data: expected,
298
- loading: false,
299
- }),
300
- ),
301
- );
302
- });
303
216
  });
304
217
  });
@@ -81,10 +81,10 @@ describe('liveCommunity events', () => {
81
81
 
82
82
  const communityUserEvents: [string, keyof Amity.MqttCommunityUserEvents][] = [
83
83
  ['it should add user on community.joined event', 'community.joined'],
84
- ['should get update on community.left event', 'community.left'],
85
- ['should get update on community.userChanged event', 'community.userChanged'],
86
- ['should get update on community.userBanned event', 'community.userBanned'],
87
- ['should get update on community.userUnbanned event', 'community.userUnbanned'],
84
+ ['it should get update on community.left event', 'community.left'],
85
+ ['it should get update on community.userChanged event', 'community.userChanged'],
86
+ ['it should get update on community.userBanned event', 'community.userBanned'],
87
+ ['it should get update on community.userUnbanned event', 'community.userUnbanned'],
88
88
  ];
89
89
 
90
90
  test.each(communityUserEvents)('%s', async (test, event) => {
@@ -95,3 +95,82 @@ export const filterByFeedType = <T extends Amity.Post>(
95
95
  return feed && feed.feedType === feedType;
96
96
  });
97
97
  };
98
+
99
+ /**
100
+ * Filter a community collection by membership of the userId
101
+ *
102
+ * @param collection the community to filter
103
+ * @param membership the membership to be filtered by
104
+ * @param userId user id to be filtered by
105
+ * @returns a filtered collection with items only matching the criteria
106
+ *
107
+ * @hidden
108
+ */
109
+ export const filterByCommunityMembership = <T extends Amity.Community>(
110
+ collection: T[],
111
+ membership: Amity.CommunityLiveCollection['membership'],
112
+ userId: Amity.Membership<'community'>['userId'],
113
+ ): T[] => {
114
+ if (membership === 'all') {
115
+ return collection;
116
+ }
117
+
118
+ return collection.filter(c => {
119
+ // get resolver for the community by user
120
+ const communityUserCacheKey = getResolver('communityUsers')({
121
+ communityId: c.communityId,
122
+ userId,
123
+ });
124
+
125
+ const communityUser = pullFromCache<Amity.Membership<'community'>>([
126
+ 'communityUsers',
127
+ 'get',
128
+ communityUserCacheKey,
129
+ ])?.data;
130
+
131
+ if (membership === 'member') {
132
+ return communityUser && communityUser.communityMembership === 'member';
133
+ }
134
+
135
+ // only membership value remainging is 'notMember'
136
+ return !communityUser || communityUser.communityMembership !== 'none';
137
+ });
138
+ };
139
+
140
+ /**
141
+ * Filter a post collection by dataType
142
+ *
143
+ * @param collection the post to filter
144
+ * @param dataTypes of the post to be filtered by
145
+ * @returns a filtered collection with items only matching the criteria
146
+ *
147
+ * @hidden
148
+ */
149
+ export const filterByPostDataTypes = <T extends Amity.Post>(
150
+ collection: T[],
151
+ dataTypes: Amity.PostLiveCollection['dataTypes'],
152
+ ): T[] => {
153
+ const filteredPosts: T[] = [];
154
+
155
+ collection.forEach(c => {
156
+ /*
157
+ * A text post with no children is just a text post
158
+ */
159
+ if (c.dataType === 'text' && !c.children.length) return;
160
+
161
+ if (c.children.length) {
162
+ const childPost = pullFromCache<Amity.Post>(['post', 'get', c.children[0]])?.data;
163
+
164
+ if (childPost && dataTypes?.includes(childPost?.dataType)) {
165
+ // @ts-ignore
166
+ filteredPosts.push(childPost);
167
+ }
168
+
169
+ return;
170
+ }
171
+
172
+ if (dataTypes?.includes(c.dataType)) filteredPosts.push(c);
173
+ });
174
+
175
+ return filteredPosts;
176
+ };
@@ -165,11 +165,11 @@ export const runQuery = <Args extends any[], Returned extends any>(
165
165
  callback?: (args: Amity.Snapshot<Returned>) => void,
166
166
  options: Amity.QueryOptions = queryOptions('cache_then_server'),
167
167
  ) => {
168
- console.warn(
169
- 'Deprecation Notice: After 1 April 2023, CreateQuery/RunQuery will no longer be supported. You will be able to use the API without either methods. Check https://docs.amity.co/ for details',
168
+ console.info(
169
+ 'Deprecation Notice: By Q3, CreateQuery/RunQuery will no longer be exported from the SDK. You will be able to use the API without either methods.',
170
170
  );
171
- console.warn(
172
- 'Deprecation Notice: After 1 March 2023, individual module imoprts will no longer be supported. All public APIs will need to be imported from their respective repositories. Ex: CommunityRepository, UserRepository, etc. Check https://docs.amity.co/ for detailed info.',
171
+ console.info(
172
+ 'Deprecation Notice: From v6.0.0, individual module imports will no longer be supported. All public APIs will need to be imported from their respective repositories. Ex: CommunityRepository, UserRepository, etc.',
173
173
  );
174
174
 
175
175
  let local: Returned | undefined;
@@ -2,7 +2,7 @@ import { getActiveClient } from '~/client/api';
2
2
 
3
3
  import { fireEvent } from '~/core/events';
4
4
  import { toPageRaw } from '~/core/query';
5
- import { pullFromCache, pushToCache } from '~/cache/api';
5
+ import { pushToCache } from '~/cache/api';
6
6
  import { ingestInCache } from '~/cache/api/ingestInCache';
7
7
  import { getResolver } from '~/core/model';
8
8
  import { convertQueryParams, prepareMessagePayload } from '../utils';
@@ -50,6 +50,12 @@ export const queryMessages = async (
50
50
  const cachedAt = client.cache && Date.now();
51
51
 
52
52
  if (client.cache) {
53
+ /*
54
+ * queryMessages.locally is unsupported for messages as message list updates
55
+ * frequently and leads to bugs when user switches between channels, i.e. when
56
+ * the public function (live messages) calls unmount. The list fetched on
57
+ * after from the cache is stale.
58
+ */
53
59
  ingestInCache(data, { cachedAt });
54
60
 
55
61
  const cacheKey = ['message', 'query', params as Amity.Serializable];
@@ -64,46 +70,3 @@ export const queryMessages = async (
64
70
  return { data: messages, cachedAt, prevPage, nextPage };
65
71
  };
66
72
  /* end_public_function */
67
-
68
- /**
69
- * ```js
70
- * import { queryMessages } from '@amityco/ts-sdk'
71
- * const messages = queryMessages.locally!({ channelId })
72
- * ```
73
- *
74
- * Queries a paginable list of {@link Amity.Message} objects from cache
75
- *
76
- * @param query The query parameters
77
- * @returns A page of {@link Amity.Message} objects
78
- *
79
- * @category Message API
80
- */
81
- queryMessages.locally = (
82
- query: Parameters<typeof queryMessages>[0],
83
- ): Amity.Cached<Amity.Paged<Amity.Message, Amity.Page<string>>> | undefined => {
84
- const client = getActiveClient();
85
- client.log('message/queryMessages.locally', query);
86
-
87
- if (!client.cache) return;
88
-
89
- const params = convertQueryParams(query);
90
-
91
- const queryKey = ['message', 'query', params as Amity.Serializable];
92
- const { data, cachedAt } =
93
- pullFromCache<{ messages: Pick<Amity.Message, 'messageId'>[] } & Amity.Pagination>(queryKey) ??
94
- {};
95
-
96
- if (!data?.messages.length) return;
97
-
98
- const messages: Amity.Message[] = data.messages
99
- .map(messageId => pullFromCache<Amity.Message>(['message', 'get', messageId])!)
100
- .filter(Boolean)
101
- .map(({ data }) => data);
102
-
103
- const prevPage = toPageRaw(data?.paging.previous);
104
- const nextPage = toPageRaw(data?.paging.next);
105
-
106
- return messages.length === data?.messages?.length
107
- ? { data: messages, cachedAt, prevPage, nextPage }
108
- : undefined;
109
- };