@amityco/ts-sdk-react-native 7.0.1-91b5585.0 → 7.0.2-3cbb4fc.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 (39) hide show
  1. package/dist/@types/core/linkPreview.d.ts +12 -0
  2. package/dist/@types/core/linkPreview.d.ts.map +1 -0
  3. package/dist/@types/domains/client.d.ts +0 -1
  4. package/dist/@types/domains/client.d.ts.map +1 -1
  5. package/dist/client/api/createClient.d.ts.map +1 -1
  6. package/dist/client/api/fetchLinkPreview.d.ts +14 -0
  7. package/dist/client/api/fetchLinkPreview.d.ts.map +1 -0
  8. package/dist/client/api/index.d.ts +1 -0
  9. package/dist/client/api/index.d.ts.map +1 -1
  10. package/dist/core/model/index.d.ts +0 -1
  11. package/dist/core/model/index.d.ts.map +1 -1
  12. package/dist/index.cjs.js +48 -122
  13. package/dist/index.esm.js +48 -122
  14. package/dist/index.umd.js +3 -3
  15. package/dist/messageRepository/api/createMessage.d.ts.map +1 -1
  16. package/dist/messageRepository/utils/getMessageFromMainDB.d.ts.map +1 -1
  17. package/dist/messageRepository/utils/index.d.ts +0 -1
  18. package/dist/messageRepository/utils/index.d.ts.map +1 -1
  19. package/dist/messageRepository/utils/prepareMessagePayload.d.ts.map +1 -1
  20. package/dist/utils/event.d.ts.map +1 -1
  21. package/package.json +1 -1
  22. package/src/@types/core/linkPreview.ts +12 -0
  23. package/src/@types/domains/client.ts +0 -2
  24. package/src/client/api/createClient.ts +0 -26
  25. package/src/client/api/fetchLinkPreview.ts +30 -0
  26. package/src/client/api/index.ts +1 -0
  27. package/src/core/model/index.ts +0 -1
  28. package/src/messageRepository/api/createMessage.ts +0 -7
  29. package/src/messageRepository/observers/getMessages/MessageQueryStreamController.ts +4 -4
  30. package/src/messageRepository/utils/getMessageFromMainDB.ts +5 -9
  31. package/src/messageRepository/utils/index.ts +0 -1
  32. package/src/messageRepository/utils/prepareMessagePayload.ts +17 -22
  33. package/src/utils/event.ts +2 -19
  34. package/dist/core/model/localReferenceId.d.ts +0 -2
  35. package/dist/core/model/localReferenceId.d.ts.map +0 -1
  36. package/dist/messageRepository/utils/getMessageIdentifierIds.d.ts +0 -2
  37. package/dist/messageRepository/utils/getMessageIdentifierIds.d.ts.map +0 -1
  38. package/src/core/model/localReferenceId.ts +0 -12
  39. package/src/messageRepository/utils/getMessageIdentifierIds.ts +0 -37
package/dist/index.esm.js CHANGED
@@ -89,8 +89,8 @@ const PostContentType = Object.freeze({
89
89
 
90
90
  function getVersion() {
91
91
  try {
92
- // the string ''v7.0.0-esm'' should be replaced by actual value by @rollup/plugin-replace
93
- return 'v7.0.0-esm';
92
+ // the string ''v7.0.1-esm'' should be replaced by actual value by @rollup/plugin-replace
93
+ return 'v7.0.1-esm';
94
94
  }
95
95
  catch (error) {
96
96
  return '__dev__';
@@ -538,18 +538,6 @@ const idResolvers = {
538
538
  */
539
539
  const getResolver = (name) => idResolvers[name];
540
540
 
541
- /*
542
- * To check if an id is a local referenceId, the id must start with 'LOCAL_'.
543
- *
544
- * Example: LOCAL_155f5158-281a-4a9d-a445-9243138d2041
545
- *
546
- * @param id as string
547
- * @returns true if the id is a local referenceId, false otherwise
548
- */
549
- const isLocalId = (id) => {
550
- return id.startsWith('LOCAL_');
551
- };
552
-
553
541
  /**
554
542
  * A map of v3 response keys to a store name.
555
543
  * @hidden
@@ -22566,26 +22554,20 @@ function convertFromRaw$2(_a) {
22566
22554
  } }, rest), { channelId: channelPublicId, creatorId: creatorPublicId, displayName: name, lastActivity: lastMessageTimestamp, latestMessageId: lastMessageId, messageCount: childCount, subChannelId: messageFeedId, isUnreadCountSupport: isUnreadCountSupport$2({ channelType }) });
22567
22555
  }
22568
22556
 
22569
- const addLocalReferenceId = (payload) => {
22570
- const client = getActiveClient();
22571
- const { objectSyncMap } = client;
22572
- return Object.assign(Object.assign({}, payload), {
22573
- /* NOTE: This logic is used to get local referenceId for each message.
22574
- *
22575
- * if messageId is a local reference id, use it as referenceId else get referenceId from objectSyncMap.
22576
- * if find referenceId in objectSyncMap, this means this message is a local created message. The referenceId will be local reference id.
22577
- * if cannot find referenceId in objectSyncMap, referenceId will be undefined.
22578
- *
22579
- * The referenceId is undefined means this message is not a local created message (optimistic creation message).
22580
- */
22581
- referenceId: isLocalId(payload.messageId)
22582
- ? payload.messageId
22583
- : objectSyncMap.get(payload.messageId) });
22557
+ const mergePayloadWithLocal = (payload) => {
22558
+ var _a, _b, _c;
22559
+ const localMessage = (_b = (_a = queryCache(['message', 'get'])) === null || _a === void 0 ? void 0 : _a.find(({ data }) => data.messageId === payload.messageId)) === null || _b === void 0 ? void 0 : _b.data;
22560
+ if (localMessage) {
22561
+ return Object.assign(Object.assign(Object.assign({}, localMessage), payload), {
22562
+ // NOTE: referenceId is missing in the some payload event. If we have local message data with referenceId, use it instead.
22563
+ referenceId: (_c = localMessage.referenceId) !== null && _c !== void 0 ? _c : payload.referenceId });
22564
+ }
22565
+ return payload;
22584
22566
  };
22585
22567
  function convertFromRaw$1(message, reactors, event) {
22586
22568
  var _a;
22587
- const messageWithReferenceId = addLocalReferenceId(message);
22588
- const { channelPublicId, childCount, creatorPublicId, mentionedUsers, messageFeedId, myReactions, reactionCount, reactions, referenceId, segment, messageId, creatorId } = messageWithReferenceId, rest = __rest(messageWithReferenceId, ["channelPublicId", "childCount", "creatorPublicId", "mentionedUsers", "messageFeedId", "myReactions", "reactionCount", "reactions", "referenceId", "segment", "messageId", "creatorId"]);
22569
+ const mergeMessage = mergePayloadWithLocal(message);
22570
+ const { channelPublicId, childCount, creatorPublicId, mentionedUsers, messageFeedId, myReactions, reactionCount, reactions, referenceId, segment, messageId, creatorId } = mergeMessage, rest = __rest(mergeMessage, ["channelPublicId", "childCount", "creatorPublicId", "mentionedUsers", "messageFeedId", "myReactions", "reactionCount", "reactions", "referenceId", "segment", "messageId", "creatorId"]);
22589
22571
  let cache;
22590
22572
  if (referenceId) {
22591
22573
  cache = pullFromCache(['message', 'get', referenceId]);
@@ -24773,42 +24755,6 @@ const updateSubChannelUnreadFromMessage = (message) => {
24773
24755
  pushToCache(cacheKeyUnreadCount, updatedCachedUnreadCount);
24774
24756
  };
24775
24757
 
24776
- /*
24777
- * To get message identifier ids used as a cache key for the each message.
24778
- * These ids are used to store in the query stream and use to get messages from cache.
24779
- *
24780
- * The query stream data keeps the message identifier ids as follows:
24781
- * ['server_message_id_1', 'server_message_id_2', 'LOCAL_message_id_1', 'LOCAL_message_id_2']
24782
- *
24783
- * Example use case: Message created locally and then created on the server.
24784
- *
24785
- * 1. Message created locally has `LOCAL_message_id_2` as referenceId.
24786
- * 2. The `message.created` event will be fired from server with message payload that has no the referenceId.
24787
- * 3. SDK use this function to find indentifer id for each message in the payload. If the message is created locally, the identifier id will be the local reference id.
24788
- * 4. The SDK use this identifier ids to check if the message is already in the query stream before appending it.
24789
- *
24790
- * @param The raw message payload from server response
24791
- * @returns The identifier ids of the messages
24792
- */
24793
- function getMessageIdentifierIds(messages) {
24794
- const client = getActiveClient();
24795
- const { objectSyncMap } = client;
24796
- return messages
24797
- .map(message => {
24798
- var _a;
24799
- /* NOTE: This logic is used to get identifier id for each message.
24800
- *
24801
- * if messageId is a local id, use it as identifier id else get identifier id from objectSyncMap.
24802
- * if find referenceId in objectSyncMap, this means this message is a local created message. The identifier id will be local message id.
24803
- * if cannot find identifier id in objectSyncMap, this means this message is not a local created message. The identifier id will be server message id.
24804
- */
24805
- if (isLocalId(message.messageId))
24806
- return message.messageId;
24807
- return (_a = objectSyncMap.get(message.messageId)) !== null && _a !== void 0 ? _a : message.messageId;
24808
- })
24809
- .filter(Boolean);
24810
- }
24811
-
24812
24758
  /**
24813
24759
  * ```js
24814
24760
  * import { onMessageCreated } from '@amityco/ts-sdk-react-native'
@@ -25528,7 +25474,6 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
25528
25474
  const sessionState = "notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */;
25529
25475
  const sessionHandler = undefined;
25530
25476
  const isUnreadCountEnabled = false;
25531
- const objectSyncMap = new Map();
25532
25477
  const client = {
25533
25478
  version: `${VERSION}`,
25534
25479
  apiKey,
@@ -25565,29 +25510,6 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
25565
25510
  * This is allow user to have multiple SDK client and Mqtt client within the same app.
25566
25511
  */
25567
25512
  prefixDeviceIdKey,
25568
- /*
25569
- * The objectSyncMap is used to keep the map between local referenceId and server id in two ways mapping.
25570
- *
25571
- * For objects created locally (optimistic creation), the SDK stores them with their local ID as the cache key, which is used throughout the entire SDK session (in-memory) for syncing updates.
25572
- * If the payload or response only contains the remote ID of the same object, the SDK will look up the corresponding local ID to retrieve the object through ObjectSyncMap.
25573
- *
25574
- * This is useful for syncing updates to the object, as the SDK can find the local ID from the server ID and update the object in the cache correctly.
25575
- *
25576
- * Example:
25577
- * In the case of message optimistically creation. The referenceId is created locally.
25578
- *
25579
- * 1. When creating a message optimistically, a local referenceId is created. The objectSyncMap will have the following structure.
25580
- * {
25581
- * "LOCAL_uuid": undefined,
25582
- * }
25583
- *
25584
- * 2. After the message was created on the server and SDK received the response from server, the objectSyncMap will be updated to
25585
- * {
25586
- * "LOCAL_uuid": "server_message_id",
25587
- * "server_message_id": "LOCAL_uuid"
25588
- * }
25589
- */
25590
- objectSyncMap,
25591
25513
  };
25592
25514
  try {
25593
25515
  const activeClient = getActiveClient();
@@ -25715,6 +25637,28 @@ const unregisterPushNotification = async () => {
25715
25637
  return status === 'success';
25716
25638
  };
25717
25639
 
25640
+ /**
25641
+ * ```js
25642
+ * import { fetchLinkPreview } from '@amityco/ts-sdk'
25643
+ * const { title, description, imageUrl } = fetchLinkPreview('https://www.example.com/')
25644
+ * ```
25645
+ *
25646
+ *
25647
+ * @param url the url to fetch link preview
25648
+ * @returns A {@link Amity.LinkPreview} instance
25649
+ *
25650
+ * @category Client API
25651
+ * */
25652
+ const fetchLinkPreview = async (url) => {
25653
+ const client = getActiveClient();
25654
+ let fetchUrl = url;
25655
+ if (!/^https?:\/\//i.test(url)) {
25656
+ fetchUrl = `https://${url}`;
25657
+ }
25658
+ const { data } = await client.http.get(`/api/v1/link-preview?url=${encodeURIComponent(fetchUrl)}`);
25659
+ return data;
25660
+ };
25661
+
25718
25662
  /**
25719
25663
  * ```js
25720
25664
  * import { onChannelMarkerFetched } from '@amityco/ts-sdk-react-native'
@@ -26085,6 +26029,7 @@ var index$l = /*#__PURE__*/Object.freeze({
26085
26029
  registerPushNotification: registerPushNotification,
26086
26030
  unregisterPushNotification: unregisterPushNotification,
26087
26031
  onRTEConnectionStateChange: onRTEConnectionStateChange,
26032
+ fetchLinkPreview: fetchLinkPreview,
26088
26033
  onConnectionError: onConnectionError,
26089
26034
  onClientDisconnected: onClientDisconnected,
26090
26035
  onClientBanned: onClientBanned,
@@ -27914,24 +27859,12 @@ function prepareBlockedUserPayload(response) {
27914
27859
  }
27915
27860
 
27916
27861
  const convertEventPayload = (eventHandler, sourceModelProp, destinationDomain) => (callback) => eventHandler(sourceModel => {
27917
- var _a, _b;
27862
+ var _a;
27918
27863
  if (!sourceModel) {
27919
27864
  return sourceModel;
27920
27865
  }
27921
- const client = getActiveClient();
27922
- const { objectSyncMap } = client;
27923
- /*
27924
- * NOTE: For objects created locally (optimistic creation), the SDK stores them with their local ID as the cache key, which is used throughout the entire SDK session (in-memory) for syncing updates.
27925
- * If the payload or response only contains the remote ID of the same object, the SDK will look up the corresponding local ID to retrieve the object.
27926
- */
27927
- const resolvedId = isLocalId(sourceModel[sourceModelProp])
27928
- ? sourceModel[sourceModelProp]
27929
- : (_a = objectSyncMap.get(sourceModel[sourceModelProp])) !== null && _a !== void 0 ? _a : sourceModel[sourceModelProp];
27930
- const model = (_b = pullFromCache([
27931
- destinationDomain,
27932
- 'get',
27933
- `${resolvedId}`,
27934
- ])) === null || _b === void 0 ? void 0 : _b.data;
27866
+ const cacheKey = [destinationDomain, 'get', `${sourceModel[sourceModelProp]}`];
27867
+ const model = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
27935
27868
  if (!model)
27936
27869
  return;
27937
27870
  return callback(model);
@@ -31159,7 +31092,6 @@ let uniqueId;
31159
31092
  const createMessageOptimistic = (bundle) => {
31160
31093
  var _a, _b;
31161
31094
  const client = getActiveClient();
31162
- const { objectSyncMap } = client;
31163
31095
  if (!client.cache)
31164
31096
  return;
31165
31097
  /*
@@ -31192,7 +31124,6 @@ const createMessageOptimistic = (bundle) => {
31192
31124
  const message = Object.assign({ creatorId: client.userId, creatorPrivateId: getActiveUser()._id, channelSegment: ((_b = subChannel === null || subChannel === void 0 ? void 0 : subChannel.data.messageCount) !== null && _b !== void 0 ? _b : 0) + 1, childrenNumber: 0, createdAt: createdTime, updatedAt: createdTime, syncState: "syncing" /* Amity.SyncState.Syncing */, isDeleted: false }, bundleWithMessageId);
31193
31125
  const cachedAt = UNSYNCED_OBJECT_CACHED_AT_VALUE;
31194
31126
  pushToCache(['message', 'get', message.messageId], message, { cachedAt });
31195
- objectSyncMap.set(message.messageId, undefined);
31196
31127
  fireEvent('local.message.created', { messages: [message] });
31197
31128
  return message;
31198
31129
  };
@@ -31220,15 +31151,12 @@ const createMessageOptimistic = (bundle) => {
31220
31151
  */
31221
31152
  const createMessage = async (bundle) => {
31222
31153
  const client = getActiveClient();
31223
- const { objectSyncMap } = client;
31224
31154
  client.log('message/createMessage', bundle);
31225
31155
  const optimisticData = createMessageOptimistic(bundle);
31226
31156
  const referenceId = bundle.referenceId || uniqueId || getLocalId();
31227
31157
  uniqueId = undefined;
31228
31158
  try {
31229
31159
  const { data: payload } = await client.http.post('/api/v5/messages', Object.assign(Object.assign({}, convertParams(bundle)), { referenceId }));
31230
- objectSyncMap.set(referenceId, payload.messages[0].messageId);
31231
- objectSyncMap.set(payload.messages[0].messageId, referenceId);
31232
31160
  const data = await prepareMessagePayload(payload);
31233
31161
  const { messages } = data;
31234
31162
  const cachedAt = client.cache && Date.now();
@@ -31983,7 +31911,7 @@ class MessageQueryStreamController extends QueryStreamController {
31983
31911
  var _a, _b;
31984
31912
  if (refresh) {
31985
31913
  pushToCache(this.cacheKey, {
31986
- data: getMessageIdentifierIds(response.messages),
31914
+ data: response.messages.map(getResolver('message')),
31987
31915
  query: this.query,
31988
31916
  });
31989
31917
  }
@@ -31991,8 +31919,8 @@ class MessageQueryStreamController extends QueryStreamController {
31991
31919
  const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
31992
31920
  const messages = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
31993
31921
  pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: direction === 'next'
31994
- ? [...new Set([...messages, ...getMessageIdentifierIds(response.messages)])]
31995
- : [...new Set([...getMessageIdentifierIds(response.messages), ...messages])] }));
31922
+ ? [...new Set([...messages, ...response.messages.map(getResolver('message'))])]
31923
+ : [...new Set([...response.messages.map(getResolver('message')), ...messages])] }));
31996
31924
  }
31997
31925
  }
31998
31926
  reactor(action) {
@@ -32059,12 +31987,11 @@ class MessagePaginationController extends PaginationController {
32059
31987
 
32060
31988
  const getMessageFromMainDB = (messageId) => {
32061
31989
  var _a, _b;
32062
- const client = getActiveClient();
32063
- const { objectSyncMap } = client;
32064
- const resolvedMessageId = isLocalId(messageId)
32065
- ? messageId
32066
- : (_a = objectSyncMap.get(messageId)) !== null && _a !== void 0 ? _a : messageId;
32067
- return (_b = pullFromCache(['message', 'get', resolvedMessageId])) === null || _b === void 0 ? void 0 : _b.data;
31990
+ const message = (_a = pullFromCache(['message', 'get', messageId])) === null || _a === void 0 ? void 0 : _a.data;
31991
+ if (message)
31992
+ return message;
31993
+ const messages = queryCache(['message', 'get']);
31994
+ return (_b = messages === null || messages === void 0 ? void 0 : messages.find(({ data }) => data.messageId === messageId)) === null || _b === void 0 ? void 0 : _b.data;
32068
31995
  };
32069
31996
 
32070
31997
  class MessageLiveCollectionController extends LiveCollectionController {
@@ -32225,8 +32152,7 @@ var index$g = /*#__PURE__*/Object.freeze({
32225
32152
  convertFromRaw: convertFromRaw$1,
32226
32153
  prepareMessagePayload: prepareMessagePayload,
32227
32154
  convertParams: convertParams,
32228
- convertQueryParams: convertQueryParams$1,
32229
- getMessageIdentifierIds: getMessageIdentifierIds
32155
+ convertQueryParams: convertQueryParams$1
32230
32156
  });
32231
32157
 
32232
32158
  /**