@amityco/ts-sdk 6.12.2-d8fe0d5.0 → 6.12.2-e1d8270.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/@types/core/live.d.ts +2 -0
  2. package/dist/@types/core/live.d.ts.map +1 -1
  3. package/dist/@types/core/paging.d.ts +8 -0
  4. package/dist/@types/core/paging.d.ts.map +1 -1
  5. package/dist/@types/domains/community.d.ts +1 -1
  6. package/dist/@types/domains/community.d.ts.map +1 -1
  7. package/dist/@types/domains/message.d.ts +1 -0
  8. package/dist/@types/domains/message.d.ts.map +1 -1
  9. package/dist/core/liveCollection/LiveCollectionController.d.ts +24 -0
  10. package/dist/core/liveCollection/LiveCollectionController.d.ts.map +1 -0
  11. package/dist/core/liveCollection/PaginationController.d.ts +15 -0
  12. package/dist/core/liveCollection/PaginationController.d.ts.map +1 -0
  13. package/dist/core/liveCollection/QueryStreamController.d.ts +8 -0
  14. package/dist/core/liveCollection/QueryStreamController.d.ts.map +1 -0
  15. package/dist/core/liveCollection/hash.d.ts +2 -0
  16. package/dist/core/liveCollection/hash.d.ts.map +1 -0
  17. package/dist/index.cjs.js +353 -163
  18. package/dist/index.esm.js +352 -163
  19. package/dist/index.umd.js +3 -3
  20. package/dist/messageRepository/observers/getMessages.d.ts.map +1 -1
  21. package/dist/messageRepository/observers/liveCollection/MessageLiveCollectionController.d.ts +14 -0
  22. package/dist/messageRepository/observers/liveCollection/MessageLiveCollectionController.d.ts.map +1 -0
  23. package/dist/messageRepository/observers/liveCollection/MessagePaginationController.d.ts +13 -0
  24. package/dist/messageRepository/observers/liveCollection/MessagePaginationController.d.ts.map +1 -0
  25. package/dist/messageRepository/observers/liveCollection/MessageQueryStreamController.d.ts +13 -0
  26. package/dist/messageRepository/observers/liveCollection/MessageQueryStreamController.d.ts.map +1 -0
  27. package/dist/messageRepository/utils/prepareMessagePayload.d.ts +1 -1
  28. package/dist/messageRepository/utils/prepareMessagePayload.d.ts.map +1 -1
  29. package/dist/utils/isEqual.d.ts.map +1 -1
  30. package/package.json +3 -1
  31. package/src/@types/core/live.ts +2 -0
  32. package/src/@types/core/paging.ts +8 -2
  33. package/src/@types/domains/community.ts +1 -1
  34. package/src/@types/domains/message.ts +1 -0
  35. package/src/communityRepository/communityMembership/observers/getMembers.ts +2 -2
  36. package/src/core/liveCollection/LiveCollectionController.ts +105 -0
  37. package/src/core/liveCollection/PaginationController.ts +62 -0
  38. package/src/core/liveCollection/QueryStreamController.ts +23 -0
  39. package/src/core/liveCollection/hash.ts +16 -0
  40. package/src/messageRepository/observers/getMessages.ts +7 -107
  41. package/src/messageRepository/observers/liveCollection/MessageLiveCollectionController.ts +162 -0
  42. package/src/messageRepository/observers/liveCollection/MessagePaginationController.ts +42 -0
  43. package/src/messageRepository/observers/liveCollection/MessageQueryStreamController.ts +129 -0
  44. package/src/messageRepository/observers/tests/getMessages.test.ts +35 -27
  45. package/src/messageRepository/utils/prepareMessagePayload.ts +3 -2
  46. package/src/utils/isEqual.ts +13 -0
package/dist/index.cjs.js CHANGED
@@ -10,6 +10,7 @@ var axios = require('axios');
10
10
  var HttpAgent = require('agentkeepalive');
11
11
  var io = require('socket.io-client');
12
12
  var uuid$1 = require('react-native-uuid');
13
+ var hash = require('object-hash');
13
14
  var Hls = require('hls.js');
14
15
 
15
16
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -21,6 +22,7 @@ var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
21
22
  var HttpAgent__default = /*#__PURE__*/_interopDefaultLegacy(HttpAgent);
22
23
  var io__default = /*#__PURE__*/_interopDefaultLegacy(io);
23
24
  var uuid__default = /*#__PURE__*/_interopDefaultLegacy(uuid$1);
25
+ var hash__default = /*#__PURE__*/_interopDefaultLegacy(hash);
24
26
  var Hls__default = /*#__PURE__*/_interopDefaultLegacy(Hls);
25
27
 
26
28
  const CommunityPostSettings = Object.freeze({
@@ -5570,6 +5572,7 @@ function convertParams(_a) {
5570
5572
  }
5571
5573
  function convertQueryParams$1(_a) {
5572
5574
  var { page, sortBy, subChannelId, tags, includeDeleted } = _a, rest = __rest(_a, ["page", "sortBy", "subChannelId", "tags", "includeDeleted"]);
5575
+ console.log('convertQueryParams', page);
5573
5576
  const out = Object.assign(Object.assign({}, rest), { messageFeedId: subChannelId, isDeleted: inferIsDeleted(includeDeleted), options: { token: toToken(page || { limit: 10 }, 'afterbeforeraw') } });
5574
5577
  if (sortBy !== undefined) {
5575
5578
  out.options.sortBy = sortBy;
@@ -5577,6 +5580,7 @@ function convertQueryParams$1(_a) {
5577
5580
  if (tags) {
5578
5581
  out.includeTags = tags;
5579
5582
  }
5583
+ console.log('out', out);
5580
5584
  return out;
5581
5585
  }
5582
5586
 
@@ -7312,6 +7316,18 @@ function isEqual(x, y) {
7312
7316
  if (Array.isArray(x) && x.length !== y.length) {
7313
7317
  return false;
7314
7318
  }
7319
+ // check each element of the array for equality
7320
+ if (Array.isArray(x) && Array.isArray(y)) {
7321
+ if (x.length !== y.length)
7322
+ return false;
7323
+ // eslint-disable-next-line no-plusplus
7324
+ for (let i = 0; i < x.length; i++) {
7325
+ if (!isEqual(x[i], y[i]))
7326
+ return false;
7327
+ }
7328
+ // if all elements are equal, the arrays are equal
7329
+ return true;
7330
+ }
7315
7331
  // if they are dates, they must had equal valueOf
7316
7332
  if (x instanceof Date) {
7317
7333
  return false;
@@ -8885,7 +8901,7 @@ queryChannelMembers.locally = (query) => {
8885
8901
  * Exported for testing
8886
8902
  * @hidden
8887
8903
  */
8888
- const applyFilter$3 = (data, params) => {
8904
+ const applyFilter$2 = (data, params) => {
8889
8905
  let channelMembers = filterByPropIntersection(data, 'roles', params.roles);
8890
8906
  if (params.memberships) {
8891
8907
  /*
@@ -8960,7 +8976,7 @@ const getMembers$1 = (params, callback, config) => {
8960
8976
  /*
8961
8977
  * Only apply filter to RTE Model
8962
8978
  */
8963
- data: isEventModel ? applyFilter$3(channelMembers, params) : channelMembers,
8979
+ data: isEventModel ? applyFilter$2(channelMembers, params) : channelMembers,
8964
8980
  hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
8965
8981
  loading: data.loading,
8966
8982
  error: data.error,
@@ -9050,7 +9066,7 @@ var index$j = /*#__PURE__*/Object.freeze({
9050
9066
  __proto__: null,
9051
9067
  addMembers: addMembers$1,
9052
9068
  removeMembers: removeMembers$1,
9053
- applyFilter: applyFilter$3,
9069
+ applyFilter: applyFilter$2,
9054
9070
  getMembers: getMembers$1,
9055
9071
  searchMembers: searchMembers
9056
9072
  });
@@ -11351,7 +11367,7 @@ queryUsers.locally = (query = {}) => {
11351
11367
  * Exported for testing
11352
11368
  * @hidden
11353
11369
  */
11354
- const applyFilter$2 = (data, params) => {
11370
+ const applyFilter$1 = (data, params) => {
11355
11371
  let users = filterByStringComparePartially(data, 'displayName', params.displayName);
11356
11372
  switch (params.sortBy) {
11357
11373
  case 'firstCreated':
@@ -11412,7 +11428,7 @@ const getUsers = (params, callback, config) => {
11412
11428
  /*
11413
11429
  * Only apply filter to RTE Model
11414
11430
  */
11415
- data: isEventModel ? applyFilter$2(users, params) : users,
11431
+ data: isEventModel ? applyFilter$1(users, params) : users,
11416
11432
  hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
11417
11433
  loading: data.loading,
11418
11434
  error: data.error,
@@ -14228,103 +14244,341 @@ const getMessage = (messageId, callback) => {
14228
14244
  };
14229
14245
  /* end_public_function */
14230
14246
 
14247
+ /* eslint-disable no-use-before-define */
14248
+ class PaginationController {
14249
+ constructor(queryParams, cacheKey) {
14250
+ const { log, cache, userId, http } = getActiveClient();
14251
+ this.queryParams = queryParams;
14252
+ this.cacheKey = cacheKey;
14253
+ this.http = http;
14254
+ }
14255
+ loadFirstPage() {
14256
+ return this.onFetch('first');
14257
+ }
14258
+ loadNextPage() {
14259
+ return this.onFetch('next');
14260
+ }
14261
+ loadPreviousPage() {
14262
+ return this.onFetch('prev');
14263
+ }
14264
+ async onFetch(direction = 'first') {
14265
+ var _a, _b, _c, _d;
14266
+ if (direction === 'prev' && !this.previousToken)
14267
+ return;
14268
+ if (direction === 'next' && !this.nextToken)
14269
+ return;
14270
+ let token;
14271
+ if (direction === 'prev')
14272
+ token = this.previousToken;
14273
+ if (direction === 'next')
14274
+ token = this.nextToken;
14275
+ const queryResponse = await this.getRequest(this.queryParams, token);
14276
+ if (direction === 'first') {
14277
+ this.nextToken = (_a = queryResponse.paging) === null || _a === void 0 ? void 0 : _a.next;
14278
+ this.previousToken = (_b = queryResponse.paging) === null || _b === void 0 ? void 0 : _b.previous;
14279
+ }
14280
+ if (direction === 'prev')
14281
+ this.previousToken = (_c = queryResponse.paging) === null || _c === void 0 ? void 0 : _c.previous;
14282
+ if (direction === 'next')
14283
+ this.nextToken = (_d = queryResponse.paging) === null || _d === void 0 ? void 0 : _d.next;
14284
+ return queryResponse;
14285
+ }
14286
+ }
14287
+
14288
+ /* eslint-disable no-use-before-define */
14231
14289
  /**
14232
- * ```js
14233
- * import { queryMessages } from '@amityco/ts-sdk'
14234
- * const messages = await queryMessages({ channelId })
14235
- * ```
14236
- *
14237
- * Queries a paginable list of {@link Amity.Message} objects
14238
- *
14239
- * @param query The query parameters
14240
- * @returns A page of {@link Amity.Message} objects
14241
- *
14242
- * @category Message API
14243
- * @async
14290
+ * TODO: handle cache receive cache option, and cache policy
14291
+ * TODO: check if querybyIds is supported
14244
14292
  */
14245
- const queryMessages = async (query) => {
14246
- const client = getActiveClient();
14247
- client.log('message/queryMessages', query);
14248
- const _a = convertQueryParams$1(query), { pageToken } = _a, params = __rest(_a, ["pageToken"]);
14249
- // API-FIX: parameters should be querystring. (1)
14250
- // API-FIX: backend should answer Amity.Response (2)
14251
- // API-FIX: pagination should not be indexed on channelSegment (3)
14252
- // const { data } = await client.http.get<Amity.Response<Amity.Paged<Amity.MessagePayload>>>(
14253
- const { data: queryPayload } = await client.http.get(`/api/v5/messages`, {
14254
- params: Object.assign(Object.assign({}, params), (pageToken
14255
- ? {
14256
- options: { token: pageToken },
14257
- }
14258
- : {})),
14259
- });
14260
- // API-FIX: backend should answer Amity.Response (2)
14261
- // const { paging, messages } = unwrapPayload(data)
14262
- const { paging } = queryPayload, payload = __rest(queryPayload, ["paging"]);
14263
- const data = await prepareMessagePayload(payload);
14264
- const { messages } = data;
14265
- const cachedAt = client.cache && Date.now();
14266
- if (client.cache) {
14267
- /*
14268
- * queryMessages.locally is unsupported for messages as message list updates
14269
- * frequently and leads to bugs when user switches between channels, i.e. when
14270
- * the public function (live messages) calls unmount. The list fetched on
14271
- * after from the cache is stale.
14272
- */
14273
- ingestInCache(data, { cachedAt });
14274
- const cacheKey = ['message', 'query', Object.assign({}, params)];
14275
- pushToCache(cacheKey, { messages: messages.map(getResolver('message')), paging });
14293
+ class MessagePaginationController extends PaginationController {
14294
+ // eslint-disable-next-line no-useless-constructor
14295
+ constructor(queryParams, cacheKey) {
14296
+ super(queryParams, cacheKey);
14297
+ }
14298
+ async getRequest(queryParams, token) {
14299
+ const { limit, aroundMessageId } = queryParams, params = __rest(queryParams, ["limit", "aroundMessageId"]);
14300
+ const processedQueryParams = convertQueryParams$1(params);
14301
+ const { data: queryResponse } = await this.http.get(`/api/v5/messages`, {
14302
+ params: Object.assign(Object.assign({}, processedQueryParams), { options: token
14303
+ ? {
14304
+ token,
14305
+ }
14306
+ : {
14307
+ limit,
14308
+ around: aroundMessageId,
14309
+ } }),
14310
+ });
14311
+ return queryResponse;
14276
14312
  }
14277
- fireEvent('local.message.fetched', { messages });
14278
- const nextPage = toPageRaw(paging.next);
14279
- const prevPage = toPageRaw(paging.previous);
14280
- return { data: messages, cachedAt, prevPage, nextPage, paging };
14281
- };
14282
- /* end_public_function */
14313
+ }
14283
14314
 
14284
- /*
14285
- * Exported for testing
14286
- * @hidden
14287
- */
14288
- const applyFilter$1 = (data, params) => {
14289
- let messages = data;
14290
- messages = messages.filter(m => {
14291
- if (params.tags) {
14292
- return params.tags.find(value => {
14293
- if (!m.tags)
14294
- return false;
14295
- return m.tags.includes(value);
14296
- });
14315
+ /* eslint-disable no-use-before-define */
14316
+ class QueryStreamController {
14317
+ constructor(query, cacheKey) {
14318
+ this.query = query;
14319
+ this.cacheKey = cacheKey;
14320
+ }
14321
+ }
14322
+
14323
+ /* eslint-disable no-use-before-define */
14324
+ class MessageQueryStreamController extends QueryStreamController {
14325
+ // private fetchMessageMarker: (response: Amity.MessagePayload) => Promise<void>;
14326
+ constructor(query, cacheKey, notifyChange) {
14327
+ super(query, cacheKey);
14328
+ this.notifyChange = notifyChange;
14329
+ }
14330
+ // eslint-disable-next-line class-methods-use-this
14331
+ async saveToMainDB(response) {
14332
+ const processedPayload = await prepareMessagePayload(response);
14333
+ const client = getActiveClient();
14334
+ const cachedAt = client.cache && Date.now();
14335
+ if (client.cache) {
14336
+ ingestInCache(processedPayload, { cachedAt });
14297
14337
  }
14298
- return true;
14299
- });
14300
- messages = messages.filter(m => {
14301
- if (params.excludeTags) {
14302
- return (params.excludeTags || []).find(value => {
14303
- if (!m.tags)
14304
- return true;
14305
- return !m.tags.includes(value);
14338
+ }
14339
+ appendToQueryStream(response, direction, refresh = false) {
14340
+ var _a, _b, _c, _d, _e, _f;
14341
+ if (refresh) {
14342
+ pushToCache(this.cacheKey, {
14343
+ data: response.messages.map(getResolver('message')),
14344
+ params: {
14345
+ page: {
14346
+ after: toPage((_a = response.paging) === null || _a === void 0 ? void 0 : _a.next),
14347
+ before: toPage((_b = response.paging) === null || _b === void 0 ? void 0 : _b.previous),
14348
+ },
14349
+ },
14306
14350
  });
14307
14351
  }
14352
+ else {
14353
+ const collection = (_c = pullFromCache(this.cacheKey)) === null || _c === void 0 ? void 0 : _c.data;
14354
+ const messages = (_d = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _d !== void 0 ? _d : [];
14355
+ pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: direction === 'next'
14356
+ ? [...new Set([...messages, ...response.messages.map(getResolver('message'))])]
14357
+ : [...new Set([...response.messages.map(getResolver('message')), ...messages])], params: {
14358
+ page: {
14359
+ before: toPage((_e = response.paging) === null || _e === void 0 ? void 0 : _e.previous),
14360
+ after: toPage((_f = response.paging) === null || _f === void 0 ? void 0 : _f.next),
14361
+ },
14362
+ } }));
14363
+ }
14364
+ }
14365
+ reactor(action) {
14366
+ return (payload) => {
14367
+ var _a, _b, _c, _d, _e;
14368
+ if (action === 'onCreate') {
14369
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
14370
+ if (!collection)
14371
+ return;
14372
+ if (this.query.subChannelId !== (payload === null || payload === void 0 ? void 0 : payload.subChannelId) || !collection)
14373
+ return;
14374
+ if (this.query.dataType && this.query.dataType !== payload.dataType)
14375
+ return;
14376
+ if (this.query.excludeTags &&
14377
+ ((_b = this.query.excludeTags) === null || _b === void 0 ? void 0 : _b.some(value => { var _a; return (_a = payload.tags) === null || _a === void 0 ? void 0 : _a.includes(value); })))
14378
+ return;
14379
+ if (!!this.query.hasFlags !== !!payload.flagCount)
14380
+ return;
14381
+ if (this.query.parentId && this.query.parentId !== payload.parentId)
14382
+ return;
14383
+ if (this.query.hasOwnProperty('includeDeleted') &&
14384
+ !this.query.includeDeleted &&
14385
+ payload.isDeleted)
14386
+ return;
14387
+ if (this.query.tags && !((_c = this.query.tags) === null || _c === void 0 ? void 0 : _c.some(value => { var _a; return (_a = payload.tags) === null || _a === void 0 ? void 0 : _a.includes(value); })))
14388
+ return;
14389
+ if ((!this.query.sortBy || this.query.sortBy === 'segmentDesc') &&
14390
+ !((_d = collection.params.page) === null || _d === void 0 ? void 0 : _d.before)) {
14391
+ collection.data = [...new Set([payload.messageId, ...collection.data])];
14392
+ }
14393
+ if (this.query.sortBy === 'segmentAsc' && !((_e = collection.params.page) === null || _e === void 0 ? void 0 : _e.after)) {
14394
+ collection.data = [...new Set([...collection.data, payload.messageId])];
14395
+ }
14396
+ pushToCache(this.cacheKey, collection);
14397
+ }
14398
+ this.notifyChange(false);
14399
+ };
14400
+ }
14401
+ subscribeRTE(createSubscriber) {
14402
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
14403
+ }
14404
+ }
14405
+
14406
+ class LiveCollectionController {
14407
+ constructor(paginationController, queryStreamId, cacheKey, callback) {
14408
+ this.paginationController = paginationController;
14409
+ this.queryStreamId = queryStreamId;
14410
+ this.cacheKey = cacheKey;
14411
+ this.callback = callback;
14412
+ }
14413
+ async refresh() {
14414
+ try {
14415
+ const result = await this.paginationController.loadFirstPage();
14416
+ if (!result)
14417
+ return;
14418
+ await this.persistModel(result);
14419
+ this.persistQueryStream(result, 'next', true);
14420
+ this.notifyChange(false);
14421
+ }
14422
+ catch (e) {
14423
+ this.notifyChange(false, e);
14424
+ }
14425
+ }
14426
+ loadPage(initial = false) {
14427
+ this.setup();
14428
+ this.notifyChange(true);
14429
+ if (initial) {
14430
+ this.refresh();
14431
+ }
14432
+ else {
14433
+ this.loadNextPage();
14434
+ }
14435
+ }
14436
+ async loadNextPage() {
14437
+ try {
14438
+ const result = await this.paginationController.loadNextPage();
14439
+ if (!result)
14440
+ return;
14441
+ await this.persistModel(result);
14442
+ this.persistQueryStream(result, 'next');
14443
+ this.notifyChange(false);
14444
+ }
14445
+ catch (e) {
14446
+ this.notifyChange(false, e);
14447
+ }
14448
+ }
14449
+ async loadPrevPage() {
14450
+ try {
14451
+ const result = await this.paginationController.loadPreviousPage();
14452
+ if (!result)
14453
+ return;
14454
+ await this.persistModel(result);
14455
+ this.persistQueryStream(result, 'prev');
14456
+ this.notifyChange(false);
14457
+ }
14458
+ catch (e) {
14459
+ this.notifyChange(false, e);
14460
+ }
14461
+ }
14462
+ shouldNotify(loading, data) {
14463
+ var _a;
14464
+ if (((_a = this.snapshot) === null || _a === void 0 ? void 0 : _a.loading) === loading && isEqual(this.snapshot.data, data))
14465
+ return false;
14466
+ this.snapshot = { loading, data };
14308
14467
  return true;
14309
- });
14310
- /*
14311
- * for cases when message is deleted via RTE, this flag is used to get
14312
- * items from cache that are !deleted
14313
- */
14314
- if (!params.includeDeleted) {
14315
- messages = filterByPropEquality(messages, 'isDeleted', false);
14316
14468
  }
14317
- messages = messages.sort((message1, message2) => {
14318
- if (params.sortBy === 'segmentAsc') {
14319
- return message1.channelSegment - message2.channelSegment;
14469
+ getCacheKey() {
14470
+ return this.cacheKey;
14471
+ }
14472
+ }
14473
+
14474
+ /* eslint-disable no-use-before-define */
14475
+ class MessageLiveCollectionController extends LiveCollectionController {
14476
+ constructor(query, callback) {
14477
+ const queryStreamId = hash__default["default"](query);
14478
+ const cacheKey = ['message', 'collection', queryStreamId];
14479
+ const paginationController = new MessagePaginationController(query, cacheKey);
14480
+ super(paginationController, queryStreamId, cacheKey, callback);
14481
+ this.query = query;
14482
+ this.queryStreamController = new MessageQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this));
14483
+ this.callback = callback.bind(this);
14484
+ this.loadPage(true);
14485
+ }
14486
+ setup() {
14487
+ var _a;
14488
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
14489
+ if (!collection) {
14490
+ pushToCache(this.cacheKey, {
14491
+ data: [],
14492
+ params: {},
14493
+ });
14320
14494
  }
14321
- if (params.sortBy === 'segmentDesc') {
14322
- return message2.channelSegment - message1.channelSegment;
14495
+ }
14496
+ async persistModel(queryPayload) {
14497
+ await this.queryStreamController.saveToMainDB(queryPayload);
14498
+ }
14499
+ persistQueryStream(queryPayload, direction, refresh) {
14500
+ this.queryStreamController.appendToQueryStream(queryPayload, direction, refresh);
14501
+ }
14502
+ startSubscription() {
14503
+ return this.queryStreamController.subscribeRTE([
14504
+ { fn: onMessageCreated, action: 'onCreate' },
14505
+ { fn: onMessageDeleted, action: 'onDelete' },
14506
+ { fn: onMessageUpdated, action: 'onUpdate' },
14507
+ { fn: onMessageFlagged, action: 'onFlagged' },
14508
+ { fn: onMessageUnflagged, action: 'onUnflagged' },
14509
+ { fn: onMessageFlagCleared, action: 'onFlagCleared' },
14510
+ { fn: onMessageReactionAdded, action: 'onReactionAdded' },
14511
+ { fn: onMessageReactionRemoved, action: 'onReactionRemoved' },
14512
+ {
14513
+ fn: convertEventPayload(onMessageMarkerFetched, 'contentId', 'message'),
14514
+ action: 'onUpdate',
14515
+ },
14516
+ { fn: convertEventPayload(onMessageMarked, 'contentId', 'message'), action: 'onUpdate' },
14517
+ ]);
14518
+ }
14519
+ notifyChange(loading, error) {
14520
+ var _a, _b, _c, _d, _e, _f;
14521
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
14522
+ if (!collection)
14523
+ return;
14524
+ const data = this.applyFilter((_b = collection.data
14525
+ .map(messageId => pullFromCache(['message', 'get', messageId]))
14526
+ .filter(Boolean)
14527
+ .map(({ data }) => data)) !== null && _b !== void 0 ? _b : []);
14528
+ if (!loading && !error && !this.shouldNotify(loading, data))
14529
+ return;
14530
+ this.callback({
14531
+ onNextPage: () => this.loadNextPage(),
14532
+ onPrevPage: () => this.loadPrevPage(),
14533
+ data,
14534
+ hasNextPage: !!((_d = (_c = collection.params) === null || _c === void 0 ? void 0 : _c.page) === null || _d === void 0 ? void 0 : _d.after),
14535
+ hasPrevPage: !!((_f = (_e = collection.params) === null || _e === void 0 ? void 0 : _e.page) === null || _f === void 0 ? void 0 : _f.before),
14536
+ loading,
14537
+ error,
14538
+ });
14539
+ }
14540
+ applyFilter(data) {
14541
+ let messages = data;
14542
+ messages = messages.filter(m => {
14543
+ if (this.query.tags) {
14544
+ return this.query.tags.find(value => {
14545
+ if (!m.tags)
14546
+ return false;
14547
+ return m.tags.includes(value);
14548
+ });
14549
+ }
14550
+ return true;
14551
+ });
14552
+ messages = messages.filter(m => {
14553
+ if (this.query.excludeTags) {
14554
+ return (this.query.excludeTags || []).find(value => {
14555
+ if (!m.tags)
14556
+ return true;
14557
+ return !m.tags.includes(value);
14558
+ });
14559
+ }
14560
+ return true;
14561
+ });
14562
+ /*
14563
+ * for cases when message is deleted via RTE, this flag is used to get
14564
+ * items from cache that are !deleted
14565
+ */
14566
+ if (!this.query.includeDeleted) {
14567
+ messages = filterByPropEquality(messages, 'isDeleted', false);
14323
14568
  }
14324
- return 0;
14325
- });
14326
- return messages;
14327
- };
14569
+ messages = messages.sort((message1, message2) => {
14570
+ if (this.query.sortBy === 'segmentAsc') {
14571
+ return message1.channelSegment - message2.channelSegment;
14572
+ }
14573
+ if (this.query.sortBy === 'segmentDesc') {
14574
+ return message2.channelSegment - message1.channelSegment;
14575
+ }
14576
+ return 0;
14577
+ });
14578
+ return messages;
14579
+ }
14580
+ }
14581
+
14328
14582
  /* begin_public_function
14329
14583
  id: message.query
14330
14584
  */
@@ -14354,76 +14608,12 @@ const getMessages = (params, callback, config) => {
14354
14608
  }
14355
14609
  const timestamp = Date.now();
14356
14610
  log(`getMessages(tmpid: ${timestamp}) > listen`);
14357
- const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
14358
- const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
14359
- const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
14360
- const disposers = [];
14361
- const uniqueId = uuid();
14362
- const cacheKey = ['message', 'collection', params, uniqueId];
14363
- const responder = (data) => {
14364
- var _a, _b;
14365
- const messages = applyFilter$1((_a = data.data
14366
- .map(messageId => pullFromCache(['message', 'get', messageId]))
14367
- .filter(Boolean)
14368
- .map(({ data }) => data)) !== null && _a !== void 0 ? _a : [], params);
14369
- callback({
14370
- onNextPage: onFetch,
14371
- data: messages,
14372
- hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
14373
- loading: data.loading,
14374
- error: data.error,
14375
- });
14376
- };
14377
- const realtimeRouter = (action) => (message) => {
14378
- var _a, _b, _c;
14379
- const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
14380
- if (params.subChannelId !== (message === null || message === void 0 ? void 0 : message.subChannelId) || !collection)
14381
- return;
14382
- // /*
14383
- // * check if RTE message event payload is with the params in order to update in the colleciton cache
14384
- // */
14385
- if (params.dataType && params.dataType !== message.dataType)
14386
- return;
14387
- if (params.excludeTags && ((_b = params.excludeTags) === null || _b === void 0 ? void 0 : _b.some(value => { var _a; return (_a = message.tags) === null || _a === void 0 ? void 0 : _a.includes(value); })))
14388
- return;
14389
- if (!!params.hasFlags !== !!message.flagCount)
14390
- return;
14391
- if (params.parentId && params.parentId !== message.parentId)
14392
- return;
14393
- if (params.hasOwnProperty('includeDeleted') && !params.includeDeleted && message.isDeleted)
14394
- return;
14395
- if (params.tags && !((_c = params.tags) === null || _c === void 0 ? void 0 : _c.some(value => { var _a; return (_a = message.tags) === null || _a === void 0 ? void 0 : _a.includes(value); })))
14396
- return;
14397
- if (!collection.data.includes(message.messageId) && action === 'onCreate') {
14398
- collection.data = [...new Set([message.messageId, ...collection.data])];
14399
- }
14400
- pushToCache(cacheKey, collection);
14401
- responder(collection);
14402
- };
14403
- const onFetch = (initial = false) => {
14404
- var _a, _b, _c, _d, _e, _f, _g;
14405
- const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
14406
- const messages = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
14407
- if (!initial && messages.length > 0 && !((_d = (_c = collection === null || collection === void 0 ? void 0 : collection.params) === null || _c === void 0 ? void 0 : _c.paging) === null || _d === void 0 ? void 0 : _d.next))
14408
- return;
14409
- const query = createQuery(queryMessages, Object.assign(Object.assign({}, queryParams), { page: (_e = (!initial ? collection === null || collection === void 0 ? void 0 : collection.params.page : undefined)) !== null && _e !== void 0 ? _e : { limit }, pageToken: (_g = (_f = collection === null || collection === void 0 ? void 0 : collection.params) === null || _f === void 0 ? void 0 : _f.paging) === null || _g === void 0 ? void 0 : _g.next }));
14410
- runQuery(query, ({ data: result, error, loading, nextPage: page, paging }) => {
14411
- const data = {
14412
- loading,
14413
- error,
14414
- params: { page, paging },
14415
- data: messages,
14416
- };
14417
- if (result) {
14418
- data.data = [...new Set([...messages, ...result.map(getResolver('message'))])];
14419
- }
14420
- pushToCache(cacheKey, data);
14421
- responder(data);
14422
- }, queryOptions(policy, CACHE_SHORTEN_LIFESPAN));
14423
- };
14424
- disposers.push(onMessageFetched(realtimeRouter('onFetch')), onMessageCreated(realtimeRouter('onCreate')), onMessageUpdated(realtimeRouter('onUpdate')), onMessageDeleted(realtimeRouter('onDelete')), onMessageFlagged(realtimeRouter('onFlagged')), onMessageUnflagged(realtimeRouter('onUnflagged')), onMessageFlagCleared(realtimeRouter('onFlagCleared')), onMessageReactionAdded(realtimeRouter('onReactionAdded')), onMessageReactionRemoved(realtimeRouter('onReactionRemoved')), convertEventPayload(onMessageMarkerFetched, 'contentId', 'message')(realtimeRouter('onUpdate')), convertEventPayload(onMessageMarked, 'contentId', 'message')(realtimeRouter('onUpdate')));
14425
- onFetch(true);
14426
- disposers.push(() => dropFromCache(cacheKey));
14611
+ const messagesLiveCollection = new MessageLiveCollectionController(params, callback);
14612
+ const disposers = messagesLiveCollection.startSubscription();
14613
+ const cacheKey = messagesLiveCollection.getCacheKey();
14614
+ disposers.push(() => {
14615
+ dropFromCache(cacheKey);
14616
+ });
14427
14617
  return () => {
14428
14618
  log(`getMessages(tmpid: ${timestamp}) > dispose`);
14429
14619
  disposers.forEach(fn => fn());
@@ -16663,10 +16853,10 @@ queryCommunityMembers.locally = (query) => {
16663
16853
  */
16664
16854
  const applyFilter = (data, params) => {
16665
16855
  let communityMembers = filterByPropIntersection(data, 'roles', params.roles);
16666
- if (params.membership) {
16856
+ if (params.memberships) {
16667
16857
  communityMembers = communityMembers.filter(({ communityMembership }) =>
16668
16858
  // @ts-ignore
16669
- params.membership.includes(communityMembership));
16859
+ params.memberships.includes(communityMembership));
16670
16860
  }
16671
16861
  if (params.search) {
16672
16862
  communityMembers = filterBySearchTerm(communityMembers, params.search);