@amityco/ts-sdk 7.1.1-1d526d8.0 → 7.1.1-a5f8c762.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 (69) hide show
  1. package/dist/@types/core/events.d.ts +2 -1
  2. package/dist/@types/core/events.d.ts.map +1 -1
  3. package/dist/@types/core/model.d.ts +2 -0
  4. package/dist/@types/core/model.d.ts.map +1 -1
  5. package/dist/@types/core/readReceipt.d.ts +12 -1
  6. package/dist/@types/core/readReceipt.d.ts.map +1 -1
  7. package/dist/@types/domains/channel.d.ts +10 -0
  8. package/dist/@types/domains/channel.d.ts.map +1 -1
  9. package/dist/@types/domains/client.d.ts +1 -0
  10. package/dist/@types/domains/client.d.ts.map +1 -1
  11. package/dist/channelRepository/api/markChannelsAsReadBySegment.d.ts +16 -0
  12. package/dist/channelRepository/api/markChannelsAsReadBySegment.d.ts.map +1 -0
  13. package/dist/channelRepository/events/onChannelDeleted.d.ts.map +1 -1
  14. package/dist/channelRepository/events/onChannelLeft.d.ts.map +1 -1
  15. package/dist/channelRepository/events/onChannelUnreadUpdatedLocal.d.ts +12 -0
  16. package/dist/channelRepository/events/onChannelUnreadUpdatedLocal.d.ts.map +1 -0
  17. package/dist/channelRepository/observers/getChannel.d.ts.map +1 -1
  18. package/dist/channelRepository/observers/getChannels/ChannelLiveCollectionController.d.ts.map +1 -1
  19. package/dist/channelRepository/utils/constructChannelDynamicValue.d.ts.map +1 -1
  20. package/dist/channelRepository/utils/getLegacyChannelUnread.d.ts +2 -0
  21. package/dist/channelRepository/utils/getLegacyChannelUnread.d.ts.map +1 -0
  22. package/dist/channelRepository/utils/prepareChannelPayload.d.ts.map +1 -1
  23. package/dist/client/api/createClient.d.ts.map +1 -1
  24. package/dist/client/api/enableUnreadCount.d.ts.map +1 -1
  25. package/dist/client/api/login.d.ts.map +1 -1
  26. package/dist/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngine.d.ts +33 -0
  27. package/dist/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngine.d.ts.map +1 -0
  28. package/dist/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngineOnLoginHandler.d.ts +3 -0
  29. package/dist/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngineOnLoginHandler.d.ts.map +1 -0
  30. package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts +2 -4
  31. package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts.map +1 -1
  32. package/dist/core/events.d.ts +3 -3
  33. package/dist/core/events.d.ts.map +1 -1
  34. package/dist/core/model/idResolvers.d.ts.map +1 -1
  35. package/dist/index.cjs.js +400 -47
  36. package/dist/index.esm.js +400 -47
  37. package/dist/index.umd.js +4 -4
  38. package/dist/marker/events/onChannelUnreadInfoUpdatedLocal.d.ts +12 -0
  39. package/dist/marker/events/onChannelUnreadInfoUpdatedLocal.d.ts.map +1 -0
  40. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts +1 -1
  41. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts.map +1 -1
  42. package/dist/messageRepository/events/onMessageCreated.d.ts.map +1 -1
  43. package/dist/messageRepository/utils/markReadMessage.d.ts.map +1 -1
  44. package/package.json +1 -1
  45. package/src/@types/core/events.ts +2 -1
  46. package/src/@types/core/model.ts +4 -0
  47. package/src/@types/core/readReceipt.ts +14 -1
  48. package/src/@types/domains/channel.ts +13 -0
  49. package/src/@types/domains/client.ts +2 -0
  50. package/src/channelRepository/api/markChannelsAsReadBySegment.ts +29 -0
  51. package/src/channelRepository/events/onChannelDeleted.ts +10 -4
  52. package/src/channelRepository/events/onChannelLeft.ts +11 -3
  53. package/src/channelRepository/events/onChannelUnreadUpdatedLocal.ts +29 -0
  54. package/src/channelRepository/observers/getChannel.ts +3 -1
  55. package/src/channelRepository/observers/getChannels/ChannelLiveCollectionController.ts +6 -1
  56. package/src/channelRepository/utils/constructChannelDynamicValue.ts +12 -2
  57. package/src/channelRepository/utils/getLegacyChannelUnread.ts +5 -0
  58. package/src/channelRepository/utils/prepareChannelPayload.ts +57 -17
  59. package/src/client/api/createClient.ts +3 -0
  60. package/src/client/api/enableUnreadCount.ts +1 -0
  61. package/src/client/api/login.ts +5 -1
  62. package/src/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngine.ts +267 -0
  63. package/src/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngineOnLoginHandler.ts +21 -0
  64. package/src/client/utils/ReadReceiptSync/readReceiptSyncEngine.ts +70 -99
  65. package/src/core/model/idResolvers.ts +2 -0
  66. package/src/marker/events/onChannelUnreadInfoUpdatedLocal.ts +29 -0
  67. package/src/marker/events/onChannelUnreadUpdatedLocal.ts +4 -4
  68. package/src/messageRepository/events/onMessageCreated.ts +34 -0
  69. package/src/messageRepository/utils/markReadMessage.ts +10 -3
package/dist/index.esm.js CHANGED
@@ -501,6 +501,7 @@ const idResolvers = {
501
501
  messagePreviewSubChannel: ({ subChannelId }) => `${subChannelId}`,
502
502
  channelUnreadInfo: ({ channelId }) => channelId,
503
503
  subChannelUnreadInfo: ({ subChannelId }) => subChannelId,
504
+ channelUnread: ({ channelId }) => channelId,
504
505
  channelMarker: ({ entityId, userId }) => `${entityId}#${userId}`,
505
506
  subChannelMarker: ({ entityId, feedId, userId }) => `${entityId}#${feedId}#${userId}`,
506
507
  messageMarker: ({ feedId, contentId, creatorId }) => `${feedId}#${contentId}#${creatorId}`,
@@ -1594,13 +1595,13 @@ class NetworkActivitiesWatcher {
1594
1595
  this._listener.clear();
1595
1596
  }
1596
1597
  }
1597
- let instance$5;
1598
+ let instance$6;
1598
1599
  var NetworkActivitiesWatcher$1 = {
1599
1600
  getInstance: () => {
1600
- if (!instance$5) {
1601
- instance$5 = new NetworkActivitiesWatcher();
1601
+ if (!instance$6) {
1602
+ instance$6 = new NetworkActivitiesWatcher();
1602
1603
  }
1603
- return instance$5;
1604
+ return instance$6;
1604
1605
  },
1605
1606
  };
1606
1607
 
@@ -21240,13 +21241,13 @@ class AnalyticsEngine {
21240
21241
  this._eventCapturer.resetAllBuckets();
21241
21242
  }
21242
21243
  }
21243
- let instance$4;
21244
+ let instance$5;
21244
21245
  var AnalyticsEngine$1 = {
21245
21246
  getInstance: () => {
21246
- if (!instance$4) {
21247
- instance$4 = new AnalyticsEngine();
21247
+ if (!instance$5) {
21248
+ instance$5 = new AnalyticsEngine();
21248
21249
  }
21249
- return instance$4;
21250
+ return instance$5;
21250
21251
  },
21251
21252
  };
21252
21253
 
@@ -21672,6 +21673,221 @@ const getMessageReadCount = (message, marker) => {
21672
21673
  getCachedMarker$2(message)) !== null && _a !== void 0 ? _a : { readCount: 0, deliveredCount: 0 };
21673
21674
  }; // and if not found in cache use default value `0`
21674
21675
 
21676
+ /**
21677
+ *
21678
+ * Mark subChannel as read by readToSegment
21679
+ *
21680
+ * @param subChannelIds the IDs of the {@link Amity.SubChannel} to update
21681
+ * @param readToSegment the segment to mark as read
21682
+ * @returns a success boolean if the {@link Amity.SubChannel} was updated
21683
+ *
21684
+ * @category Channel API
21685
+ * @async
21686
+ */
21687
+ const markChannelsAsReadBySegment = async (readings) => {
21688
+ const client = getActiveClient();
21689
+ try {
21690
+ await client.http.post('api/v3/channels/seen', { channels: readings });
21691
+ return true;
21692
+ }
21693
+ catch (e) {
21694
+ return false;
21695
+ }
21696
+ };
21697
+
21698
+ class MessageReadReceiptSyncEngine {
21699
+ constructor() {
21700
+ this.isActive = true;
21701
+ this.MAX_RETRY = 3;
21702
+ this.JOB_QUEUE_SIZE = 120;
21703
+ this.jobQueue = [];
21704
+ // Interval for message read receipt sync in seconds
21705
+ this.RECEIPT_SYNC_INTERVAL = 1;
21706
+ this.client = getActiveClient();
21707
+ // Get remaining unsync read receipts from cache
21708
+ this.getUnsyncJobs();
21709
+ }
21710
+ // Call this when client call client.login
21711
+ startSyncReadReceipt() {
21712
+ // Start timer when start receipt sync
21713
+ this.timer = setInterval(() => {
21714
+ this.syncReadReceipts();
21715
+ }, this.RECEIPT_SYNC_INTERVAL * 1000);
21716
+ }
21717
+ // Read receipt observer handling
21718
+ syncReadReceipts() {
21719
+ if (this.jobQueue.length === 0 || this.isActive === false)
21720
+ return;
21721
+ const readReceipts = this.getReadReceipts();
21722
+ if (readReceipts) {
21723
+ this.markReadApi(readReceipts);
21724
+ }
21725
+ }
21726
+ getUnsyncJobs() {
21727
+ var _a;
21728
+ // Get all read receipts that has latestSyncSegment < latestSegment
21729
+ const readReceipts = (_a = queryCache(['readReceipt'])) === null || _a === void 0 ? void 0 : _a.filter(({ data }) => {
21730
+ return data.latestSyncSegment < data.latestSegment;
21731
+ });
21732
+ // Enqueue unsync read receipts to the job queue
21733
+ readReceipts === null || readReceipts === void 0 ? void 0 : readReceipts.forEach(({ data: readReceipt }) => {
21734
+ this.enqueueReadReceipt(readReceipt.channelId, readReceipt.latestSegment);
21735
+ });
21736
+ }
21737
+ getReadReceipts() {
21738
+ // get all read receipts from queue, now the queue is empty
21739
+ const syncJob = this.jobQueue.splice(0, this.jobQueue.length);
21740
+ if (syncJob.length === 0)
21741
+ return;
21742
+ return syncJob.filter(job => {
21743
+ var _a;
21744
+ const readReceipt = (_a = pullFromCache(['readReceipt', job.channelId])) === null || _a === void 0 ? void 0 : _a.data;
21745
+ if (!readReceipt)
21746
+ return false;
21747
+ if (readReceipt.latestSegment > readReceipt.latestSyncSegment)
21748
+ return true;
21749
+ return false;
21750
+ });
21751
+ }
21752
+ async markReadApi(syncJobs) {
21753
+ var _a;
21754
+ // constuct payload
21755
+ // example: [{ channelId: 'channelId', readToSegment: 2 }]
21756
+ const syncJobsPayload = syncJobs.map(job => {
21757
+ return {
21758
+ channelId: job.channelId,
21759
+ readToSegment: job.segment,
21760
+ };
21761
+ });
21762
+ const response = await markChannelsAsReadBySegment(syncJobsPayload);
21763
+ if (response) {
21764
+ for (let i = 0; i < syncJobs.length; i += 1) {
21765
+ // update lastestSyncSegment in read receipt cache
21766
+ const cacheKey = ['readReceipt', syncJobs[i].channelId];
21767
+ const readReceiptCache = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
21768
+ pushToCache(cacheKey, Object.assign(Object.assign({}, readReceiptCache), { latestSyncSegment: syncJobs[i].segment }));
21769
+ }
21770
+ }
21771
+ else {
21772
+ for (let i = 0; i < syncJobs.length; i += 1) {
21773
+ // push them back to queue if the syncing is failed and retry count is less than max retry
21774
+ if (syncJobs[i].retryCount >= this.MAX_RETRY)
21775
+ return;
21776
+ const updatedJob = Object.assign(Object.assign({}, syncJobs[i]), { syncState: "create" /* Amity.ReadReceiptSyncState.CREATED */, retryCount: syncJobs[i].retryCount + 1 });
21777
+ this.enqueueJob(updatedJob);
21778
+ }
21779
+ }
21780
+ }
21781
+ startObservingReadReceiptQueue() {
21782
+ if (this.client.useLegacyUnreadCount) {
21783
+ this.isActive = true;
21784
+ this.startSyncReadReceipt();
21785
+ }
21786
+ }
21787
+ stopObservingReadReceiptQueue() {
21788
+ this.isActive = false;
21789
+ this.jobQueue.map(job => {
21790
+ if (job.syncState === "syncing" /* Amity.ReadReceiptSyncState.SYNCING */) {
21791
+ return Object.assign(Object.assign({}, job), { syncState: "create" /* Amity.ReadReceiptSyncState.CREATED */ });
21792
+ }
21793
+ return job;
21794
+ });
21795
+ if (this.timer)
21796
+ clearInterval(this.timer);
21797
+ }
21798
+ // Session Management
21799
+ onSessionEstablished() {
21800
+ this.startObservingReadReceiptQueue();
21801
+ }
21802
+ onSessionDestroyed() {
21803
+ this.stopObservingReadReceiptQueue();
21804
+ this.jobQueue = [];
21805
+ }
21806
+ onTokenExpired() {
21807
+ this.stopObservingReadReceiptQueue();
21808
+ }
21809
+ // Network Connection Management
21810
+ onNetworkOffline() {
21811
+ // Stop observing to the read receipt queue.
21812
+ this.stopObservingReadReceiptQueue();
21813
+ }
21814
+ onNetworkOnline() {
21815
+ // Resume observing to the read receipt queue.
21816
+ this.startObservingReadReceiptQueue();
21817
+ }
21818
+ markRead(channelId, segment) {
21819
+ var _a;
21820
+ // Step 1: Optimistic update of channelUnread.readToSegment to message.segment and update unreadCount value
21821
+ const cacheKey = ['channelUnread', 'get', channelId];
21822
+ const channelUnread = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
21823
+ if (channelUnread && segment > channelUnread.readToSegment) {
21824
+ channelUnread.readToSegment = segment;
21825
+ channelUnread.unreadCount = Math.max(channelUnread.lastSegment - segment, 0);
21826
+ pushToCache(cacheKey, channelUnread);
21827
+ fireEvent('local.channelUnread.updated', channelUnread);
21828
+ }
21829
+ // Step 2: Enqueue the read receipt
21830
+ this.enqueueReadReceipt(channelId, segment);
21831
+ }
21832
+ enqueueReadReceipt(channelId, segment) {
21833
+ var _a;
21834
+ const readReceipt = (_a = pullFromCache(['readReceipt', channelId])) === null || _a === void 0 ? void 0 : _a.data;
21835
+ // Create new read receipt if it's not exists and add the job to queue
21836
+ if (!readReceipt) {
21837
+ const readReceiptChannel = {
21838
+ channelId,
21839
+ latestSegment: segment,
21840
+ latestSyncSegment: 0,
21841
+ };
21842
+ pushToCache(['readReceipt', channelId], readReceiptChannel);
21843
+ }
21844
+ else if (readReceipt.latestSegment < segment) {
21845
+ // Update latestSegment in read receipt cache
21846
+ pushToCache(['readReceipt', channelId], Object.assign(Object.assign({}, readReceipt), { latestSegment: segment }));
21847
+ }
21848
+ else if (readReceipt.latestSyncSegment >= segment) {
21849
+ // Skip the job when lastSyncSegment > = segment
21850
+ return;
21851
+ }
21852
+ let syncJob = this.getSyncJob(channelId);
21853
+ if (syncJob === null || syncJob.syncState === "syncing" /* Amity.ReadReceiptSyncState.SYNCING */) {
21854
+ syncJob = {
21855
+ channelId,
21856
+ segment,
21857
+ syncState: "create" /* Amity.ReadReceiptSyncState.CREATED */,
21858
+ retryCount: 0,
21859
+ };
21860
+ this.enqueueJob(syncJob);
21861
+ }
21862
+ else if (syncJob.segment < segment) {
21863
+ syncJob.segment = segment;
21864
+ }
21865
+ }
21866
+ getSyncJob(channelId) {
21867
+ const { jobQueue } = this;
21868
+ const targetJob = jobQueue.find(job => job.channelId === channelId);
21869
+ return targetJob || null;
21870
+ }
21871
+ enqueueJob(syncJob) {
21872
+ if (this.jobQueue.length < this.JOB_QUEUE_SIZE) {
21873
+ this.jobQueue.push(syncJob);
21874
+ }
21875
+ else {
21876
+ // Remove oldest job when queue reach maximum capacity
21877
+ this.jobQueue.shift();
21878
+ this.jobQueue.push(syncJob);
21879
+ }
21880
+ }
21881
+ }
21882
+ let instance$4 = null;
21883
+ var ReadReceiptSyncEngine = {
21884
+ getInstance: () => {
21885
+ if (!instance$4)
21886
+ instance$4 = new MessageReadReceiptSyncEngine();
21887
+ return instance$4;
21888
+ },
21889
+ };
21890
+
21675
21891
  /**
21676
21892
  *
21677
21893
  * Mark subChannel as read by readToSegment
@@ -21720,7 +21936,7 @@ const reCalculateChannelUnreadInfo = (channelId) => {
21720
21936
  return channelUnreadInfo;
21721
21937
  };
21722
21938
 
21723
- class MessageReadReceiptSyncEngine {
21939
+ class LegacyMessageReadReceiptSyncEngine {
21724
21940
  constructor() {
21725
21941
  this.isActive = true;
21726
21942
  this.MAX_RETRY = 3;
@@ -21751,7 +21967,7 @@ class MessageReadReceiptSyncEngine {
21751
21967
  getUnsyncJobs() {
21752
21968
  var _a;
21753
21969
  // Get all read receipts that has latestSyncSegment < latestSegment
21754
- const readReceipts = (_a = queryCache(['readReceipt'])) === null || _a === void 0 ? void 0 : _a.filter(({ data }) => {
21970
+ const readReceipts = (_a = queryCache(['legacyReadReceipt'])) === null || _a === void 0 ? void 0 : _a.filter(({ data }) => {
21755
21971
  return data.latestSyncSegment < data.latestSegment;
21756
21972
  });
21757
21973
  // Enqueue unsync read receipts to the job queue
@@ -21770,7 +21986,7 @@ class MessageReadReceiptSyncEngine {
21770
21986
  return;
21771
21987
  // Get readReceipt from cache by subChannelId
21772
21988
  const readReceipt = (_a = pullFromCache([
21773
- 'readReceipt',
21989
+ 'legacyReadReceipt',
21774
21990
  syncJob.subChannelId,
21775
21991
  ])) === null || _a === void 0 ? void 0 : _a.data;
21776
21992
  if (!readReceipt)
@@ -21793,10 +22009,10 @@ class MessageReadReceiptSyncEngine {
21793
22009
  if (response) {
21794
22010
  this.removeSynedReceipt(syncJob.subChannelId, syncJob.segment);
21795
22011
  const readReceiptCache = (_a = pullFromCache([
21796
- 'readReceipt',
22012
+ 'legacyReadReceipt',
21797
22013
  subChannelId,
21798
22014
  ])) === null || _a === void 0 ? void 0 : _a.data;
21799
- pushToCache(['readReceipt', subChannelId], Object.assign(Object.assign({}, readReceiptCache), { latestSyncSegment: segment }));
22015
+ pushToCache(['legacyReadReceipt', subChannelId], Object.assign(Object.assign({}, readReceiptCache), { latestSyncSegment: segment }));
21800
22016
  }
21801
22017
  else if (!response) {
21802
22018
  if (newSyncJob.retryCount > this.MAX_RETRY) {
@@ -21863,7 +22079,7 @@ class MessageReadReceiptSyncEngine {
21863
22079
  subChannelUnreadInfo.readToSegment = segment;
21864
22080
  subChannelUnreadInfo.unreadCount = Math.max(subChannelUnreadInfo.lastSegment - segment, 0);
21865
22081
  const channelUnreadInfo = reCalculateChannelUnreadInfo(subChannelUnreadInfo.channelId);
21866
- fireEvent('local.channelUnread.updated', channelUnreadInfo);
22082
+ fireEvent('local.channelUnreadInfo.updated', channelUnreadInfo);
21867
22083
  pushToCache(cacheKey, subChannelUnreadInfo);
21868
22084
  fireEvent('local.subChannelUnread.updated', subChannelUnreadInfo);
21869
22085
  }
@@ -21872,7 +22088,10 @@ class MessageReadReceiptSyncEngine {
21872
22088
  }
21873
22089
  enqueueReadReceipt(subChannelId, segment) {
21874
22090
  var _a;
21875
- const readReceipt = (_a = pullFromCache(['readReceipt', subChannelId])) === null || _a === void 0 ? void 0 : _a.data;
22091
+ const readReceipt = (_a = pullFromCache([
22092
+ 'legacyReadReceipt',
22093
+ subChannelId,
22094
+ ])) === null || _a === void 0 ? void 0 : _a.data;
21876
22095
  // Create new read receipt if it's not exists and add job to queue
21877
22096
  if (!readReceipt) {
21878
22097
  const readReceiptSubChannel = {
@@ -21880,10 +22099,10 @@ class MessageReadReceiptSyncEngine {
21880
22099
  latestSegment: segment,
21881
22100
  latestSyncSegment: 0,
21882
22101
  };
21883
- pushToCache(['readReceipt', subChannelId], readReceiptSubChannel);
22102
+ pushToCache(['legacyReadReceipt', subChannelId], readReceiptSubChannel);
21884
22103
  }
21885
22104
  else if (readReceipt.latestSegment < segment) {
21886
- pushToCache(['readReceipt', subChannelId], Object.assign(Object.assign({}, readReceipt), { latestSegment: segment }));
22105
+ pushToCache(['legacyReadReceipt', subChannelId], Object.assign(Object.assign({}, readReceipt), { latestSegment: segment }));
21887
22106
  }
21888
22107
  else if (readReceipt.latestSyncSegment >= segment) {
21889
22108
  // Skip the job when lastSyncSegment > = segment
@@ -21926,18 +22145,24 @@ class MessageReadReceiptSyncEngine {
21926
22145
  }
21927
22146
  }
21928
22147
  let instance$3 = null;
21929
- var ReadReceiptSyncEngine = {
22148
+ var LegacyReadReceiptSyncEngine = {
21930
22149
  getInstance: () => {
21931
22150
  if (!instance$3)
21932
- instance$3 = new MessageReadReceiptSyncEngine();
22151
+ instance$3 = new LegacyMessageReadReceiptSyncEngine();
21933
22152
  return instance$3;
21934
22153
  },
21935
22154
  };
21936
22155
 
21937
22156
  const markReadMessage = (message) => {
21938
- const { subChannelId, channelSegment } = message;
21939
- const markReadReceiptEngine = ReadReceiptSyncEngine.getInstance();
21940
- markReadReceiptEngine.markRead(subChannelId, channelSegment);
22157
+ const client = getActiveClient();
22158
+ if (client.useLegacyUnreadCount) {
22159
+ const markReadReceiptEngine = ReadReceiptSyncEngine.getInstance();
22160
+ markReadReceiptEngine.markRead(message.channelId, message.channelSegment);
22161
+ }
22162
+ else {
22163
+ const markReadReceiptEngine = LegacyReadReceiptSyncEngine.getInstance();
22164
+ markReadReceiptEngine.markRead(message.subChannelId, message.channelSegment);
22165
+ }
21941
22166
  };
21942
22167
 
21943
22168
  const messageLinkedObject = (message) => {
@@ -22925,6 +23150,24 @@ const preUpdateChannelCache = (rawPayload, options = { isMessagePreviewUpdated:
22925
23150
  channels: rawPayload.channels.map(channel => convertFromRaw(channel, { isMessagePreviewUpdated: options.isMessagePreviewUpdated })),
22926
23151
  });
22927
23152
  };
23153
+ const updateChannelUnread = ({ currentUserId, channels, channelUsers, }) => {
23154
+ for (let i = 0; i < channels.length; i += 1) {
23155
+ const cacheKey = ['channelUnread', 'get', channels[i].channelId];
23156
+ const { readToSegment, lastMentionedSegment } = channelUsers.find(channelUser => channelUser.channelId === channels[i].channelId && channelUser.userId === currentUserId) || {
23157
+ readToSegment: 0,
23158
+ lastMentionedSegment: 0,
23159
+ };
23160
+ pushToCache(cacheKey, {
23161
+ channelId: channels[i].channelId,
23162
+ lastSegment: channels[i].messageCount,
23163
+ readToSegment,
23164
+ lastMentionedSegment,
23165
+ unreadCount: channels[i].messageCount - readToSegment,
23166
+ isMentioned: lastMentionedSegment > readToSegment,
23167
+ isDeleted: channels[i].isDeleted,
23168
+ });
23169
+ }
23170
+ };
22928
23171
  const prepareChannelPayload = async (rawPayload, options = { isMessagePreviewUpdated: true }) => {
22929
23172
  const client = getActiveClient();
22930
23173
  const networkPreviewSetting = await client.getMessagePreviewSetting(false);
@@ -22934,23 +23177,34 @@ const prepareChannelPayload = async (rawPayload, options = { isMessagePreviewUpd
22934
23177
  rawPayload.messagePreviews.length > 0) {
22935
23178
  updateChannelMessagePreviewCache(rawPayload);
22936
23179
  }
22937
- const markerIds = rawPayload.channels
22938
- // filter channel by type. Only conversation, community and broadcast type are included.
22939
- .filter(isUnreadCountSupport)
22940
- .map(({ channelInternalId }) => channelInternalId);
22941
- if (markerIds.length > 0) {
22942
- // since the get markers method requires a channel cache to function with the reducer.
22943
- preUpdateChannelCache(rawPayload, { isMessagePreviewUpdated: options.isMessagePreviewUpdated });
22944
- try {
22945
- await getChannelMarkers(markerIds);
22946
- }
22947
- catch (e) {
22948
- // empty block (from the spec, allow marker fetch to fail without having to do anything)
23180
+ if (client.useLegacyUnreadCount) {
23181
+ updateChannelUnread({
23182
+ channels: rawPayload.channels,
23183
+ channelUsers: rawPayload.channelUsers,
23184
+ currentUserId: client.userId,
23185
+ });
23186
+ }
23187
+ else {
23188
+ const markerIds = rawPayload.channels
23189
+ // filter channel by type. Only conversation, community and broadcast type are included.
23190
+ .filter(isUnreadCountSupport)
23191
+ .map(({ channelInternalId }) => channelInternalId);
23192
+ if (markerIds.length > 0) {
23193
+ // since the get markers method requires a channel cache to function with the reducer.
23194
+ preUpdateChannelCache(rawPayload, {
23195
+ isMessagePreviewUpdated: options.isMessagePreviewUpdated,
23196
+ });
23197
+ try {
23198
+ await getChannelMarkers(markerIds);
23199
+ }
23200
+ catch (e) {
23201
+ // empty block (from the spec, allow marker fetch to fail without having to do anything)
23202
+ }
22949
23203
  }
22950
23204
  }
22951
- // attach marker to channel
23205
+ // convert raw channel to internal channel
22952
23206
  const channels = rawPayload.channels.map(payload => convertFromRaw(payload, { isMessagePreviewUpdated: options.isMessagePreviewUpdated }));
22953
- // user marker to channel users
23207
+ // convert raw channel user to membership (add user object)
22954
23208
  const channelUsers = rawPayload.channelUsers.map(channelUser => {
22955
23209
  return convertRawMembershipToMembership(channelUser);
22956
23210
  });
@@ -23077,15 +23331,28 @@ const getSubChannelsUnreadCount = (channel, marker) => {
23077
23331
  return (_e = (_c = marker === null || marker === void 0 ? void 0 : marker.unreadCount) !== null && _c !== void 0 ? _c : (_d = getCachedMarker(channel.channelInternalId)) === null || _d === void 0 ? void 0 : _d.unreadCount) !== null && _e !== void 0 ? _e : 0;
23078
23332
  };
23079
23333
 
23334
+ const getLegacyChannelUnread = (channelId) => {
23335
+ var _a;
23336
+ return (_a = pullFromCache(['channelUnread', 'get', channelId])) === null || _a === void 0 ? void 0 : _a.data;
23337
+ };
23338
+
23080
23339
  const constructChannelDynamicValue = (channel) => {
23340
+ const client = getActiveClient();
23081
23341
  const rest = __rest(channel, ["messageCount"]);
23082
23342
  return shallowClone(rest, {
23083
- get isMentioned() {
23084
- return getChannelIsMentioned(rest);
23343
+ get unreadCount() {
23344
+ var _a, _b;
23345
+ return (_b = (_a = getLegacyChannelUnread(rest.channelId)) === null || _a === void 0 ? void 0 : _a.unreadCount) !== null && _b !== void 0 ? _b : 0;
23085
23346
  },
23086
23347
  get subChannelsUnreadCount() {
23087
23348
  return getSubChannelsUnreadCount(rest);
23088
23349
  },
23350
+ get isMentioned() {
23351
+ var _a, _b;
23352
+ if (client.useLegacyUnreadCount)
23353
+ return (_b = (_a = getLegacyChannelUnread(rest.channelId)) === null || _a === void 0 ? void 0 : _a.isMentioned) !== null && _b !== void 0 ? _b : false;
23354
+ return getChannelIsMentioned(rest);
23355
+ },
23089
23356
  });
23090
23357
  };
23091
23358
 
@@ -23742,12 +24009,17 @@ const onChannelDeleted = (callback) => {
23742
24009
  const client = getActiveClient();
23743
24010
  const filter = async (payload) => {
23744
24011
  const data = await prepareChannelPayload(payload);
23745
- if (client.isUnreadCountEnabled && client.getMarkerSyncConsistentMode()) {
23746
- data.channels.forEach(channel => {
24012
+ const isConsistentMode = client.getMarkerSyncConsistentMode() && client.isUnreadCountEnabled;
24013
+ const isLegacyUnreadCount = client.useLegacyUnreadCount;
24014
+ data.channels.forEach(channel => {
24015
+ if (isConsistentMode) {
23747
24016
  addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
23748
24017
  deleteChannelUnreadByChannelId(channel.channelId);
23749
- });
23750
- }
24018
+ }
24019
+ else if (isLegacyUnreadCount) {
24020
+ dropFromCache(['channelUnread', 'get', channel.channelId]);
24021
+ }
24022
+ });
23751
24023
  ingestInCache(data);
23752
24024
  callbacks$b.forEach(cb => cb(data.channels[0]));
23753
24025
  };
@@ -23861,6 +24133,25 @@ var readReceiptSyncEngineOnLoginHandler = () => {
23861
24133
  };
23862
24134
  };
23863
24135
 
24136
+ var legacyReadReceiptSyncEngineOnLoginHandler = () => {
24137
+ const readReceiptSyncEngine = LegacyReadReceiptSyncEngine.getInstance();
24138
+ readReceiptSyncEngine.startSyncReadReceipt();
24139
+ onSessionStateChange(state => {
24140
+ if (state === "established" /* Amity.SessionStates.ESTABLISHED */) {
24141
+ readReceiptSyncEngine.onSessionEstablished();
24142
+ }
24143
+ else if (state === "tokenExpired" /* Amity.SessionStates.TOKEN_EXPIRED */) {
24144
+ readReceiptSyncEngine.onTokenExpired();
24145
+ }
24146
+ else {
24147
+ readReceiptSyncEngine.onSessionDestroyed();
24148
+ }
24149
+ });
24150
+ return () => {
24151
+ readReceiptSyncEngine.onSessionDestroyed();
24152
+ };
24153
+ };
24154
+
23864
24155
  const onOnline = (callback) => {
23865
24156
  if (typeof window !== 'undefined' && window.addEventListener) {
23866
24157
  window.addEventListener('online', callback);
@@ -24427,10 +24718,17 @@ const onChannelLeft = (callback) => {
24427
24718
  const preparedPayload = await prepareChannelPayload(payload, {
24428
24719
  isMessagePreviewUpdated: isLeftByMe,
24429
24720
  });
24430
- if (client.isUnreadCountEnabled && client.getMarkerSyncConsistentMode() && isLeftByMe) {
24721
+ const isConsistentMode = client.getMarkerSyncConsistentMode() && client.isUnreadCountEnabled;
24722
+ const isLegacyUnreadCount = client.useLegacyUnreadCount;
24723
+ if (isLeftByMe) {
24431
24724
  preparedPayload.channels.forEach(channel => {
24432
- addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
24433
- deleteChannelUnreadByChannelId(channel.channelId);
24725
+ if (isConsistentMode) {
24726
+ addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
24727
+ deleteChannelUnreadByChannelId(channel.channelId);
24728
+ }
24729
+ else if (isLegacyUnreadCount) {
24730
+ dropFromCache(['channelUnread', 'get', channel.channelId]);
24731
+ }
24434
24732
  });
24435
24733
  }
24436
24734
  const { channels, channelUsers } = preparedPayload;
@@ -24698,6 +24996,29 @@ const onMessageCreatedMqtt = (callback) => {
24698
24996
  reCalculateChannelUnreadInfo(message.channelId);
24699
24997
  });
24700
24998
  }
24999
+ if (client.useLegacyUnreadCount) {
25000
+ rawPayload.messages.forEach(message => {
25001
+ var _a, _b;
25002
+ const channelUnread = (_a = pullFromCache([
25003
+ 'channelUnread',
25004
+ 'get',
25005
+ message.channelId,
25006
+ ])) === null || _a === void 0 ? void 0 : _a.data;
25007
+ if (!channelUnread || channelUnread.lastSegment >= message.segment)
25008
+ return;
25009
+ const lastSegment = message.segment;
25010
+ const isMentionedInMessage = (_b = message.mentionedUsers) === null || _b === void 0 ? void 0 : _b.some(mention => {
25011
+ return (mention.type === 'channel' ||
25012
+ (mention.type === 'user' &&
25013
+ client.userId &&
25014
+ mention.userPublicIds.includes(client.userId)));
25015
+ });
25016
+ const lastMentionSegment = isMentionedInMessage
25017
+ ? message.segment
25018
+ : channelUnread.lastMentionSegment;
25019
+ pushToCache(['channelUnread', 'get', message.channelId], Object.assign(Object.assign({}, channelUnread), { lastSegment, unreadCount: lastSegment - channelUnread.readToSegment, lastMentionSegment, isMentioned: !(channelUnread.readToSegment >= lastMentionSegment) }));
25020
+ });
25021
+ }
24701
25022
  // Update in cache
24702
25023
  ingestInCache(payload);
24703
25024
  payload.messages.forEach(message => {
@@ -24873,6 +25194,7 @@ const enableUnreadCount = () => {
24873
25194
  if (client.isUnreadCountEnabled)
24874
25195
  return false;
24875
25196
  client.isUnreadCountEnabled = true;
25197
+ client.useLegacyUnreadCount = false;
24876
25198
  client.emitter.emit('unreadCountEnabled', true);
24877
25199
  return true;
24878
25200
  };
@@ -25188,7 +25510,12 @@ const login = async (params, sessionHandler, config) => {
25188
25510
  // NOTE: This is a temporary solution to handle the channel marker when the user is forced to leave
25189
25511
  // the channel because currently backend can't handle this, so every time a user is banned from
25190
25512
  // a channel or the channel is deleted the channel's unread count will not be reset to zero
25191
- onChannelDeleted(removeChannelMarkerCache), onChannelMemberBanned(removeChannelMarkerCache), markReadEngineOnLoginHandler(), analyticsEngineOnLoginHandler(), readReceiptSyncEngineOnLoginHandler(), objectResolverEngineOnLoginHandler());
25513
+ onChannelDeleted(removeChannelMarkerCache), onChannelMemberBanned(removeChannelMarkerCache), markReadEngineOnLoginHandler(), analyticsEngineOnLoginHandler(), objectResolverEngineOnLoginHandler());
25514
+ if (client.useLegacyUnreadCount) {
25515
+ subscriptions.push(readReceiptSyncEngineOnLoginHandler());
25516
+ }
25517
+ else
25518
+ subscriptions.push(legacyReadReceiptSyncEngineOnLoginHandler());
25192
25519
  const markerSyncUnsubscriber = await startMarkerSync();
25193
25520
  subscriptions.push(markerSyncUnsubscriber);
25194
25521
  }
@@ -25369,6 +25696,8 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
25369
25696
  const sessionState = "notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */;
25370
25697
  const sessionHandler = undefined;
25371
25698
  const isUnreadCountEnabled = false;
25699
+ // Legacy unread count is true by default
25700
+ const useLegacyUnreadCount = true;
25372
25701
  const client = {
25373
25702
  version: `${VERSION}`,
25374
25703
  apiKey,
@@ -25398,6 +25727,7 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
25398
25727
  getMessagePreviewSetting,
25399
25728
  use: () => setActiveClient(client),
25400
25729
  isUnreadCountEnabled,
25730
+ useLegacyUnreadCount,
25401
25731
  getMarkerSyncConsistentMode,
25402
25732
  /**
25403
25733
  * Prefix for the deviceId key in the local storage or async storage.
@@ -32528,6 +32858,24 @@ var index$f = /*#__PURE__*/Object.freeze({
32528
32858
  stopMessageReceiptSync: stopMessageReceiptSync
32529
32859
  });
32530
32860
 
32861
+ /**
32862
+ * Internal used only
32863
+ *
32864
+ * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
32865
+ *
32866
+ * @param callback The function to call when the event was fired
32867
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
32868
+ *
32869
+ * @category MessageMarker Events
32870
+ */
32871
+ const onChannelUnreadInfoUpdatedLocal = (callback) => {
32872
+ const client = getActiveClient();
32873
+ const filter = (payload) => {
32874
+ callback(payload);
32875
+ };
32876
+ return createEventSubscriber(client, 'channelMarker/onChannelUnreadInfoUpdatedLocal', 'local.channelUnreadInfo.updated', filter);
32877
+ };
32878
+
32531
32879
  /**
32532
32880
  * Internal used only
32533
32881
  *
@@ -32543,7 +32891,7 @@ const onChannelUnreadUpdatedLocal = (callback) => {
32543
32891
  const filter = (payload) => {
32544
32892
  callback(payload);
32545
32893
  };
32546
- return createEventSubscriber(client, 'channelMarker/onChannelUnreadUpdatedLocal', 'local.channelUnread.updated', filter);
32894
+ return createEventSubscriber(client, 'channel/onChannelUnreadUpdatedLocal', 'local.channelUnread.updated', filter);
32547
32895
  };
32548
32896
 
32549
32897
  /* begin_public_function
@@ -32745,6 +33093,7 @@ const getChannel = (channelId, callback) => {
32745
33093
  return onSubChannelUpdated(updateMessagePreview);
32746
33094
  }, 'channelId', 'channel'),
32747
33095
  convertEventPayload(onSubChannelCreated, 'channelId', 'channel'),
33096
+ convertEventPayload(onChannelUnreadInfoUpdatedLocal, 'channelId', 'channel'),
32748
33097
  convertEventPayload(onChannelUnreadUpdatedLocal, 'channelId', 'channel'),
32749
33098
  ], {
32750
33099
  forceDispatch: true,
@@ -33258,6 +33607,10 @@ class ChannelLiveCollectionController extends LiveCollectionController {
33258
33607
  },
33259
33608
  action: "OnResolveUnread" /* Amity.ChannelActionType.OnResolveUnread */,
33260
33609
  },
33610
+ {
33611
+ fn: convertEventPayload(onChannelUnreadInfoUpdatedLocal, 'channelId', 'channel'),
33612
+ action: "onUpdate" /* Amity.ChannelActionType.OnUpdate */,
33613
+ },
33261
33614
  {
33262
33615
  fn: convertEventPayload(onChannelUnreadUpdatedLocal, 'channelId', 'channel'),
33263
33616
  action: "onUpdate" /* Amity.ChannelActionType.OnUpdate */,