@amityco/ts-sdk 0.0.1-beta.5 → 0.0.1-beta.9
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.
- package/dist/@types/core/events.d.ts +63 -17
- package/dist/@types/core/events.d.ts.map +1 -1
- package/dist/@types/core/transport.d.ts +15 -0
- package/dist/@types/core/transport.d.ts.map +1 -1
- package/dist/@types/domains/client.d.ts +4 -0
- package/dist/@types/domains/client.d.ts.map +1 -1
- package/dist/@types/domains/comment.d.ts +1 -1
- package/dist/@types/domains/comment.d.ts.map +1 -1
- package/dist/@types/domains/community.d.ts +1 -1
- package/dist/@types/domains/community.d.ts.map +1 -1
- package/dist/@types/domains/group.d.ts +2 -2
- package/dist/@types/domains/group.d.ts.map +1 -1
- package/dist/@types/domains/partials.d.ts +3 -0
- package/dist/@types/domains/partials.d.ts.map +1 -1
- package/dist/@types/domains/post.d.ts +1 -1
- package/dist/@types/domains/post.d.ts.map +1 -1
- package/dist/@types/domains/user.d.ts +2 -1
- package/dist/@types/domains/user.d.ts.map +1 -1
- package/dist/client/api/connectClient.d.ts.map +1 -1
- package/dist/client/api/createClient.d.ts +2 -1
- package/dist/client/api/createClient.d.ts.map +1 -1
- package/dist/client/api/disconnectClient.d.ts.map +1 -1
- package/dist/client/api/getToken.d.ts +3 -1
- package/dist/client/api/getToken.d.ts.map +1 -1
- package/dist/comment/api/deleteComment.d.ts +37 -1
- package/dist/comment/api/deleteComment.d.ts.map +1 -1
- package/dist/comment/api/getComment.d.ts.map +1 -1
- package/dist/comment/api/queryComments.d.ts +1 -1
- package/dist/comment/events/index.d.ts +4 -0
- package/dist/comment/events/index.d.ts.map +1 -1
- package/dist/comment/events/onCommentCreated.d.ts +1 -1
- package/dist/comment/events/onCommentCreated.d.ts.map +1 -1
- package/dist/comment/events/onCommentDeleted.d.ts +1 -1
- package/dist/comment/events/onCommentDeleted.d.ts.map +1 -1
- package/dist/comment/events/onCommentFlagged.d.ts +17 -0
- package/dist/comment/events/onCommentFlagged.d.ts.map +1 -0
- package/dist/comment/events/onCommentReactionAdded.d.ts +17 -0
- package/dist/comment/events/onCommentReactionAdded.d.ts.map +1 -0
- package/dist/comment/events/onCommentReactionRemoved.d.ts +17 -0
- package/dist/comment/events/onCommentReactionRemoved.d.ts.map +1 -0
- package/dist/comment/events/onCommentUnflagged.d.ts +17 -0
- package/dist/comment/events/onCommentUnflagged.d.ts.map +1 -0
- package/dist/comment/events/onCommentUpdated.d.ts +1 -1
- package/dist/comment/events/onCommentUpdated.d.ts.map +1 -1
- package/dist/comment/events/utils.d.ts +2 -0
- package/dist/comment/events/utils.d.ts.map +1 -0
- package/dist/comment/observers/observeComment.d.ts +1 -1
- package/dist/comment/observers/observeComment.d.ts.map +1 -1
- package/dist/comment/observers/observeComments.d.ts +5 -1
- package/dist/comment/observers/observeComments.d.ts.map +1 -1
- package/dist/community/events/index.d.ts +4 -0
- package/dist/community/events/index.d.ts.map +1 -1
- package/dist/community/events/onCommunityCreated.d.ts +2 -2
- package/dist/community/events/onCommunityCreated.d.ts.map +1 -1
- package/dist/community/events/onCommunityDeleted.d.ts +2 -2
- package/dist/community/events/onCommunityDeleted.d.ts.map +1 -1
- package/dist/community/events/onCommunityJoined.d.ts +3 -3
- package/dist/community/events/onCommunityJoined.d.ts.map +1 -1
- package/dist/community/events/onCommunityLeft.d.ts +3 -3
- package/dist/community/events/onCommunityLeft.d.ts.map +1 -1
- package/dist/community/events/onCommunityUpdated.d.ts +2 -2
- package/dist/community/events/onCommunityUpdated.d.ts.map +1 -1
- package/dist/community/events/onCommunityUserAdded.d.ts +17 -0
- package/dist/community/events/onCommunityUserAdded.d.ts.map +1 -0
- package/dist/community/events/onCommunityUserBanned.d.ts +17 -0
- package/dist/community/events/onCommunityUserBanned.d.ts.map +1 -0
- package/dist/community/events/onCommunityUserRemoved.d.ts +17 -0
- package/dist/community/events/onCommunityUserRemoved.d.ts.map +1 -0
- package/dist/community/events/onCommunityUserUnbanned.d.ts +17 -0
- package/dist/community/events/onCommunityUserUnbanned.d.ts.map +1 -0
- package/dist/community/events/utils.d.ts +3 -0
- package/dist/community/events/utils.d.ts.map +1 -0
- package/dist/community/observers/observeCommunity.d.ts +1 -1
- package/dist/community/observers/observeCommunity.d.ts.map +1 -1
- package/dist/core/cache/ingest.d.ts.map +1 -1
- package/dist/core/events.d.ts +4 -2
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/model/index.d.ts +1 -0
- package/dist/core/model/index.d.ts.map +1 -1
- package/dist/core/model/isMatch.d.ts +3 -0
- package/dist/core/model/isMatch.d.ts.map +1 -0
- package/dist/core/network.d.ts +11 -0
- package/dist/core/network.d.ts.map +1 -0
- package/dist/core/query/paging.d.ts.map +1 -1
- package/dist/core/subscription.d.ts +14 -0
- package/dist/core/subscription.d.ts.map +1 -0
- package/dist/core/transports/http.d.ts +11 -0
- package/dist/core/transports/http.d.ts.map +1 -0
- package/dist/core/transports/index.d.ts +4 -0
- package/dist/core/transports/index.d.ts.map +1 -0
- package/dist/core/transports/mqtt.d.ts +17 -0
- package/dist/core/transports/mqtt.d.ts.map +1 -0
- package/dist/core/transports/utils.d.ts +13 -0
- package/dist/core/transports/utils.d.ts.map +1 -0
- package/dist/core/transports/ws.d.ts +27 -0
- package/dist/core/transports/ws.d.ts.map +1 -0
- package/dist/feed/api/queryGlobalFeed.d.ts +1 -1
- package/dist/file/api/deleteFile.d.ts +3 -1
- package/dist/file/api/deleteFile.d.ts.map +1 -1
- package/dist/index.cjs.js +1292 -526
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +1268 -527
- package/dist/index.umd.js +19 -5144
- package/dist/post/api/deletePost.d.ts +40 -1
- package/dist/post/api/deletePost.d.ts.map +1 -1
- package/dist/post/api/getPost.d.ts.map +1 -1
- package/dist/post/events/index.d.ts +6 -0
- package/dist/post/events/index.d.ts.map +1 -1
- package/dist/post/events/onPostApproved.d.ts +17 -0
- package/dist/post/events/onPostApproved.d.ts.map +1 -0
- package/dist/post/events/onPostCreated.d.ts +1 -1
- package/dist/post/events/onPostCreated.d.ts.map +1 -1
- package/dist/post/events/onPostDeclined.d.ts +17 -0
- package/dist/post/events/onPostDeclined.d.ts.map +1 -0
- package/dist/post/events/onPostDeleted.d.ts +1 -1
- package/dist/post/events/onPostDeleted.d.ts.map +1 -1
- package/dist/post/events/onPostFlagged.d.ts +17 -0
- package/dist/post/events/onPostFlagged.d.ts.map +1 -0
- package/dist/post/events/onPostReactionAdded.d.ts +17 -0
- package/dist/post/events/onPostReactionAdded.d.ts.map +1 -0
- package/dist/post/events/onPostReactionRemoved.d.ts +17 -0
- package/dist/post/events/onPostReactionRemoved.d.ts.map +1 -0
- package/dist/post/events/onPostUnflagged.d.ts +17 -0
- package/dist/post/events/onPostUnflagged.d.ts.map +1 -0
- package/dist/post/events/onPostUpdated.d.ts +1 -1
- package/dist/post/events/onPostUpdated.d.ts.map +1 -1
- package/dist/post/events/utils.d.ts +2 -0
- package/dist/post/events/utils.d.ts.map +1 -0
- package/dist/post/observers/observePost.d.ts +1 -1
- package/dist/post/observers/observePost.d.ts.map +1 -1
- package/dist/post/observers/observePosts.d.ts +1 -1
- package/dist/post/observers/observePosts.d.ts.map +1 -1
- package/dist/reaction/events/onReactionAdded.d.ts +4 -2
- package/dist/reaction/events/onReactionAdded.d.ts.map +1 -1
- package/dist/reaction/events/onReactionRemoved.d.ts +4 -2
- package/dist/reaction/events/onReactionRemoved.d.ts.map +1 -1
- package/dist/reaction/utils/index.d.ts +1 -0
- package/dist/reaction/utils/index.d.ts.map +1 -1
- package/dist/reaction/utils/preparePayloadForCache.d.ts +3 -0
- package/dist/reaction/utils/preparePayloadForCache.d.ts.map +1 -0
- package/dist/report/api/createReport.d.ts.map +1 -1
- package/dist/report/api/deleteReport.d.ts.map +1 -1
- package/dist/user/events/index.d.ts +3 -0
- package/dist/user/events/index.d.ts.map +1 -1
- package/dist/user/events/onUserFlagCleared.d.ts +17 -0
- package/dist/user/events/onUserFlagCleared.d.ts.map +1 -0
- package/dist/user/events/onUserFlagged.d.ts +17 -0
- package/dist/user/events/onUserFlagged.d.ts.map +1 -0
- package/dist/user/events/onUserUnflagged.d.ts +17 -0
- package/dist/user/events/onUserUnflagged.d.ts.map +1 -0
- package/dist/user/events/onUserUpdated.d.ts +1 -1
- package/dist/user/events/onUserUpdated.d.ts.map +1 -1
- package/dist/user/events/utils.d.ts +2 -0
- package/dist/user/events/utils.d.ts.map +1 -0
- package/dist/user/observers/observeUser.d.ts +1 -1
- package/dist/user/observers/observeUser.d.ts.map +1 -1
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/package.json +7 -2
- package/rollup.config.js +63 -23
- package/src/@types/core/events.ts +64 -23
- package/src/@types/core/transport.ts +14 -0
- package/src/@types/domains/client.ts +5 -0
- package/src/@types/domains/comment.ts +1 -0
- package/src/@types/domains/community.ts +1 -0
- package/src/@types/domains/group.ts +2 -2
- package/src/@types/domains/partials.ts +4 -0
- package/src/@types/domains/post.ts +1 -0
- package/src/@types/domains/user.ts +2 -0
- package/src/client/api/connectClient.ts +12 -2
- package/src/client/api/createClient.ts +23 -15
- package/src/client/api/disconnectClient.ts +5 -0
- package/src/client/api/getToken.ts +3 -2
- package/src/client/events/onConnectionError.ts +1 -1
- package/src/comment/api/createComment.ts +2 -2
- package/src/comment/api/deleteComment.ts +25 -5
- package/src/comment/api/getComment.ts +17 -5
- package/src/comment/api/queryComments.ts +9 -8
- package/src/comment/api/updateComment.ts +1 -1
- package/src/comment/events/index.ts +6 -0
- package/src/comment/events/onCommentCreated.ts +4 -22
- package/src/comment/events/onCommentDeleted.ts +4 -22
- package/src/comment/events/onCommentFlagged.ts +21 -0
- package/src/comment/events/onCommentReactionAdded.ts +52 -0
- package/src/comment/events/onCommentReactionRemoved.ts +52 -0
- package/src/comment/events/onCommentUnflagged.ts +21 -0
- package/src/comment/events/onCommentUpdated.ts +4 -22
- package/src/comment/events/utils.ts +34 -0
- package/src/comment/observers/observeComment.ts +21 -7
- package/src/comment/observers/observeComments.ts +11 -3
- package/src/community/api/addCommunityMembers.ts +1 -1
- package/src/community/api/createCommunity.ts +1 -1
- package/src/community/api/deleteCommunity.ts +1 -1
- package/src/community/api/joinCommunity.ts +1 -1
- package/src/community/api/leaveCommunity.ts +1 -1
- package/src/community/api/removeCommunityMembers.ts +1 -1
- package/src/community/api/updateCommunity.ts +1 -1
- package/src/community/events/index.ts +5 -0
- package/src/community/events/onCommunityCreated.ts +5 -23
- package/src/community/events/onCommunityDeleted.ts +5 -23
- package/src/community/events/onCommunityJoined.ts +5 -23
- package/src/community/events/onCommunityLeft.ts +5 -23
- package/src/community/events/onCommunityUpdated.ts +5 -23
- package/src/community/events/onCommunityUserAdded.ts +19 -0
- package/src/community/events/onCommunityUserBanned.ts +19 -0
- package/src/community/events/onCommunityUserRemoved.ts +19 -0
- package/src/community/events/onCommunityUserUnbanned.ts +19 -0
- package/src/community/events/utils.ts +108 -0
- package/src/community/observers/observeCommunity.ts +23 -9
- package/src/core/cache/ingest.ts +19 -3
- package/src/core/events.ts +45 -4
- package/src/core/model/index.ts +1 -0
- package/src/core/model/isMatch.ts +12 -0
- package/src/core/network.ts +14 -0
- package/src/core/query/paging.ts +4 -0
- package/src/core/subscription.ts +75 -0
- package/src/core/transports/http.ts +63 -0
- package/src/core/transports/index.ts +3 -0
- package/src/core/transports/mqtt.ts +141 -0
- package/src/core/transports/utils.ts +25 -0
- package/src/core/transports/ws.ts +61 -0
- package/src/feed/api/queryGlobalFeed.ts +1 -1
- package/src/file/api/deleteFile.ts +5 -6
- package/src/index.ts +3 -0
- package/src/post/api/createPost.ts +2 -2
- package/src/post/api/deletePost.ts +29 -8
- package/src/post/api/getPost.ts +17 -5
- package/src/post/api/updatePost.ts +1 -1
- package/src/post/events/index.ts +9 -0
- package/src/post/events/onPostApproved.ts +19 -0
- package/src/post/events/onPostCreated.ts +4 -24
- package/src/post/events/onPostDeclined.ts +19 -0
- package/src/post/events/onPostDeleted.ts +4 -24
- package/src/post/events/onPostFlagged.ts +19 -0
- package/src/post/events/onPostReactionAdded.ts +50 -0
- package/src/post/events/onPostReactionRemoved.ts +50 -0
- package/src/post/events/onPostUnflagged.ts +19 -0
- package/src/post/events/onPostUpdated.ts +4 -24
- package/src/post/events/utils.ts +40 -0
- package/src/post/observers/observePost.ts +25 -7
- package/src/post/observers/observePosts.ts +12 -2
- package/src/reaction/events/onReactionAdded.ts +36 -15
- package/src/reaction/events/onReactionRemoved.ts +37 -14
- package/src/reaction/utils/dispatchReactable.ts +2 -2
- package/src/reaction/utils/index.ts +1 -0
- package/src/reaction/utils/preparePayloadForCache.ts +45 -0
- package/src/report/api/createReport.ts +5 -1
- package/src/report/api/deleteReport.ts +5 -1
- package/src/user/events/index.ts +4 -0
- package/src/user/events/onUserFlagCleared.ts +19 -0
- package/src/user/events/onUserFlagged.ts +19 -0
- package/src/user/events/onUserUnflagged.ts +19 -0
- package/src/user/events/onUserUpdated.ts +4 -24
- package/src/user/events/utils.ts +24 -0
- package/src/user/observers/observeUser.ts +11 -3
- package/src/version.ts +2 -0
- package/tsconfig.json +1 -0
- package/dist/core/transports.d.ts +0 -49
- package/dist/core/transports.d.ts.map +0 -1
- package/src/core/transports.ts +0 -151
package/dist/index.cjs.js
CHANGED
|
@@ -2,25 +2,33 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var isMatchWith = require('lodash.ismatchwith');
|
|
5
6
|
var jsBase64 = require('js-base64');
|
|
6
7
|
var nativeIdGenerator = require('react-native-uuid');
|
|
7
8
|
var debug = require('debug');
|
|
8
9
|
var axios = require('axios');
|
|
9
10
|
var HttpAgent = require('agentkeepalive');
|
|
10
11
|
var io = require('socket.io-client');
|
|
12
|
+
var mqtt = require('mqtt');
|
|
11
13
|
var mitt = require('mitt');
|
|
12
14
|
var AsyncStorage = require('@react-native-async-storage/async-storage');
|
|
13
15
|
|
|
14
16
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
17
|
|
|
18
|
+
var isMatchWith__default = /*#__PURE__*/_interopDefaultLegacy(isMatchWith);
|
|
16
19
|
var nativeIdGenerator__default = /*#__PURE__*/_interopDefaultLegacy(nativeIdGenerator);
|
|
17
20
|
var debug__default = /*#__PURE__*/_interopDefaultLegacy(debug);
|
|
18
21
|
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
|
|
19
22
|
var HttpAgent__default = /*#__PURE__*/_interopDefaultLegacy(HttpAgent);
|
|
20
23
|
var io__default = /*#__PURE__*/_interopDefaultLegacy(io);
|
|
24
|
+
var mqtt__default = /*#__PURE__*/_interopDefaultLegacy(mqtt);
|
|
21
25
|
var mitt__default = /*#__PURE__*/_interopDefaultLegacy(mitt);
|
|
22
26
|
var AsyncStorage__default = /*#__PURE__*/_interopDefaultLegacy(AsyncStorage);
|
|
23
27
|
|
|
28
|
+
var _a;
|
|
29
|
+
// used in createClient
|
|
30
|
+
const VERSION$1 = (_a = 'v0.0.1-beta.9-cjs') !== null && _a !== void 0 ? _a : '__dev__';
|
|
31
|
+
|
|
24
32
|
/**
|
|
25
33
|
* Generic ASC error
|
|
26
34
|
* @category Errors
|
|
@@ -286,24 +294,6 @@ const syncInCache = (original, replacement) => {
|
|
|
286
294
|
pushToCache(name, replacement);
|
|
287
295
|
};
|
|
288
296
|
|
|
289
|
-
/**
|
|
290
|
-
* Ingest a payload (v3) into the cache
|
|
291
|
-
*
|
|
292
|
-
* @param payload a "backend v3" payload object
|
|
293
|
-
* @param order the order of the keys to process (in case of need)
|
|
294
|
-
*
|
|
295
|
-
* @category Cache
|
|
296
|
-
*/
|
|
297
|
-
const ingestInCache = (payload = {}, order = Object.keys(payload)) => {
|
|
298
|
-
order
|
|
299
|
-
.forEach(key => {
|
|
300
|
-
const name = storeInterests[key];
|
|
301
|
-
if (!name)
|
|
302
|
-
return;
|
|
303
|
-
pushToCache(name, ...payload[key]);
|
|
304
|
-
});
|
|
305
|
-
};
|
|
306
|
-
|
|
307
297
|
/**
|
|
308
298
|
* Apply partial mutation to objects in the cache
|
|
309
299
|
*
|
|
@@ -335,6 +325,58 @@ const mergeInCache = (name, mutation) => {
|
|
|
335
325
|
mutateCache(name, key, model => (Object.assign(Object.assign({}, model), mutation)));
|
|
336
326
|
};
|
|
337
327
|
|
|
328
|
+
/**
|
|
329
|
+
* converts an array looking collection to an object collection, grouped by localId
|
|
330
|
+
*
|
|
331
|
+
* @param array array of {@link Amity.Syncable}
|
|
332
|
+
* @returns array converted to object, grouped by localId
|
|
333
|
+
*
|
|
334
|
+
* @hidden
|
|
335
|
+
*/
|
|
336
|
+
const toRecord = (array) => (array.reduce((acc, item) => (Object.assign(Object.assign({}, acc), { [item.localId]: item })), {}));
|
|
337
|
+
|
|
338
|
+
/** @hidden */
|
|
339
|
+
const isMatch = (model1, model2) => {
|
|
340
|
+
const customizer = (oldValue, newValue, key) => {
|
|
341
|
+
if (key === 'syncedAt') {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
return isMatchWith__default['default'](model1, model2, customizer);
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Ingest a payload (v3) into the cache
|
|
350
|
+
*
|
|
351
|
+
* @param payload a "backend v3" payload object
|
|
352
|
+
* @param order the order of the keys to process (in case of need)
|
|
353
|
+
*
|
|
354
|
+
* @category Cache
|
|
355
|
+
*/
|
|
356
|
+
const ingestInCache = (payload = {}, order = Object.keys(payload)) => {
|
|
357
|
+
order
|
|
358
|
+
.forEach(key => {
|
|
359
|
+
const name = storeInterests[key];
|
|
360
|
+
if (!name)
|
|
361
|
+
return;
|
|
362
|
+
payload[key].forEach(model => {
|
|
363
|
+
var _a;
|
|
364
|
+
const [cached] = (_a = pullFromCache(name, getResolver(name)(model))) !== null && _a !== void 0 ? _a : [];
|
|
365
|
+
if (cached) {
|
|
366
|
+
// > instead of >= - some updates like reactions do not change updatedAt
|
|
367
|
+
if (cached.updatedAt && cached.updatedAt > model.updatedAt) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
// personal fields like isJoined, myReactions are missed from real time event payload
|
|
371
|
+
mergeInCache(name, model);
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
pushToCache(name, model);
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
};
|
|
379
|
+
|
|
338
380
|
/**
|
|
339
381
|
* Creates a cache instance. The cache instance will handle stores
|
|
340
382
|
* given to it as input, and will be used as the first source of cached data
|
|
@@ -422,6 +464,11 @@ const toToken = (paging, style) => {
|
|
|
422
464
|
first: paging.limit,
|
|
423
465
|
};
|
|
424
466
|
}
|
|
467
|
+
else if (!isNaN(paging === null || paging === void 0 ? void 0 : paging.limit)) {
|
|
468
|
+
payload = {
|
|
469
|
+
last: paging.limit,
|
|
470
|
+
};
|
|
471
|
+
}
|
|
425
472
|
}
|
|
426
473
|
// avoid sending {} as it seems backend flips when we do
|
|
427
474
|
if (!Object.keys(payload).length)
|
|
@@ -760,6 +807,45 @@ const createLogger = (session) => {
|
|
|
760
807
|
return print;
|
|
761
808
|
};
|
|
762
809
|
|
|
810
|
+
const API_ENDPOINT = {
|
|
811
|
+
EU: 'eu',
|
|
812
|
+
SG: 'sg',
|
|
813
|
+
US: 'us',
|
|
814
|
+
};
|
|
815
|
+
var UrlTemplate;
|
|
816
|
+
(function (UrlTemplate) {
|
|
817
|
+
UrlTemplate["HTTP"] = "https://api.{region}.amity.co";
|
|
818
|
+
UrlTemplate["MQTT"] = "wss://sse.{region}.amity.co:443/mqtt";
|
|
819
|
+
})(UrlTemplate || (UrlTemplate = {}));
|
|
820
|
+
const getUrlFromTemplate = (template, region) => {
|
|
821
|
+
return template.replace('{region}', region);
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Unwraps v3 server payload (for error handling and type extraction)
|
|
826
|
+
*
|
|
827
|
+
* @param response A typical "v3" response (related to backend's service strategy)
|
|
828
|
+
*
|
|
829
|
+
* @returns The data returned by the backend
|
|
830
|
+
* @throws An error related to backend's rejection
|
|
831
|
+
*
|
|
832
|
+
* @category Transport
|
|
833
|
+
* @hidden
|
|
834
|
+
*/
|
|
835
|
+
const unwrapPayload = (response) => {
|
|
836
|
+
if (response.status === 'success') {
|
|
837
|
+
return response.data;
|
|
838
|
+
}
|
|
839
|
+
else if (response.status === 'fail') {
|
|
840
|
+
throw new ASCUnknownError(response.code);
|
|
841
|
+
}
|
|
842
|
+
else if (response.status === 'error') {
|
|
843
|
+
throw new ASCApiError(response.message, response.code, "error" /* ERROR */);
|
|
844
|
+
}
|
|
845
|
+
// unjust, but helps force uncast "T | undefined" to allow deconstruction without warnings
|
|
846
|
+
return undefined;
|
|
847
|
+
};
|
|
848
|
+
|
|
763
849
|
/**
|
|
764
850
|
* Axios param serializer (for querystring)
|
|
765
851
|
* @hidden
|
|
@@ -788,7 +874,7 @@ const stringify = (params, prefix) => {
|
|
|
788
874
|
* @category Transport
|
|
789
875
|
* @hidden
|
|
790
876
|
*/
|
|
791
|
-
const createHttpTransport = (endpoint
|
|
877
|
+
const createHttpTransport = (endpoint) => {
|
|
792
878
|
const options = {
|
|
793
879
|
maxSockets: 100,
|
|
794
880
|
maxFreeSockets: 10,
|
|
@@ -809,7 +895,8 @@ const createHttpTransport = (endpoint = 'https://api.amity.co') => {
|
|
|
809
895
|
unwrapPayload(response.data);
|
|
810
896
|
});
|
|
811
897
|
return instance;
|
|
812
|
-
};
|
|
898
|
+
};
|
|
899
|
+
|
|
813
900
|
/**
|
|
814
901
|
* Creates a pre-configured socket.io instance
|
|
815
902
|
*
|
|
@@ -819,42 +906,19 @@ const createHttpTransport = (endpoint = 'https://api.amity.co') => {
|
|
|
819
906
|
* @category Transport
|
|
820
907
|
* @hidden
|
|
821
908
|
*/
|
|
822
|
-
const createWebsocketTransport = (endpoint
|
|
909
|
+
const createWebsocketTransport = (endpoint) => {
|
|
823
910
|
const socket = io__default['default'](endpoint, {
|
|
824
911
|
autoConnect: false,
|
|
825
912
|
reconnectionDelay: 2000,
|
|
826
913
|
transports: ['websocket'],
|
|
827
914
|
});
|
|
915
|
+
// FIXME: disconnectClient removes this listener and client can get global ban event
|
|
828
916
|
socket.on('disconnect', (reason) => {
|
|
829
917
|
if (reason === 'io server disconnect')
|
|
830
918
|
socket.connect();
|
|
831
919
|
});
|
|
832
920
|
return socket;
|
|
833
921
|
};
|
|
834
|
-
/**
|
|
835
|
-
* Unwraps v3 server payload (for error handling and type extraction)
|
|
836
|
-
*
|
|
837
|
-
* @param response A typical "v3" response (related to backend's service strategy)
|
|
838
|
-
*
|
|
839
|
-
* @returns The data returned by the backend
|
|
840
|
-
* @throws An error related to backend's rejection
|
|
841
|
-
*
|
|
842
|
-
* @category Transport
|
|
843
|
-
* @hidden
|
|
844
|
-
*/
|
|
845
|
-
const unwrapPayload = (response) => {
|
|
846
|
-
if (response.status === 'success') {
|
|
847
|
-
return response.data;
|
|
848
|
-
}
|
|
849
|
-
else if (response.status === 'fail') {
|
|
850
|
-
throw new ASCUnknownError(response.code);
|
|
851
|
-
}
|
|
852
|
-
else if (response.status === 'error') {
|
|
853
|
-
throw new ASCApiError(response.message, response.code, "error" /* ERROR */);
|
|
854
|
-
}
|
|
855
|
-
// unjust, but helps force uncast "T | undefined" to allow deconstruction without warnings
|
|
856
|
-
return undefined;
|
|
857
|
-
};
|
|
858
922
|
/**
|
|
859
923
|
* Promisify a websocket event emission - resulting in a synchronous http-like XHR (ws legacy)
|
|
860
924
|
*
|
|
@@ -884,14 +948,131 @@ const synchronousWSCall = async (client, event, data) => {
|
|
|
884
948
|
return value;
|
|
885
949
|
};
|
|
886
950
|
|
|
951
|
+
const QOS_FAILURE_CODE = 128;
|
|
952
|
+
const RETRY_BASE_TIMEOUT = 1000;
|
|
953
|
+
const RETRY_COUNT_MAX = 10;
|
|
954
|
+
var MqttError;
|
|
955
|
+
(function (MqttError) {
|
|
956
|
+
MqttError[MqttError["IDENTIFIER_REJECTED"] = 2] = "IDENTIFIER_REJECTED";
|
|
957
|
+
MqttError[MqttError["BAD_USERNAME_OR_PASSWORD"] = 4] = "BAD_USERNAME_OR_PASSWORD";
|
|
958
|
+
MqttError[MqttError["NOT_AUTHORIZED"] = 5] = "NOT_AUTHORIZED";
|
|
959
|
+
})(MqttError || (MqttError = {}));
|
|
960
|
+
function getMqttOptions(params) {
|
|
961
|
+
return Object.assign({ clean: false, clientId: `mqttjs_ + ${Math.random().toString(16).substr(2, 8)}`, protocolId: 'MQTT', protocolVersion: 4, reconnectPeriod: 0, will: {
|
|
962
|
+
topic: 'WillMsg',
|
|
963
|
+
payload: 'Connection Closed abnormally..!',
|
|
964
|
+
qos: 0,
|
|
965
|
+
retain: false
|
|
966
|
+
} }, params);
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Creates a pre-configured socket.io instance
|
|
970
|
+
*
|
|
971
|
+
* @param endpoint The mqtt server's URL
|
|
972
|
+
* @returns A pre-configured (non-connected) mqtt client instance
|
|
973
|
+
*
|
|
974
|
+
* @category Transport
|
|
975
|
+
* @hidden
|
|
976
|
+
*/
|
|
977
|
+
const createMqttTransport = (endpoint) => {
|
|
978
|
+
let mqttClient;
|
|
979
|
+
let retryCount = 0;
|
|
980
|
+
let allowReconnection = true;
|
|
981
|
+
async function connect(params) {
|
|
982
|
+
if (mqttClient) {
|
|
983
|
+
mqttClient.removeAllListeners();
|
|
984
|
+
mqttClient.end(true);
|
|
985
|
+
}
|
|
986
|
+
mqttClient = mqtt__default['default'].connect(endpoint, getMqttOptions({ username: params.userId, password: params.accessToken }));
|
|
987
|
+
mqttClient.on('connect', () => {
|
|
988
|
+
retryCount = 0;
|
|
989
|
+
allowReconnection = true;
|
|
990
|
+
});
|
|
991
|
+
mqttClient.on('error', (error) => {
|
|
992
|
+
switch (error.code) {
|
|
993
|
+
case 2 /* IDENTIFIER_REJECTED */:
|
|
994
|
+
case 4 /* BAD_USERNAME_OR_PASSWORD */:
|
|
995
|
+
case 5 /* NOT_AUTHORIZED */:
|
|
996
|
+
allowReconnection = false;
|
|
997
|
+
}
|
|
998
|
+
});
|
|
999
|
+
mqttClient.on('close', () => {
|
|
1000
|
+
if (allowReconnection) {
|
|
1001
|
+
reconnect();
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
return new Promise(resolve => mqttClient.once('connect', () => resolve()));
|
|
1005
|
+
}
|
|
1006
|
+
const reconnect = () => {
|
|
1007
|
+
setTimeout(() => {
|
|
1008
|
+
retryCount = retryCount + 1;
|
|
1009
|
+
mqttClient.reconnect();
|
|
1010
|
+
}, RETRY_BASE_TIMEOUT * Math.pow(2, retryCount > RETRY_COUNT_MAX ? RETRY_COUNT_MAX : retryCount));
|
|
1011
|
+
};
|
|
1012
|
+
return {
|
|
1013
|
+
connect,
|
|
1014
|
+
async disconnect() {
|
|
1015
|
+
allowReconnection = false;
|
|
1016
|
+
if (this.connected) {
|
|
1017
|
+
return new Promise(resolve => mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.end(true, undefined, () => resolve()));
|
|
1018
|
+
}
|
|
1019
|
+
},
|
|
1020
|
+
get connected() {
|
|
1021
|
+
return !!(mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.connected);
|
|
1022
|
+
},
|
|
1023
|
+
on(event, handler) {
|
|
1024
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.on(event, handler);
|
|
1025
|
+
},
|
|
1026
|
+
once(event, handler) {
|
|
1027
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.once(event, handler);
|
|
1028
|
+
},
|
|
1029
|
+
off(event, handler) {
|
|
1030
|
+
if (handler !== undefined) {
|
|
1031
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.off(event, handler);
|
|
1032
|
+
}
|
|
1033
|
+
else {
|
|
1034
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.removeAllListeners(event);
|
|
1035
|
+
}
|
|
1036
|
+
},
|
|
1037
|
+
removeAllListeners() {
|
|
1038
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.removeAllListeners();
|
|
1039
|
+
},
|
|
1040
|
+
subscribe(topic, callback) {
|
|
1041
|
+
const callbackWrapper = (error, granted) => {
|
|
1042
|
+
if (error || granted[0].qos === QOS_FAILURE_CODE) {
|
|
1043
|
+
const ascError = error
|
|
1044
|
+
? new ASCError(error.message, 800000 /* UNKNOWN_ERROR */, "error" /* ERROR */)
|
|
1045
|
+
// TODO throw the actual error, once BE can tell us the actual error code
|
|
1046
|
+
: new ASCUnknownError(800000 /* UNKNOWN_ERROR */, "error" /* ERROR */);
|
|
1047
|
+
console.error(`Failed to subscribe to topic ${topic}`, ascError);
|
|
1048
|
+
callback === null || callback === void 0 ? void 0 : callback(ascError);
|
|
1049
|
+
}
|
|
1050
|
+
else {
|
|
1051
|
+
console.log(`Subscribed to topic ${granted[0].topic}`);
|
|
1052
|
+
callback === null || callback === void 0 ? void 0 : callback();
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
if (mqttClient) {
|
|
1056
|
+
mqttClient.subscribe(topic, { qos: 0 }, callbackWrapper);
|
|
1057
|
+
}
|
|
1058
|
+
else {
|
|
1059
|
+
callbackWrapper(new Error('No connection to broker'), []);
|
|
1060
|
+
}
|
|
1061
|
+
return () => mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.unsubscribe(topic);
|
|
1062
|
+
},
|
|
1063
|
+
unsubscribe(topic) {
|
|
1064
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.unsubscribe(topic);
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
};
|
|
1068
|
+
|
|
887
1069
|
// need to sync up the list
|
|
888
|
-
const
|
|
1070
|
+
const WS_EVENTS = [
|
|
889
1071
|
'disconnected',
|
|
890
1072
|
'error',
|
|
891
1073
|
'connect_error',
|
|
892
1074
|
'reconnect_error',
|
|
893
1075
|
'reconnect_failed',
|
|
894
|
-
'v3.user.didUpdate',
|
|
895
1076
|
'v3.channel.didUpdate',
|
|
896
1077
|
'v3.channel.didUpdate',
|
|
897
1078
|
'v3.channel.didJoin',
|
|
@@ -904,6 +1085,15 @@ const EVENTS$1 = [
|
|
|
904
1085
|
'v3.message.didUpdate',
|
|
905
1086
|
'v3.message.didDelete',
|
|
906
1087
|
];
|
|
1088
|
+
const MQTT_EVENTS = [
|
|
1089
|
+
'connect',
|
|
1090
|
+
'message',
|
|
1091
|
+
'disconnect',
|
|
1092
|
+
'error',
|
|
1093
|
+
'close',
|
|
1094
|
+
'end',
|
|
1095
|
+
'reconnect'
|
|
1096
|
+
];
|
|
907
1097
|
/** @hidden */
|
|
908
1098
|
const createEventEmitter = () => {
|
|
909
1099
|
return mitt__default['default']();
|
|
@@ -919,10 +1109,20 @@ const createEventEmitter = () => {
|
|
|
919
1109
|
* @hidden
|
|
920
1110
|
*/
|
|
921
1111
|
const proxyWebsocketEvents = (ws, emitter) => {
|
|
922
|
-
|
|
1112
|
+
WS_EVENTS.forEach(event => {
|
|
923
1113
|
ws.on(event, (param) => emitter.emit(event, param));
|
|
924
1114
|
});
|
|
925
1115
|
};
|
|
1116
|
+
const proxyMqttEvents = (mqttClient, emitter) => {
|
|
1117
|
+
MQTT_EVENTS.forEach(event => {
|
|
1118
|
+
mqttClient.on(event, (...params) => emitter.emit(event, params.length === 1 ? params[0] : params));
|
|
1119
|
+
});
|
|
1120
|
+
// @ts-ignore
|
|
1121
|
+
mqttClient.on('message', (topic, payload) => {
|
|
1122
|
+
const message = JSON.parse(payload.toString());
|
|
1123
|
+
emitter.emit(message.eventType, message.data);
|
|
1124
|
+
});
|
|
1125
|
+
};
|
|
926
1126
|
/**
|
|
927
1127
|
* Standardize the subscription of SSE through web sockets
|
|
928
1128
|
*
|
|
@@ -974,17 +1174,24 @@ const fireEvent = (event, payload) => {
|
|
|
974
1174
|
*
|
|
975
1175
|
* @category Client API
|
|
976
1176
|
**/
|
|
977
|
-
const createClient = (apiKey, apiEndpoint =
|
|
1177
|
+
const createClient = (apiKey, apiEndpoint = API_ENDPOINT.SG, debugSession = 'amity') => {
|
|
978
1178
|
const log = createLogger(debugSession);
|
|
979
1179
|
log('client/api/createClient', {
|
|
980
1180
|
apiKey: apiKey.replace(/.{5}$/g, 'xxxxx'),
|
|
981
1181
|
apiEndpoint,
|
|
982
1182
|
});
|
|
983
|
-
const http = createHttpTransport(apiEndpoint);
|
|
984
|
-
const ws = createWebsocketTransport(apiEndpoint);
|
|
1183
|
+
const http = createHttpTransport(getUrlFromTemplate(UrlTemplate.HTTP, apiEndpoint));
|
|
1184
|
+
const ws = createWebsocketTransport(getUrlFromTemplate(UrlTemplate.HTTP, apiEndpoint));
|
|
1185
|
+
const mqtt = createMqttTransport(getUrlFromTemplate(UrlTemplate.MQTT, apiEndpoint));
|
|
985
1186
|
const emitter = createEventEmitter();
|
|
986
1187
|
const client = {
|
|
987
|
-
|
|
1188
|
+
version: `${VERSION$1}`,
|
|
1189
|
+
apiKey,
|
|
1190
|
+
log,
|
|
1191
|
+
http,
|
|
1192
|
+
ws,
|
|
1193
|
+
mqtt,
|
|
1194
|
+
emitter,
|
|
988
1195
|
use: () => setActiveClient(client),
|
|
989
1196
|
};
|
|
990
1197
|
try {
|
|
@@ -996,6 +1203,38 @@ const createClient = (apiKey, apiEndpoint = 'https://api.amity.co', debugSession
|
|
|
996
1203
|
return client;
|
|
997
1204
|
};
|
|
998
1205
|
|
|
1206
|
+
/**
|
|
1207
|
+
* ```js
|
|
1208
|
+
* import { disconnectClient } from '@amityco/ts-sdk'
|
|
1209
|
+
* const success = await disconnectClient()
|
|
1210
|
+
* ```
|
|
1211
|
+
*
|
|
1212
|
+
* Disconnects an {@link Amity.Client} instance from ASC servers
|
|
1213
|
+
*
|
|
1214
|
+
* @param client The {@link Amity.Client} instance to disconnect
|
|
1215
|
+
* @returns a success boolean if disconnected
|
|
1216
|
+
*
|
|
1217
|
+
* @category Client API
|
|
1218
|
+
* @async
|
|
1219
|
+
*/
|
|
1220
|
+
const disconnectClient = async () => {
|
|
1221
|
+
const client = getActiveClient();
|
|
1222
|
+
client.log('client/api/disconnectClient');
|
|
1223
|
+
await new Promise(resolve => {
|
|
1224
|
+
client.ws.once('disconnect', resolve);
|
|
1225
|
+
client.ws.disconnect();
|
|
1226
|
+
client.mqtt.disconnect();
|
|
1227
|
+
});
|
|
1228
|
+
client.emitter.all.clear();
|
|
1229
|
+
// FIXME: it removes listener in ws.ts, it breaks global ban event
|
|
1230
|
+
client.ws.removeAllListeners();
|
|
1231
|
+
client.mqtt.removeAllListeners();
|
|
1232
|
+
client.userId = undefined;
|
|
1233
|
+
client.user = undefined;
|
|
1234
|
+
client.accessToken = undefined;
|
|
1235
|
+
return !client.ws.connected;
|
|
1236
|
+
};
|
|
1237
|
+
|
|
999
1238
|
// TODO: find a way to uniquely identify the device
|
|
1000
1239
|
// import { getStorage } from './storage'
|
|
1001
1240
|
/** @hidden */
|
|
@@ -1046,11 +1285,89 @@ const getToken = async (params) => {
|
|
|
1046
1285
|
client.log('client/api/getToken', params);
|
|
1047
1286
|
const deviceInfo = await getDeviceInfo();
|
|
1048
1287
|
const { data } = await client.http.post('/v1/device', Object.assign(Object.assign({}, params), { deviceInfo }), {
|
|
1288
|
+
//@ts-ignore
|
|
1049
1289
|
headers: { 'X-API-Key': client.apiKey }
|
|
1050
1290
|
});
|
|
1051
1291
|
return data;
|
|
1052
1292
|
};
|
|
1053
1293
|
|
|
1294
|
+
const EVENTS = [
|
|
1295
|
+
'disconnected',
|
|
1296
|
+
'error',
|
|
1297
|
+
'connect_error',
|
|
1298
|
+
'reconnect_error',
|
|
1299
|
+
'reconnect_failed',
|
|
1300
|
+
];
|
|
1301
|
+
/**
|
|
1302
|
+
* ```js
|
|
1303
|
+
* import { onConnectionError } from '@amityco/ts-sdk'
|
|
1304
|
+
* const dispose = onClientBanned(() => {
|
|
1305
|
+
* // ...
|
|
1306
|
+
* })
|
|
1307
|
+
* ```
|
|
1308
|
+
*
|
|
1309
|
+
* Fired when any {@link Amity.Client} have been banned from the platform
|
|
1310
|
+
*
|
|
1311
|
+
* @param callback The function to call when the event was fired
|
|
1312
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1313
|
+
*
|
|
1314
|
+
* @category Client Events
|
|
1315
|
+
*/
|
|
1316
|
+
const onConnectionError = (callback) => {
|
|
1317
|
+
const client = getActiveClient();
|
|
1318
|
+
const subscribers = EVENTS.map(event => (createEventSubscriber(client, `client/onConnectionError(${event})`, event, (response) => {
|
|
1319
|
+
const error = (response && 'code' in response)
|
|
1320
|
+
? new ASCError(response.message, response.code, "fatal" /* FATAL */)
|
|
1321
|
+
: new ASCConnectionError(event);
|
|
1322
|
+
callback(error);
|
|
1323
|
+
})));
|
|
1324
|
+
return () => {
|
|
1325
|
+
subscribers.forEach(fn => fn());
|
|
1326
|
+
};
|
|
1327
|
+
};
|
|
1328
|
+
|
|
1329
|
+
/**
|
|
1330
|
+
* ```js
|
|
1331
|
+
* import { onClientDisconnected } from '@amityco/ts-sdk'
|
|
1332
|
+
* const dispose = onClientDisconnected(() => {
|
|
1333
|
+
* // ...
|
|
1334
|
+
* })
|
|
1335
|
+
* ```
|
|
1336
|
+
*
|
|
1337
|
+
* Fired when any {@link Amity.Client} have been disconnected
|
|
1338
|
+
*
|
|
1339
|
+
* @param callback The function to call when the event was fired
|
|
1340
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1341
|
+
*
|
|
1342
|
+
* @category Client Events
|
|
1343
|
+
*/
|
|
1344
|
+
const onClientDisconnected = (callback) => {
|
|
1345
|
+
return onConnectionError(({ code }) => {
|
|
1346
|
+
code === 800211 /* DISCONNECTED */ && callback();
|
|
1347
|
+
});
|
|
1348
|
+
};
|
|
1349
|
+
|
|
1350
|
+
/**
|
|
1351
|
+
* ```js
|
|
1352
|
+
* import { onClientBanned } from '@amityco/ts-sdk'
|
|
1353
|
+
* const dispose = onClientBanned(() => {
|
|
1354
|
+
* // ...
|
|
1355
|
+
* })
|
|
1356
|
+
* ```
|
|
1357
|
+
*
|
|
1358
|
+
* Fired when any {@link Amity.Client} have been banned from the platform
|
|
1359
|
+
*
|
|
1360
|
+
* @param callback The function to call when the event was fired
|
|
1361
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1362
|
+
*
|
|
1363
|
+
* @category Client Events
|
|
1364
|
+
*/
|
|
1365
|
+
const onClientBanned = (callback) => {
|
|
1366
|
+
return onConnectionError(({ code }) => {
|
|
1367
|
+
code === 400312 /* GLOBAL_BAN */ && callback();
|
|
1368
|
+
});
|
|
1369
|
+
};
|
|
1370
|
+
|
|
1054
1371
|
/**
|
|
1055
1372
|
* ```js
|
|
1056
1373
|
* import { connectClient } from '@amityco/ts-sdk/client/api'
|
|
@@ -1079,11 +1396,12 @@ const connectClient = async (params) => {
|
|
|
1079
1396
|
(_b = params.deviceId) !== null && _b !== void 0 ? _b : (params.deviceId = getDeviceId());
|
|
1080
1397
|
// @ts-ignore: in that particular case, typing can be
|
|
1081
1398
|
// ignored since we enforce default values just before.
|
|
1082
|
-
const { accessToken } = await getToken(params);
|
|
1399
|
+
const { accessToken, users } = await getToken(params);
|
|
1083
1400
|
// manually setup the token for http transport
|
|
1084
1401
|
client.http.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
|
|
1085
1402
|
// manually setup the token for ws transport
|
|
1086
1403
|
client.ws.io.opts.query = { token: accessToken };
|
|
1404
|
+
// FIXME: events are duplicated if connectClient is called few times without disconnectClient
|
|
1087
1405
|
// wire websocket events to our event emitter
|
|
1088
1406
|
proxyWebsocketEvents(client.ws, client.emitter);
|
|
1089
1407
|
// TODO: in phase 2, this should not be necessary. we should have WS
|
|
@@ -1092,44 +1410,23 @@ const connectClient = async (params) => {
|
|
|
1092
1410
|
client.ws.once('connect', resolve);
|
|
1093
1411
|
client.ws.open();
|
|
1094
1412
|
});
|
|
1413
|
+
const unsubscribe = onClientBanned(() => {
|
|
1414
|
+
disconnectClient();
|
|
1415
|
+
unsubscribe();
|
|
1416
|
+
});
|
|
1095
1417
|
client.userId = params.userId;
|
|
1418
|
+
client.user = users[0];
|
|
1419
|
+
client.accessToken = accessToken;
|
|
1096
1420
|
return client.ws.connected;
|
|
1097
1421
|
};
|
|
1098
1422
|
|
|
1099
1423
|
/**
|
|
1100
1424
|
* ```js
|
|
1101
|
-
* import {
|
|
1102
|
-
* const
|
|
1425
|
+
* import { isConnected } from '@amityco/ts-sdk'
|
|
1426
|
+
* const connected = isConnected()
|
|
1103
1427
|
* ```
|
|
1104
1428
|
*
|
|
1105
|
-
*
|
|
1106
|
-
*
|
|
1107
|
-
* @param client The {@link Amity.Client} instance to disconnect
|
|
1108
|
-
* @returns a success boolean if disconnected
|
|
1109
|
-
*
|
|
1110
|
-
* @category Client API
|
|
1111
|
-
* @async
|
|
1112
|
-
*/
|
|
1113
|
-
const disconnectClient = async () => {
|
|
1114
|
-
const client = getActiveClient();
|
|
1115
|
-
client.log('client/api/disconnectClient');
|
|
1116
|
-
await new Promise(resolve => {
|
|
1117
|
-
client.ws.once('disconnect', resolve);
|
|
1118
|
-
client.ws.disconnect();
|
|
1119
|
-
});
|
|
1120
|
-
client.emitter.all.clear();
|
|
1121
|
-
client.ws.removeAllListeners();
|
|
1122
|
-
client.userId = undefined;
|
|
1123
|
-
return !client.ws.connected;
|
|
1124
|
-
};
|
|
1125
|
-
|
|
1126
|
-
/**
|
|
1127
|
-
* ```js
|
|
1128
|
-
* import { isConnected } from '@amityco/ts-sdk'
|
|
1129
|
-
* const connected = isConnected()
|
|
1130
|
-
* ```
|
|
1131
|
-
*
|
|
1132
|
-
* Checks if a {@link Amity.Client} instance is connected to ASC servers
|
|
1429
|
+
* Checks if a {@link Amity.Client} instance is connected to ASC servers
|
|
1133
1430
|
*
|
|
1134
1431
|
* @returns a success boolean if connected
|
|
1135
1432
|
*
|
|
@@ -1279,16 +1576,6 @@ const clearCache = (stores = ['user', 'file', 'channel', 'message', 'community',
|
|
|
1279
1576
|
stores.forEach(store => { var _a, _b; return (_b = (_a = client.cache) === null || _a === void 0 ? void 0 : _a[store]) === null || _b === void 0 ? void 0 : _b.erase(); });
|
|
1280
1577
|
};
|
|
1281
1578
|
|
|
1282
|
-
/**
|
|
1283
|
-
* converts an array looking collection to an object collection, grouped by localId
|
|
1284
|
-
*
|
|
1285
|
-
* @param array array of {@link Amity.Syncable}
|
|
1286
|
-
* @returns array converted to object, grouped by localId
|
|
1287
|
-
*
|
|
1288
|
-
* @hidden
|
|
1289
|
-
*/
|
|
1290
|
-
const toRecord = (array) => (array.reduce((acc, item) => (Object.assign(Object.assign({}, acc), { [item.localId]: item })), {}));
|
|
1291
|
-
|
|
1292
1579
|
/**
|
|
1293
1580
|
* Set the model's syncedAt property and localId
|
|
1294
1581
|
*
|
|
@@ -1374,82 +1661,51 @@ const sortByLastCreated = ({ createdAt: a }, { createdAt: b }) => new Date(b).va
|
|
|
1374
1661
|
*/
|
|
1375
1662
|
const sortByLastActivity = ({ lastActivity: a }, { lastActivity: b }) => new Date(b).valueOf() - new Date(a).valueOf();
|
|
1376
1663
|
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
];
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
const
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
* @param callback The function to call when the event was fired
|
|
1423
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1424
|
-
*
|
|
1425
|
-
* @category Client Events
|
|
1426
|
-
*/
|
|
1427
|
-
const onClientDisconnected = (callback) => {
|
|
1428
|
-
return onConnectionError(({ code }) => {
|
|
1429
|
-
code === 800211 /* DISCONNECTED */ && callback();
|
|
1430
|
-
});
|
|
1431
|
-
};
|
|
1432
|
-
|
|
1433
|
-
/**
|
|
1434
|
-
* ```js
|
|
1435
|
-
* import { onClientBanned } from '@amityco/ts-sdk'
|
|
1436
|
-
* const dispose = onClientBanned(() => {
|
|
1437
|
-
* // ...
|
|
1438
|
-
* })
|
|
1439
|
-
* ```
|
|
1440
|
-
*
|
|
1441
|
-
* Fired when any {@link Amity.Client} have been banned from the platform
|
|
1442
|
-
*
|
|
1443
|
-
* @param callback The function to call when the event was fired
|
|
1444
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1445
|
-
*
|
|
1446
|
-
* @category Client Events
|
|
1447
|
-
*/
|
|
1448
|
-
const onClientBanned = (callback) => {
|
|
1449
|
-
return onConnectionError(({ code }) => {
|
|
1450
|
-
code === 400312 /* GLOBAL_BAN */ && callback();
|
|
1451
|
-
});
|
|
1452
|
-
};
|
|
1664
|
+
exports.SubscriptionLevels = void 0;
|
|
1665
|
+
(function (SubscriptionLevels) {
|
|
1666
|
+
SubscriptionLevels["COMMUNITY"] = "community";
|
|
1667
|
+
SubscriptionLevels["POST"] = "post";
|
|
1668
|
+
SubscriptionLevels["COMMENT"] = "comment";
|
|
1669
|
+
SubscriptionLevels["POST_AND_COMMENT"] = "post_and_comment";
|
|
1670
|
+
SubscriptionLevels["USER"] = "user";
|
|
1671
|
+
})(exports.SubscriptionLevels || (exports.SubscriptionLevels = {}));
|
|
1672
|
+
const getCommunityUserTopic = ({ path }, level) => {
|
|
1673
|
+
switch (level) {
|
|
1674
|
+
case 'post':
|
|
1675
|
+
return `${path}/post/+`;
|
|
1676
|
+
case 'comment':
|
|
1677
|
+
return `${path}/post/+/comment/+`;
|
|
1678
|
+
case 'post_and_comment':
|
|
1679
|
+
return `${path}/post/#`;
|
|
1680
|
+
default:
|
|
1681
|
+
return path;
|
|
1682
|
+
}
|
|
1683
|
+
};
|
|
1684
|
+
const getCommunityTopic = ({ path }, level = exports.SubscriptionLevels.COMMUNITY) => getCommunityUserTopic({ path }, level);
|
|
1685
|
+
const getUserTopic = ({ path }, level = exports.SubscriptionLevels.USER) => getCommunityUserTopic({ path }, level);
|
|
1686
|
+
const getPostTopic = ({ path }, level = exports.SubscriptionLevels.POST) => {
|
|
1687
|
+
switch (level) {
|
|
1688
|
+
case 'comment':
|
|
1689
|
+
return `${path}/comment/+`;
|
|
1690
|
+
default:
|
|
1691
|
+
return path;
|
|
1692
|
+
}
|
|
1693
|
+
};
|
|
1694
|
+
const getCommentTopic = ({ path }) => {
|
|
1695
|
+
return path;
|
|
1696
|
+
};
|
|
1697
|
+
let mqttAccessToken;
|
|
1698
|
+
let mqttUserId;
|
|
1699
|
+
function subscribeTopic(topic, callback) {
|
|
1700
|
+
const { mqtt, emitter, accessToken, user } = getActiveClient();
|
|
1701
|
+
if (mqttAccessToken !== accessToken || mqttUserId !== user._id) {
|
|
1702
|
+
mqttAccessToken = accessToken;
|
|
1703
|
+
mqttUserId = user._id;
|
|
1704
|
+
mqtt.connect({ accessToken: accessToken, userId: user._id });
|
|
1705
|
+
proxyMqttEvents(mqtt, emitter);
|
|
1706
|
+
}
|
|
1707
|
+
return mqtt.subscribe(topic, callback);
|
|
1708
|
+
}
|
|
1453
1709
|
|
|
1454
1710
|
/**
|
|
1455
1711
|
* ```js
|
|
@@ -1651,6 +1907,16 @@ const updateUser = async (userId, patch) => {
|
|
|
1651
1907
|
return data.users.find(user => user.userId === userId);
|
|
1652
1908
|
};
|
|
1653
1909
|
|
|
1910
|
+
const createUserEventSubscriber = (event, callback) => {
|
|
1911
|
+
const client = getActiveClient();
|
|
1912
|
+
const filter = (payload) => {
|
|
1913
|
+
decoratePayload(payload);
|
|
1914
|
+
ingestInCache(payload);
|
|
1915
|
+
callback(payload.users[0]);
|
|
1916
|
+
};
|
|
1917
|
+
return createEventSubscriber(client, event, event, filter);
|
|
1918
|
+
};
|
|
1919
|
+
|
|
1654
1920
|
/**
|
|
1655
1921
|
* ```js
|
|
1656
1922
|
* import { onUserUpdated } from '@amityco/ts-sdk'
|
|
@@ -1659,22 +1925,65 @@ const updateUser = async (userId, patch) => {
|
|
|
1659
1925
|
* })
|
|
1660
1926
|
* ```
|
|
1661
1927
|
*
|
|
1662
|
-
* Fired when
|
|
1928
|
+
* Fired when a {@link Amity.User} has been updated
|
|
1663
1929
|
*
|
|
1664
1930
|
* @param callback The function to call when the event was fired
|
|
1665
1931
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1666
1932
|
*
|
|
1667
1933
|
* @category User Events
|
|
1668
1934
|
*/
|
|
1669
|
-
const onUserUpdated = (callback) =>
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1935
|
+
const onUserUpdated = (callback) => createUserEventSubscriber('user.updated', callback);
|
|
1936
|
+
|
|
1937
|
+
/**
|
|
1938
|
+
* ```js
|
|
1939
|
+
* import { onUserFlagged } from '@amityco/ts-sdk'
|
|
1940
|
+
* const dispose = onUserFlagged(user => {
|
|
1941
|
+
* // ...
|
|
1942
|
+
* })
|
|
1943
|
+
* ```
|
|
1944
|
+
*
|
|
1945
|
+
* Fired when a {@link Amity.User} has been flagged
|
|
1946
|
+
*
|
|
1947
|
+
* @param callback The function to call when the event was fired
|
|
1948
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1949
|
+
*
|
|
1950
|
+
* @category User Events
|
|
1951
|
+
*/
|
|
1952
|
+
const onUserFlagged = (callback) => createUserEventSubscriber('user.flagged', callback);
|
|
1953
|
+
|
|
1954
|
+
/**
|
|
1955
|
+
* ```js
|
|
1956
|
+
* import { onUserUnflagged } from '@amityco/ts-sdk'
|
|
1957
|
+
* const dispose = onUserUnflagged(user => {
|
|
1958
|
+
* // ...
|
|
1959
|
+
* })
|
|
1960
|
+
* ```
|
|
1961
|
+
*
|
|
1962
|
+
* Fired when a flag has been removed from a {@link Amity.User}
|
|
1963
|
+
*
|
|
1964
|
+
* @param callback The function to call when the event was fired
|
|
1965
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1966
|
+
*
|
|
1967
|
+
* @category User Events
|
|
1968
|
+
*/
|
|
1969
|
+
const onUserUnflagged = (callback) => createUserEventSubscriber('user.unflagged', callback);
|
|
1970
|
+
|
|
1971
|
+
/**
|
|
1972
|
+
* ```js
|
|
1973
|
+
* import { onUserFlagCleared } from '@amityco/ts-sdk'
|
|
1974
|
+
* const dispose = onUserFlagCleared(user => {
|
|
1975
|
+
* // ...
|
|
1976
|
+
* })
|
|
1977
|
+
* ```
|
|
1978
|
+
*
|
|
1979
|
+
* Fired when flags have been cleared for a {@link Amity.User}
|
|
1980
|
+
*
|
|
1981
|
+
* @param callback The function to call when the event was fired
|
|
1982
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1983
|
+
*
|
|
1984
|
+
* @category User Events
|
|
1985
|
+
*/
|
|
1986
|
+
const onUserFlagCleared = (callback) => createUserEventSubscriber('user.flagCleared', callback);
|
|
1678
1987
|
|
|
1679
1988
|
/**
|
|
1680
1989
|
* ```js
|
|
@@ -1708,7 +2017,7 @@ const observeUser = (userId, callback) => {
|
|
|
1708
2017
|
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
1709
2018
|
};
|
|
1710
2019
|
const disposers = [];
|
|
1711
|
-
disposers.push(onUserUpdated(data => router({ data }, 'onUpdate')));
|
|
2020
|
+
disposers.push(onUserUpdated(data => router({ data }, 'onUpdate')), onUserFlagged(data => router({ data }, 'onFlagged')), onUserUnflagged(data => router({ data }, 'onUnflagged')), onUserFlagCleared(data => router({ data }, 'onFlagCleared')));
|
|
1712
2021
|
runQuery(createQuery(getUser, userId), (result) => result.data && router(result, 'onFetch'));
|
|
1713
2022
|
return () => {
|
|
1714
2023
|
log(`observeUser(tmpid: ${timestamp}) > dispose`);
|
|
@@ -1813,11 +2122,10 @@ const createFile = async (formData, onProgress) => {
|
|
|
1813
2122
|
const deleteFile = async (fileId) => {
|
|
1814
2123
|
const client = getActiveClient();
|
|
1815
2124
|
client.log('file/deleteFile', fileId);
|
|
2125
|
+
// API-FIX: payload is different than swagger docs!!
|
|
1816
2126
|
const { data } = await client.http.delete(`/api/v3/files/${fileId}`);
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
mergeInCache('file', { fileId, isDeleted: true });
|
|
1820
|
-
return success;
|
|
2127
|
+
dropFromCache('file', { fileId: data.fileId });
|
|
2128
|
+
return { success: true };
|
|
1821
2129
|
};
|
|
1822
2130
|
|
|
1823
2131
|
/**
|
|
@@ -1973,10 +2281,10 @@ const dispatchReactable = (referenceType, model) => {
|
|
|
1973
2281
|
fireEvent('v3.message.didUpdate', { messages: [model] });
|
|
1974
2282
|
else if (referenceType === 'post')
|
|
1975
2283
|
// @ts-ignore: refactor later
|
|
1976
|
-
fireEvent('
|
|
2284
|
+
fireEvent('post.updated', { posts: [model] });
|
|
1977
2285
|
else if (referenceType === 'comment')
|
|
1978
2286
|
// @ts-ignore: refactor later
|
|
1979
|
-
fireEvent('
|
|
2287
|
+
fireEvent('comment.updated', { comments: [model] });
|
|
1980
2288
|
};
|
|
1981
2289
|
|
|
1982
2290
|
/**
|
|
@@ -2441,10 +2749,21 @@ getPosts.locally = (postIds) => {
|
|
|
2441
2749
|
* @async
|
|
2442
2750
|
*/
|
|
2443
2751
|
const getPost = async (postId) => {
|
|
2752
|
+
var _a;
|
|
2444
2753
|
const client = getActiveClient();
|
|
2445
2754
|
client.log('post/getPost', postId);
|
|
2446
|
-
|
|
2447
|
-
|
|
2755
|
+
let data;
|
|
2756
|
+
try {
|
|
2757
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
2758
|
+
const response = await client.http.get(`/api/v3/posts/${postId}`);
|
|
2759
|
+
data = response.data;
|
|
2760
|
+
}
|
|
2761
|
+
catch (error) {
|
|
2762
|
+
if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 400400 /* ITEM_NOT_FOUND */) {
|
|
2763
|
+
dropFromCache('post', { postId });
|
|
2764
|
+
}
|
|
2765
|
+
throw error;
|
|
2766
|
+
}
|
|
2448
2767
|
decoratePayload(data);
|
|
2449
2768
|
ingestInCache(data);
|
|
2450
2769
|
const { posts } = data;
|
|
@@ -2498,8 +2817,8 @@ const createPost = async (bundle) => {
|
|
|
2498
2817
|
const { data } = await client.http.post('/api/v3/posts', bundle);
|
|
2499
2818
|
decoratePayload(data);
|
|
2500
2819
|
ingestInCache(data);
|
|
2501
|
-
fireEvent('
|
|
2502
|
-
fireEvent('
|
|
2820
|
+
fireEvent('community.updated', { communities: data.communities });
|
|
2821
|
+
fireEvent('post.created', data);
|
|
2503
2822
|
const { posts } = data;
|
|
2504
2823
|
return posts[0];
|
|
2505
2824
|
};
|
|
@@ -2527,7 +2846,7 @@ const updatePost = async (postId, patch) => {
|
|
|
2527
2846
|
const { data } = await client.http.put(`/api/v3/posts/${postId}`, patch);
|
|
2528
2847
|
decoratePayload(data);
|
|
2529
2848
|
ingestInCache(data);
|
|
2530
|
-
fireEvent('
|
|
2849
|
+
fireEvent('post.updated', data);
|
|
2531
2850
|
const { posts } = data;
|
|
2532
2851
|
return posts.find(post => post.postId === postId);
|
|
2533
2852
|
};
|
|
@@ -2697,7 +3016,7 @@ const createCommunity = async (bundle) => {
|
|
|
2697
3016
|
const { data } = await client.http.post('/api/v3/communities/', bundle);
|
|
2698
3017
|
decoratePayload(data);
|
|
2699
3018
|
ingestInCache(data);
|
|
2700
|
-
fireEvent('
|
|
3019
|
+
fireEvent('community.created', data);
|
|
2701
3020
|
const { communities } = data;
|
|
2702
3021
|
return communities[0];
|
|
2703
3022
|
};
|
|
@@ -2723,7 +3042,7 @@ const updateCommunity = async (communityId, patch) => {
|
|
|
2723
3042
|
const { data } = await client.http.put(`/api/v3/communities/${communityId}`, patch);
|
|
2724
3043
|
decoratePayload(data);
|
|
2725
3044
|
ingestInCache(data);
|
|
2726
|
-
fireEvent('
|
|
3045
|
+
fireEvent('community.updated', data);
|
|
2727
3046
|
const { communities } = data;
|
|
2728
3047
|
return communities.find(community => community.communityId === communityId);
|
|
2729
3048
|
};
|
|
@@ -2748,7 +3067,7 @@ const deleteCommunity = async (communityId) => {
|
|
|
2748
3067
|
// API-FIX: This endpoint has not been implemented yet.
|
|
2749
3068
|
await client.http.delete(`/api/v3/communities/${communityId}`);
|
|
2750
3069
|
const deleted = await getCommunity(communityId);
|
|
2751
|
-
fireEvent('
|
|
3070
|
+
fireEvent('community.deleted', { communities: [deleted] });
|
|
2752
3071
|
return deleted;
|
|
2753
3072
|
};
|
|
2754
3073
|
|
|
@@ -2772,7 +3091,7 @@ const joinCommunity = async (communityId) => {
|
|
|
2772
3091
|
const { data } = await client.http.post(`/api/v3/communities/${communityId}/join`);
|
|
2773
3092
|
decoratePayload(data);
|
|
2774
3093
|
ingestInCache(data);
|
|
2775
|
-
fireEvent('
|
|
3094
|
+
fireEvent('community.joined', {
|
|
2776
3095
|
communities: data.communities
|
|
2777
3096
|
});
|
|
2778
3097
|
const { communityUsers } = data;
|
|
@@ -2800,7 +3119,7 @@ const leaveCommunity = async (communityId) => {
|
|
|
2800
3119
|
const { data } = await client.http.delete(`/api/v3/communities/${communityId}/leave`);
|
|
2801
3120
|
decoratePayload(data);
|
|
2802
3121
|
ingestInCache(data);
|
|
2803
|
-
fireEvent('
|
|
3122
|
+
fireEvent('community.left', {
|
|
2804
3123
|
communities: data.communities,
|
|
2805
3124
|
});
|
|
2806
3125
|
const { communityUsers } = data;
|
|
@@ -2879,7 +3198,7 @@ const addCommunityMembers = async (communityId, userIds) => {
|
|
|
2879
3198
|
const { data } = await client.http.post(`/api/v3/communities/${communityId}/users`, { communityId, userIds });
|
|
2880
3199
|
decoratePayload(data);
|
|
2881
3200
|
ingestInCache(data);
|
|
2882
|
-
fireEvent('
|
|
3201
|
+
fireEvent('community.userAdded', {
|
|
2883
3202
|
communities: data.communities,
|
|
2884
3203
|
});
|
|
2885
3204
|
const { communityUsers } = data;
|
|
@@ -2908,7 +3227,7 @@ const removeCommunityMembers = async (communityId, userIds) => {
|
|
|
2908
3227
|
const { data } = await client.http.delete(`/api/v3/communities/${communityId}/users`, { data: { communityId, userIds } });
|
|
2909
3228
|
decoratePayload(data);
|
|
2910
3229
|
ingestInCache(data);
|
|
2911
|
-
fireEvent('
|
|
3230
|
+
fireEvent('community.userRemoved', {
|
|
2912
3231
|
communities: data.communities,
|
|
2913
3232
|
});
|
|
2914
3233
|
const { communityUsers } = data;
|
|
@@ -2982,18 +3301,36 @@ const removeCommunityMembersRole = async (communityId, roleId, userIds) => {
|
|
|
2982
3301
|
* @category Post API
|
|
2983
3302
|
* @async
|
|
2984
3303
|
*/
|
|
2985
|
-
const deletePost = async (postId) => {
|
|
3304
|
+
const deletePost = async (postId, permanent = false) => {
|
|
2986
3305
|
const client = getActiveClient();
|
|
2987
3306
|
client.log('post/deletePost', postId);
|
|
3307
|
+
let post = await getPost(postId);
|
|
2988
3308
|
// API-FIX: This endpoint has not been implemented yet.
|
|
2989
|
-
await client.http.delete(`/api/
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
3309
|
+
await client.http.delete(`/api/v4/posts/${postId}`, {
|
|
3310
|
+
params: {
|
|
3311
|
+
postId,
|
|
3312
|
+
permanent,
|
|
3313
|
+
},
|
|
3314
|
+
});
|
|
3315
|
+
// there is currently a limitation which doesn't allow us to fire event to tell that community
|
|
3316
|
+
// has been updated. reason is that, when the object is deleted, we don't have its `communityId`
|
|
3317
|
+
// and so we cannot refetch the community or advertise on events. hopefully this should be solved
|
|
3318
|
+
// later when realtime events covers that for us.
|
|
3319
|
+
if (post.targetType === 'community') {
|
|
3320
|
+
const community = await getCommunity(post.targetId);
|
|
3321
|
+
fireEvent('community.updated', { communities: [community] });
|
|
3322
|
+
}
|
|
3323
|
+
// to support hard deletion
|
|
3324
|
+
const deleted = Object.assign(Object.assign({}, post), { isDeleted: true });
|
|
3325
|
+
fireEvent('post.deleted', { posts: [deleted] });
|
|
3326
|
+
if (permanent) {
|
|
3327
|
+
setTimeout(() => {
|
|
3328
|
+
dropFromCache('post', { postId });
|
|
3329
|
+
}, 0);
|
|
3330
|
+
}
|
|
3331
|
+
else {
|
|
3332
|
+
mergeInCache('post', { postId, isDeleted: true });
|
|
2995
3333
|
}
|
|
2996
|
-
fireEvent('v3.post.didDelete', { posts: [deleted] });
|
|
2997
3334
|
return deleted;
|
|
2998
3335
|
};
|
|
2999
3336
|
|
|
@@ -3054,8 +3391,6 @@ queryComments.locally = (query) => {
|
|
|
3054
3391
|
const client = getActiveClient();
|
|
3055
3392
|
client.log('post/queryComments.locally', query);
|
|
3056
3393
|
const { referenceId, referenceType, parentId, isDeleted, page = { before: 0, limit: 5 }, } = query;
|
|
3057
|
-
if (isNaN(page.after) && isNaN(page.before))
|
|
3058
|
-
page.before = 0;
|
|
3059
3394
|
if (isNaN(page.limit))
|
|
3060
3395
|
page.limit = 5;
|
|
3061
3396
|
let comments = (_a = pullFromCache('comment')) !== null && _a !== void 0 ? _a : [];
|
|
@@ -3085,8 +3420,8 @@ queryComments.locally = (query) => {
|
|
|
3085
3420
|
if (afterIndex < 0 || beforeIndex < 0)
|
|
3086
3421
|
return;
|
|
3087
3422
|
let slice = comments.slice(afterIndex !== -1 ? afterIndex : 0, beforeIndex !== -1 ? beforeIndex : undefined);
|
|
3088
|
-
//
|
|
3089
|
-
if (
|
|
3423
|
+
// check if slice data is correct (complete)
|
|
3424
|
+
if (slice.length < page.limit)
|
|
3090
3425
|
return;
|
|
3091
3426
|
if (!isNaN(page.after) && !isNaN(page.before)) ;
|
|
3092
3427
|
else if (!isNaN(page.after)) {
|
|
@@ -3095,16 +3430,20 @@ queryComments.locally = (query) => {
|
|
|
3095
3430
|
else if (!isNaN(page.before)) {
|
|
3096
3431
|
slice = slice.slice(-page.limit);
|
|
3097
3432
|
}
|
|
3433
|
+
else {
|
|
3434
|
+
slice = slice.slice(-page.limit);
|
|
3435
|
+
}
|
|
3098
3436
|
for (let i = 0; i < slice.length - 1; i++)
|
|
3099
3437
|
if (slice[i].segmentNumber !== slice[i + 1].segmentNumber - 1)
|
|
3100
3438
|
return;
|
|
3101
3439
|
const prevPage = slice[0].segmentNumber > 1
|
|
3102
3440
|
? { before: slice[0].segmentNumber, limit: page.limit }
|
|
3103
3441
|
: undefined;
|
|
3104
|
-
const nextPage =
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3442
|
+
const nextPage = slice[slice.length - 1].segmentNumber < comments[comments.length - 1].segmentNumber
|
|
3443
|
+
? {
|
|
3444
|
+
after: slice[slice.length - 1].segmentNumber,
|
|
3445
|
+
limit: page.limit,
|
|
3446
|
+
} : undefined;
|
|
3108
3447
|
return { data: toRecord(slice), nextPage, prevPage };
|
|
3109
3448
|
};
|
|
3110
3449
|
|
|
@@ -3169,10 +3508,21 @@ getComments.locally = (commentIds) => {
|
|
|
3169
3508
|
* @async
|
|
3170
3509
|
*/
|
|
3171
3510
|
const getComment = async (commentId) => {
|
|
3511
|
+
var _a;
|
|
3172
3512
|
const client = getActiveClient();
|
|
3173
3513
|
client.log('comment/getComment', commentId);
|
|
3174
|
-
|
|
3175
|
-
|
|
3514
|
+
let data;
|
|
3515
|
+
try {
|
|
3516
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
3517
|
+
const response = await client.http.get(`/api/v3/comments/${commentId}`);
|
|
3518
|
+
data = response.data;
|
|
3519
|
+
}
|
|
3520
|
+
catch (error) {
|
|
3521
|
+
if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 400400 /* ITEM_NOT_FOUND */) {
|
|
3522
|
+
dropFromCache('comment', { commentId });
|
|
3523
|
+
}
|
|
3524
|
+
throw error;
|
|
3525
|
+
}
|
|
3176
3526
|
decoratePayload(data);
|
|
3177
3527
|
ingestInCache(data);
|
|
3178
3528
|
const { comments } = data;
|
|
@@ -3220,8 +3570,8 @@ const createComment = async (bundle) => {
|
|
|
3220
3570
|
decoratePayload(data);
|
|
3221
3571
|
ingestInCache(data);
|
|
3222
3572
|
const post = await getPost(bundle.referenceId);
|
|
3223
|
-
fireEvent('
|
|
3224
|
-
fireEvent('
|
|
3573
|
+
fireEvent('post.updated', { posts: [post] });
|
|
3574
|
+
fireEvent('comment.created', data);
|
|
3225
3575
|
const { comments } = data;
|
|
3226
3576
|
return comments[0];
|
|
3227
3577
|
};
|
|
@@ -3249,7 +3599,7 @@ const updateComment = async (commentId, patch) => {
|
|
|
3249
3599
|
const { data } = await client.http.put(`/api/v3/comments/${commentId}`, patch);
|
|
3250
3600
|
decoratePayload(data);
|
|
3251
3601
|
ingestInCache(data);
|
|
3252
|
-
fireEvent('
|
|
3602
|
+
fireEvent('comment.updated', data);
|
|
3253
3603
|
const { comments } = data;
|
|
3254
3604
|
return comments.find(comment => comment.commentId === commentId);
|
|
3255
3605
|
};
|
|
@@ -3268,15 +3618,30 @@ const updateComment = async (commentId, patch) => {
|
|
|
3268
3618
|
* @category Comment API
|
|
3269
3619
|
* @async
|
|
3270
3620
|
*/
|
|
3271
|
-
const deleteComment = async (commentId) => {
|
|
3621
|
+
const deleteComment = async (commentId, permanent = false) => {
|
|
3272
3622
|
const client = getActiveClient();
|
|
3273
3623
|
client.log('comment/deleteComment', commentId);
|
|
3624
|
+
const comment = await getComment(commentId);
|
|
3274
3625
|
// API-FIX: This endpoint has not been implemented yet.
|
|
3275
|
-
await client.http.delete(`/api/
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3626
|
+
await client.http.delete(`/api/v4/comments/${commentId}`, {
|
|
3627
|
+
params: {
|
|
3628
|
+
commentId,
|
|
3629
|
+
permanent,
|
|
3630
|
+
},
|
|
3631
|
+
});
|
|
3632
|
+
// to support hard deletion
|
|
3633
|
+
const deleted = Object.assign(Object.assign({}, comment), { isDeleted: true });
|
|
3634
|
+
const post = await getPost(comment.referenceId);
|
|
3635
|
+
fireEvent('post.updated', { posts: [post] });
|
|
3636
|
+
fireEvent('comment.deleted', { comments: [deleted] });
|
|
3637
|
+
if (permanent) {
|
|
3638
|
+
setTimeout(() => {
|
|
3639
|
+
dropFromCache('comment', { commentId });
|
|
3640
|
+
}, 0);
|
|
3641
|
+
}
|
|
3642
|
+
else {
|
|
3643
|
+
mergeInCache('comment', { commentId, isDeleted: true });
|
|
3644
|
+
}
|
|
3280
3645
|
return deleted;
|
|
3281
3646
|
};
|
|
3282
3647
|
|
|
@@ -3291,6 +3656,42 @@ const fetchReference = async (referenceType, referenceId) => {
|
|
|
3291
3656
|
throw new Error(`Incorrect referenceType: ${referenceType}`);
|
|
3292
3657
|
};
|
|
3293
3658
|
|
|
3659
|
+
/** @hidden */
|
|
3660
|
+
const preparePayloadForCache = (event, payload) => {
|
|
3661
|
+
var _a, _b;
|
|
3662
|
+
const client = getActiveClient();
|
|
3663
|
+
if (client.userId !== payload.reactor.userId) {
|
|
3664
|
+
return;
|
|
3665
|
+
}
|
|
3666
|
+
let model;
|
|
3667
|
+
let cached;
|
|
3668
|
+
if (event === 'post.addReaction' || event === 'post.removeReaction') {
|
|
3669
|
+
const post = payload.posts[0];
|
|
3670
|
+
cached = ((_a = pullFromCache('post', post.postId)) !== null && _a !== void 0 ? _a : [])[0];
|
|
3671
|
+
model = post;
|
|
3672
|
+
}
|
|
3673
|
+
else {
|
|
3674
|
+
const comment = payload.comments[0];
|
|
3675
|
+
cached = ((_b = pullFromCache('comment', comment.commentId)) !== null && _b !== void 0 ? _b : [])[0];
|
|
3676
|
+
model = comment;
|
|
3677
|
+
}
|
|
3678
|
+
if (!cached || !cached.myReactions) {
|
|
3679
|
+
return;
|
|
3680
|
+
}
|
|
3681
|
+
if (event === 'post.addReaction' || event === 'comment.addReaction') {
|
|
3682
|
+
if (cached.myReactions.includes(payload.reactor.reactionName)) {
|
|
3683
|
+
return;
|
|
3684
|
+
}
|
|
3685
|
+
Object.assign(model, { myReactions: [...cached.myReactions, payload.reactor.reactionName] });
|
|
3686
|
+
}
|
|
3687
|
+
else {
|
|
3688
|
+
if (!cached.myReactions.includes(payload.reactor.reactionName)) {
|
|
3689
|
+
return;
|
|
3690
|
+
}
|
|
3691
|
+
Object.assign(model, { myReactions: cached.myReactions.filter(x => x !== payload.reactor.reactionName) });
|
|
3692
|
+
}
|
|
3693
|
+
};
|
|
3694
|
+
|
|
3294
3695
|
/**
|
|
3295
3696
|
* ```js
|
|
3296
3697
|
* import { addReaction } from '@amityco/ts-sdk'
|
|
@@ -3403,51 +3804,441 @@ removeReaction.optimistically = (referenceType, referenceId, reactionName) => {
|
|
|
3403
3804
|
return (_b = !((_a = model === null || model === void 0 ? void 0 : model.myReactions) === null || _a === void 0 ? void 0 : _a.includes(reactionName))) !== null && _b !== void 0 ? _b : false;
|
|
3404
3805
|
};
|
|
3405
3806
|
|
|
3807
|
+
const createPostEventSubscriber = (event, callback) => {
|
|
3808
|
+
const client = getActiveClient();
|
|
3809
|
+
const filter = (payload) => {
|
|
3810
|
+
var _a, _b;
|
|
3811
|
+
const [cached] = (_a = pullFromCache('post', payload.posts[0].postId)) !== null && _a !== void 0 ? _a : [];
|
|
3812
|
+
const { communities } = payload, others = __rest(payload, ["communities"]);
|
|
3813
|
+
decoratePayload(others);
|
|
3814
|
+
ingestInCache(others);
|
|
3815
|
+
if (communities === null || communities === void 0 ? void 0 : communities[0]) {
|
|
3816
|
+
fireEvent('community.updated', { communities });
|
|
3817
|
+
}
|
|
3818
|
+
const [post] = (_b = pullFromCache('post', others.posts[0].postId)) !== null && _b !== void 0 ? _b : [];
|
|
3819
|
+
if (cached && isMatch(cached, post)) {
|
|
3820
|
+
return;
|
|
3821
|
+
}
|
|
3822
|
+
callback(post);
|
|
3823
|
+
};
|
|
3824
|
+
return createEventSubscriber(client, event, event, filter);
|
|
3825
|
+
};
|
|
3826
|
+
|
|
3406
3827
|
/**
|
|
3407
3828
|
* ```js
|
|
3408
|
-
* import {
|
|
3409
|
-
* const dispose =
|
|
3829
|
+
* import { onPostCreated } from '@amityco/ts-sdk'
|
|
3830
|
+
* const dispose = onPostCreated(post => {
|
|
3410
3831
|
* // ...
|
|
3411
3832
|
* })
|
|
3412
3833
|
* ```
|
|
3413
3834
|
*
|
|
3414
|
-
* Fired when
|
|
3835
|
+
* Fired when {@link Amity.Post} has been created
|
|
3415
3836
|
*
|
|
3416
3837
|
* @param callback The function to call when the event was fired
|
|
3417
3838
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3418
3839
|
*
|
|
3419
|
-
* @category
|
|
3420
|
-
|
|
3421
|
-
const
|
|
3422
|
-
const client = getActiveClient();
|
|
3423
|
-
const filter = (payload) => {
|
|
3424
|
-
// TODO: lol
|
|
3425
|
-
decoratePayload(payload);
|
|
3426
|
-
ingestInCache(payload);
|
|
3427
|
-
};
|
|
3428
|
-
return createEventSubscriber(client, 'reaction/onReactionRemoved', 'v3.message.didUpdate', filter);
|
|
3429
|
-
};
|
|
3840
|
+
* @category Post Events
|
|
3841
|
+
*/
|
|
3842
|
+
const onPostCreated = (callback) => createPostEventSubscriber('post.created', callback);
|
|
3430
3843
|
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3844
|
+
/**
|
|
3845
|
+
* ```js
|
|
3846
|
+
* import { onPostUpdated } from '@amityco/ts-sdk'
|
|
3847
|
+
* const dispose = onPostUpdated(post => {
|
|
3848
|
+
* // ...
|
|
3849
|
+
* })
|
|
3850
|
+
* ```
|
|
3851
|
+
*
|
|
3852
|
+
* Fired when a {@link Amity.Post} has been updated
|
|
3853
|
+
*
|
|
3854
|
+
* @param callback The function to call when the event was fired
|
|
3855
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3856
|
+
*
|
|
3857
|
+
* @category Post Events
|
|
3858
|
+
*/
|
|
3859
|
+
const onPostUpdated = (callback) => createPostEventSubscriber('post.updated', callback);
|
|
3860
|
+
|
|
3861
|
+
/**
|
|
3862
|
+
* ```js
|
|
3863
|
+
* import { onPostDeleted } from '@amityco/ts-sdk'
|
|
3864
|
+
* const dispose = onPostDeleted(post => {
|
|
3865
|
+
* // ...
|
|
3866
|
+
* })
|
|
3867
|
+
* ```
|
|
3868
|
+
*
|
|
3869
|
+
* Fired when a {@link Amity.Post} has been deleted
|
|
3870
|
+
*
|
|
3871
|
+
* @param callback The function to call when the event was fired
|
|
3872
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3873
|
+
*
|
|
3874
|
+
* @category Post Events
|
|
3875
|
+
*/
|
|
3876
|
+
const onPostDeleted = (callback) => createPostEventSubscriber('post.deleted', callback);
|
|
3877
|
+
|
|
3878
|
+
/**
|
|
3879
|
+
* ```js
|
|
3880
|
+
* import { onPostApproved } from '@amityco/ts-sdk'
|
|
3881
|
+
* const dispose = onPostApproved(post => {
|
|
3882
|
+
* // ...
|
|
3883
|
+
* })
|
|
3884
|
+
* ```
|
|
3885
|
+
*
|
|
3886
|
+
* Fired when a {@link Amity.Post} has been updated
|
|
3887
|
+
*
|
|
3888
|
+
* @param callback The function to call when the event was fired
|
|
3889
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3890
|
+
*
|
|
3891
|
+
* @category Post Events
|
|
3892
|
+
*/
|
|
3893
|
+
const onPostApproved = (callback) => createPostEventSubscriber('post.approved', callback);
|
|
3894
|
+
|
|
3895
|
+
/**
|
|
3896
|
+
* ```js
|
|
3897
|
+
* import { onPostDeclined } from '@amityco/ts-sdk'
|
|
3898
|
+
* const dispose = onPostDeclined(post => {
|
|
3899
|
+
* // ...
|
|
3900
|
+
* })
|
|
3901
|
+
* ```
|
|
3902
|
+
*
|
|
3903
|
+
* Fired when a {@link Amity.Post} has been updated
|
|
3904
|
+
*
|
|
3905
|
+
* @param callback The function to call when the event was fired
|
|
3906
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3907
|
+
*
|
|
3908
|
+
* @category Post Events
|
|
3909
|
+
*/
|
|
3910
|
+
const onPostDeclined = (callback) => createPostEventSubscriber('post.declined', callback);
|
|
3911
|
+
|
|
3912
|
+
/**
|
|
3913
|
+
* ```js
|
|
3914
|
+
* import { onPostFlagged } from '@amityco/ts-sdk'
|
|
3915
|
+
* const dispose = onPostFlagged(post => {
|
|
3916
|
+
* // ...
|
|
3917
|
+
* })
|
|
3918
|
+
* ```
|
|
3919
|
+
*
|
|
3920
|
+
* Fired when a {@link Amity.Post} has been flagged
|
|
3921
|
+
*
|
|
3922
|
+
* @param callback The function to call when the event was fired
|
|
3923
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3924
|
+
*
|
|
3925
|
+
* @category Post Events
|
|
3926
|
+
*/
|
|
3927
|
+
const onPostFlagged = (callback) => createPostEventSubscriber('post.flagged', callback);
|
|
3928
|
+
|
|
3929
|
+
/**
|
|
3930
|
+
* ```js
|
|
3931
|
+
* import { onPostUnflagged } from '@amityco/ts-sdk'
|
|
3932
|
+
* const dispose = onPostUnflagged(post => {
|
|
3933
|
+
* // ...
|
|
3934
|
+
* })
|
|
3935
|
+
* ```
|
|
3936
|
+
*
|
|
3937
|
+
* Fired when a flag has been removed from a {@link Amity.Post}
|
|
3938
|
+
*
|
|
3939
|
+
* @param callback The function to call when the event was fired
|
|
3940
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3941
|
+
*
|
|
3942
|
+
* @category Post Events
|
|
3943
|
+
*/
|
|
3944
|
+
const onPostUnflagged = (callback) => createPostEventSubscriber('post.unflagged', callback);
|
|
3945
|
+
|
|
3946
|
+
/**
|
|
3947
|
+
* ```js
|
|
3948
|
+
* import { onPostReactionAdded } from '@amityco/ts-sdk'
|
|
3949
|
+
* const dispose = onPostReactionAdded(post => {
|
|
3950
|
+
* // ...
|
|
3951
|
+
* })
|
|
3952
|
+
* ```
|
|
3953
|
+
*
|
|
3954
|
+
* Fired when a {@link Amity.Post} has been reacted
|
|
3955
|
+
*
|
|
3956
|
+
* @param callback The function to call when the event was fired
|
|
3957
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3958
|
+
*
|
|
3959
|
+
* @category Post Events
|
|
3960
|
+
*/
|
|
3961
|
+
const onPostReactionAdded = (callback) => {
|
|
3962
|
+
const client = getActiveClient();
|
|
3963
|
+
const filter = (payload) => {
|
|
3964
|
+
var _a, _b;
|
|
3965
|
+
const [cached] = (_a = pullFromCache('post', payload.posts[0].postId)) !== null && _a !== void 0 ? _a : [];
|
|
3966
|
+
const postPayload = __rest(payload, ["reactor"]);
|
|
3967
|
+
decoratePayload(postPayload);
|
|
3968
|
+
preparePayloadForCache('post.addReaction', payload);
|
|
3969
|
+
ingestInCache(postPayload);
|
|
3970
|
+
const [post] = (_b = pullFromCache('post', payload.posts[0].postId)) !== null && _b !== void 0 ? _b : [];
|
|
3971
|
+
if (cached && isMatch(cached, post)) {
|
|
3972
|
+
return;
|
|
3973
|
+
}
|
|
3974
|
+
callback(post);
|
|
3975
|
+
};
|
|
3976
|
+
return createEventSubscriber(client, 'post.addReaction', 'post.addReaction', filter);
|
|
3977
|
+
};
|
|
3978
|
+
|
|
3979
|
+
/**
|
|
3980
|
+
* ```js
|
|
3981
|
+
* import { onPostReactionRemoved } from '@amityco/ts-sdk'
|
|
3982
|
+
* const dispose = onPostReactionRemoved(post => {
|
|
3983
|
+
* // ...
|
|
3984
|
+
* })
|
|
3985
|
+
* ```
|
|
3986
|
+
*
|
|
3987
|
+
* Fired when a reaction has been removed from a {@link Amity.Post}
|
|
3988
|
+
*
|
|
3989
|
+
* @param callback The function to call when the event was fired
|
|
3990
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3991
|
+
*
|
|
3992
|
+
* @category Post Events
|
|
3993
|
+
*/
|
|
3994
|
+
const onPostReactionRemoved = (callback) => {
|
|
3995
|
+
const client = getActiveClient();
|
|
3996
|
+
const filter = (payload) => {
|
|
3997
|
+
var _a, _b;
|
|
3998
|
+
const [cached] = (_a = pullFromCache('post', payload.posts[0].postId)) !== null && _a !== void 0 ? _a : [];
|
|
3999
|
+
const postPayload = __rest(payload, ["reactor"]);
|
|
4000
|
+
decoratePayload(postPayload);
|
|
4001
|
+
preparePayloadForCache('post.removeReaction', payload);
|
|
4002
|
+
ingestInCache(postPayload);
|
|
4003
|
+
const [post] = (_b = pullFromCache('post', payload.posts[0].postId)) !== null && _b !== void 0 ? _b : [];
|
|
4004
|
+
if (cached && isMatch(cached, post)) {
|
|
4005
|
+
return;
|
|
4006
|
+
}
|
|
4007
|
+
callback(post);
|
|
4008
|
+
};
|
|
4009
|
+
return createEventSubscriber(client, 'post.removeReaction', 'post.removeReaction', filter);
|
|
4010
|
+
};
|
|
4011
|
+
|
|
4012
|
+
const createCommentEventSubscriber = (event, callback) => {
|
|
4013
|
+
const client = getActiveClient();
|
|
4014
|
+
const filter = (payload) => {
|
|
4015
|
+
var _a, _b;
|
|
4016
|
+
const [cached] = (_a = pullFromCache('comment', payload.comments[0].commentId)) !== null && _a !== void 0 ? _a : [];
|
|
4017
|
+
decoratePayload(payload);
|
|
4018
|
+
ingestInCache(payload);
|
|
4019
|
+
const [comment] = (_b = pullFromCache('comment', payload.comments[0].commentId)) !== null && _b !== void 0 ? _b : [];
|
|
4020
|
+
if (cached && isMatch(cached, comment)) {
|
|
4021
|
+
return;
|
|
4022
|
+
}
|
|
4023
|
+
callback(comment);
|
|
4024
|
+
};
|
|
4025
|
+
return createEventSubscriber(client, event, event, filter);
|
|
4026
|
+
};
|
|
4027
|
+
|
|
4028
|
+
/**
|
|
4029
|
+
* ```js
|
|
4030
|
+
* import { onCommentCreated } from '@amityco/ts-sdk'
|
|
4031
|
+
* const dispose = onCommentCreated(comment => {
|
|
4032
|
+
* // ...
|
|
4033
|
+
* })
|
|
4034
|
+
* ```
|
|
4035
|
+
*
|
|
4036
|
+
* Fired when {@link Amity.Comment} has been created
|
|
4037
|
+
*
|
|
4038
|
+
* @param callback The function to call when the event was fired
|
|
4039
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4040
|
+
*
|
|
4041
|
+
* @category Comment Events
|
|
4042
|
+
*/
|
|
4043
|
+
const onCommentCreated = (callback) => createCommentEventSubscriber('comment.created', callback);
|
|
4044
|
+
|
|
4045
|
+
/**
|
|
4046
|
+
* ```js
|
|
4047
|
+
* import { onCommentUpdated } from '@amityco/ts-sdk'
|
|
4048
|
+
* const dispose = onCommentUpdated(comment => {
|
|
4049
|
+
* // ...
|
|
4050
|
+
* })
|
|
4051
|
+
* ```
|
|
4052
|
+
*
|
|
4053
|
+
* Fired when a {@link Amity.Comment} has been updated
|
|
4054
|
+
*
|
|
4055
|
+
* @param callback The function to call when the event was fired
|
|
4056
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4057
|
+
*
|
|
4058
|
+
* @category Comment Events
|
|
4059
|
+
*/
|
|
4060
|
+
const onCommentUpdated = (callback) => createCommentEventSubscriber('comment.updated', callback);
|
|
4061
|
+
|
|
4062
|
+
/**
|
|
4063
|
+
* ```js
|
|
4064
|
+
* import { onCommentDeleted } from '@amityco/ts-sdk'
|
|
4065
|
+
* const dispose = onCommentDeleted(comment => {
|
|
4066
|
+
* // ...
|
|
4067
|
+
* })
|
|
4068
|
+
* ```
|
|
4069
|
+
*
|
|
4070
|
+
* Fired when a {@link Amity.Comment} has been deleted
|
|
4071
|
+
*
|
|
4072
|
+
* @param callback The function to call when the event was fired
|
|
4073
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4074
|
+
*
|
|
4075
|
+
* @category Comment Events
|
|
4076
|
+
*/
|
|
4077
|
+
const onCommentDeleted = (callback) => createCommentEventSubscriber('comment.deleted', callback);
|
|
4078
|
+
|
|
4079
|
+
/**
|
|
4080
|
+
* ```js
|
|
4081
|
+
* import { onCommentFlagged } from '@amityco/ts-sdk'
|
|
4082
|
+
* const dispose = onCommentFlagged(comment => {
|
|
4083
|
+
* // ...
|
|
4084
|
+
* })
|
|
4085
|
+
* ```
|
|
4086
|
+
*
|
|
4087
|
+
* Fired when a {@link Amity.Comment} has been flagged
|
|
4088
|
+
*
|
|
4089
|
+
* @param callback The function to call when the event was fired
|
|
4090
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4091
|
+
*
|
|
4092
|
+
* @category Comment Events
|
|
4093
|
+
*/
|
|
4094
|
+
const onCommentFlagged = (callback) => createCommentEventSubscriber('comment.flagged', callback);
|
|
4095
|
+
|
|
4096
|
+
/**
|
|
4097
|
+
* ```js
|
|
4098
|
+
* import { onCommentUnflagged } from '@amityco/ts-sdk'
|
|
4099
|
+
* const dispose = onCommentUnflagged(comment => {
|
|
4100
|
+
* // ...
|
|
4101
|
+
* })
|
|
4102
|
+
* ```
|
|
4103
|
+
*
|
|
4104
|
+
* Fired when a flag has been removed from a {@link Amity.Comment}
|
|
4105
|
+
*
|
|
4106
|
+
* @param callback The function to call when the event was fired
|
|
4107
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4108
|
+
*
|
|
4109
|
+
* @category Comment Events
|
|
4110
|
+
*/
|
|
4111
|
+
const onCommentUnflagged = (callback) => createCommentEventSubscriber('comment.unflagged', callback);
|
|
4112
|
+
|
|
4113
|
+
/**
|
|
4114
|
+
* ```js
|
|
4115
|
+
* import { onCommentReactionAdded } from '@amityco/ts-sdk'
|
|
4116
|
+
* const dispose = onCommentReactionAdded(comment => {
|
|
4117
|
+
* // ...
|
|
4118
|
+
* })
|
|
4119
|
+
* ```
|
|
4120
|
+
*
|
|
4121
|
+
* Fired when a {@link Amity.Comment} has been reacted
|
|
4122
|
+
*
|
|
4123
|
+
* @param callback The function to call when the event was fired
|
|
4124
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4125
|
+
*
|
|
4126
|
+
* @category Comment Events
|
|
4127
|
+
*/
|
|
4128
|
+
const onCommentReactionAdded = (callback) => {
|
|
4129
|
+
const client = getActiveClient();
|
|
4130
|
+
const filter = (payload) => {
|
|
4131
|
+
var _a, _b;
|
|
4132
|
+
const [cached] = (_a = pullFromCache('comment', payload.comments[0].commentId)) !== null && _a !== void 0 ? _a : [];
|
|
4133
|
+
const commentPayload = __rest(payload, ["reactor"]);
|
|
4134
|
+
decoratePayload(commentPayload);
|
|
4135
|
+
preparePayloadForCache('comment.addReaction', payload);
|
|
4136
|
+
ingestInCache(commentPayload);
|
|
4137
|
+
const [comment] = (_b = pullFromCache('comment', payload.comments[0].commentId)) !== null && _b !== void 0 ? _b : [];
|
|
4138
|
+
if (cached && isMatch(cached, comment)) {
|
|
4139
|
+
return;
|
|
4140
|
+
}
|
|
4141
|
+
callback(comment);
|
|
4142
|
+
};
|
|
4143
|
+
return createEventSubscriber(client, 'comment.addReaction', 'comment.addReaction', filter);
|
|
4144
|
+
};
|
|
4145
|
+
|
|
4146
|
+
/**
|
|
4147
|
+
* ```js
|
|
4148
|
+
* import { onCommentReactionRemoved } from '@amityco/ts-sdk'
|
|
4149
|
+
* const dispose = onCommentReactionRemoved(comment => {
|
|
4150
|
+
* // ...
|
|
4151
|
+
* })
|
|
4152
|
+
* ```
|
|
4153
|
+
*
|
|
4154
|
+
* Fired when a reaction has been removed from a {@link Amity.Comment}
|
|
4155
|
+
*
|
|
4156
|
+
* @param callback The function to call when the event was fired
|
|
4157
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4158
|
+
*
|
|
4159
|
+
* @category Comment Events
|
|
4160
|
+
*/
|
|
4161
|
+
const onCommentReactionRemoved = (callback) => {
|
|
4162
|
+
const client = getActiveClient();
|
|
4163
|
+
const filter = (payload) => {
|
|
4164
|
+
var _a, _b;
|
|
4165
|
+
const [cached] = (_a = pullFromCache('comment', payload.comments[0].commentId)) !== null && _a !== void 0 ? _a : [];
|
|
4166
|
+
const commentPayload = __rest(payload, ["reactor"]);
|
|
4167
|
+
decoratePayload(commentPayload);
|
|
4168
|
+
preparePayloadForCache('comment.removeReaction', payload);
|
|
4169
|
+
ingestInCache(commentPayload);
|
|
4170
|
+
const [comment] = (_b = pullFromCache('comment', payload.comments[0].commentId)) !== null && _b !== void 0 ? _b : [];
|
|
4171
|
+
if (cached && isMatch(cached, comment)) {
|
|
4172
|
+
return;
|
|
4173
|
+
}
|
|
4174
|
+
callback(comment);
|
|
4175
|
+
};
|
|
4176
|
+
return createEventSubscriber(client, 'comment.removeReaction', 'comment.removeReaction', filter);
|
|
4177
|
+
};
|
|
4178
|
+
|
|
4179
|
+
/**
|
|
4180
|
+
* ```js
|
|
4181
|
+
* import { onReactionRemoved } from '@amityco/ts-sdk'
|
|
4182
|
+
* const dispose = onReactionRemoved('post', postId, post => {
|
|
4183
|
+
* // ...
|
|
4184
|
+
* })
|
|
4185
|
+
* ```
|
|
4186
|
+
*
|
|
4187
|
+
* Fired when an {@link Amity.Reaction} has been removed
|
|
4188
|
+
*
|
|
4189
|
+
* @param {@link Amity.ReactableType} referenceType
|
|
4190
|
+
* @param {string} referenceId
|
|
4191
|
+
* @param callback The function to call when the event was fired
|
|
4192
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4193
|
+
*
|
|
4194
|
+
* @category Message Events
|
|
4195
|
+
**/
|
|
4196
|
+
const onReactionRemoved = (referenceType, referenceId, callback) => {
|
|
4197
|
+
const callbackWrapper = (model) => {
|
|
4198
|
+
if (getResolver(referenceType)(model) === referenceId) {
|
|
4199
|
+
callback(model);
|
|
4200
|
+
}
|
|
4201
|
+
};
|
|
4202
|
+
if (referenceType === 'message') {
|
|
4203
|
+
const client = getActiveClient();
|
|
4204
|
+
const filter = (payload) => {
|
|
4205
|
+
var _a, _b;
|
|
4206
|
+
const reactionsCountInCache = (_b = ((_a = pullFromCache('message', payload.messages[0].messageId)) !== null && _a !== void 0 ? _a : [])[0]) === null || _b === void 0 ? void 0 : _b.reactionsCount;
|
|
4207
|
+
const isReactionEvent = reactionsCountInCache !== payload.messages[0].reactionsCount;
|
|
4208
|
+
decoratePayload(payload);
|
|
4209
|
+
ingestInCache(payload);
|
|
4210
|
+
if (isReactionEvent) {
|
|
4211
|
+
callbackWrapper(payload.messages[0]);
|
|
4212
|
+
}
|
|
4213
|
+
};
|
|
4214
|
+
return createEventSubscriber(client, 'reaction/onReactionRemoved', 'v3.message.didUpdate', filter);
|
|
4215
|
+
}
|
|
4216
|
+
if (referenceType === 'post') {
|
|
4217
|
+
return onPostReactionRemoved(callbackWrapper);
|
|
4218
|
+
}
|
|
4219
|
+
return onCommentReactionRemoved(callbackWrapper);
|
|
4220
|
+
};
|
|
4221
|
+
|
|
4222
|
+
var REFERENCE_TYPES = {
|
|
4223
|
+
post: {
|
|
4224
|
+
domainName: 'posts',
|
|
4225
|
+
eventName: 'v3/post.isflagbyme',
|
|
4226
|
+
primaryKey: 'postId',
|
|
4227
|
+
},
|
|
4228
|
+
comment: {
|
|
4229
|
+
domainName: 'comments',
|
|
4230
|
+
eventName: 'v3/comment.isflagbyme',
|
|
4231
|
+
primaryKey: 'commentId',
|
|
4232
|
+
},
|
|
4233
|
+
message: {
|
|
4234
|
+
domainName: 'messages',
|
|
4235
|
+
eventName: 'v3/message.isFlaggedByMe',
|
|
4236
|
+
primaryKey: 'messageId',
|
|
4237
|
+
},
|
|
4238
|
+
user: {
|
|
4239
|
+
domainName: 'users',
|
|
4240
|
+
eventName: 'user.isFlagByMe',
|
|
4241
|
+
primaryKey: 'userId',
|
|
3451
4242
|
},
|
|
3452
4243
|
};
|
|
3453
4244
|
|
|
@@ -3469,9 +4260,12 @@ const createReport = async (referenceType, referenceId) => {
|
|
|
3469
4260
|
client.log('report/createReport', { referenceType, referenceId });
|
|
3470
4261
|
const { domainName } = REFERENCE_TYPES[referenceType];
|
|
3471
4262
|
const { data } = await client.http.post(`/api/v3/${domainName}/${referenceId}/flag`);
|
|
4263
|
+
const event = referenceType === 'message'
|
|
4264
|
+
? 'v3.message.didUpdate'
|
|
4265
|
+
: `${referenceType}.updated`;
|
|
3472
4266
|
decoratePayload(data);
|
|
3473
4267
|
ingestInCache(data);
|
|
3474
|
-
fireEvent(
|
|
4268
|
+
fireEvent(event, data);
|
|
3475
4269
|
return !!data;
|
|
3476
4270
|
};
|
|
3477
4271
|
|
|
@@ -3493,9 +4287,12 @@ const deleteReport = async (referenceType, referenceId) => {
|
|
|
3493
4287
|
client.log('report/deleteReport', { referenceType, referenceId });
|
|
3494
4288
|
const { domainName } = REFERENCE_TYPES[referenceType];
|
|
3495
4289
|
const { data } = await client.http.delete(`/api/v3/${domainName}/${referenceId}/unflag`);
|
|
4290
|
+
const event = referenceType === 'message'
|
|
4291
|
+
? 'v3.message.didUpdate'
|
|
4292
|
+
: `${referenceType}.updated`;
|
|
3496
4293
|
decoratePayload(data);
|
|
3497
4294
|
ingestInCache(data);
|
|
3498
|
-
fireEvent(
|
|
4295
|
+
fireEvent(event, data);
|
|
3499
4296
|
return !!data;
|
|
3500
4297
|
};
|
|
3501
4298
|
|
|
@@ -4265,166 +5062,253 @@ const observeMessage = (messageId, callback, policy = 'cache_then_server') => {
|
|
|
4265
5062
|
|
|
4266
5063
|
/**
|
|
4267
5064
|
* ```js
|
|
4268
|
-
* import { observeMessages } from '@amityco/ts-sdk'
|
|
5065
|
+
* import { observeMessages } from '@amityco/ts-sdk'
|
|
5066
|
+
*
|
|
5067
|
+
* let messages = []
|
|
5068
|
+
* const unsub = observeMessages(channelId, message => merge(messages, message))
|
|
5069
|
+
* ```
|
|
5070
|
+
*
|
|
5071
|
+
* Observe all mutations on a list of {@link Amity.Message} for a given {@link Amity.Channel} object
|
|
5072
|
+
*
|
|
5073
|
+
* @param client the client to observe the message with
|
|
5074
|
+
* @param channelId the ID of the channel where to observe the messages
|
|
5075
|
+
* @param callback the function to call when new data are available
|
|
5076
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
5077
|
+
*
|
|
5078
|
+
* @category Message Observer
|
|
5079
|
+
*/
|
|
5080
|
+
const observeMessages = (channelId, callback) => {
|
|
5081
|
+
const { log } = getActiveClient();
|
|
5082
|
+
const timestamp = Date.now();
|
|
5083
|
+
log(`observeMessages(tmpid: ${timestamp}) > listen`);
|
|
5084
|
+
const disposers = [];
|
|
5085
|
+
const router = (message, action) => {
|
|
5086
|
+
var _a, _b;
|
|
5087
|
+
if (message.channelId !== channelId)
|
|
5088
|
+
return;
|
|
5089
|
+
if (callback instanceof Function)
|
|
5090
|
+
return callback(message);
|
|
5091
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, message);
|
|
5092
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, message);
|
|
5093
|
+
};
|
|
5094
|
+
disposers.push(onMessageCreated(message => router(message, 'onCreate')), onMessageUpdated(message => router(message, 'onUpdate')), onMessageDeleted(message => router(message, 'onDelete')));
|
|
5095
|
+
return () => {
|
|
5096
|
+
log(`observeMessages(tmpid: ${timestamp}) > dispose`);
|
|
5097
|
+
disposers.forEach(fn => fn());
|
|
5098
|
+
};
|
|
5099
|
+
};
|
|
5100
|
+
|
|
5101
|
+
const createCommunityEventSubscriber = (event, callback) => {
|
|
5102
|
+
const client = getActiveClient();
|
|
5103
|
+
const filter = (payload) => {
|
|
5104
|
+
var _a, _b;
|
|
5105
|
+
const [cached] = (_a = pullFromCache('community', payload.communities[0].communityId)) !== null && _a !== void 0 ? _a : [];
|
|
5106
|
+
decoratePayload(payload);
|
|
5107
|
+
ingestInCache(payload);
|
|
5108
|
+
const [community] = (_b = pullFromCache('community', payload.communities[0].communityId)) !== null && _b !== void 0 ? _b : [];
|
|
5109
|
+
if (cached && isMatch(cached, community)) {
|
|
5110
|
+
return;
|
|
5111
|
+
}
|
|
5112
|
+
callback(community);
|
|
5113
|
+
};
|
|
5114
|
+
return createEventSubscriber(client, event, event, filter);
|
|
5115
|
+
};
|
|
5116
|
+
function hasPermission(member, payload, permission) {
|
|
5117
|
+
if (member.permissions.some(x => x === permission)) {
|
|
5118
|
+
return true;
|
|
5119
|
+
}
|
|
5120
|
+
return payload.users
|
|
5121
|
+
.find(user => user.userId === member.userId)
|
|
5122
|
+
.permissions.some(x => x === permission);
|
|
5123
|
+
}
|
|
5124
|
+
function getEventRelatedMember(event, payload) {
|
|
5125
|
+
if (event === 'community.joined' || event === 'community.left') {
|
|
5126
|
+
return payload.communityUsers[0];
|
|
5127
|
+
}
|
|
5128
|
+
// NOTE: backend returns the one who took the action and the one on whom
|
|
5129
|
+
// the action was taken. We need the 2nd one
|
|
5130
|
+
if (event === 'community.userRemoved') {
|
|
5131
|
+
return payload.communityUsers.filter(x => x.communityMembership === 'none')[0];
|
|
5132
|
+
}
|
|
5133
|
+
if (event === 'community.userBanned') {
|
|
5134
|
+
return payload.communityUsers.filter(x => x.communityMembership === 'banned')[0];
|
|
5135
|
+
}
|
|
5136
|
+
// NOTE: it might be that in certain edge cases permission check won't be enough
|
|
5137
|
+
if (event === 'community.userUnbanned') {
|
|
5138
|
+
return payload.communityUsers.filter(x => !hasPermission(x, payload, 'BAN_COMMUNITY_USER'))[0];
|
|
5139
|
+
}
|
|
5140
|
+
return payload.communityUsers.filter(x => !hasPermission(x, payload, 'ADD_COMMUNITY_USER'))[0];
|
|
5141
|
+
}
|
|
5142
|
+
const createCommunityMemberEventSubscriber = (event, callback) => {
|
|
5143
|
+
const client = getActiveClient();
|
|
5144
|
+
const filter = (payload) => {
|
|
5145
|
+
var _a, _b, _c, _d;
|
|
5146
|
+
const [cachedCommunity] = (_a = pullFromCache('community', payload.communities[0].communityId)) !== null && _a !== void 0 ? _a : [];
|
|
5147
|
+
const [cachedMember] = (_b = pullFromCache('communityUsers', getResolver('communityUsers')(getEventRelatedMember(event, payload)))) !== null && _b !== void 0 ? _b : [];
|
|
5148
|
+
decoratePayload(payload);
|
|
5149
|
+
ingestInCache(payload);
|
|
5150
|
+
const [community] = (_c = pullFromCache('community', payload.communities[0].communityId)) !== null && _c !== void 0 ? _c : [];
|
|
5151
|
+
const [member] = (_d = pullFromCache('communityUsers', getResolver('communityUsers')(getEventRelatedMember(event, payload)))) !== null && _d !== void 0 ? _d : [];
|
|
5152
|
+
if (cachedCommunity && isMatch(cachedCommunity, community) && cachedMember && isMatch(cachedMember, member)) {
|
|
5153
|
+
return;
|
|
5154
|
+
}
|
|
5155
|
+
callback(community, member);
|
|
5156
|
+
};
|
|
5157
|
+
return createEventSubscriber(client, event, event, filter);
|
|
5158
|
+
};
|
|
5159
|
+
|
|
5160
|
+
/**
|
|
5161
|
+
* ```js
|
|
5162
|
+
* import { onCommunityCreated } from '@amityco/ts-sdk'
|
|
5163
|
+
* const dispose = onCommunityCreated(community => {
|
|
5164
|
+
* // ...
|
|
5165
|
+
* })
|
|
5166
|
+
* ```
|
|
5167
|
+
*
|
|
5168
|
+
* Fired when {@link Amity.Community} have been created
|
|
5169
|
+
*
|
|
5170
|
+
* @param callback The function to call when the event was fired
|
|
5171
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
5172
|
+
*
|
|
5173
|
+
* @category Community Events
|
|
5174
|
+
*/
|
|
5175
|
+
const onCommunityCreated = (callback) => createCommunityEventSubscriber('community.created', callback);
|
|
5176
|
+
|
|
5177
|
+
/**
|
|
5178
|
+
* ```js
|
|
5179
|
+
* import { onCommunityUpdated } from '@amityco/ts-sdk'
|
|
5180
|
+
* const dispose = onCommunityUpdated(community => {
|
|
5181
|
+
* // ...
|
|
5182
|
+
* })
|
|
5183
|
+
* ```
|
|
5184
|
+
*
|
|
5185
|
+
* Fired when a {@link Amity.Community} has been updated
|
|
5186
|
+
*
|
|
5187
|
+
* @param callback The function to call when the event was fired
|
|
5188
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
5189
|
+
*
|
|
5190
|
+
* @category Community Events
|
|
5191
|
+
*/
|
|
5192
|
+
const onCommunityUpdated = (callback) => createCommunityEventSubscriber('community.updated', callback);
|
|
5193
|
+
|
|
5194
|
+
/**
|
|
5195
|
+
* ```js
|
|
5196
|
+
* import { onCommunityDeleted } from '@amityco/ts-sdk'
|
|
5197
|
+
* const dispose = onCommunityDeleted(community => {
|
|
5198
|
+
* // ...
|
|
5199
|
+
* })
|
|
5200
|
+
* ```
|
|
4269
5201
|
*
|
|
4270
|
-
*
|
|
4271
|
-
*
|
|
5202
|
+
* Fired when a {@link Amity.Community} has been deleted
|
|
5203
|
+
*
|
|
5204
|
+
* @param callback The function to call when the event was fired
|
|
5205
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
5206
|
+
*
|
|
5207
|
+
* @category Community Events
|
|
5208
|
+
*/
|
|
5209
|
+
const onCommunityDeleted = (callback) => createCommunityEventSubscriber('community.deleted', callback);
|
|
5210
|
+
|
|
5211
|
+
/**
|
|
5212
|
+
* ```js
|
|
5213
|
+
* import { onCommunityJoined } from '@amityco/ts-sdk'
|
|
5214
|
+
* const dispose = onCommunityJoined((community, member) => {
|
|
5215
|
+
* // ...
|
|
5216
|
+
* })
|
|
4272
5217
|
* ```
|
|
4273
5218
|
*
|
|
4274
|
-
*
|
|
5219
|
+
* Fired when a {@link Amity.Community} has been joined
|
|
4275
5220
|
*
|
|
4276
|
-
* @param
|
|
4277
|
-
* @
|
|
4278
|
-
* @param callback the function to call when new data are available
|
|
4279
|
-
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
5221
|
+
* @param callback The function to call when the event was fired
|
|
5222
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4280
5223
|
*
|
|
4281
|
-
* @category
|
|
5224
|
+
* @category Community Events
|
|
4282
5225
|
*/
|
|
4283
|
-
const
|
|
4284
|
-
const { log } = getActiveClient();
|
|
4285
|
-
const timestamp = Date.now();
|
|
4286
|
-
log(`observeMessages(tmpid: ${timestamp}) > listen`);
|
|
4287
|
-
const disposers = [];
|
|
4288
|
-
const router = (message, action) => {
|
|
4289
|
-
var _a, _b;
|
|
4290
|
-
if (message.channelId !== channelId)
|
|
4291
|
-
return;
|
|
4292
|
-
if (callback instanceof Function)
|
|
4293
|
-
return callback(message);
|
|
4294
|
-
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, message);
|
|
4295
|
-
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, message);
|
|
4296
|
-
};
|
|
4297
|
-
disposers.push(onMessageCreated(message => router(message, 'onCreate')), onMessageUpdated(message => router(message, 'onUpdate')), onMessageDeleted(message => router(message, 'onDelete')));
|
|
4298
|
-
return () => {
|
|
4299
|
-
log(`observeMessages(tmpid: ${timestamp}) > dispose`);
|
|
4300
|
-
disposers.forEach(fn => fn());
|
|
4301
|
-
};
|
|
4302
|
-
};
|
|
5226
|
+
const onCommunityJoined = (callback) => createCommunityMemberEventSubscriber('community.joined', callback);
|
|
4303
5227
|
|
|
4304
5228
|
/**
|
|
4305
5229
|
* ```js
|
|
4306
|
-
* import {
|
|
4307
|
-
* const dispose =
|
|
5230
|
+
* import { onCommunityLeft } from '@amityco/ts-sdk'
|
|
5231
|
+
* const dispose = onCommunityLeft((community, member) => {
|
|
4308
5232
|
* // ...
|
|
4309
5233
|
* })
|
|
4310
5234
|
* ```
|
|
4311
5235
|
*
|
|
4312
|
-
* Fired when
|
|
5236
|
+
* Fired when a {@link Amity.Community} has been left
|
|
4313
5237
|
*
|
|
4314
5238
|
* @param callback The function to call when the event was fired
|
|
4315
5239
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4316
5240
|
*
|
|
4317
5241
|
* @category Community Events
|
|
4318
5242
|
*/
|
|
4319
|
-
const
|
|
4320
|
-
const client = getActiveClient();
|
|
4321
|
-
const filter = (payload) => {
|
|
4322
|
-
decoratePayload(payload);
|
|
4323
|
-
ingestInCache(payload);
|
|
4324
|
-
callback(payload.communities[0]);
|
|
4325
|
-
};
|
|
4326
|
-
return createEventSubscriber(client, 'onCommunityCreated', 'v3.community.didCreate', filter);
|
|
4327
|
-
};
|
|
5243
|
+
const onCommunityLeft = (callback) => createCommunityMemberEventSubscriber('community.left', callback);
|
|
4328
5244
|
|
|
4329
5245
|
/**
|
|
4330
5246
|
* ```js
|
|
4331
|
-
* import {
|
|
4332
|
-
* const dispose =
|
|
5247
|
+
* import { onCommunityUserAdded } from '@amityco/ts-sdk'
|
|
5248
|
+
* const dispose = onCommunityUserAdded((community, member) => {
|
|
4333
5249
|
* // ...
|
|
4334
5250
|
* })
|
|
4335
5251
|
* ```
|
|
4336
5252
|
*
|
|
4337
|
-
* Fired when
|
|
5253
|
+
* Fired when a user has been added to a {@link Amity.Community}
|
|
4338
5254
|
*
|
|
4339
5255
|
* @param callback The function to call when the event was fired
|
|
4340
5256
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4341
5257
|
*
|
|
4342
5258
|
* @category Community Events
|
|
4343
5259
|
*/
|
|
4344
|
-
const
|
|
4345
|
-
const client = getActiveClient();
|
|
4346
|
-
const filter = (payload) => {
|
|
4347
|
-
decoratePayload(payload);
|
|
4348
|
-
ingestInCache(payload);
|
|
4349
|
-
callback(payload.communities[0]);
|
|
4350
|
-
};
|
|
4351
|
-
return createEventSubscriber(client, 'onCommunityUpdated', 'v3.community.didUpdate', filter);
|
|
4352
|
-
};
|
|
5260
|
+
const onCommunityUserAdded = (callback) => createCommunityMemberEventSubscriber('community.userAdded', callback);
|
|
4353
5261
|
|
|
4354
5262
|
/**
|
|
4355
5263
|
* ```js
|
|
4356
|
-
* import {
|
|
4357
|
-
* const dispose =
|
|
5264
|
+
* import { onCommunityUserRemoved } from '@amityco/ts-sdk'
|
|
5265
|
+
* const dispose = onCommunityUserRemoved((community, member) => {
|
|
4358
5266
|
* // ...
|
|
4359
5267
|
* })
|
|
4360
5268
|
* ```
|
|
4361
5269
|
*
|
|
4362
|
-
* Fired when
|
|
5270
|
+
* Fired when a user has been removed from a {@link Amity.Community}
|
|
4363
5271
|
*
|
|
4364
5272
|
* @param callback The function to call when the event was fired
|
|
4365
5273
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4366
5274
|
*
|
|
4367
5275
|
* @category Community Events
|
|
4368
5276
|
*/
|
|
4369
|
-
const
|
|
4370
|
-
const client = getActiveClient();
|
|
4371
|
-
const filter = (payload) => {
|
|
4372
|
-
decoratePayload(payload);
|
|
4373
|
-
ingestInCache(payload);
|
|
4374
|
-
callback(payload.communities[0]);
|
|
4375
|
-
};
|
|
4376
|
-
return createEventSubscriber(client, 'onCommunityDeleted', 'v3.community.didDelete', filter);
|
|
4377
|
-
};
|
|
5277
|
+
const onCommunityUserRemoved = (callback) => createCommunityMemberEventSubscriber('community.userRemoved', callback);
|
|
4378
5278
|
|
|
4379
5279
|
/**
|
|
4380
5280
|
* ```js
|
|
4381
|
-
* import {
|
|
4382
|
-
* const dispose =
|
|
5281
|
+
* import { onCommunityUserBanned } from '@amityco/ts-sdk'
|
|
5282
|
+
* const dispose = onCommunityUserBanned((community, member) => {
|
|
4383
5283
|
* // ...
|
|
4384
5284
|
* })
|
|
4385
5285
|
* ```
|
|
4386
5286
|
*
|
|
4387
|
-
* Fired when
|
|
5287
|
+
* Fired when a user has been banned in a {@link Amity.Community}
|
|
4388
5288
|
*
|
|
4389
5289
|
* @param callback The function to call when the event was fired
|
|
4390
5290
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4391
5291
|
*
|
|
4392
5292
|
* @category Community Events
|
|
4393
5293
|
*/
|
|
4394
|
-
const
|
|
4395
|
-
const client = getActiveClient();
|
|
4396
|
-
const filter = (payload) => {
|
|
4397
|
-
decoratePayload(payload);
|
|
4398
|
-
ingestInCache(payload);
|
|
4399
|
-
callback(payload.communities[0]);
|
|
4400
|
-
};
|
|
4401
|
-
return createEventSubscriber(client, 'onCommunityJoined', 'v3.community.didJoin', filter);
|
|
4402
|
-
};
|
|
5294
|
+
const onCommunityUserBanned = (callback) => createCommunityMemberEventSubscriber('community.userBanned', callback);
|
|
4403
5295
|
|
|
4404
5296
|
/**
|
|
4405
5297
|
* ```js
|
|
4406
|
-
* import {
|
|
4407
|
-
* const dispose =
|
|
5298
|
+
* import { onCommunityUserUnbanned } from '@amityco/ts-sdk'
|
|
5299
|
+
* const dispose = onCommunityUserUnbanned((community, member) => {
|
|
4408
5300
|
* // ...
|
|
4409
5301
|
* })
|
|
4410
5302
|
* ```
|
|
4411
5303
|
*
|
|
4412
|
-
* Fired when
|
|
5304
|
+
* Fired when a user has been unbanned in a {@link Amity.Community}
|
|
4413
5305
|
*
|
|
4414
5306
|
* @param callback The function to call when the event was fired
|
|
4415
5307
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4416
5308
|
*
|
|
4417
5309
|
* @category Community Events
|
|
4418
5310
|
*/
|
|
4419
|
-
const
|
|
4420
|
-
const client = getActiveClient();
|
|
4421
|
-
const filter = (payload) => {
|
|
4422
|
-
decoratePayload(payload);
|
|
4423
|
-
ingestInCache(payload);
|
|
4424
|
-
callback(payload.communities[0]);
|
|
4425
|
-
};
|
|
4426
|
-
return createEventSubscriber(client, 'onCommunityLeft', 'v3.community.didLeave', filter);
|
|
4427
|
-
};
|
|
5311
|
+
const onCommunityUserUnbanned = (callback) => createCommunityMemberEventSubscriber('community.userUnbanned', callback);
|
|
4428
5312
|
|
|
4429
5313
|
/**
|
|
4430
5314
|
* ```js
|
|
@@ -4448,19 +5332,22 @@ const observeCommunity = (communityId, callback) => {
|
|
|
4448
5332
|
log(`observeCommunity(tmpid: ${timestamp}) > listen`);
|
|
4449
5333
|
// wrapper function to make sure
|
|
4450
5334
|
const router = (result, action) => {
|
|
4451
|
-
var _a, _b
|
|
4452
|
-
// filter function
|
|
4453
|
-
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.communityId) !== communityId)
|
|
4454
|
-
return;
|
|
5335
|
+
var _a, _b;
|
|
4455
5336
|
if (callback instanceof Function)
|
|
4456
5337
|
return callback(result);
|
|
4457
5338
|
if (action !== 'onFetch')
|
|
4458
|
-
(
|
|
4459
|
-
(
|
|
5339
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, result);
|
|
5340
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, result);
|
|
5341
|
+
};
|
|
5342
|
+
const realtimeRouter = (result, action) => {
|
|
5343
|
+
var _a;
|
|
5344
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.communityId) !== communityId)
|
|
5345
|
+
return;
|
|
5346
|
+
router(result, action);
|
|
4460
5347
|
};
|
|
4461
5348
|
const disposers = [];
|
|
4462
|
-
disposers.push(onCommunityUpdated(data =>
|
|
4463
|
-
runQuery(createQuery(getCommunity, communityId), result =>
|
|
5349
|
+
disposers.push(onCommunityUpdated(data => realtimeRouter({ data }, 'onUpdate')), onCommunityDeleted(data => realtimeRouter({ data }, 'onDelete')), onCommunityJoined(data => realtimeRouter({ data }, 'onJoin')), onCommunityLeft(data => realtimeRouter({ data }, 'onLeft')), onCommunityUserAdded(data => realtimeRouter({ data }, 'onUserAdded')), onCommunityUserRemoved(data => realtimeRouter({ data }, 'onUserRemoved')), onCommunityUserBanned(data => realtimeRouter({ data }, 'onUserBanned')), onCommunityUserUnbanned(data => realtimeRouter({ data }, 'onUserUnbanned')));
|
|
5350
|
+
runQuery(createQuery(getCommunity, communityId), result => router(result, 'onFetch'));
|
|
4464
5351
|
return () => {
|
|
4465
5352
|
log(`observeCommunity(tmpid: ${timestamp}) > dispose`);
|
|
4466
5353
|
disposers.forEach(fn => fn());
|
|
@@ -4497,81 +5384,6 @@ const queryGlobalFeed = async (query) => {
|
|
|
4497
5384
|
return { data: toRecord(posts), nextPage, prevPage };
|
|
4498
5385
|
};
|
|
4499
5386
|
|
|
4500
|
-
/**
|
|
4501
|
-
* ```js
|
|
4502
|
-
* import { onPostCreated } from '@amityco/ts-sdk'
|
|
4503
|
-
* const dispose = onPostCreated(post => {
|
|
4504
|
-
* // ...
|
|
4505
|
-
* })
|
|
4506
|
-
* ```
|
|
4507
|
-
*
|
|
4508
|
-
* Fired when an {@link Amity.Post} has been created
|
|
4509
|
-
*
|
|
4510
|
-
* @param callback The function to call when the event was fired
|
|
4511
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4512
|
-
*
|
|
4513
|
-
* @category Post Events
|
|
4514
|
-
*/
|
|
4515
|
-
const onPostCreated = (callback) => {
|
|
4516
|
-
const client = getActiveClient();
|
|
4517
|
-
const filter = (payload) => {
|
|
4518
|
-
decoratePayload(payload);
|
|
4519
|
-
ingestInCache(payload);
|
|
4520
|
-
callback(payload.posts[0]);
|
|
4521
|
-
};
|
|
4522
|
-
return createEventSubscriber(client, 'message/onPostCreated', 'v3.post.didCreate', filter);
|
|
4523
|
-
};
|
|
4524
|
-
|
|
4525
|
-
/**
|
|
4526
|
-
* ```js
|
|
4527
|
-
* import { onPostUpdated } from '@amityco/ts-sdk'
|
|
4528
|
-
* const dispose = onPostUpdated(post => {
|
|
4529
|
-
* // ...
|
|
4530
|
-
* })
|
|
4531
|
-
* ```
|
|
4532
|
-
*
|
|
4533
|
-
* Fired when an {@link Amity.Post} has been updated
|
|
4534
|
-
*
|
|
4535
|
-
* @param callback The function to call when the event was fired
|
|
4536
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4537
|
-
*
|
|
4538
|
-
* @category Post Events
|
|
4539
|
-
*/
|
|
4540
|
-
const onPostUpdated = (callback) => {
|
|
4541
|
-
const client = getActiveClient();
|
|
4542
|
-
const filter = (payload) => {
|
|
4543
|
-
decoratePayload(payload);
|
|
4544
|
-
ingestInCache(payload);
|
|
4545
|
-
callback(payload.posts[0]);
|
|
4546
|
-
};
|
|
4547
|
-
return createEventSubscriber(client, 'message/onPostUpdated', 'v3.post.didUpdate', filter);
|
|
4548
|
-
};
|
|
4549
|
-
|
|
4550
|
-
/**
|
|
4551
|
-
* ```js
|
|
4552
|
-
* import { onPostDeleted } from '@amityco/ts-sdk'
|
|
4553
|
-
* const dispose = onPostDeleted(post => {
|
|
4554
|
-
* // ...
|
|
4555
|
-
* })
|
|
4556
|
-
* ```
|
|
4557
|
-
*
|
|
4558
|
-
* Fired when an {@link Amity.Post} has been deleted
|
|
4559
|
-
*
|
|
4560
|
-
* @param callback The function to call when the event was fired
|
|
4561
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4562
|
-
*
|
|
4563
|
-
* @category Post Events
|
|
4564
|
-
*/
|
|
4565
|
-
const onPostDeleted = (callback) => {
|
|
4566
|
-
const client = getActiveClient();
|
|
4567
|
-
const filter = (payload) => {
|
|
4568
|
-
decoratePayload(payload);
|
|
4569
|
-
ingestInCache(payload);
|
|
4570
|
-
callback(payload.posts[0]);
|
|
4571
|
-
};
|
|
4572
|
-
return createEventSubscriber(client, 'message/onPostDeleted', 'v3.post.didDelete', filter);
|
|
4573
|
-
};
|
|
4574
|
-
|
|
4575
5387
|
/**
|
|
4576
5388
|
* ```js
|
|
4577
5389
|
* import { observePosts } from '@amityco/ts-sdk'
|
|
@@ -4607,7 +5419,7 @@ const observePosts = (params, callback) => {
|
|
|
4607
5419
|
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, post);
|
|
4608
5420
|
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, post);
|
|
4609
5421
|
};
|
|
4610
|
-
disposers.push(onPostCreated(post => router(post, 'onCreate')), onPostUpdated(post => router(post, 'onUpdate')), onPostDeleted(post => router(post, 'onDelete')));
|
|
5422
|
+
disposers.push(onPostCreated(post => router(post, 'onCreate')), onPostUpdated(post => router(post, 'onUpdate')), onPostDeleted(post => router(post, 'onDelete')), onPostApproved(post => router(post, 'onApproved')), onPostDeclined(post => router(post, 'onDeclined')), onPostFlagged(post => router(post, 'onFlagged')), onPostUnflagged(post => router(post, 'onUnflagged')), onPostReactionAdded(post => router(post, 'onReactionAdded')), onPostReactionRemoved(post => router(post, 'onReactionRemoved')));
|
|
4611
5423
|
return () => {
|
|
4612
5424
|
log(`observePosts(tmpid: ${timestamp}) > dispose`);
|
|
4613
5425
|
disposers.forEach(fn => fn());
|
|
@@ -4636,100 +5448,28 @@ const observePost = (postId, callback, policy = 'cache_then_server') => {
|
|
|
4636
5448
|
log(`observePost(tmpid: ${timestamp}) > listen`);
|
|
4637
5449
|
// wrapper function to make sure
|
|
4638
5450
|
const router = (result, action) => {
|
|
4639
|
-
var _a, _b
|
|
4640
|
-
// filter function
|
|
4641
|
-
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.postId) !== postId)
|
|
4642
|
-
return;
|
|
5451
|
+
var _a, _b;
|
|
4643
5452
|
if (callback instanceof Function)
|
|
4644
5453
|
return callback(result);
|
|
4645
5454
|
if (action !== 'onFetch')
|
|
4646
|
-
(
|
|
4647
|
-
(
|
|
5455
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, result);
|
|
5456
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, result);
|
|
5457
|
+
};
|
|
5458
|
+
const realtimeRouter = (result, action) => {
|
|
5459
|
+
var _a;
|
|
5460
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.postId) !== postId)
|
|
5461
|
+
return;
|
|
5462
|
+
router(result, action);
|
|
4648
5463
|
};
|
|
4649
5464
|
const disposers = [];
|
|
4650
|
-
disposers.push(onPostUpdated(data =>
|
|
4651
|
-
runQuery(createQuery(getPost, postId), (result) =>
|
|
5465
|
+
disposers.push(onPostUpdated(data => realtimeRouter({ data }, 'onUpdate')), onPostDeleted(data => realtimeRouter({ data }, 'onDelete')), onPostApproved(data => realtimeRouter({ data }, 'onApproved')), onPostDeclined(data => realtimeRouter({ data }, 'onDeclined')), onPostFlagged(data => realtimeRouter({ data }, 'onFlagged')), onPostUnflagged(data => realtimeRouter({ data }, 'onUnflagged')), onPostReactionAdded(data => realtimeRouter({ data }, 'onReactionAdded')), onPostReactionRemoved(data => realtimeRouter({ data }, 'onReactionRemoved')));
|
|
5466
|
+
runQuery(createQuery(getPost, postId), (result) => router(result, 'onFetch'), queryOptions(policy));
|
|
4652
5467
|
return () => {
|
|
4653
5468
|
log(`observePosts(tmpid: ${timestamp}) > dispose`);
|
|
4654
5469
|
disposers.forEach(fn => fn());
|
|
4655
5470
|
};
|
|
4656
5471
|
};
|
|
4657
5472
|
|
|
4658
|
-
/**
|
|
4659
|
-
* ```js
|
|
4660
|
-
* import { onCommentCreated } from '@amityco/ts-sdk'
|
|
4661
|
-
* const dispose = onCommentCreated(comment => {
|
|
4662
|
-
* // ...
|
|
4663
|
-
* })
|
|
4664
|
-
* ```
|
|
4665
|
-
*
|
|
4666
|
-
* Fired when an {@link Amity.Comment} has been created
|
|
4667
|
-
*
|
|
4668
|
-
* @param callback The function to call when the event was fired
|
|
4669
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4670
|
-
*
|
|
4671
|
-
* @category Comment Events
|
|
4672
|
-
*/
|
|
4673
|
-
const onCommentCreated = (callback) => {
|
|
4674
|
-
const client = getActiveClient();
|
|
4675
|
-
const filter = (payload) => {
|
|
4676
|
-
decoratePayload(payload);
|
|
4677
|
-
ingestInCache(payload);
|
|
4678
|
-
callback(payload.comments[0]);
|
|
4679
|
-
};
|
|
4680
|
-
return createEventSubscriber(client, 'message/onCommentCreated', 'v3.comment.didCreate', filter);
|
|
4681
|
-
};
|
|
4682
|
-
|
|
4683
|
-
/**
|
|
4684
|
-
* ```js
|
|
4685
|
-
* import { onCommentUpdated } from '@amityco/ts-sdk'
|
|
4686
|
-
* const dispose = onCommentUpdated(comment => {
|
|
4687
|
-
* // ...
|
|
4688
|
-
* })
|
|
4689
|
-
* ```
|
|
4690
|
-
*
|
|
4691
|
-
* Fired when an {@link Amity.Comment} has been updated
|
|
4692
|
-
*
|
|
4693
|
-
* @param callback The function to call when the event was fired
|
|
4694
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4695
|
-
*
|
|
4696
|
-
* @category Comment Events
|
|
4697
|
-
*/
|
|
4698
|
-
const onCommentUpdated = (callback) => {
|
|
4699
|
-
const client = getActiveClient();
|
|
4700
|
-
const filter = (payload) => {
|
|
4701
|
-
decoratePayload(payload);
|
|
4702
|
-
ingestInCache(payload);
|
|
4703
|
-
callback(payload.comments[0]);
|
|
4704
|
-
};
|
|
4705
|
-
return createEventSubscriber(client, 'message/onCommentUpdated', 'v3.comment.didUpdate', filter);
|
|
4706
|
-
};
|
|
4707
|
-
|
|
4708
|
-
/**
|
|
4709
|
-
* ```js
|
|
4710
|
-
* import { onCommentDeleted } from '@amityco/ts-sdk'
|
|
4711
|
-
* const dispose = onCommentDeleted(comment => {
|
|
4712
|
-
* // ...
|
|
4713
|
-
* })
|
|
4714
|
-
* ```
|
|
4715
|
-
*
|
|
4716
|
-
* Fired when an {@link Amity.Comment} has been deleted
|
|
4717
|
-
*
|
|
4718
|
-
* @param callback The function to call when the event was fired
|
|
4719
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4720
|
-
*
|
|
4721
|
-
* @category Comment Events
|
|
4722
|
-
*/
|
|
4723
|
-
const onCommentDeleted = (callback) => {
|
|
4724
|
-
const client = getActiveClient();
|
|
4725
|
-
const filter = (payload) => {
|
|
4726
|
-
decoratePayload(payload);
|
|
4727
|
-
ingestInCache(payload);
|
|
4728
|
-
callback(payload.comments[0]);
|
|
4729
|
-
};
|
|
4730
|
-
return createEventSubscriber(client, 'message/onCommentDeleted', 'v3.comment.didDelete', filter);
|
|
4731
|
-
};
|
|
4732
|
-
|
|
4733
5473
|
/**
|
|
4734
5474
|
* ```js
|
|
4735
5475
|
* import { observeComments } from '@amityco/ts-sdk'
|
|
@@ -4760,7 +5500,7 @@ const observeComments = (postId, callback) => {
|
|
|
4760
5500
|
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, comment);
|
|
4761
5501
|
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, comment);
|
|
4762
5502
|
};
|
|
4763
|
-
disposers.push(onCommentCreated(comment => router(comment, 'onCreate')), onCommentUpdated(comment => router(comment, 'onUpdate')), onCommentDeleted(comment => router(comment, 'onDelete')));
|
|
5503
|
+
disposers.push(onCommentCreated(comment => router(comment, 'onCreate')), onCommentUpdated(comment => router(comment, 'onUpdate')), onCommentDeleted(comment => router(comment, 'onDelete')), onCommentFlagged(comment => router(comment, 'onFlagged')), onCommentUnflagged(comment => router(comment, 'onUnflagged')), onCommentReactionAdded(comment => router(comment, 'onReactionAdded')), onCommentReactionRemoved(comment => router(comment, 'onReactionRemoved')));
|
|
4764
5504
|
return () => {
|
|
4765
5505
|
log(`observeComments(tmpid: ${timestamp}) > dispose`);
|
|
4766
5506
|
disposers.forEach(fn => fn());
|
|
@@ -4789,19 +5529,22 @@ const observeComment = (commentId, callback, policy = 'cache_then_server') => {
|
|
|
4789
5529
|
log(`observeComment(tmpid: ${timestamp}) > listen`);
|
|
4790
5530
|
// wrapper function to make sure
|
|
4791
5531
|
const router = (result, action) => {
|
|
4792
|
-
var _a, _b
|
|
4793
|
-
// filter function
|
|
4794
|
-
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.commentId) !== commentId)
|
|
4795
|
-
return;
|
|
5532
|
+
var _a, _b;
|
|
4796
5533
|
if (callback instanceof Function)
|
|
4797
5534
|
return callback(result);
|
|
4798
5535
|
if (action !== 'onFetch')
|
|
4799
|
-
(
|
|
4800
|
-
(
|
|
5536
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, result);
|
|
5537
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, result);
|
|
5538
|
+
};
|
|
5539
|
+
const realtimeRouter = (result, action) => {
|
|
5540
|
+
var _a;
|
|
5541
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.commentId) !== commentId)
|
|
5542
|
+
return;
|
|
5543
|
+
router(result, action);
|
|
4801
5544
|
};
|
|
4802
5545
|
const disposers = [];
|
|
4803
|
-
disposers.push(onCommentUpdated(data =>
|
|
4804
|
-
runQuery(createQuery(getComment, commentId), result =>
|
|
5546
|
+
disposers.push(onCommentUpdated(data => realtimeRouter({ data }, 'onUpdate')), onCommentDeleted(data => realtimeRouter({ data }, 'onDelete')), onCommentFlagged(data => realtimeRouter({ data }, 'onFlagged')), onCommentUnflagged(data => realtimeRouter({ data }, 'onUnflagged')), onCommentReactionAdded(data => realtimeRouter({ data }, 'onReactionAdded')), onCommentReactionRemoved(data => realtimeRouter({ data }, 'onReactionRemoved')));
|
|
5547
|
+
runQuery(createQuery(getComment, commentId), result => router(result, 'onFetch'), queryOptions(policy));
|
|
4805
5548
|
return () => {
|
|
4806
5549
|
log(`observeComment(tmpid: ${timestamp}) > dispose`);
|
|
4807
5550
|
disposers.forEach(fn => fn());
|
|
@@ -5001,6 +5744,7 @@ const disposeStream = async (streamId) => {
|
|
|
5001
5744
|
return videoStreamings.find(stream => stream.streamId === streamId);
|
|
5002
5745
|
};
|
|
5003
5746
|
|
|
5747
|
+
exports.VERSION = VERSION$1;
|
|
5004
5748
|
exports.addChannelMembers = addChannelMembers;
|
|
5005
5749
|
exports.addChannelMembersRole = addChannelMembersRole;
|
|
5006
5750
|
exports.addCommunityMembers = addCommunityMembers;
|
|
@@ -5040,17 +5784,21 @@ exports.getActiveClient = getActiveClient;
|
|
|
5040
5784
|
exports.getChannel = getChannel;
|
|
5041
5785
|
exports.getChannels = getChannels;
|
|
5042
5786
|
exports.getComment = getComment;
|
|
5787
|
+
exports.getCommentTopic = getCommentTopic;
|
|
5043
5788
|
exports.getComments = getComments;
|
|
5044
5789
|
exports.getCommunities = getCommunities;
|
|
5045
5790
|
exports.getCommunity = getCommunity;
|
|
5791
|
+
exports.getCommunityTopic = getCommunityTopic;
|
|
5046
5792
|
exports.getFile = getFile;
|
|
5047
5793
|
exports.getMessage = getMessage;
|
|
5048
5794
|
exports.getMessages = getMessages;
|
|
5049
5795
|
exports.getPost = getPost;
|
|
5796
|
+
exports.getPostTopic = getPostTopic;
|
|
5050
5797
|
exports.getPosts = getPosts;
|
|
5051
5798
|
exports.getRole = getRole;
|
|
5052
5799
|
exports.getStream = getStream;
|
|
5053
5800
|
exports.getUser = getUser;
|
|
5801
|
+
exports.getUserTopic = getUserTopic;
|
|
5054
5802
|
exports.getUsers = getUsers;
|
|
5055
5803
|
exports.ingestInCache = ingestInCache;
|
|
5056
5804
|
exports.isAfterBefore = isAfterBefore;
|
|
@@ -5091,22 +5839,39 @@ exports.onClientBanned = onClientBanned;
|
|
|
5091
5839
|
exports.onClientDisconnected = onClientDisconnected;
|
|
5092
5840
|
exports.onCommentCreated = onCommentCreated;
|
|
5093
5841
|
exports.onCommentDeleted = onCommentDeleted;
|
|
5842
|
+
exports.onCommentFlagged = onCommentFlagged;
|
|
5843
|
+
exports.onCommentReactionAdded = onCommentReactionAdded;
|
|
5844
|
+
exports.onCommentReactionRemoved = onCommentReactionRemoved;
|
|
5845
|
+
exports.onCommentUnflagged = onCommentUnflagged;
|
|
5094
5846
|
exports.onCommentUpdated = onCommentUpdated;
|
|
5095
5847
|
exports.onCommunityCreated = onCommunityCreated;
|
|
5096
5848
|
exports.onCommunityDeleted = onCommunityDeleted;
|
|
5097
5849
|
exports.onCommunityJoined = onCommunityJoined;
|
|
5098
5850
|
exports.onCommunityLeft = onCommunityLeft;
|
|
5099
5851
|
exports.onCommunityUpdated = onCommunityUpdated;
|
|
5852
|
+
exports.onCommunityUserAdded = onCommunityUserAdded;
|
|
5853
|
+
exports.onCommunityUserBanned = onCommunityUserBanned;
|
|
5854
|
+
exports.onCommunityUserRemoved = onCommunityUserRemoved;
|
|
5855
|
+
exports.onCommunityUserUnbanned = onCommunityUserUnbanned;
|
|
5100
5856
|
exports.onConnectionError = onConnectionError;
|
|
5101
5857
|
exports.onMemberAdded = onMemberAdded;
|
|
5102
5858
|
exports.onMemberRemoved = onMemberRemoved;
|
|
5103
5859
|
exports.onMessageCreated = onMessageCreated;
|
|
5104
5860
|
exports.onMessageDeleted = onMessageDeleted;
|
|
5105
5861
|
exports.onMessageUpdated = onMessageUpdated;
|
|
5862
|
+
exports.onPostApproved = onPostApproved;
|
|
5106
5863
|
exports.onPostCreated = onPostCreated;
|
|
5864
|
+
exports.onPostDeclined = onPostDeclined;
|
|
5107
5865
|
exports.onPostDeleted = onPostDeleted;
|
|
5866
|
+
exports.onPostFlagged = onPostFlagged;
|
|
5867
|
+
exports.onPostReactionAdded = onPostReactionAdded;
|
|
5868
|
+
exports.onPostReactionRemoved = onPostReactionRemoved;
|
|
5869
|
+
exports.onPostUnflagged = onPostUnflagged;
|
|
5108
5870
|
exports.onPostUpdated = onPostUpdated;
|
|
5109
5871
|
exports.onReactionRemoved = onReactionRemoved;
|
|
5872
|
+
exports.onUserFlagCleared = onUserFlagCleared;
|
|
5873
|
+
exports.onUserFlagged = onUserFlagged;
|
|
5874
|
+
exports.onUserUnflagged = onUserUnflagged;
|
|
5110
5875
|
exports.onUserUpdated = onUserUpdated;
|
|
5111
5876
|
exports.persistentStore = persistentStore;
|
|
5112
5877
|
exports.pullFromCache = pullFromCache;
|
|
@@ -5136,6 +5901,7 @@ exports.sortByFirstCreated = sortByFirstCreated;
|
|
|
5136
5901
|
exports.sortByLastActivity = sortByLastActivity;
|
|
5137
5902
|
exports.sortByLastCreated = sortByLastCreated;
|
|
5138
5903
|
exports.sortBySegmentNumber = sortBySegmentNumber;
|
|
5904
|
+
exports.subscribeTopic = subscribeTopic;
|
|
5139
5905
|
exports.syncInCache = syncInCache;
|
|
5140
5906
|
exports.toPage = toPage;
|
|
5141
5907
|
exports.toToken = toToken;
|