@amityco/ts-sdk 7.1.1-c8d4edca.0 → 7.1.1-dbdbe662.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 (35) hide show
  1. package/dist/@types/domains/channel.d.ts +3 -2
  2. package/dist/@types/domains/channel.d.ts.map +1 -1
  3. package/dist/channelRepository/events/onChannelDeleted.d.ts.map +1 -1
  4. package/dist/channelRepository/events/onChannelLeft.d.ts.map +1 -1
  5. package/dist/channelRepository/events/onChannelUnreadUpdatedLocal.d.ts +2 -2
  6. package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts +11 -0
  7. package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts.map +1 -0
  8. package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts +20 -0
  9. package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts.map +1 -0
  10. package/dist/channelRepository/observers/index.d.ts +1 -0
  11. package/dist/channelRepository/observers/index.d.ts.map +1 -1
  12. package/dist/channelRepository/utils/prepareChannelPayload.d.ts.map +1 -1
  13. package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts.map +1 -1
  14. package/dist/index.cjs.js +186 -20
  15. package/dist/index.esm.js +186 -20
  16. package/dist/index.umd.js +4 -4
  17. package/dist/marker/events/onChannelUnreadInfoUpdatedLocal.d.ts +2 -2
  18. package/dist/messageRepository/events/onMessageCreated.d.ts.map +1 -1
  19. package/dist/messageRepository/observers/getMessage.d.ts.map +1 -1
  20. package/package.json +1 -1
  21. package/src/@types/domains/channel.ts +4 -2
  22. package/src/channelRepository/events/onChannelDeleted.ts +17 -4
  23. package/src/channelRepository/events/onChannelLeft.ts +11 -3
  24. package/src/channelRepository/events/onChannelUnreadUpdatedLocal.ts +2 -2
  25. package/src/channelRepository/internalApi/getTotalChannelsUnread.ts +38 -0
  26. package/src/channelRepository/observers/getTotalChannelsUnread.ts +129 -0
  27. package/src/channelRepository/observers/index.ts +1 -0
  28. package/src/channelRepository/utils/prepareChannelPayload.ts +16 -7
  29. package/src/client/utils/ReadReceiptSync/readReceiptSyncEngine.ts +5 -4
  30. package/src/marker/events/onChannelUnreadInfoUpdatedLocal.ts +2 -2
  31. package/src/messageRepository/events/onMessageCreated.ts +45 -1
  32. package/src/messageRepository/observers/getMessage.ts +0 -1
  33. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts +0 -12
  34. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts.map +0 -1
  35. package/src/marker/events/onChannelUnreadUpdatedLocal.ts +0 -29
package/dist/index.esm.js CHANGED
@@ -21719,7 +21719,6 @@ class MessageReadReceiptSyncEngine {
21719
21719
  if (this.jobQueue.length === 0 || this.isActive === false)
21720
21720
  return;
21721
21721
  const readReceipts = this.getReadReceipts();
21722
- console.log('[New 🌟 readReceipts] Sync read receipts', readReceipts);
21723
21722
  if (readReceipts) {
21724
21723
  this.markReadApi(readReceipts);
21725
21724
  }
@@ -21821,8 +21820,9 @@ class MessageReadReceiptSyncEngine {
21821
21820
  // Step 1: Optimistic update of channelUnread.readToSegment to message.segment and update unreadCount value
21822
21821
  const cacheKey = ['channelUnread', 'get', channelId];
21823
21822
  const channelUnread = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
21824
- console.log('[New 🌟] Mark read => channel unread', channelUnread);
21825
- if (channelUnread && segment > channelUnread.readToSegment) {
21823
+ if (typeof (channelUnread === null || channelUnread === void 0 ? void 0 : channelUnread.readToSegment) === 'number' &&
21824
+ channelUnread &&
21825
+ segment > channelUnread.readToSegment) {
21826
21826
  channelUnread.readToSegment = segment;
21827
21827
  channelUnread.unreadCount = Math.max(channelUnread.lastSegment - segment, 0);
21828
21828
  pushToCache(cacheKey, channelUnread);
@@ -23155,17 +23155,24 @@ const preUpdateChannelCache = (rawPayload, options = { isMessagePreviewUpdated:
23155
23155
  const updateChannelUnread = ({ currentUserId, channels, channelUsers, }) => {
23156
23156
  for (let i = 0; i < channels.length; i += 1) {
23157
23157
  const cacheKey = ['channelUnread', 'get', channels[i].channelId];
23158
- const { readToSegment, lastMentionedSegment } = channelUsers.find(channelUser => channelUser.channelId === channels[i].channelId && channelUser.userId === currentUserId) || {
23159
- readToSegment: 0,
23160
- lastMentionedSegment: 0,
23161
- };
23158
+ const channelUser = channelUsers.find(channelUser => channelUser.channelId === channels[i].channelId && channelUser.userId === currentUserId);
23159
+ let unreadCount = 0;
23160
+ let readToSegment = null;
23161
+ let lastMentionedSegment = null;
23162
+ let isMentioned = false;
23163
+ if (channelUser) {
23164
+ readToSegment = channelUser.readToSegment;
23165
+ lastMentionedSegment = channelUser.lastMentionedSegment;
23166
+ unreadCount = Math.max(channels[i].messageCount - readToSegment, 0);
23167
+ isMentioned = lastMentionedSegment > readToSegment;
23168
+ }
23162
23169
  pushToCache(cacheKey, {
23163
23170
  channelId: channels[i].channelId,
23164
23171
  lastSegment: channels[i].messageCount,
23165
23172
  readToSegment,
23166
23173
  lastMentionedSegment,
23167
- unreadCount: channels[i].messageCount - readToSegment,
23168
- isMentioned: lastMentionedSegment > readToSegment,
23174
+ unreadCount,
23175
+ isMentioned,
23169
23176
  isDeleted: channels[i].isDeleted,
23170
23177
  });
23171
23178
  }
@@ -24011,12 +24018,21 @@ const onChannelDeleted = (callback) => {
24011
24018
  const client = getActiveClient();
24012
24019
  const filter = async (payload) => {
24013
24020
  const data = await prepareChannelPayload(payload);
24014
- if (client.isUnreadCountEnabled && client.getMarkerSyncConsistentMode()) {
24015
- data.channels.forEach(channel => {
24021
+ const isConsistentMode = client.getMarkerSyncConsistentMode() && client.isUnreadCountEnabled;
24022
+ const isLegacyUnreadCount = client.useLegacyUnreadCount;
24023
+ data.channels.forEach(channel => {
24024
+ if (isConsistentMode) {
24016
24025
  addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
24017
24026
  deleteChannelUnreadByChannelId(channel.channelId);
24018
- });
24019
- }
24027
+ }
24028
+ else if (isLegacyUnreadCount) {
24029
+ const cacheKey = ['channelUnread', 'get', channel.channelId];
24030
+ const cache = pullFromCache(cacheKey);
24031
+ if (cache) {
24032
+ pushToCache(cacheKey, Object.assign(Object.assign({}, cache), { isDeleted: true }));
24033
+ }
24034
+ }
24035
+ });
24020
24036
  ingestInCache(data);
24021
24037
  callbacks$b.forEach(cb => cb(data.channels[0]));
24022
24038
  };
@@ -24715,10 +24731,17 @@ const onChannelLeft = (callback) => {
24715
24731
  const preparedPayload = await prepareChannelPayload(payload, {
24716
24732
  isMessagePreviewUpdated: isLeftByMe,
24717
24733
  });
24718
- if (client.isUnreadCountEnabled && client.getMarkerSyncConsistentMode() && isLeftByMe) {
24734
+ const isConsistentMode = client.getMarkerSyncConsistentMode() && client.isUnreadCountEnabled;
24735
+ const isLegacyUnreadCount = client.useLegacyUnreadCount;
24736
+ if (isLeftByMe) {
24719
24737
  preparedPayload.channels.forEach(channel => {
24720
- addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
24721
- deleteChannelUnreadByChannelId(channel.channelId);
24738
+ if (isConsistentMode) {
24739
+ addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
24740
+ deleteChannelUnreadByChannelId(channel.channelId);
24741
+ }
24742
+ else if (isLegacyUnreadCount) {
24743
+ dropFromCache(['channelUnread', 'get', channel.channelId]);
24744
+ }
24722
24745
  });
24723
24746
  }
24724
24747
  const { channels, channelUsers } = preparedPayload;
@@ -24986,6 +25009,34 @@ const onMessageCreatedMqtt = (callback) => {
24986
25009
  reCalculateChannelUnreadInfo(message.channelId);
24987
25010
  });
24988
25011
  }
25012
+ if (client.useLegacyUnreadCount) {
25013
+ rawPayload.messages.forEach(message => {
25014
+ var _a, _b;
25015
+ const channelUnread = (_a = pullFromCache([
25016
+ 'channelUnread',
25017
+ 'get',
25018
+ message.channelId,
25019
+ ])) === null || _a === void 0 ? void 0 : _a.data;
25020
+ if (!channelUnread ||
25021
+ channelUnread.lastSegment >= message.segment ||
25022
+ typeof channelUnread.readToSegment !== 'number' ||
25023
+ typeof channelUnread.lastMentionSegment !== 'number')
25024
+ return;
25025
+ const lastSegment = message.segment;
25026
+ const isMentionedInMessage = (_b = message.mentionedUsers) === null || _b === void 0 ? void 0 : _b.some(mention => {
25027
+ return (mention.type === 'channel' ||
25028
+ (mention.type === 'user' &&
25029
+ client.userId &&
25030
+ mention.userPublicIds.includes(client.userId)));
25031
+ });
25032
+ const lastMentionSegment = isMentionedInMessage
25033
+ ? message.segment
25034
+ : channelUnread.lastMentionSegment;
25035
+ const updatedChannelUnread = Object.assign(Object.assign({}, channelUnread), { lastSegment, unreadCount: Math.max(lastSegment - channelUnread.readToSegment, 0), lastMentionSegment, isMentioned: !(channelUnread.readToSegment >= lastMentionSegment) });
25036
+ pushToCache(['channelUnread', 'get', message.channelId], updatedChannelUnread);
25037
+ fireEvent('local.channelUnread.updated', updatedChannelUnread);
25038
+ });
25039
+ }
24989
25040
  // Update in cache
24990
25041
  ingestInCache(payload);
24991
25042
  payload.messages.forEach(message => {
@@ -32828,12 +32879,12 @@ var index$f = /*#__PURE__*/Object.freeze({
32828
32879
  /**
32829
32880
  * Internal used only
32830
32881
  *
32831
- * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
32882
+ * Fired when an {@link Amity.channelUnreadInfo} has been updated.
32832
32883
  *
32833
32884
  * @param callback The function to call when the event was fired
32834
32885
  * @returns an {@link Amity.Unsubscriber} function to stop listening
32835
32886
  *
32836
- * @category MessageMarker Events
32887
+ * @category ChannelMarker Events
32837
32888
  */
32838
32889
  const onChannelUnreadInfoUpdatedLocal = (callback) => {
32839
32890
  const client = getActiveClient();
@@ -32846,12 +32897,12 @@ const onChannelUnreadInfoUpdatedLocal = (callback) => {
32846
32897
  /**
32847
32898
  * Internal used only
32848
32899
  *
32849
- * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
32900
+ * Fired when an {@link Amity.ChannelUnread} has been updated.
32850
32901
  *
32851
32902
  * @param callback The function to call when the event was fired
32852
32903
  * @returns an {@link Amity.Unsubscriber} function to stop listening
32853
32904
  *
32854
- * @category MessageMarker Events
32905
+ * @category Channel Events
32855
32906
  */
32856
32907
  const onChannelUnreadUpdatedLocal = (callback) => {
32857
32908
  const client = getActiveClient();
@@ -33643,6 +33694,120 @@ const getChannels = (params, callback, config) => {
33643
33694
  };
33644
33695
  /* end_public_function */
33645
33696
 
33697
+ /**
33698
+ *
33699
+ * Calculate user unread from {@link Amity.ChannelUnread} objects
33700
+ *
33701
+ * @returns the {@link Amity.UserUnread} objects
33702
+ *
33703
+ * @category Channel API
33704
+ * @async
33705
+ */
33706
+ const getTotalChannelsUnread$1 = () => {
33707
+ var _a;
33708
+ const client = getActiveClient();
33709
+ client.log('channel/getTotalChannelsUnread.locally');
33710
+ const cachedChannelsUnread = ((_a = queryCache(['channelUnread', 'get'])) === null || _a === void 0 ? void 0 : _a.filter(({ data }) => {
33711
+ return !data.isDeleted;
33712
+ })) || [];
33713
+ const totalChannelsUnread = (cachedChannelsUnread === null || cachedChannelsUnread === void 0 ? void 0 : cachedChannelsUnread.reduce((acc, { data }) => {
33714
+ acc.unreadCount += data.unreadCount;
33715
+ acc.isMentioned = acc.isMentioned || data.isMentioned;
33716
+ return acc;
33717
+ }, { unreadCount: 0, isMentioned: false })) || { unreadCount: 0, isMentioned: false };
33718
+ const cachedAt = client.cache && Date.now();
33719
+ return {
33720
+ data: totalChannelsUnread,
33721
+ cachedAt,
33722
+ };
33723
+ };
33724
+
33725
+ /* begin_public_function
33726
+ id: totalChannelsUnread.get
33727
+ */
33728
+ /**
33729
+ * ```js
33730
+ * import { ChannelRepository } from '@amityco/ts-sdk';
33731
+ *
33732
+ * let totalChannelsUnread;
33733
+ *
33734
+ * const unsubscribe = ChannelRepository.getTotalChannelsUnread(response => {
33735
+ * unread = response.data;
33736
+ * });
33737
+ * ```
33738
+ *
33739
+ * Observe all mutation on a given {@link Amity.UserUnread}
33740
+ *
33741
+ * @returns An {@link Amity.UserUnread} function to run when willing to stop observing the message
33742
+ *
33743
+ * @category User Unread Live Object
33744
+ *
33745
+ */
33746
+ const getTotalChannelsUnread = (callback) => {
33747
+ const { _id: userId } = getActiveUser();
33748
+ if (!userId)
33749
+ throw new ASCError('The _id has not been defined in ActiveUser', 800000 /* Amity.ClientError.UNKNOWN_ERROR */, "error" /* Amity.ErrorLevel.ERROR */);
33750
+ const { log, cache } = getActiveClient();
33751
+ if (!cache) {
33752
+ console.log('For using Live Object feature you need to enable Cache!');
33753
+ }
33754
+ const timestamp = Date.now();
33755
+ log(`liveTotalChannelsUnread(tmpid: ${timestamp}) > listen`);
33756
+ const disposers = [];
33757
+ let isUnsyncedModel = false; // for messages
33758
+ let model;
33759
+ const dispatcher = (data) => {
33760
+ const { data: userUnread } = data;
33761
+ const callbackModel = userUnread
33762
+ ? {
33763
+ unreadCount: userUnread.unreadCount,
33764
+ isMentioned: userUnread.isMentioned,
33765
+ }
33766
+ : undefined;
33767
+ model = callbackModel ? convertGetterPropsToStatic(callbackModel) : callbackModel;
33768
+ callback({
33769
+ data: callbackModel
33770
+ ? Object.assign(Object.assign({}, callbackModel), { isMentioned: callbackModel.isMentioned }) : callbackModel,
33771
+ loading: data.loading,
33772
+ error: data.error,
33773
+ });
33774
+ };
33775
+ const realtimeRouter = (userUnread) => {
33776
+ if (isEqual(model, userUnread))
33777
+ return;
33778
+ dispatcher({
33779
+ loading: false,
33780
+ data: userUnread,
33781
+ });
33782
+ };
33783
+ const onFetch = () => {
33784
+ const query = createQuery(async () => getTotalChannelsUnread$1());
33785
+ runQuery(query, ({ error, data, loading, origin, cachedAt }) => {
33786
+ if (cachedAt === UNSYNCED_OBJECT_CACHED_AT_VALUE) {
33787
+ dispatcher({
33788
+ data,
33789
+ origin,
33790
+ loading: false,
33791
+ error: new ASCApiError(UNSYNCED_OBJECT_CACHED_AT_MESSAGE, 800800 /* Amity.ClientError.DISALOOW_UNSYNCED_OBJECT */, "error" /* Amity.ErrorLevel.ERROR */),
33792
+ });
33793
+ isUnsyncedModel = true;
33794
+ disposers.forEach(fn => fn());
33795
+ }
33796
+ else if (!isUnsyncedModel) {
33797
+ dispatcher({ loading, data, origin, error });
33798
+ }
33799
+ if (error) {
33800
+ disposers.forEach(fn => fn());
33801
+ }
33802
+ });
33803
+ };
33804
+ disposers.push(onChannelUnreadUpdatedLocal(realtimeRouter));
33805
+ onFetch();
33806
+ return () => {
33807
+ disposers.forEach(fn => fn());
33808
+ };
33809
+ };
33810
+
33646
33811
  /* begin_public_function
33647
33812
  id: channel.member.add
33648
33813
  */
@@ -34246,6 +34411,7 @@ var index$c = /*#__PURE__*/Object.freeze({
34246
34411
  onChannelMemberRoleRemoved: onChannelMemberRoleRemoved,
34247
34412
  getChannel: getChannel,
34248
34413
  getChannels: getChannels,
34414
+ getTotalChannelsUnread: getTotalChannelsUnread,
34249
34415
  MARKER_INCLUDED_CHANNEL_TYPE: MARKER_INCLUDED_CHANNEL_TYPE,
34250
34416
  isUnreadCountSupport: isUnreadCountSupport,
34251
34417
  convertFromRaw: convertFromRaw,