@amityco/ts-sdk 7.1.0 → 7.1.1-15835b61.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 (74) hide show
  1. package/.env +26 -26
  2. package/dist/@types/core/events.d.ts +4 -0
  3. package/dist/@types/core/events.d.ts.map +1 -1
  4. package/dist/@types/core/model.d.ts +4 -0
  5. package/dist/@types/core/model.d.ts.map +1 -1
  6. package/dist/@types/core/payload.d.ts +18 -0
  7. package/dist/@types/core/payload.d.ts.map +1 -1
  8. package/dist/@types/domains/notification.d.ts +74 -0
  9. package/dist/@types/domains/notification.d.ts.map +1 -0
  10. package/dist/core/events.d.ts +3 -3
  11. package/dist/core/events.d.ts.map +1 -1
  12. package/dist/core/model/idResolvers.d.ts.map +1 -1
  13. package/dist/core/model/index.d.ts.map +1 -1
  14. package/dist/index.cjs.js +415 -45
  15. package/dist/index.d.ts +1 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.esm.js +398 -29
  18. package/dist/index.umd.js +4 -4
  19. package/dist/notificationTrayRepository/api/index.d.ts +3 -0
  20. package/dist/notificationTrayRepository/api/index.d.ts.map +1 -0
  21. package/dist/notificationTrayRepository/api/markNotificationItemsSeen.d.ts +16 -0
  22. package/dist/notificationTrayRepository/api/markNotificationItemsSeen.d.ts.map +1 -0
  23. package/dist/notificationTrayRepository/api/markNotificationTraySeen.d.ts +19 -0
  24. package/dist/notificationTrayRepository/api/markNotificationTraySeen.d.ts.map +1 -0
  25. package/dist/notificationTrayRepository/events/index.d.ts +2 -0
  26. package/dist/notificationTrayRepository/events/index.d.ts.map +1 -0
  27. package/dist/notificationTrayRepository/events/onNotificationTraySeenUpdated.d.ts +17 -0
  28. package/dist/notificationTrayRepository/events/onNotificationTraySeenUpdated.d.ts.map +1 -0
  29. package/dist/notificationTrayRepository/index.d.ts +4 -0
  30. package/dist/notificationTrayRepository/index.d.ts.map +1 -0
  31. package/dist/notificationTrayRepository/internalApi/getNotificationTraySeen.d.ts +30 -0
  32. package/dist/notificationTrayRepository/internalApi/getNotificationTraySeen.d.ts.map +1 -0
  33. package/dist/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsLiveCollectionController.d.ts +13 -0
  34. package/dist/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsLiveCollectionController.d.ts.map +1 -0
  35. package/dist/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsPaginationController.d.ts +9 -0
  36. package/dist/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsPaginationController.d.ts.map +1 -0
  37. package/dist/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsQuerystreamController.d.ts +9 -0
  38. package/dist/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsQuerystreamController.d.ts.map +1 -0
  39. package/dist/notificationTrayRepository/observers/getNotificationTrayItems.d.ts +12 -0
  40. package/dist/notificationTrayRepository/observers/getNotificationTrayItems.d.ts.map +1 -0
  41. package/dist/notificationTrayRepository/observers/getNotificationTraySeen.d.ts +21 -0
  42. package/dist/notificationTrayRepository/observers/getNotificationTraySeen.d.ts.map +1 -0
  43. package/dist/notificationTrayRepository/observers/index.d.ts +3 -0
  44. package/dist/notificationTrayRepository/observers/index.d.ts.map +1 -0
  45. package/dist/notificationTrayRepository/utils/prepareNotificationTrayItemsPayload.d.ts +2 -0
  46. package/dist/notificationTrayRepository/utils/prepareNotificationTrayItemsPayload.d.ts.map +1 -0
  47. package/dist/utils/linkedObject/index.d.ts +1 -0
  48. package/dist/utils/linkedObject/index.d.ts.map +1 -1
  49. package/dist/utils/linkedObject/notificationTrayLinkedObject.d.ts +2 -0
  50. package/dist/utils/linkedObject/notificationTrayLinkedObject.d.ts.map +1 -0
  51. package/package.json +1 -1
  52. package/src/@types/core/events.ts +5 -0
  53. package/src/@types/core/model.ts +6 -0
  54. package/src/@types/core/payload.ts +25 -0
  55. package/src/@types/domains/notification.ts +83 -0
  56. package/src/core/model/idResolvers.ts +3 -0
  57. package/src/core/model/index.ts +2 -0
  58. package/src/index.ts +2 -0
  59. package/src/notificationTrayRepository/api/index.ts +2 -0
  60. package/src/notificationTrayRepository/api/markNotificationItemsSeen.ts +59 -0
  61. package/src/notificationTrayRepository/api/markNotificationTraySeen.ts +65 -0
  62. package/src/notificationTrayRepository/events/index.ts +1 -0
  63. package/src/notificationTrayRepository/events/onNotificationTraySeenUpdated.ts +36 -0
  64. package/src/notificationTrayRepository/index.ts +3 -0
  65. package/src/notificationTrayRepository/internalApi/getNotificationTraySeen.ts +80 -0
  66. package/src/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsLiveCollectionController.ts +94 -0
  67. package/src/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsPaginationController.ts +31 -0
  68. package/src/notificationTrayRepository/observers/getNotificationTrayItems/NotificationTrayItemsQuerystreamController.ts +68 -0
  69. package/src/notificationTrayRepository/observers/getNotificationTrayItems.ts +44 -0
  70. package/src/notificationTrayRepository/observers/getNotificationTraySeen.ts +43 -0
  71. package/src/notificationTrayRepository/observers/index.ts +2 -0
  72. package/src/notificationTrayRepository/utils/prepareNotificationTrayItemsPayload.ts +12 -0
  73. package/src/utils/linkedObject/index.ts +2 -0
  74. package/src/utils/linkedObject/notificationTrayLinkedObject.ts +18 -0
@@ -0,0 +1,80 @@
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 { NotificationTrayRepository } from '@amityco/ts-sdk'
10
+ * const notificationTraySeen = await NotificationTrayRepository.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.InternalNotificationTraySeen>
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
+ },
46
+ cachedAt,
47
+ };
48
+ };
49
+ /* end_public_function */
50
+
51
+ /**
52
+ * ```js
53
+ * import { NotificationTrayRepository } from '@amityco/ts-sdk'
54
+ * const notificationTraySeen = await NotificationTrayRepository.getNotificationTraySeen.locally()
55
+ * ```
56
+ *
57
+ * Queries a paginable list of {@link Amity.NotificationTraySeen} objects from cache
58
+ *
59
+ * @returns A page of {@link Amity.NotificationTraySeen} objects
60
+ *
61
+ * @category NotificationTray API
62
+ * @async
63
+ * */
64
+ getNotificationTraySeen.locally = ():
65
+ | Amity.Cached<Amity.NotificationTraySeenPayload>
66
+ | undefined => {
67
+ const client = getActiveClient();
68
+ client.log('notificationTray/getNotificationTraySeen.locally', {});
69
+
70
+ if (!client.cache) return;
71
+
72
+ const queryKey = ['notificationTraySeen', 'get'];
73
+
74
+ const { data, cachedAt } =
75
+ pullFromCache<{ notificationTraySeen: Amity.NotificationTraySeenPayload }>(queryKey) ?? {};
76
+
77
+ if (!data?.notificationTraySeen) return;
78
+
79
+ return { data: data.notificationTraySeen, cachedAt };
80
+ };
@@ -0,0 +1,94 @@
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 '~/notificationTrayRepository/utils/prepareNotificationTrayItemsPayload';
7
+ import { isNonNullable } from '~/utils';
8
+
9
+ export class NotificationTrayItemsLiveCollectionController extends LiveCollectionController<
10
+ 'notificationTrayItem',
11
+ Amity.NotificationTrayItemLiveCollection,
12
+ Amity.RawNotificationTrayItem,
13
+ NotificationTrayItemsPaginationController
14
+ > {
15
+ private queryStreamController: NotificationTrayItemsQuerystreamController;
16
+
17
+ private query: Amity.NotificationTrayItemLiveCollection;
18
+
19
+ constructor(
20
+ query: Amity.NotificationTrayItemLiveCollection,
21
+ callback: Amity.LiveCollectionCallback<Amity.RawNotificationTrayItem>,
22
+ ) {
23
+ const queryStreamId = hash(query);
24
+ const cacheKey = ['notificationTrayItem', 'collection', queryStreamId];
25
+ const paginationController = new NotificationTrayItemsPaginationController(query);
26
+
27
+ super(paginationController, queryStreamId, cacheKey, callback);
28
+
29
+ this.query = query;
30
+ this.queryStreamController = new NotificationTrayItemsQuerystreamController(
31
+ this.query,
32
+ this.cacheKey,
33
+ this.notifyChange.bind(this),
34
+ prepareNotificationTrayItemsPayload,
35
+ );
36
+
37
+ this.callback = callback.bind(this);
38
+ this.loadPage({ initial: true });
39
+ }
40
+
41
+ protected setup() {
42
+ const collection = pullFromCache<Amity.NotificationTrayItemLiveCollectionCache>(
43
+ this.cacheKey,
44
+ )?.data;
45
+ if (!collection) {
46
+ pushToCache(this.cacheKey, {
47
+ data: [],
48
+ params: {},
49
+ });
50
+ }
51
+ }
52
+
53
+ protected async persistModel(queryPayload: Amity.NotificationTrayPayload & Amity.Pagination) {
54
+ await this.queryStreamController.saveToMainDB(queryPayload);
55
+ }
56
+
57
+ protected persistQueryStream({
58
+ response,
59
+ direction,
60
+ refresh,
61
+ }: Amity.LiveCollectionPersistQueryStreamParams<'notificationTrayItem'>) {
62
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
63
+ }
64
+
65
+ // eslint-disable-next-line class-methods-use-this
66
+ startSubscription() {
67
+ return [] as Amity.Unsubscriber[];
68
+ }
69
+
70
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
71
+ const collection = pullFromCache<Amity.NotificationTrayItemLiveCollectionCache>(
72
+ this.cacheKey,
73
+ )?.data;
74
+ if (!collection) return;
75
+
76
+ const data =
77
+ collection.data
78
+ .map(id =>
79
+ pullFromCache<Amity.InternalNotificationTrayItem>(['notificationTrayItem', 'get', id]),
80
+ )
81
+ .filter(isNonNullable)
82
+ .map(({ data }) => data) ?? [];
83
+
84
+ if (!this.shouldNotify(data) && origin === 'event') return;
85
+
86
+ this.callback({
87
+ onNextPage: () => this.loadPage({ direction: Amity.LiveCollectionPageDirection.NEXT }),
88
+ data,
89
+ hasNextPage: !!this.paginationController.getNextToken(),
90
+ loading,
91
+ error,
92
+ });
93
+ }
94
+ }
@@ -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.RawNotificationTrayItem>,
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
+ };
@@ -10,6 +10,7 @@ import { reactorLinkedObject } from './reactorLinkedObject';
10
10
  import { channelLinkedObject } from './channelLinkedObject';
11
11
  import { adLinkedObject } from './adLinkedObject';
12
12
  import { pinnedPostLinkedObject } from './pinnedPostLinkedObject';
13
+ import { notificationTrayLinkedObject } from './notificationTrayLinkedObject';
13
14
 
14
15
  export const LinkedObject = {
15
16
  ad: adLinkedObject,
@@ -24,4 +25,5 @@ export const LinkedObject = {
24
25
  reactor: reactorLinkedObject,
25
26
  channel: channelLinkedObject,
26
27
  pinnedPost: pinnedPostLinkedObject,
28
+ notificationTray: notificationTrayLinkedObject,
27
29
  };
@@ -0,0 +1,18 @@
1
+ import { markNotificationItemsSeen } from '~/notificationTrayRepository/api/markNotificationItemsSeen';
2
+
3
+ export const notificationTrayLinkedObject = (
4
+ noti: Amity.InternalNotificationTrayItem,
5
+ ): Amity.NotificationTrayItem => {
6
+ return {
7
+ ...noti,
8
+ markRead: () =>
9
+ markNotificationItemsSeen({
10
+ trayItems: [
11
+ {
12
+ id: noti._id,
13
+ lastSeenAt: noti.lastSeenAt,
14
+ },
15
+ ],
16
+ }),
17
+ };
18
+ };