@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.esm.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
import isMatchWith from 'lodash.ismatchwith';
|
|
1
2
|
import { encode, decode } from 'js-base64';
|
|
2
3
|
import nativeIdGenerator from 'react-native-uuid';
|
|
3
4
|
import debug from 'debug';
|
|
4
5
|
import axios from 'axios';
|
|
5
6
|
import HttpAgent, { HttpsAgent } from 'agentkeepalive';
|
|
6
7
|
import io from 'socket.io-client';
|
|
8
|
+
import mqtt from 'mqtt';
|
|
7
9
|
import mitt from 'mitt';
|
|
8
10
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
9
11
|
|
|
12
|
+
var _a;
|
|
13
|
+
// used in createClient
|
|
14
|
+
const VERSION$1 = (_a = 'v0.0.1-beta.9-esm') !== null && _a !== void 0 ? _a : '__dev__';
|
|
15
|
+
|
|
10
16
|
/**
|
|
11
17
|
* Generic ASC error
|
|
12
18
|
* @category Errors
|
|
@@ -272,24 +278,6 @@ const syncInCache = (original, replacement) => {
|
|
|
272
278
|
pushToCache(name, replacement);
|
|
273
279
|
};
|
|
274
280
|
|
|
275
|
-
/**
|
|
276
|
-
* Ingest a payload (v3) into the cache
|
|
277
|
-
*
|
|
278
|
-
* @param payload a "backend v3" payload object
|
|
279
|
-
* @param order the order of the keys to process (in case of need)
|
|
280
|
-
*
|
|
281
|
-
* @category Cache
|
|
282
|
-
*/
|
|
283
|
-
const ingestInCache = (payload = {}, order = Object.keys(payload)) => {
|
|
284
|
-
order
|
|
285
|
-
.forEach(key => {
|
|
286
|
-
const name = storeInterests[key];
|
|
287
|
-
if (!name)
|
|
288
|
-
return;
|
|
289
|
-
pushToCache(name, ...payload[key]);
|
|
290
|
-
});
|
|
291
|
-
};
|
|
292
|
-
|
|
293
281
|
/**
|
|
294
282
|
* Apply partial mutation to objects in the cache
|
|
295
283
|
*
|
|
@@ -321,6 +309,58 @@ const mergeInCache = (name, mutation) => {
|
|
|
321
309
|
mutateCache(name, key, model => (Object.assign(Object.assign({}, model), mutation)));
|
|
322
310
|
};
|
|
323
311
|
|
|
312
|
+
/**
|
|
313
|
+
* converts an array looking collection to an object collection, grouped by localId
|
|
314
|
+
*
|
|
315
|
+
* @param array array of {@link Amity.Syncable}
|
|
316
|
+
* @returns array converted to object, grouped by localId
|
|
317
|
+
*
|
|
318
|
+
* @hidden
|
|
319
|
+
*/
|
|
320
|
+
const toRecord = (array) => (array.reduce((acc, item) => (Object.assign(Object.assign({}, acc), { [item.localId]: item })), {}));
|
|
321
|
+
|
|
322
|
+
/** @hidden */
|
|
323
|
+
const isMatch = (model1, model2) => {
|
|
324
|
+
const customizer = (oldValue, newValue, key) => {
|
|
325
|
+
if (key === 'syncedAt') {
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
return isMatchWith(model1, model2, customizer);
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Ingest a payload (v3) into the cache
|
|
334
|
+
*
|
|
335
|
+
* @param payload a "backend v3" payload object
|
|
336
|
+
* @param order the order of the keys to process (in case of need)
|
|
337
|
+
*
|
|
338
|
+
* @category Cache
|
|
339
|
+
*/
|
|
340
|
+
const ingestInCache = (payload = {}, order = Object.keys(payload)) => {
|
|
341
|
+
order
|
|
342
|
+
.forEach(key => {
|
|
343
|
+
const name = storeInterests[key];
|
|
344
|
+
if (!name)
|
|
345
|
+
return;
|
|
346
|
+
payload[key].forEach(model => {
|
|
347
|
+
var _a;
|
|
348
|
+
const [cached] = (_a = pullFromCache(name, getResolver(name)(model))) !== null && _a !== void 0 ? _a : [];
|
|
349
|
+
if (cached) {
|
|
350
|
+
// > instead of >= - some updates like reactions do not change updatedAt
|
|
351
|
+
if (cached.updatedAt && cached.updatedAt > model.updatedAt) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
// personal fields like isJoined, myReactions are missed from real time event payload
|
|
355
|
+
mergeInCache(name, model);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
pushToCache(name, model);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
};
|
|
363
|
+
|
|
324
364
|
/**
|
|
325
365
|
* Creates a cache instance. The cache instance will handle stores
|
|
326
366
|
* given to it as input, and will be used as the first source of cached data
|
|
@@ -408,6 +448,11 @@ const toToken = (paging, style) => {
|
|
|
408
448
|
first: paging.limit,
|
|
409
449
|
};
|
|
410
450
|
}
|
|
451
|
+
else if (!isNaN(paging === null || paging === void 0 ? void 0 : paging.limit)) {
|
|
452
|
+
payload = {
|
|
453
|
+
last: paging.limit,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
411
456
|
}
|
|
412
457
|
// avoid sending {} as it seems backend flips when we do
|
|
413
458
|
if (!Object.keys(payload).length)
|
|
@@ -746,6 +791,45 @@ const createLogger = (session) => {
|
|
|
746
791
|
return print;
|
|
747
792
|
};
|
|
748
793
|
|
|
794
|
+
const API_ENDPOINT = {
|
|
795
|
+
EU: 'eu',
|
|
796
|
+
SG: 'sg',
|
|
797
|
+
US: 'us',
|
|
798
|
+
};
|
|
799
|
+
var UrlTemplate;
|
|
800
|
+
(function (UrlTemplate) {
|
|
801
|
+
UrlTemplate["HTTP"] = "https://api.{region}.amity.co";
|
|
802
|
+
UrlTemplate["MQTT"] = "wss://sse.{region}.amity.co:443/mqtt";
|
|
803
|
+
})(UrlTemplate || (UrlTemplate = {}));
|
|
804
|
+
const getUrlFromTemplate = (template, region) => {
|
|
805
|
+
return template.replace('{region}', region);
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* Unwraps v3 server payload (for error handling and type extraction)
|
|
810
|
+
*
|
|
811
|
+
* @param response A typical "v3" response (related to backend's service strategy)
|
|
812
|
+
*
|
|
813
|
+
* @returns The data returned by the backend
|
|
814
|
+
* @throws An error related to backend's rejection
|
|
815
|
+
*
|
|
816
|
+
* @category Transport
|
|
817
|
+
* @hidden
|
|
818
|
+
*/
|
|
819
|
+
const unwrapPayload = (response) => {
|
|
820
|
+
if (response.status === 'success') {
|
|
821
|
+
return response.data;
|
|
822
|
+
}
|
|
823
|
+
else if (response.status === 'fail') {
|
|
824
|
+
throw new ASCUnknownError(response.code);
|
|
825
|
+
}
|
|
826
|
+
else if (response.status === 'error') {
|
|
827
|
+
throw new ASCApiError(response.message, response.code, "error" /* ERROR */);
|
|
828
|
+
}
|
|
829
|
+
// unjust, but helps force uncast "T | undefined" to allow deconstruction without warnings
|
|
830
|
+
return undefined;
|
|
831
|
+
};
|
|
832
|
+
|
|
749
833
|
/**
|
|
750
834
|
* Axios param serializer (for querystring)
|
|
751
835
|
* @hidden
|
|
@@ -774,7 +858,7 @@ const stringify = (params, prefix) => {
|
|
|
774
858
|
* @category Transport
|
|
775
859
|
* @hidden
|
|
776
860
|
*/
|
|
777
|
-
const createHttpTransport = (endpoint
|
|
861
|
+
const createHttpTransport = (endpoint) => {
|
|
778
862
|
const options = {
|
|
779
863
|
maxSockets: 100,
|
|
780
864
|
maxFreeSockets: 10,
|
|
@@ -795,7 +879,8 @@ const createHttpTransport = (endpoint = 'https://api.amity.co') => {
|
|
|
795
879
|
unwrapPayload(response.data);
|
|
796
880
|
});
|
|
797
881
|
return instance;
|
|
798
|
-
};
|
|
882
|
+
};
|
|
883
|
+
|
|
799
884
|
/**
|
|
800
885
|
* Creates a pre-configured socket.io instance
|
|
801
886
|
*
|
|
@@ -805,42 +890,19 @@ const createHttpTransport = (endpoint = 'https://api.amity.co') => {
|
|
|
805
890
|
* @category Transport
|
|
806
891
|
* @hidden
|
|
807
892
|
*/
|
|
808
|
-
const createWebsocketTransport = (endpoint
|
|
893
|
+
const createWebsocketTransport = (endpoint) => {
|
|
809
894
|
const socket = io(endpoint, {
|
|
810
895
|
autoConnect: false,
|
|
811
896
|
reconnectionDelay: 2000,
|
|
812
897
|
transports: ['websocket'],
|
|
813
898
|
});
|
|
899
|
+
// FIXME: disconnectClient removes this listener and client can get global ban event
|
|
814
900
|
socket.on('disconnect', (reason) => {
|
|
815
901
|
if (reason === 'io server disconnect')
|
|
816
902
|
socket.connect();
|
|
817
903
|
});
|
|
818
904
|
return socket;
|
|
819
905
|
};
|
|
820
|
-
/**
|
|
821
|
-
* Unwraps v3 server payload (for error handling and type extraction)
|
|
822
|
-
*
|
|
823
|
-
* @param response A typical "v3" response (related to backend's service strategy)
|
|
824
|
-
*
|
|
825
|
-
* @returns The data returned by the backend
|
|
826
|
-
* @throws An error related to backend's rejection
|
|
827
|
-
*
|
|
828
|
-
* @category Transport
|
|
829
|
-
* @hidden
|
|
830
|
-
*/
|
|
831
|
-
const unwrapPayload = (response) => {
|
|
832
|
-
if (response.status === 'success') {
|
|
833
|
-
return response.data;
|
|
834
|
-
}
|
|
835
|
-
else if (response.status === 'fail') {
|
|
836
|
-
throw new ASCUnknownError(response.code);
|
|
837
|
-
}
|
|
838
|
-
else if (response.status === 'error') {
|
|
839
|
-
throw new ASCApiError(response.message, response.code, "error" /* ERROR */);
|
|
840
|
-
}
|
|
841
|
-
// unjust, but helps force uncast "T | undefined" to allow deconstruction without warnings
|
|
842
|
-
return undefined;
|
|
843
|
-
};
|
|
844
906
|
/**
|
|
845
907
|
* Promisify a websocket event emission - resulting in a synchronous http-like XHR (ws legacy)
|
|
846
908
|
*
|
|
@@ -870,14 +932,131 @@ const synchronousWSCall = async (client, event, data) => {
|
|
|
870
932
|
return value;
|
|
871
933
|
};
|
|
872
934
|
|
|
935
|
+
const QOS_FAILURE_CODE = 128;
|
|
936
|
+
const RETRY_BASE_TIMEOUT = 1000;
|
|
937
|
+
const RETRY_COUNT_MAX = 10;
|
|
938
|
+
var MqttError;
|
|
939
|
+
(function (MqttError) {
|
|
940
|
+
MqttError[MqttError["IDENTIFIER_REJECTED"] = 2] = "IDENTIFIER_REJECTED";
|
|
941
|
+
MqttError[MqttError["BAD_USERNAME_OR_PASSWORD"] = 4] = "BAD_USERNAME_OR_PASSWORD";
|
|
942
|
+
MqttError[MqttError["NOT_AUTHORIZED"] = 5] = "NOT_AUTHORIZED";
|
|
943
|
+
})(MqttError || (MqttError = {}));
|
|
944
|
+
function getMqttOptions(params) {
|
|
945
|
+
return Object.assign({ clean: false, clientId: `mqttjs_ + ${Math.random().toString(16).substr(2, 8)}`, protocolId: 'MQTT', protocolVersion: 4, reconnectPeriod: 0, will: {
|
|
946
|
+
topic: 'WillMsg',
|
|
947
|
+
payload: 'Connection Closed abnormally..!',
|
|
948
|
+
qos: 0,
|
|
949
|
+
retain: false
|
|
950
|
+
} }, params);
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* Creates a pre-configured socket.io instance
|
|
954
|
+
*
|
|
955
|
+
* @param endpoint The mqtt server's URL
|
|
956
|
+
* @returns A pre-configured (non-connected) mqtt client instance
|
|
957
|
+
*
|
|
958
|
+
* @category Transport
|
|
959
|
+
* @hidden
|
|
960
|
+
*/
|
|
961
|
+
const createMqttTransport = (endpoint) => {
|
|
962
|
+
let mqttClient;
|
|
963
|
+
let retryCount = 0;
|
|
964
|
+
let allowReconnection = true;
|
|
965
|
+
async function connect(params) {
|
|
966
|
+
if (mqttClient) {
|
|
967
|
+
mqttClient.removeAllListeners();
|
|
968
|
+
mqttClient.end(true);
|
|
969
|
+
}
|
|
970
|
+
mqttClient = mqtt.connect(endpoint, getMqttOptions({ username: params.userId, password: params.accessToken }));
|
|
971
|
+
mqttClient.on('connect', () => {
|
|
972
|
+
retryCount = 0;
|
|
973
|
+
allowReconnection = true;
|
|
974
|
+
});
|
|
975
|
+
mqttClient.on('error', (error) => {
|
|
976
|
+
switch (error.code) {
|
|
977
|
+
case 2 /* IDENTIFIER_REJECTED */:
|
|
978
|
+
case 4 /* BAD_USERNAME_OR_PASSWORD */:
|
|
979
|
+
case 5 /* NOT_AUTHORIZED */:
|
|
980
|
+
allowReconnection = false;
|
|
981
|
+
}
|
|
982
|
+
});
|
|
983
|
+
mqttClient.on('close', () => {
|
|
984
|
+
if (allowReconnection) {
|
|
985
|
+
reconnect();
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
return new Promise(resolve => mqttClient.once('connect', () => resolve()));
|
|
989
|
+
}
|
|
990
|
+
const reconnect = () => {
|
|
991
|
+
setTimeout(() => {
|
|
992
|
+
retryCount = retryCount + 1;
|
|
993
|
+
mqttClient.reconnect();
|
|
994
|
+
}, RETRY_BASE_TIMEOUT * Math.pow(2, retryCount > RETRY_COUNT_MAX ? RETRY_COUNT_MAX : retryCount));
|
|
995
|
+
};
|
|
996
|
+
return {
|
|
997
|
+
connect,
|
|
998
|
+
async disconnect() {
|
|
999
|
+
allowReconnection = false;
|
|
1000
|
+
if (this.connected) {
|
|
1001
|
+
return new Promise(resolve => mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.end(true, undefined, () => resolve()));
|
|
1002
|
+
}
|
|
1003
|
+
},
|
|
1004
|
+
get connected() {
|
|
1005
|
+
return !!(mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.connected);
|
|
1006
|
+
},
|
|
1007
|
+
on(event, handler) {
|
|
1008
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.on(event, handler);
|
|
1009
|
+
},
|
|
1010
|
+
once(event, handler) {
|
|
1011
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.once(event, handler);
|
|
1012
|
+
},
|
|
1013
|
+
off(event, handler) {
|
|
1014
|
+
if (handler !== undefined) {
|
|
1015
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.off(event, handler);
|
|
1016
|
+
}
|
|
1017
|
+
else {
|
|
1018
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.removeAllListeners(event);
|
|
1019
|
+
}
|
|
1020
|
+
},
|
|
1021
|
+
removeAllListeners() {
|
|
1022
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.removeAllListeners();
|
|
1023
|
+
},
|
|
1024
|
+
subscribe(topic, callback) {
|
|
1025
|
+
const callbackWrapper = (error, granted) => {
|
|
1026
|
+
if (error || granted[0].qos === QOS_FAILURE_CODE) {
|
|
1027
|
+
const ascError = error
|
|
1028
|
+
? new ASCError(error.message, 800000 /* UNKNOWN_ERROR */, "error" /* ERROR */)
|
|
1029
|
+
// TODO throw the actual error, once BE can tell us the actual error code
|
|
1030
|
+
: new ASCUnknownError(800000 /* UNKNOWN_ERROR */, "error" /* ERROR */);
|
|
1031
|
+
console.error(`Failed to subscribe to topic ${topic}`, ascError);
|
|
1032
|
+
callback === null || callback === void 0 ? void 0 : callback(ascError);
|
|
1033
|
+
}
|
|
1034
|
+
else {
|
|
1035
|
+
console.log(`Subscribed to topic ${granted[0].topic}`);
|
|
1036
|
+
callback === null || callback === void 0 ? void 0 : callback();
|
|
1037
|
+
}
|
|
1038
|
+
};
|
|
1039
|
+
if (mqttClient) {
|
|
1040
|
+
mqttClient.subscribe(topic, { qos: 0 }, callbackWrapper);
|
|
1041
|
+
}
|
|
1042
|
+
else {
|
|
1043
|
+
callbackWrapper(new Error('No connection to broker'), []);
|
|
1044
|
+
}
|
|
1045
|
+
return () => mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.unsubscribe(topic);
|
|
1046
|
+
},
|
|
1047
|
+
unsubscribe(topic) {
|
|
1048
|
+
mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.unsubscribe(topic);
|
|
1049
|
+
}
|
|
1050
|
+
};
|
|
1051
|
+
};
|
|
1052
|
+
|
|
873
1053
|
// need to sync up the list
|
|
874
|
-
const
|
|
1054
|
+
const WS_EVENTS = [
|
|
875
1055
|
'disconnected',
|
|
876
1056
|
'error',
|
|
877
1057
|
'connect_error',
|
|
878
1058
|
'reconnect_error',
|
|
879
1059
|
'reconnect_failed',
|
|
880
|
-
'v3.user.didUpdate',
|
|
881
1060
|
'v3.channel.didUpdate',
|
|
882
1061
|
'v3.channel.didUpdate',
|
|
883
1062
|
'v3.channel.didJoin',
|
|
@@ -890,6 +1069,15 @@ const EVENTS$1 = [
|
|
|
890
1069
|
'v3.message.didUpdate',
|
|
891
1070
|
'v3.message.didDelete',
|
|
892
1071
|
];
|
|
1072
|
+
const MQTT_EVENTS = [
|
|
1073
|
+
'connect',
|
|
1074
|
+
'message',
|
|
1075
|
+
'disconnect',
|
|
1076
|
+
'error',
|
|
1077
|
+
'close',
|
|
1078
|
+
'end',
|
|
1079
|
+
'reconnect'
|
|
1080
|
+
];
|
|
893
1081
|
/** @hidden */
|
|
894
1082
|
const createEventEmitter = () => {
|
|
895
1083
|
return mitt();
|
|
@@ -905,10 +1093,20 @@ const createEventEmitter = () => {
|
|
|
905
1093
|
* @hidden
|
|
906
1094
|
*/
|
|
907
1095
|
const proxyWebsocketEvents = (ws, emitter) => {
|
|
908
|
-
|
|
1096
|
+
WS_EVENTS.forEach(event => {
|
|
909
1097
|
ws.on(event, (param) => emitter.emit(event, param));
|
|
910
1098
|
});
|
|
911
1099
|
};
|
|
1100
|
+
const proxyMqttEvents = (mqttClient, emitter) => {
|
|
1101
|
+
MQTT_EVENTS.forEach(event => {
|
|
1102
|
+
mqttClient.on(event, (...params) => emitter.emit(event, params.length === 1 ? params[0] : params));
|
|
1103
|
+
});
|
|
1104
|
+
// @ts-ignore
|
|
1105
|
+
mqttClient.on('message', (topic, payload) => {
|
|
1106
|
+
const message = JSON.parse(payload.toString());
|
|
1107
|
+
emitter.emit(message.eventType, message.data);
|
|
1108
|
+
});
|
|
1109
|
+
};
|
|
912
1110
|
/**
|
|
913
1111
|
* Standardize the subscription of SSE through web sockets
|
|
914
1112
|
*
|
|
@@ -960,17 +1158,24 @@ const fireEvent = (event, payload) => {
|
|
|
960
1158
|
*
|
|
961
1159
|
* @category Client API
|
|
962
1160
|
**/
|
|
963
|
-
const createClient = (apiKey, apiEndpoint =
|
|
1161
|
+
const createClient = (apiKey, apiEndpoint = API_ENDPOINT.SG, debugSession = 'amity') => {
|
|
964
1162
|
const log = createLogger(debugSession);
|
|
965
1163
|
log('client/api/createClient', {
|
|
966
1164
|
apiKey: apiKey.replace(/.{5}$/g, 'xxxxx'),
|
|
967
1165
|
apiEndpoint,
|
|
968
1166
|
});
|
|
969
|
-
const http = createHttpTransport(apiEndpoint);
|
|
970
|
-
const ws = createWebsocketTransport(apiEndpoint);
|
|
1167
|
+
const http = createHttpTransport(getUrlFromTemplate(UrlTemplate.HTTP, apiEndpoint));
|
|
1168
|
+
const ws = createWebsocketTransport(getUrlFromTemplate(UrlTemplate.HTTP, apiEndpoint));
|
|
1169
|
+
const mqtt = createMqttTransport(getUrlFromTemplate(UrlTemplate.MQTT, apiEndpoint));
|
|
971
1170
|
const emitter = createEventEmitter();
|
|
972
1171
|
const client = {
|
|
973
|
-
|
|
1172
|
+
version: `${VERSION$1}`,
|
|
1173
|
+
apiKey,
|
|
1174
|
+
log,
|
|
1175
|
+
http,
|
|
1176
|
+
ws,
|
|
1177
|
+
mqtt,
|
|
1178
|
+
emitter,
|
|
974
1179
|
use: () => setActiveClient(client),
|
|
975
1180
|
};
|
|
976
1181
|
try {
|
|
@@ -982,6 +1187,38 @@ const createClient = (apiKey, apiEndpoint = 'https://api.amity.co', debugSession
|
|
|
982
1187
|
return client;
|
|
983
1188
|
};
|
|
984
1189
|
|
|
1190
|
+
/**
|
|
1191
|
+
* ```js
|
|
1192
|
+
* import { disconnectClient } from '@amityco/ts-sdk'
|
|
1193
|
+
* const success = await disconnectClient()
|
|
1194
|
+
* ```
|
|
1195
|
+
*
|
|
1196
|
+
* Disconnects an {@link Amity.Client} instance from ASC servers
|
|
1197
|
+
*
|
|
1198
|
+
* @param client The {@link Amity.Client} instance to disconnect
|
|
1199
|
+
* @returns a success boolean if disconnected
|
|
1200
|
+
*
|
|
1201
|
+
* @category Client API
|
|
1202
|
+
* @async
|
|
1203
|
+
*/
|
|
1204
|
+
const disconnectClient = async () => {
|
|
1205
|
+
const client = getActiveClient();
|
|
1206
|
+
client.log('client/api/disconnectClient');
|
|
1207
|
+
await new Promise(resolve => {
|
|
1208
|
+
client.ws.once('disconnect', resolve);
|
|
1209
|
+
client.ws.disconnect();
|
|
1210
|
+
client.mqtt.disconnect();
|
|
1211
|
+
});
|
|
1212
|
+
client.emitter.all.clear();
|
|
1213
|
+
// FIXME: it removes listener in ws.ts, it breaks global ban event
|
|
1214
|
+
client.ws.removeAllListeners();
|
|
1215
|
+
client.mqtt.removeAllListeners();
|
|
1216
|
+
client.userId = undefined;
|
|
1217
|
+
client.user = undefined;
|
|
1218
|
+
client.accessToken = undefined;
|
|
1219
|
+
return !client.ws.connected;
|
|
1220
|
+
};
|
|
1221
|
+
|
|
985
1222
|
// TODO: find a way to uniquely identify the device
|
|
986
1223
|
// import { getStorage } from './storage'
|
|
987
1224
|
/** @hidden */
|
|
@@ -1032,11 +1269,89 @@ const getToken = async (params) => {
|
|
|
1032
1269
|
client.log('client/api/getToken', params);
|
|
1033
1270
|
const deviceInfo = await getDeviceInfo();
|
|
1034
1271
|
const { data } = await client.http.post('/v1/device', Object.assign(Object.assign({}, params), { deviceInfo }), {
|
|
1272
|
+
//@ts-ignore
|
|
1035
1273
|
headers: { 'X-API-Key': client.apiKey }
|
|
1036
1274
|
});
|
|
1037
1275
|
return data;
|
|
1038
1276
|
};
|
|
1039
1277
|
|
|
1278
|
+
const EVENTS = [
|
|
1279
|
+
'disconnected',
|
|
1280
|
+
'error',
|
|
1281
|
+
'connect_error',
|
|
1282
|
+
'reconnect_error',
|
|
1283
|
+
'reconnect_failed',
|
|
1284
|
+
];
|
|
1285
|
+
/**
|
|
1286
|
+
* ```js
|
|
1287
|
+
* import { onConnectionError } from '@amityco/ts-sdk'
|
|
1288
|
+
* const dispose = onClientBanned(() => {
|
|
1289
|
+
* // ...
|
|
1290
|
+
* })
|
|
1291
|
+
* ```
|
|
1292
|
+
*
|
|
1293
|
+
* Fired when any {@link Amity.Client} have been banned from the platform
|
|
1294
|
+
*
|
|
1295
|
+
* @param callback The function to call when the event was fired
|
|
1296
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1297
|
+
*
|
|
1298
|
+
* @category Client Events
|
|
1299
|
+
*/
|
|
1300
|
+
const onConnectionError = (callback) => {
|
|
1301
|
+
const client = getActiveClient();
|
|
1302
|
+
const subscribers = EVENTS.map(event => (createEventSubscriber(client, `client/onConnectionError(${event})`, event, (response) => {
|
|
1303
|
+
const error = (response && 'code' in response)
|
|
1304
|
+
? new ASCError(response.message, response.code, "fatal" /* FATAL */)
|
|
1305
|
+
: new ASCConnectionError(event);
|
|
1306
|
+
callback(error);
|
|
1307
|
+
})));
|
|
1308
|
+
return () => {
|
|
1309
|
+
subscribers.forEach(fn => fn());
|
|
1310
|
+
};
|
|
1311
|
+
};
|
|
1312
|
+
|
|
1313
|
+
/**
|
|
1314
|
+
* ```js
|
|
1315
|
+
* import { onClientDisconnected } from '@amityco/ts-sdk'
|
|
1316
|
+
* const dispose = onClientDisconnected(() => {
|
|
1317
|
+
* // ...
|
|
1318
|
+
* })
|
|
1319
|
+
* ```
|
|
1320
|
+
*
|
|
1321
|
+
* Fired when any {@link Amity.Client} have been disconnected
|
|
1322
|
+
*
|
|
1323
|
+
* @param callback The function to call when the event was fired
|
|
1324
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1325
|
+
*
|
|
1326
|
+
* @category Client Events
|
|
1327
|
+
*/
|
|
1328
|
+
const onClientDisconnected = (callback) => {
|
|
1329
|
+
return onConnectionError(({ code }) => {
|
|
1330
|
+
code === 800211 /* DISCONNECTED */ && callback();
|
|
1331
|
+
});
|
|
1332
|
+
};
|
|
1333
|
+
|
|
1334
|
+
/**
|
|
1335
|
+
* ```js
|
|
1336
|
+
* import { onClientBanned } from '@amityco/ts-sdk'
|
|
1337
|
+
* const dispose = onClientBanned(() => {
|
|
1338
|
+
* // ...
|
|
1339
|
+
* })
|
|
1340
|
+
* ```
|
|
1341
|
+
*
|
|
1342
|
+
* Fired when any {@link Amity.Client} have been banned from the platform
|
|
1343
|
+
*
|
|
1344
|
+
* @param callback The function to call when the event was fired
|
|
1345
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1346
|
+
*
|
|
1347
|
+
* @category Client Events
|
|
1348
|
+
*/
|
|
1349
|
+
const onClientBanned = (callback) => {
|
|
1350
|
+
return onConnectionError(({ code }) => {
|
|
1351
|
+
code === 400312 /* GLOBAL_BAN */ && callback();
|
|
1352
|
+
});
|
|
1353
|
+
};
|
|
1354
|
+
|
|
1040
1355
|
/**
|
|
1041
1356
|
* ```js
|
|
1042
1357
|
* import { connectClient } from '@amityco/ts-sdk/client/api'
|
|
@@ -1065,11 +1380,12 @@ const connectClient = async (params) => {
|
|
|
1065
1380
|
(_b = params.deviceId) !== null && _b !== void 0 ? _b : (params.deviceId = getDeviceId());
|
|
1066
1381
|
// @ts-ignore: in that particular case, typing can be
|
|
1067
1382
|
// ignored since we enforce default values just before.
|
|
1068
|
-
const { accessToken } = await getToken(params);
|
|
1383
|
+
const { accessToken, users } = await getToken(params);
|
|
1069
1384
|
// manually setup the token for http transport
|
|
1070
1385
|
client.http.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
|
|
1071
1386
|
// manually setup the token for ws transport
|
|
1072
1387
|
client.ws.io.opts.query = { token: accessToken };
|
|
1388
|
+
// FIXME: events are duplicated if connectClient is called few times without disconnectClient
|
|
1073
1389
|
// wire websocket events to our event emitter
|
|
1074
1390
|
proxyWebsocketEvents(client.ws, client.emitter);
|
|
1075
1391
|
// TODO: in phase 2, this should not be necessary. we should have WS
|
|
@@ -1078,44 +1394,23 @@ const connectClient = async (params) => {
|
|
|
1078
1394
|
client.ws.once('connect', resolve);
|
|
1079
1395
|
client.ws.open();
|
|
1080
1396
|
});
|
|
1397
|
+
const unsubscribe = onClientBanned(() => {
|
|
1398
|
+
disconnectClient();
|
|
1399
|
+
unsubscribe();
|
|
1400
|
+
});
|
|
1081
1401
|
client.userId = params.userId;
|
|
1402
|
+
client.user = users[0];
|
|
1403
|
+
client.accessToken = accessToken;
|
|
1082
1404
|
return client.ws.connected;
|
|
1083
1405
|
};
|
|
1084
1406
|
|
|
1085
1407
|
/**
|
|
1086
1408
|
* ```js
|
|
1087
|
-
* import {
|
|
1088
|
-
* const
|
|
1409
|
+
* import { isConnected } from '@amityco/ts-sdk'
|
|
1410
|
+
* const connected = isConnected()
|
|
1089
1411
|
* ```
|
|
1090
1412
|
*
|
|
1091
|
-
*
|
|
1092
|
-
*
|
|
1093
|
-
* @param client The {@link Amity.Client} instance to disconnect
|
|
1094
|
-
* @returns a success boolean if disconnected
|
|
1095
|
-
*
|
|
1096
|
-
* @category Client API
|
|
1097
|
-
* @async
|
|
1098
|
-
*/
|
|
1099
|
-
const disconnectClient = async () => {
|
|
1100
|
-
const client = getActiveClient();
|
|
1101
|
-
client.log('client/api/disconnectClient');
|
|
1102
|
-
await new Promise(resolve => {
|
|
1103
|
-
client.ws.once('disconnect', resolve);
|
|
1104
|
-
client.ws.disconnect();
|
|
1105
|
-
});
|
|
1106
|
-
client.emitter.all.clear();
|
|
1107
|
-
client.ws.removeAllListeners();
|
|
1108
|
-
client.userId = undefined;
|
|
1109
|
-
return !client.ws.connected;
|
|
1110
|
-
};
|
|
1111
|
-
|
|
1112
|
-
/**
|
|
1113
|
-
* ```js
|
|
1114
|
-
* import { isConnected } from '@amityco/ts-sdk'
|
|
1115
|
-
* const connected = isConnected()
|
|
1116
|
-
* ```
|
|
1117
|
-
*
|
|
1118
|
-
* Checks if a {@link Amity.Client} instance is connected to ASC servers
|
|
1413
|
+
* Checks if a {@link Amity.Client} instance is connected to ASC servers
|
|
1119
1414
|
*
|
|
1120
1415
|
* @returns a success boolean if connected
|
|
1121
1416
|
*
|
|
@@ -1265,16 +1560,6 @@ const clearCache = (stores = ['user', 'file', 'channel', 'message', 'community',
|
|
|
1265
1560
|
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(); });
|
|
1266
1561
|
};
|
|
1267
1562
|
|
|
1268
|
-
/**
|
|
1269
|
-
* converts an array looking collection to an object collection, grouped by localId
|
|
1270
|
-
*
|
|
1271
|
-
* @param array array of {@link Amity.Syncable}
|
|
1272
|
-
* @returns array converted to object, grouped by localId
|
|
1273
|
-
*
|
|
1274
|
-
* @hidden
|
|
1275
|
-
*/
|
|
1276
|
-
const toRecord = (array) => (array.reduce((acc, item) => (Object.assign(Object.assign({}, acc), { [item.localId]: item })), {}));
|
|
1277
|
-
|
|
1278
1563
|
/**
|
|
1279
1564
|
* Set the model's syncedAt property and localId
|
|
1280
1565
|
*
|
|
@@ -1360,82 +1645,51 @@ const sortByLastCreated = ({ createdAt: a }, { createdAt: b }) => new Date(b).va
|
|
|
1360
1645
|
*/
|
|
1361
1646
|
const sortByLastActivity = ({ lastActivity: a }, { lastActivity: b }) => new Date(b).valueOf() - new Date(a).valueOf();
|
|
1362
1647
|
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
];
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
const
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
* @param callback The function to call when the event was fired
|
|
1409
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1410
|
-
*
|
|
1411
|
-
* @category Client Events
|
|
1412
|
-
*/
|
|
1413
|
-
const onClientDisconnected = (callback) => {
|
|
1414
|
-
return onConnectionError(({ code }) => {
|
|
1415
|
-
code === 800211 /* DISCONNECTED */ && callback();
|
|
1416
|
-
});
|
|
1417
|
-
};
|
|
1418
|
-
|
|
1419
|
-
/**
|
|
1420
|
-
* ```js
|
|
1421
|
-
* import { onClientBanned } from '@amityco/ts-sdk'
|
|
1422
|
-
* const dispose = onClientBanned(() => {
|
|
1423
|
-
* // ...
|
|
1424
|
-
* })
|
|
1425
|
-
* ```
|
|
1426
|
-
*
|
|
1427
|
-
* Fired when any {@link Amity.Client} have been banned from the platform
|
|
1428
|
-
*
|
|
1429
|
-
* @param callback The function to call when the event was fired
|
|
1430
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1431
|
-
*
|
|
1432
|
-
* @category Client Events
|
|
1433
|
-
*/
|
|
1434
|
-
const onClientBanned = (callback) => {
|
|
1435
|
-
return onConnectionError(({ code }) => {
|
|
1436
|
-
code === 400312 /* GLOBAL_BAN */ && callback();
|
|
1437
|
-
});
|
|
1438
|
-
};
|
|
1648
|
+
var SubscriptionLevels;
|
|
1649
|
+
(function (SubscriptionLevels) {
|
|
1650
|
+
SubscriptionLevels["COMMUNITY"] = "community";
|
|
1651
|
+
SubscriptionLevels["POST"] = "post";
|
|
1652
|
+
SubscriptionLevels["COMMENT"] = "comment";
|
|
1653
|
+
SubscriptionLevels["POST_AND_COMMENT"] = "post_and_comment";
|
|
1654
|
+
SubscriptionLevels["USER"] = "user";
|
|
1655
|
+
})(SubscriptionLevels || (SubscriptionLevels = {}));
|
|
1656
|
+
const getCommunityUserTopic = ({ path }, level) => {
|
|
1657
|
+
switch (level) {
|
|
1658
|
+
case 'post':
|
|
1659
|
+
return `${path}/post/+`;
|
|
1660
|
+
case 'comment':
|
|
1661
|
+
return `${path}/post/+/comment/+`;
|
|
1662
|
+
case 'post_and_comment':
|
|
1663
|
+
return `${path}/post/#`;
|
|
1664
|
+
default:
|
|
1665
|
+
return path;
|
|
1666
|
+
}
|
|
1667
|
+
};
|
|
1668
|
+
const getCommunityTopic = ({ path }, level = SubscriptionLevels.COMMUNITY) => getCommunityUserTopic({ path }, level);
|
|
1669
|
+
const getUserTopic = ({ path }, level = SubscriptionLevels.USER) => getCommunityUserTopic({ path }, level);
|
|
1670
|
+
const getPostTopic = ({ path }, level = SubscriptionLevels.POST) => {
|
|
1671
|
+
switch (level) {
|
|
1672
|
+
case 'comment':
|
|
1673
|
+
return `${path}/comment/+`;
|
|
1674
|
+
default:
|
|
1675
|
+
return path;
|
|
1676
|
+
}
|
|
1677
|
+
};
|
|
1678
|
+
const getCommentTopic = ({ path }) => {
|
|
1679
|
+
return path;
|
|
1680
|
+
};
|
|
1681
|
+
let mqttAccessToken;
|
|
1682
|
+
let mqttUserId;
|
|
1683
|
+
function subscribeTopic(topic, callback) {
|
|
1684
|
+
const { mqtt, emitter, accessToken, user } = getActiveClient();
|
|
1685
|
+
if (mqttAccessToken !== accessToken || mqttUserId !== user._id) {
|
|
1686
|
+
mqttAccessToken = accessToken;
|
|
1687
|
+
mqttUserId = user._id;
|
|
1688
|
+
mqtt.connect({ accessToken: accessToken, userId: user._id });
|
|
1689
|
+
proxyMqttEvents(mqtt, emitter);
|
|
1690
|
+
}
|
|
1691
|
+
return mqtt.subscribe(topic, callback);
|
|
1692
|
+
}
|
|
1439
1693
|
|
|
1440
1694
|
/**
|
|
1441
1695
|
* ```js
|
|
@@ -1637,6 +1891,16 @@ const updateUser = async (userId, patch) => {
|
|
|
1637
1891
|
return data.users.find(user => user.userId === userId);
|
|
1638
1892
|
};
|
|
1639
1893
|
|
|
1894
|
+
const createUserEventSubscriber = (event, callback) => {
|
|
1895
|
+
const client = getActiveClient();
|
|
1896
|
+
const filter = (payload) => {
|
|
1897
|
+
decoratePayload(payload);
|
|
1898
|
+
ingestInCache(payload);
|
|
1899
|
+
callback(payload.users[0]);
|
|
1900
|
+
};
|
|
1901
|
+
return createEventSubscriber(client, event, event, filter);
|
|
1902
|
+
};
|
|
1903
|
+
|
|
1640
1904
|
/**
|
|
1641
1905
|
* ```js
|
|
1642
1906
|
* import { onUserUpdated } from '@amityco/ts-sdk'
|
|
@@ -1645,22 +1909,65 @@ const updateUser = async (userId, patch) => {
|
|
|
1645
1909
|
* })
|
|
1646
1910
|
* ```
|
|
1647
1911
|
*
|
|
1648
|
-
* Fired when
|
|
1912
|
+
* Fired when a {@link Amity.User} has been updated
|
|
1649
1913
|
*
|
|
1650
1914
|
* @param callback The function to call when the event was fired
|
|
1651
1915
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1652
1916
|
*
|
|
1653
1917
|
* @category User Events
|
|
1654
1918
|
*/
|
|
1655
|
-
const onUserUpdated = (callback) =>
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1919
|
+
const onUserUpdated = (callback) => createUserEventSubscriber('user.updated', callback);
|
|
1920
|
+
|
|
1921
|
+
/**
|
|
1922
|
+
* ```js
|
|
1923
|
+
* import { onUserFlagged } from '@amityco/ts-sdk'
|
|
1924
|
+
* const dispose = onUserFlagged(user => {
|
|
1925
|
+
* // ...
|
|
1926
|
+
* })
|
|
1927
|
+
* ```
|
|
1928
|
+
*
|
|
1929
|
+
* Fired when a {@link Amity.User} has been flagged
|
|
1930
|
+
*
|
|
1931
|
+
* @param callback The function to call when the event was fired
|
|
1932
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1933
|
+
*
|
|
1934
|
+
* @category User Events
|
|
1935
|
+
*/
|
|
1936
|
+
const onUserFlagged = (callback) => createUserEventSubscriber('user.flagged', callback);
|
|
1937
|
+
|
|
1938
|
+
/**
|
|
1939
|
+
* ```js
|
|
1940
|
+
* import { onUserUnflagged } from '@amityco/ts-sdk'
|
|
1941
|
+
* const dispose = onUserUnflagged(user => {
|
|
1942
|
+
* // ...
|
|
1943
|
+
* })
|
|
1944
|
+
* ```
|
|
1945
|
+
*
|
|
1946
|
+
* Fired when a flag has been removed from a {@link Amity.User}
|
|
1947
|
+
*
|
|
1948
|
+
* @param callback The function to call when the event was fired
|
|
1949
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1950
|
+
*
|
|
1951
|
+
* @category User Events
|
|
1952
|
+
*/
|
|
1953
|
+
const onUserUnflagged = (callback) => createUserEventSubscriber('user.unflagged', callback);
|
|
1954
|
+
|
|
1955
|
+
/**
|
|
1956
|
+
* ```js
|
|
1957
|
+
* import { onUserFlagCleared } from '@amityco/ts-sdk'
|
|
1958
|
+
* const dispose = onUserFlagCleared(user => {
|
|
1959
|
+
* // ...
|
|
1960
|
+
* })
|
|
1961
|
+
* ```
|
|
1962
|
+
*
|
|
1963
|
+
* Fired when flags have been cleared for a {@link Amity.User}
|
|
1964
|
+
*
|
|
1965
|
+
* @param callback The function to call when the event was fired
|
|
1966
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1967
|
+
*
|
|
1968
|
+
* @category User Events
|
|
1969
|
+
*/
|
|
1970
|
+
const onUserFlagCleared = (callback) => createUserEventSubscriber('user.flagCleared', callback);
|
|
1664
1971
|
|
|
1665
1972
|
/**
|
|
1666
1973
|
* ```js
|
|
@@ -1694,7 +2001,7 @@ const observeUser = (userId, callback) => {
|
|
|
1694
2001
|
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
1695
2002
|
};
|
|
1696
2003
|
const disposers = [];
|
|
1697
|
-
disposers.push(onUserUpdated(data => router({ data }, 'onUpdate')));
|
|
2004
|
+
disposers.push(onUserUpdated(data => router({ data }, 'onUpdate')), onUserFlagged(data => router({ data }, 'onFlagged')), onUserUnflagged(data => router({ data }, 'onUnflagged')), onUserFlagCleared(data => router({ data }, 'onFlagCleared')));
|
|
1698
2005
|
runQuery(createQuery(getUser, userId), (result) => result.data && router(result, 'onFetch'));
|
|
1699
2006
|
return () => {
|
|
1700
2007
|
log(`observeUser(tmpid: ${timestamp}) > dispose`);
|
|
@@ -1799,11 +2106,10 @@ const createFile = async (formData, onProgress) => {
|
|
|
1799
2106
|
const deleteFile = async (fileId) => {
|
|
1800
2107
|
const client = getActiveClient();
|
|
1801
2108
|
client.log('file/deleteFile', fileId);
|
|
2109
|
+
// API-FIX: payload is different than swagger docs!!
|
|
1802
2110
|
const { data } = await client.http.delete(`/api/v3/files/${fileId}`);
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
mergeInCache('file', { fileId, isDeleted: true });
|
|
1806
|
-
return success;
|
|
2111
|
+
dropFromCache('file', { fileId: data.fileId });
|
|
2112
|
+
return { success: true };
|
|
1807
2113
|
};
|
|
1808
2114
|
|
|
1809
2115
|
/**
|
|
@@ -1959,10 +2265,10 @@ const dispatchReactable = (referenceType, model) => {
|
|
|
1959
2265
|
fireEvent('v3.message.didUpdate', { messages: [model] });
|
|
1960
2266
|
else if (referenceType === 'post')
|
|
1961
2267
|
// @ts-ignore: refactor later
|
|
1962
|
-
fireEvent('
|
|
2268
|
+
fireEvent('post.updated', { posts: [model] });
|
|
1963
2269
|
else if (referenceType === 'comment')
|
|
1964
2270
|
// @ts-ignore: refactor later
|
|
1965
|
-
fireEvent('
|
|
2271
|
+
fireEvent('comment.updated', { comments: [model] });
|
|
1966
2272
|
};
|
|
1967
2273
|
|
|
1968
2274
|
/**
|
|
@@ -2427,10 +2733,21 @@ getPosts.locally = (postIds) => {
|
|
|
2427
2733
|
* @async
|
|
2428
2734
|
*/
|
|
2429
2735
|
const getPost = async (postId) => {
|
|
2736
|
+
var _a;
|
|
2430
2737
|
const client = getActiveClient();
|
|
2431
2738
|
client.log('post/getPost', postId);
|
|
2432
|
-
|
|
2433
|
-
|
|
2739
|
+
let data;
|
|
2740
|
+
try {
|
|
2741
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
2742
|
+
const response = await client.http.get(`/api/v3/posts/${postId}`);
|
|
2743
|
+
data = response.data;
|
|
2744
|
+
}
|
|
2745
|
+
catch (error) {
|
|
2746
|
+
if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 400400 /* ITEM_NOT_FOUND */) {
|
|
2747
|
+
dropFromCache('post', { postId });
|
|
2748
|
+
}
|
|
2749
|
+
throw error;
|
|
2750
|
+
}
|
|
2434
2751
|
decoratePayload(data);
|
|
2435
2752
|
ingestInCache(data);
|
|
2436
2753
|
const { posts } = data;
|
|
@@ -2484,8 +2801,8 @@ const createPost = async (bundle) => {
|
|
|
2484
2801
|
const { data } = await client.http.post('/api/v3/posts', bundle);
|
|
2485
2802
|
decoratePayload(data);
|
|
2486
2803
|
ingestInCache(data);
|
|
2487
|
-
fireEvent('
|
|
2488
|
-
fireEvent('
|
|
2804
|
+
fireEvent('community.updated', { communities: data.communities });
|
|
2805
|
+
fireEvent('post.created', data);
|
|
2489
2806
|
const { posts } = data;
|
|
2490
2807
|
return posts[0];
|
|
2491
2808
|
};
|
|
@@ -2513,7 +2830,7 @@ const updatePost = async (postId, patch) => {
|
|
|
2513
2830
|
const { data } = await client.http.put(`/api/v3/posts/${postId}`, patch);
|
|
2514
2831
|
decoratePayload(data);
|
|
2515
2832
|
ingestInCache(data);
|
|
2516
|
-
fireEvent('
|
|
2833
|
+
fireEvent('post.updated', data);
|
|
2517
2834
|
const { posts } = data;
|
|
2518
2835
|
return posts.find(post => post.postId === postId);
|
|
2519
2836
|
};
|
|
@@ -2683,7 +3000,7 @@ const createCommunity = async (bundle) => {
|
|
|
2683
3000
|
const { data } = await client.http.post('/api/v3/communities/', bundle);
|
|
2684
3001
|
decoratePayload(data);
|
|
2685
3002
|
ingestInCache(data);
|
|
2686
|
-
fireEvent('
|
|
3003
|
+
fireEvent('community.created', data);
|
|
2687
3004
|
const { communities } = data;
|
|
2688
3005
|
return communities[0];
|
|
2689
3006
|
};
|
|
@@ -2709,7 +3026,7 @@ const updateCommunity = async (communityId, patch) => {
|
|
|
2709
3026
|
const { data } = await client.http.put(`/api/v3/communities/${communityId}`, patch);
|
|
2710
3027
|
decoratePayload(data);
|
|
2711
3028
|
ingestInCache(data);
|
|
2712
|
-
fireEvent('
|
|
3029
|
+
fireEvent('community.updated', data);
|
|
2713
3030
|
const { communities } = data;
|
|
2714
3031
|
return communities.find(community => community.communityId === communityId);
|
|
2715
3032
|
};
|
|
@@ -2734,7 +3051,7 @@ const deleteCommunity = async (communityId) => {
|
|
|
2734
3051
|
// API-FIX: This endpoint has not been implemented yet.
|
|
2735
3052
|
await client.http.delete(`/api/v3/communities/${communityId}`);
|
|
2736
3053
|
const deleted = await getCommunity(communityId);
|
|
2737
|
-
fireEvent('
|
|
3054
|
+
fireEvent('community.deleted', { communities: [deleted] });
|
|
2738
3055
|
return deleted;
|
|
2739
3056
|
};
|
|
2740
3057
|
|
|
@@ -2758,7 +3075,7 @@ const joinCommunity = async (communityId) => {
|
|
|
2758
3075
|
const { data } = await client.http.post(`/api/v3/communities/${communityId}/join`);
|
|
2759
3076
|
decoratePayload(data);
|
|
2760
3077
|
ingestInCache(data);
|
|
2761
|
-
fireEvent('
|
|
3078
|
+
fireEvent('community.joined', {
|
|
2762
3079
|
communities: data.communities
|
|
2763
3080
|
});
|
|
2764
3081
|
const { communityUsers } = data;
|
|
@@ -2786,7 +3103,7 @@ const leaveCommunity = async (communityId) => {
|
|
|
2786
3103
|
const { data } = await client.http.delete(`/api/v3/communities/${communityId}/leave`);
|
|
2787
3104
|
decoratePayload(data);
|
|
2788
3105
|
ingestInCache(data);
|
|
2789
|
-
fireEvent('
|
|
3106
|
+
fireEvent('community.left', {
|
|
2790
3107
|
communities: data.communities,
|
|
2791
3108
|
});
|
|
2792
3109
|
const { communityUsers } = data;
|
|
@@ -2865,7 +3182,7 @@ const addCommunityMembers = async (communityId, userIds) => {
|
|
|
2865
3182
|
const { data } = await client.http.post(`/api/v3/communities/${communityId}/users`, { communityId, userIds });
|
|
2866
3183
|
decoratePayload(data);
|
|
2867
3184
|
ingestInCache(data);
|
|
2868
|
-
fireEvent('
|
|
3185
|
+
fireEvent('community.userAdded', {
|
|
2869
3186
|
communities: data.communities,
|
|
2870
3187
|
});
|
|
2871
3188
|
const { communityUsers } = data;
|
|
@@ -2894,7 +3211,7 @@ const removeCommunityMembers = async (communityId, userIds) => {
|
|
|
2894
3211
|
const { data } = await client.http.delete(`/api/v3/communities/${communityId}/users`, { data: { communityId, userIds } });
|
|
2895
3212
|
decoratePayload(data);
|
|
2896
3213
|
ingestInCache(data);
|
|
2897
|
-
fireEvent('
|
|
3214
|
+
fireEvent('community.userRemoved', {
|
|
2898
3215
|
communities: data.communities,
|
|
2899
3216
|
});
|
|
2900
3217
|
const { communityUsers } = data;
|
|
@@ -2968,18 +3285,36 @@ const removeCommunityMembersRole = async (communityId, roleId, userIds) => {
|
|
|
2968
3285
|
* @category Post API
|
|
2969
3286
|
* @async
|
|
2970
3287
|
*/
|
|
2971
|
-
const deletePost = async (postId) => {
|
|
3288
|
+
const deletePost = async (postId, permanent = false) => {
|
|
2972
3289
|
const client = getActiveClient();
|
|
2973
3290
|
client.log('post/deletePost', postId);
|
|
3291
|
+
let post = await getPost(postId);
|
|
2974
3292
|
// API-FIX: This endpoint has not been implemented yet.
|
|
2975
|
-
await client.http.delete(`/api/
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
3293
|
+
await client.http.delete(`/api/v4/posts/${postId}`, {
|
|
3294
|
+
params: {
|
|
3295
|
+
postId,
|
|
3296
|
+
permanent,
|
|
3297
|
+
},
|
|
3298
|
+
});
|
|
3299
|
+
// there is currently a limitation which doesn't allow us to fire event to tell that community
|
|
3300
|
+
// has been updated. reason is that, when the object is deleted, we don't have its `communityId`
|
|
3301
|
+
// and so we cannot refetch the community or advertise on events. hopefully this should be solved
|
|
3302
|
+
// later when realtime events covers that for us.
|
|
3303
|
+
if (post.targetType === 'community') {
|
|
3304
|
+
const community = await getCommunity(post.targetId);
|
|
3305
|
+
fireEvent('community.updated', { communities: [community] });
|
|
3306
|
+
}
|
|
3307
|
+
// to support hard deletion
|
|
3308
|
+
const deleted = Object.assign(Object.assign({}, post), { isDeleted: true });
|
|
3309
|
+
fireEvent('post.deleted', { posts: [deleted] });
|
|
3310
|
+
if (permanent) {
|
|
3311
|
+
setTimeout(() => {
|
|
3312
|
+
dropFromCache('post', { postId });
|
|
3313
|
+
}, 0);
|
|
3314
|
+
}
|
|
3315
|
+
else {
|
|
3316
|
+
mergeInCache('post', { postId, isDeleted: true });
|
|
2981
3317
|
}
|
|
2982
|
-
fireEvent('v3.post.didDelete', { posts: [deleted] });
|
|
2983
3318
|
return deleted;
|
|
2984
3319
|
};
|
|
2985
3320
|
|
|
@@ -3040,8 +3375,6 @@ queryComments.locally = (query) => {
|
|
|
3040
3375
|
const client = getActiveClient();
|
|
3041
3376
|
client.log('post/queryComments.locally', query);
|
|
3042
3377
|
const { referenceId, referenceType, parentId, isDeleted, page = { before: 0, limit: 5 }, } = query;
|
|
3043
|
-
if (isNaN(page.after) && isNaN(page.before))
|
|
3044
|
-
page.before = 0;
|
|
3045
3378
|
if (isNaN(page.limit))
|
|
3046
3379
|
page.limit = 5;
|
|
3047
3380
|
let comments = (_a = pullFromCache('comment')) !== null && _a !== void 0 ? _a : [];
|
|
@@ -3071,8 +3404,8 @@ queryComments.locally = (query) => {
|
|
|
3071
3404
|
if (afterIndex < 0 || beforeIndex < 0)
|
|
3072
3405
|
return;
|
|
3073
3406
|
let slice = comments.slice(afterIndex !== -1 ? afterIndex : 0, beforeIndex !== -1 ? beforeIndex : undefined);
|
|
3074
|
-
//
|
|
3075
|
-
if (
|
|
3407
|
+
// check if slice data is correct (complete)
|
|
3408
|
+
if (slice.length < page.limit)
|
|
3076
3409
|
return;
|
|
3077
3410
|
if (!isNaN(page.after) && !isNaN(page.before)) ;
|
|
3078
3411
|
else if (!isNaN(page.after)) {
|
|
@@ -3081,16 +3414,20 @@ queryComments.locally = (query) => {
|
|
|
3081
3414
|
else if (!isNaN(page.before)) {
|
|
3082
3415
|
slice = slice.slice(-page.limit);
|
|
3083
3416
|
}
|
|
3417
|
+
else {
|
|
3418
|
+
slice = slice.slice(-page.limit);
|
|
3419
|
+
}
|
|
3084
3420
|
for (let i = 0; i < slice.length - 1; i++)
|
|
3085
3421
|
if (slice[i].segmentNumber !== slice[i + 1].segmentNumber - 1)
|
|
3086
3422
|
return;
|
|
3087
3423
|
const prevPage = slice[0].segmentNumber > 1
|
|
3088
3424
|
? { before: slice[0].segmentNumber, limit: page.limit }
|
|
3089
3425
|
: undefined;
|
|
3090
|
-
const nextPage =
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3426
|
+
const nextPage = slice[slice.length - 1].segmentNumber < comments[comments.length - 1].segmentNumber
|
|
3427
|
+
? {
|
|
3428
|
+
after: slice[slice.length - 1].segmentNumber,
|
|
3429
|
+
limit: page.limit,
|
|
3430
|
+
} : undefined;
|
|
3094
3431
|
return { data: toRecord(slice), nextPage, prevPage };
|
|
3095
3432
|
};
|
|
3096
3433
|
|
|
@@ -3155,10 +3492,21 @@ getComments.locally = (commentIds) => {
|
|
|
3155
3492
|
* @async
|
|
3156
3493
|
*/
|
|
3157
3494
|
const getComment = async (commentId) => {
|
|
3495
|
+
var _a;
|
|
3158
3496
|
const client = getActiveClient();
|
|
3159
3497
|
client.log('comment/getComment', commentId);
|
|
3160
|
-
|
|
3161
|
-
|
|
3498
|
+
let data;
|
|
3499
|
+
try {
|
|
3500
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
3501
|
+
const response = await client.http.get(`/api/v3/comments/${commentId}`);
|
|
3502
|
+
data = response.data;
|
|
3503
|
+
}
|
|
3504
|
+
catch (error) {
|
|
3505
|
+
if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 400400 /* ITEM_NOT_FOUND */) {
|
|
3506
|
+
dropFromCache('comment', { commentId });
|
|
3507
|
+
}
|
|
3508
|
+
throw error;
|
|
3509
|
+
}
|
|
3162
3510
|
decoratePayload(data);
|
|
3163
3511
|
ingestInCache(data);
|
|
3164
3512
|
const { comments } = data;
|
|
@@ -3206,8 +3554,8 @@ const createComment = async (bundle) => {
|
|
|
3206
3554
|
decoratePayload(data);
|
|
3207
3555
|
ingestInCache(data);
|
|
3208
3556
|
const post = await getPost(bundle.referenceId);
|
|
3209
|
-
fireEvent('
|
|
3210
|
-
fireEvent('
|
|
3557
|
+
fireEvent('post.updated', { posts: [post] });
|
|
3558
|
+
fireEvent('comment.created', data);
|
|
3211
3559
|
const { comments } = data;
|
|
3212
3560
|
return comments[0];
|
|
3213
3561
|
};
|
|
@@ -3235,7 +3583,7 @@ const updateComment = async (commentId, patch) => {
|
|
|
3235
3583
|
const { data } = await client.http.put(`/api/v3/comments/${commentId}`, patch);
|
|
3236
3584
|
decoratePayload(data);
|
|
3237
3585
|
ingestInCache(data);
|
|
3238
|
-
fireEvent('
|
|
3586
|
+
fireEvent('comment.updated', data);
|
|
3239
3587
|
const { comments } = data;
|
|
3240
3588
|
return comments.find(comment => comment.commentId === commentId);
|
|
3241
3589
|
};
|
|
@@ -3254,15 +3602,30 @@ const updateComment = async (commentId, patch) => {
|
|
|
3254
3602
|
* @category Comment API
|
|
3255
3603
|
* @async
|
|
3256
3604
|
*/
|
|
3257
|
-
const deleteComment = async (commentId) => {
|
|
3605
|
+
const deleteComment = async (commentId, permanent = false) => {
|
|
3258
3606
|
const client = getActiveClient();
|
|
3259
3607
|
client.log('comment/deleteComment', commentId);
|
|
3608
|
+
const comment = await getComment(commentId);
|
|
3260
3609
|
// API-FIX: This endpoint has not been implemented yet.
|
|
3261
|
-
await client.http.delete(`/api/
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3610
|
+
await client.http.delete(`/api/v4/comments/${commentId}`, {
|
|
3611
|
+
params: {
|
|
3612
|
+
commentId,
|
|
3613
|
+
permanent,
|
|
3614
|
+
},
|
|
3615
|
+
});
|
|
3616
|
+
// to support hard deletion
|
|
3617
|
+
const deleted = Object.assign(Object.assign({}, comment), { isDeleted: true });
|
|
3618
|
+
const post = await getPost(comment.referenceId);
|
|
3619
|
+
fireEvent('post.updated', { posts: [post] });
|
|
3620
|
+
fireEvent('comment.deleted', { comments: [deleted] });
|
|
3621
|
+
if (permanent) {
|
|
3622
|
+
setTimeout(() => {
|
|
3623
|
+
dropFromCache('comment', { commentId });
|
|
3624
|
+
}, 0);
|
|
3625
|
+
}
|
|
3626
|
+
else {
|
|
3627
|
+
mergeInCache('comment', { commentId, isDeleted: true });
|
|
3628
|
+
}
|
|
3266
3629
|
return deleted;
|
|
3267
3630
|
};
|
|
3268
3631
|
|
|
@@ -3277,6 +3640,42 @@ const fetchReference = async (referenceType, referenceId) => {
|
|
|
3277
3640
|
throw new Error(`Incorrect referenceType: ${referenceType}`);
|
|
3278
3641
|
};
|
|
3279
3642
|
|
|
3643
|
+
/** @hidden */
|
|
3644
|
+
const preparePayloadForCache = (event, payload) => {
|
|
3645
|
+
var _a, _b;
|
|
3646
|
+
const client = getActiveClient();
|
|
3647
|
+
if (client.userId !== payload.reactor.userId) {
|
|
3648
|
+
return;
|
|
3649
|
+
}
|
|
3650
|
+
let model;
|
|
3651
|
+
let cached;
|
|
3652
|
+
if (event === 'post.addReaction' || event === 'post.removeReaction') {
|
|
3653
|
+
const post = payload.posts[0];
|
|
3654
|
+
cached = ((_a = pullFromCache('post', post.postId)) !== null && _a !== void 0 ? _a : [])[0];
|
|
3655
|
+
model = post;
|
|
3656
|
+
}
|
|
3657
|
+
else {
|
|
3658
|
+
const comment = payload.comments[0];
|
|
3659
|
+
cached = ((_b = pullFromCache('comment', comment.commentId)) !== null && _b !== void 0 ? _b : [])[0];
|
|
3660
|
+
model = comment;
|
|
3661
|
+
}
|
|
3662
|
+
if (!cached || !cached.myReactions) {
|
|
3663
|
+
return;
|
|
3664
|
+
}
|
|
3665
|
+
if (event === 'post.addReaction' || event === 'comment.addReaction') {
|
|
3666
|
+
if (cached.myReactions.includes(payload.reactor.reactionName)) {
|
|
3667
|
+
return;
|
|
3668
|
+
}
|
|
3669
|
+
Object.assign(model, { myReactions: [...cached.myReactions, payload.reactor.reactionName] });
|
|
3670
|
+
}
|
|
3671
|
+
else {
|
|
3672
|
+
if (!cached.myReactions.includes(payload.reactor.reactionName)) {
|
|
3673
|
+
return;
|
|
3674
|
+
}
|
|
3675
|
+
Object.assign(model, { myReactions: cached.myReactions.filter(x => x !== payload.reactor.reactionName) });
|
|
3676
|
+
}
|
|
3677
|
+
};
|
|
3678
|
+
|
|
3280
3679
|
/**
|
|
3281
3680
|
* ```js
|
|
3282
3681
|
* import { addReaction } from '@amityco/ts-sdk'
|
|
@@ -3389,51 +3788,441 @@ removeReaction.optimistically = (referenceType, referenceId, reactionName) => {
|
|
|
3389
3788
|
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;
|
|
3390
3789
|
};
|
|
3391
3790
|
|
|
3791
|
+
const createPostEventSubscriber = (event, callback) => {
|
|
3792
|
+
const client = getActiveClient();
|
|
3793
|
+
const filter = (payload) => {
|
|
3794
|
+
var _a, _b;
|
|
3795
|
+
const [cached] = (_a = pullFromCache('post', payload.posts[0].postId)) !== null && _a !== void 0 ? _a : [];
|
|
3796
|
+
const { communities } = payload, others = __rest(payload, ["communities"]);
|
|
3797
|
+
decoratePayload(others);
|
|
3798
|
+
ingestInCache(others);
|
|
3799
|
+
if (communities === null || communities === void 0 ? void 0 : communities[0]) {
|
|
3800
|
+
fireEvent('community.updated', { communities });
|
|
3801
|
+
}
|
|
3802
|
+
const [post] = (_b = pullFromCache('post', others.posts[0].postId)) !== null && _b !== void 0 ? _b : [];
|
|
3803
|
+
if (cached && isMatch(cached, post)) {
|
|
3804
|
+
return;
|
|
3805
|
+
}
|
|
3806
|
+
callback(post);
|
|
3807
|
+
};
|
|
3808
|
+
return createEventSubscriber(client, event, event, filter);
|
|
3809
|
+
};
|
|
3810
|
+
|
|
3392
3811
|
/**
|
|
3393
3812
|
* ```js
|
|
3394
|
-
* import {
|
|
3395
|
-
* const dispose =
|
|
3813
|
+
* import { onPostCreated } from '@amityco/ts-sdk'
|
|
3814
|
+
* const dispose = onPostCreated(post => {
|
|
3396
3815
|
* // ...
|
|
3397
3816
|
* })
|
|
3398
3817
|
* ```
|
|
3399
3818
|
*
|
|
3400
|
-
* Fired when
|
|
3819
|
+
* Fired when {@link Amity.Post} has been created
|
|
3401
3820
|
*
|
|
3402
3821
|
* @param callback The function to call when the event was fired
|
|
3403
3822
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3404
3823
|
*
|
|
3405
|
-
* @category
|
|
3406
|
-
|
|
3407
|
-
const
|
|
3408
|
-
const client = getActiveClient();
|
|
3409
|
-
const filter = (payload) => {
|
|
3410
|
-
// TODO: lol
|
|
3411
|
-
decoratePayload(payload);
|
|
3412
|
-
ingestInCache(payload);
|
|
3413
|
-
};
|
|
3414
|
-
return createEventSubscriber(client, 'reaction/onReactionRemoved', 'v3.message.didUpdate', filter);
|
|
3415
|
-
};
|
|
3824
|
+
* @category Post Events
|
|
3825
|
+
*/
|
|
3826
|
+
const onPostCreated = (callback) => createPostEventSubscriber('post.created', callback);
|
|
3416
3827
|
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3828
|
+
/**
|
|
3829
|
+
* ```js
|
|
3830
|
+
* import { onPostUpdated } from '@amityco/ts-sdk'
|
|
3831
|
+
* const dispose = onPostUpdated(post => {
|
|
3832
|
+
* // ...
|
|
3833
|
+
* })
|
|
3834
|
+
* ```
|
|
3835
|
+
*
|
|
3836
|
+
* Fired when a {@link Amity.Post} has been updated
|
|
3837
|
+
*
|
|
3838
|
+
* @param callback The function to call when the event was fired
|
|
3839
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3840
|
+
*
|
|
3841
|
+
* @category Post Events
|
|
3842
|
+
*/
|
|
3843
|
+
const onPostUpdated = (callback) => createPostEventSubscriber('post.updated', callback);
|
|
3844
|
+
|
|
3845
|
+
/**
|
|
3846
|
+
* ```js
|
|
3847
|
+
* import { onPostDeleted } from '@amityco/ts-sdk'
|
|
3848
|
+
* const dispose = onPostDeleted(post => {
|
|
3849
|
+
* // ...
|
|
3850
|
+
* })
|
|
3851
|
+
* ```
|
|
3852
|
+
*
|
|
3853
|
+
* Fired when a {@link Amity.Post} has been deleted
|
|
3854
|
+
*
|
|
3855
|
+
* @param callback The function to call when the event was fired
|
|
3856
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3857
|
+
*
|
|
3858
|
+
* @category Post Events
|
|
3859
|
+
*/
|
|
3860
|
+
const onPostDeleted = (callback) => createPostEventSubscriber('post.deleted', callback);
|
|
3861
|
+
|
|
3862
|
+
/**
|
|
3863
|
+
* ```js
|
|
3864
|
+
* import { onPostApproved } from '@amityco/ts-sdk'
|
|
3865
|
+
* const dispose = onPostApproved(post => {
|
|
3866
|
+
* // ...
|
|
3867
|
+
* })
|
|
3868
|
+
* ```
|
|
3869
|
+
*
|
|
3870
|
+
* Fired when a {@link Amity.Post} has been updated
|
|
3871
|
+
*
|
|
3872
|
+
* @param callback The function to call when the event was fired
|
|
3873
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3874
|
+
*
|
|
3875
|
+
* @category Post Events
|
|
3876
|
+
*/
|
|
3877
|
+
const onPostApproved = (callback) => createPostEventSubscriber('post.approved', callback);
|
|
3878
|
+
|
|
3879
|
+
/**
|
|
3880
|
+
* ```js
|
|
3881
|
+
* import { onPostDeclined } from '@amityco/ts-sdk'
|
|
3882
|
+
* const dispose = onPostDeclined(post => {
|
|
3883
|
+
* // ...
|
|
3884
|
+
* })
|
|
3885
|
+
* ```
|
|
3886
|
+
*
|
|
3887
|
+
* Fired when a {@link Amity.Post} has been updated
|
|
3888
|
+
*
|
|
3889
|
+
* @param callback The function to call when the event was fired
|
|
3890
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3891
|
+
*
|
|
3892
|
+
* @category Post Events
|
|
3893
|
+
*/
|
|
3894
|
+
const onPostDeclined = (callback) => createPostEventSubscriber('post.declined', callback);
|
|
3895
|
+
|
|
3896
|
+
/**
|
|
3897
|
+
* ```js
|
|
3898
|
+
* import { onPostFlagged } from '@amityco/ts-sdk'
|
|
3899
|
+
* const dispose = onPostFlagged(post => {
|
|
3900
|
+
* // ...
|
|
3901
|
+
* })
|
|
3902
|
+
* ```
|
|
3903
|
+
*
|
|
3904
|
+
* Fired when a {@link Amity.Post} has been flagged
|
|
3905
|
+
*
|
|
3906
|
+
* @param callback The function to call when the event was fired
|
|
3907
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3908
|
+
*
|
|
3909
|
+
* @category Post Events
|
|
3910
|
+
*/
|
|
3911
|
+
const onPostFlagged = (callback) => createPostEventSubscriber('post.flagged', callback);
|
|
3912
|
+
|
|
3913
|
+
/**
|
|
3914
|
+
* ```js
|
|
3915
|
+
* import { onPostUnflagged } from '@amityco/ts-sdk'
|
|
3916
|
+
* const dispose = onPostUnflagged(post => {
|
|
3917
|
+
* // ...
|
|
3918
|
+
* })
|
|
3919
|
+
* ```
|
|
3920
|
+
*
|
|
3921
|
+
* Fired when a flag has been removed from a {@link Amity.Post}
|
|
3922
|
+
*
|
|
3923
|
+
* @param callback The function to call when the event was fired
|
|
3924
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3925
|
+
*
|
|
3926
|
+
* @category Post Events
|
|
3927
|
+
*/
|
|
3928
|
+
const onPostUnflagged = (callback) => createPostEventSubscriber('post.unflagged', callback);
|
|
3929
|
+
|
|
3930
|
+
/**
|
|
3931
|
+
* ```js
|
|
3932
|
+
* import { onPostReactionAdded } from '@amityco/ts-sdk'
|
|
3933
|
+
* const dispose = onPostReactionAdded(post => {
|
|
3934
|
+
* // ...
|
|
3935
|
+
* })
|
|
3936
|
+
* ```
|
|
3937
|
+
*
|
|
3938
|
+
* Fired when a {@link Amity.Post} has been reacted
|
|
3939
|
+
*
|
|
3940
|
+
* @param callback The function to call when the event was fired
|
|
3941
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3942
|
+
*
|
|
3943
|
+
* @category Post Events
|
|
3944
|
+
*/
|
|
3945
|
+
const onPostReactionAdded = (callback) => {
|
|
3946
|
+
const client = getActiveClient();
|
|
3947
|
+
const filter = (payload) => {
|
|
3948
|
+
var _a, _b;
|
|
3949
|
+
const [cached] = (_a = pullFromCache('post', payload.posts[0].postId)) !== null && _a !== void 0 ? _a : [];
|
|
3950
|
+
const postPayload = __rest(payload, ["reactor"]);
|
|
3951
|
+
decoratePayload(postPayload);
|
|
3952
|
+
preparePayloadForCache('post.addReaction', payload);
|
|
3953
|
+
ingestInCache(postPayload);
|
|
3954
|
+
const [post] = (_b = pullFromCache('post', payload.posts[0].postId)) !== null && _b !== void 0 ? _b : [];
|
|
3955
|
+
if (cached && isMatch(cached, post)) {
|
|
3956
|
+
return;
|
|
3957
|
+
}
|
|
3958
|
+
callback(post);
|
|
3959
|
+
};
|
|
3960
|
+
return createEventSubscriber(client, 'post.addReaction', 'post.addReaction', filter);
|
|
3961
|
+
};
|
|
3962
|
+
|
|
3963
|
+
/**
|
|
3964
|
+
* ```js
|
|
3965
|
+
* import { onPostReactionRemoved } from '@amityco/ts-sdk'
|
|
3966
|
+
* const dispose = onPostReactionRemoved(post => {
|
|
3967
|
+
* // ...
|
|
3968
|
+
* })
|
|
3969
|
+
* ```
|
|
3970
|
+
*
|
|
3971
|
+
* Fired when a reaction has been removed from a {@link Amity.Post}
|
|
3972
|
+
*
|
|
3973
|
+
* @param callback The function to call when the event was fired
|
|
3974
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3975
|
+
*
|
|
3976
|
+
* @category Post Events
|
|
3977
|
+
*/
|
|
3978
|
+
const onPostReactionRemoved = (callback) => {
|
|
3979
|
+
const client = getActiveClient();
|
|
3980
|
+
const filter = (payload) => {
|
|
3981
|
+
var _a, _b;
|
|
3982
|
+
const [cached] = (_a = pullFromCache('post', payload.posts[0].postId)) !== null && _a !== void 0 ? _a : [];
|
|
3983
|
+
const postPayload = __rest(payload, ["reactor"]);
|
|
3984
|
+
decoratePayload(postPayload);
|
|
3985
|
+
preparePayloadForCache('post.removeReaction', payload);
|
|
3986
|
+
ingestInCache(postPayload);
|
|
3987
|
+
const [post] = (_b = pullFromCache('post', payload.posts[0].postId)) !== null && _b !== void 0 ? _b : [];
|
|
3988
|
+
if (cached && isMatch(cached, post)) {
|
|
3989
|
+
return;
|
|
3990
|
+
}
|
|
3991
|
+
callback(post);
|
|
3992
|
+
};
|
|
3993
|
+
return createEventSubscriber(client, 'post.removeReaction', 'post.removeReaction', filter);
|
|
3994
|
+
};
|
|
3995
|
+
|
|
3996
|
+
const createCommentEventSubscriber = (event, callback) => {
|
|
3997
|
+
const client = getActiveClient();
|
|
3998
|
+
const filter = (payload) => {
|
|
3999
|
+
var _a, _b;
|
|
4000
|
+
const [cached] = (_a = pullFromCache('comment', payload.comments[0].commentId)) !== null && _a !== void 0 ? _a : [];
|
|
4001
|
+
decoratePayload(payload);
|
|
4002
|
+
ingestInCache(payload);
|
|
4003
|
+
const [comment] = (_b = pullFromCache('comment', payload.comments[0].commentId)) !== null && _b !== void 0 ? _b : [];
|
|
4004
|
+
if (cached && isMatch(cached, comment)) {
|
|
4005
|
+
return;
|
|
4006
|
+
}
|
|
4007
|
+
callback(comment);
|
|
4008
|
+
};
|
|
4009
|
+
return createEventSubscriber(client, event, event, filter);
|
|
4010
|
+
};
|
|
4011
|
+
|
|
4012
|
+
/**
|
|
4013
|
+
* ```js
|
|
4014
|
+
* import { onCommentCreated } from '@amityco/ts-sdk'
|
|
4015
|
+
* const dispose = onCommentCreated(comment => {
|
|
4016
|
+
* // ...
|
|
4017
|
+
* })
|
|
4018
|
+
* ```
|
|
4019
|
+
*
|
|
4020
|
+
* Fired when {@link Amity.Comment} has been created
|
|
4021
|
+
*
|
|
4022
|
+
* @param callback The function to call when the event was fired
|
|
4023
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4024
|
+
*
|
|
4025
|
+
* @category Comment Events
|
|
4026
|
+
*/
|
|
4027
|
+
const onCommentCreated = (callback) => createCommentEventSubscriber('comment.created', callback);
|
|
4028
|
+
|
|
4029
|
+
/**
|
|
4030
|
+
* ```js
|
|
4031
|
+
* import { onCommentUpdated } from '@amityco/ts-sdk'
|
|
4032
|
+
* const dispose = onCommentUpdated(comment => {
|
|
4033
|
+
* // ...
|
|
4034
|
+
* })
|
|
4035
|
+
* ```
|
|
4036
|
+
*
|
|
4037
|
+
* Fired when a {@link Amity.Comment} has been updated
|
|
4038
|
+
*
|
|
4039
|
+
* @param callback The function to call when the event was fired
|
|
4040
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4041
|
+
*
|
|
4042
|
+
* @category Comment Events
|
|
4043
|
+
*/
|
|
4044
|
+
const onCommentUpdated = (callback) => createCommentEventSubscriber('comment.updated', callback);
|
|
4045
|
+
|
|
4046
|
+
/**
|
|
4047
|
+
* ```js
|
|
4048
|
+
* import { onCommentDeleted } from '@amityco/ts-sdk'
|
|
4049
|
+
* const dispose = onCommentDeleted(comment => {
|
|
4050
|
+
* // ...
|
|
4051
|
+
* })
|
|
4052
|
+
* ```
|
|
4053
|
+
*
|
|
4054
|
+
* Fired when a {@link Amity.Comment} has been deleted
|
|
4055
|
+
*
|
|
4056
|
+
* @param callback The function to call when the event was fired
|
|
4057
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4058
|
+
*
|
|
4059
|
+
* @category Comment Events
|
|
4060
|
+
*/
|
|
4061
|
+
const onCommentDeleted = (callback) => createCommentEventSubscriber('comment.deleted', callback);
|
|
4062
|
+
|
|
4063
|
+
/**
|
|
4064
|
+
* ```js
|
|
4065
|
+
* import { onCommentFlagged } from '@amityco/ts-sdk'
|
|
4066
|
+
* const dispose = onCommentFlagged(comment => {
|
|
4067
|
+
* // ...
|
|
4068
|
+
* })
|
|
4069
|
+
* ```
|
|
4070
|
+
*
|
|
4071
|
+
* Fired when a {@link Amity.Comment} has been flagged
|
|
4072
|
+
*
|
|
4073
|
+
* @param callback The function to call when the event was fired
|
|
4074
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4075
|
+
*
|
|
4076
|
+
* @category Comment Events
|
|
4077
|
+
*/
|
|
4078
|
+
const onCommentFlagged = (callback) => createCommentEventSubscriber('comment.flagged', callback);
|
|
4079
|
+
|
|
4080
|
+
/**
|
|
4081
|
+
* ```js
|
|
4082
|
+
* import { onCommentUnflagged } from '@amityco/ts-sdk'
|
|
4083
|
+
* const dispose = onCommentUnflagged(comment => {
|
|
4084
|
+
* // ...
|
|
4085
|
+
* })
|
|
4086
|
+
* ```
|
|
4087
|
+
*
|
|
4088
|
+
* Fired when a flag has been removed from a {@link Amity.Comment}
|
|
4089
|
+
*
|
|
4090
|
+
* @param callback The function to call when the event was fired
|
|
4091
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4092
|
+
*
|
|
4093
|
+
* @category Comment Events
|
|
4094
|
+
*/
|
|
4095
|
+
const onCommentUnflagged = (callback) => createCommentEventSubscriber('comment.unflagged', callback);
|
|
4096
|
+
|
|
4097
|
+
/**
|
|
4098
|
+
* ```js
|
|
4099
|
+
* import { onCommentReactionAdded } from '@amityco/ts-sdk'
|
|
4100
|
+
* const dispose = onCommentReactionAdded(comment => {
|
|
4101
|
+
* // ...
|
|
4102
|
+
* })
|
|
4103
|
+
* ```
|
|
4104
|
+
*
|
|
4105
|
+
* Fired when a {@link Amity.Comment} has been reacted
|
|
4106
|
+
*
|
|
4107
|
+
* @param callback The function to call when the event was fired
|
|
4108
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4109
|
+
*
|
|
4110
|
+
* @category Comment Events
|
|
4111
|
+
*/
|
|
4112
|
+
const onCommentReactionAdded = (callback) => {
|
|
4113
|
+
const client = getActiveClient();
|
|
4114
|
+
const filter = (payload) => {
|
|
4115
|
+
var _a, _b;
|
|
4116
|
+
const [cached] = (_a = pullFromCache('comment', payload.comments[0].commentId)) !== null && _a !== void 0 ? _a : [];
|
|
4117
|
+
const commentPayload = __rest(payload, ["reactor"]);
|
|
4118
|
+
decoratePayload(commentPayload);
|
|
4119
|
+
preparePayloadForCache('comment.addReaction', payload);
|
|
4120
|
+
ingestInCache(commentPayload);
|
|
4121
|
+
const [comment] = (_b = pullFromCache('comment', payload.comments[0].commentId)) !== null && _b !== void 0 ? _b : [];
|
|
4122
|
+
if (cached && isMatch(cached, comment)) {
|
|
4123
|
+
return;
|
|
4124
|
+
}
|
|
4125
|
+
callback(comment);
|
|
4126
|
+
};
|
|
4127
|
+
return createEventSubscriber(client, 'comment.addReaction', 'comment.addReaction', filter);
|
|
4128
|
+
};
|
|
4129
|
+
|
|
4130
|
+
/**
|
|
4131
|
+
* ```js
|
|
4132
|
+
* import { onCommentReactionRemoved } from '@amityco/ts-sdk'
|
|
4133
|
+
* const dispose = onCommentReactionRemoved(comment => {
|
|
4134
|
+
* // ...
|
|
4135
|
+
* })
|
|
4136
|
+
* ```
|
|
4137
|
+
*
|
|
4138
|
+
* Fired when a reaction has been removed from a {@link Amity.Comment}
|
|
4139
|
+
*
|
|
4140
|
+
* @param callback The function to call when the event was fired
|
|
4141
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4142
|
+
*
|
|
4143
|
+
* @category Comment Events
|
|
4144
|
+
*/
|
|
4145
|
+
const onCommentReactionRemoved = (callback) => {
|
|
4146
|
+
const client = getActiveClient();
|
|
4147
|
+
const filter = (payload) => {
|
|
4148
|
+
var _a, _b;
|
|
4149
|
+
const [cached] = (_a = pullFromCache('comment', payload.comments[0].commentId)) !== null && _a !== void 0 ? _a : [];
|
|
4150
|
+
const commentPayload = __rest(payload, ["reactor"]);
|
|
4151
|
+
decoratePayload(commentPayload);
|
|
4152
|
+
preparePayloadForCache('comment.removeReaction', payload);
|
|
4153
|
+
ingestInCache(commentPayload);
|
|
4154
|
+
const [comment] = (_b = pullFromCache('comment', payload.comments[0].commentId)) !== null && _b !== void 0 ? _b : [];
|
|
4155
|
+
if (cached && isMatch(cached, comment)) {
|
|
4156
|
+
return;
|
|
4157
|
+
}
|
|
4158
|
+
callback(comment);
|
|
4159
|
+
};
|
|
4160
|
+
return createEventSubscriber(client, 'comment.removeReaction', 'comment.removeReaction', filter);
|
|
4161
|
+
};
|
|
4162
|
+
|
|
4163
|
+
/**
|
|
4164
|
+
* ```js
|
|
4165
|
+
* import { onReactionRemoved } from '@amityco/ts-sdk'
|
|
4166
|
+
* const dispose = onReactionRemoved('post', postId, post => {
|
|
4167
|
+
* // ...
|
|
4168
|
+
* })
|
|
4169
|
+
* ```
|
|
4170
|
+
*
|
|
4171
|
+
* Fired when an {@link Amity.Reaction} has been removed
|
|
4172
|
+
*
|
|
4173
|
+
* @param {@link Amity.ReactableType} referenceType
|
|
4174
|
+
* @param {string} referenceId
|
|
4175
|
+
* @param callback The function to call when the event was fired
|
|
4176
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4177
|
+
*
|
|
4178
|
+
* @category Message Events
|
|
4179
|
+
**/
|
|
4180
|
+
const onReactionRemoved = (referenceType, referenceId, callback) => {
|
|
4181
|
+
const callbackWrapper = (model) => {
|
|
4182
|
+
if (getResolver(referenceType)(model) === referenceId) {
|
|
4183
|
+
callback(model);
|
|
4184
|
+
}
|
|
4185
|
+
};
|
|
4186
|
+
if (referenceType === 'message') {
|
|
4187
|
+
const client = getActiveClient();
|
|
4188
|
+
const filter = (payload) => {
|
|
4189
|
+
var _a, _b;
|
|
4190
|
+
const reactionsCountInCache = (_b = ((_a = pullFromCache('message', payload.messages[0].messageId)) !== null && _a !== void 0 ? _a : [])[0]) === null || _b === void 0 ? void 0 : _b.reactionsCount;
|
|
4191
|
+
const isReactionEvent = reactionsCountInCache !== payload.messages[0].reactionsCount;
|
|
4192
|
+
decoratePayload(payload);
|
|
4193
|
+
ingestInCache(payload);
|
|
4194
|
+
if (isReactionEvent) {
|
|
4195
|
+
callbackWrapper(payload.messages[0]);
|
|
4196
|
+
}
|
|
4197
|
+
};
|
|
4198
|
+
return createEventSubscriber(client, 'reaction/onReactionRemoved', 'v3.message.didUpdate', filter);
|
|
4199
|
+
}
|
|
4200
|
+
if (referenceType === 'post') {
|
|
4201
|
+
return onPostReactionRemoved(callbackWrapper);
|
|
4202
|
+
}
|
|
4203
|
+
return onCommentReactionRemoved(callbackWrapper);
|
|
4204
|
+
};
|
|
4205
|
+
|
|
4206
|
+
var REFERENCE_TYPES = {
|
|
4207
|
+
post: {
|
|
4208
|
+
domainName: 'posts',
|
|
4209
|
+
eventName: 'v3/post.isflagbyme',
|
|
4210
|
+
primaryKey: 'postId',
|
|
4211
|
+
},
|
|
4212
|
+
comment: {
|
|
4213
|
+
domainName: 'comments',
|
|
4214
|
+
eventName: 'v3/comment.isflagbyme',
|
|
4215
|
+
primaryKey: 'commentId',
|
|
4216
|
+
},
|
|
4217
|
+
message: {
|
|
4218
|
+
domainName: 'messages',
|
|
4219
|
+
eventName: 'v3/message.isFlaggedByMe',
|
|
4220
|
+
primaryKey: 'messageId',
|
|
4221
|
+
},
|
|
4222
|
+
user: {
|
|
4223
|
+
domainName: 'users',
|
|
4224
|
+
eventName: 'user.isFlagByMe',
|
|
4225
|
+
primaryKey: 'userId',
|
|
3437
4226
|
},
|
|
3438
4227
|
};
|
|
3439
4228
|
|
|
@@ -3455,9 +4244,12 @@ const createReport = async (referenceType, referenceId) => {
|
|
|
3455
4244
|
client.log('report/createReport', { referenceType, referenceId });
|
|
3456
4245
|
const { domainName } = REFERENCE_TYPES[referenceType];
|
|
3457
4246
|
const { data } = await client.http.post(`/api/v3/${domainName}/${referenceId}/flag`);
|
|
4247
|
+
const event = referenceType === 'message'
|
|
4248
|
+
? 'v3.message.didUpdate'
|
|
4249
|
+
: `${referenceType}.updated`;
|
|
3458
4250
|
decoratePayload(data);
|
|
3459
4251
|
ingestInCache(data);
|
|
3460
|
-
fireEvent(
|
|
4252
|
+
fireEvent(event, data);
|
|
3461
4253
|
return !!data;
|
|
3462
4254
|
};
|
|
3463
4255
|
|
|
@@ -3479,9 +4271,12 @@ const deleteReport = async (referenceType, referenceId) => {
|
|
|
3479
4271
|
client.log('report/deleteReport', { referenceType, referenceId });
|
|
3480
4272
|
const { domainName } = REFERENCE_TYPES[referenceType];
|
|
3481
4273
|
const { data } = await client.http.delete(`/api/v3/${domainName}/${referenceId}/unflag`);
|
|
4274
|
+
const event = referenceType === 'message'
|
|
4275
|
+
? 'v3.message.didUpdate'
|
|
4276
|
+
: `${referenceType}.updated`;
|
|
3482
4277
|
decoratePayload(data);
|
|
3483
4278
|
ingestInCache(data);
|
|
3484
|
-
fireEvent(
|
|
4279
|
+
fireEvent(event, data);
|
|
3485
4280
|
return !!data;
|
|
3486
4281
|
};
|
|
3487
4282
|
|
|
@@ -4251,166 +5046,253 @@ const observeMessage = (messageId, callback, policy = 'cache_then_server') => {
|
|
|
4251
5046
|
|
|
4252
5047
|
/**
|
|
4253
5048
|
* ```js
|
|
4254
|
-
* import { observeMessages } from '@amityco/ts-sdk'
|
|
5049
|
+
* import { observeMessages } from '@amityco/ts-sdk'
|
|
5050
|
+
*
|
|
5051
|
+
* let messages = []
|
|
5052
|
+
* const unsub = observeMessages(channelId, message => merge(messages, message))
|
|
5053
|
+
* ```
|
|
5054
|
+
*
|
|
5055
|
+
* Observe all mutations on a list of {@link Amity.Message} for a given {@link Amity.Channel} object
|
|
5056
|
+
*
|
|
5057
|
+
* @param client the client to observe the message with
|
|
5058
|
+
* @param channelId the ID of the channel where to observe the messages
|
|
5059
|
+
* @param callback the function to call when new data are available
|
|
5060
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
5061
|
+
*
|
|
5062
|
+
* @category Message Observer
|
|
5063
|
+
*/
|
|
5064
|
+
const observeMessages = (channelId, callback) => {
|
|
5065
|
+
const { log } = getActiveClient();
|
|
5066
|
+
const timestamp = Date.now();
|
|
5067
|
+
log(`observeMessages(tmpid: ${timestamp}) > listen`);
|
|
5068
|
+
const disposers = [];
|
|
5069
|
+
const router = (message, action) => {
|
|
5070
|
+
var _a, _b;
|
|
5071
|
+
if (message.channelId !== channelId)
|
|
5072
|
+
return;
|
|
5073
|
+
if (callback instanceof Function)
|
|
5074
|
+
return callback(message);
|
|
5075
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, message);
|
|
5076
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, message);
|
|
5077
|
+
};
|
|
5078
|
+
disposers.push(onMessageCreated(message => router(message, 'onCreate')), onMessageUpdated(message => router(message, 'onUpdate')), onMessageDeleted(message => router(message, 'onDelete')));
|
|
5079
|
+
return () => {
|
|
5080
|
+
log(`observeMessages(tmpid: ${timestamp}) > dispose`);
|
|
5081
|
+
disposers.forEach(fn => fn());
|
|
5082
|
+
};
|
|
5083
|
+
};
|
|
5084
|
+
|
|
5085
|
+
const createCommunityEventSubscriber = (event, callback) => {
|
|
5086
|
+
const client = getActiveClient();
|
|
5087
|
+
const filter = (payload) => {
|
|
5088
|
+
var _a, _b;
|
|
5089
|
+
const [cached] = (_a = pullFromCache('community', payload.communities[0].communityId)) !== null && _a !== void 0 ? _a : [];
|
|
5090
|
+
decoratePayload(payload);
|
|
5091
|
+
ingestInCache(payload);
|
|
5092
|
+
const [community] = (_b = pullFromCache('community', payload.communities[0].communityId)) !== null && _b !== void 0 ? _b : [];
|
|
5093
|
+
if (cached && isMatch(cached, community)) {
|
|
5094
|
+
return;
|
|
5095
|
+
}
|
|
5096
|
+
callback(community);
|
|
5097
|
+
};
|
|
5098
|
+
return createEventSubscriber(client, event, event, filter);
|
|
5099
|
+
};
|
|
5100
|
+
function hasPermission(member, payload, permission) {
|
|
5101
|
+
if (member.permissions.some(x => x === permission)) {
|
|
5102
|
+
return true;
|
|
5103
|
+
}
|
|
5104
|
+
return payload.users
|
|
5105
|
+
.find(user => user.userId === member.userId)
|
|
5106
|
+
.permissions.some(x => x === permission);
|
|
5107
|
+
}
|
|
5108
|
+
function getEventRelatedMember(event, payload) {
|
|
5109
|
+
if (event === 'community.joined' || event === 'community.left') {
|
|
5110
|
+
return payload.communityUsers[0];
|
|
5111
|
+
}
|
|
5112
|
+
// NOTE: backend returns the one who took the action and the one on whom
|
|
5113
|
+
// the action was taken. We need the 2nd one
|
|
5114
|
+
if (event === 'community.userRemoved') {
|
|
5115
|
+
return payload.communityUsers.filter(x => x.communityMembership === 'none')[0];
|
|
5116
|
+
}
|
|
5117
|
+
if (event === 'community.userBanned') {
|
|
5118
|
+
return payload.communityUsers.filter(x => x.communityMembership === 'banned')[0];
|
|
5119
|
+
}
|
|
5120
|
+
// NOTE: it might be that in certain edge cases permission check won't be enough
|
|
5121
|
+
if (event === 'community.userUnbanned') {
|
|
5122
|
+
return payload.communityUsers.filter(x => !hasPermission(x, payload, 'BAN_COMMUNITY_USER'))[0];
|
|
5123
|
+
}
|
|
5124
|
+
return payload.communityUsers.filter(x => !hasPermission(x, payload, 'ADD_COMMUNITY_USER'))[0];
|
|
5125
|
+
}
|
|
5126
|
+
const createCommunityMemberEventSubscriber = (event, callback) => {
|
|
5127
|
+
const client = getActiveClient();
|
|
5128
|
+
const filter = (payload) => {
|
|
5129
|
+
var _a, _b, _c, _d;
|
|
5130
|
+
const [cachedCommunity] = (_a = pullFromCache('community', payload.communities[0].communityId)) !== null && _a !== void 0 ? _a : [];
|
|
5131
|
+
const [cachedMember] = (_b = pullFromCache('communityUsers', getResolver('communityUsers')(getEventRelatedMember(event, payload)))) !== null && _b !== void 0 ? _b : [];
|
|
5132
|
+
decoratePayload(payload);
|
|
5133
|
+
ingestInCache(payload);
|
|
5134
|
+
const [community] = (_c = pullFromCache('community', payload.communities[0].communityId)) !== null && _c !== void 0 ? _c : [];
|
|
5135
|
+
const [member] = (_d = pullFromCache('communityUsers', getResolver('communityUsers')(getEventRelatedMember(event, payload)))) !== null && _d !== void 0 ? _d : [];
|
|
5136
|
+
if (cachedCommunity && isMatch(cachedCommunity, community) && cachedMember && isMatch(cachedMember, member)) {
|
|
5137
|
+
return;
|
|
5138
|
+
}
|
|
5139
|
+
callback(community, member);
|
|
5140
|
+
};
|
|
5141
|
+
return createEventSubscriber(client, event, event, filter);
|
|
5142
|
+
};
|
|
5143
|
+
|
|
5144
|
+
/**
|
|
5145
|
+
* ```js
|
|
5146
|
+
* import { onCommunityCreated } from '@amityco/ts-sdk'
|
|
5147
|
+
* const dispose = onCommunityCreated(community => {
|
|
5148
|
+
* // ...
|
|
5149
|
+
* })
|
|
5150
|
+
* ```
|
|
5151
|
+
*
|
|
5152
|
+
* Fired when {@link Amity.Community} have been created
|
|
5153
|
+
*
|
|
5154
|
+
* @param callback The function to call when the event was fired
|
|
5155
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
5156
|
+
*
|
|
5157
|
+
* @category Community Events
|
|
5158
|
+
*/
|
|
5159
|
+
const onCommunityCreated = (callback) => createCommunityEventSubscriber('community.created', callback);
|
|
5160
|
+
|
|
5161
|
+
/**
|
|
5162
|
+
* ```js
|
|
5163
|
+
* import { onCommunityUpdated } from '@amityco/ts-sdk'
|
|
5164
|
+
* const dispose = onCommunityUpdated(community => {
|
|
5165
|
+
* // ...
|
|
5166
|
+
* })
|
|
5167
|
+
* ```
|
|
5168
|
+
*
|
|
5169
|
+
* Fired when a {@link Amity.Community} has been updated
|
|
5170
|
+
*
|
|
5171
|
+
* @param callback The function to call when the event was fired
|
|
5172
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
5173
|
+
*
|
|
5174
|
+
* @category Community Events
|
|
5175
|
+
*/
|
|
5176
|
+
const onCommunityUpdated = (callback) => createCommunityEventSubscriber('community.updated', callback);
|
|
5177
|
+
|
|
5178
|
+
/**
|
|
5179
|
+
* ```js
|
|
5180
|
+
* import { onCommunityDeleted } from '@amityco/ts-sdk'
|
|
5181
|
+
* const dispose = onCommunityDeleted(community => {
|
|
5182
|
+
* // ...
|
|
5183
|
+
* })
|
|
5184
|
+
* ```
|
|
4255
5185
|
*
|
|
4256
|
-
*
|
|
4257
|
-
*
|
|
5186
|
+
* Fired when a {@link Amity.Community} has been deleted
|
|
5187
|
+
*
|
|
5188
|
+
* @param callback The function to call when the event was fired
|
|
5189
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
5190
|
+
*
|
|
5191
|
+
* @category Community Events
|
|
5192
|
+
*/
|
|
5193
|
+
const onCommunityDeleted = (callback) => createCommunityEventSubscriber('community.deleted', callback);
|
|
5194
|
+
|
|
5195
|
+
/**
|
|
5196
|
+
* ```js
|
|
5197
|
+
* import { onCommunityJoined } from '@amityco/ts-sdk'
|
|
5198
|
+
* const dispose = onCommunityJoined((community, member) => {
|
|
5199
|
+
* // ...
|
|
5200
|
+
* })
|
|
4258
5201
|
* ```
|
|
4259
5202
|
*
|
|
4260
|
-
*
|
|
5203
|
+
* Fired when a {@link Amity.Community} has been joined
|
|
4261
5204
|
*
|
|
4262
|
-
* @param
|
|
4263
|
-
* @
|
|
4264
|
-
* @param callback the function to call when new data are available
|
|
4265
|
-
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
5205
|
+
* @param callback The function to call when the event was fired
|
|
5206
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4266
5207
|
*
|
|
4267
|
-
* @category
|
|
5208
|
+
* @category Community Events
|
|
4268
5209
|
*/
|
|
4269
|
-
const
|
|
4270
|
-
const { log } = getActiveClient();
|
|
4271
|
-
const timestamp = Date.now();
|
|
4272
|
-
log(`observeMessages(tmpid: ${timestamp}) > listen`);
|
|
4273
|
-
const disposers = [];
|
|
4274
|
-
const router = (message, action) => {
|
|
4275
|
-
var _a, _b;
|
|
4276
|
-
if (message.channelId !== channelId)
|
|
4277
|
-
return;
|
|
4278
|
-
if (callback instanceof Function)
|
|
4279
|
-
return callback(message);
|
|
4280
|
-
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, message);
|
|
4281
|
-
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, message);
|
|
4282
|
-
};
|
|
4283
|
-
disposers.push(onMessageCreated(message => router(message, 'onCreate')), onMessageUpdated(message => router(message, 'onUpdate')), onMessageDeleted(message => router(message, 'onDelete')));
|
|
4284
|
-
return () => {
|
|
4285
|
-
log(`observeMessages(tmpid: ${timestamp}) > dispose`);
|
|
4286
|
-
disposers.forEach(fn => fn());
|
|
4287
|
-
};
|
|
4288
|
-
};
|
|
5210
|
+
const onCommunityJoined = (callback) => createCommunityMemberEventSubscriber('community.joined', callback);
|
|
4289
5211
|
|
|
4290
5212
|
/**
|
|
4291
5213
|
* ```js
|
|
4292
|
-
* import {
|
|
4293
|
-
* const dispose =
|
|
5214
|
+
* import { onCommunityLeft } from '@amityco/ts-sdk'
|
|
5215
|
+
* const dispose = onCommunityLeft((community, member) => {
|
|
4294
5216
|
* // ...
|
|
4295
5217
|
* })
|
|
4296
5218
|
* ```
|
|
4297
5219
|
*
|
|
4298
|
-
* Fired when
|
|
5220
|
+
* Fired when a {@link Amity.Community} has been left
|
|
4299
5221
|
*
|
|
4300
5222
|
* @param callback The function to call when the event was fired
|
|
4301
5223
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4302
5224
|
*
|
|
4303
5225
|
* @category Community Events
|
|
4304
5226
|
*/
|
|
4305
|
-
const
|
|
4306
|
-
const client = getActiveClient();
|
|
4307
|
-
const filter = (payload) => {
|
|
4308
|
-
decoratePayload(payload);
|
|
4309
|
-
ingestInCache(payload);
|
|
4310
|
-
callback(payload.communities[0]);
|
|
4311
|
-
};
|
|
4312
|
-
return createEventSubscriber(client, 'onCommunityCreated', 'v3.community.didCreate', filter);
|
|
4313
|
-
};
|
|
5227
|
+
const onCommunityLeft = (callback) => createCommunityMemberEventSubscriber('community.left', callback);
|
|
4314
5228
|
|
|
4315
5229
|
/**
|
|
4316
5230
|
* ```js
|
|
4317
|
-
* import {
|
|
4318
|
-
* const dispose =
|
|
5231
|
+
* import { onCommunityUserAdded } from '@amityco/ts-sdk'
|
|
5232
|
+
* const dispose = onCommunityUserAdded((community, member) => {
|
|
4319
5233
|
* // ...
|
|
4320
5234
|
* })
|
|
4321
5235
|
* ```
|
|
4322
5236
|
*
|
|
4323
|
-
* Fired when
|
|
5237
|
+
* Fired when a user has been added to a {@link Amity.Community}
|
|
4324
5238
|
*
|
|
4325
5239
|
* @param callback The function to call when the event was fired
|
|
4326
5240
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4327
5241
|
*
|
|
4328
5242
|
* @category Community Events
|
|
4329
5243
|
*/
|
|
4330
|
-
const
|
|
4331
|
-
const client = getActiveClient();
|
|
4332
|
-
const filter = (payload) => {
|
|
4333
|
-
decoratePayload(payload);
|
|
4334
|
-
ingestInCache(payload);
|
|
4335
|
-
callback(payload.communities[0]);
|
|
4336
|
-
};
|
|
4337
|
-
return createEventSubscriber(client, 'onCommunityUpdated', 'v3.community.didUpdate', filter);
|
|
4338
|
-
};
|
|
5244
|
+
const onCommunityUserAdded = (callback) => createCommunityMemberEventSubscriber('community.userAdded', callback);
|
|
4339
5245
|
|
|
4340
5246
|
/**
|
|
4341
5247
|
* ```js
|
|
4342
|
-
* import {
|
|
4343
|
-
* const dispose =
|
|
5248
|
+
* import { onCommunityUserRemoved } from '@amityco/ts-sdk'
|
|
5249
|
+
* const dispose = onCommunityUserRemoved((community, member) => {
|
|
4344
5250
|
* // ...
|
|
4345
5251
|
* })
|
|
4346
5252
|
* ```
|
|
4347
5253
|
*
|
|
4348
|
-
* Fired when
|
|
5254
|
+
* Fired when a user has been removed from a {@link Amity.Community}
|
|
4349
5255
|
*
|
|
4350
5256
|
* @param callback The function to call when the event was fired
|
|
4351
5257
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4352
5258
|
*
|
|
4353
5259
|
* @category Community Events
|
|
4354
5260
|
*/
|
|
4355
|
-
const
|
|
4356
|
-
const client = getActiveClient();
|
|
4357
|
-
const filter = (payload) => {
|
|
4358
|
-
decoratePayload(payload);
|
|
4359
|
-
ingestInCache(payload);
|
|
4360
|
-
callback(payload.communities[0]);
|
|
4361
|
-
};
|
|
4362
|
-
return createEventSubscriber(client, 'onCommunityDeleted', 'v3.community.didDelete', filter);
|
|
4363
|
-
};
|
|
5261
|
+
const onCommunityUserRemoved = (callback) => createCommunityMemberEventSubscriber('community.userRemoved', callback);
|
|
4364
5262
|
|
|
4365
5263
|
/**
|
|
4366
5264
|
* ```js
|
|
4367
|
-
* import {
|
|
4368
|
-
* const dispose =
|
|
5265
|
+
* import { onCommunityUserBanned } from '@amityco/ts-sdk'
|
|
5266
|
+
* const dispose = onCommunityUserBanned((community, member) => {
|
|
4369
5267
|
* // ...
|
|
4370
5268
|
* })
|
|
4371
5269
|
* ```
|
|
4372
5270
|
*
|
|
4373
|
-
* Fired when
|
|
5271
|
+
* Fired when a user has been banned in a {@link Amity.Community}
|
|
4374
5272
|
*
|
|
4375
5273
|
* @param callback The function to call when the event was fired
|
|
4376
5274
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4377
5275
|
*
|
|
4378
5276
|
* @category Community Events
|
|
4379
5277
|
*/
|
|
4380
|
-
const
|
|
4381
|
-
const client = getActiveClient();
|
|
4382
|
-
const filter = (payload) => {
|
|
4383
|
-
decoratePayload(payload);
|
|
4384
|
-
ingestInCache(payload);
|
|
4385
|
-
callback(payload.communities[0]);
|
|
4386
|
-
};
|
|
4387
|
-
return createEventSubscriber(client, 'onCommunityJoined', 'v3.community.didJoin', filter);
|
|
4388
|
-
};
|
|
5278
|
+
const onCommunityUserBanned = (callback) => createCommunityMemberEventSubscriber('community.userBanned', callback);
|
|
4389
5279
|
|
|
4390
5280
|
/**
|
|
4391
5281
|
* ```js
|
|
4392
|
-
* import {
|
|
4393
|
-
* const dispose =
|
|
5282
|
+
* import { onCommunityUserUnbanned } from '@amityco/ts-sdk'
|
|
5283
|
+
* const dispose = onCommunityUserUnbanned((community, member) => {
|
|
4394
5284
|
* // ...
|
|
4395
5285
|
* })
|
|
4396
5286
|
* ```
|
|
4397
5287
|
*
|
|
4398
|
-
* Fired when
|
|
5288
|
+
* Fired when a user has been unbanned in a {@link Amity.Community}
|
|
4399
5289
|
*
|
|
4400
5290
|
* @param callback The function to call when the event was fired
|
|
4401
5291
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4402
5292
|
*
|
|
4403
5293
|
* @category Community Events
|
|
4404
5294
|
*/
|
|
4405
|
-
const
|
|
4406
|
-
const client = getActiveClient();
|
|
4407
|
-
const filter = (payload) => {
|
|
4408
|
-
decoratePayload(payload);
|
|
4409
|
-
ingestInCache(payload);
|
|
4410
|
-
callback(payload.communities[0]);
|
|
4411
|
-
};
|
|
4412
|
-
return createEventSubscriber(client, 'onCommunityLeft', 'v3.community.didLeave', filter);
|
|
4413
|
-
};
|
|
5295
|
+
const onCommunityUserUnbanned = (callback) => createCommunityMemberEventSubscriber('community.userUnbanned', callback);
|
|
4414
5296
|
|
|
4415
5297
|
/**
|
|
4416
5298
|
* ```js
|
|
@@ -4434,19 +5316,22 @@ const observeCommunity = (communityId, callback) => {
|
|
|
4434
5316
|
log(`observeCommunity(tmpid: ${timestamp}) > listen`);
|
|
4435
5317
|
// wrapper function to make sure
|
|
4436
5318
|
const router = (result, action) => {
|
|
4437
|
-
var _a, _b
|
|
4438
|
-
// filter function
|
|
4439
|
-
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.communityId) !== communityId)
|
|
4440
|
-
return;
|
|
5319
|
+
var _a, _b;
|
|
4441
5320
|
if (callback instanceof Function)
|
|
4442
5321
|
return callback(result);
|
|
4443
5322
|
if (action !== 'onFetch')
|
|
4444
|
-
(
|
|
4445
|
-
(
|
|
5323
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, result);
|
|
5324
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, result);
|
|
5325
|
+
};
|
|
5326
|
+
const realtimeRouter = (result, action) => {
|
|
5327
|
+
var _a;
|
|
5328
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.communityId) !== communityId)
|
|
5329
|
+
return;
|
|
5330
|
+
router(result, action);
|
|
4446
5331
|
};
|
|
4447
5332
|
const disposers = [];
|
|
4448
|
-
disposers.push(onCommunityUpdated(data =>
|
|
4449
|
-
runQuery(createQuery(getCommunity, communityId), result =>
|
|
5333
|
+
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')));
|
|
5334
|
+
runQuery(createQuery(getCommunity, communityId), result => router(result, 'onFetch'));
|
|
4450
5335
|
return () => {
|
|
4451
5336
|
log(`observeCommunity(tmpid: ${timestamp}) > dispose`);
|
|
4452
5337
|
disposers.forEach(fn => fn());
|
|
@@ -4483,81 +5368,6 @@ const queryGlobalFeed = async (query) => {
|
|
|
4483
5368
|
return { data: toRecord(posts), nextPage, prevPage };
|
|
4484
5369
|
};
|
|
4485
5370
|
|
|
4486
|
-
/**
|
|
4487
|
-
* ```js
|
|
4488
|
-
* import { onPostCreated } from '@amityco/ts-sdk'
|
|
4489
|
-
* const dispose = onPostCreated(post => {
|
|
4490
|
-
* // ...
|
|
4491
|
-
* })
|
|
4492
|
-
* ```
|
|
4493
|
-
*
|
|
4494
|
-
* Fired when an {@link Amity.Post} has been created
|
|
4495
|
-
*
|
|
4496
|
-
* @param callback The function to call when the event was fired
|
|
4497
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4498
|
-
*
|
|
4499
|
-
* @category Post Events
|
|
4500
|
-
*/
|
|
4501
|
-
const onPostCreated = (callback) => {
|
|
4502
|
-
const client = getActiveClient();
|
|
4503
|
-
const filter = (payload) => {
|
|
4504
|
-
decoratePayload(payload);
|
|
4505
|
-
ingestInCache(payload);
|
|
4506
|
-
callback(payload.posts[0]);
|
|
4507
|
-
};
|
|
4508
|
-
return createEventSubscriber(client, 'message/onPostCreated', 'v3.post.didCreate', filter);
|
|
4509
|
-
};
|
|
4510
|
-
|
|
4511
|
-
/**
|
|
4512
|
-
* ```js
|
|
4513
|
-
* import { onPostUpdated } from '@amityco/ts-sdk'
|
|
4514
|
-
* const dispose = onPostUpdated(post => {
|
|
4515
|
-
* // ...
|
|
4516
|
-
* })
|
|
4517
|
-
* ```
|
|
4518
|
-
*
|
|
4519
|
-
* Fired when an {@link Amity.Post} has been updated
|
|
4520
|
-
*
|
|
4521
|
-
* @param callback The function to call when the event was fired
|
|
4522
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4523
|
-
*
|
|
4524
|
-
* @category Post Events
|
|
4525
|
-
*/
|
|
4526
|
-
const onPostUpdated = (callback) => {
|
|
4527
|
-
const client = getActiveClient();
|
|
4528
|
-
const filter = (payload) => {
|
|
4529
|
-
decoratePayload(payload);
|
|
4530
|
-
ingestInCache(payload);
|
|
4531
|
-
callback(payload.posts[0]);
|
|
4532
|
-
};
|
|
4533
|
-
return createEventSubscriber(client, 'message/onPostUpdated', 'v3.post.didUpdate', filter);
|
|
4534
|
-
};
|
|
4535
|
-
|
|
4536
|
-
/**
|
|
4537
|
-
* ```js
|
|
4538
|
-
* import { onPostDeleted } from '@amityco/ts-sdk'
|
|
4539
|
-
* const dispose = onPostDeleted(post => {
|
|
4540
|
-
* // ...
|
|
4541
|
-
* })
|
|
4542
|
-
* ```
|
|
4543
|
-
*
|
|
4544
|
-
* Fired when an {@link Amity.Post} has been deleted
|
|
4545
|
-
*
|
|
4546
|
-
* @param callback The function to call when the event was fired
|
|
4547
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4548
|
-
*
|
|
4549
|
-
* @category Post Events
|
|
4550
|
-
*/
|
|
4551
|
-
const onPostDeleted = (callback) => {
|
|
4552
|
-
const client = getActiveClient();
|
|
4553
|
-
const filter = (payload) => {
|
|
4554
|
-
decoratePayload(payload);
|
|
4555
|
-
ingestInCache(payload);
|
|
4556
|
-
callback(payload.posts[0]);
|
|
4557
|
-
};
|
|
4558
|
-
return createEventSubscriber(client, 'message/onPostDeleted', 'v3.post.didDelete', filter);
|
|
4559
|
-
};
|
|
4560
|
-
|
|
4561
5371
|
/**
|
|
4562
5372
|
* ```js
|
|
4563
5373
|
* import { observePosts } from '@amityco/ts-sdk'
|
|
@@ -4593,7 +5403,7 @@ const observePosts = (params, callback) => {
|
|
|
4593
5403
|
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, post);
|
|
4594
5404
|
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, post);
|
|
4595
5405
|
};
|
|
4596
|
-
disposers.push(onPostCreated(post => router(post, 'onCreate')), onPostUpdated(post => router(post, 'onUpdate')), onPostDeleted(post => router(post, 'onDelete')));
|
|
5406
|
+
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')));
|
|
4597
5407
|
return () => {
|
|
4598
5408
|
log(`observePosts(tmpid: ${timestamp}) > dispose`);
|
|
4599
5409
|
disposers.forEach(fn => fn());
|
|
@@ -4622,100 +5432,28 @@ const observePost = (postId, callback, policy = 'cache_then_server') => {
|
|
|
4622
5432
|
log(`observePost(tmpid: ${timestamp}) > listen`);
|
|
4623
5433
|
// wrapper function to make sure
|
|
4624
5434
|
const router = (result, action) => {
|
|
4625
|
-
var _a, _b
|
|
4626
|
-
// filter function
|
|
4627
|
-
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.postId) !== postId)
|
|
4628
|
-
return;
|
|
5435
|
+
var _a, _b;
|
|
4629
5436
|
if (callback instanceof Function)
|
|
4630
5437
|
return callback(result);
|
|
4631
5438
|
if (action !== 'onFetch')
|
|
4632
|
-
(
|
|
4633
|
-
(
|
|
5439
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, result);
|
|
5440
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, result);
|
|
5441
|
+
};
|
|
5442
|
+
const realtimeRouter = (result, action) => {
|
|
5443
|
+
var _a;
|
|
5444
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.postId) !== postId)
|
|
5445
|
+
return;
|
|
5446
|
+
router(result, action);
|
|
4634
5447
|
};
|
|
4635
5448
|
const disposers = [];
|
|
4636
|
-
disposers.push(onPostUpdated(data =>
|
|
4637
|
-
runQuery(createQuery(getPost, postId), (result) =>
|
|
5449
|
+
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')));
|
|
5450
|
+
runQuery(createQuery(getPost, postId), (result) => router(result, 'onFetch'), queryOptions(policy));
|
|
4638
5451
|
return () => {
|
|
4639
5452
|
log(`observePosts(tmpid: ${timestamp}) > dispose`);
|
|
4640
5453
|
disposers.forEach(fn => fn());
|
|
4641
5454
|
};
|
|
4642
5455
|
};
|
|
4643
5456
|
|
|
4644
|
-
/**
|
|
4645
|
-
* ```js
|
|
4646
|
-
* import { onCommentCreated } from '@amityco/ts-sdk'
|
|
4647
|
-
* const dispose = onCommentCreated(comment => {
|
|
4648
|
-
* // ...
|
|
4649
|
-
* })
|
|
4650
|
-
* ```
|
|
4651
|
-
*
|
|
4652
|
-
* Fired when an {@link Amity.Comment} has been created
|
|
4653
|
-
*
|
|
4654
|
-
* @param callback The function to call when the event was fired
|
|
4655
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4656
|
-
*
|
|
4657
|
-
* @category Comment Events
|
|
4658
|
-
*/
|
|
4659
|
-
const onCommentCreated = (callback) => {
|
|
4660
|
-
const client = getActiveClient();
|
|
4661
|
-
const filter = (payload) => {
|
|
4662
|
-
decoratePayload(payload);
|
|
4663
|
-
ingestInCache(payload);
|
|
4664
|
-
callback(payload.comments[0]);
|
|
4665
|
-
};
|
|
4666
|
-
return createEventSubscriber(client, 'message/onCommentCreated', 'v3.comment.didCreate', filter);
|
|
4667
|
-
};
|
|
4668
|
-
|
|
4669
|
-
/**
|
|
4670
|
-
* ```js
|
|
4671
|
-
* import { onCommentUpdated } from '@amityco/ts-sdk'
|
|
4672
|
-
* const dispose = onCommentUpdated(comment => {
|
|
4673
|
-
* // ...
|
|
4674
|
-
* })
|
|
4675
|
-
* ```
|
|
4676
|
-
*
|
|
4677
|
-
* Fired when an {@link Amity.Comment} has been updated
|
|
4678
|
-
*
|
|
4679
|
-
* @param callback The function to call when the event was fired
|
|
4680
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4681
|
-
*
|
|
4682
|
-
* @category Comment Events
|
|
4683
|
-
*/
|
|
4684
|
-
const onCommentUpdated = (callback) => {
|
|
4685
|
-
const client = getActiveClient();
|
|
4686
|
-
const filter = (payload) => {
|
|
4687
|
-
decoratePayload(payload);
|
|
4688
|
-
ingestInCache(payload);
|
|
4689
|
-
callback(payload.comments[0]);
|
|
4690
|
-
};
|
|
4691
|
-
return createEventSubscriber(client, 'message/onCommentUpdated', 'v3.comment.didUpdate', filter);
|
|
4692
|
-
};
|
|
4693
|
-
|
|
4694
|
-
/**
|
|
4695
|
-
* ```js
|
|
4696
|
-
* import { onCommentDeleted } from '@amityco/ts-sdk'
|
|
4697
|
-
* const dispose = onCommentDeleted(comment => {
|
|
4698
|
-
* // ...
|
|
4699
|
-
* })
|
|
4700
|
-
* ```
|
|
4701
|
-
*
|
|
4702
|
-
* Fired when an {@link Amity.Comment} has been deleted
|
|
4703
|
-
*
|
|
4704
|
-
* @param callback The function to call when the event was fired
|
|
4705
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4706
|
-
*
|
|
4707
|
-
* @category Comment Events
|
|
4708
|
-
*/
|
|
4709
|
-
const onCommentDeleted = (callback) => {
|
|
4710
|
-
const client = getActiveClient();
|
|
4711
|
-
const filter = (payload) => {
|
|
4712
|
-
decoratePayload(payload);
|
|
4713
|
-
ingestInCache(payload);
|
|
4714
|
-
callback(payload.comments[0]);
|
|
4715
|
-
};
|
|
4716
|
-
return createEventSubscriber(client, 'message/onCommentDeleted', 'v3.comment.didDelete', filter);
|
|
4717
|
-
};
|
|
4718
|
-
|
|
4719
5457
|
/**
|
|
4720
5458
|
* ```js
|
|
4721
5459
|
* import { observeComments } from '@amityco/ts-sdk'
|
|
@@ -4746,7 +5484,7 @@ const observeComments = (postId, callback) => {
|
|
|
4746
5484
|
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, comment);
|
|
4747
5485
|
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, comment);
|
|
4748
5486
|
};
|
|
4749
|
-
disposers.push(onCommentCreated(comment => router(comment, 'onCreate')), onCommentUpdated(comment => router(comment, 'onUpdate')), onCommentDeleted(comment => router(comment, 'onDelete')));
|
|
5487
|
+
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')));
|
|
4750
5488
|
return () => {
|
|
4751
5489
|
log(`observeComments(tmpid: ${timestamp}) > dispose`);
|
|
4752
5490
|
disposers.forEach(fn => fn());
|
|
@@ -4775,19 +5513,22 @@ const observeComment = (commentId, callback, policy = 'cache_then_server') => {
|
|
|
4775
5513
|
log(`observeComment(tmpid: ${timestamp}) > listen`);
|
|
4776
5514
|
// wrapper function to make sure
|
|
4777
5515
|
const router = (result, action) => {
|
|
4778
|
-
var _a, _b
|
|
4779
|
-
// filter function
|
|
4780
|
-
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.commentId) !== commentId)
|
|
4781
|
-
return;
|
|
5516
|
+
var _a, _b;
|
|
4782
5517
|
if (callback instanceof Function)
|
|
4783
5518
|
return callback(result);
|
|
4784
5519
|
if (action !== 'onFetch')
|
|
4785
|
-
(
|
|
4786
|
-
(
|
|
5520
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, result);
|
|
5521
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, result);
|
|
5522
|
+
};
|
|
5523
|
+
const realtimeRouter = (result, action) => {
|
|
5524
|
+
var _a;
|
|
5525
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.commentId) !== commentId)
|
|
5526
|
+
return;
|
|
5527
|
+
router(result, action);
|
|
4787
5528
|
};
|
|
4788
5529
|
const disposers = [];
|
|
4789
|
-
disposers.push(onCommentUpdated(data =>
|
|
4790
|
-
runQuery(createQuery(getComment, commentId), result =>
|
|
5530
|
+
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')));
|
|
5531
|
+
runQuery(createQuery(getComment, commentId), result => router(result, 'onFetch'), queryOptions(policy));
|
|
4791
5532
|
return () => {
|
|
4792
5533
|
log(`observeComment(tmpid: ${timestamp}) > dispose`);
|
|
4793
5534
|
disposers.forEach(fn => fn());
|
|
@@ -4987,4 +5728,4 @@ const disposeStream = async (streamId) => {
|
|
|
4987
5728
|
return videoStreamings.find(stream => stream.streamId === streamId);
|
|
4988
5729
|
};
|
|
4989
5730
|
|
|
4990
|
-
export { addChannelMembers, addChannelMembersRole, addCommunityMembers, addCommunityMembersRole, addReaction, clearCache, connectClient, createCache, createChannel, createClient, createComment, createCommunity, createFile, createMessage, createPost, createQuery, createReport, createStream, decorateModel, decorateModels, decoratePayload, deleteChannel, deleteComment, deleteCommunity, deleteFile, deleteMessage, deletePost, deleteReport, deleteStream, disconnectClient, disposeStream, dropFromCache, enableCache, fileUrlWithSize, filterByPropEquality, getActiveClient, getChannel, getChannels, getComment, getComments, getCommunities, getCommunity, getFile, getMessage, getMessages, getPost, getPosts, getRole, getStream, getUser, getUsers, ingestInCache, isAfterBefore, isConnected, isFetcher, isFresh, isLocal, isMutator, isOffline, isPaged, isReportedByMe, isSkipLimit, isSyncable, isSynced, joinChannel, joinCommunity, leaveChannel, leaveCommunity, memoryStore, memoryStores, mergeInCache, mutateCache, observeChannel, observeComment, observeComments, observeCommunity, observeFile, observeMessage, observeMessages, observePost, observePosts, observeUser, onChannelDeleted, onChannelJoined, onChannelLeft, onChannelUpdated, onClientBanned, onClientDisconnected, onCommentCreated, onCommentDeleted, onCommentUpdated, onCommunityCreated, onCommunityDeleted, onCommunityJoined, onCommunityLeft, onCommunityUpdated, onConnectionError, onMemberAdded, onMemberRemoved, onMessageCreated, onMessageDeleted, onMessageUpdated, onPostCreated, onPostDeleted, onPostUpdated, onReactionRemoved, onUserUpdated, persistentStore, pullFromCache, pushToCache, queryChannelMembers, queryChannels, queryComments, queryCommunities, queryCommunityMembers, queryGlobalFeed, queryMessages, queryOptions, queryPosts, queryRoles, queryStreams, queryUsers, removeChannelMembers, removeChannelMembersRole, removeCommunityMembers, removeCommunityMembersRole, removeReaction, runQuery, sliceByPaging, sortByChannelSegment, sortByDisplayName, sortByFirstCreated, sortByLastActivity, sortByLastCreated, sortBySegmentNumber, syncInCache, toPage, toToken, updateChannel, updateComment, updateCommunity, updateMessage, updatePost, updateStream, updateUser };
|
|
5731
|
+
export { SubscriptionLevels, VERSION$1 as VERSION, addChannelMembers, addChannelMembersRole, addCommunityMembers, addCommunityMembersRole, addReaction, clearCache, connectClient, createCache, createChannel, createClient, createComment, createCommunity, createFile, createMessage, createPost, createQuery, createReport, createStream, decorateModel, decorateModels, decoratePayload, deleteChannel, deleteComment, deleteCommunity, deleteFile, deleteMessage, deletePost, deleteReport, deleteStream, disconnectClient, disposeStream, dropFromCache, enableCache, fileUrlWithSize, filterByPropEquality, getActiveClient, getChannel, getChannels, getComment, getCommentTopic, getComments, getCommunities, getCommunity, getCommunityTopic, getFile, getMessage, getMessages, getPost, getPostTopic, getPosts, getRole, getStream, getUser, getUserTopic, getUsers, ingestInCache, isAfterBefore, isConnected, isFetcher, isFresh, isLocal, isMutator, isOffline, isPaged, isReportedByMe, isSkipLimit, isSyncable, isSynced, joinChannel, joinCommunity, leaveChannel, leaveCommunity, memoryStore, memoryStores, mergeInCache, mutateCache, observeChannel, observeComment, observeComments, observeCommunity, observeFile, observeMessage, observeMessages, observePost, observePosts, observeUser, onChannelDeleted, onChannelJoined, onChannelLeft, onChannelUpdated, onClientBanned, onClientDisconnected, onCommentCreated, onCommentDeleted, onCommentFlagged, onCommentReactionAdded, onCommentReactionRemoved, onCommentUnflagged, onCommentUpdated, onCommunityCreated, onCommunityDeleted, onCommunityJoined, onCommunityLeft, onCommunityUpdated, onCommunityUserAdded, onCommunityUserBanned, onCommunityUserRemoved, onCommunityUserUnbanned, onConnectionError, onMemberAdded, onMemberRemoved, onMessageCreated, onMessageDeleted, onMessageUpdated, onPostApproved, onPostCreated, onPostDeclined, onPostDeleted, onPostFlagged, onPostReactionAdded, onPostReactionRemoved, onPostUnflagged, onPostUpdated, onReactionRemoved, onUserFlagCleared, onUserFlagged, onUserUnflagged, onUserUpdated, persistentStore, pullFromCache, pushToCache, queryChannelMembers, queryChannels, queryComments, queryCommunities, queryCommunityMembers, queryGlobalFeed, queryMessages, queryOptions, queryPosts, queryRoles, queryStreams, queryUsers, removeChannelMembers, removeChannelMembersRole, removeCommunityMembers, removeCommunityMembersRole, removeReaction, runQuery, sliceByPaging, sortByChannelSegment, sortByDisplayName, sortByFirstCreated, sortByLastActivity, sortByLastCreated, sortBySegmentNumber, subscribeTopic, syncInCache, toPage, toToken, updateChannel, updateComment, updateCommunity, updateMessage, updatePost, updateStream, updateUser };
|