@amityco/ts-sdk 7.1.1-207e990f.0 → 7.1.1-279cf96.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 (38) hide show
  1. package/dist/@types/domains/channel.d.ts +2 -2
  2. package/dist/@types/domains/channel.d.ts.map +1 -1
  3. package/dist/@types/domains/client.d.ts +1 -0
  4. package/dist/@types/domains/client.d.ts.map +1 -1
  5. package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts +11 -0
  6. package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts.map +1 -0
  7. package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts +20 -0
  8. package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts.map +1 -0
  9. package/dist/channelRepository/observers/index.d.ts +1 -0
  10. package/dist/channelRepository/observers/index.d.ts.map +1 -1
  11. package/dist/channelRepository/utils/prepareChannelPayload.d.ts.map +1 -1
  12. package/dist/client/api/createClient.d.ts +1 -0
  13. package/dist/client/api/createClient.d.ts.map +1 -1
  14. package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts.map +1 -1
  15. package/dist/client/utils/endpoints.d.ts +1 -0
  16. package/dist/client/utils/endpoints.d.ts.map +1 -1
  17. package/dist/client/utils/setClientToken.d.ts.map +1 -1
  18. package/dist/index.cjs.js +154 -15
  19. package/dist/index.esm.js +154 -15
  20. package/dist/index.umd.js +4 -4
  21. package/dist/messageRepository/events/onMessageCreated.d.ts.map +1 -1
  22. package/dist/messageRepository/observers/getMessage.d.ts.map +1 -1
  23. package/package.json +1 -1
  24. package/src/@types/domains/channel.ts +2 -2
  25. package/src/@types/domains/client.ts +1 -0
  26. package/src/channelRepository/internalApi/getTotalChannelsUnread.ts +38 -0
  27. package/src/channelRepository/observers/getTotalChannelsUnread.ts +129 -0
  28. package/src/channelRepository/observers/index.ts +1 -0
  29. package/src/channelRepository/utils/prepareChannelPayload.ts +16 -7
  30. package/src/client/api/createClient.ts +4 -1
  31. package/src/client/utils/ReadReceiptSync/readReceiptSyncEngine.ts +5 -1
  32. package/src/client/utils/endpoints.ts +1 -0
  33. package/src/client/utils/setClientToken.ts +8 -0
  34. package/src/fileRepository/api/uploadFile.ts +1 -1
  35. package/src/fileRepository/api/uploadImage.ts +1 -1
  36. package/src/fileRepository/api/uploadVideo.ts +1 -1
  37. package/src/messageRepository/events/onMessageCreated.ts +15 -5
  38. package/src/messageRepository/observers/getMessage.ts +0 -1
package/dist/index.esm.js CHANGED
@@ -1547,6 +1547,7 @@ const API_REGIONS = {
1547
1547
  };
1548
1548
  const URLS = {
1549
1549
  http: 'https://apix.{region}.amity.co',
1550
+ upload: 'https://upload.{region}.amity.co',
1550
1551
  mqtt: 'wss://sse.{region}.amity.co:443/mqtt',
1551
1552
  };
1552
1553
  function computeUrl(type, region) {
@@ -21820,7 +21821,9 @@ class MessageReadReceiptSyncEngine {
21820
21821
  // Step 1: Optimistic update of channelUnread.readToSegment to message.segment and update unreadCount value
21821
21822
  const cacheKey = ['channelUnread', 'get', channelId];
21822
21823
  const channelUnread = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
21823
- if (channelUnread && segment > channelUnread.readToSegment) {
21824
+ if (typeof (channelUnread === null || channelUnread === void 0 ? void 0 : channelUnread.readToSegment) === 'number' &&
21825
+ channelUnread &&
21826
+ segment > channelUnread.readToSegment) {
21824
21827
  channelUnread.readToSegment = segment;
21825
21828
  channelUnread.unreadCount = Math.max(channelUnread.lastSegment - segment, 0);
21826
21829
  pushToCache(cacheKey, channelUnread);
@@ -23153,17 +23156,24 @@ const preUpdateChannelCache = (rawPayload, options = { isMessagePreviewUpdated:
23153
23156
  const updateChannelUnread = ({ currentUserId, channels, channelUsers, }) => {
23154
23157
  for (let i = 0; i < channels.length; i += 1) {
23155
23158
  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
- };
23159
+ const channelUser = channelUsers.find(channelUser => channelUser.channelId === channels[i].channelId && channelUser.userId === currentUserId);
23160
+ let unreadCount = 0;
23161
+ let readToSegment = null;
23162
+ let lastMentionedSegment = null;
23163
+ let isMentioned = false;
23164
+ if (channelUser) {
23165
+ readToSegment = channelUser.readToSegment;
23166
+ lastMentionedSegment = channelUser.lastMentionedSegment;
23167
+ unreadCount = Math.max(channels[i].messageCount - readToSegment, 0);
23168
+ isMentioned = lastMentionedSegment > readToSegment;
23169
+ }
23160
23170
  pushToCache(cacheKey, {
23161
23171
  channelId: channels[i].channelId,
23162
23172
  lastSegment: channels[i].messageCount,
23163
23173
  readToSegment,
23164
23174
  lastMentionedSegment,
23165
- unreadCount: channels[i].messageCount - readToSegment,
23166
- isMentioned: lastMentionedSegment > readToSegment,
23175
+ unreadCount,
23176
+ isMentioned,
23167
23177
  isDeleted: channels[i].isDeleted,
23168
23178
  });
23169
23179
  }
@@ -23965,6 +23975,12 @@ const setClientToken = async (params) => {
23965
23975
  isGlobalBanned: false,
23966
23976
  isUserDeleted: false,
23967
23977
  };
23978
+ client.upload.defaults.headers.common.Authorization = `Bearer ${accessToken}`;
23979
+ client.upload.defaults.metadata = {
23980
+ tokenExpiry: expiresAt,
23981
+ isGlobalBanned: false,
23982
+ isUserDeleted: false,
23983
+ };
23968
23984
  // manually setup the token for ws transport
23969
23985
  if (client.ws)
23970
23986
  client.ws.io.opts.query = { token: accessToken };
@@ -25008,7 +25024,10 @@ const onMessageCreatedMqtt = (callback) => {
25008
25024
  'get',
25009
25025
  message.channelId,
25010
25026
  ])) === null || _a === void 0 ? void 0 : _a.data;
25011
- if (!channelUnread || channelUnread.lastSegment >= message.segment)
25027
+ if (!channelUnread ||
25028
+ channelUnread.lastSegment >= message.segment ||
25029
+ typeof channelUnread.readToSegment !== 'number' ||
25030
+ typeof channelUnread.lastMentionSegment !== 'number')
25012
25031
  return;
25013
25032
  const lastSegment = message.segment;
25014
25033
  const isMentionedInMessage = (_b = message.mentionedUsers) === null || _b === void 0 ? void 0 : _b.some(mention => {
@@ -25020,7 +25039,9 @@ const onMessageCreatedMqtt = (callback) => {
25020
25039
  const lastMentionSegment = isMentionedInMessage
25021
25040
  ? message.segment
25022
25041
  : channelUnread.lastMentionSegment;
25023
- pushToCache(['channelUnread', 'get', message.channelId], Object.assign(Object.assign({}, channelUnread), { lastSegment, unreadCount: lastSegment - channelUnread.readToSegment, lastMentionSegment, isMentioned: !(channelUnread.readToSegment >= lastMentionSegment) }));
25042
+ const updatedChannelUnread = Object.assign(Object.assign({}, channelUnread), { lastSegment, unreadCount: Math.max(lastSegment - channelUnread.readToSegment, 0), lastMentionSegment, isMentioned: !(channelUnread.readToSegment >= lastMentionSegment) });
25043
+ pushToCache(['channelUnread', 'get', message.channelId], updatedChannelUnread);
25044
+ fireEvent('local.channelUnread.updated', updatedChannelUnread);
25024
25045
  });
25025
25046
  }
25026
25047
  // Update in cache
@@ -25677,15 +25698,17 @@ const DEFAULT_DEBUG_SESSION = 'amity';
25677
25698
  * @category Client API
25678
25699
  * */
25679
25700
  const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAULT_DEBUG_SESSION, apiEndpoint, prefixDeviceIdKey, rteEnabled = true, } = {}) => {
25680
- var _a, _b;
25701
+ var _a, _b, _c;
25681
25702
  const log = createLogger(debugSession);
25682
25703
  log('client/api/createClient', {
25683
25704
  apiKey: apiKey.replace(/.{5}$/g, 'xxxxx'),
25684
25705
  apiRegion,
25685
25706
  });
25686
25707
  const httpEndpoint = (_a = apiEndpoint === null || apiEndpoint === void 0 ? void 0 : apiEndpoint.http) !== null && _a !== void 0 ? _a : computeUrl('http', apiRegion);
25687
- const mqttEndpoint = (_b = apiEndpoint === null || apiEndpoint === void 0 ? void 0 : apiEndpoint.mqtt) !== null && _b !== void 0 ? _b : computeUrl('mqtt', apiRegion);
25708
+ const uploadEndpoint = (_b = apiEndpoint === null || apiEndpoint === void 0 ? void 0 : apiEndpoint.upload) !== null && _b !== void 0 ? _b : computeUrl('upload', apiRegion);
25709
+ const mqttEndpoint = (_c = apiEndpoint === null || apiEndpoint === void 0 ? void 0 : apiEndpoint.mqtt) !== null && _c !== void 0 ? _c : computeUrl('mqtt', apiRegion);
25688
25710
  const http = createHttpTransport(httpEndpoint);
25711
+ const upload = createHttpTransport(uploadEndpoint);
25689
25712
  let ws;
25690
25713
  let mqtt;
25691
25714
  if (rteEnabled) {
@@ -25716,6 +25739,7 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
25716
25739
  http,
25717
25740
  ws,
25718
25741
  mqtt,
25742
+ upload,
25719
25743
  emitter,
25720
25744
  /*
25721
25745
  * Session Components
@@ -25745,7 +25769,7 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
25745
25769
  return activeClient;
25746
25770
  setActiveClient(client);
25747
25771
  }
25748
- catch (_c) {
25772
+ catch (_d) {
25749
25773
  setActiveClient(client);
25750
25774
  }
25751
25775
  return client;
@@ -28534,7 +28558,7 @@ const uploadFile = async (formData, onProgress) => {
28534
28558
  const headers = 'getHeaders' in formData
28535
28559
  ? formData.getHeaders()
28536
28560
  : { 'content-type': 'multipart/form-data' };
28537
- const { data } = await client.http.post('/api/v4/files', formData, {
28561
+ const { data } = await client.upload.post('/api/v4/files', formData, {
28538
28562
  headers,
28539
28563
  onUploadProgress({ loaded, total = 100 }) {
28540
28564
  onProgress && onProgress(Math.round((loaded * 100) / total));
@@ -28624,7 +28648,7 @@ const uploadVideo = async (formData, feedType, onProgress) => {
28624
28648
  const headers = 'getHeaders' in formData
28625
28649
  ? formData.getHeaders()
28626
28650
  : { 'content-type': 'multipart/form-data' };
28627
- const { data } = await client.http.post('/api/v4/videos', formData, {
28651
+ const { data } = await client.upload.post('/api/v4/videos', formData, {
28628
28652
  headers,
28629
28653
  onUploadProgress({ loaded, total = 100 }) {
28630
28654
  onProgress && onProgress(Math.round((loaded * 100) / total));
@@ -28672,7 +28696,7 @@ const uploadImage = async (formData, onProgress) => {
28672
28696
  const headers = 'getHeaders' in formData
28673
28697
  ? formData.getHeaders()
28674
28698
  : { 'content-type': 'multipart/form-data' };
28675
- const { data } = await client.http.post('/api/v4/images', formData, {
28699
+ const { data } = await client.upload.post('/api/v4/images', formData, {
28676
28700
  headers,
28677
28701
  onUploadProgress({ loaded, total = 100 }) {
28678
28702
  onProgress && onProgress(Math.round((loaded * 100) / total));
@@ -33680,6 +33704,120 @@ const getChannels = (params, callback, config) => {
33680
33704
  };
33681
33705
  /* end_public_function */
33682
33706
 
33707
+ /**
33708
+ *
33709
+ * Calculate user unread from {@link Amity.ChannelUnread} objects
33710
+ *
33711
+ * @returns the {@link Amity.UserUnread} objects
33712
+ *
33713
+ * @category Channel API
33714
+ * @async
33715
+ */
33716
+ const getTotalChannelsUnread$1 = () => {
33717
+ var _a;
33718
+ const client = getActiveClient();
33719
+ client.log('channel/getTotalChannelsUnread.locally');
33720
+ const cachedChannelsUnread = ((_a = queryCache(['channelUnread', 'get'])) === null || _a === void 0 ? void 0 : _a.filter(({ data }) => {
33721
+ return !data.isDeleted;
33722
+ })) || [];
33723
+ const totalChannelsUnread = (cachedChannelsUnread === null || cachedChannelsUnread === void 0 ? void 0 : cachedChannelsUnread.reduce((acc, { data }) => {
33724
+ acc.unreadCount += data.unreadCount;
33725
+ acc.isMentioned = acc.isMentioned || data.isMentioned;
33726
+ return acc;
33727
+ }, { unreadCount: 0, isMentioned: false })) || { unreadCount: 0, isMentioned: false };
33728
+ const cachedAt = client.cache && Date.now();
33729
+ return {
33730
+ data: totalChannelsUnread,
33731
+ cachedAt,
33732
+ };
33733
+ };
33734
+
33735
+ /* begin_public_function
33736
+ id: totalChannelsUnread.get
33737
+ */
33738
+ /**
33739
+ * ```js
33740
+ * import { ChannelRepository } from '@amityco/ts-sdk';
33741
+ *
33742
+ * let totalChannelsUnread;
33743
+ *
33744
+ * const unsubscribe = ChannelRepository.getTotalChannelsUnread(response => {
33745
+ * unread = response.data;
33746
+ * });
33747
+ * ```
33748
+ *
33749
+ * Observe all mutation on a given {@link Amity.UserUnread}
33750
+ *
33751
+ * @returns An {@link Amity.UserUnread} function to run when willing to stop observing the message
33752
+ *
33753
+ * @category User Unread Live Object
33754
+ *
33755
+ */
33756
+ const getTotalChannelsUnread = (callback) => {
33757
+ const { _id: userId } = getActiveUser();
33758
+ if (!userId)
33759
+ throw new ASCError('The _id has not been defined in ActiveUser', 800000 /* Amity.ClientError.UNKNOWN_ERROR */, "error" /* Amity.ErrorLevel.ERROR */);
33760
+ const { log, cache } = getActiveClient();
33761
+ if (!cache) {
33762
+ console.log('For using Live Object feature you need to enable Cache!');
33763
+ }
33764
+ const timestamp = Date.now();
33765
+ log(`liveTotalChannelsUnread(tmpid: ${timestamp}) > listen`);
33766
+ const disposers = [];
33767
+ let isUnsyncedModel = false; // for messages
33768
+ let model;
33769
+ const dispatcher = (data) => {
33770
+ const { data: userUnread } = data;
33771
+ const callbackModel = userUnread
33772
+ ? {
33773
+ unreadCount: userUnread.unreadCount,
33774
+ isMentioned: userUnread.isMentioned,
33775
+ }
33776
+ : undefined;
33777
+ model = callbackModel ? convertGetterPropsToStatic(callbackModel) : callbackModel;
33778
+ callback({
33779
+ data: callbackModel
33780
+ ? Object.assign(Object.assign({}, callbackModel), { isMentioned: callbackModel.isMentioned }) : callbackModel,
33781
+ loading: data.loading,
33782
+ error: data.error,
33783
+ });
33784
+ };
33785
+ const realtimeRouter = (userUnread) => {
33786
+ if (isEqual(model, userUnread))
33787
+ return;
33788
+ dispatcher({
33789
+ loading: false,
33790
+ data: userUnread,
33791
+ });
33792
+ };
33793
+ const onFetch = () => {
33794
+ const query = createQuery(async () => getTotalChannelsUnread$1());
33795
+ runQuery(query, ({ error, data, loading, origin, cachedAt }) => {
33796
+ if (cachedAt === UNSYNCED_OBJECT_CACHED_AT_VALUE) {
33797
+ dispatcher({
33798
+ data,
33799
+ origin,
33800
+ loading: false,
33801
+ error: new ASCApiError(UNSYNCED_OBJECT_CACHED_AT_MESSAGE, 800800 /* Amity.ClientError.DISALOOW_UNSYNCED_OBJECT */, "error" /* Amity.ErrorLevel.ERROR */),
33802
+ });
33803
+ isUnsyncedModel = true;
33804
+ disposers.forEach(fn => fn());
33805
+ }
33806
+ else if (!isUnsyncedModel) {
33807
+ dispatcher({ loading, data, origin, error });
33808
+ }
33809
+ if (error) {
33810
+ disposers.forEach(fn => fn());
33811
+ }
33812
+ });
33813
+ };
33814
+ disposers.push(onChannelUnreadUpdatedLocal(realtimeRouter));
33815
+ onFetch();
33816
+ return () => {
33817
+ disposers.forEach(fn => fn());
33818
+ };
33819
+ };
33820
+
33683
33821
  /* begin_public_function
33684
33822
  id: channel.member.add
33685
33823
  */
@@ -34283,6 +34421,7 @@ var index$c = /*#__PURE__*/Object.freeze({
34283
34421
  onChannelMemberRoleRemoved: onChannelMemberRoleRemoved,
34284
34422
  getChannel: getChannel,
34285
34423
  getChannels: getChannels,
34424
+ getTotalChannelsUnread: getTotalChannelsUnread,
34286
34425
  MARKER_INCLUDED_CHANNEL_TYPE: MARKER_INCLUDED_CHANNEL_TYPE,
34287
34426
  isUnreadCountSupport: isUnreadCountSupport,
34288
34427
  convertFromRaw: convertFromRaw,