-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathUserNotifications.js
More file actions
300 lines (256 loc) · 10.8 KB
/
UserNotifications.js
File metadata and controls
300 lines (256 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/**
* User Notifications Service
*
* This module handles server-sent notifications from the entitlements API.
* Notifications are displayed as toast messages and acknowledged back to the server.
*/
define(function (require, exports, module) {
const KernalModeTrust = window.KernalModeTrust;
if(!KernalModeTrust){
throw new Error("UserNotifications should have access to KernalModeTrust. Cannot boot without trust ring");
}
const PreferencesManager = require("preferences/PreferencesManager"),
NotificationUI = require("widgets/NotificationUI");
const PREF_NOTIFICATIONS_SHOWN_LIST = "notificationsShownList";
PreferencesManager.stateManager.definePreference(PREF_NOTIFICATIONS_SHOWN_LIST, "object", {});
let EntitlementsManager;
let LoginService;
// In-memory tracking to prevent duplicate notifications during rapid EVENT_ENTITLEMENTS_CHANGED events
const currentlyShownNotifications = new Set();
// Save a copy of window.fetch so that extensions won't tamper with it
let fetchFn = window.fetch;
/**
* Get the list of notification IDs that have been shown and acknowledged
* @returns {Object} Map of notificationID -> timestamp
*/
function getShownNotifications() {
return PreferencesManager.stateManager.get(PREF_NOTIFICATIONS_SHOWN_LIST) || {};
}
/**
* Mark a notification as shown and acknowledged
* @param {string} notificationID - The notification ID to mark as shown
*/
function markNotificationAsShown(notificationID) {
const shownNotifications = getShownNotifications();
shownNotifications[notificationID] = Date.now();
PreferencesManager.stateManager.set(PREF_NOTIFICATIONS_SHOWN_LIST, shownNotifications);
currentlyShownNotifications.delete(notificationID);
}
/**
* Call the server API to acknowledge a notification
* @param {string} notificationID - The notification ID to acknowledge
* @returns {Promise<boolean>} - True if successful, false otherwise
*/
async function acknowledgeNotificationToServer(notificationID) {
try {
const accountBaseURL = LoginService.getAccountBaseURL();
let url = `${accountBaseURL}/notificationAcknowledged`;
const requestBody = {
notificationID: notificationID
};
let fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(requestBody)
};
// Handle different authentication methods for browser vs desktop
if (Phoenix.isNativeApp) {
// Desktop app: use appSessionID and validationCode
const profile = LoginService.getProfile();
if (profile && profile.apiKey && profile.validationCode) {
requestBody.appSessionID = profile.apiKey;
requestBody.validationCode = profile.validationCode;
fetchOptions.body = JSON.stringify(requestBody);
}
} else {
// Browser app: use session cookies
fetchOptions.credentials = 'include';
}
const response = await fetchFn(url, fetchOptions);
if (response.ok) {
const result = await response.json();
if (result.isSuccess) {
console.log(`Notification ${notificationID} acknowledged successfully`);
return true;
}
}
console.warn(`Failed to acknowledge notification ${notificationID}:`, response.status);
return false;
} catch (error) {
console.error(`Error acknowledging notification ${notificationID}:`, error);
return false;
}
}
/**
* Handle notification dismissal
* @param {string} notificationID - The notification ID that was dismissed
*/
async function handleNotificationDismiss(notificationID) {
// Always mark as shown locally to prevent re-showing, even if API fails
markNotificationAsShown(notificationID);
// Call server API to acknowledge
return acknowledgeNotificationToServer(notificationID);
}
/**
* Check if a notification should be shown
* @param {Object} notification - The notification object from server
* @returns {boolean} - True if should be shown, false otherwise
*/
function shouldShowNotification(notification) {
if (!notification || !notification.notificationID) {
return false;
}
// Check if expired
if (notification.validTill && Date.now() > notification.validTill) {
return false;
}
// Check if already shown (persistent storage)
const shownNotifications = getShownNotifications();
if (shownNotifications[notification.notificationID]) {
return false;
}
// Check if currently being shown (in-memory)
if (currentlyShownNotifications.has(notification.notificationID)) {
return false;
}
return true;
}
/**
* Display a single notification
* @param {Object} notification - The notification object from server
*/
function displayNotification(notification) {
const {
notificationID,
title,
htmlContent,
options = {}
} = notification;
// Mark as currently showing to prevent duplicates
currentlyShownNotifications.add(notificationID);
// Prepare options for NotificationUI
const toastOptions = {
dismissOnClick: options.dismissOnClick !== undefined ? options.dismissOnClick : true,
toastStyle: options.toastStyle || NotificationUI.NOTIFICATION_STYLES_CSS_CLASS.INFO
};
// Add autoCloseTimeS if provided
if (options.autoCloseTimeS) {
toastOptions.autoCloseTimeS = options.autoCloseTimeS;
}
// Create and show the toast notification
const notificationInstance = NotificationUI.createToastFromTemplate(
title,
htmlContent,
toastOptions
);
// Handle notification dismissal
notificationInstance.done(() => {
handleNotificationDismiss(notificationID);
});
}
/**
* Clean up stale notification IDs from state manager
* Removes notification IDs that are no longer in the remote notifications list
* @param {Array} remoteNotifications - The current notifications from server
*/
function cleanupStaleNotifications(remoteNotifications) {
if (!remoteNotifications || remoteNotifications.length === 0) {
return;
}
// Build a set of remote notification IDs for quick lookup
const remoteIDs = new Set();
for (const notification of remoteNotifications) {
if (notification.notificationID) {
remoteIDs.add(notification.notificationID);
}
}
// Keep only notification IDs that are still in remote notifications
const shownNotifications = getShownNotifications();
const updatedShownNotifications = {};
for (const id in shownNotifications) {
if (remoteIDs.has(id)) {
updatedShownNotifications[id] = shownNotifications[id];
}
}
// Update state if we removed any stale IDs
const oldCount = Object.keys(shownNotifications).length;
const newCount = Object.keys(updatedShownNotifications).length;
if (newCount < oldCount) {
console.log(`Cleaning up ${oldCount - newCount} stale notification ID(s) from state`);
PreferencesManager.stateManager.set(PREF_NOTIFICATIONS_SHOWN_LIST, updatedShownNotifications);
}
}
/**
* Process notifications from entitlements
*/
async function processNotifications() {
try {
const notifications = await EntitlementsManager.getNotifications();
if (!notifications || !Array.isArray(notifications)) {
return;
}
// Clean up stale notification IDs if we have at least 1 notification from server
if (notifications.length > 0) {
cleanupStaleNotifications(notifications);
}
// Filter and show new notifications
const notificationsToShow = notifications.filter(shouldShowNotification);
if (notificationsToShow.length > 0) {
console.log(`Showing ${notificationsToShow.length} new notification(s)`);
notificationsToShow.forEach(displayNotification);
}
} catch (error) {
console.error('Error processing notifications:', error);
}
}
/**
* Initialize the UserNotifications service
*/
function init() {
EntitlementsManager = KernalModeTrust.EntitlementsManager;
LoginService = KernalModeTrust.loginService;
if (!EntitlementsManager || !LoginService) {
throw new Error("UserNotifications requires EntitlementsManager and LoginService in KernalModeTrust");
}
// Listen for entitlements changes
EntitlementsManager.on(EntitlementsManager.EVENT_ENTITLEMENTS_CHANGED, processNotifications);
console.log('UserNotifications service initialized');
}
// Test-only exports for integration testing
if (Phoenix.isTestWindow) {
window._test_user_notifications_exports = {
getShownNotifications,
markNotificationAsShown,
shouldShowNotification,
acknowledgeNotificationToServer,
processNotifications,
cleanupStaleNotifications,
currentlyShownNotifications,
setFetchFn: function (fn) {
fetchFn = fn;
}
};
}
exports.init = init;
// no public exports to prevent extension tampering
});