@amityco/ts-sdk-react-native 6.9.1-e6a3d26.0 → 6.10.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 (47) hide show
  1. package/dist/channelRepsitory/observers/getChannels.d.ts.map +1 -1
  2. package/dist/client/api/logout.d.ts.map +1 -1
  3. package/dist/commentRepository/events/onCommentCreated.d.ts +3 -3
  4. package/dist/commentRepository/events/onCommentCreated.d.ts.map +1 -1
  5. package/dist/commentRepository/events/onCommentDeleted.d.ts +3 -3
  6. package/dist/commentRepository/events/onCommentDeleted.d.ts.map +1 -1
  7. package/dist/commentRepository/events/onCommentFlagged.d.ts +3 -3
  8. package/dist/commentRepository/events/onCommentFlagged.d.ts.map +1 -1
  9. package/dist/commentRepository/events/onCommentReactionAdded.d.ts +3 -3
  10. package/dist/commentRepository/events/onCommentReactionAdded.d.ts.map +1 -1
  11. package/dist/commentRepository/events/onCommentReactionRemoved.d.ts +3 -3
  12. package/dist/commentRepository/events/onCommentReactionRemoved.d.ts.map +1 -1
  13. package/dist/commentRepository/events/onCommentUnflagged.d.ts +3 -3
  14. package/dist/commentRepository/events/onCommentUnflagged.d.ts.map +1 -1
  15. package/dist/commentRepository/events/onCommentUpdated.d.ts +3 -3
  16. package/dist/commentRepository/events/onCommentUpdated.d.ts.map +1 -1
  17. package/dist/commentRepository/events/utils.d.ts +1 -1
  18. package/dist/commentRepository/events/utils.d.ts.map +1 -1
  19. package/dist/commentRepository/observers/getComment.d.ts +2 -2
  20. package/dist/commentRepository/observers/getComment.d.ts.map +1 -1
  21. package/dist/commentRepository/observers/getComments.d.ts.map +1 -1
  22. package/dist/core/device.d.ts.map +1 -1
  23. package/dist/core/events.d.ts.map +1 -1
  24. package/dist/core/query/filtering.d.ts.map +1 -1
  25. package/dist/index.cjs.js +113 -86
  26. package/dist/index.esm.js +113 -86
  27. package/dist/index.umd.js +2 -2
  28. package/dist/utils/tests/dummy/comment.d.ts +40 -1
  29. package/dist/utils/tests/dummy/comment.d.ts.map +1 -1
  30. package/package.json +1 -1
  31. package/src/channelRepsitory/observers/getChannels.ts +17 -2
  32. package/src/client/api/logout.ts +7 -5
  33. package/src/commentRepository/events/onCommentCreated.ts +4 -5
  34. package/src/commentRepository/events/onCommentDeleted.ts +4 -5
  35. package/src/commentRepository/events/onCommentFlagged.ts +4 -5
  36. package/src/commentRepository/events/onCommentReactionAdded.ts +6 -5
  37. package/src/commentRepository/events/onCommentReactionRemoved.ts +6 -5
  38. package/src/commentRepository/events/onCommentUnflagged.ts +4 -5
  39. package/src/commentRepository/events/onCommentUpdated.ts +4 -5
  40. package/src/commentRepository/events/utils.ts +4 -3
  41. package/src/commentRepository/observers/getComment.ts +2 -2
  42. package/src/commentRepository/observers/getComments.ts +5 -1
  43. package/src/commentRepository/observers/tests/getComment.test.ts +18 -14
  44. package/src/core/device.ts +2 -1
  45. package/src/core/events.ts +6 -1
  46. package/src/core/query/filtering.ts +24 -17
  47. package/src/utils/tests/dummy/comment.ts +16 -9
package/dist/index.esm.js CHANGED
@@ -82,8 +82,8 @@ const PostContentType = Object.freeze({
82
82
 
83
83
  function getVersion() {
84
84
  try {
85
- // the string ''v6.9.0-esm'' should be replaced by actual value by @rollup/plugin-replace
86
- return 'v6.9.0-esm';
85
+ // the string ''v6.10.0-esm'' should be replaced by actual value by @rollup/plugin-replace
86
+ return 'v6.10.0-esm';
87
87
  }
88
88
  catch (error) {
89
89
  return '__dev__';
@@ -1178,26 +1178,35 @@ const filterByCommunityMembership = (collection, membership, userId) => {
1178
1178
  * @hidden
1179
1179
  */
1180
1180
  const filterByPostDataTypes = (collection, dataTypes) => {
1181
- const filteredPosts = [];
1182
- collection.forEach(c => {
1181
+ const postIds = [];
1182
+ return collection.reduce((acc, post) => {
1183
1183
  var _a;
1184
- /*
1185
- * A text post with no children is just a text post
1186
- */
1187
- if (c.dataType === 'text' && !c.children.length)
1188
- return;
1189
- if (c.children.length) {
1190
- const childPost = (_a = pullFromCache(['post', 'get', c.children[0]])) === null || _a === void 0 ? void 0 : _a.data;
1191
- if (childPost && (dataTypes === null || dataTypes === void 0 ? void 0 : dataTypes.includes(childPost === null || childPost === void 0 ? void 0 : childPost.dataType))) {
1192
- // @ts-ignore
1193
- filteredPosts.push(childPost);
1194
- }
1195
- return;
1184
+ // A text post with no children is just a text post
1185
+ if (post.dataType === 'text' && !post.children.length)
1186
+ return acc;
1187
+ // Skip if post is already in the collection
1188
+ if (postIds.includes(post.postId))
1189
+ return acc;
1190
+ // Check dataType for current post
1191
+ if (dataTypes === null || dataTypes === void 0 ? void 0 : dataTypes.includes(post.dataType)) {
1192
+ acc.push(post);
1196
1193
  }
1197
- if (dataTypes === null || dataTypes === void 0 ? void 0 : dataTypes.includes(c.dataType))
1198
- filteredPosts.push(c);
1199
- });
1200
- return filteredPosts;
1194
+ // Check dataType for child post. this function will return all child posts in a same level of parent post (flatten)
1195
+ if (!post.children.length)
1196
+ return acc;
1197
+ const childPost = (_a = pullFromCache(['post', 'get', post.children[0]])) === null || _a === void 0 ? void 0 : _a.data;
1198
+ if (!childPost)
1199
+ return acc;
1200
+ // Prevent to insert a duplicate record
1201
+ if (postIds.includes(childPost.postId))
1202
+ return acc;
1203
+ if (dataTypes === null || dataTypes === void 0 ? void 0 : dataTypes.includes(childPost === null || childPost === void 0 ? void 0 : childPost.dataType))
1204
+ return acc;
1205
+ postIds.push(childPost.postId);
1206
+ // @ts-ignore
1207
+ acc.push(childPost);
1208
+ return acc;
1209
+ }, []);
1201
1210
  };
1202
1211
  /**
1203
1212
  * Filter a collection by search term
@@ -1371,7 +1380,12 @@ const createEventSubscriber = (client, namespace, event, fn) => {
1371
1380
  log(`${namespace}(tmpid: ${timestamp}) > listen`);
1372
1381
  const handler = (...payload) => {
1373
1382
  log(`${namespace}(tmpid: ${timestamp}) > trigger`, payload);
1374
- fn(...payload);
1383
+ try {
1384
+ fn(...payload);
1385
+ }
1386
+ catch (e) {
1387
+ log(`${namespace}(tmpid: ${timestamp}) > error`, e);
1388
+ }
1375
1389
  };
1376
1390
  emitter.on(event, handler);
1377
1391
  return () => {
@@ -20478,7 +20492,8 @@ const uuid = () => nativeIdGenerator.v4().toString();
20478
20492
 
20479
20493
  // TODO: find a way to uniquely identify the device
20480
20494
  let tabIndex = 0;
20481
- if (typeof BroadcastChannel !== 'undefined') {
20495
+ // This logic is running on browser only
20496
+ if (typeof BroadcastChannel !== 'undefined' && typeof window !== 'undefined') {
20482
20497
  const tabId = uuid();
20483
20498
  const openedTabs = {};
20484
20499
  const internalBroadcast = new BroadcastChannel('amity_ts_sdk');
@@ -21906,11 +21921,12 @@ const onUserDeleted = (callback) => createUserEventSubscriber('user.deleted', ca
21906
21921
  const logout = async () => {
21907
21922
  const client = getActiveClient();
21908
21923
  client.log('client/api/disconnectClient');
21909
- await new Promise(resolve => {
21910
- client.ws.once('disconnect', resolve);
21911
- client.ws.disconnect();
21924
+ if (client.mqtt.connected) {
21912
21925
  client.mqtt.disconnect();
21913
- });
21926
+ }
21927
+ if (client.ws.connected) {
21928
+ client.ws.disconnect();
21929
+ }
21914
21930
  /*
21915
21931
  * for cases when session state is terminated (example on ban) or token expired,
21916
21932
  * the terminating block will set session state to terminated or for the or
@@ -21940,7 +21956,7 @@ const logout = async () => {
21940
21956
  if (client.sessionState !== "tokenExpired" /* Amity.SessionStates.TOKEN_EXPIRED */ && client.cache) {
21941
21957
  client.cache = { data: {} };
21942
21958
  }
21943
- return !client.ws.connected;
21959
+ return true;
21944
21960
  };
21945
21961
  /* end_public_function */
21946
21962
 
@@ -23578,6 +23594,42 @@ const getChannel = (channelId, callback) => {
23578
23594
  };
23579
23595
  /* end_public_function */
23580
23596
 
23597
+ /**
23598
+ * ```js
23599
+ * import { onMessageCreated } from '@amityco/ts-sdk-react-native'
23600
+ * const dispose = onMessageCreated(message => {
23601
+ * // ...
23602
+ * })
23603
+ * ```
23604
+ *
23605
+ * Fired when an {@link Amity.Message} has been created
23606
+ *
23607
+ * @param callback The function to call when the event was fired
23608
+ * @param local Trigger when an event occurs from a local event or not
23609
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
23610
+ *
23611
+ * @category Message Events
23612
+ */
23613
+ const onMessageCreated = (callback, local = true) => {
23614
+ const client = getActiveClient();
23615
+ const filter = async (rawPayload) => {
23616
+ const payload = await prepareMessagePayload(rawPayload);
23617
+ const message = payload.messages[0];
23618
+ // Update in cache
23619
+ ingestInCache(payload);
23620
+ callback(message);
23621
+ };
23622
+ const disposers = [
23623
+ createEventSubscriber(client, 'message/onMessageCreated', 'message.created', filter),
23624
+ ];
23625
+ if (local) {
23626
+ createEventSubscriber(client, 'message/onMessageCreated', 'local.message.created', payload => callback(payload.messages[0]));
23627
+ }
23628
+ return () => {
23629
+ disposers.forEach(fn => fn());
23630
+ };
23631
+ };
23632
+
23581
23633
  /**
23582
23634
  * ```js
23583
23635
  * import { queryChannels } from '@amityco/ts-sdk-react-native'
@@ -23740,7 +23792,14 @@ const getChannels = (params, callback, config) => {
23740
23792
  responder(data);
23741
23793
  }, queryOptions(policy, CACHE_SHORTEN_LIFESPAN));
23742
23794
  };
23743
- disposers.push(onChannelCreated(realtimeRouter()), onChannelDeleted(realtimeRouter()), onChannelUpdated(realtimeRouter()), onChannelMuted(realtimeRouter()), onChannelJoined(realtimeRouter()), onChannelLeft(realtimeRouter()), onChannelMemberAdded(realtimeRouter()), onChannelMemberRemoved(realtimeRouter()), convertEventPayload(onChannelMarkerFetched, 'entityId', 'channel')(realtimeRouter()));
23795
+ disposers.push(onMessageCreated(message => {
23796
+ const cacheData = pullFromCache(['channel', 'get', message.channelId]);
23797
+ if (!cacheData)
23798
+ return;
23799
+ const channelData = Object.assign(Object.assign({}, cacheData.data), { lastActivity: message.createdAt });
23800
+ upsertInCache(['channel', 'get', message.channelId], channelData);
23801
+ realtimeRouter()(channelData);
23802
+ }), onChannelCreated(realtimeRouter()), onChannelDeleted(realtimeRouter()), onChannelUpdated(realtimeRouter()), onChannelMuted(realtimeRouter()), onChannelJoined(realtimeRouter()), onChannelLeft(realtimeRouter()), onChannelMemberAdded(realtimeRouter()), onChannelMemberRemoved(realtimeRouter()), convertEventPayload(onChannelMarkerFetched, 'entityId', 'channel')(realtimeRouter()));
23744
23803
  onFetch(true);
23745
23804
  disposers.push(() => dropFromCache(cacheKey));
23746
23805
  return () => {
@@ -24281,42 +24340,6 @@ const onUserMarkerSync = (callback) => {
24281
24340
  return createEventSubscriber(client, 'UserMarker/onUserMarkerSync', 'marker.user-sync', filter);
24282
24341
  };
24283
24342
 
24284
- /**
24285
- * ```js
24286
- * import { onMessageCreated } from '@amityco/ts-sdk-react-native'
24287
- * const dispose = onMessageCreated(message => {
24288
- * // ...
24289
- * })
24290
- * ```
24291
- *
24292
- * Fired when an {@link Amity.Message} has been created
24293
- *
24294
- * @param callback The function to call when the event was fired
24295
- * @param local Trigger when an event occurs from a local event or not
24296
- * @returns an {@link Amity.Unsubscriber} function to stop listening
24297
- *
24298
- * @category Message Events
24299
- */
24300
- const onMessageCreated = (callback, local = true) => {
24301
- const client = getActiveClient();
24302
- const filter = async (rawPayload) => {
24303
- const payload = await prepareMessagePayload(rawPayload);
24304
- const message = payload.messages[0];
24305
- // Update in cache
24306
- ingestInCache(payload);
24307
- callback(message);
24308
- };
24309
- const disposers = [
24310
- createEventSubscriber(client, 'message/onMessageCreated', 'message.created', filter),
24311
- ];
24312
- if (local) {
24313
- createEventSubscriber(client, 'message/onMessageCreated', 'local.message.created', payload => callback(payload.messages[0]));
24314
- }
24315
- return () => {
24316
- disposers.forEach(fn => fn());
24317
- };
24318
- };
24319
-
24320
24343
  /**
24321
24344
  * ```js
24322
24345
  * import { onMessageUpdated } from '@amityco/ts-sdk-react-native'
@@ -27809,7 +27832,7 @@ const createCommentEventSubscriber = (event, callback) => {
27809
27832
  var _a;
27810
27833
  if (!client.cache) {
27811
27834
  // TODO: here we are missing specific properties here!
27812
- callback(payload.comments[0]);
27835
+ callback(LinkedObject.comment(payload.comments[0]));
27813
27836
  }
27814
27837
  else {
27815
27838
  ingestInCache(payload);
@@ -27822,7 +27845,7 @@ const createCommentEventSubscriber = (event, callback) => {
27822
27845
  const queries = (_a = queryCache(['comment', 'query'])) === null || _a === void 0 ? void 0 : _a.filter(({ key }) => { var _a; return ((_a = key[2]) === null || _a === void 0 ? void 0 : _a.referenceId) === comment.data.referenceId; });
27823
27846
  queries === null || queries === void 0 ? void 0 : queries.map(({ key, data }) => upsertInCache(key, data, { cachedAt: -1 }));
27824
27847
  }
27825
- callback(comment.data);
27848
+ callback(LinkedObject.comment(comment.data));
27826
27849
  }
27827
27850
  };
27828
27851
  return createEventSubscriber(client, event, event, filter);
@@ -27836,12 +27859,12 @@ const createCommentEventSubscriber = (event, callback) => {
27836
27859
  * })
27837
27860
  * ```
27838
27861
  *
27839
- * Fired when a {@link Amity.InternalComment} has been created
27862
+ * Fired when a {@link Amity.Comment} has been created
27840
27863
  *
27841
27864
  * @param callback The function to call when the event was fired
27842
27865
  * @returns an {@link Amity.Unsubscriber} function to stop listening
27843
27866
  *
27844
- * @category InternalComment Events
27867
+ * @category Comment Events
27845
27868
  */
27846
27869
  const onCommentCreated = (callback) => createCommentEventSubscriber('comment.created', callback);
27847
27870
 
@@ -27853,12 +27876,12 @@ const onCommentCreated = (callback) => createCommentEventSubscriber('comment.cre
27853
27876
  * })
27854
27877
  * ```
27855
27878
  *
27856
- * Fired when a {@link Amity.InternalComment} has been updated
27879
+ * Fired when a {@link Amity.Comment} has been updated
27857
27880
  *
27858
27881
  * @param callback The function to call when the event was fired
27859
27882
  * @returns an {@link Amity.Unsubscriber} function to stop listening
27860
27883
  *
27861
- * @category InternalComment Events
27884
+ * @category Comment Events
27862
27885
  */
27863
27886
  const onCommentUpdated = (callback) => createCommentEventSubscriber('comment.updated', callback);
27864
27887
 
@@ -27870,12 +27893,12 @@ const onCommentUpdated = (callback) => createCommentEventSubscriber('comment.upd
27870
27893
  * })
27871
27894
  * ```
27872
27895
  *
27873
- * Fired when a {@link Amity.InternalComment} has been deleted
27896
+ * Fired when a {@link Amity.Comment} has been deleted
27874
27897
  *
27875
27898
  * @param callback The function to call when the event was fired
27876
27899
  * @returns an {@link Amity.Unsubscriber} function to stop listening
27877
27900
  *
27878
- * @category InternalComment Events
27901
+ * @category Comment Events
27879
27902
  */
27880
27903
  const onCommentDeleted = (callback) => createCommentEventSubscriber('comment.deleted', callback);
27881
27904
 
@@ -27887,12 +27910,12 @@ const onCommentDeleted = (callback) => createCommentEventSubscriber('comment.del
27887
27910
  * })
27888
27911
  * ```
27889
27912
  *
27890
- * Fired when a {@link Amity.InternalComment} has been flagged
27913
+ * Fired when a {@link Amity.Comment} has been flagged
27891
27914
  *
27892
27915
  * @param callback The function to call when the event was fired
27893
27916
  * @returns an {@link Amity.Unsubscriber} function to stop listening
27894
27917
  *
27895
- * @category InternalComment Events
27918
+ * @category Comment Events
27896
27919
  */
27897
27920
  const onCommentFlagged = (callback) => createCommentEventSubscriber('comment.flagged', callback);
27898
27921
 
@@ -27904,12 +27927,12 @@ const onCommentFlagged = (callback) => createCommentEventSubscriber('comment.fla
27904
27927
  * })
27905
27928
  * ```
27906
27929
  *
27907
- * Fired when a flag has been removed from a {@link Amity.InternalComment}
27930
+ * Fired when a flag has been removed from a {@link Amity.Comment}
27908
27931
  *
27909
27932
  * @param callback The function to call when the event was fired
27910
27933
  * @returns an {@link Amity.Unsubscriber} function to stop listening
27911
27934
  *
27912
- * @category InternalComment Events
27935
+ * @category Comment Events
27913
27936
  */
27914
27937
  const onCommentUnflagged = (callback) => createCommentEventSubscriber('comment.unflagged', callback);
27915
27938
 
@@ -27921,18 +27944,18 @@ const onCommentUnflagged = (callback) => createCommentEventSubscriber('comment.u
27921
27944
  * })
27922
27945
  * ```
27923
27946
  *
27924
- * Fired when a {@link Amity.InternalComment} has been reacted
27947
+ * Fired when a {@link Amity.Comment} has been reacted
27925
27948
  *
27926
27949
  * @param callback The function to call when the event was fired
27927
27950
  * @returns an {@link Amity.Unsubscriber} function to stop listening
27928
27951
  *
27929
- * @category InternalComment Events
27952
+ * @category Comment Events
27930
27953
  */
27931
27954
  const onCommentReactionAdded = (callback) => {
27932
27955
  const client = getActiveClient();
27933
27956
  const filter = (payload) => {
27934
27957
  if (!client.cache) {
27935
- callback(payload.comments[0]);
27958
+ callback(LinkedObject.comment(payload.comments[0]));
27936
27959
  }
27937
27960
  else {
27938
27961
  const processed = prepareReactionPayloadFormEvent('comment.addReaction', payload);
@@ -27943,7 +27966,7 @@ const onCommentReactionAdded = (callback) => {
27943
27966
  'get',
27944
27967
  payload.comments[0].commentId,
27945
27968
  ]);
27946
- callback(comment.data);
27969
+ callback(LinkedObject.comment(comment.data));
27947
27970
  }
27948
27971
  };
27949
27972
  return createEventSubscriber(client, 'comment.addReaction', 'comment.addReaction', filter);
@@ -27957,18 +27980,18 @@ const onCommentReactionAdded = (callback) => {
27957
27980
  * })
27958
27981
  * ```
27959
27982
  *
27960
- * Fired when a reaction has been removed from a {@link Amity.InternalComment}
27983
+ * Fired when a reaction has been removed from a {@link Amity.Comment}
27961
27984
  *
27962
27985
  * @param callback The function to call when the event was fired
27963
27986
  * @returns an {@link Amity.Unsubscriber} function to stop listening
27964
27987
  *
27965
- * @category InternalComment Events
27988
+ * @category Comment Events
27966
27989
  */
27967
27990
  const onCommentReactionRemoved = (callback) => {
27968
27991
  const client = getActiveClient();
27969
27992
  const filter = (payload) => {
27970
27993
  if (!client.cache) {
27971
- callback(payload.comments[0]);
27994
+ callback(LinkedObject.comment(payload.comments[0]));
27972
27995
  }
27973
27996
  else {
27974
27997
  const processed = prepareReactionPayloadFormEvent('comment.removeReaction', payload);
@@ -27979,7 +28002,7 @@ const onCommentReactionRemoved = (callback) => {
27979
28002
  'get',
27980
28003
  payload.comments[0].commentId,
27981
28004
  ]);
27982
- callback(comment.data);
28005
+ callback(LinkedObject.comment(comment.data));
27983
28006
  }
27984
28007
  };
27985
28008
  return createEventSubscriber(client, 'comment.removeReaction', 'comment.removeReaction', filter);
@@ -33302,7 +33325,7 @@ const observeComment = (commentId, callback, policy = 'cache_then_server') => {
33302
33325
  * });
33303
33326
  * ```
33304
33327
  *
33305
- * Observe all mutation on a given {@link Amity.InternalComment}
33328
+ * Observe all mutation on a given {@link Amity.Comment}
33306
33329
  *
33307
33330
  * @param commentId the ID of the comment to observe
33308
33331
  * @param callback the function to call when new data are available
@@ -33409,7 +33432,11 @@ const getComments = (params, callback, config) => {
33409
33432
  const cacheKey = [
33410
33433
  'comment',
33411
33434
  'collection',
33412
- { referenceId: params.referenceType, referenceType: params.referenceId },
33435
+ {
33436
+ referenceId: params.referenceType,
33437
+ referenceType: params.referenceId,
33438
+ parentId: (params === null || params === void 0 ? void 0 : params.parentId) || '',
33439
+ },
33413
33440
  ];
33414
33441
  const responder = (data) => {
33415
33442
  var _a, _b;