@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.esm.js CHANGED
@@ -6,6 +6,7 @@ import axios from 'axios';
6
6
  import HttpAgent, { HttpsAgent } from 'agentkeepalive';
7
7
  import io from 'socket.io-client';
8
8
  import uuid$1 from 'react-native-uuid';
9
+ import hash from 'object-hash';
9
10
  import Hls from 'hls.js';
10
11
 
11
12
  const CommunityPostSettings = Object.freeze({
@@ -21660,6 +21661,7 @@ function convertParams(_a) {
21660
21661
  }
21661
21662
  function convertQueryParams$1(_a) {
21662
21663
  var { page, sortBy, subChannelId, tags, includeDeleted } = _a, rest = __rest(_a, ["page", "sortBy", "subChannelId", "tags", "includeDeleted"]);
21664
+ console.log('convertQueryParams', page);
21663
21665
  const out = Object.assign(Object.assign({}, rest), { messageFeedId: subChannelId, isDeleted: inferIsDeleted(includeDeleted), options: { token: toToken(page || { limit: 10 }, 'afterbeforeraw') } });
21664
21666
  if (sortBy !== undefined) {
21665
21667
  out.options.sortBy = sortBy;
@@ -21667,6 +21669,7 @@ function convertQueryParams$1(_a) {
21667
21669
  if (tags) {
21668
21670
  out.includeTags = tags;
21669
21671
  }
21672
+ console.log('out', out);
21670
21673
  return out;
21671
21674
  }
21672
21675
 
@@ -23402,6 +23405,18 @@ function isEqual(x, y) {
23402
23405
  if (Array.isArray(x) && x.length !== y.length) {
23403
23406
  return false;
23404
23407
  }
23408
+ // check each element of the array for equality
23409
+ if (Array.isArray(x) && Array.isArray(y)) {
23410
+ if (x.length !== y.length)
23411
+ return false;
23412
+ // eslint-disable-next-line no-plusplus
23413
+ for (let i = 0; i < x.length; i++) {
23414
+ if (!isEqual(x[i], y[i]))
23415
+ return false;
23416
+ }
23417
+ // if all elements are equal, the arrays are equal
23418
+ return true;
23419
+ }
23405
23420
  // if they are dates, they must had equal valueOf
23406
23421
  if (x instanceof Date) {
23407
23422
  return false;
@@ -24975,7 +24990,7 @@ queryChannelMembers.locally = (query) => {
24975
24990
  * Exported for testing
24976
24991
  * @hidden
24977
24992
  */
24978
- const applyFilter$3 = (data, params) => {
24993
+ const applyFilter$2 = (data, params) => {
24979
24994
  let channelMembers = filterByPropIntersection(data, 'roles', params.roles);
24980
24995
  if (params.memberships) {
24981
24996
  /*
@@ -25050,7 +25065,7 @@ const getMembers$1 = (params, callback, config) => {
25050
25065
  /*
25051
25066
  * Only apply filter to RTE Model
25052
25067
  */
25053
- data: isEventModel ? applyFilter$3(channelMembers, params) : channelMembers,
25068
+ data: isEventModel ? applyFilter$2(channelMembers, params) : channelMembers,
25054
25069
  hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
25055
25070
  loading: data.loading,
25056
25071
  error: data.error,
@@ -25140,7 +25155,7 @@ var index$j = /*#__PURE__*/Object.freeze({
25140
25155
  __proto__: null,
25141
25156
  addMembers: addMembers$1,
25142
25157
  removeMembers: removeMembers$1,
25143
- applyFilter: applyFilter$3,
25158
+ applyFilter: applyFilter$2,
25144
25159
  getMembers: getMembers$1,
25145
25160
  searchMembers: searchMembers
25146
25161
  });
@@ -27441,7 +27456,7 @@ queryUsers.locally = (query = {}) => {
27441
27456
  * Exported for testing
27442
27457
  * @hidden
27443
27458
  */
27444
- const applyFilter$2 = (data, params) => {
27459
+ const applyFilter$1 = (data, params) => {
27445
27460
  let users = filterByStringComparePartially(data, 'displayName', params.displayName);
27446
27461
  switch (params.sortBy) {
27447
27462
  case 'firstCreated':
@@ -27502,7 +27517,7 @@ const getUsers = (params, callback, config) => {
27502
27517
  /*
27503
27518
  * Only apply filter to RTE Model
27504
27519
  */
27505
- data: isEventModel ? applyFilter$2(users, params) : users,
27520
+ data: isEventModel ? applyFilter$1(users, params) : users,
27506
27521
  hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
27507
27522
  loading: data.loading,
27508
27523
  error: data.error,
@@ -30318,103 +30333,341 @@ const getMessage = (messageId, callback) => {
30318
30333
  };
30319
30334
  /* end_public_function */
30320
30335
 
30336
+ /* eslint-disable no-use-before-define */
30337
+ class PaginationController {
30338
+ constructor(queryParams, cacheKey) {
30339
+ const { log, cache, userId, http } = getActiveClient();
30340
+ this.queryParams = queryParams;
30341
+ this.cacheKey = cacheKey;
30342
+ this.http = http;
30343
+ }
30344
+ loadFirstPage() {
30345
+ return this.onFetch('first');
30346
+ }
30347
+ loadNextPage() {
30348
+ return this.onFetch('next');
30349
+ }
30350
+ loadPreviousPage() {
30351
+ return this.onFetch('prev');
30352
+ }
30353
+ async onFetch(direction = 'first') {
30354
+ var _a, _b, _c, _d;
30355
+ if (direction === 'prev' && !this.previousToken)
30356
+ return;
30357
+ if (direction === 'next' && !this.nextToken)
30358
+ return;
30359
+ let token;
30360
+ if (direction === 'prev')
30361
+ token = this.previousToken;
30362
+ if (direction === 'next')
30363
+ token = this.nextToken;
30364
+ const queryResponse = await this.getRequest(this.queryParams, token);
30365
+ if (direction === 'first') {
30366
+ this.nextToken = (_a = queryResponse.paging) === null || _a === void 0 ? void 0 : _a.next;
30367
+ this.previousToken = (_b = queryResponse.paging) === null || _b === void 0 ? void 0 : _b.previous;
30368
+ }
30369
+ if (direction === 'prev')
30370
+ this.previousToken = (_c = queryResponse.paging) === null || _c === void 0 ? void 0 : _c.previous;
30371
+ if (direction === 'next')
30372
+ this.nextToken = (_d = queryResponse.paging) === null || _d === void 0 ? void 0 : _d.next;
30373
+ return queryResponse;
30374
+ }
30375
+ }
30376
+
30377
+ /* eslint-disable no-use-before-define */
30321
30378
  /**
30322
- * ```js
30323
- * import { queryMessages } from '@amityco/ts-sdk'
30324
- * const messages = await queryMessages({ channelId })
30325
- * ```
30326
- *
30327
- * Queries a paginable list of {@link Amity.Message} objects
30328
- *
30329
- * @param query The query parameters
30330
- * @returns A page of {@link Amity.Message} objects
30331
- *
30332
- * @category Message API
30333
- * @async
30379
+ * TODO: handle cache receive cache option, and cache policy
30380
+ * TODO: check if querybyIds is supported
30334
30381
  */
30335
- const queryMessages = async (query) => {
30336
- const client = getActiveClient();
30337
- client.log('message/queryMessages', query);
30338
- const _a = convertQueryParams$1(query), { pageToken } = _a, params = __rest(_a, ["pageToken"]);
30339
- // API-FIX: parameters should be querystring. (1)
30340
- // API-FIX: backend should answer Amity.Response (2)
30341
- // API-FIX: pagination should not be indexed on channelSegment (3)
30342
- // const { data } = await client.http.get<Amity.Response<Amity.Paged<Amity.MessagePayload>>>(
30343
- const { data: queryPayload } = await client.http.get(`/api/v5/messages`, {
30344
- params: Object.assign(Object.assign({}, params), (pageToken
30345
- ? {
30346
- options: { token: pageToken },
30347
- }
30348
- : {})),
30349
- });
30350
- // API-FIX: backend should answer Amity.Response (2)
30351
- // const { paging, messages } = unwrapPayload(data)
30352
- const { paging } = queryPayload, payload = __rest(queryPayload, ["paging"]);
30353
- const data = await prepareMessagePayload(payload);
30354
- const { messages } = data;
30355
- const cachedAt = client.cache && Date.now();
30356
- if (client.cache) {
30357
- /*
30358
- * queryMessages.locally is unsupported for messages as message list updates
30359
- * frequently and leads to bugs when user switches between channels, i.e. when
30360
- * the public function (live messages) calls unmount. The list fetched on
30361
- * after from the cache is stale.
30362
- */
30363
- ingestInCache(data, { cachedAt });
30364
- const cacheKey = ['message', 'query', Object.assign({}, params)];
30365
- pushToCache(cacheKey, { messages: messages.map(getResolver('message')), paging });
30382
+ class MessagePaginationController extends PaginationController {
30383
+ // eslint-disable-next-line no-useless-constructor
30384
+ constructor(queryParams, cacheKey) {
30385
+ super(queryParams, cacheKey);
30386
+ }
30387
+ async getRequest(queryParams, token) {
30388
+ const { limit, aroundMessageId } = queryParams, params = __rest(queryParams, ["limit", "aroundMessageId"]);
30389
+ const processedQueryParams = convertQueryParams$1(params);
30390
+ const { data: queryResponse } = await this.http.get(`/api/v5/messages`, {
30391
+ params: Object.assign(Object.assign({}, processedQueryParams), { options: token
30392
+ ? {
30393
+ token,
30394
+ }
30395
+ : {
30396
+ limit,
30397
+ around: aroundMessageId,
30398
+ } }),
30399
+ });
30400
+ return queryResponse;
30366
30401
  }
30367
- fireEvent('local.message.fetched', { messages });
30368
- const nextPage = toPageRaw(paging.next);
30369
- const prevPage = toPageRaw(paging.previous);
30370
- return { data: messages, cachedAt, prevPage, nextPage, paging };
30371
- };
30372
- /* end_public_function */
30402
+ }
30373
30403
 
30374
- /*
30375
- * Exported for testing
30376
- * @hidden
30377
- */
30378
- const applyFilter$1 = (data, params) => {
30379
- let messages = data;
30380
- messages = messages.filter(m => {
30381
- if (params.tags) {
30382
- return params.tags.find(value => {
30383
- if (!m.tags)
30384
- return false;
30385
- return m.tags.includes(value);
30386
- });
30404
+ /* eslint-disable no-use-before-define */
30405
+ class QueryStreamController {
30406
+ constructor(query, cacheKey) {
30407
+ this.query = query;
30408
+ this.cacheKey = cacheKey;
30409
+ }
30410
+ }
30411
+
30412
+ /* eslint-disable no-use-before-define */
30413
+ class MessageQueryStreamController extends QueryStreamController {
30414
+ // private fetchMessageMarker: (response: Amity.MessagePayload) => Promise<void>;
30415
+ constructor(query, cacheKey, notifyChange) {
30416
+ super(query, cacheKey);
30417
+ this.notifyChange = notifyChange;
30418
+ }
30419
+ // eslint-disable-next-line class-methods-use-this
30420
+ async saveToMainDB(response) {
30421
+ const processedPayload = await prepareMessagePayload(response);
30422
+ const client = getActiveClient();
30423
+ const cachedAt = client.cache && Date.now();
30424
+ if (client.cache) {
30425
+ ingestInCache(processedPayload, { cachedAt });
30387
30426
  }
30388
- return true;
30389
- });
30390
- messages = messages.filter(m => {
30391
- if (params.excludeTags) {
30392
- return (params.excludeTags || []).find(value => {
30393
- if (!m.tags)
30394
- return true;
30395
- return !m.tags.includes(value);
30427
+ }
30428
+ appendToQueryStream(response, direction, refresh = false) {
30429
+ var _a, _b, _c, _d, _e, _f;
30430
+ if (refresh) {
30431
+ pushToCache(this.cacheKey, {
30432
+ data: response.messages.map(getResolver('message')),
30433
+ params: {
30434
+ page: {
30435
+ after: toPage((_a = response.paging) === null || _a === void 0 ? void 0 : _a.next),
30436
+ before: toPage((_b = response.paging) === null || _b === void 0 ? void 0 : _b.previous),
30437
+ },
30438
+ },
30396
30439
  });
30397
30440
  }
30441
+ else {
30442
+ const collection = (_c = pullFromCache(this.cacheKey)) === null || _c === void 0 ? void 0 : _c.data;
30443
+ const messages = (_d = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _d !== void 0 ? _d : [];
30444
+ pushToCache(this.cacheKey, Object.assign(Object.assign({}, collection), { data: direction === 'next'
30445
+ ? [...new Set([...messages, ...response.messages.map(getResolver('message'))])]
30446
+ : [...new Set([...response.messages.map(getResolver('message')), ...messages])], params: {
30447
+ page: {
30448
+ before: toPage((_e = response.paging) === null || _e === void 0 ? void 0 : _e.previous),
30449
+ after: toPage((_f = response.paging) === null || _f === void 0 ? void 0 : _f.next),
30450
+ },
30451
+ } }));
30452
+ }
30453
+ }
30454
+ reactor(action) {
30455
+ return (payload) => {
30456
+ var _a, _b, _c, _d, _e;
30457
+ if (action === 'onCreate') {
30458
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30459
+ if (!collection)
30460
+ return;
30461
+ if (this.query.subChannelId !== (payload === null || payload === void 0 ? void 0 : payload.subChannelId) || !collection)
30462
+ return;
30463
+ if (this.query.dataType && this.query.dataType !== payload.dataType)
30464
+ return;
30465
+ if (this.query.excludeTags &&
30466
+ ((_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); })))
30467
+ return;
30468
+ if (!!this.query.hasFlags !== !!payload.flagCount)
30469
+ return;
30470
+ if (this.query.parentId && this.query.parentId !== payload.parentId)
30471
+ return;
30472
+ if (this.query.hasOwnProperty('includeDeleted') &&
30473
+ !this.query.includeDeleted &&
30474
+ payload.isDeleted)
30475
+ return;
30476
+ 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); })))
30477
+ return;
30478
+ if ((!this.query.sortBy || this.query.sortBy === 'segmentDesc') &&
30479
+ !((_d = collection.params.page) === null || _d === void 0 ? void 0 : _d.before)) {
30480
+ collection.data = [...new Set([payload.messageId, ...collection.data])];
30481
+ }
30482
+ if (this.query.sortBy === 'segmentAsc' && !((_e = collection.params.page) === null || _e === void 0 ? void 0 : _e.after)) {
30483
+ collection.data = [...new Set([...collection.data, payload.messageId])];
30484
+ }
30485
+ pushToCache(this.cacheKey, collection);
30486
+ }
30487
+ this.notifyChange(false);
30488
+ };
30489
+ }
30490
+ subscribeRTE(createSubscriber) {
30491
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
30492
+ }
30493
+ }
30494
+
30495
+ class LiveCollectionController {
30496
+ constructor(paginationController, queryStreamId, cacheKey, callback) {
30497
+ this.paginationController = paginationController;
30498
+ this.queryStreamId = queryStreamId;
30499
+ this.cacheKey = cacheKey;
30500
+ this.callback = callback;
30501
+ }
30502
+ async refresh() {
30503
+ try {
30504
+ const result = await this.paginationController.loadFirstPage();
30505
+ if (!result)
30506
+ return;
30507
+ await this.persistModel(result);
30508
+ this.persistQueryStream(result, 'next', true);
30509
+ this.notifyChange(false);
30510
+ }
30511
+ catch (e) {
30512
+ this.notifyChange(false, e);
30513
+ }
30514
+ }
30515
+ loadPage(initial = false) {
30516
+ this.setup();
30517
+ this.notifyChange(true);
30518
+ if (initial) {
30519
+ this.refresh();
30520
+ }
30521
+ else {
30522
+ this.loadNextPage();
30523
+ }
30524
+ }
30525
+ async loadNextPage() {
30526
+ try {
30527
+ const result = await this.paginationController.loadNextPage();
30528
+ if (!result)
30529
+ return;
30530
+ await this.persistModel(result);
30531
+ this.persistQueryStream(result, 'next');
30532
+ this.notifyChange(false);
30533
+ }
30534
+ catch (e) {
30535
+ this.notifyChange(false, e);
30536
+ }
30537
+ }
30538
+ async loadPrevPage() {
30539
+ try {
30540
+ const result = await this.paginationController.loadPreviousPage();
30541
+ if (!result)
30542
+ return;
30543
+ await this.persistModel(result);
30544
+ this.persistQueryStream(result, 'prev');
30545
+ this.notifyChange(false);
30546
+ }
30547
+ catch (e) {
30548
+ this.notifyChange(false, e);
30549
+ }
30550
+ }
30551
+ shouldNotify(loading, data) {
30552
+ var _a;
30553
+ if (((_a = this.snapshot) === null || _a === void 0 ? void 0 : _a.loading) === loading && isEqual(this.snapshot.data, data))
30554
+ return false;
30555
+ this.snapshot = { loading, data };
30398
30556
  return true;
30399
- });
30400
- /*
30401
- * for cases when message is deleted via RTE, this flag is used to get
30402
- * items from cache that are !deleted
30403
- */
30404
- if (!params.includeDeleted) {
30405
- messages = filterByPropEquality(messages, 'isDeleted', false);
30406
30557
  }
30407
- messages = messages.sort((message1, message2) => {
30408
- if (params.sortBy === 'segmentAsc') {
30409
- return message1.channelSegment - message2.channelSegment;
30558
+ getCacheKey() {
30559
+ return this.cacheKey;
30560
+ }
30561
+ }
30562
+
30563
+ /* eslint-disable no-use-before-define */
30564
+ class MessageLiveCollectionController extends LiveCollectionController {
30565
+ constructor(query, callback) {
30566
+ const queryStreamId = hash(query);
30567
+ const cacheKey = ['message', 'collection', queryStreamId];
30568
+ const paginationController = new MessagePaginationController(query, cacheKey);
30569
+ super(paginationController, queryStreamId, cacheKey, callback);
30570
+ this.query = query;
30571
+ this.queryStreamController = new MessageQueryStreamController(this.query, this.cacheKey, this.notifyChange.bind(this));
30572
+ this.callback = callback.bind(this);
30573
+ this.loadPage(true);
30574
+ }
30575
+ setup() {
30576
+ var _a;
30577
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30578
+ if (!collection) {
30579
+ pushToCache(this.cacheKey, {
30580
+ data: [],
30581
+ params: {},
30582
+ });
30410
30583
  }
30411
- if (params.sortBy === 'segmentDesc') {
30412
- return message2.channelSegment - message1.channelSegment;
30584
+ }
30585
+ async persistModel(queryPayload) {
30586
+ await this.queryStreamController.saveToMainDB(queryPayload);
30587
+ }
30588
+ persistQueryStream(queryPayload, direction, refresh) {
30589
+ this.queryStreamController.appendToQueryStream(queryPayload, direction, refresh);
30590
+ }
30591
+ startSubscription() {
30592
+ return this.queryStreamController.subscribeRTE([
30593
+ { fn: onMessageCreated, action: 'onCreate' },
30594
+ { fn: onMessageDeleted, action: 'onDelete' },
30595
+ { fn: onMessageUpdated, action: 'onUpdate' },
30596
+ { fn: onMessageFlagged, action: 'onFlagged' },
30597
+ { fn: onMessageUnflagged, action: 'onUnflagged' },
30598
+ { fn: onMessageFlagCleared, action: 'onFlagCleared' },
30599
+ { fn: onMessageReactionAdded, action: 'onReactionAdded' },
30600
+ { fn: onMessageReactionRemoved, action: 'onReactionRemoved' },
30601
+ {
30602
+ fn: convertEventPayload(onMessageMarkerFetched, 'contentId', 'message'),
30603
+ action: 'onUpdate',
30604
+ },
30605
+ { fn: convertEventPayload(onMessageMarked, 'contentId', 'message'), action: 'onUpdate' },
30606
+ ]);
30607
+ }
30608
+ notifyChange(loading, error) {
30609
+ var _a, _b, _c, _d, _e, _f;
30610
+ const collection = (_a = pullFromCache(this.cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30611
+ if (!collection)
30612
+ return;
30613
+ const data = this.applyFilter((_b = collection.data
30614
+ .map(messageId => pullFromCache(['message', 'get', messageId]))
30615
+ .filter(Boolean)
30616
+ .map(({ data }) => data)) !== null && _b !== void 0 ? _b : []);
30617
+ if (!loading && !error && !this.shouldNotify(loading, data))
30618
+ return;
30619
+ this.callback({
30620
+ onNextPage: () => this.loadNextPage(),
30621
+ onPrevPage: () => this.loadPrevPage(),
30622
+ data,
30623
+ hasNextPage: !!((_d = (_c = collection.params) === null || _c === void 0 ? void 0 : _c.page) === null || _d === void 0 ? void 0 : _d.after),
30624
+ hasPrevPage: !!((_f = (_e = collection.params) === null || _e === void 0 ? void 0 : _e.page) === null || _f === void 0 ? void 0 : _f.before),
30625
+ loading,
30626
+ error,
30627
+ });
30628
+ }
30629
+ applyFilter(data) {
30630
+ let messages = data;
30631
+ messages = messages.filter(m => {
30632
+ if (this.query.tags) {
30633
+ return this.query.tags.find(value => {
30634
+ if (!m.tags)
30635
+ return false;
30636
+ return m.tags.includes(value);
30637
+ });
30638
+ }
30639
+ return true;
30640
+ });
30641
+ messages = messages.filter(m => {
30642
+ if (this.query.excludeTags) {
30643
+ return (this.query.excludeTags || []).find(value => {
30644
+ if (!m.tags)
30645
+ return true;
30646
+ return !m.tags.includes(value);
30647
+ });
30648
+ }
30649
+ return true;
30650
+ });
30651
+ /*
30652
+ * for cases when message is deleted via RTE, this flag is used to get
30653
+ * items from cache that are !deleted
30654
+ */
30655
+ if (!this.query.includeDeleted) {
30656
+ messages = filterByPropEquality(messages, 'isDeleted', false);
30413
30657
  }
30414
- return 0;
30415
- });
30416
- return messages;
30417
- };
30658
+ messages = messages.sort((message1, message2) => {
30659
+ if (this.query.sortBy === 'segmentAsc') {
30660
+ return message1.channelSegment - message2.channelSegment;
30661
+ }
30662
+ if (this.query.sortBy === 'segmentDesc') {
30663
+ return message2.channelSegment - message1.channelSegment;
30664
+ }
30665
+ return 0;
30666
+ });
30667
+ return messages;
30668
+ }
30669
+ }
30670
+
30418
30671
  /* begin_public_function
30419
30672
  id: message.query
30420
30673
  */
@@ -30444,76 +30697,12 @@ const getMessages = (params, callback, config) => {
30444
30697
  }
30445
30698
  const timestamp = Date.now();
30446
30699
  log(`getMessages(tmpid: ${timestamp}) > listen`);
30447
- const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
30448
- const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
30449
- const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
30450
- const disposers = [];
30451
- const uniqueId = uuid();
30452
- const cacheKey = ['message', 'collection', params, uniqueId];
30453
- const responder = (data) => {
30454
- var _a, _b;
30455
- const messages = applyFilter$1((_a = data.data
30456
- .map(messageId => pullFromCache(['message', 'get', messageId]))
30457
- .filter(Boolean)
30458
- .map(({ data }) => data)) !== null && _a !== void 0 ? _a : [], params);
30459
- callback({
30460
- onNextPage: onFetch,
30461
- data: messages,
30462
- hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
30463
- loading: data.loading,
30464
- error: data.error,
30465
- });
30466
- };
30467
- const realtimeRouter = (action) => (message) => {
30468
- var _a, _b, _c;
30469
- const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30470
- if (params.subChannelId !== (message === null || message === void 0 ? void 0 : message.subChannelId) || !collection)
30471
- return;
30472
- // /*
30473
- // * check if RTE message event payload is with the params in order to update in the colleciton cache
30474
- // */
30475
- if (params.dataType && params.dataType !== message.dataType)
30476
- return;
30477
- 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); })))
30478
- return;
30479
- if (!!params.hasFlags !== !!message.flagCount)
30480
- return;
30481
- if (params.parentId && params.parentId !== message.parentId)
30482
- return;
30483
- if (params.hasOwnProperty('includeDeleted') && !params.includeDeleted && message.isDeleted)
30484
- return;
30485
- 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); })))
30486
- return;
30487
- if (!collection.data.includes(message.messageId) && action === 'onCreate') {
30488
- collection.data = [...new Set([message.messageId, ...collection.data])];
30489
- }
30490
- pushToCache(cacheKey, collection);
30491
- responder(collection);
30492
- };
30493
- const onFetch = (initial = false) => {
30494
- var _a, _b, _c, _d, _e, _f, _g;
30495
- const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
30496
- const messages = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
30497
- 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))
30498
- return;
30499
- 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 }));
30500
- runQuery(query, ({ data: result, error, loading, nextPage: page, paging }) => {
30501
- const data = {
30502
- loading,
30503
- error,
30504
- params: { page, paging },
30505
- data: messages,
30506
- };
30507
- if (result) {
30508
- data.data = [...new Set([...messages, ...result.map(getResolver('message'))])];
30509
- }
30510
- pushToCache(cacheKey, data);
30511
- responder(data);
30512
- }, queryOptions(policy, CACHE_SHORTEN_LIFESPAN));
30513
- };
30514
- 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')));
30515
- onFetch(true);
30516
- disposers.push(() => dropFromCache(cacheKey));
30700
+ const messagesLiveCollection = new MessageLiveCollectionController(params, callback);
30701
+ const disposers = messagesLiveCollection.startSubscription();
30702
+ const cacheKey = messagesLiveCollection.getCacheKey();
30703
+ disposers.push(() => {
30704
+ dropFromCache(cacheKey);
30705
+ });
30517
30706
  return () => {
30518
30707
  log(`getMessages(tmpid: ${timestamp}) > dispose`);
30519
30708
  disposers.forEach(fn => fn());
@@ -32753,10 +32942,10 @@ queryCommunityMembers.locally = (query) => {
32753
32942
  */
32754
32943
  const applyFilter = (data, params) => {
32755
32944
  let communityMembers = filterByPropIntersection(data, 'roles', params.roles);
32756
- if (params.membership) {
32945
+ if (params.memberships) {
32757
32946
  communityMembers = communityMembers.filter(({ communityMembership }) =>
32758
32947
  // @ts-ignore
32759
- params.membership.includes(communityMembership));
32948
+ params.memberships.includes(communityMembership));
32760
32949
  }
32761
32950
  if (params.search) {
32762
32951
  communityMembers = filterBySearchTerm(communityMembers, params.search);