@amityco/ts-sdk-react-native 6.34.1-b036e5d.0 → 6.35.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 (43) hide show
  1. package/.env +26 -26
  2. package/dist/@types/domains/client.d.ts +2 -2
  3. package/dist/@types/domains/client.d.ts.map +1 -1
  4. package/dist/client/api/createClient.d.ts +2 -1
  5. package/dist/client/api/createClient.d.ts.map +1 -1
  6. package/dist/client/api/isConnected.d.ts.map +1 -1
  7. package/dist/client/api/logout.d.ts.map +1 -1
  8. package/dist/client/utils/modifyMqttConnection.d.ts.map +1 -1
  9. package/dist/core/subscription.d.ts.map +1 -1
  10. package/dist/index.cjs.js +133 -22
  11. package/dist/index.esm.js +133 -22
  12. package/dist/index.umd.js +3 -3
  13. package/dist/reactionRepository/events/onReactorAddedLocal.d.ts +1 -1
  14. package/dist/reactionRepository/events/onReactorRemovedLocal.d.ts +1 -1
  15. package/dist/reactionRepository/observers/getReactions/getReactions.d.ts +1 -1
  16. package/dist/storyRepository/api/createImageStory.d.ts.map +1 -1
  17. package/dist/storyRepository/api/createVideoStory.d.ts.map +1 -1
  18. package/dist/storyRepository/internalApi/deleteStory.d.ts.map +1 -1
  19. package/dist/storyRepository/observers/getActiveStoriesByTarget.d.ts.map +1 -1
  20. package/dist/storyRepository/observers/getGlobalStoryTargets/GlobalStoryLiveCollectionController.d.ts.map +1 -1
  21. package/dist/storyRepository/utils/createOptimisticTarget.d.ts +5 -0
  22. package/dist/storyRepository/utils/createOptimisticTarget.d.ts.map +1 -0
  23. package/package.json +1 -1
  24. package/src/@types/domains/client.ts +2 -2
  25. package/src/client/api/createClient.ts +10 -2
  26. package/src/client/api/isConnected.ts +4 -1
  27. package/src/client/api/login.ts +1 -1
  28. package/src/client/api/logout.ts +6 -5
  29. package/src/client/utils/modifyMqttConnection.ts +2 -0
  30. package/src/client/utils/setClientToken.ts +1 -1
  31. package/src/core/events.ts +2 -2
  32. package/src/core/subscription.ts +2 -0
  33. package/src/core/transports/ws.ts +1 -1
  34. package/src/reactionRepository/events/onReactorAddedLocal.ts +1 -1
  35. package/src/reactionRepository/events/onReactorRemovedLocal.ts +1 -1
  36. package/src/reactionRepository/observers/getReactions/getReactions.ts +1 -1
  37. package/src/storyRepository/api/createImageStory.ts +3 -0
  38. package/src/storyRepository/api/createVideoStory.ts +3 -0
  39. package/src/storyRepository/internalApi/deleteStory.ts +74 -0
  40. package/src/storyRepository/observers/getActiveStoriesByTarget.ts +16 -1
  41. package/src/storyRepository/observers/getGlobalStoryTargets/GlobalStoryLiveCollectionController.ts +14 -2
  42. package/src/storyRepository/utils/StoryComputedValue.ts +2 -2
  43. package/src/storyRepository/utils/createOptimisticTarget.ts +46 -0
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 ''v6.34.0-esm'' should be replaced by actual value by @rollup/plugin-replace
93
- return 'v6.34.0-esm';
92
+ // the string ''v6.35.0-esm'' should be replaced by actual value by @rollup/plugin-replace
93
+ return 'v6.35.0-esm';
94
94
  }
95
95
  catch (error) {
96
96
  return '__dev__';
@@ -1348,14 +1348,14 @@ const createEventEmitter = () => {
1348
1348
  */
1349
1349
  const proxyWebsocketEvents = (ws, emitter) => {
1350
1350
  WS_EVENTS.forEach(event => {
1351
- ws.on(event, (param) => {
1351
+ ws === null || ws === void 0 ? void 0 : ws.on(event, (param) => {
1352
1352
  emitter.emit(event, param);
1353
1353
  });
1354
1354
  });
1355
1355
  };
1356
1356
  const proxyMqttEvents = (mqttClient, emitter) => {
1357
1357
  MQTT_EVENTS.forEach(event => {
1358
- mqttClient.on(event, (...params) => {
1358
+ mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.on(event, (...params) => {
1359
1359
  emitter.emit(event, params.length === 1 ? params[0] : params);
1360
1360
  });
1361
1361
  });
@@ -1413,6 +1413,8 @@ let mqttUserId;
1413
1413
  async function modifyMqttConnection() {
1414
1414
  var _a;
1415
1415
  const { mqtt, emitter, token } = getActiveClient();
1416
+ if (!mqtt)
1417
+ return;
1416
1418
  const accessToken = (_a = token === null || token === void 0 ? void 0 : token.accessToken) !== null && _a !== void 0 ? _a : '';
1417
1419
  const user = getActiveUser();
1418
1420
  if (mqttAccessToken !== accessToken || mqttUserId !== user._id) {
@@ -1523,6 +1525,8 @@ const getSmartFeedMessageTopic = () => {
1523
1525
  };
1524
1526
  function subscribeTopic(topic, callback) {
1525
1527
  const { mqtt } = getActiveClient();
1528
+ if (!mqtt)
1529
+ return () => null;
1526
1530
  modifyMqttConnection();
1527
1531
  return mqtt.subscribe(topic, callback);
1528
1532
  }
@@ -21353,8 +21357,8 @@ class StoryComputedValue {
21353
21357
  return;
21354
21358
  }
21355
21359
  const groupByType = stories.reduce((acc, story) => {
21356
- const { data: { targetId, syncState }, } = story;
21357
- if (targetId === this._targetId) {
21360
+ const { data: { targetId, syncState, isDeleted }, } = story;
21361
+ if (targetId === this._targetId && !isDeleted) {
21358
21362
  acc[syncState] += 1;
21359
21363
  }
21360
21364
  return acc;
@@ -23892,7 +23896,8 @@ const setClientToken = async (params) => {
23892
23896
  isUserDeleted: false,
23893
23897
  };
23894
23898
  // manually setup the token for ws transport
23895
- client.ws.io.opts.query = { token: accessToken };
23899
+ if (client.ws)
23900
+ client.ws.io.opts.query = { token: accessToken };
23896
23901
  client.token = { accessToken, issuedAt, expiresAt };
23897
23902
  setSessionState("established" /* Amity.SessionStates.ESTABLISHED */);
23898
23903
  return { accessToken, users };
@@ -24240,12 +24245,13 @@ var objectResolverEngineOnLoginHandler = () => {
24240
24245
  * @async
24241
24246
  */
24242
24247
  const logout = async () => {
24248
+ var _a, _b;
24243
24249
  const client = getActiveClient();
24244
24250
  client.log('client/api/disconnectClient');
24245
- if (client.mqtt.connected) {
24251
+ if (client.mqtt && client.mqtt.connected) {
24246
24252
  client.mqtt.disconnect();
24247
24253
  }
24248
- if (client.ws.connected) {
24254
+ if (client.ws && client.ws.connected) {
24249
24255
  client.ws.disconnect();
24250
24256
  }
24251
24257
  /*
@@ -24264,8 +24270,8 @@ const logout = async () => {
24264
24270
  setSessionState("notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */);
24265
24271
  client.emitter.all.clear();
24266
24272
  // FIXME: it removes listener in ws.ts, it breaks global ban event
24267
- client.ws.removeAllListeners();
24268
- client.mqtt.removeAllListeners();
24273
+ (_a = client.ws) === null || _a === void 0 ? void 0 : _a.removeAllListeners();
24274
+ (_b = client.mqtt) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
24269
24275
  client.userId = undefined;
24270
24276
  client.token = undefined;
24271
24277
  client.http.defaults.headers.common.Authorization = '';
@@ -24274,7 +24280,8 @@ const logout = async () => {
24274
24280
  isGlobalBanned: false,
24275
24281
  isUserDeleted: false,
24276
24282
  };
24277
- client.ws.io.opts.query = { token: '' };
24283
+ if (client.ws)
24284
+ client.ws.io.opts.query = { token: '' };
24278
24285
  if (typeof document !== 'undefined') {
24279
24286
  document.cookie = '_ascSession=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
24280
24287
  }
@@ -25257,6 +25264,7 @@ async function runMqtt() {
25257
25264
  * @async
25258
25265
  */
25259
25266
  const login = async (params, sessionHandler, config) => {
25267
+ var _a;
25260
25268
  const client = getActiveClient();
25261
25269
  let unsubWatcher;
25262
25270
  client.log('client/api/connectClient', Object.assign({ apiKey: client.apiKey, sessionState: client.sessionState }, params));
@@ -25291,7 +25299,7 @@ const login = async (params, sessionHandler, config) => {
25291
25299
  // FIXME: events are duplicated if connectClient is called few times without disconnectClient
25292
25300
  // wire websocket events to our event emitter
25293
25301
  proxyWebsocketEvents(client.ws, client.emitter);
25294
- client.ws.open();
25302
+ (_a = client.ws) === null || _a === void 0 ? void 0 : _a.open();
25295
25303
  client.userId = user.userId;
25296
25304
  client.sessionHandler = sessionHandler;
25297
25305
  /*
@@ -25496,7 +25504,7 @@ const DEFAULT_DEBUG_SESSION = 'amity';
25496
25504
  *
25497
25505
  * @category Client API
25498
25506
  * */
25499
- const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAULT_DEBUG_SESSION, apiEndpoint, prefixDeviceIdKey, } = {}) => {
25507
+ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAULT_DEBUG_SESSION, apiEndpoint, prefixDeviceIdKey, rteEnabled = true, } = {}) => {
25500
25508
  var _a, _b;
25501
25509
  const log = createLogger(debugSession);
25502
25510
  log('client/api/createClient', {
@@ -25506,8 +25514,12 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
25506
25514
  const httpEndpoint = (_a = apiEndpoint === null || apiEndpoint === void 0 ? void 0 : apiEndpoint.http) !== null && _a !== void 0 ? _a : computeUrl('http', apiRegion);
25507
25515
  const mqttEndpoint = (_b = apiEndpoint === null || apiEndpoint === void 0 ? void 0 : apiEndpoint.mqtt) !== null && _b !== void 0 ? _b : computeUrl('mqtt', apiRegion);
25508
25516
  const http = createHttpTransport(httpEndpoint);
25509
- const ws = createWebsocketTransport(httpEndpoint);
25510
- const mqtt = createMqttTransport(mqttEndpoint);
25517
+ let ws;
25518
+ let mqtt;
25519
+ if (rteEnabled) {
25520
+ ws = createWebsocketTransport(httpEndpoint);
25521
+ mqtt = createMqttTransport(mqttEndpoint);
25522
+ }
25511
25523
  const emitter = createEventEmitter();
25512
25524
  /*
25513
25525
  * Since v6 cache is enabled by default
@@ -25631,9 +25643,11 @@ const isConnected = () => {
25631
25643
  var _a, _b;
25632
25644
  const client = getActiveClient();
25633
25645
  client.log('client/api/isConnected', client);
25646
+ // if client is connected to ws, it means client is connected. If ws is undefined, it means ws is not used.
25647
+ const isWsConnected = (client.ws && client.ws.connected) || !!client.ws;
25634
25648
  return !!(client.userId &&
25635
25649
  ((_b = String((_a = client.http.defaults.headers.common) === null || _a === void 0 ? void 0 : _a.Authorization)) === null || _b === void 0 ? void 0 : _b.length) &&
25636
- client.ws.connected);
25650
+ isWsConnected);
25637
25651
  };
25638
25652
 
25639
25653
  var _GlobalFileAccessType_fileAccessType;
@@ -30697,7 +30711,7 @@ class ReactionQueryStreamController extends QueryStreamController {
30697
30711
 
30698
30712
  /**
30699
30713
  * ```js
30700
- * import { onReactorRemovedLocal } from '@amityco/ts-sdk'
30714
+ * import { onReactorRemovedLocal } from '@amityco/ts-sdk-react-native'
30701
30715
  * const dispose = onReactorRemoved('post', postId, reactor => {
30702
30716
  * // ...
30703
30717
  * })
@@ -30754,7 +30768,7 @@ const onReactorRemovedLocal = (referenceType, referenceId, callback) => {
30754
30768
 
30755
30769
  /**
30756
30770
  * ```js
30757
- * import { onReactorAddedLocal } from '@amityco/ts-sdk'
30771
+ * import { onReactorAddedLocal } from '@amityco/ts-sdk-react-native'
30758
30772
  * const dispose = onReactorAdded('post', postId, reactor => {
30759
30773
  * // ...
30760
30774
  * })
@@ -30884,7 +30898,7 @@ class ReactionLiveCollectionController extends LiveCollectionController {
30884
30898
  */
30885
30899
  /**
30886
30900
  * ```js
30887
- * import { getReactions } from '@amityco/ts-sdk'
30901
+ * import { getReactions } from '@amityco/ts-sdk-react-native'
30888
30902
  *
30889
30903
  * let reactions = []
30890
30904
  * const unsub = liveReactions({
@@ -41505,7 +41519,7 @@ var index$3 = /*#__PURE__*/Object.freeze({
41505
41519
  getPoll: getPoll
41506
41520
  });
41507
41521
 
41508
- const privateKey = "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDAARz+hmBgi8pJ\nQb8LeY41gtHhk+ACMwRfhsn7GqpqRQNG2qU0755mzZuVDUqjQMGSo8THJB7O+OJs\nflbZRkFXlFoFOVNw1UpNOgwEQZ6wB9oRwzepTJAfF1sVhm/o/ixvXh1zDFNDy6yZ\npXyiiJHUVxqyjllZhxnwdvjoVtDs6hW6awG09bB9nh/TTejlUKXoAgzqVwu/1QMu\nUVViET495elEe19aUarEy+oL2iKeXCEvqda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R3pw+V19YWAjq/glP6fz8wQKBgQD/oQq+ukKF0PRgBeM5\ngeTjSwsdGppQLmf5ndujvoiz/TpdjDEPu6R8kigQr1rG2t4K/yfdZoI8RdmJD1al\n3Q7N9hofooSy4rj6E3txzWZCHJjHad2cnCp/O26HiReGAl7wTcfTmNdiFHhZQzm5\nJBkvWAiwuvQMNfEbnXxw6/vIDwKBgQDH7fX8gsc77JLvAWgp1MaQN/sbqVb6JeT1\nFQfR8E/WFCSmzQBtNzd5KgYuCeelwr/8DyYytvN2BzCYZXp73gI1jF3YlW5jVn74\nOY6TwQ095digwo6Z0yuxopdIOApKgAkL9PRKgNrqAf3NAyMua6lOGifzjDojC3KU\nfylQmxMn4wKBgHp2B9O/H0dEBw5JQ8W0+JX6yWQz7mEjGiR2/1W+XXb8hQ1zr709\nw1r6Gb+EghRpnZ3fBpYGGbYOMFx8wKHM+N6qW3F0ReX8v2juFGE8aRSa5oYBrWzt\nU16Idjbv8hj84cZ1PJmdyvDtpYn9rpWHOZl4rxEbPvbqkIsOMyNVqdT5AoGAOSge\nmwIIU2le2FVeohbibXiToWTYKMuMmURZ5/r72AgKMmWJKbAPe+Q3wBG01/7FRBpQ\noU8Ma0HC8s6QJbliiEyIx9JwrJWd1vkdecBHONrtA4ibm/5zD2WcOllLF+FitLhi\n3qnX6+6F0IaFGFBPJrTzlv0P4dTz/OAdv52V7GECgYEA2TttOKBAqWllgOaZOkql\nLVMJVmgR7s6tLi1+cEP8ZcapV9aRbRzTAKXm4f8AEhtlG9F9kCOvHYCYGi6JaiWJ\nZkHjeex3T+eE6Di6y5Bm/Ift5jtVhJ4jCVwHOKTMej79NPUFTJfv8hCo29haBDv6\nRXFrv+T21KCcw8k3sJeJWWQ=\n-----END PRIVATE KEY-----";
41509
41523
  /*
41510
41524
  * The crypto algorithm used for importing key and signing string
41511
41525
  */
@@ -41941,6 +41955,30 @@ const createStory = async (payload) => {
41941
41955
  };
41942
41956
  };
41943
41957
 
41958
+ const createOptimisticTarget = async ({ targetId, targetType, }) => {
41959
+ var _a, _b;
41960
+ const targetCache = pullFromCache([
41961
+ "storyTarget" /* STORY_KEY_CACHE.STORY_TARGET */,
41962
+ 'get',
41963
+ targetId,
41964
+ ]);
41965
+ if (!targetCache) {
41966
+ let optimisticTarget = {
41967
+ targetId,
41968
+ targetType,
41969
+ };
41970
+ if (targetType === 'community') {
41971
+ const community = await getCommunity$1(targetId);
41972
+ optimisticTarget = Object.assign(Object.assign({}, optimisticTarget), { targetPublicId: community.data.communityId, targetUpdatedAt: (_a = community.data.updatedAt) !== null && _a !== void 0 ? _a : new Date().toISOString() });
41973
+ }
41974
+ if (targetType === 'user') {
41975
+ const user = await getUser$2(targetId);
41976
+ optimisticTarget = Object.assign(Object.assign({}, optimisticTarget), { targetPublicId: user.data.userPublicId, targetUpdatedAt: (_b = user.data.updatedAt) !== null && _b !== void 0 ? _b : new Date().toISOString() });
41977
+ }
41978
+ pushToCache(["storyTarget" /* STORY_KEY_CACHE.STORY_TARGET */, 'get', targetId], optimisticTarget);
41979
+ }
41980
+ };
41981
+
41944
41982
  /**
41945
41983
  * ```js
41946
41984
  * import { StoryRepository } from '@amityco/ts-sdk-react-native'
@@ -41990,6 +42028,7 @@ const createImageStory = async (targetType, targetId, formData, metadata = {}, i
41990
42028
  throw new Error('Failed to upload image');
41991
42029
  const { fileId } = data[0];
41992
42030
  payload = Object.assign(Object.assign({}, payload), { data: Object.assign(Object.assign({}, payload.data), { fileId }) });
42031
+ createOptimisticTarget({ targetId, targetType });
41993
42032
  // Fire optimistic event - update fileId
41994
42033
  createOptimisticEvent({ payload, formData }, optimisticData => {
41995
42034
  fireEvent('local.story.created', optimisticData);
@@ -42043,6 +42082,7 @@ const createVideoStory = async (targetType, targetId, formData, metadata = {}, i
42043
42082
  pushToCache(["story-sync-state" /* STORY_KEY_CACHE.SYNC_STATE */, targetId], "syncing" /* Amity.SyncState.Syncing */);
42044
42083
  // Update local story expires time
42045
42084
  pushToCache(["story-expire" /* STORY_KEY_CACHE.EXPIRE */, targetId], new Date(date.setFullYear(date.getFullYear() + 1)));
42085
+ createOptimisticTarget({ targetId, targetType });
42046
42086
  // Fire optimistic event
42047
42087
  createOptimisticEvent({ payload, formData, isVideo: true }, optimisticData => {
42048
42088
  fireEvent('local.story.created', optimisticData);
@@ -42080,6 +42120,7 @@ const getStoryCache = (storyId) => {
42080
42120
  };
42081
42121
 
42082
42122
  const deleteStory = async (storyId, permanent = false) => {
42123
+ var _a, _b, _c;
42083
42124
  const client = getActiveClient();
42084
42125
  client.log('story/deleteStory', storyId);
42085
42126
  let cached;
@@ -42096,6 +42137,57 @@ const deleteStory = async (storyId, permanent = false) => {
42096
42137
  users: [],
42097
42138
  stories: [Object.assign(Object.assign({}, data), { isDeleted: true })],
42098
42139
  });
42140
+ /* ------ Unsynced story case ------ */
42141
+ if (data.syncState !== "synced" /* Amity.SyncState.Synced */) {
42142
+ if (permanent) {
42143
+ dropFromCache(["story" /* STORY_KEY_CACHE.STORY */, 'get', storyId]);
42144
+ }
42145
+ else {
42146
+ pushToCache(["story" /* STORY_KEY_CACHE.STORY */, 'get', storyId], Object.assign(Object.assign({}, cached.data), { isDeleted: true }));
42147
+ }
42148
+ const unsyncedStoriesCache = (_a = queryCache([
42149
+ "story" /* STORY_KEY_CACHE.STORY */,
42150
+ 'get',
42151
+ ])) === null || _a === void 0 ? void 0 : _a.filter(story => story.data.syncState !== "synced" /* Amity.SyncState.Synced */ &&
42152
+ story.data.targetId === data.targetId &&
42153
+ story.data.isDeleted !== true);
42154
+ // update localLastStoryExpiresAt to be the last story expires
42155
+ if (unsyncedStoriesCache && unsyncedStoriesCache.length > 0) {
42156
+ const lastStoryExpires = unsyncedStoriesCache.reduce((acc, story) => {
42157
+ const expireAt = new Date(story.data.expiresAt || 0);
42158
+ return expireAt > acc ? expireAt : acc;
42159
+ }, new Date(0));
42160
+ pushToCache(["story-expire" /* STORY_KEY_CACHE.EXPIRE */, cached.data.targetId], lastStoryExpires.toISOString());
42161
+ }
42162
+ else {
42163
+ // if no unsynced stories, remove last story expires since there is no more story
42164
+ dropFromCache(["story-expire" /* STORY_KEY_CACHE.EXPIRE */, cached.data.targetId]);
42165
+ }
42166
+ return true;
42167
+ }
42168
+ /* ------ Synced story case ------ */
42169
+ const targetCache = (_b = pullFromCache([
42170
+ "storyTarget" /* STORY_KEY_CACHE.STORY_TARGET */,
42171
+ 'get',
42172
+ data.targetId,
42173
+ ])) === null || _b === void 0 ? void 0 : _b.data;
42174
+ const syncedStoriesCache = (_c = queryCache([
42175
+ "story" /* STORY_KEY_CACHE.STORY */,
42176
+ 'get',
42177
+ ])) === null || _c === void 0 ? void 0 : _c.filter(story => story.data.syncState === "synced" /* Amity.SyncState.Synced */ &&
42178
+ story.data.targetId === data.targetId &&
42179
+ story.data.isDeleted !== true);
42180
+ let lastStoryExpiresAt;
42181
+ if (syncedStoriesCache && syncedStoriesCache.length > 0) {
42182
+ lastStoryExpiresAt = syncedStoriesCache === null || syncedStoriesCache === void 0 ? void 0 : syncedStoriesCache.reduce((acc, story) => {
42183
+ const expireAt = new Date(story.data.expiresAt || 0);
42184
+ return expireAt > acc ? expireAt : acc;
42185
+ }, new Date(0));
42186
+ }
42187
+ // update lastStoryExpiresAt from lastest synced story
42188
+ if (targetCache) {
42189
+ pushToCache(["storyTarget" /* STORY_KEY_CACHE.STORY_TARGET */, 'get', data.targetId], Object.assign(Object.assign({}, targetCache), { lastStoryExpiresAt }));
42190
+ }
42099
42191
  }
42100
42192
  }
42101
42193
  const response = await client.http.delete(`/api/v4/stories/${storyId}`, {
@@ -42408,6 +42500,7 @@ const getActiveStoriesByTarget = (params, callback) => {
42408
42500
  });
42409
42501
  };
42410
42502
  const processNewData = (result, event, initial = false, loading = false, error = false) => {
42503
+ var _a;
42411
42504
  const cached = pullFromCache(cacheKey);
42412
42505
  const data = {
42413
42506
  loading,
@@ -42426,6 +42519,14 @@ const getActiveStoriesByTarget = (params, callback) => {
42426
42519
  : [...new Set([...data.data, ...result.map(getResolver('story'))])];
42427
42520
  }
42428
42521
  }
42522
+ const unSyncedStories = (_a = queryCache(["story" /* STORY_KEY_CACHE.STORY */, 'get'])) === null || _a === void 0 ? void 0 : _a.filter(story => story.data.targetId === params.targetId &&
42523
+ story.data.syncState !== "synced" /* Amity.SyncState.Synced */).map(story => getResolver('story')(story.data));
42524
+ if (unSyncedStories && (unSyncedStories === null || unSyncedStories === void 0 ? void 0 : unSyncedStories.length) > 0) {
42525
+ unSyncedStories.forEach(referenceId => {
42526
+ if (!data.data.includes(referenceId))
42527
+ data.data.push(referenceId);
42528
+ });
42529
+ }
42429
42530
  pushToCache(cacheKey, data.data);
42430
42531
  responder(data);
42431
42532
  };
@@ -42857,13 +42958,23 @@ class GlobalStoryLiveCollectionController extends LiveCollectionController {
42857
42958
  const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
42858
42959
  if (!collection)
42859
42960
  return;
42860
- let data = collection.data
42961
+ const targetIds = collection.data;
42962
+ const cachedTargets = queryCache(["storyTarget" /* STORY_KEY_CACHE.STORY_TARGET */]);
42963
+ if (cachedTargets && (cachedTargets === null || cachedTargets === void 0 ? void 0 : cachedTargets.length) > 0) {
42964
+ cachedTargets === null || cachedTargets === void 0 ? void 0 : cachedTargets.forEach(({ key }) => {
42965
+ if (!collection.data.includes(key[2]))
42966
+ targetIds.push(key[2]);
42967
+ });
42968
+ }
42969
+ let data = targetIds
42861
42970
  .map(targetId => pullFromCache(["storyTarget" /* STORY_KEY_CACHE.STORY_TARGET */, 'get', targetId]))
42862
42971
  .filter(Boolean)
42863
42972
  .map(storyTarget => LinkedObject.storyTarget(storyTarget.data));
42864
42973
  if (!this.shouldNotify(data) && origin === 'event')
42865
42974
  return;
42866
42975
  data = this.applyFilter(data)
42976
+ // exclude story targets with invalid stories
42977
+ .filter(({ localSortingDate }) => !!localSortingDate)
42867
42978
  // Remove internal fields
42868
42979
  .map((_a) => {
42869
42980
  var rest = __rest(_a, ["localFilter", "localLastExpires", "localLastSeen", "localSortingDate"]);