More web push progress.

Ylian Saint-Hilaire committed Feb 9, 2021 at 12:24 UTC 50185dac86a1bc0b1310c534a4b53238b22e8b7a
3 files changed +42 -1
meshuser.js
+39 -1
@@ -5292,7 +5292,45 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5292 break;
5293 }
5294 case 'webpush': {
5295 - //console.log(command);
5295 + // Check if web push is enabled
5296 + if (parent.parent.webpush == null) break;
5297 +
5298 + // Adds a web push session to the user. Start by sanitizing the input.
5299 + if ((typeof command.sub != 'object') && (typeof command.sub.keys != 'object') && (typeof command.sub.endpoint != 'string')) break;
5300 + if (common.validateString(command.sub.endpoint, 1, 1024) == false) break; // Check endpoint
5301 + if (common.validateString(command.sub.keys.auth, 1, 64) == false) break; // Check key auth
5302 + if (common.validateString(command.sub.keys.p256dh, 1, 256) == false) break; // Check key dh
5303 + var newWebPush = { endpoint: command.sub.endpoint, keys: { auth: command.sub.keys.auth, p256dh: command.sub.keys.p256dh } }
5304 +
5305 + // See if we need to add this session
5306 + var changed = false;
5307 + if (user.webpush == null) {
5308 + changed = true;
5309 + user.webpush = [newWebPush];
5310 + } else {
5311 + var found = false;
5312 + for (var i in user.webpush) {
5313 + if ((user.webpush[i].endpoint == newWebPush.endpoint) && (user.webpush[i].keys.auth == newWebPush.keys.auth) && (user.webpush[i].keys.p256dh == newWebPush.keys.p256dh)) { found = true; }
5314 + }
5315 + if (found == true) break;
5316 + changed = true;
5317 + user.webpush.push(newWebPush);
5318 + while (user.webpush.length > 5) { user.webpush.shift(); }
5319 + }
5320 +
5321 + // If we added the session, update the user
5322 + if (changed == true) {
5323 + // Update the database
5324 + parent.db.SetUser(user);
5325 +
5326 + // Event the change
5327 + var message = { etype: 'user', userid: user._id, username: user.name, account: parent.CloneSafeUser(user), action: 'accountchange', domain: domain.id, nolog: 1 };
5328 + if (db.changeStream) { message.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
5329 + var targets = ['*', 'server-users', user._id];
5330 + if (user.groups) { for (var i in user.groups) { targets.push('server-users:' + i); } }
5331 + parent.parent.DispatchEvent(targets, obj, message);
5332 + }
5333 +
5334 break;
5335 }
5336 case 'print': {
views/default.handlebars
+1
@@ -13842,6 +13842,7 @@
13842 //
13843
13844 function setupServiceWorker() {
13845 + if ((features2 & 8) == 0) return; // Web push not enabled on this server
13846 if ((typeof serverinfo.vapidpublickey != 'string') || (Notification == null) || (Notification.permission != 'granted')) return;
13847 // Register the service worker
13848 if ('serviceWorker' in navigator) {
webserver.js
+2
@@ -2457,6 +2457,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
2457 if (obj.parent.amtManager != null) { features2 += 0x00000001; } // Indicates that the Intel AMT manager is active
2458 if (obj.parent.firebase != null) { features2 += 0x00000002; } // Indicates the server supports Firebase push messaging
2459 if ((obj.parent.firebase != null) && (obj.parent.firebase.pushOnly != true)) { features2 += 0x00000004; } // Indicates the server supports Firebase two-way push messaging
2460 + if (obj.parent.webpush != null) { features2 += 0x00000008; } // Indicates web push is enabled
2461
2462 // Create a authentication cookie
2463 const authCookie = obj.parent.encodeCookie({ userid: dbGetFunc.user._id, domainid: domain.id, ip: req.clientIp }, obj.parent.loginCookieEncryptionKey);
@@ -6340,6 +6341,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
6341 if ((typeof user2.otpsecret == 'string') && (user2.otpsecret != null)) { user2.otpsecret = 1; } // Indicates a time secret is present.
6342 if ((typeof user2.otpkeys == 'object') && (user2.otpkeys != null)) { user2.otpkeys = 0; if (user.otpkeys != null) { for (var i = 0; i < user.otpkeys.keys.length; i++) { if (user.otpkeys.keys[i].u == true) { user2.otpkeys = 1; } } } } // Indicates the number of one time backup codes that are active.
6343 if ((typeof user2.otphkeys == 'object') && (user2.otphkeys != null)) { user2.otphkeys = user2.otphkeys.length; } // Indicates the number of hardware keys setup
6344 + if ((typeof user2.webpush == 'object') && (user2.webpush != null)) { user2.webpush = user2.webpush.length; } // Indicates the number of web push sessions we have
6345 return user2;
6346 }
6347