@amityco/ts-sdk-react-native 6.23.0 → 6.24.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 (80) hide show
  1. package/dist/@types/core/events.d.ts +5 -0
  2. package/dist/@types/core/events.d.ts.map +1 -1
  3. package/dist/@types/domains/community.d.ts +15 -0
  4. package/dist/@types/domains/community.d.ts.map +1 -1
  5. package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts.map +1 -1
  6. package/dist/commentRepository/api/deleteComment.d.ts.map +1 -1
  7. package/dist/commentRepository/internalApi/createComment.d.ts.map +1 -1
  8. package/dist/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.d.ts +14 -0
  9. package/dist/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.d.ts.map +1 -0
  10. package/dist/communityRepository/observers/getCommunities/CommunitiesPaginationController.d.ts +9 -0
  11. package/dist/communityRepository/observers/getCommunities/CommunitiesPaginationController.d.ts.map +1 -0
  12. package/dist/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.d.ts +15 -0
  13. package/dist/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.d.ts.map +1 -0
  14. package/dist/communityRepository/observers/getCommunities/enums.d.ts +6 -0
  15. package/dist/communityRepository/observers/getCommunities/enums.d.ts.map +1 -0
  16. package/dist/communityRepository/observers/index.d.ts +1 -0
  17. package/dist/communityRepository/observers/index.d.ts.map +1 -1
  18. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.d.ts +14 -0
  19. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.d.ts.map +1 -0
  20. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesPaginationController.d.ts +9 -0
  21. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesPaginationController.d.ts.map +1 -0
  22. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.d.ts +15 -0
  23. package/dist/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.d.ts.map +1 -0
  24. package/dist/communityRepository/observers/searchCommunities/enums.d.ts +6 -0
  25. package/dist/communityRepository/observers/searchCommunities/enums.d.ts.map +1 -0
  26. package/dist/communityRepository/observers/searchCommunities.d.ts +20 -0
  27. package/dist/communityRepository/observers/searchCommunities.d.ts.map +1 -0
  28. package/dist/core/events.d.ts +3 -3
  29. package/dist/core/events.d.ts.map +1 -1
  30. package/dist/core/liveCollection/LiveCollectionController.d.ts +4 -1
  31. package/dist/core/liveCollection/LiveCollectionController.d.ts.map +1 -1
  32. package/dist/index.cjs.js +582 -334
  33. package/dist/index.esm.js +573 -325
  34. package/dist/index.umd.js +4 -4
  35. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts +12 -0
  36. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts.map +1 -0
  37. package/dist/marker/events/onSubChannelUnreadUpdatedLocal.d.ts +12 -0
  38. package/dist/marker/events/onSubChannelUnreadUpdatedLocal.d.ts.map +1 -0
  39. package/dist/marker/events/onUserFeedMarkerUpdated.d.ts.map +1 -1
  40. package/dist/marker/utils/reCalculateChannelUnreadInfo.d.ts +1 -1
  41. package/dist/marker/utils/reCalculateChannelUnreadInfo.d.ts.map +1 -1
  42. package/dist/postRepository/observers/getPost.d.ts.map +1 -1
  43. package/dist/postRepository/observers/getPosts.d.ts.map +1 -1
  44. package/package.json +1 -1
  45. package/src/@types/core/events.ts +8 -0
  46. package/src/@types/domains/community.ts +25 -0
  47. package/src/channelRepository/channelMembership/observers/getMembers/ChannelMemberLiveCollectionController.ts +2 -2
  48. package/src/channelRepository/observers/getChannel.ts +2 -2
  49. package/src/channelRepository/observers/getChannels/ChannelLiveCollectionController.ts +7 -7
  50. package/src/client/utils/ReadReceiptSync/readReceiptSyncEngine.ts +6 -3
  51. package/src/commentRepository/api/createComment.ts +2 -2
  52. package/src/commentRepository/api/deleteComment.ts +2 -4
  53. package/src/commentRepository/events/utils.ts +1 -1
  54. package/src/commentRepository/internalApi/createComment.ts +3 -2
  55. package/src/commentRepository/internalApi/deleteComment.ts +2 -2
  56. package/src/communityRepository/observers/getCommunities/CommunitiesLiveCollectionController.ts +147 -0
  57. package/src/communityRepository/observers/getCommunities/CommunitiesPaginationController.ts +31 -0
  58. package/src/communityRepository/observers/getCommunities/CommunitiesQueryStreamController.ts +86 -0
  59. package/src/communityRepository/observers/getCommunities/enums.ts +5 -0
  60. package/src/communityRepository/observers/index.ts +1 -0
  61. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesLiveCollectionController.ts +129 -0
  62. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesPaginationController.ts +31 -0
  63. package/src/communityRepository/observers/searchCommunities/SearchCommunitiesQueryStreamController.ts +81 -0
  64. package/src/communityRepository/observers/searchCommunities/enums.ts +5 -0
  65. package/src/communityRepository/observers/searchCommunities.ts +56 -0
  66. package/src/core/liveCollection/LiveCollectionController.ts +6 -3
  67. package/src/marker/events/onChannelUnreadUpdatedLocal.ts +29 -0
  68. package/src/marker/events/onSubChannelMarkerFetched.ts +1 -1
  69. package/src/marker/events/onSubChannelUnreadUpdatedLocal.ts +29 -0
  70. package/src/marker/events/onUserFeedMarkerUpdated.ts +3 -4
  71. package/src/marker/utils/reCalculateChannelUnreadInfo.ts +7 -3
  72. package/src/messageRepository/observers/getMessages/MessageLiveCollectionController.ts +3 -3
  73. package/src/postRepository/api/editPost.ts +1 -1
  74. package/src/postRepository/api/updatePost.ts +1 -1
  75. package/src/postRepository/observers/getPost.ts +26 -0
  76. package/src/postRepository/observers/getPosts.ts +31 -0
  77. package/src/storyRepository/observers/getGlobalStoryTargets/GlobalStoryLiveCollectionController.ts +2 -2
  78. package/src/storyRepository/observers/getStoriesByTargetIds/StoryLiveCollectionController.ts +1 -1
  79. package/src/subChannelRepository/observers/getSubChannel.ts +2 -2
  80. package/src/subChannelRepository/observers/getSubChannels/SubChannelLiveCollectionController.ts +4 -4
@@ -0,0 +1,129 @@
1
+ /* eslint-disable no-use-before-define */
2
+ import hash from 'object-hash';
3
+ import { pullFromCache, pushToCache } from '~/cache/api';
4
+ import { CommunitiesPaginationController } from './SearchCommunitiesPaginationController';
5
+ import { CommunitiesQueryStreamController } from './SearchCommunitiesQueryStreamController';
6
+ import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
7
+ import {
8
+ onCommunityCreated,
9
+ onCommunityDeleted,
10
+ onCommunityUpdated,
11
+ } from '~/communityRepository/events';
12
+ import {
13
+ filterByCommunityMembership,
14
+ filterByPropEquality,
15
+ sortByDisplayName,
16
+ sortByFirstCreated,
17
+ sortByLastCreated,
18
+ } from '~/core/query';
19
+ import { prepareCommunityPayload } from '~/communityRepository/utils';
20
+ import { getActiveClient } from '~/client';
21
+ import { EnumCommunityActions } from './enums';
22
+
23
+ export class SearchCommunityLiveCollectionController extends LiveCollectionController<
24
+ 'community',
25
+ Amity.SearchCommunityLiveCollection,
26
+ Amity.Community,
27
+ CommunitiesPaginationController
28
+ > {
29
+ private queryStreamController: CommunitiesQueryStreamController;
30
+
31
+ private query: Amity.SearchCommunityLiveCollection;
32
+
33
+ constructor(
34
+ query: Amity.SearchCommunityLiveCollection,
35
+ callback: Amity.LiveCollectionCallback<Amity.Community>,
36
+ ) {
37
+ const queryStreamId = hash(query);
38
+ const cacheKey = ['community', 'collection', queryStreamId];
39
+ const paginationController = new CommunitiesPaginationController(query);
40
+
41
+ super(paginationController, queryStreamId, cacheKey, callback);
42
+
43
+ this.query = query;
44
+ this.queryStreamController = new CommunitiesQueryStreamController(
45
+ this.query,
46
+ this.cacheKey,
47
+ this.notifyChange.bind(this),
48
+ prepareCommunityPayload,
49
+ );
50
+
51
+ this.callback = callback.bind(this);
52
+ this.loadPage({ initial: true });
53
+ }
54
+
55
+ protected setup() {
56
+ const collection = pullFromCache<Amity.SearchCommunityLiveCollectionCache>(this.cacheKey)?.data;
57
+ if (!collection) {
58
+ pushToCache(this.cacheKey, {
59
+ data: [],
60
+ params: {},
61
+ });
62
+ }
63
+ }
64
+
65
+ protected async persistModel(queryPayload: Amity.CommunityPayload & Amity.Pagination) {
66
+ await this.queryStreamController.saveToMainDB(queryPayload);
67
+ }
68
+
69
+ protected persistQueryStream({
70
+ response,
71
+ direction,
72
+ refresh,
73
+ }: Amity.LiveCollectionPersistQueryStreamParams<'community'>) {
74
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
75
+ }
76
+
77
+ startSubscription() {
78
+ return this.queryStreamController.subscribeRTE([
79
+ { fn: onCommunityDeleted, action: EnumCommunityActions.OnCommunityDeleted },
80
+ { fn: onCommunityUpdated, action: EnumCommunityActions.OnCommunityUpdated },
81
+ ]);
82
+ }
83
+
84
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
85
+ const collection = pullFromCache<Amity.SearchCommunityLiveCollectionCache>(this.cacheKey)?.data;
86
+ if (!collection) return;
87
+
88
+ const data = this.applyFilter(
89
+ collection.data
90
+ .map(id => pullFromCache<Amity.Community>(['community', 'get', id])!)
91
+ .filter(Boolean)
92
+ .map(({ data }) => data) ?? [],
93
+ );
94
+
95
+ if (!this.shouldNotify(data) && origin === 'event') return;
96
+
97
+ this.callback({
98
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
99
+ data,
100
+ hasNextPage: !!this.paginationController.getNextToken(),
101
+ loading,
102
+ error,
103
+ });
104
+ }
105
+
106
+ applyFilter(data: Amity.Community[]) {
107
+ const { userId } = getActiveClient();
108
+
109
+ let communities = data;
110
+
111
+ if (this.query.includeDeleted) {
112
+ communities = filterByPropEquality(communities, 'isDeleted', false);
113
+ }
114
+
115
+ if (this.query.categoryId) {
116
+ communities = communities.filter(c => c.categoryIds?.includes(this.query.categoryId!));
117
+ }
118
+
119
+ if (this.query.tags) {
120
+ communities = communities.filter(c => c.tags?.some(t => this.query.tags?.includes(t)));
121
+ }
122
+
123
+ if (this.query.membership && userId) {
124
+ communities = filterByCommunityMembership(communities, this.query.membership, userId);
125
+ }
126
+
127
+ return communities;
128
+ }
129
+ }
@@ -0,0 +1,31 @@
1
+ import { PaginationController } from '~/core/liveCollection/PaginationController';
2
+ import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';
3
+ import { inferIsDeleted } from '~/utils/inferIsDeleted';
4
+
5
+ /**
6
+ * TODO: handle cache receive cache option, and cache policy
7
+ * TODO: check if querybyIds is supported
8
+ */
9
+ export class CommunitiesPaginationController extends PaginationController<
10
+ 'community',
11
+ Amity.SearchCommunityLiveCollection
12
+ > {
13
+ async getRequest(queryParams: Amity.SearchCommunityLiveCollection, token: string | undefined) {
14
+ const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, ...params } = queryParams;
15
+ const options = token ? { token } : { limit };
16
+
17
+ const { data: queryResponse } = await this.http.get<Amity.CommunityPayload & Amity.Pagination>(
18
+ `/api/v3/communities`,
19
+ {
20
+ params: {
21
+ ...params,
22
+ isDeleted: inferIsDeleted(params.includeDeleted),
23
+ keyword: params.displayName,
24
+ filter: params.membership,
25
+ options,
26
+ },
27
+ },
28
+ );
29
+ return queryResponse;
30
+ }
31
+ }
@@ -0,0 +1,81 @@
1
+ import { QueryStreamController } from '~/core/liveCollection/QueryStreamController';
2
+ import { pullFromCache, pushToCache } from '~/cache/api';
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+ import { getResolver } from '~/core/model';
5
+ import { getActiveClient } from '~/client';
6
+ import { EnumCommunityActions } from './enums';
7
+
8
+ export class CommunitiesQueryStreamController extends QueryStreamController<
9
+ Amity.CommunityPayload,
10
+ Amity.SearchCommunityLiveCollection
11
+ > {
12
+ private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
13
+
14
+ private preparePayload: (response: Amity.CommunityPayload) => Amity.ProcessedCommunityPayload;
15
+
16
+ constructor(
17
+ query: Amity.SearchCommunityLiveCollection,
18
+ cacheKey: string[],
19
+ notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
20
+ preparePayload: (response: Amity.CommunityPayload) => Amity.ProcessedCommunityPayload,
21
+ ) {
22
+ super(query, cacheKey);
23
+ this.notifyChange = notifyChange;
24
+ this.preparePayload = preparePayload;
25
+ }
26
+
27
+ async saveToMainDB(response: Amity.CommunityPayload) {
28
+ const processedPayload = await this.preparePayload(response);
29
+
30
+ const client = getActiveClient();
31
+ const cachedAt = client.cache && Date.now();
32
+
33
+ if (client.cache) {
34
+ ingestInCache(processedPayload, { cachedAt });
35
+ }
36
+ }
37
+
38
+ appendToQueryStream(
39
+ response: Amity.CommunityPayload & Partial<Amity.Pagination>,
40
+ direction: Amity.LiveCollectionPageDirection,
41
+ refresh = false,
42
+ ) {
43
+ if (refresh) {
44
+ pushToCache(this.cacheKey, {
45
+ data: response.communities.map(getResolver('community')),
46
+ });
47
+ } else {
48
+ const collection = pullFromCache<Amity.SearchCommunityLiveCollectionCache>(
49
+ this.cacheKey,
50
+ )?.data;
51
+
52
+ const communities = collection?.data ?? [];
53
+
54
+ pushToCache(this.cacheKey, {
55
+ ...collection,
56
+ data: [...new Set([...communities, ...response.communities.map(getResolver('community'))])],
57
+ });
58
+ }
59
+ }
60
+
61
+ reactor(action: EnumCommunityActions) {
62
+ return (community: Amity.Community) => {
63
+ const collection = pullFromCache<Amity.SearchCommunityLiveCollectionCache>(
64
+ this.cacheKey,
65
+ )?.data;
66
+ if (!collection) return;
67
+
68
+ pushToCache(this.cacheKey, collection);
69
+ this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
70
+ };
71
+ }
72
+
73
+ subscribeRTE(
74
+ createSubscriber: {
75
+ fn: (reactor: (channel: Amity.Community) => void) => Amity.Unsubscriber;
76
+ action: EnumCommunityActions;
77
+ }[],
78
+ ) {
79
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
80
+ }
81
+ }
@@ -0,0 +1,5 @@
1
+ export enum EnumCommunityActions {
2
+ OnCommunityCreated = 'onCommunityCreated',
3
+ OnCommunityDeleted = 'onCommunityDeleted',
4
+ OnCommunityUpdated = 'onCommunityUpdated',
5
+ }
@@ -0,0 +1,56 @@
1
+ import { dropFromCache } from '~/cache/api';
2
+ import { getActiveClient } from '~/client/api';
3
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
4
+ import { SearchCommunityLiveCollectionController } from './searchCommunities/SearchCommunitiesLiveCollectionController';
5
+
6
+ /* begin_public_function
7
+ id: community.query
8
+ */
9
+ /**
10
+ * ```js
11
+ * import { CommunityRepository } from '@amityco/ts-sdk'
12
+ *
13
+ * let communities = []
14
+ * const unsub = CommunityRepository.searchCommunities({
15
+ * displayName: Amity.Community['displayName'],
16
+ * }, response => merge(communities, response.data))
17
+ * ```
18
+ *
19
+ * Observe all mutations on a list of {@link Amity.Community}s
20
+ *
21
+ * @param params for querying communities
22
+ * @param callback the function to call when new data are available
23
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the communities
24
+ *
25
+ * @category Community Live Collection
26
+ */
27
+ export const searchCommunities = (
28
+ params: Amity.CommunityLiveCollection,
29
+ callback: Amity.LiveCollectionCallback<Amity.Community>,
30
+ config?: Amity.LiveCollectionConfig,
31
+ ) => {
32
+ const { log, cache } = getActiveClient();
33
+
34
+ if (!cache) {
35
+ console.log(ENABLE_CACHE_MESSAGE);
36
+ }
37
+
38
+ const timestamp = Date.now();
39
+ log(`searchCommunities(tmpid: ${timestamp}) > listen`);
40
+
41
+ const searchCommunitiesLiveCollection = new SearchCommunityLiveCollectionController(
42
+ params,
43
+ callback,
44
+ );
45
+ const disposers = searchCommunitiesLiveCollection.startSubscription();
46
+
47
+ const cacheKey = searchCommunitiesLiveCollection.getCacheKey();
48
+
49
+ disposers.push(() => dropFromCache(cacheKey));
50
+
51
+ return () => {
52
+ log(`searchCommunities(tmpid: ${timestamp}) > dispose`);
53
+ disposers.forEach(fn => fn());
54
+ };
55
+ };
56
+ /* end_public_function */
@@ -57,10 +57,13 @@ export abstract class LiveCollectionController<
57
57
  }
58
58
  }
59
59
 
60
- protected loadPage(
60
+ protected loadPage({
61
61
  initial = false,
62
- direction: Amity.LiveCollectionPageDirection = Amity.LiveCollectionPageDirection.NEXT,
63
- ) {
62
+ direction = Amity.LiveCollectionPageDirection.NEXT,
63
+ }: {
64
+ initial?: boolean;
65
+ direction?: Amity.LiveCollectionPageDirection;
66
+ }) {
64
67
  this.setup();
65
68
 
66
69
  this.notifyChange({ origin: Amity.LiveDataOrigin.LOCAL, loading: true });
@@ -0,0 +1,29 @@
1
+ import { getActiveClient } from '~/client/api/activeClient';
2
+ import { createEventSubscriber } from '~/core/events';
3
+
4
+ /**
5
+ * Internal used only
6
+ *
7
+ * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
8
+ *
9
+ * @param callback The function to call when the event was fired
10
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
11
+ *
12
+ * @category MessageMarker Events
13
+ */
14
+ export const onChannelUnreadUpdatedLocal = (
15
+ callback: Amity.Listener<Amity.Events['local.channelUnread.updated']>,
16
+ ): Amity.Unsubscriber => {
17
+ const client = getActiveClient();
18
+
19
+ const filter = (payload: Amity.Events['local.channelUnread.updated']) => {
20
+ callback(payload);
21
+ };
22
+
23
+ return createEventSubscriber(
24
+ client,
25
+ 'channelMarker/onChannelUnreadUpdatedLocal',
26
+ 'local.channelUnread.updated',
27
+ filter,
28
+ );
29
+ };
@@ -22,7 +22,7 @@ export const onSubChannelMarkerFetched = (
22
22
  const client = getActiveClient();
23
23
 
24
24
  const filter = (payload: Amity.Events['local.subChannelMarker.fetched']) => {
25
- callback(payload.userFeedMarkers[0]);
25
+ payload.userFeedMarkers.forEach(callback);
26
26
  };
27
27
 
28
28
  return createEventSubscriber(
@@ -0,0 +1,29 @@
1
+ import { getActiveClient } from '~/client/api/activeClient';
2
+ import { createEventSubscriber } from '~/core/events';
3
+
4
+ /**
5
+ * Internal used only
6
+ *
7
+ * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
8
+ *
9
+ * @param callback The function to call when the event was fired
10
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
11
+ *
12
+ * @category MessageMarker Events
13
+ */
14
+ export const onSubChannelUnreadUpdatedLocal = (
15
+ callback: Amity.Listener<Amity.Events['local.subChannelUnread.updated']>,
16
+ ): Amity.Unsubscriber => {
17
+ const client = getActiveClient();
18
+
19
+ const filter = (payload: Amity.Events['local.subChannelUnread.updated']) => {
20
+ callback(payload);
21
+ };
22
+
23
+ return createEventSubscriber(
24
+ client,
25
+ 'subChannelMarker/onSubChannelUnreadUpdatedLocal',
26
+ 'local.subChannelUnread.updated',
27
+ filter,
28
+ );
29
+ };
@@ -23,10 +23,9 @@ export const onUserFeedMarkerUpdated = (
23
23
  const client = getActiveClient();
24
24
 
25
25
  const filter = (payload: Amity.Events['marker.userFeed-updated']) => {
26
- // update sub channel unread info and channel unread info in cache
27
- persistUnreadCountInfo(payload);
28
-
29
- callback(payload.feedMarkers[0]);
26
+ payload.feedMarkers.forEach(feedMarker => {
27
+ callback(feedMarker);
28
+ });
30
29
  };
31
30
 
32
31
  return createEventSubscriber(
@@ -1,7 +1,7 @@
1
1
  import { pushToCache, queryCache } from '~/cache/api';
2
2
  import { pullFromCache } from '~/cache/api/pullFromCache';
3
3
 
4
- export const reCalculateChannelUnreadInfo = async (channelId: string) => {
4
+ export const reCalculateChannelUnreadInfo = (channelId: string): Amity.ChannelUnreadInfo => {
5
5
  const cacheKeyChannelUnread = ['channelUnreadInfo', 'get', channelId];
6
6
  const cacheChannelUnreadInfo =
7
7
  pullFromCache<Amity.ChannelUnreadInfo>(cacheKeyChannelUnread)?.data;
@@ -25,7 +25,7 @@ export const reCalculateChannelUnreadInfo = async (channelId: string) => {
25
25
  isMentioned = subChannelUnreadsInfo.some(({ data }) => data.isMentioned);
26
26
  }
27
27
 
28
- pushToCache(cacheKeyChannelUnread, {
28
+ const channelUnreadInfo = {
29
29
  ...(cacheChannelUnreadInfo ?? {
30
30
  channelId,
31
31
  createdAt: new Date().toISOString(),
@@ -33,5 +33,9 @@ export const reCalculateChannelUnreadInfo = async (channelId: string) => {
33
33
  updatedAt: new Date().toISOString(),
34
34
  unreadCount: channelUnreads,
35
35
  isMentioned,
36
- });
36
+ };
37
+
38
+ pushToCache(cacheKeyChannelUnread, channelUnreadInfo);
39
+
40
+ return channelUnreadInfo;
37
41
  };
@@ -49,7 +49,7 @@ export class MessageLiveCollectionController extends LiveCollectionController<
49
49
  );
50
50
 
51
51
  this.callback = callback.bind(this);
52
- this.loadPage(true);
52
+ this.loadPage({ initial: true });
53
53
  }
54
54
 
55
55
  protected setup() {
@@ -107,8 +107,8 @@ export class MessageLiveCollectionController extends LiveCollectionController<
107
107
  if (!this.shouldNotify(data) && origin === 'event') return;
108
108
 
109
109
  this.callback({
110
- onNextPage: () => this.loadPage(false, Amity.LiveCollectionPageDirection.NEXT),
111
- onPrevPage: () => this.loadPage(false, Amity.LiveCollectionPageDirection.PREV),
110
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
111
+ onPrevPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.PREV }),
112
112
  data,
113
113
  hasNextPage: !!this.paginationController.getNextToken(),
114
114
  hasPrevPage: !!this.paginationController.getPrevToken(),
@@ -47,7 +47,7 @@ export const editPost = async <T extends Amity.PostContentType>(
47
47
  const cachedAt = client.cache && Date.now();
48
48
  if (client.cache) ingestInCache(data, { cachedAt });
49
49
 
50
- fireEvent('post.updated', data);
50
+ fireEvent('local.post.updated', data);
51
51
 
52
52
  const { posts } = data;
53
53
 
@@ -51,7 +51,7 @@ export const updatePost = async <T extends Amity.PostContentType>(
51
51
  const cachedAt = client.cache && Date.now();
52
52
  if (client.cache) ingestInCache(data, { cachedAt });
53
53
 
54
- fireEvent('post.updated', data);
54
+ fireEvent('local.post.updated', data);
55
55
 
56
56
  const { posts } = data;
57
57
 
@@ -13,6 +13,8 @@ import {
13
13
  } from '../events';
14
14
  import { pullFromCache, pushToCache } from '~/cache/api';
15
15
  import { LinkedObject } from '~/utils/linkedObject';
16
+ import { onCommentCreated, onCommentDeleted } from '~/commentRepository';
17
+ import { convertEventPayload } from '~/utils/event';
16
18
 
17
19
  /* begin_public_function
18
20
  id: post.get
@@ -80,6 +82,30 @@ export const getPost = (
80
82
  },
81
83
  onPostUnflagged,
82
84
  onPostUpdated,
85
+ convertEventPayload(
86
+ (callback: Amity.Listener<Amity.InternalComment>) => {
87
+ return onCommentCreated(async (comment: Amity.InternalComment) => {
88
+ if (comment.referenceId === postId) {
89
+ await _getPost(postId);
90
+ callback(comment);
91
+ }
92
+ });
93
+ },
94
+ 'referenceId',
95
+ 'post',
96
+ ),
97
+ convertEventPayload(
98
+ (callback: Amity.Listener<Amity.InternalComment>) => {
99
+ return onCommentDeleted(async (comment: Amity.InternalComment) => {
100
+ if (comment.referenceId === postId) {
101
+ await _getPost(postId);
102
+ callback(comment);
103
+ }
104
+ });
105
+ },
106
+ 'referenceId',
107
+ 'post',
108
+ ),
83
109
  ]);
84
110
  };
85
111
  /* end_public_function */
@@ -2,6 +2,7 @@
2
2
  import { getResolver } from '~/core/model';
3
3
  import { getActiveClient } from '~/client/api';
4
4
  import { dropFromCache, pullFromCache, pushToCache } from '~/cache/api';
5
+ import { getPost as _getPost } from '../api/getPost';
5
6
 
6
7
  import {
7
8
  createQuery,
@@ -34,6 +35,8 @@ import {
34
35
  onPostUpdated,
35
36
  } from '../events';
36
37
  import { queryPosts } from '../api/queryPosts';
38
+ import { convertEventPayload } from '~/utils/event';
39
+ import { onCommentCreated, onCommentDeleted } from '~/commentRepository';
37
40
 
38
41
  /* begin_public_function
39
42
  id: post.query
@@ -204,6 +207,34 @@ export const getPosts = (
204
207
  onPostUnflagged(realtimeRouter('onUnflagged')),
205
208
  onPostReactionAdded(realtimeRouter('onReactionAdded')),
206
209
  onPostReactionRemoved(realtimeRouter('onReactionRemoved')),
210
+ convertEventPayload(
211
+ (callback: Amity.Listener<Amity.InternalComment>) => {
212
+ return onCommentCreated(async (comment: Amity.InternalComment) => {
213
+ const currentCollection = pullFromCache<Amity.PostLiveCollectionCache>(cacheKey)?.data;
214
+
215
+ if (!currentCollection || currentCollection.data.includes(comment.referenceId)) return;
216
+
217
+ await _getPost(comment.referenceId);
218
+ callback(comment);
219
+ });
220
+ },
221
+ 'referenceId',
222
+ 'post',
223
+ )(realtimeRouter('onUpdate')),
224
+ convertEventPayload(
225
+ (callback: Amity.Listener<Amity.InternalComment>) => {
226
+ return onCommentDeleted(async (comment: Amity.InternalComment) => {
227
+ const currentCollection = pullFromCache<Amity.PostLiveCollectionCache>(cacheKey)?.data;
228
+
229
+ if (!currentCollection || currentCollection.data.includes(comment.referenceId)) return;
230
+
231
+ await _getPost(comment.referenceId);
232
+ callback(comment);
233
+ });
234
+ },
235
+ 'referenceId',
236
+ 'post',
237
+ )(realtimeRouter('onUpdate')),
207
238
  );
208
239
 
209
240
  onFetch(true);
@@ -48,7 +48,7 @@ export class GlobalStoryLiveCollectionController extends LiveCollectionControlle
48
48
 
49
49
  this.paginationController = paginationController;
50
50
  this.callback = callback.bind(this);
51
- this.loadPage(true);
51
+ this.loadPage({ initial: true });
52
52
  }
53
53
 
54
54
  protected setup() {
@@ -94,7 +94,7 @@ export class GlobalStoryLiveCollectionController extends LiveCollectionControlle
94
94
  ) as Amity.StoryTarget[];
95
95
 
96
96
  this.callback({
97
- onNextPage: () => this.loadPage(false, Amity.LiveCollectionPageDirection.NEXT),
97
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
98
98
  data,
99
99
  hasNextPage: !!this.paginationController.getNextToken(),
100
100
  loading,
@@ -42,7 +42,7 @@ export class StoryLiveCollectionController extends LiveCollectionController<
42
42
 
43
43
  this.paginationController = paginationController;
44
44
  this.callback = callback.bind(this);
45
- this.loadPage(true);
45
+ this.loadPage({ initial: true });
46
46
  }
47
47
 
48
48
  protected setup() {
@@ -20,7 +20,7 @@ import { getActiveClient } from '~/client';
20
20
  import { getSubChannelMessagePreviewWithUser } from '~/messagePreview/utils';
21
21
  import { isEqual } from '~/utils/isEqual';
22
22
  import { updateSubChannelCache } from '../utils/updateSubChannelCache';
23
- import { onUserFeedMarkerUpdated } from '~/marker/events/onUserFeedMarkerUpdated';
23
+ import { onSubChannelUnreadUpdatedLocal } from '~/marker/events/onSubChannelUnreadUpdatedLocal';
24
24
 
25
25
  /* begin_public_function
26
26
  id: subchannel.get
@@ -221,7 +221,7 @@ export const getSubChannel = (
221
221
  'subChannelId',
222
222
  'subChannel',
223
223
  ),
224
- convertEventPayload(onUserFeedMarkerUpdated, 'feedId', 'subChannel'),
224
+ convertEventPayload(onSubChannelUnreadUpdatedLocal, 'subChannelId', 'subChannel'),
225
225
  ],
226
226
  {
227
227
  forceDispatch: true,
@@ -29,7 +29,7 @@ import { getSubChannel } from '~/subChannelRepository/api/getSubChannel';
29
29
  import { updateSubChannelCache } from '~/subChannelRepository/utils/updateSubChannelCache';
30
30
  import { prepareSubChannelPayload } from '~/subChannelRepository/utils';
31
31
  import { onSubChannelFetched } from '~/subChannelRepository/events/onSubChannelFetched';
32
- import { onUserFeedMarkerUpdated } from '~/marker/events/onUserFeedMarkerUpdated';
32
+ import { onSubChannelUnreadUpdatedLocal } from '~/marker/events/onSubChannelUnreadUpdatedLocal';
33
33
 
34
34
  export class SubChannelLiveCollectionController extends LiveCollectionController<
35
35
  'subChannel',
@@ -59,7 +59,7 @@ export class SubChannelLiveCollectionController extends LiveCollectionController
59
59
  prepareSubChannelPayload,
60
60
  );
61
61
  this.callback = callback.bind(this);
62
- this.loadPage(true);
62
+ this.loadPage({ initial: true });
63
63
  }
64
64
 
65
65
  protected setup() {
@@ -294,7 +294,7 @@ export class SubChannelLiveCollectionController extends LiveCollectionController
294
294
  action: 'onUpdate',
295
295
  },
296
296
  {
297
- fn: convertEventPayload(onUserFeedMarkerUpdated, 'feedId', 'subChannel'),
297
+ fn: convertEventPayload(onSubChannelUnreadUpdatedLocal, 'subChannelId', 'subChannel'),
298
298
  action: 'onUpdate',
299
299
  },
300
300
  ]);
@@ -315,7 +315,7 @@ export class SubChannelLiveCollectionController extends LiveCollectionController
315
315
  if (!this.shouldNotify(data) && origin === 'event') return;
316
316
 
317
317
  this.callback({
318
- onNextPage: () => this.loadPage(false, Amity.LiveCollectionPageDirection.NEXT),
318
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
319
319
  data,
320
320
  hasNextPage: !!this.paginationController.getNextToken(),
321
321
  loading,