@amityco/ts-sdk 7.1.1-a5f8c762.0 → 7.1.1-ce25d503.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 (27) hide show
  1. package/dist/channelRepository/events/onChannelDeleted.d.ts.map +1 -1
  2. package/dist/channelRepository/events/onChannelUnreadUpdatedLocal.d.ts +2 -2
  3. package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts +11 -0
  4. package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts.map +1 -0
  5. package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts +20 -0
  6. package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts.map +1 -0
  7. package/dist/channelRepository/observers/index.d.ts +1 -0
  8. package/dist/channelRepository/observers/index.d.ts.map +1 -1
  9. package/dist/index.cjs.js +128 -7
  10. package/dist/index.esm.js +128 -7
  11. package/dist/index.umd.js +4 -4
  12. package/dist/marker/events/onChannelUnreadInfoUpdatedLocal.d.ts +2 -2
  13. package/dist/messageRepository/events/onMessageCreated.d.ts.map +1 -1
  14. package/dist/messageRepository/observers/getMessage.d.ts.map +1 -1
  15. package/package.json +1 -1
  16. package/src/channelRepository/events/onChannelDeleted.ts +9 -2
  17. package/src/channelRepository/events/onChannelUnreadUpdatedLocal.ts +2 -2
  18. package/src/channelRepository/internalApi/getTotalChannelsUnread.ts +38 -0
  19. package/src/channelRepository/observers/getTotalChannelsUnread.ts +129 -0
  20. package/src/channelRepository/observers/index.ts +1 -0
  21. package/src/channelRepository/utils/prepareChannelPayload.ts +1 -1
  22. package/src/marker/events/onChannelUnreadInfoUpdatedLocal.ts +2 -2
  23. package/src/messageRepository/events/onMessageCreated.ts +7 -4
  24. package/src/messageRepository/observers/getMessage.ts +0 -1
  25. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts +0 -12
  26. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts.map +0 -1
  27. package/src/marker/events/onChannelUnreadUpdatedLocal.ts +0 -29
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * Internal used only
3
3
  *
4
- * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
4
+ * Fired when an {@link Amity.channelUnreadInfo} has been updated.
5
5
  *
6
6
  * @param callback The function to call when the event was fired
7
7
  * @returns an {@link Amity.Unsubscriber} function to stop listening
8
8
  *
9
- * @category MessageMarker Events
9
+ * @category ChannelMarker Events
10
10
  */
11
11
  export declare const onChannelUnreadInfoUpdatedLocal: (callback: Amity.Listener<Amity.Events['local.channelUnreadInfo.updated']>) => Amity.Unsubscriber;
12
12
  //# sourceMappingURL=onChannelUnreadInfoUpdatedLocal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"onMessageCreated.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/events/onMessageCreated.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,oBAAoB,aACrB,MAAM,QAAQ,CAAC,MAAM,eAAe,CAAC,KAC9C,MAAM,YAgER,CAAC;AAEF,eAAO,MAAM,qBAAqB,aACtB,MAAM,QAAQ,CAAC,MAAM,eAAe,CAAC,KAC9C,MAAM,YAoBR,CAAC"}
1
+ {"version":3,"file":"onMessageCreated.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/events/onMessageCreated.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,oBAAoB,aACrB,MAAM,QAAQ,CAAC,MAAM,eAAe,CAAC,KAC9C,MAAM,YAmER,CAAC;AAEF,eAAO,MAAM,qBAAqB,aACtB,MAAM,QAAQ,CAAC,MAAM,eAAe,CAAC,KAC9C,MAAM,YAoBR,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"getMessage.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/observers/getMessage.ts"],"names":[],"mappings":"AAoBA;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,UAAU,cACV,MAAM,OAAO,CAAC,WAAW,CAAC,YAC3B,MAAM,kBAAkB,CAAC,MAAM,OAAO,CAAC,KAChD,MAAM,YAmBR,CAAC"}
1
+ {"version":3,"file":"getMessage.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/observers/getMessage.ts"],"names":[],"mappings":"AAmBA;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,UAAU,cACV,MAAM,OAAO,CAAC,WAAW,CAAC,YAC3B,MAAM,kBAAkB,CAAC,MAAM,OAAO,CAAC,KAChD,MAAM,YAmBR,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amityco/ts-sdk",
3
- "version": "7.1.1-a5f8c762.0",
3
+ "version": "7.1.1-ce25d503.0",
4
4
  "license": "CC-BY-ND-4.0",
5
5
  "author": "amity.co <developers@amity.co> (https://amity.co)",
6
6
  "description": "Amity Social Cloud Typescript SDK",
@@ -6,7 +6,7 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
6
6
  import { prepareChannelPayload } from '../utils/prepareChannelPayload';
7
7
  import { addFlagIsDeletedSubChannelUnreadByChannelId } from '~/marker/utils/addFlagIsDeletedSubChannelUnreadByChannelId';
8
8
  import { deleteChannelUnreadByChannelId } from '../../marker/utils/deleteChannelUnreadByChannelId';
9
- import { dropFromCache } from '~/cache/api';
9
+ import { dropFromCache, pullFromCache, pushToCache } from '~/cache/api';
10
10
 
11
11
  type CallbackFn = (channel: Amity.StaticInternalChannel) => void;
12
12
  const callbacks: CallbackFn[] = [];
@@ -37,7 +37,14 @@ export const onChannelDeleted = (callback: Amity.Listener<Amity.StaticInternalCh
37
37
  addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
38
38
  deleteChannelUnreadByChannelId(channel.channelId);
39
39
  } else if (isLegacyUnreadCount) {
40
- dropFromCache(['channelUnread', 'get', channel.channelId]);
40
+ const cacheKey = ['channelUnread', 'get', channel.channelId];
41
+ const cache = pullFromCache<Amity.ChannelUnread>(cacheKey);
42
+ if (cache) {
43
+ pushToCache(cacheKey, {
44
+ ...cache,
45
+ isDeleted: true,
46
+ });
47
+ }
41
48
  }
42
49
  });
43
50
 
@@ -4,12 +4,12 @@ import { createEventSubscriber } from '~/core/events';
4
4
  /**
5
5
  * Internal used only
6
6
  *
7
- * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
7
+ * Fired when an {@link Amity.ChannelUnread} has been updated.
8
8
  *
9
9
  * @param callback The function to call when the event was fired
10
10
  * @returns an {@link Amity.Unsubscriber} function to stop listening
11
11
  *
12
- * @category MessageMarker Events
12
+ * @category Channel Events
13
13
  */
14
14
  export const onChannelUnreadUpdatedLocal = (
15
15
  callback: Amity.Listener<Amity.Events['local.channelUnread.updated']>,
@@ -0,0 +1,38 @@
1
+ import { queryCache } from '~/cache/api';
2
+ import { getActiveClient } from '~/client/api/activeClient';
3
+
4
+ /**
5
+ *
6
+ * Calculate user unread from {@link Amity.ChannelUnread} objects
7
+ *
8
+ * @returns the {@link Amity.UserUnread} objects
9
+ *
10
+ * @category Channel API
11
+ * @async
12
+ */
13
+
14
+ export const getTotalChannelsUnread = (): Amity.Cached<Amity.UserUnread> => {
15
+ const client = getActiveClient();
16
+ client.log('channel/getTotalChannelsUnread.locally');
17
+
18
+ const cachedChannelsUnread =
19
+ queryCache<Amity.ChannelUnread>(['channelUnread', 'get'])?.filter(({ data }) => {
20
+ return !data.isDeleted;
21
+ }) || [];
22
+
23
+ const totalChannelsUnread: Amity.UserUnread = cachedChannelsUnread?.reduce(
24
+ (acc, { data }) => {
25
+ acc.unreadCount += data.unreadCount;
26
+ acc.isMentioned = acc.isMentioned || data.isMentioned;
27
+ return acc;
28
+ },
29
+ { unreadCount: 0, isMentioned: false as boolean },
30
+ ) || { unreadCount: 0, isMentioned: false };
31
+
32
+ const cachedAt = client.cache && Date.now();
33
+
34
+ return {
35
+ data: totalChannelsUnread,
36
+ cachedAt,
37
+ };
38
+ };
@@ -0,0 +1,129 @@
1
+ import { getActiveUser } from '~/client/api/activeUser';
2
+ import { getTotalChannelsUnread as _getTotalChannelsUnread } from '../internalApi/getTotalChannelsUnread';
3
+ import { onChannelUnreadUpdatedLocal } from '../events/onChannelUnreadUpdatedLocal';
4
+ import { ASCApiError, ASCError } from '~/core/errors';
5
+ import { getActiveClient } from '~/client';
6
+ import { convertGetterPropsToStatic } from '~/utils/object';
7
+ import { createQuery, runQuery } from '~/core/query';
8
+ import {
9
+ UNSYNCED_OBJECT_CACHED_AT_MESSAGE,
10
+ UNSYNCED_OBJECT_CACHED_AT_VALUE,
11
+ } from '~/utils/constants';
12
+ import { isEqual } from '~/utils/isEqual';
13
+
14
+ /* begin_public_function
15
+ id: totalChannelsUnread.get
16
+ */
17
+ /**
18
+ * ```js
19
+ * import { ChannelRepository } from '@amityco/ts-sdk';
20
+ *
21
+ * let totalChannelsUnread;
22
+ *
23
+ * const unsubscribe = ChannelRepository.getTotalChannelsUnread(response => {
24
+ * unread = response.data;
25
+ * });
26
+ * ```
27
+ *
28
+ * Observe all mutation on a given {@link Amity.UserUnread}
29
+ *
30
+ * @returns An {@link Amity.UserUnread} function to run when willing to stop observing the message
31
+ *
32
+ * @category User Unread Live Object
33
+ *
34
+ */
35
+
36
+ export const getTotalChannelsUnread = (
37
+ callback: Amity.LiveObjectCallback<Amity.UserUnread | undefined>,
38
+ ): Amity.Unsubscriber => {
39
+ const { _id: userId } = getActiveUser();
40
+
41
+ if (!userId)
42
+ throw new ASCError(
43
+ 'The _id has not been defined in ActiveUser',
44
+ Amity.ClientError.UNKNOWN_ERROR,
45
+ Amity.ErrorLevel.ERROR,
46
+ );
47
+
48
+ const { log, cache } = getActiveClient();
49
+
50
+ if (!cache) {
51
+ console.log('For using Live Object feature you need to enable Cache!');
52
+ }
53
+
54
+ const timestamp = Date.now();
55
+ log(`liveTotalChannelsUnread(tmpid: ${timestamp}) > listen`);
56
+
57
+ const disposers: Amity.Unsubscriber[] = [];
58
+
59
+ let isUnsyncedModel = false; // for messages
60
+
61
+ let model: Amity.UserUnread | undefined;
62
+
63
+ const dispatcher = (data: Amity.LiveObject<Amity.UserUnread | undefined>) => {
64
+ const { data: userUnread } = data;
65
+
66
+ const callbackModel = userUnread
67
+ ? {
68
+ unreadCount: userUnread.unreadCount,
69
+ isMentioned: userUnread.isMentioned,
70
+ }
71
+ : undefined;
72
+
73
+ model = callbackModel ? convertGetterPropsToStatic(callbackModel) : callbackModel;
74
+
75
+ callback({
76
+ data: callbackModel
77
+ ? { ...callbackModel, isMentioned: callbackModel.isMentioned }
78
+ : callbackModel,
79
+
80
+ loading: data.loading,
81
+ error: data.error,
82
+ });
83
+ };
84
+
85
+ const realtimeRouter = (userUnread: Amity.UserUnread) => {
86
+ if (isEqual(model, userUnread)) return;
87
+
88
+ dispatcher({
89
+ loading: false,
90
+ data: userUnread,
91
+ });
92
+ };
93
+
94
+ const onFetch = () => {
95
+ const query = createQuery(async () => _getTotalChannelsUnread());
96
+
97
+ runQuery(query, ({ error, data, loading, origin, cachedAt }) => {
98
+ if (cachedAt === UNSYNCED_OBJECT_CACHED_AT_VALUE) {
99
+ dispatcher({
100
+ data,
101
+ origin,
102
+ loading: false,
103
+ error: new ASCApiError(
104
+ UNSYNCED_OBJECT_CACHED_AT_MESSAGE,
105
+ Amity.ClientError.DISALOOW_UNSYNCED_OBJECT,
106
+ Amity.ErrorLevel.ERROR,
107
+ ),
108
+ });
109
+
110
+ isUnsyncedModel = true;
111
+ disposers.forEach(fn => fn());
112
+ } else if (!isUnsyncedModel) {
113
+ dispatcher({ loading, data, origin, error });
114
+ }
115
+
116
+ if (error) {
117
+ disposers.forEach(fn => fn());
118
+ }
119
+ });
120
+ };
121
+
122
+ disposers.push(onChannelUnreadUpdatedLocal(realtimeRouter));
123
+
124
+ onFetch();
125
+
126
+ return () => {
127
+ disposers.forEach(fn => fn());
128
+ };
129
+ };
@@ -1,2 +1,3 @@
1
1
  export * from './getChannel';
2
2
  export * from './getChannels';
3
+ export * from './getTotalChannelsUnread';
@@ -71,7 +71,7 @@ const updateChannelUnread = ({
71
71
  lastSegment: channels[i].messageCount,
72
72
  readToSegment,
73
73
  lastMentionedSegment,
74
- unreadCount: channels[i].messageCount - readToSegment,
74
+ unreadCount: Math.max(channels[i].messageCount - readToSegment, 0),
75
75
  isMentioned: lastMentionedSegment > readToSegment,
76
76
  isDeleted: channels[i].isDeleted,
77
77
  });
@@ -4,12 +4,12 @@ import { createEventSubscriber } from '~/core/events';
4
4
  /**
5
5
  * Internal used only
6
6
  *
7
- * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
7
+ * Fired when an {@link Amity.channelUnreadInfo} has been updated.
8
8
  *
9
9
  * @param callback The function to call when the event was fired
10
10
  * @returns an {@link Amity.Unsubscriber} function to stop listening
11
11
  *
12
- * @category MessageMarker Events
12
+ * @category ChannelMarker Events
13
13
  */
14
14
  export const onChannelUnreadInfoUpdatedLocal = (
15
15
  callback: Amity.Listener<Amity.Events['local.channelUnreadInfo.updated']>,
@@ -1,5 +1,5 @@
1
1
  import { getActiveClient } from '~/client/api/activeClient';
2
- import { createEventSubscriber } from '~/core/events';
2
+ import { createEventSubscriber, fireEvent } from '~/core/events';
3
3
  import { ingestInCache } from '~/cache/api/ingestInCache';
4
4
  import { updateSubChannelUnreadFromMessage } from '~/marker/utils/updateSubChannelUnreadFromMessage';
5
5
  import { reCalculateChannelUnreadInfo } from '~/marker/utils/reCalculateChannelUnreadInfo';
@@ -64,13 +64,16 @@ export const onMessageCreatedMqtt = (
64
64
  ? message.segment
65
65
  : channelUnread.lastMentionSegment;
66
66
 
67
- pushToCache(['channelUnread', 'get', message.channelId], {
67
+ const updatedChannelUnread = {
68
68
  ...channelUnread,
69
69
  lastSegment,
70
- unreadCount: lastSegment - channelUnread.readToSegment,
70
+ unreadCount: Math.max(lastSegment - channelUnread.readToSegment, 0),
71
71
  lastMentionSegment,
72
72
  isMentioned: !(channelUnread.readToSegment >= lastMentionSegment),
73
- });
73
+ };
74
+
75
+ pushToCache(['channelUnread', 'get', message.channelId], updatedChannelUnread);
76
+ fireEvent('local.channelUnread.updated', updatedChannelUnread);
74
77
  });
75
78
  }
76
79
 
@@ -13,7 +13,6 @@ import {
13
13
  } from '../events';
14
14
  import { onMessageFetched } from '../events/onMessageFetched';
15
15
  import { LinkedObject } from '~/utils/linkedObject';
16
- import { date } from '~/utils/tests';
17
16
 
18
17
  /* begin_public_function
19
18
  id: message.get
@@ -1,12 +0,0 @@
1
- /**
2
- * Internal used only
3
- *
4
- * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
5
- *
6
- * @param callback The function to call when the event was fired
7
- * @returns an {@link Amity.Unsubscriber} function to stop listening
8
- *
9
- * @category MessageMarker Events
10
- */
11
- export declare const onChannelUnreadUpdatedLocal: (callback: Amity.Listener<Amity.Events['local.channelUnreadInfo.updated']>) => Amity.Unsubscriber;
12
- //# sourceMappingURL=onChannelUnreadUpdatedLocal.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"onChannelUnreadUpdatedLocal.d.ts","sourceRoot":"","sources":["../../../src/marker/events/onChannelUnreadUpdatedLocal.ts"],"names":[],"mappings":"AAGA;;;;;;;;;GASG;AACH,eAAO,MAAM,2BAA2B,aAC5B,MAAM,QAAQ,CAAC,MAAM,MAAM,CAAC,iCAAiC,CAAC,CAAC,KACxE,MAAM,YAaR,CAAC"}
@@ -1,29 +0,0 @@
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.channelUnreadInfo.updated']>,
16
- ): Amity.Unsubscriber => {
17
- const client = getActiveClient();
18
-
19
- const filter = (payload: Amity.Events['local.channelUnreadInfo.updated']) => {
20
- callback(payload);
21
- };
22
-
23
- return createEventSubscriber(
24
- client,
25
- 'channel/onChannelUnreadUpdatedLocal',
26
- 'local.channelUnreadInfo.updated',
27
- filter,
28
- );
29
- };