@amityco/ts-sdk 7.1.1-207e990f.0 → 7.1.1-53d7c83.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.
@@ -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,YA0ER,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-207e990f.0",
3
+ "version": "7.1.1-53d7c83.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",
@@ -145,9 +145,9 @@ declare global {
145
145
  channelId: Amity.Channel['channelId'];
146
146
  unreadCount: number;
147
147
  isMentioned: boolean;
148
- readToSegment: number;
148
+ readToSegment: number | null;
149
149
  lastSegment: number;
150
- lastMentionSegment: number;
150
+ lastMentionSegment: number | null;
151
151
  isDeleted: boolean;
152
152
  };
153
153
  }
@@ -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';
@@ -58,21 +58,30 @@ const updateChannelUnread = ({
58
58
  }) => {
59
59
  for (let i = 0; i < channels.length; i += 1) {
60
60
  const cacheKey = ['channelUnread', 'get', channels[i].channelId];
61
- const { readToSegment, lastMentionedSegment } = channelUsers.find(
61
+ const channelUser = channelUsers.find(
62
62
  channelUser =>
63
63
  channelUser.channelId === channels[i].channelId && channelUser.userId === currentUserId,
64
- ) || {
65
- readToSegment: 0,
66
- lastMentionedSegment: 0,
67
- };
64
+ );
65
+
66
+ let unreadCount = 0;
67
+ let readToSegment = null;
68
+ let lastMentionedSegment = null;
69
+ let isMentioned = false;
70
+
71
+ if (channelUser) {
72
+ readToSegment = channelUser.readToSegment;
73
+ lastMentionedSegment = channelUser.lastMentionedSegment;
74
+ unreadCount = Math.max(channels[i].messageCount - readToSegment, 0);
75
+ isMentioned = lastMentionedSegment > readToSegment;
76
+ }
68
77
 
69
78
  pushToCache(cacheKey, {
70
79
  channelId: channels[i].channelId,
71
80
  lastSegment: channels[i].messageCount,
72
81
  readToSegment,
73
82
  lastMentionedSegment,
74
- unreadCount: channels[i].messageCount - readToSegment,
75
- isMentioned: lastMentionedSegment > readToSegment,
83
+ unreadCount,
84
+ isMentioned,
76
85
  isDeleted: channels[i].isDeleted,
77
86
  });
78
87
  }
@@ -158,7 +158,11 @@ export class MessageReadReceiptSyncEngine {
158
158
  const cacheKey = ['channelUnread', 'get', channelId];
159
159
  const channelUnread = pullFromCache<Amity.ChannelUnread>(cacheKey)?.data;
160
160
 
161
- if (channelUnread && segment > channelUnread.readToSegment) {
161
+ if (
162
+ typeof channelUnread?.readToSegment === 'number' &&
163
+ channelUnread &&
164
+ segment > channelUnread.readToSegment
165
+ ) {
162
166
  channelUnread.readToSegment = segment;
163
167
  channelUnread.unreadCount = Math.max(channelUnread.lastSegment - segment, 0);
164
168
 
@@ -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';
@@ -48,7 +48,14 @@ export const onMessageCreatedMqtt = (
48
48
  'get',
49
49
  message.channelId,
50
50
  ])?.data;
51
- if (!channelUnread || channelUnread.lastSegment >= message.segment) return;
51
+
52
+ if (
53
+ !channelUnread ||
54
+ channelUnread.lastSegment >= message.segment ||
55
+ typeof channelUnread.readToSegment !== 'number' ||
56
+ typeof channelUnread.lastMentionSegment !== 'number'
57
+ )
58
+ return;
52
59
 
53
60
  const lastSegment = message.segment;
54
61
  const isMentionedInMessage = message.mentionedUsers?.some(mention => {
@@ -64,13 +71,16 @@ export const onMessageCreatedMqtt = (
64
71
  ? message.segment
65
72
  : channelUnread.lastMentionSegment;
66
73
 
67
- pushToCache(['channelUnread', 'get', message.channelId], {
74
+ const updatedChannelUnread = {
68
75
  ...channelUnread,
69
76
  lastSegment,
70
- unreadCount: lastSegment - channelUnread.readToSegment,
77
+ unreadCount: Math.max(lastSegment - channelUnread.readToSegment, 0),
71
78
  lastMentionSegment,
72
79
  isMentioned: !(channelUnread.readToSegment >= lastMentionSegment),
73
- });
80
+ };
81
+
82
+ pushToCache(['channelUnread', 'get', message.channelId], updatedChannelUnread);
83
+ fireEvent('local.channelUnread.updated', updatedChannelUnread);
74
84
  });
75
85
  }
76
86
 
@@ -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