@amityco/ts-sdk 7.1.1-67cf0d9.0 → 7.1.1-6dd179b0.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 (149) hide show
  1. package/dist/@types/core/events.d.ts +5 -2
  2. package/dist/@types/core/events.d.ts.map +1 -1
  3. package/dist/@types/core/model.d.ts +4 -2
  4. package/dist/@types/core/model.d.ts.map +1 -1
  5. package/dist/@types/core/payload.d.ts +18 -0
  6. package/dist/@types/core/payload.d.ts.map +1 -1
  7. package/dist/@types/core/readReceipt.d.ts +1 -12
  8. package/dist/@types/core/readReceipt.d.ts.map +1 -1
  9. package/dist/@types/domains/channel.d.ts +0 -10
  10. package/dist/@types/domains/channel.d.ts.map +1 -1
  11. package/dist/@types/domains/client.d.ts +0 -2
  12. package/dist/@types/domains/client.d.ts.map +1 -1
  13. package/dist/@types/domains/notification.d.ts +81 -0
  14. package/dist/@types/domains/notification.d.ts.map +1 -0
  15. package/dist/@types/index.d.ts +1 -0
  16. package/dist/@types/index.d.ts.map +1 -1
  17. package/dist/channelRepository/events/onChannelDeleted.d.ts.map +1 -1
  18. package/dist/channelRepository/events/onChannelLeft.d.ts.map +1 -1
  19. package/dist/channelRepository/observers/getChannel.d.ts.map +1 -1
  20. package/dist/channelRepository/observers/getChannels/ChannelLiveCollectionController.d.ts.map +1 -1
  21. package/dist/channelRepository/observers/index.d.ts +0 -1
  22. package/dist/channelRepository/observers/index.d.ts.map +1 -1
  23. package/dist/channelRepository/utils/constructChannelDynamicValue.d.ts.map +1 -1
  24. package/dist/channelRepository/utils/prepareChannelPayload.d.ts.map +1 -1
  25. package/dist/client/api/createClient.d.ts +0 -1
  26. package/dist/client/api/createClient.d.ts.map +1 -1
  27. package/dist/client/api/enableUnreadCount.d.ts.map +1 -1
  28. package/dist/client/api/login.d.ts.map +1 -1
  29. package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts +4 -2
  30. package/dist/client/utils/ReadReceiptSync/readReceiptSyncEngine.d.ts.map +1 -1
  31. package/dist/client/utils/endpoints.d.ts +0 -1
  32. package/dist/client/utils/endpoints.d.ts.map +1 -1
  33. package/dist/client/utils/setClientToken.d.ts.map +1 -1
  34. package/dist/core/events.d.ts +3 -3
  35. package/dist/core/events.d.ts.map +1 -1
  36. package/dist/core/model/idResolvers.d.ts.map +1 -1
  37. package/dist/core/model/index.d.ts.map +1 -1
  38. package/dist/index.cjs.js +468 -596
  39. package/dist/index.d.ts +1 -0
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.esm.js +451 -580
  42. package/dist/index.umd.js +4 -4
  43. package/dist/{channelRepository → marker}/events/onChannelUnreadUpdatedLocal.d.ts +2 -2
  44. package/dist/marker/events/onChannelUnreadUpdatedLocal.d.ts.map +1 -0
  45. package/dist/messageRepository/events/onMessageCreated.d.ts.map +1 -1
  46. package/dist/messageRepository/observers/getMessage.d.ts.map +1 -1
  47. package/dist/messageRepository/utils/markReadMessage.d.ts.map +1 -1
  48. package/dist/notificationTray/api/index.d.ts +3 -0
  49. package/dist/notificationTray/api/index.d.ts.map +1 -0
  50. package/dist/notificationTray/api/markItemsSeen.d.ts +16 -0
  51. package/dist/notificationTray/api/markItemsSeen.d.ts.map +1 -0
  52. package/dist/notificationTray/api/markTraySeen.d.ts +19 -0
  53. package/dist/notificationTray/api/markTraySeen.d.ts.map +1 -0
  54. package/dist/notificationTray/events/index.d.ts +2 -0
  55. package/dist/notificationTray/events/index.d.ts.map +1 -0
  56. package/dist/notificationTray/events/onNotificationTraySeenUpdated.d.ts +17 -0
  57. package/dist/notificationTray/events/onNotificationTraySeenUpdated.d.ts.map +1 -0
  58. package/dist/notificationTray/index.d.ts +4 -0
  59. package/dist/notificationTray/index.d.ts.map +1 -0
  60. package/dist/notificationTray/internalApi/getNotificationTraySeen.d.ts +30 -0
  61. package/dist/notificationTray/internalApi/getNotificationTraySeen.d.ts.map +1 -0
  62. package/dist/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsLiveCollectionController.d.ts +13 -0
  63. package/dist/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsLiveCollectionController.d.ts.map +1 -0
  64. package/dist/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsPaginationController.d.ts +9 -0
  65. package/dist/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsPaginationController.d.ts.map +1 -0
  66. package/dist/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsQuerystreamController.d.ts +9 -0
  67. package/dist/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsQuerystreamController.d.ts.map +1 -0
  68. package/dist/notificationTray/observers/getNotificationTrayItems.d.ts +12 -0
  69. package/dist/notificationTray/observers/getNotificationTrayItems.d.ts.map +1 -0
  70. package/dist/notificationTray/observers/getNotificationTraySeen.d.ts +21 -0
  71. package/dist/notificationTray/observers/getNotificationTraySeen.d.ts.map +1 -0
  72. package/dist/notificationTray/observers/index.d.ts +3 -0
  73. package/dist/notificationTray/observers/index.d.ts.map +1 -0
  74. package/dist/notificationTray/utils/prepareNotificationTrayItemsPayload.d.ts +2 -0
  75. package/dist/notificationTray/utils/prepareNotificationTrayItemsPayload.d.ts.map +1 -0
  76. package/dist/utils/linkedObject/index.d.ts +1 -0
  77. package/dist/utils/linkedObject/index.d.ts.map +1 -1
  78. package/dist/utils/linkedObject/notificationTrayLinkedObject.d.ts +2 -0
  79. package/dist/utils/linkedObject/notificationTrayLinkedObject.d.ts.map +1 -0
  80. package/package.json +1 -1
  81. package/src/@types/core/events.ts +6 -2
  82. package/src/@types/core/model.ts +6 -4
  83. package/src/@types/core/payload.ts +25 -0
  84. package/src/@types/core/readReceipt.ts +1 -14
  85. package/src/@types/domains/channel.ts +0 -13
  86. package/src/@types/domains/client.ts +0 -3
  87. package/src/@types/domains/notification.ts +90 -0
  88. package/src/@types/index.ts +1 -0
  89. package/src/channelRepository/events/onChannelDeleted.ts +4 -17
  90. package/src/channelRepository/events/onChannelLeft.ts +3 -11
  91. package/src/channelRepository/observers/getChannel.ts +1 -3
  92. package/src/channelRepository/observers/getChannels/ChannelLiveCollectionController.ts +1 -6
  93. package/src/channelRepository/observers/index.ts +0 -1
  94. package/src/channelRepository/utils/constructChannelDynamicValue.ts +2 -12
  95. package/src/channelRepository/utils/prepareChannelPayload.ts +17 -68
  96. package/src/client/api/createClient.ts +1 -7
  97. package/src/client/api/enableUnreadCount.ts +0 -1
  98. package/src/client/api/login.ts +1 -5
  99. package/src/client/utils/ReadReceiptSync/readReceiptSyncEngine.ts +99 -74
  100. package/src/client/utils/endpoints.ts +0 -1
  101. package/src/client/utils/setClientToken.ts +0 -8
  102. package/src/core/model/idResolvers.ts +3 -2
  103. package/src/core/model/index.ts +2 -0
  104. package/src/fileRepository/api/uploadFile.ts +1 -1
  105. package/src/fileRepository/api/uploadImage.ts +1 -1
  106. package/src/fileRepository/api/uploadVideo.ts +1 -1
  107. package/src/index.ts +2 -0
  108. package/src/{channelRepository → marker}/events/onChannelUnreadUpdatedLocal.ts +3 -3
  109. package/src/messageRepository/events/onMessageCreated.ts +1 -45
  110. package/src/messageRepository/observers/getMessage.ts +1 -0
  111. package/src/messageRepository/utils/markReadMessage.ts +3 -10
  112. package/src/notificationTray/api/index.ts +2 -0
  113. package/src/notificationTray/api/markItemsSeen.ts +59 -0
  114. package/src/notificationTray/api/markTraySeen.ts +65 -0
  115. package/src/notificationTray/events/index.ts +1 -0
  116. package/src/notificationTray/events/onNotificationTraySeenUpdated.ts +36 -0
  117. package/src/notificationTray/index.ts +3 -0
  118. package/src/notificationTray/internalApi/getNotificationTraySeen.ts +81 -0
  119. package/src/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsLiveCollectionController.ts +96 -0
  120. package/src/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsPaginationController.ts +31 -0
  121. package/src/notificationTray/observers/getNotificationTrayItems/NotificationTrayItemsQuerystreamController.ts +68 -0
  122. package/src/notificationTray/observers/getNotificationTrayItems.ts +44 -0
  123. package/src/notificationTray/observers/getNotificationTraySeen.ts +43 -0
  124. package/src/notificationTray/observers/index.ts +2 -0
  125. package/src/notificationTray/utils/prepareNotificationTrayItemsPayload.ts +12 -0
  126. package/src/utils/linkedObject/index.ts +2 -0
  127. package/src/utils/linkedObject/notificationTrayLinkedObject.ts +19 -0
  128. package/dist/channelRepository/api/markChannelsAsReadBySegment.d.ts +0 -16
  129. package/dist/channelRepository/api/markChannelsAsReadBySegment.d.ts.map +0 -1
  130. package/dist/channelRepository/events/onChannelUnreadUpdatedLocal.d.ts.map +0 -1
  131. package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts +0 -11
  132. package/dist/channelRepository/internalApi/getTotalChannelsUnread.d.ts.map +0 -1
  133. package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts +0 -20
  134. package/dist/channelRepository/observers/getTotalChannelsUnread.d.ts.map +0 -1
  135. package/dist/channelRepository/utils/getLegacyChannelUnread.d.ts +0 -2
  136. package/dist/channelRepository/utils/getLegacyChannelUnread.d.ts.map +0 -1
  137. package/dist/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngine.d.ts +0 -33
  138. package/dist/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngine.d.ts.map +0 -1
  139. package/dist/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngineOnLoginHandler.d.ts +0 -3
  140. package/dist/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngineOnLoginHandler.d.ts.map +0 -1
  141. package/dist/marker/events/onChannelUnreadInfoUpdatedLocal.d.ts +0 -12
  142. package/dist/marker/events/onChannelUnreadInfoUpdatedLocal.d.ts.map +0 -1
  143. package/src/channelRepository/api/markChannelsAsReadBySegment.ts +0 -29
  144. package/src/channelRepository/internalApi/getTotalChannelsUnread.ts +0 -38
  145. package/src/channelRepository/observers/getTotalChannelsUnread.ts +0 -129
  146. package/src/channelRepository/utils/getLegacyChannelUnread.ts +0 -5
  147. package/src/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngine.ts +0 -267
  148. package/src/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngineOnLoginHandler.ts +0 -21
  149. package/src/marker/events/onChannelUnreadInfoUpdatedLocal.ts +0 -29
@@ -4,12 +4,12 @@ import { createEventSubscriber } from '~/core/events';
4
4
  /**
5
5
  * Internal used only
6
6
  *
7
- * Fired when an {@link Amity.ChannelUnread} has been updated.
7
+ * Fired when an {@link Amity.userMessageFeedMarkers} has been resolved by Object Rsesolver
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 Channel Events
12
+ * @category MessageMarker Events
13
13
  */
14
14
  export const onChannelUnreadUpdatedLocal = (
15
15
  callback: Amity.Listener<Amity.Events['local.channelUnread.updated']>,
@@ -22,7 +22,7 @@ export const onChannelUnreadUpdatedLocal = (
22
22
 
23
23
  return createEventSubscriber(
24
24
  client,
25
- 'channel/onChannelUnreadUpdatedLocal',
25
+ 'channelMarker/onChannelUnreadUpdatedLocal',
26
26
  'local.channelUnread.updated',
27
27
  filter,
28
28
  );
@@ -1,12 +1,11 @@
1
1
  import { getActiveClient } from '~/client/api/activeClient';
2
- import { createEventSubscriber, fireEvent } from '~/core/events';
2
+ import { createEventSubscriber } 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';
10
9
 
11
10
  /**
12
11
  * ```js
@@ -41,49 +40,6 @@ export const onMessageCreatedMqtt = (
41
40
  });
42
41
  }
43
42
 
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.lastMentionedSegment !== '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 lastMentionedSegment = isMentionedInMessage
71
- ? message.segment
72
- : channelUnread.lastMentionedSegment;
73
-
74
- const updatedChannelUnread: Amity.ChannelUnread = {
75
- ...channelUnread,
76
- lastSegment,
77
- unreadCount: Math.max(lastSegment - channelUnread.readToSegment, 0),
78
- lastMentionedSegment,
79
- isMentioned: !(channelUnread.readToSegment >= lastMentionedSegment),
80
- };
81
-
82
- pushToCache(['channelUnread', 'get', message.channelId], updatedChannelUnread);
83
- fireEvent('local.channelUnread.updated', updatedChannelUnread);
84
- });
85
- }
86
-
87
43
  // Update in cache
88
44
  ingestInCache(payload);
89
45
 
@@ -13,6 +13,7 @@ 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';
16
17
 
17
18
  /* begin_public_function
18
19
  id: message.get
@@ -1,15 +1,8 @@
1
- import { getActiveClient } from '~/client/api/activeClient';
2
1
  import ReadReceiptSyncEngine from '~/client/utils/ReadReceiptSync/readReceiptSyncEngine';
3
- import LegacyReadReceiptSyncEngine from '~/client/utils/ReadReceiptSync/legacyReadReceiptSyncEngine';
4
2
 
5
3
  export const markReadMessage = (message: Amity.InternalMessage) => {
6
- const client = getActiveClient();
4
+ const { subChannelId, channelSegment } = message;
5
+ const markReadReceiptEngine = ReadReceiptSyncEngine.getInstance();
7
6
 
8
- if (client.useLegacyUnreadCount) {
9
- const markReadReceiptEngine = ReadReceiptSyncEngine.getInstance();
10
- markReadReceiptEngine.markRead(message.channelId, message.channelSegment);
11
- } else {
12
- const markReadReceiptEngine = LegacyReadReceiptSyncEngine.getInstance();
13
- markReadReceiptEngine.markRead(message.subChannelId, message.channelSegment);
14
- }
7
+ markReadReceiptEngine.markRead(subChannelId, channelSegment);
15
8
  };
@@ -0,0 +1,2 @@
1
+ export * from './markItemsSeen';
2
+ export * from './markTraySeen';
@@ -0,0 +1,59 @@
1
+ import { getActiveClient } from '~/client/api/activeClient';
2
+ import { fireEvent } from '~/core/events';
3
+ import { pullFromCache, pushToCache } from '~/cache/api';
4
+
5
+ /* begin_public_function
6
+ id: notificationTrayItem.markSeen
7
+ */
8
+ /**
9
+ * ```js
10
+ * import { notificationTray } from '@amityco/ts-sdk'
11
+ * const updated = await notificationTray.markItemsSeen()
12
+ * ```
13
+ *
14
+ * Updates an {@link Amity.NotificationItemSeen}
15
+ *
16
+ * @param trayItems[] that include id and lastTraySeenAt, The ID of the {@link Amity.NotificationItemSeen} to edit
17
+ * @returns the updated {@link Amity.NotificationItemSeen} object
18
+ *
19
+ * @category NotificationItemSeen API
20
+ * @async
21
+ */
22
+ export const markItemsSeen = async (patch: Amity.QueryNotificationItemSeen) => {
23
+ const client = getActiveClient();
24
+ client.log('notificationTray/markItemsSeen', {});
25
+
26
+ const { data: payload } = await client.http.put<Amity.RawNotificationTraySeenUpdated>(
27
+ `api/v1/notification-tray/items/seen`,
28
+ {
29
+ patch,
30
+ },
31
+ );
32
+
33
+ const updatedData = patch.trayItems
34
+ .map(patchItem => {
35
+ const cacheData = pullFromCache<Amity.InternalNotificationTrayItem>([
36
+ 'notificationTraySeen',
37
+ 'get',
38
+ patchItem.id,
39
+ ])?.data;
40
+
41
+ if (!cacheData) return;
42
+
43
+ const data = {
44
+ ...cacheData,
45
+ ...payload,
46
+ };
47
+
48
+ if (client.cache) {
49
+ const cachedAt = Date.now();
50
+ pushToCache(['notificationTrayItem', 'get'], data, { cachedAt });
51
+ }
52
+
53
+ return data;
54
+ })
55
+ .filter(Boolean) as Amity.InternalNotificationTrayItem[];
56
+
57
+ fireEvent('local.notificationTrayItem.updated', { notificationTrayItems: updatedData });
58
+ };
59
+ /* end_public_function */
@@ -0,0 +1,65 @@
1
+ import { getActiveClient } from '~/client/api';
2
+ import { fireEvent } from '~/core/events';
3
+ import { pullFromCache, pushToCache } from '~/cache/api';
4
+
5
+ /* begin_public_function
6
+ id: notificationTray.markSeen
7
+ */
8
+ /**
9
+ * ```js
10
+ * import { notificationTray } from '@amityco/ts-sdk'
11
+ * const updated = await notificationTray.markTraySeen({
12
+ * lastSeenAt: Amity.timestamp,
13
+ * })
14
+ * ```
15
+ *
16
+ * Updates an {@link Amity.NotificationTraySeen}
17
+ *
18
+ * @param userId The ID of the {@link Amity.NotificationTraySeen} to edit
19
+ * @param lastSeenAt The patch data to apply
20
+ * @returns the updated {@link Amity.NotificationTraySeen} object
21
+ *
22
+ * @category Post API
23
+ * @async
24
+ */
25
+ export const markTraySeen = async (
26
+ lastSeenAt: Amity.timestamp,
27
+ ): Promise<Amity.Cached<Amity.NotificationTraySeenUpdatedPayload>> => {
28
+ const client = getActiveClient();
29
+ client.log('notificationTray/markTraySeen', {});
30
+
31
+ const { data: payload } = await client.http.put<Amity.NotificationTraySeenUpdatedPayload>(
32
+ `api/v1/notification-tray/tray/seen`,
33
+ {
34
+ lastSeenAt,
35
+ },
36
+ );
37
+
38
+ const cacheData = pullFromCache<Amity.InternalNotificationTraySeen>([
39
+ 'notificationTraySeen',
40
+ 'get',
41
+ ])?.data;
42
+
43
+ const data = {
44
+ userId: client.userId!,
45
+ ...payload,
46
+ } as Amity.InternalNotificationTraySeen;
47
+
48
+ const updateCacheData = {
49
+ ...cacheData,
50
+ ...data,
51
+ };
52
+
53
+ const cachedAt = client.cache && Date.now();
54
+
55
+ if (client.cache)
56
+ pushToCache(['notificationTraySeen', 'get', client.userId!], updateCacheData, { cachedAt });
57
+
58
+ fireEvent('local.notificationTraySeen.updated', data);
59
+
60
+ return {
61
+ data: payload,
62
+ cachedAt,
63
+ };
64
+ };
65
+ /* end_public_function */
@@ -0,0 +1 @@
1
+ export * from './onNotificationTraySeenUpdated';
@@ -0,0 +1,36 @@
1
+ import { getActiveClient } from '~/client/api/activeClient';
2
+ import { createEventSubscriber } from '~/core/events';
3
+
4
+ /**
5
+ * ```js
6
+ * import { onNotificationTraySeenUpdated } from '@amityco/ts-sdk'
7
+ * const dispose = onNotificationTraySeenUpdated(data => {
8
+ * // ...
9
+ * })
10
+ * ```
11
+ *
12
+ * Fired when an {@link Amity.NotificationTraySeen} has been updated
13
+ *
14
+ * @param callback The function to call when the event was fired
15
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
16
+ *
17
+ * @category NotificationTraySeen Events
18
+ */
19
+ export const onNotificationTraySeenUpdated = (
20
+ callback: Amity.Listener<Amity.InternalNotificationTraySeen>,
21
+ ): Amity.Unsubscriber => {
22
+ const client = getActiveClient();
23
+
24
+ const disposers = [
25
+ createEventSubscriber(
26
+ client,
27
+ 'onNotificationTraySeenUpdated',
28
+ 'local.notificationTraySeen.updated',
29
+ payload => callback(payload),
30
+ ),
31
+ ];
32
+
33
+ return () => {
34
+ disposers.forEach(fn => fn());
35
+ };
36
+ };
@@ -0,0 +1,3 @@
1
+ export * from './observers';
2
+ export * from './api';
3
+ export * from './events';
@@ -0,0 +1,81 @@
1
+ import { getActiveClient } from '~/client/api/activeClient';
2
+ import { pullFromCache, pushToCache } from '~/cache/api';
3
+
4
+ /* begin_public_function
5
+ id: notificationTray.getNotificationTraySeen
6
+ */
7
+ /**
8
+ * ```js
9
+ * import { notificationTray } from '@amityco/ts-sdk'
10
+ * const notificationTraySeen = await notificationTray.getNotificationTraySeen()
11
+ * ```
12
+ *
13
+ *
14
+ * @returns A page of {@link Amity.NotificationTraySeen} objects
15
+ *
16
+ * @category NotificationTray API
17
+ * @async
18
+ * */
19
+ export const getNotificationTraySeen = async (): Promise<
20
+ Amity.Cached<Amity.NotificationTraySeen>
21
+ > => {
22
+ const client = getActiveClient();
23
+ client.log('notificationTray/getNotificationTraySeen', {});
24
+
25
+ const { data: payload } = await client.http.get<Amity.NotificationTraySeenPayload>(
26
+ `api/v1/notification-tray/tray/seen`,
27
+ );
28
+
29
+ const cachedAt = client.cache && Date.now();
30
+
31
+ if (client.cache) {
32
+ const cacheKey = ['notificationTraySeen', 'get', client.userId];
33
+ pushToCache(cacheKey, {
34
+ userId: client.userId,
35
+ lastTraySeenAt: payload.lastTraySeenAt,
36
+ lastTrayOccuredAt: payload.lastTrayOccuredAt,
37
+ });
38
+ }
39
+
40
+ return {
41
+ data: {
42
+ userId: client.userId!,
43
+ lastTraySeenAt: payload.lastTraySeenAt,
44
+ lastTrayOccuredAt: payload.lastTrayOccuredAt,
45
+ isSeen: payload.lastTraySeenAt > payload.lastTrayOccuredAt,
46
+ },
47
+ cachedAt,
48
+ };
49
+ };
50
+ /* end_public_function */
51
+
52
+ /**
53
+ * ```js
54
+ * import { notificationTray } from '@amityco/ts-sdk'
55
+ * const notificationTraySeen = await notificationTray.getNotificationTraySeen.locally()
56
+ * ```
57
+ *
58
+ * Queries a paginable list of {@link Amity.NotificationTraySeen} objects from cache
59
+ *
60
+ * @returns A page of {@link Amity.NotificationTraySeen} objects
61
+ *
62
+ * @category NotificationTray API
63
+ * @async
64
+ * */
65
+ getNotificationTraySeen.locally = ():
66
+ | Amity.Cached<Amity.NotificationTraySeenPayload>
67
+ | undefined => {
68
+ const client = getActiveClient();
69
+ client.log('notificationTray/getNotificationTraySeen.locally', {});
70
+
71
+ if (!client.cache) return;
72
+
73
+ const queryKey = ['notificationTraySeen', 'get'];
74
+
75
+ const { data, cachedAt } =
76
+ pullFromCache<{ notificationTraySeen: Amity.NotificationTraySeenPayload }>(queryKey) ?? {};
77
+
78
+ if (!data?.notificationTraySeen) return;
79
+
80
+ return { data: data.notificationTraySeen, cachedAt };
81
+ };
@@ -0,0 +1,96 @@
1
+ import hash from 'object-hash';
2
+ import { pullFromCache, pushToCache } from '~/cache/api';
3
+ import { NotificationTrayItemsPaginationController } from './NotificationTrayItemsPaginationController';
4
+ import { NotificationTrayItemsQuerystreamController } from './NotificationTrayItemsQuerystreamController';
5
+ import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
6
+ import { prepareNotificationTrayItemsPayload } from '~/notificationTray/utils/prepareNotificationTrayItemsPayload';
7
+ import { isNonNullable } from '~/utils';
8
+ import { LinkedObject } from '~/utils/linkedObject';
9
+
10
+ export class NotificationTrayItemsLiveCollectionController extends LiveCollectionController<
11
+ 'notificationTrayItem',
12
+ Amity.NotificationTrayItemLiveCollection,
13
+ Amity.NotificationTrayItem,
14
+ NotificationTrayItemsPaginationController
15
+ > {
16
+ private queryStreamController: NotificationTrayItemsQuerystreamController;
17
+
18
+ private query: Amity.NotificationTrayItemLiveCollection;
19
+
20
+ constructor(
21
+ query: Amity.NotificationTrayItemLiveCollection,
22
+ callback: Amity.LiveCollectionCallback<Amity.NotificationTrayItem>,
23
+ ) {
24
+ const queryStreamId = hash(query);
25
+ const cacheKey = ['notificationTrayItem', 'collection', queryStreamId];
26
+ const paginationController = new NotificationTrayItemsPaginationController(query);
27
+
28
+ super(paginationController, queryStreamId, cacheKey, callback);
29
+
30
+ this.query = query;
31
+ this.queryStreamController = new NotificationTrayItemsQuerystreamController(
32
+ this.query,
33
+ this.cacheKey,
34
+ this.notifyChange.bind(this),
35
+ prepareNotificationTrayItemsPayload,
36
+ );
37
+
38
+ this.callback = callback.bind(this);
39
+ this.loadPage({ initial: true });
40
+ }
41
+
42
+ protected setup() {
43
+ const collection = pullFromCache<Amity.NotificationTrayItemLiveCollectionCache>(
44
+ this.cacheKey,
45
+ )?.data;
46
+ if (!collection) {
47
+ pushToCache(this.cacheKey, {
48
+ data: [],
49
+ params: {},
50
+ });
51
+ }
52
+ }
53
+
54
+ protected async persistModel(queryPayload: Amity.NotificationTrayPayload & Amity.Pagination) {
55
+ await this.queryStreamController.saveToMainDB(queryPayload);
56
+ }
57
+
58
+ protected persistQueryStream({
59
+ response,
60
+ direction,
61
+ refresh,
62
+ }: Amity.LiveCollectionPersistQueryStreamParams<'notificationTrayItem'>) {
63
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
64
+ }
65
+
66
+ // eslint-disable-next-line class-methods-use-this
67
+ startSubscription() {
68
+ return [] as Amity.Unsubscriber[];
69
+ }
70
+
71
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
72
+ const collection = pullFromCache<Amity.NotificationTrayItemLiveCollectionCache>(
73
+ this.cacheKey,
74
+ )?.data;
75
+ if (!collection) return;
76
+
77
+ const data = (
78
+ collection.data
79
+ .map(id =>
80
+ pullFromCache<Amity.InternalNotificationTrayItem>(['notificationTrayItem', 'get', id]),
81
+ )
82
+ .filter(isNonNullable)
83
+ .map(({ data }) => data) ?? []
84
+ ).map(LinkedObject.notificationTray);
85
+
86
+ if (!this.shouldNotify(data) && origin === 'event') return;
87
+
88
+ this.callback({
89
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
90
+ data,
91
+ hasNextPage: !!this.paginationController.getNextToken(),
92
+ loading,
93
+ error,
94
+ });
95
+ }
96
+ }
@@ -0,0 +1,31 @@
1
+ import { PaginationController } from '~/core/liveCollection/PaginationController';
2
+ import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';
3
+
4
+ /**
5
+ * TODO: handle cache receive cache option, and cache policy
6
+ * TODO: check if querybyIds is supported
7
+ */
8
+ export class NotificationTrayItemsPaginationController extends PaginationController<
9
+ 'notificationTrayItem',
10
+ Amity.NotificationTrayItemLiveCollection
11
+ > {
12
+ async getRequest(
13
+ queryParams: Amity.NotificationTrayItemLiveCollection,
14
+ token: string | undefined,
15
+ ) {
16
+ const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, ...params } = queryParams;
17
+
18
+ const options = token ? { token } : { limit };
19
+
20
+ const { data: queryResponse } = await this.http.get<
21
+ Amity.NotificationTrayPayload & Amity.Pagination
22
+ >(`/api/v1/notification-tray`, {
23
+ params: {
24
+ ...params,
25
+ options,
26
+ },
27
+ });
28
+
29
+ return queryResponse;
30
+ }
31
+ }
@@ -0,0 +1,68 @@
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
+
7
+ export class NotificationTrayItemsQuerystreamController extends QueryStreamController<
8
+ Amity.NotificationTrayPayload,
9
+ Amity.NotificationTrayItemLiveCollection
10
+ > {
11
+ private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
12
+
13
+ private preparePayload: (
14
+ response: Amity.NotificationTrayPayload,
15
+ ) => Amity.ProcessedNotificationTrayPayload;
16
+
17
+ constructor(
18
+ query: Amity.NotificationTrayItemLiveCollection,
19
+ cacheKey: string[],
20
+ notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
21
+ preparePayload: (
22
+ response: Amity.NotificationTrayPayload,
23
+ ) => Amity.ProcessedNotificationTrayPayload,
24
+ ) {
25
+ super(query, cacheKey);
26
+ this.notifyChange = notifyChange;
27
+ this.preparePayload = preparePayload;
28
+ }
29
+
30
+ async saveToMainDB(response: Amity.NotificationTrayPayload) {
31
+ const processedPayload = await this.preparePayload(response);
32
+
33
+ const client = getActiveClient();
34
+ const cachedAt = client.cache && Date.now();
35
+
36
+ if (client.cache) {
37
+ ingestInCache(processedPayload, { cachedAt });
38
+ }
39
+ }
40
+
41
+ appendToQueryStream(
42
+ response: Amity.NotificationTrayPayload & Partial<Amity.Pagination>,
43
+ direction: Amity.LiveCollectionPageDirection,
44
+ refresh = false,
45
+ ) {
46
+ if (refresh) {
47
+ pushToCache(this.cacheKey, {
48
+ data: response.notificationTrayItems.map(getResolver('notificationTrayItem')),
49
+ });
50
+ } else {
51
+ const collection = pullFromCache<Amity.NotificationTrayItemLiveCollectionCache>(
52
+ this.cacheKey,
53
+ )?.data;
54
+
55
+ const notifications = collection?.data ?? [];
56
+
57
+ pushToCache(this.cacheKey, {
58
+ ...collection,
59
+ data: [
60
+ ...new Set([
61
+ ...notifications,
62
+ ...response.notificationTrayItems.map(getResolver('notificationTrayItem')),
63
+ ]),
64
+ ],
65
+ });
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,44 @@
1
+ import { getActiveClient } from '~/client/api';
2
+ import { dropFromCache } from '~/cache/api';
3
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
4
+ import { NotificationTrayItemsLiveCollectionController } from './getNotificationTrayItems/NotificationTrayItemsLiveCollectionController';
5
+
6
+ /**
7
+ * Get notification tray items for a notification tray page
8
+ *
9
+ * @param params the limit query parameters
10
+ * @param callback the callback to be called when the notification tray items are updated
11
+ * @returns items in the notification tray
12
+ *
13
+ * @category Notification tray items Live Collection
14
+ *
15
+ */
16
+ export const getNotificationTrayItems = (
17
+ params: Amity.LiveCollectionParams<Amity.NotificationTrayItemLiveCollection>,
18
+ callback: Amity.LiveCollectionCallback<Amity.NotificationTrayItem>,
19
+ config?: Amity.LiveCollectionConfig,
20
+ ) => {
21
+ const { log, cache } = getActiveClient();
22
+
23
+ if (!cache) {
24
+ console.log(ENABLE_CACHE_MESSAGE);
25
+ }
26
+
27
+ const timestamp = Date.now();
28
+ log(`getNotificationTrayItems(tmpid: ${timestamp}) > listen`);
29
+
30
+ const notiTrayItemsLiveCollection = new NotificationTrayItemsLiveCollectionController(
31
+ params,
32
+ callback,
33
+ );
34
+ const disposers = notiTrayItemsLiveCollection.startSubscription();
35
+
36
+ const cacheKey = notiTrayItemsLiveCollection.getCacheKey();
37
+
38
+ disposers.push(() => dropFromCache(cacheKey));
39
+
40
+ return () => {
41
+ log(`getNotificationTrayItems(tmpid: ${timestamp}) > dispose`);
42
+ disposers.forEach(fn => fn());
43
+ };
44
+ };
@@ -0,0 +1,43 @@
1
+ import { liveObject } from '~/utils/liveObject';
2
+ import { getNotificationTraySeen as _getNotificationTraySeen } from '../internalApi/getNotificationTraySeen';
3
+ import { onNotificationTraySeenUpdated } from '../events/onNotificationTraySeenUpdated';
4
+ import { getActiveUser } from '~/client';
5
+
6
+ /* begin_public_function
7
+ id: notificationTraySeen.get
8
+ */
9
+ /**
10
+ * ```js
11
+ * import { getNotificationTraySeen } from '@amityco/ts-sdk';
12
+ *
13
+ * let notificationTraySeen;
14
+ *
15
+ * const unsubscribe = getNotificationTraySeen(userId, response => {
16
+ * notificationTraySeen = response.data;
17
+ * });
18
+ * ```
19
+ *
20
+ * Observe all mutation on a given {@link Amity.NotificationTraySeen}
21
+ *
22
+ * @param userId the ID of the user to observe
23
+ * @param callback the function to call when new data are available
24
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the message
25
+ *
26
+ * @category NotificationTraySeen Live Object
27
+ */
28
+ export const getNotificationTraySeen = (
29
+ callback: Amity.LiveObjectCallback<Amity.NotificationTraySeenPayload>,
30
+ ): Amity.Unsubscriber => {
31
+ const responder: Amity.LiveObjectCallback<Amity.InternalNotificationTraySeen> = (
32
+ snapshot: Amity.LiveObject<Amity.InternalNotificationTraySeen>,
33
+ ) => {
34
+ const { data } = snapshot;
35
+ callback({ ...snapshot, data });
36
+ };
37
+
38
+ const { userId } = getActiveUser();
39
+ return liveObject(userId, responder, 'userId', _getNotificationTraySeen, [
40
+ onNotificationTraySeenUpdated,
41
+ ]);
42
+ };
43
+ /* end_public_function */
@@ -0,0 +1,2 @@
1
+ export * from './getNotificationTraySeen';
2
+ export * from './getNotificationTrayItems';
@@ -0,0 +1,12 @@
1
+ import { convertRawUserToInternalUser } from '~/userRepository/utils/convertRawUserToInternalUser';
2
+
3
+ export const prepareNotificationTrayItemsPayload = (
4
+ rawPayload: Amity.NotificationTrayPayload,
5
+ ): Amity.ProcessedNotificationTrayPayload => {
6
+ const users = rawPayload.users.map(convertRawUserToInternalUser);
7
+
8
+ return {
9
+ ...rawPayload,
10
+ users,
11
+ };
12
+ };