@amityco/ts-sdk 7.5.4-d9c7b36.0 → 7.5.4-ec2fdd4.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 (33) hide show
  1. package/dist/@types/domains/content.d.ts +5 -2
  2. package/dist/@types/domains/content.d.ts.map +1 -1
  3. package/dist/@types/domains/pinnedPost.d.ts +1 -1
  4. package/dist/@types/domains/pinnedPost.d.ts.map +1 -1
  5. package/dist/@types/index.d.ts +2 -0
  6. package/dist/@types/index.d.ts.map +1 -1
  7. package/dist/client/api/index.d.ts +1 -0
  8. package/dist/client/api/index.d.ts.map +1 -1
  9. package/dist/client/api/resumeSession.d.ts +32 -0
  10. package/dist/client/api/resumeSession.d.ts.map +1 -0
  11. package/dist/client/api/tests/resumeSession.test.d.ts +2 -0
  12. package/dist/client/api/tests/resumeSession.test.d.ts.map +1 -0
  13. package/dist/communityRepository/api/deleteCommunity.d.ts.map +1 -1
  14. package/dist/communityRepository/api/getCommunity.d.ts +2 -2
  15. package/dist/communityRepository/api/getCommunity.d.ts.map +1 -1
  16. package/dist/communityRepository/observers/getCommunity.d.ts.map +1 -1
  17. package/dist/communityRepository/observers/getJoinRequests/JoinRequestsPaginationController.d.ts.map +1 -1
  18. package/dist/index.cjs.js +195 -18
  19. package/dist/index.esm.js +194 -18
  20. package/dist/index.umd.js +4 -4
  21. package/dist/messagePreview/utils/getSubChannelMessagePreviewWithUser.d.ts +1 -1
  22. package/dist/utils/tests/dummy/comment.d.ts +1 -1
  23. package/package.json +2 -1
  24. package/src/@types/domains/content.ts +15 -1
  25. package/src/@types/domains/pinnedPost.ts +1 -1
  26. package/src/@types/index.ts +2 -0
  27. package/src/client/api/index.ts +1 -0
  28. package/src/client/api/resumeSession.ts +282 -0
  29. package/src/client/api/tests/resumeSession.test.ts +173 -0
  30. package/src/communityRepository/api/deleteCommunity.ts +2 -1
  31. package/src/communityRepository/api/getCommunity.ts +5 -7
  32. package/src/communityRepository/observers/getCommunity.ts +24 -12
  33. package/src/communityRepository/observers/getJoinRequests/JoinRequestsPaginationController.ts +1 -2
package/dist/index.esm.js CHANGED
@@ -6,6 +6,7 @@ import HttpAgent, { HttpsAgent } from 'agentkeepalive';
6
6
  import io from 'socket.io-client';
7
7
  import uuid$1 from 'react-native-uuid';
8
8
  import hash from 'object-hash';
9
+ import jwtDecode from 'jwt-decode';
9
10
  import Hls from 'hls.js';
10
11
 
11
12
  var MembershipAcceptanceTypeEnum;
@@ -23817,7 +23818,7 @@ class PaginationController {
23817
23818
  */
23818
23819
  class JoinRequestsPaginationController extends PaginationController {
23819
23820
  async getRequest(queryParams, token) {
23820
- const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, communityId } = queryParams, params = __rest(queryParams, ["limit", "communityId"]);
23821
+ const { limit = 20, communityId } = queryParams, params = __rest(queryParams, ["limit", "communityId"]);
23821
23822
  const options = token ? { token } : { limit };
23822
23823
  const { data: queryResponse } = await this.http.get(`/api/v4/communities/${communityId}/join`, {
23823
23824
  params: Object.assign(Object.assign({}, params), { options }),
@@ -25993,8 +25994,8 @@ const removeChannelMarkerCache = (channel) => {
25993
25994
  * than the one already connected, in which case the existing subscriptions need
25994
25995
  * to be cleared
25995
25996
  */
25996
- let subscriptions = [];
25997
- async function runMqtt() {
25997
+ let subscriptions$1 = [];
25998
+ async function runMqtt$1() {
25998
25999
  await modifyMqttConnection();
25999
26000
  }
26000
26001
  /* begin_public_function
@@ -26029,8 +26030,8 @@ const login = async (params, sessionHandler, config) => {
26029
26030
  if (client.userId && client.userId !== params.userId) {
26030
26031
  await logout();
26031
26032
  // Remove subscription to ban and delete
26032
- subscriptions.forEach(fn => fn());
26033
- subscriptions = [];
26033
+ subscriptions$1.forEach(fn => fn());
26034
+ subscriptions$1 = [];
26034
26035
  }
26035
26036
  // default values
26036
26037
  const defaultDeviceId = await getDeviceId();
@@ -26076,40 +26077,40 @@ const login = async (params, sessionHandler, config) => {
26076
26077
  throw error;
26077
26078
  }
26078
26079
  if ((config === null || config === void 0 ? void 0 : config.disableRTE) !== true) {
26079
- runMqtt();
26080
+ runMqtt$1();
26080
26081
  }
26081
26082
  await initializeMessagePreviewSetting();
26082
- if (subscriptions.length === 0) {
26083
- subscriptions.push(
26083
+ if (subscriptions$1.length === 0) {
26084
+ subscriptions$1.push(
26084
26085
  // GLOBAL_BAN
26085
26086
  onClientBanned((_) => {
26086
26087
  terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
26087
- subscriptions.forEach(fn => fn());
26088
+ subscriptions$1.forEach(fn => fn());
26088
26089
  unsubWatcher();
26089
26090
  }), onTokenTerminated(_ => {
26090
26091
  terminateClient();
26091
- subscriptions.forEach(fn => fn());
26092
+ subscriptions$1.forEach(fn => fn());
26092
26093
  unsubWatcher();
26093
26094
  }), onUserDeleted$2((user) => {
26094
26095
  if (user.userId === client.userId) {
26095
26096
  terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
26096
- subscriptions.forEach(fn => fn());
26097
+ subscriptions$1.forEach(fn => fn());
26097
26098
  unsubWatcher();
26098
26099
  }
26099
26100
  }), onTokenExpired(state => {
26100
26101
  SessionWatcher$1.getInstance().setSessionState(state);
26101
26102
  logout();
26102
- subscriptions.forEach(fn => fn());
26103
+ subscriptions$1.forEach(fn => fn());
26103
26104
  }),
26104
26105
  // NOTE: This is a temporary solution to handle the channel marker when the user is forced to leave
26105
26106
  // the channel because currently backend can't handle this, so every time a user is banned from
26106
26107
  // a channel or the channel is deleted the channel's unread count will not be reset to zero
26107
26108
  onChannelDeleted(removeChannelMarkerCache), onChannelMemberBanned(removeChannelMarkerCache), markReadEngineOnLoginHandler(), analyticsEngineOnLoginHandler(), objectResolverEngineOnLoginHandler());
26108
26109
  if (client.useLegacyUnreadCount) {
26109
- subscriptions.push(readReceiptSyncEngineOnLoginHandler());
26110
+ subscriptions$1.push(readReceiptSyncEngineOnLoginHandler());
26110
26111
  }
26111
26112
  else
26112
- subscriptions.push(legacyReadReceiptSyncEngineOnLoginHandler());
26113
+ subscriptions$1.push(legacyReadReceiptSyncEngineOnLoginHandler());
26113
26114
  }
26114
26115
  return true;
26115
26116
  };
@@ -27034,6 +27035,176 @@ const secureLogout = async () => {
27034
27035
  };
27035
27036
  /* end_public_function */
27036
27037
 
27038
+ /*
27039
+ * declared earlier to accomodate case when logging in with a different user
27040
+ * than the one already connected, in which case the existing subscriptions need
27041
+ * to be cleared
27042
+ */
27043
+ let subscriptions = [];
27044
+ async function runMqtt() {
27045
+ await modifyMqttConnection();
27046
+ }
27047
+ const isSameUserId = (token) => {
27048
+ var _a;
27049
+ const client = getActiveClient();
27050
+ const decoded = jwtDecode(token);
27051
+ return ((_a = decoded === null || decoded === void 0 ? void 0 : decoded.user) === null || _a === void 0 ? void 0 : _a.publicUserId) === client.userId;
27052
+ };
27053
+ const validateAccessToken = async ({ token, userId }) => {
27054
+ const client = getActiveClient();
27055
+ // begin establishing session
27056
+ setSessionState("establishing" /* Amity.SessionStates.ESTABLISHING */);
27057
+ const { data: { users }, } = await client.http.get(`/api/v3/users/${userId}`, {
27058
+ headers: {
27059
+ Authorization: `Bearer ${token.accessToken}`,
27060
+ },
27061
+ });
27062
+ const user = users.find((u) => u.userId === userId);
27063
+ client.http.defaults.headers.common.Authorization = `Bearer ${token.accessToken}`;
27064
+ client.http.defaults.metadata = {
27065
+ tokenExpiry: token.expiresAt,
27066
+ isGlobalBanned: false,
27067
+ isUserDeleted: false,
27068
+ };
27069
+ client.upload.defaults.headers.common.Authorization = `Bearer ${token.accessToken}`;
27070
+ client.upload.defaults.metadata = {
27071
+ tokenExpiry: token.expiresAt,
27072
+ isGlobalBanned: false,
27073
+ isUserDeleted: false,
27074
+ };
27075
+ // manually setup the token for ws transport
27076
+ if (client.ws)
27077
+ client.ws.io.opts.query = { token: token.accessToken };
27078
+ client.token = token;
27079
+ setSessionState("established" /* Amity.SessionStates.ESTABLISHED */);
27080
+ return user;
27081
+ };
27082
+ /* begin_public_function
27083
+ id: client.resumeSession
27084
+ */
27085
+ /**
27086
+ * ```js
27087
+ * import { resumeSession } from '@amityco/ts-sdk/client/api'
27088
+ * const success = await resumeSession({
27089
+ * userId: 'XYZ123456789',
27090
+ * token: { accessToken: 'abc123', issuedAt: '2023-01-01T00:00:00Z', expiresAt: '2023-01-02T00:00:00Z' }
27091
+ * })
27092
+ * ```
27093
+ *
27094
+ * Connects an {@link Amity.Client} instance to ASC servers using an existing access token
27095
+ *
27096
+ * @param params the connect parameters
27097
+ * @param params.userId the user ID for the current session
27098
+ * @param params.token the existing access token with its metadata
27099
+ * @param sessionHandler the session handler for token renewal
27100
+ * @param config optional configuration
27101
+ * @returns a success boolean if connected
27102
+ *
27103
+ * @category Client API
27104
+ * @async
27105
+ */
27106
+ const resumeSession = async (params, sessionHandler, config) => {
27107
+ var _a;
27108
+ const client = getActiveClient();
27109
+ let unsubWatcher;
27110
+ client.log('client/api/resumeSession', Object.assign({ apiKey: client.apiKey, sessionState: client.sessionState }, params));
27111
+ // Handle existing connected user
27112
+ if (client.userId) {
27113
+ if (client.userId === params.userId && isSameUserId(params.token.accessToken)) {
27114
+ // Clear connections and listeners but preserve cache
27115
+ if (client.mqtt && client.mqtt.connected) {
27116
+ client.mqtt.disconnect();
27117
+ }
27118
+ if (client.ws && client.ws.connected) {
27119
+ client.ws.disconnect();
27120
+ }
27121
+ // Clear existing subscriptions
27122
+ subscriptions.forEach(fn => fn());
27123
+ subscriptions = [];
27124
+ }
27125
+ else {
27126
+ // Different user - do full logout
27127
+ await logout();
27128
+ // Remove subscription to ban and delete
27129
+ subscriptions.forEach(fn => fn());
27130
+ subscriptions = [];
27131
+ }
27132
+ }
27133
+ try {
27134
+ const user = await validateAccessToken(params);
27135
+ if (user == null) {
27136
+ throw new ASCError(`${params.userId} has not been found`, 800000 /* Amity.ClientError.UNKNOWN_ERROR */, "error" /* Amity.ErrorLevel.ERROR */);
27137
+ }
27138
+ if (user.isDeleted) {
27139
+ terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
27140
+ return false;
27141
+ }
27142
+ if (user.isGlobalBanned) {
27143
+ terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
27144
+ return false;
27145
+ }
27146
+ // FIXME: events are duplicated if connectClient is called few times without disconnectClient
27147
+ // wire websocket events to our event emitter
27148
+ proxyWebsocketEvents(client.ws, client.emitter);
27149
+ (_a = client.ws) === null || _a === void 0 ? void 0 : _a.open();
27150
+ client.userId = user.userId;
27151
+ client.sessionHandler = sessionHandler;
27152
+ /*
27153
+ * Cannot push to subscriptions as watcher needs to continue working even if
27154
+ * token expires
27155
+ */
27156
+ unsubWatcher = client.accessTokenExpiryWatcher(sessionHandler);
27157
+ setActiveUser(user);
27158
+ }
27159
+ catch (error) {
27160
+ /*
27161
+ * if getting token failed session state reverts to initial state when app
27162
+ * is first launched
27163
+ */
27164
+ SessionWatcher$1.getInstance().setSessionState("notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */);
27165
+ // pass error down tree so the calling function handle it
27166
+ throw error;
27167
+ }
27168
+ if ((config === null || config === void 0 ? void 0 : config.disableRTE) !== true) {
27169
+ runMqtt();
27170
+ }
27171
+ await initializeMessagePreviewSetting();
27172
+ if (subscriptions.length === 0) {
27173
+ subscriptions.push(
27174
+ // GLOBAL_BAN
27175
+ onClientBanned((_) => {
27176
+ terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
27177
+ subscriptions.forEach(fn => fn());
27178
+ unsubWatcher();
27179
+ }), onTokenTerminated(_ => {
27180
+ terminateClient();
27181
+ subscriptions.forEach(fn => fn());
27182
+ unsubWatcher();
27183
+ }), onUserDeleted$2((user) => {
27184
+ if (user.userId === client.userId) {
27185
+ terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
27186
+ subscriptions.forEach(fn => fn());
27187
+ unsubWatcher();
27188
+ }
27189
+ }), onTokenExpired(state => {
27190
+ SessionWatcher$1.getInstance().setSessionState(state);
27191
+ logout();
27192
+ subscriptions.forEach(fn => fn());
27193
+ }),
27194
+ // NOTE: This is a temporary solution to handle the channel marker when the user is forced to leave
27195
+ // the channel because currently backend can't handle this, so every time a user is banned from
27196
+ // a channel or the channel is deleted the channel's unread count will not be reset to zero
27197
+ onChannelDeleted(removeChannelMarkerCache), onChannelMemberBanned(removeChannelMarkerCache), markReadEngineOnLoginHandler(), analyticsEngineOnLoginHandler(), objectResolverEngineOnLoginHandler());
27198
+ if (client.useLegacyUnreadCount) {
27199
+ subscriptions.push(readReceiptSyncEngineOnLoginHandler());
27200
+ }
27201
+ else
27202
+ subscriptions.push(legacyReadReceiptSyncEngineOnLoginHandler());
27203
+ }
27204
+ return true;
27205
+ };
27206
+ /* end_public_function */
27207
+
27037
27208
  /**
27038
27209
  * ```js
27039
27210
  * import { isConnected } from '@amityco/ts-sdk'
@@ -27439,6 +27610,7 @@ var index$n = /*#__PURE__*/Object.freeze({
27439
27610
  login: login,
27440
27611
  logout: logout,
27441
27612
  secureLogout: secureLogout,
27613
+ resumeSession: resumeSession,
27442
27614
  isConnected: isConnected,
27443
27615
  getFeedSettings: getFeedSettings,
27444
27616
  renewal: renewal,
@@ -35700,7 +35872,7 @@ const getCommunity$1 = async (communityId, type, includeDiscoverablePrivateCommu
35700
35872
  }
35701
35873
  const { communities } = data;
35702
35874
  return {
35703
- data: LinkedObject.community(communities.find(community => community.communityId === communityId)),
35875
+ data: communities.find(community => community.communityId === communityId),
35704
35876
  cachedAt,
35705
35877
  };
35706
35878
  };
@@ -35726,7 +35898,7 @@ getCommunity$1.locally = (communityId) => {
35726
35898
  if (!cached)
35727
35899
  return;
35728
35900
  return {
35729
- data: LinkedObject.community(cached.data),
35901
+ data: cached.data,
35730
35902
  cachedAt: cached.cachedAt,
35731
35903
  };
35732
35904
  };
@@ -35762,7 +35934,7 @@ const deleteCommunity = async (communityId) => {
35762
35934
  files: [],
35763
35935
  users: [],
35764
35936
  });
35765
- return deleted.data;
35937
+ return LinkedObject.community(deleted.data);
35766
35938
  };
35767
35939
  /* end_public_function */
35768
35940
 
@@ -37272,7 +37444,11 @@ const getCommunity = (communityId, callback) => {
37272
37444
  onCommunityUserUnbanned,
37273
37445
  onCommunityUserChanged,
37274
37446
  convertEventPayload(onLocalCommunityJoin, 'targetId', 'community'),
37275
- ]);
37447
+ ], {
37448
+ callbackDataSelector: (data) => {
37449
+ return LinkedObject.community(data);
37450
+ },
37451
+ });
37276
37452
  };
37277
37453
  /* end_public_function */
37278
37454