@metamask-previews/notification-services-controller 0.0.0-preview-7ff4bd7f → 0.0.0-preview-bc4ac8fa
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/NotificationServicesController/NotificationServicesController.js +3 -3
- package/dist/NotificationServicesController/NotificationServicesController.mjs +3 -3
- package/dist/NotificationServicesController/index.js +4 -4
- package/dist/NotificationServicesController/index.mjs +4 -4
- package/dist/NotificationServicesController/services/onchain-notifications.js +2 -2
- package/dist/NotificationServicesController/services/onchain-notifications.mjs +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.js +5 -5
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +5 -5
- package/dist/NotificationServicesPushController/index.js +6 -6
- package/dist/NotificationServicesPushController/index.mjs +6 -6
- package/dist/NotificationServicesPushController/services/push/push-web.js +3 -3
- package/dist/NotificationServicesPushController/services/push/push-web.mjs +3 -3
- package/dist/NotificationServicesPushController/services/services.js +3 -3
- package/dist/NotificationServicesPushController/services/services.mjs +3 -3
- package/dist/NotificationServicesPushController/utils/get-notification-message.js +3 -3
- package/dist/NotificationServicesPushController/utils/get-notification-message.mjs +3 -3
- package/dist/NotificationServicesPushController/utils/index.js +3 -3
- package/dist/NotificationServicesPushController/utils/index.mjs +3 -3
- package/dist/{chunk-RNE2I5IN.mjs → chunk-3F4M3OUO.mjs} +4 -4
- package/dist/chunk-3F4M3OUO.mjs.map +1 -0
- package/dist/{chunk-UZXBH6WS.mjs → chunk-5OYPOYXZ.mjs} +2 -2
- package/dist/chunk-5OYPOYXZ.mjs.map +1 -0
- package/dist/{chunk-DKQBDI4C.js → chunk-5WWCWQCE.js} +1 -1
- package/dist/chunk-5WWCWQCE.js.map +1 -0
- package/dist/{chunk-EG5NZ5DA.js → chunk-6B5FOWRH.js} +5 -5
- package/dist/chunk-6B5FOWRH.js.map +1 -0
- package/dist/{chunk-3O5NGZWB.js → chunk-DNTK4U5M.js} +892 -2266
- package/dist/chunk-DNTK4U5M.js.map +1 -0
- package/dist/{chunk-IG3CMJBW.mjs → chunk-KTQP5UDT.mjs} +898 -2272
- package/dist/chunk-KTQP5UDT.mjs.map +1 -0
- package/dist/{chunk-ODZ6V4PH.mjs → chunk-OUPWMPLU.mjs} +1 -1
- package/dist/chunk-OUPWMPLU.mjs.map +1 -0
- package/dist/chunk-UIRZIKC3.mjs +187 -0
- package/dist/chunk-UIRZIKC3.mjs.map +1 -0
- package/dist/chunk-VOZ7Y2OO.js +187 -0
- package/dist/chunk-VOZ7Y2OO.js.map +1 -0
- package/dist/{chunk-3NF7QYJN.js → chunk-XNBTOK6G.js} +7 -7
- package/dist/chunk-XNBTOK6G.js.map +1 -0
- package/dist/index.js +6 -6
- package/dist/index.mjs +5 -5
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/NotificationServicesController/NotificationServicesController.d.ts +1 -1
- package/dist/types/NotificationServicesController/NotificationServicesController.d.ts.map +1 -1
- package/dist/types/NotificationServicesController/index.d.ts +0 -2
- package/dist/types/NotificationServicesController/index.d.ts.map +1 -1
- package/dist/types/NotificationServicesPushController/NotificationServicesPushController.d.ts +1 -1
- package/dist/types/NotificationServicesPushController/NotificationServicesPushController.d.ts.map +1 -1
- package/dist/types/NotificationServicesPushController/index.d.ts +0 -2
- package/dist/types/NotificationServicesPushController/index.d.ts.map +1 -1
- package/package.json +4 -2
- package/dist/chunk-3NF7QYJN.js.map +0 -1
- package/dist/chunk-3O5NGZWB.js.map +0 -1
- package/dist/chunk-DKQBDI4C.js.map +0 -1
- package/dist/chunk-EG5NZ5DA.js.map +0 -1
- package/dist/chunk-IG3CMJBW.mjs.map +0 -1
- package/dist/chunk-KPO4QJHS.js +0 -965
- package/dist/chunk-KPO4QJHS.js.map +0 -1
- package/dist/chunk-ODZ6V4PH.mjs.map +0 -1
- package/dist/chunk-RNE2I5IN.mjs.map +0 -1
- package/dist/chunk-UZXBH6WS.mjs.map +0 -1
- package/dist/chunk-ZJRT7ACU.mjs +0 -965
- package/dist/chunk-ZJRT7ACU.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"sourcesContent":["import type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport type { Types } from '../NotificationServicesController';\nimport { createRegToken, deleteRegToken } from './services/push/push-web';\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n listenToPushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n fcmToken: string;\n};\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\n\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\n\nexport type Actions =\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | ControllerGetStateAction<'state', NotificationServicesPushControllerState>;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClicked = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type AllowedEvents =\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClicked;\n\nexport type NotificationServicesPushControllerMessenger =\n RestrictedControllerMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n >;\n\nconst metadata = {\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n};\n\ntype ControllerConfig = {\n /**\n * Config to turn on/off push notifications.\n * This is currently linked to MV3 builds on extension.\n */\n isPushEnabled: boolean;\n\n /**\n * Must handle when a push notification is received.\n * You must call `registration.showNotification` or equivalent to show the notification on web/mobile\n */\n onPushNotificationReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n\n /**\n * Must handle when a push notification is clicked.\n * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.\n */\n onPushNotificationClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n};\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n *\n * @augments {BaseController<typeof controllerName, NotificationServicesPushControllerState, NotificationServicesPushControllerMessenger>}\n */\nexport class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n #env: PushNotificationEnv;\n\n #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n env: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: {\n fcmToken: state?.fcmToken || '',\n },\n });\n\n this.#env = env;\n this.#config = config;\n\n this.#registerMessageHandlers();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param UUIDs - An array of UUIDs to enable push notifications for.\n */\n async enablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n\n try {\n // Activate Push Notifications\n const regToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken,\n platform: this.#config.platform,\n });\n\n if (!regToken) {\n return;\n }\n\n this.#pushListenerUnsubscribe = await listenToPushNotifications({\n env: this.#env,\n listenToPushReceived: async (n) => {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:onNewNotifications',\n n,\n );\n await this.#config.onPushNotificationReceived(n);\n },\n listenToPushClicked: (e, n) => {\n if (n) {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n n,\n );\n }\n\n this.#config.onPushNotificationClicked(e);\n },\n });\n\n // Update state\n this.update((state) => {\n state.fcmToken = regToken;\n });\n } catch (error) {\n log.error('Failed to enable push notifications:', error);\n throw new Error('Failed to enable push notifications');\n }\n }\n\n /**\n * Disables push notifications for the application.\n * This method handles the process of disabling push notifications by:\n * 1. Unregistering the service worker to stop listening for messages.\n * 2. Sending a request to the server to unregister the device using the FCM token.\n * 3. Removing the FCM token from the state to complete the process.\n *\n * @param UUIDs - An array of UUIDs for which push notifications should be disabled.\n */\n async disablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n let isPushNotificationsDisabled: boolean;\n\n try {\n // Send a request to the server to unregister the token/device\n isPushNotificationsDisabled = await deactivatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n\n // Remove the FCM token from the state\n if (!isPushNotificationsDisabled) {\n return;\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n if (isPushNotificationsDisabled) {\n this.update((state) => {\n state.fcmToken = '';\n });\n }\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n\n try {\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken,\n deleteRegToken,\n platform: this.#config.platform,\n regToken: this.state.fcmToken,\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.update((state) => {\n state.fcmToken = fcmToken;\n });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push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AAAA,QACV,KAAK,mBAAK;AAAA,QACV;AAAA,QACA;AAAA,QACA,UAAU,mBAAK,SAAQ;AAAA,QACvB,UAAU,KAAK,MAAM;AAAA,MACvB,CAAC;AAGD,UAAI,UAAU;AACZ,aAAK,OAAO,CAAC,UAAU;AACrB,gBAAM,WAAW;AAAA,QACnB,CAAC;AAAA,MACH;AAAA,IACF,SAAS,OAAO;AACd,YAAM,eAAe,qDACnB,KACF;AACA,UAAI,MAAM,YAAY;AACtB,YAAM,IAAI,MAAM,YAAY;AAAA,IAC9B;AAAA,EACF;AACF;AApNE;AAEA;AAEA;AA4BA;AAAA,6BAAwB,WAAS;AAC/B,OAAK,gBAAgB;AAAA,IACnB;AAAA,IACA,KAAK,wBAAwB,KAAK,IAAI;AAAA,EACxC;AACA,OAAK,gBAAgB;AAAA,IACnB;AAAA,IACA,KAAK,yBAAyB,KAAK,IAAI;AAAA,EACzC;AACA,OAAK,gBAAgB;AAAA,IACnB;AAAA,IACA,KAAK,+BAA+B,KAAK,IAAI;AAAA,EAC/C;AACF;AAEM;AAAA,6BAAwB,iBAAG;AAC/B,QAAM,cAAc,MAAM,KAAK,gBAAgB;AAAA,IAC7C;AAAA,EACF;AACA,MAAI,CAAC,aAAa;AAChB,QAAI;AAAA,MACF;AAAA,IACF;AACA,UAAM,IAAI,MAAM,8BAA8B;AAAA,EAChD;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import {
|
|
2
|
+
makeApiCall,
|
|
3
|
+
toggleUserStorageTriggerStatus,
|
|
4
|
+
traverseUserStorageTriggers
|
|
5
|
+
} from "./chunk-SMKJEA45.mjs";
|
|
6
|
+
|
|
7
|
+
// src/NotificationServicesController/services/onchain-notifications.ts
|
|
8
|
+
import { UserStorageController } from "@metamask/profile-sync-controller";
|
|
9
|
+
import log from "loglevel";
|
|
10
|
+
var TRIGGER_API = "https://trigger.api.cx.metamask.io";
|
|
11
|
+
var NOTIFICATION_API = "https://notification.api.cx.metamask.io";
|
|
12
|
+
var TRIGGER_API_BATCH_ENDPOINT = `${TRIGGER_API}/api/v1/triggers/batch`;
|
|
13
|
+
var NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications`;
|
|
14
|
+
var NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY = (page) => `${NOTIFICATION_API_LIST_ENDPOINT}?page=${page}&per_page=100`;
|
|
15
|
+
var NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications/mark-as-read`;
|
|
16
|
+
async function createOnChainTriggers(userStorage, storageKey, bearerToken, triggers) {
|
|
17
|
+
const triggersToCreate = triggers.map((t) => ({
|
|
18
|
+
id: t.id,
|
|
19
|
+
token: UserStorageController.createSHA256Hash(t.id + storageKey),
|
|
20
|
+
config: {
|
|
21
|
+
kind: t.kind,
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
23
|
+
chain_id: Number(t.chainId),
|
|
24
|
+
address: t.address
|
|
25
|
+
}
|
|
26
|
+
}));
|
|
27
|
+
if (triggersToCreate.length === 0) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const response = await makeApiCall(
|
|
31
|
+
bearerToken,
|
|
32
|
+
TRIGGER_API_BATCH_ENDPOINT,
|
|
33
|
+
"POST",
|
|
34
|
+
triggersToCreate
|
|
35
|
+
);
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
const errorData = await response.json().catch(() => void 0);
|
|
38
|
+
log.error("Error creating triggers:", errorData);
|
|
39
|
+
throw new Error("OnChain Notifications - unable to create triggers");
|
|
40
|
+
}
|
|
41
|
+
for (const trigger of triggersToCreate) {
|
|
42
|
+
toggleUserStorageTriggerStatus(
|
|
43
|
+
userStorage,
|
|
44
|
+
trigger.config.address,
|
|
45
|
+
String(trigger.config.chain_id),
|
|
46
|
+
trigger.id,
|
|
47
|
+
true
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function deleteOnChainTriggers(userStorage, storageKey, bearerToken, uuids) {
|
|
52
|
+
const triggersToDelete = uuids.map((uuid) => ({
|
|
53
|
+
id: uuid,
|
|
54
|
+
token: UserStorageController.createSHA256Hash(uuid + storageKey)
|
|
55
|
+
}));
|
|
56
|
+
try {
|
|
57
|
+
const response = await makeApiCall(
|
|
58
|
+
bearerToken,
|
|
59
|
+
TRIGGER_API_BATCH_ENDPOINT,
|
|
60
|
+
"DELETE",
|
|
61
|
+
triggersToDelete
|
|
62
|
+
);
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Failed to delete on-chain notifications for uuids ${uuids.join(", ")}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
for (const uuid of uuids) {
|
|
69
|
+
for (const address in userStorage) {
|
|
70
|
+
if (address in userStorage) {
|
|
71
|
+
for (const chainId in userStorage[address]) {
|
|
72
|
+
if (userStorage?.[address]?.[chainId]?.[uuid]) {
|
|
73
|
+
delete userStorage[address][chainId][uuid];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const isEmpty = (obj = {}) => Object.keys(obj).length === 0;
|
|
80
|
+
for (const address in userStorage) {
|
|
81
|
+
if (address in userStorage) {
|
|
82
|
+
for (const chainId in userStorage[address]) {
|
|
83
|
+
if (isEmpty(userStorage?.[address]?.[chainId])) {
|
|
84
|
+
delete userStorage[address][chainId];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (isEmpty(userStorage?.[address])) {
|
|
88
|
+
delete userStorage[address];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} catch (err) {
|
|
93
|
+
log.error(
|
|
94
|
+
`Error deleting on-chain notifications for uuids ${uuids.join(", ")}:`,
|
|
95
|
+
err
|
|
96
|
+
);
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
return userStorage;
|
|
100
|
+
}
|
|
101
|
+
async function getOnChainNotifications(userStorage, bearerToken) {
|
|
102
|
+
const triggerIds = traverseUserStorageTriggers(userStorage, {
|
|
103
|
+
mapTrigger: (t) => {
|
|
104
|
+
if (!t.enabled) {
|
|
105
|
+
return void 0;
|
|
106
|
+
}
|
|
107
|
+
return t.id;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
if (triggerIds.length === 0) {
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
const onChainNotifications = [];
|
|
114
|
+
const PAGE_LIMIT = 2;
|
|
115
|
+
for (let page = 1; page <= PAGE_LIMIT; page++) {
|
|
116
|
+
try {
|
|
117
|
+
const response = await makeApiCall(
|
|
118
|
+
bearerToken,
|
|
119
|
+
NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY(page),
|
|
120
|
+
"POST",
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
122
|
+
{ trigger_ids: triggerIds }
|
|
123
|
+
);
|
|
124
|
+
const notifications = await response.json();
|
|
125
|
+
const transformedNotifications = notifications.map(
|
|
126
|
+
(n) => {
|
|
127
|
+
if (!n.data?.kind) {
|
|
128
|
+
return void 0;
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
...n,
|
|
132
|
+
type: n.data.kind
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
).filter((n) => Boolean(n));
|
|
136
|
+
onChainNotifications.push(...transformedNotifications);
|
|
137
|
+
if (notifications.length < 100) {
|
|
138
|
+
page = PAGE_LIMIT + 1;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
} catch (err) {
|
|
142
|
+
log.error(
|
|
143
|
+
`Error fetching on-chain notifications for trigger IDs ${triggerIds.join(
|
|
144
|
+
", "
|
|
145
|
+
)}:`,
|
|
146
|
+
err
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return onChainNotifications;
|
|
151
|
+
}
|
|
152
|
+
async function markNotificationsAsRead(bearerToken, notificationIds) {
|
|
153
|
+
if (notificationIds.length === 0) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
const response = await makeApiCall(
|
|
158
|
+
bearerToken,
|
|
159
|
+
NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,
|
|
160
|
+
"POST",
|
|
161
|
+
{ ids: notificationIds }
|
|
162
|
+
);
|
|
163
|
+
if (response.status !== 200) {
|
|
164
|
+
const errorData = await response.json().catch(() => void 0);
|
|
165
|
+
throw new Error(
|
|
166
|
+
`Error marking notifications as read: ${errorData?.message}`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
} catch (err) {
|
|
170
|
+
log.error("Error marking notifications as read:", err);
|
|
171
|
+
throw err;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export {
|
|
176
|
+
TRIGGER_API,
|
|
177
|
+
NOTIFICATION_API,
|
|
178
|
+
TRIGGER_API_BATCH_ENDPOINT,
|
|
179
|
+
NOTIFICATION_API_LIST_ENDPOINT,
|
|
180
|
+
NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY,
|
|
181
|
+
NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,
|
|
182
|
+
createOnChainTriggers,
|
|
183
|
+
deleteOnChainTriggers,
|
|
184
|
+
getOnChainNotifications,
|
|
185
|
+
markNotificationsAsRead
|
|
186
|
+
};
|
|
187
|
+
//# sourceMappingURL=chunk-UIRZIKC3.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/NotificationServicesController/services/onchain-notifications.ts"],"sourcesContent":["import { UserStorageController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport type { TRIGGER_TYPES } from '../constants/notification-schema';\nimport type { OnChainRawNotification } from '../types/on-chain-notification/on-chain-notification';\nimport type { components } from '../types/on-chain-notification/schema';\nimport type { UserStorage } from '../types/user-storage/user-storage';\nimport {\n traverseUserStorageTriggers,\n toggleUserStorageTriggerStatus,\n makeApiCall,\n} from '../utils/utils';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n};\n\nexport const TRIGGER_API = 'https://trigger.api.cx.metamask.io';\nexport const NOTIFICATION_API = 'https://notification.api.cx.metamask.io';\nexport const TRIGGER_API_BATCH_ENDPOINT = `${TRIGGER_API}/api/v1/triggers/batch`;\nexport const NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications`;\nexport const NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY = (page: number) =>\n `${NOTIFICATION_API_LIST_ENDPOINT}?page=${page}&per_page=100`;\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications/mark-as-read`;\n\n/**\n * Creates on-chain triggers based on the provided notification triggers.\n * This method generates a unique token for each trigger using the trigger ID and storage key,\n * proving ownership of the trigger being updated. It then makes an API call to create these triggers.\n * Upon successful creation, it updates the userStorage to reflect the new trigger status.\n *\n * @param userStorage - The user's storage object where triggers and their statuses are stored.\n * @param storageKey - A key used along with the trigger ID to generate a unique token for each trigger.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param triggers - An array of notification triggers to be created. Each trigger includes an ID, chain ID, kind, and address.\n * @returns A promise that resolves to void. Throws an error if the API call fails or if there's an issue creating the triggers.\n */\nexport async function createOnChainTriggers(\n userStorage: UserStorage,\n storageKey: string,\n bearerToken: string,\n triggers: NotificationTrigger[],\n): Promise<void> {\n type RequestPayloadTrigger = {\n id: string;\n // this is the trigger token, generated by using the uuid + storage key. It proves you own the trigger you are updating\n token: string;\n config: {\n kind: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n chain_id: number;\n address: string;\n };\n };\n const triggersToCreate: RequestPayloadTrigger[] = triggers.map((t) => ({\n id: t.id,\n token: UserStorageController.createSHA256Hash(t.id + storageKey),\n config: {\n kind: t.kind,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n chain_id: Number(t.chainId),\n address: t.address,\n },\n }));\n\n if (triggersToCreate.length === 0) {\n return;\n }\n\n const response = await makeApiCall(\n bearerToken,\n TRIGGER_API_BATCH_ENDPOINT,\n 'POST',\n triggersToCreate,\n );\n\n if (!response.ok) {\n const errorData = await response.json().catch(() => undefined);\n log.error('Error creating triggers:', errorData);\n throw new Error('OnChain Notifications - unable to create triggers');\n }\n\n // If the trigger creation was fine\n // then update the userStorage\n for (const trigger of triggersToCreate) {\n toggleUserStorageTriggerStatus(\n userStorage,\n trigger.config.address,\n String(trigger.config.chain_id),\n trigger.id,\n true,\n );\n }\n}\n\n/**\n * Deletes on-chain triggers based on the provided UUIDs.\n * This method generates a unique token for each trigger using the UUID and storage key,\n * proving ownership of the trigger being deleted. It then makes an API call to delete these triggers.\n * Upon successful deletion, it updates the userStorage to remove the deleted trigger statuses.\n *\n * @param userStorage - The user's storage object where triggers and their statuses are stored.\n * @param storageKey - A key used along with the UUID to generate a unique token for each trigger.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param uuids - An array of UUIDs representing the triggers to be deleted.\n * @returns A promise that resolves to the updated UserStorage object. Throws an error if the API call fails or if there's an issue deleting the triggers.\n */\nexport async function deleteOnChainTriggers(\n userStorage: UserStorage,\n storageKey: string,\n bearerToken: string,\n uuids: string[],\n): Promise<UserStorage> {\n const triggersToDelete = uuids.map((uuid) => ({\n id: uuid,\n token: UserStorageController.createSHA256Hash(uuid + storageKey),\n }));\n\n try {\n const response = await makeApiCall(\n bearerToken,\n TRIGGER_API_BATCH_ENDPOINT,\n 'DELETE',\n triggersToDelete,\n );\n\n if (!response.ok) {\n throw new Error(\n `Failed to delete on-chain notifications for uuids ${uuids.join(', ')}`,\n );\n }\n\n // Update the state of the deleted trigger to false\n for (const uuid of uuids) {\n for (const address in userStorage) {\n if (address in userStorage) {\n for (const chainId in userStorage[address]) {\n if (userStorage?.[address]?.[chainId]?.[uuid]) {\n delete userStorage[address][chainId][uuid];\n }\n }\n }\n }\n }\n\n // Follow-up cleanup, if an address had no triggers whatsoever, then we can delete the address\n const isEmpty = (obj = {}) => Object.keys(obj).length === 0;\n for (const address in userStorage) {\n if (address in userStorage) {\n for (const chainId in userStorage[address]) {\n // Chain isEmpty Check\n if (isEmpty(userStorage?.[address]?.[chainId])) {\n delete userStorage[address][chainId];\n }\n }\n\n // Address isEmpty Check\n if (isEmpty(userStorage?.[address])) {\n delete userStorage[address];\n }\n }\n }\n } catch (err) {\n log.error(\n `Error deleting on-chain notifications for uuids ${uuids.join(', ')}:`,\n err,\n );\n throw err;\n }\n\n return userStorage;\n}\n\n/**\n * Fetches on-chain notifications for the given user storage and BearerToken.\n * This method iterates through the userStorage to find enabled triggers and fetches notifications for those triggers.\n * It makes paginated API calls to the notifications service, transforming and aggregating the notifications into a single array.\n * The process stops either when all pages have been fetched or when a page has less than 100 notifications, indicating the end of the data.\n *\n * @param userStorage - The user's storage object containing trigger information.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @returns A promise that resolves to an array of OnChainRawNotification objects. If no triggers are enabled or an error occurs, it may return an empty array.\n */\nexport async function getOnChainNotifications(\n userStorage: UserStorage,\n bearerToken: string,\n): Promise<OnChainRawNotification[]> {\n const triggerIds = traverseUserStorageTriggers(userStorage, {\n mapTrigger: (t) => {\n if (!t.enabled) {\n return undefined;\n }\n return t.id;\n },\n });\n\n if (triggerIds.length === 0) {\n return [];\n }\n\n const onChainNotifications: OnChainRawNotification[] = [];\n const PAGE_LIMIT = 2;\n for (let page = 1; page <= PAGE_LIMIT; page++) {\n try {\n const response = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY(page),\n 'POST',\n // eslint-disable-next-line @typescript-eslint/naming-convention\n { trigger_ids: triggerIds },\n );\n\n const notifications = (await response.json()) as OnChainRawNotification[];\n\n // Transform and sort notifications\n const transformedNotifications = notifications\n .map(\n (\n n: components['schemas']['Notification'],\n ): OnChainRawNotification | undefined => {\n if (!n.data?.kind) {\n return undefined;\n }\n\n return {\n ...n,\n type: n.data.kind as TRIGGER_TYPES,\n } as OnChainRawNotification;\n },\n )\n .filter((n): n is OnChainRawNotification => Boolean(n));\n\n onChainNotifications.push(...transformedNotifications);\n\n // if less than 100 notifications on page, then means we reached end\n if (notifications.length < 100) {\n page = PAGE_LIMIT + 1;\n break;\n }\n } catch (err) {\n log.error(\n `Error fetching on-chain notifications for trigger IDs ${triggerIds.join(\n ', ',\n )}:`,\n err,\n );\n // do nothing\n }\n }\n\n return onChainNotifications;\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n bearerToken: string,\n notificationIds: string[],\n): Promise<void> {\n if (notificationIds.length === 0) {\n return;\n }\n\n try {\n const response = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,\n 'POST',\n { ids: notificationIds },\n );\n\n if (response.status !== 200) {\n const errorData = await response.json().catch(() => undefined);\n throw new Error(\n `Error marking notifications as read: ${errorData?.message as string}`,\n );\n }\n } catch (err) {\n log.error('Error marking notifications as read:', err);\n throw err;\n }\n}\n"],"mappings":";;;;;;;AAAA,SAAS,6BAA6B;AACtC,OAAO,SAAS;AAmBT,IAAM,cAAc;AACpB,IAAM,mBAAmB;AACzB,IAAM,6BAA6B,GAAG,WAAW;AACjD,IAAM,iCAAiC,GAAG,gBAAgB;AAC1D,IAAM,4CAA4C,CAAC,SACxD,GAAG,8BAA8B,SAAS,IAAI;AACzC,IAAM,6CAA6C,GAAG,gBAAgB;AAc7E,eAAsB,sBACpB,aACA,YACA,aACA,UACe;AAYf,Q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,EAAE,MAAM,MAAM;AACjB,mBAAO;AAAA,UACT;AAEA,iBAAO;AAAA,YACL,GAAG;AAAA,YACH,MAAM,EAAE,KAAK;AAAA,UACf;AAAA,QACF;AAAA,MACF,EACC,OAAO,CAAC,MAAmC,QAAQ,CAAC,CAAC;AAExD,2BAAqB,KAAK,GAAG,wBAAwB;AAGrD,UAAI,cAAc,SAAS,KAAK;AAC9B,eAAO,aAAa;AACpB;AAAA,MACF;AAAA,IACF,SAAS,KAAK;AACZ,UAAI;AAAA,QACF,yDAAyD,WAAW;AAAA,UAClE;AAAA,QACF,CAAC;AAAA,QACD;AAAA,MACF;AAAA,IAEF;AAAA,EACF;AAEA,SAAO;AACT;AAWA,eAAsB,wBACpB,aACA,iBACe;AACf,MAAI,gBAAgB,WAAW,GAAG;AAChC;AAAA,EACF;AAEA,MAAI;AACF,UAAM,WAAW,MAAM;AAAA,MACrB;AAAA,MACA;AAAA,MACA;AAAA,MACA,EAAE,KAAK,gBAAgB;AAAA,IACzB;AAEA,QAAI,SAAS,WAAW,KAAK;AAC3B,YAAM,YAAY,MAAM,SAAS,KAAK,EAAE,MAAM,MAAM,MAAS;AAC7D,YAAM,IAAI;AAAA,QACR,wCAAwC,WAAW,OAAiB;AAAA,MACtE;AAAA,IACF;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,MAAM,wCAAwC,GAAG;AACrD,UAAM;AAAA,EACR;AACF;","names":[]}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkPIZCE3JDjs = require('./chunk-PIZCE3JD.js');
|
|
6
|
+
|
|
7
|
+
// src/NotificationServicesController/services/onchain-notifications.ts
|
|
8
|
+
var _profilesynccontroller = require('@metamask/profile-sync-controller');
|
|
9
|
+
var _loglevel = require('loglevel'); var _loglevel2 = _interopRequireDefault(_loglevel);
|
|
10
|
+
var TRIGGER_API = "https://trigger.api.cx.metamask.io";
|
|
11
|
+
var NOTIFICATION_API = "https://notification.api.cx.metamask.io";
|
|
12
|
+
var TRIGGER_API_BATCH_ENDPOINT = `${TRIGGER_API}/api/v1/triggers/batch`;
|
|
13
|
+
var NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications`;
|
|
14
|
+
var NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY = (page) => `${NOTIFICATION_API_LIST_ENDPOINT}?page=${page}&per_page=100`;
|
|
15
|
+
var NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications/mark-as-read`;
|
|
16
|
+
async function createOnChainTriggers(userStorage, storageKey, bearerToken, triggers) {
|
|
17
|
+
const triggersToCreate = triggers.map((t) => ({
|
|
18
|
+
id: t.id,
|
|
19
|
+
token: _profilesynccontroller.UserStorageController.createSHA256Hash(t.id + storageKey),
|
|
20
|
+
config: {
|
|
21
|
+
kind: t.kind,
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
23
|
+
chain_id: Number(t.chainId),
|
|
24
|
+
address: t.address
|
|
25
|
+
}
|
|
26
|
+
}));
|
|
27
|
+
if (triggersToCreate.length === 0) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const response = await _chunkPIZCE3JDjs.makeApiCall.call(void 0,
|
|
31
|
+
bearerToken,
|
|
32
|
+
TRIGGER_API_BATCH_ENDPOINT,
|
|
33
|
+
"POST",
|
|
34
|
+
triggersToCreate
|
|
35
|
+
);
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
const errorData = await response.json().catch(() => void 0);
|
|
38
|
+
_loglevel2.default.error("Error creating triggers:", errorData);
|
|
39
|
+
throw new Error("OnChain Notifications - unable to create triggers");
|
|
40
|
+
}
|
|
41
|
+
for (const trigger of triggersToCreate) {
|
|
42
|
+
_chunkPIZCE3JDjs.toggleUserStorageTriggerStatus.call(void 0,
|
|
43
|
+
userStorage,
|
|
44
|
+
trigger.config.address,
|
|
45
|
+
String(trigger.config.chain_id),
|
|
46
|
+
trigger.id,
|
|
47
|
+
true
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function deleteOnChainTriggers(userStorage, storageKey, bearerToken, uuids) {
|
|
52
|
+
const triggersToDelete = uuids.map((uuid) => ({
|
|
53
|
+
id: uuid,
|
|
54
|
+
token: _profilesynccontroller.UserStorageController.createSHA256Hash(uuid + storageKey)
|
|
55
|
+
}));
|
|
56
|
+
try {
|
|
57
|
+
const response = await _chunkPIZCE3JDjs.makeApiCall.call(void 0,
|
|
58
|
+
bearerToken,
|
|
59
|
+
TRIGGER_API_BATCH_ENDPOINT,
|
|
60
|
+
"DELETE",
|
|
61
|
+
triggersToDelete
|
|
62
|
+
);
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Failed to delete on-chain notifications for uuids ${uuids.join(", ")}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
for (const uuid of uuids) {
|
|
69
|
+
for (const address in userStorage) {
|
|
70
|
+
if (address in userStorage) {
|
|
71
|
+
for (const chainId in userStorage[address]) {
|
|
72
|
+
if (userStorage?.[address]?.[chainId]?.[uuid]) {
|
|
73
|
+
delete userStorage[address][chainId][uuid];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const isEmpty = (obj = {}) => Object.keys(obj).length === 0;
|
|
80
|
+
for (const address in userStorage) {
|
|
81
|
+
if (address in userStorage) {
|
|
82
|
+
for (const chainId in userStorage[address]) {
|
|
83
|
+
if (isEmpty(userStorage?.[address]?.[chainId])) {
|
|
84
|
+
delete userStorage[address][chainId];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (isEmpty(userStorage?.[address])) {
|
|
88
|
+
delete userStorage[address];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} catch (err) {
|
|
93
|
+
_loglevel2.default.error(
|
|
94
|
+
`Error deleting on-chain notifications for uuids ${uuids.join(", ")}:`,
|
|
95
|
+
err
|
|
96
|
+
);
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
return userStorage;
|
|
100
|
+
}
|
|
101
|
+
async function getOnChainNotifications(userStorage, bearerToken) {
|
|
102
|
+
const triggerIds = _chunkPIZCE3JDjs.traverseUserStorageTriggers.call(void 0, userStorage, {
|
|
103
|
+
mapTrigger: (t) => {
|
|
104
|
+
if (!t.enabled) {
|
|
105
|
+
return void 0;
|
|
106
|
+
}
|
|
107
|
+
return t.id;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
if (triggerIds.length === 0) {
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
const onChainNotifications = [];
|
|
114
|
+
const PAGE_LIMIT = 2;
|
|
115
|
+
for (let page = 1; page <= PAGE_LIMIT; page++) {
|
|
116
|
+
try {
|
|
117
|
+
const response = await _chunkPIZCE3JDjs.makeApiCall.call(void 0,
|
|
118
|
+
bearerToken,
|
|
119
|
+
NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY(page),
|
|
120
|
+
"POST",
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
122
|
+
{ trigger_ids: triggerIds }
|
|
123
|
+
);
|
|
124
|
+
const notifications = await response.json();
|
|
125
|
+
const transformedNotifications = notifications.map(
|
|
126
|
+
(n) => {
|
|
127
|
+
if (!n.data?.kind) {
|
|
128
|
+
return void 0;
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
...n,
|
|
132
|
+
type: n.data.kind
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
).filter((n) => Boolean(n));
|
|
136
|
+
onChainNotifications.push(...transformedNotifications);
|
|
137
|
+
if (notifications.length < 100) {
|
|
138
|
+
page = PAGE_LIMIT + 1;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
} catch (err) {
|
|
142
|
+
_loglevel2.default.error(
|
|
143
|
+
`Error fetching on-chain notifications for trigger IDs ${triggerIds.join(
|
|
144
|
+
", "
|
|
145
|
+
)}:`,
|
|
146
|
+
err
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return onChainNotifications;
|
|
151
|
+
}
|
|
152
|
+
async function markNotificationsAsRead(bearerToken, notificationIds) {
|
|
153
|
+
if (notificationIds.length === 0) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
const response = await _chunkPIZCE3JDjs.makeApiCall.call(void 0,
|
|
158
|
+
bearerToken,
|
|
159
|
+
NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,
|
|
160
|
+
"POST",
|
|
161
|
+
{ ids: notificationIds }
|
|
162
|
+
);
|
|
163
|
+
if (response.status !== 200) {
|
|
164
|
+
const errorData = await response.json().catch(() => void 0);
|
|
165
|
+
throw new Error(
|
|
166
|
+
`Error marking notifications as read: ${errorData?.message}`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
} catch (err) {
|
|
170
|
+
_loglevel2.default.error("Error marking notifications as read:", err);
|
|
171
|
+
throw err;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
exports.TRIGGER_API = TRIGGER_API; exports.NOTIFICATION_API = NOTIFICATION_API; exports.TRIGGER_API_BATCH_ENDPOINT = TRIGGER_API_BATCH_ENDPOINT; exports.NOTIFICATION_API_LIST_ENDPOINT = NOTIFICATION_API_LIST_ENDPOINT; exports.NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY = NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY; exports.NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT; exports.createOnChainTriggers = createOnChainTriggers; exports.deleteOnChainTriggers = deleteOnChainTriggers; exports.getOnChainNotifications = getOnChainNotifications; exports.markNotificationsAsRead = markNotificationsAsRead;
|
|
187
|
+
//# sourceMappingURL=chunk-VOZ7Y2OO.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/NotificationServicesController/services/onchain-notifications.ts"],"names":[],"mappings":";;;;;;;AAAA,SAAS,6BAA6B;AACtC,OAAO,SAAS;AAmBT,IAAM,cAAc;AACpB,IAAM,mBAAmB;AACzB,IAAM,6BAA6B,GAAG,WAAW;AACjD,IAAM,iCAAiC,GAAG,gBAAgB;AAC1D,IAAM,4CAA4C,CAAC,SACxD,GAAG,8BAA8B,SAAS,IAAI;AACzC,IAAM,6CAA6C,GAAG,gBAAgB;AAc7E,eAAsB,sBACpB,aACA,YACA,aACA,UACe;AAYf,QAAM,mBAA4C,SAAS,IAAI,CAAC,OAAO;AAAA,IACrE,IAAI,EAAE;AAAA,IACN,OAAO,sBAAsB,iBAAiB,EAAE,KAAK,UAAU;AAAA,IAC/D,QAAQ;AAAA,MACN,MAAM,EAAE;AAAA;AAAA,MAER,UAAU,OAAO,EAAE,OAAO;AAAA,MAC1B,SAAS,EAAE;AAAA,IACb;AAAA,EACF,EAAE;AAEF,MAAI,iBAAiB,WAAW,GAAG;AACjC;AAAA,EACF;AAEA,QAAM,WAAW,MAAM;AAAA,IACrB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,YAAY,MAAM,SAAS,KAAK,EAAE,MAAM,MAAM,MAAS;AAC7D,QAAI,MAAM,4BAA4B,SAAS;AAC/C,UAAM,IAAI,MAAM,mDAAmD;AAAA,EACrE;AAIA,aAAW,WAAW,kBAA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A,MACA;AAAA,MACA,EAAE,KAAK,gBAAgB;AAAA,IACzB;AAEA,QAAI,SAAS,WAAW,KAAK;AAC3B,YAAM,YAAY,MAAM,SAAS,KAAK,EAAE,MAAM,MAAM,MAAS;AAC7D,YAAM,IAAI;AAAA,QACR,wCAAwC,WAAW,OAAiB;AAAA,MACtE;AAAA,IACF;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,MAAM,wCAAwC,GAAG;AACrD,UAAM;AAAA,EACR;AACF","sourcesContent":["import { UserStorageController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport type { TRIGGER_TYPES } from '../constants/notification-schema';\nimport type { OnChainRawNotification } from '../types/on-chain-notification/on-chain-notification';\nimport type { components } from '../types/on-chain-notification/schema';\nimport type { UserStorage } from '../types/user-storage/user-storage';\nimport {\n traverseUserStorageTriggers,\n toggleUserStorageTriggerStatus,\n makeApiCall,\n} from '../utils/utils';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n};\n\nexport const TRIGGER_API = 'https://trigger.api.cx.metamask.io';\nexport const NOTIFICATION_API = 'https://notification.api.cx.metamask.io';\nexport const TRIGGER_API_BATCH_ENDPOINT = `${TRIGGER_API}/api/v1/triggers/batch`;\nexport const NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications`;\nexport const NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY = (page: number) =>\n `${NOTIFICATION_API_LIST_ENDPOINT}?page=${page}&per_page=100`;\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications/mark-as-read`;\n\n/**\n * Creates on-chain triggers based on the provided notification triggers.\n * This method generates a unique token for each trigger using the trigger ID and storage key,\n * proving ownership of the trigger being updated. It then makes an API call to create these triggers.\n * Upon successful creation, it updates the userStorage to reflect the new trigger status.\n *\n * @param userStorage - The user's storage object where triggers and their statuses are stored.\n * @param storageKey - A key used along with the trigger ID to generate a unique token for each trigger.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param triggers - An array of notification triggers to be created. Each trigger includes an ID, chain ID, kind, and address.\n * @returns A promise that resolves to void. Throws an error if the API call fails or if there's an issue creating the triggers.\n */\nexport async function createOnChainTriggers(\n userStorage: UserStorage,\n storageKey: string,\n bearerToken: string,\n triggers: NotificationTrigger[],\n): Promise<void> {\n type RequestPayloadTrigger = {\n id: string;\n // this is the trigger token, generated by using the uuid + storage key. It proves you own the trigger you are updating\n token: string;\n config: {\n kind: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n chain_id: number;\n address: string;\n };\n };\n const triggersToCreate: RequestPayloadTrigger[] = triggers.map((t) => ({\n id: t.id,\n token: UserStorageController.createSHA256Hash(t.id + storageKey),\n config: {\n kind: t.kind,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n chain_id: Number(t.chainId),\n address: t.address,\n },\n }));\n\n if (triggersToCreate.length === 0) {\n return;\n }\n\n const response = await makeApiCall(\n bearerToken,\n TRIGGER_API_BATCH_ENDPOINT,\n 'POST',\n triggersToCreate,\n );\n\n if (!response.ok) {\n const errorData = await response.json().catch(() => undefined);\n log.error('Error creating triggers:', errorData);\n throw new Error('OnChain Notifications - unable to create triggers');\n }\n\n // If the trigger creation was fine\n // then update the userStorage\n for (const trigger of triggersToCreate) {\n toggleUserStorageTriggerStatus(\n userStorage,\n trigger.config.address,\n String(trigger.config.chain_id),\n trigger.id,\n true,\n );\n }\n}\n\n/**\n * Deletes on-chain triggers based on the provided UUIDs.\n * This method generates a unique token for each trigger using the UUID and storage key,\n * proving ownership of the trigger being deleted. It then makes an API call to delete these triggers.\n * Upon successful deletion, it updates the userStorage to remove the deleted trigger statuses.\n *\n * @param userStorage - The user's storage object where triggers and their statuses are stored.\n * @param storageKey - A key used along with the UUID to generate a unique token for each trigger.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param uuids - An array of UUIDs representing the triggers to be deleted.\n * @returns A promise that resolves to the updated UserStorage object. Throws an error if the API call fails or if there's an issue deleting the triggers.\n */\nexport async function deleteOnChainTriggers(\n userStorage: UserStorage,\n storageKey: string,\n bearerToken: string,\n uuids: string[],\n): Promise<UserStorage> {\n const triggersToDelete = uuids.map((uuid) => ({\n id: uuid,\n token: UserStorageController.createSHA256Hash(uuid + storageKey),\n }));\n\n try {\n const response = await makeApiCall(\n bearerToken,\n TRIGGER_API_BATCH_ENDPOINT,\n 'DELETE',\n triggersToDelete,\n );\n\n if (!response.ok) {\n throw new Error(\n `Failed to delete on-chain notifications for uuids ${uuids.join(', ')}`,\n );\n }\n\n // Update the state of the deleted trigger to false\n for (const uuid of uuids) {\n for (const address in userStorage) {\n if (address in userStorage) {\n for (const chainId in userStorage[address]) {\n if (userStorage?.[address]?.[chainId]?.[uuid]) {\n delete userStorage[address][chainId][uuid];\n }\n }\n }\n }\n }\n\n // Follow-up cleanup, if an address had no triggers whatsoever, then we can delete the address\n const isEmpty = (obj = {}) => Object.keys(obj).length === 0;\n for (const address in userStorage) {\n if (address in userStorage) {\n for (const chainId in userStorage[address]) {\n // Chain isEmpty Check\n if (isEmpty(userStorage?.[address]?.[chainId])) {\n delete userStorage[address][chainId];\n }\n }\n\n // Address isEmpty Check\n if (isEmpty(userStorage?.[address])) {\n delete userStorage[address];\n }\n }\n }\n } catch (err) {\n log.error(\n `Error deleting on-chain notifications for uuids ${uuids.join(', ')}:`,\n err,\n );\n throw err;\n }\n\n return userStorage;\n}\n\n/**\n * Fetches on-chain notifications for the given user storage and BearerToken.\n * This method iterates through the userStorage to find enabled triggers and fetches notifications for those triggers.\n * It makes paginated API calls to the notifications service, transforming and aggregating the notifications into a single array.\n * The process stops either when all pages have been fetched or when a page has less than 100 notifications, indicating the end of the data.\n *\n * @param userStorage - The user's storage object containing trigger information.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @returns A promise that resolves to an array of OnChainRawNotification objects. If no triggers are enabled or an error occurs, it may return an empty array.\n */\nexport async function getOnChainNotifications(\n userStorage: UserStorage,\n bearerToken: string,\n): Promise<OnChainRawNotification[]> {\n const triggerIds = traverseUserStorageTriggers(userStorage, {\n mapTrigger: (t) => {\n if (!t.enabled) {\n return undefined;\n }\n return t.id;\n },\n });\n\n if (triggerIds.length === 0) {\n return [];\n }\n\n const onChainNotifications: OnChainRawNotification[] = [];\n const PAGE_LIMIT = 2;\n for (let page = 1; page <= PAGE_LIMIT; page++) {\n try {\n const response = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY(page),\n 'POST',\n // eslint-disable-next-line @typescript-eslint/naming-convention\n { trigger_ids: triggerIds },\n );\n\n const notifications = (await response.json()) as OnChainRawNotification[];\n\n // Transform and sort notifications\n const transformedNotifications = notifications\n .map(\n (\n n: components['schemas']['Notification'],\n ): OnChainRawNotification | undefined => {\n if (!n.data?.kind) {\n return undefined;\n }\n\n return {\n ...n,\n type: n.data.kind as TRIGGER_TYPES,\n } as OnChainRawNotification;\n },\n )\n .filter((n): n is OnChainRawNotification => Boolean(n));\n\n onChainNotifications.push(...transformedNotifications);\n\n // if less than 100 notifications on page, then means we reached end\n if (notifications.length < 100) {\n page = PAGE_LIMIT + 1;\n break;\n }\n } catch (err) {\n log.error(\n `Error fetching on-chain notifications for trigger IDs ${triggerIds.join(\n ', ',\n )}:`,\n err,\n );\n // do nothing\n }\n }\n\n return onChainNotifications;\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n bearerToken: string,\n notificationIds: string[],\n): Promise<void> {\n if (notificationIds.length === 0) {\n return;\n }\n\n try {\n const response = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,\n 'POST',\n { ids: notificationIds },\n );\n\n if (response.status !== 200) {\n const errorData = await response.json().catch(() => undefined);\n throw new Error(\n `Error marking notifications as read: ${errorData?.message as string}`,\n );\n }\n } catch (err) {\n log.error('Error marking notifications as read:', err);\n throw err;\n }\n}\n"]}
|
|
@@ -9,7 +9,7 @@ var _chunkZMNXLHACjs = require('./chunk-ZMNXLHAC.js');
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
var
|
|
12
|
+
var _chunkVOZ7Y2OOjs = require('./chunk-VOZ7Y2OO.js');
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
|
|
@@ -390,7 +390,7 @@ var NotificationServicesController = class extends _basecontroller.BaseControlle
|
|
|
390
390
|
await _chunkV46WVGWNjs.__privateGet.call(void 0, this, _storage).setNotificationStorage(JSON.stringify(userStorage));
|
|
391
391
|
}
|
|
392
392
|
const triggers = _chunkPIZCE3JDjs.traverseUserStorageTriggers.call(void 0, userStorage);
|
|
393
|
-
await
|
|
393
|
+
await _chunkVOZ7Y2OOjs.createOnChainTriggers.call(void 0,
|
|
394
394
|
userStorage,
|
|
395
395
|
storageKey,
|
|
396
396
|
bearerToken,
|
|
@@ -484,7 +484,7 @@ var NotificationServicesController = class extends _basecontroller.BaseControlle
|
|
|
484
484
|
if (UUIDs.length === 0) {
|
|
485
485
|
return userStorage;
|
|
486
486
|
}
|
|
487
|
-
await
|
|
487
|
+
await _chunkVOZ7Y2OOjs.deleteOnChainTriggers.call(void 0,
|
|
488
488
|
userStorage,
|
|
489
489
|
storageKey,
|
|
490
490
|
bearerToken,
|
|
@@ -540,7 +540,7 @@ var NotificationServicesController = class extends _basecontroller.BaseControlle
|
|
|
540
540
|
return void 0;
|
|
541
541
|
}
|
|
542
542
|
});
|
|
543
|
-
await
|
|
543
|
+
await _chunkVOZ7Y2OOjs.createOnChainTriggers.call(void 0,
|
|
544
544
|
userStorage,
|
|
545
545
|
storageKey,
|
|
546
546
|
bearerToken,
|
|
@@ -576,7 +576,7 @@ var NotificationServicesController = class extends _basecontroller.BaseControlle
|
|
|
576
576
|
const userStorage = await _chunkV46WVGWNjs.__privateGet.call(void 0, this, _storage).getNotificationStorage().then((s) => s && JSON.parse(s)).catch(() => null);
|
|
577
577
|
const bearerToken = await _chunkV46WVGWNjs.__privateGet.call(void 0, this, _auth).getBearerToken().catch(() => null);
|
|
578
578
|
if (userStorage && bearerToken) {
|
|
579
|
-
const notifications = await
|
|
579
|
+
const notifications = await _chunkVOZ7Y2OOjs.getOnChainNotifications.call(void 0,
|
|
580
580
|
userStorage,
|
|
581
581
|
bearerToken
|
|
582
582
|
).catch(() => []);
|
|
@@ -629,7 +629,7 @@ var NotificationServicesController = class extends _basecontroller.BaseControlle
|
|
|
629
629
|
onchainNotificationIds = onChainNotifications.map(
|
|
630
630
|
(notification) => notification.id
|
|
631
631
|
);
|
|
632
|
-
await
|
|
632
|
+
await _chunkVOZ7Y2OOjs.markNotificationsAsRead.call(void 0,
|
|
633
633
|
bearerToken,
|
|
634
634
|
onchainNotificationIds
|
|
635
635
|
).catch(() => {
|
|
@@ -799,4 +799,4 @@ clearUpdatingAccountsState_fn = function(accounts) {
|
|
|
799
799
|
|
|
800
800
|
|
|
801
801
|
exports.defaultState = defaultState; exports.NotificationServicesController = NotificationServicesController;
|
|
802
|
-
//# sourceMappingURL=chunk-
|
|
802
|
+
//# sourceMappingURL=chunk-XNBTOK6G.js.map
|