@amityco/ts-sdk 7.1.1-c8d4edca.0 → 7.1.1-dbdbe662.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.
- package/dist/@types/domains/channel.d.ts +3 -2
- package/dist/@types/domains/channel.d.ts.map +1 -1
- package/dist/channelRepository/events/onChannelDeleted.d.ts.map +1 -1
- package/dist/channelRepository/events/onChannelLeft.d.ts.map +1 -1
- package/dist/channelRepository/events/onChannelUnreadUpdatedLocal.d.ts +2 -2
- package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts +11 -0
- package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts.map +1 -0
- package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts +20 -0
- package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts.map +1 -0
- package/dist/channelRepository/observers/index.d.ts +1 -0
- package/dist/channelRepository/observers/index.d.ts.map +1 -1
- package/dist/channelRepository/utils/prepareChannelPayload.d.ts.map +1 -1
- package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts.map +1 -1
- package/dist/index.cjs.js +186 -20
- package/dist/index.esm.js +186 -20
- package/dist/index.umd.js +4 -4
- package/dist/marker/events/onChannelUnreadInfoUpdatedLocal.d.ts +2 -2
- package/dist/messageRepository/events/onMessageCreated.d.ts.map +1 -1
- package/dist/messageRepository/observers/getMessage.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/@types/domains/channel.ts +4 -2
- package/src/channelRepository/events/onChannelDeleted.ts +17 -4
- package/src/channelRepository/events/onChannelLeft.ts +11 -3
- package/src/channelRepository/events/onChannelUnreadUpdatedLocal.ts +2 -2
- package/src/channelRepository/internalApi/getTotalChannelsUnread.ts +38 -0
- package/src/channelRepository/observers/getTotalChannelsUnread.ts +129 -0
- package/src/channelRepository/observers/index.ts +1 -0
- package/src/channelRepository/utils/prepareChannelPayload.ts +16 -7
- package/src/client/utils/ReadReceiptSync/readReceiptSyncEngine.ts +5 -4
- package/src/marker/events/onChannelUnreadInfoUpdatedLocal.ts +2 -2
- package/src/messageRepository/events/onMessageCreated.ts +45 -1
- package/src/messageRepository/observers/getMessage.ts +0 -1
- package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts +0 -12
- package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts.map +0 -1
- 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.
|
|
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
|
|
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":"
|
|
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":"
|
|
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
|
@@ -84,6 +84,8 @@ declare global {
|
|
|
84
84
|
> & {
|
|
85
85
|
isMentioned: boolean;
|
|
86
86
|
subChannelsUnreadCount: number;
|
|
87
|
+
// legacy unread count does not use the maker service
|
|
88
|
+
unreadCount: number;
|
|
87
89
|
};
|
|
88
90
|
|
|
89
91
|
/* public type */
|
|
@@ -143,9 +145,9 @@ declare global {
|
|
|
143
145
|
channelId: Amity.Channel['channelId'];
|
|
144
146
|
unreadCount: number;
|
|
145
147
|
isMentioned: boolean;
|
|
146
|
-
readToSegment: number;
|
|
148
|
+
readToSegment: number | null;
|
|
147
149
|
lastSegment: number;
|
|
148
|
-
lastMentionSegment: number;
|
|
150
|
+
lastMentionSegment: number | null;
|
|
149
151
|
isDeleted: boolean;
|
|
150
152
|
};
|
|
151
153
|
}
|
|
@@ -6,6 +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, pullFromCache, pushToCache } from '~/cache/api';
|
|
9
10
|
|
|
10
11
|
type CallbackFn = (channel: Amity.StaticInternalChannel) => void;
|
|
11
12
|
const callbacks: CallbackFn[] = [];
|
|
@@ -28,12 +29,24 @@ export const onChannelDeleted = (callback: Amity.Listener<Amity.StaticInternalCh
|
|
|
28
29
|
const filter = async (payload: Amity.ChannelPayload) => {
|
|
29
30
|
const data = await prepareChannelPayload(payload);
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
const isConsistentMode = client.getMarkerSyncConsistentMode() && client.isUnreadCountEnabled;
|
|
33
|
+
const isLegacyUnreadCount = client.useLegacyUnreadCount;
|
|
34
|
+
|
|
35
|
+
data.channels.forEach(channel => {
|
|
36
|
+
if (isConsistentMode) {
|
|
33
37
|
addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
|
|
34
38
|
deleteChannelUnreadByChannelId(channel.channelId);
|
|
35
|
-
})
|
|
36
|
-
|
|
39
|
+
} else if (isLegacyUnreadCount) {
|
|
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
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
37
50
|
|
|
38
51
|
ingestInCache(data);
|
|
39
52
|
callbacks.forEach(cb => cb(data.channels[0]));
|
|
@@ -6,6 +6,7 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
|
6
6
|
import { prepareChannelPayload } from '../utils';
|
|
7
7
|
import { deleteChannelUnreadByChannelId } from '../../marker/utils/deleteChannelUnreadByChannelId';
|
|
8
8
|
import { addFlagIsDeletedSubChannelUnreadByChannelId } from '~/marker/utils/addFlagIsDeletedSubChannelUnreadByChannelId';
|
|
9
|
+
import { dropFromCache } from '~/cache/api';
|
|
9
10
|
|
|
10
11
|
type CallbackFn = (
|
|
11
12
|
channel: Amity.StaticInternalChannel,
|
|
@@ -40,10 +41,17 @@ export const onChannelLeft = (
|
|
|
40
41
|
isMessagePreviewUpdated: isLeftByMe,
|
|
41
42
|
});
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
const isConsistentMode = client.getMarkerSyncConsistentMode() && client.isUnreadCountEnabled;
|
|
45
|
+
const isLegacyUnreadCount = client.useLegacyUnreadCount;
|
|
46
|
+
|
|
47
|
+
if (isLeftByMe) {
|
|
44
48
|
preparedPayload.channels.forEach(channel => {
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
if (isConsistentMode) {
|
|
50
|
+
addFlagIsDeletedSubChannelUnreadByChannelId(channel.channelId);
|
|
51
|
+
deleteChannelUnreadByChannelId(channel.channelId);
|
|
52
|
+
} else if (isLegacyUnreadCount) {
|
|
53
|
+
dropFromCache(['channelUnread', 'get', channel.channelId]);
|
|
54
|
+
}
|
|
47
55
|
});
|
|
48
56
|
}
|
|
49
57
|
|
|
@@ -4,12 +4,12 @@ import { createEventSubscriber } from '~/core/events';
|
|
|
4
4
|
/**
|
|
5
5
|
* Internal used only
|
|
6
6
|
*
|
|
7
|
-
* Fired when an {@link Amity.
|
|
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
|
|
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
|
+
};
|
|
@@ -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
|
|
61
|
+
const channelUser = channelUsers.find(
|
|
62
62
|
channelUser =>
|
|
63
63
|
channelUser.channelId === channels[i].channelId && channelUser.userId === currentUserId,
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
|
|
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
|
|
75
|
-
isMentioned
|
|
83
|
+
unreadCount,
|
|
84
|
+
isMentioned,
|
|
76
85
|
isDeleted: channels[i].isDeleted,
|
|
77
86
|
});
|
|
78
87
|
}
|
|
@@ -38,7 +38,6 @@ export class MessageReadReceiptSyncEngine {
|
|
|
38
38
|
if (this.jobQueue.length === 0 || this.isActive === false) return;
|
|
39
39
|
|
|
40
40
|
const readReceipts = this.getReadReceipts();
|
|
41
|
-
console.log('[New 🌟 readReceipts] Sync read receipts', readReceipts);
|
|
42
41
|
if (readReceipts) {
|
|
43
42
|
this.markReadApi(readReceipts);
|
|
44
43
|
}
|
|
@@ -159,9 +158,11 @@ export class MessageReadReceiptSyncEngine {
|
|
|
159
158
|
const cacheKey = ['channelUnread', 'get', channelId];
|
|
160
159
|
const channelUnread = pullFromCache<Amity.ChannelUnread>(cacheKey)?.data;
|
|
161
160
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
161
|
+
if (
|
|
162
|
+
typeof channelUnread?.readToSegment === 'number' &&
|
|
163
|
+
channelUnread &&
|
|
164
|
+
segment > channelUnread.readToSegment
|
|
165
|
+
) {
|
|
165
166
|
channelUnread.readToSegment = segment;
|
|
166
167
|
channelUnread.unreadCount = Math.max(channelUnread.lastSegment - segment, 0);
|
|
167
168
|
|
|
@@ -4,12 +4,12 @@ import { createEventSubscriber } from '~/core/events';
|
|
|
4
4
|
/**
|
|
5
5
|
* Internal used only
|
|
6
6
|
*
|
|
7
|
-
* Fired when an {@link Amity.
|
|
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
|
|
12
|
+
* @category ChannelMarker Events
|
|
13
13
|
*/
|
|
14
14
|
export const onChannelUnreadInfoUpdatedLocal = (
|
|
15
15
|
callback: Amity.Listener<Amity.Events['local.channelUnreadInfo.updated']>,
|
|
@@ -1,11 +1,12 @@
|
|
|
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';
|
|
6
6
|
import { getActiveUser } from '~/client/api/activeUser';
|
|
7
7
|
import { markReadMessage } from '../utils/markReadMessage';
|
|
8
8
|
import { prepareMessagePayload } from '../utils';
|
|
9
|
+
import { pullFromCache, pushToCache } from '~/cache/api';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* ```js
|
|
@@ -40,6 +41,49 @@ export const onMessageCreatedMqtt = (
|
|
|
40
41
|
});
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
if (client.useLegacyUnreadCount) {
|
|
45
|
+
rawPayload.messages.forEach(message => {
|
|
46
|
+
const channelUnread = pullFromCache<Amity.ChannelUnread>([
|
|
47
|
+
'channelUnread',
|
|
48
|
+
'get',
|
|
49
|
+
message.channelId,
|
|
50
|
+
])?.data;
|
|
51
|
+
|
|
52
|
+
if (
|
|
53
|
+
!channelUnread ||
|
|
54
|
+
channelUnread.lastSegment >= message.segment ||
|
|
55
|
+
typeof channelUnread.readToSegment !== 'number' ||
|
|
56
|
+
typeof channelUnread.lastMentionSegment !== 'number'
|
|
57
|
+
)
|
|
58
|
+
return;
|
|
59
|
+
|
|
60
|
+
const lastSegment = message.segment;
|
|
61
|
+
const isMentionedInMessage = message.mentionedUsers?.some(mention => {
|
|
62
|
+
return (
|
|
63
|
+
mention.type === 'channel' ||
|
|
64
|
+
(mention.type === 'user' &&
|
|
65
|
+
client.userId &&
|
|
66
|
+
mention.userPublicIds.includes(client.userId))
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const lastMentionSegment = isMentionedInMessage
|
|
71
|
+
? message.segment
|
|
72
|
+
: channelUnread.lastMentionSegment;
|
|
73
|
+
|
|
74
|
+
const updatedChannelUnread = {
|
|
75
|
+
...channelUnread,
|
|
76
|
+
lastSegment,
|
|
77
|
+
unreadCount: Math.max(lastSegment - channelUnread.readToSegment, 0),
|
|
78
|
+
lastMentionSegment,
|
|
79
|
+
isMentioned: !(channelUnread.readToSegment >= lastMentionSegment),
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
pushToCache(['channelUnread', 'get', message.channelId], updatedChannelUnread);
|
|
83
|
+
fireEvent('local.channelUnread.updated', updatedChannelUnread);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
43
87
|
// Update in cache
|
|
44
88
|
ingestInCache(payload);
|
|
45
89
|
|
|
@@ -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.channelUnread.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,6BAA6B,CAAC,CAAC,KACpE,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.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
|
-
'channel/onChannelUnreadUpdatedLocal',
|
|
26
|
-
'local.channelUnread.updated',
|
|
27
|
-
filter,
|
|
28
|
-
);
|
|
29
|
-
};
|