mirror of
https://github.com/wekan/wekan.git
synced 2026-11-20 09:45:04 -05:00
Member Settings / Notification Settings / "Browser notifications on this device": the browser asks for permission and gives a push subscription, and from then on every notification the member gets in WeKan - the same recipients, watching and muted kinds as the tray and e-mail - also pops up from that browser even with WeKan closed; a click opens the card. Turn it on per browser and device, up to ten per account. web-push 3.6.7 (pinned) sends it with this WeKan's own VAPID key pair, made once and kept in a server-only collection; only the public key leaves the server. A subscription's endpoint is chosen by the browser and POSTed to by the server, so it is accepted only over https and only at the push services browsers use - an internal address or another site is refused when saved and again before every send. Gone subscriptions are forgotten. The service worker opens only paths of this WeKan. Not offered on Sandstorm. models/lib/webPush.js decides; tests/webPush.test.cjs covers the push services, internal and look-alike hosts, foreign click targets and the bound; server/lib/tests/webPush.tests.js runs in Meteor (passing): one key pair, only the public key out, an internal endpoint refused. With the webhook activity picker (c3321926ab) and the all-boards activity feed (656af74b2c), this completes both issues. The new strings are English placeholders until the next translation pass. Thanks to saurabharch, hingerl and xet7 ! Fixes #2026, Fixes #1377.
113 lines
5.1 KiB
JavaScript
113 lines
5.1 KiB
JavaScript
// #2026 / #1377: browser push notifications (models/lib/webPush.js), through
|
|
// the web-push library (pinned) with this WeKan's own VAPID keys, made once
|
|
// and kept server-side: the private key is never published.
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { check } from 'meteor/check';
|
|
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
|
|
import { TAPi18n } from '/imports/i18n';
|
|
import { formatActivityNotificationTitle } from '/server/lib/activityNotificationTitle';
|
|
|
|
const { cleanSubscription, subscriptionsAfterAdd, pushPayload } = require('/models/lib/webPush');
|
|
const { redactForRecipient } = require('/models/lib/notificationDelivery');
|
|
|
|
// One document, { _id: 'vapid', publicKey, privateKey }; never published.
|
|
export const WebPushKeys = new Mongo.Collection('webPushKeys');
|
|
WebPushKeys.deny({ insert: () => true, update: () => true, remove: () => true });
|
|
|
|
let keysPromise = null;
|
|
function vapidKeys() {
|
|
if (!keysPromise) {
|
|
keysPromise = (async () => {
|
|
const found = await WebPushKeys.findOneAsync('vapid');
|
|
if (found) return found;
|
|
const generated = { _id: 'vapid', ...require('web-push').generateVAPIDKeys() };
|
|
try { await WebPushKeys.insertAsync(generated); } catch (e) { return WebPushKeys.findOneAsync('vapid'); }
|
|
return generated;
|
|
})().catch(error => { keysPromise = null; throw error; });
|
|
}
|
|
return keysPromise;
|
|
}
|
|
|
|
function rootPath(url) {
|
|
try { return new URL(url).pathname; } catch (e) { return '/'; }
|
|
}
|
|
|
|
async function pushTo(user, subscriptions, payload) {
|
|
const webpush = require('web-push');
|
|
const keys = await vapidKeys();
|
|
const subject = (process.env.ROOT_URL && /^https:/.test(process.env.ROOT_URL)) ? process.env.ROOT_URL : 'mailto:webmaster@localhost';
|
|
for (const sub of subscriptions) {
|
|
const clean = cleanSubscription(sub);
|
|
if (!clean) continue;
|
|
try {
|
|
await webpush.sendNotification(clean, payload, { TTL: 3600, timeout: 10000,
|
|
vapidDetails: { subject, publicKey: keys.publicKey, privateKey: keys.privateKey } });
|
|
} catch (error) {
|
|
// Gone (unsubscribed, expired): forget it.
|
|
if (error && (error.statusCode === 404 || error.statusCode === 410)) {
|
|
await Meteor.users.updateAsync(user._id, { $pull: { webPushSubscriptions: { endpoint: clean.endpoint } } });
|
|
} else {
|
|
console.warn(`Browser push not delivered: ${error && (error.statusCode || error.message)}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Called with an activity's prepared notification, like ntfy: the same
|
|
// recipients the tray and e-mail get; best effort, after the activity.
|
|
export function sendActivityToWebPush({ users, title, description, params } = {}) {
|
|
if (Meteor.settings?.public?.sandstorm) return;
|
|
const recipients = (Array.isArray(users) ? users : []).filter(u => u && u._id);
|
|
if (!recipients.length) return;
|
|
Meteor.defer(async () => {
|
|
try {
|
|
const own = await Meteor.users.find({ _id: { $in: recipients.map(u => u._id) }, 'webPushSubscriptions.0': { $exists: true },
|
|
loginDisabled: { $ne: true } }, { fields: { webPushSubscriptions: 1 } }).fetchAsync();
|
|
if (!own.length) return;
|
|
const Boards = require('/models/boards').default;
|
|
const board = params.boardId ? await Boards.findOneAsync(params.boardId) : null;
|
|
for (const row of own) {
|
|
const user = recipients.find(u => u._id === row._id);
|
|
const lan = typeof user.getLanguage === 'function' ? user.getLanguage() : 'en';
|
|
await TAPi18n.ensureLanguageLoaded(lan);
|
|
const safe = redactForRecipient(params, { isBoardAdmin: !!(board && board.hasAdmin && board.hasAdmin(user._id)) });
|
|
await pushTo(row, row.webPushSubscriptions, pushPayload({
|
|
title: formatActivityNotificationTitle(title, safe, TAPi18n.__.bind(TAPi18n), lan),
|
|
body: `${safe.user || ''} ${TAPi18n.__(description, safe, lan)}`.trim(),
|
|
url: rootPath(safe.url),
|
|
}));
|
|
}
|
|
} catch (error) {
|
|
console.warn(`Browser push skipped: ${error && error.message}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
Meteor.methods({
|
|
async webPushPublicKey() {
|
|
if (!this.userId) throw new Meteor.Error('not-authorized');
|
|
return (await vapidKeys()).publicKey;
|
|
},
|
|
async webPushSubscribe(subscription) {
|
|
check(subscription, Object);
|
|
if (!this.userId) throw new Meteor.Error('not-authorized');
|
|
const clean = cleanSubscription(subscription);
|
|
if (!clean) throw new Meteor.Error('web-push-invalid-subscription');
|
|
const user = await Meteor.users.findOneAsync(this.userId, { fields: { webPushSubscriptions: 1 } });
|
|
await Meteor.users.updateAsync(this.userId, { $set: {
|
|
webPushSubscriptions: subscriptionsAfterAdd(user && user.webPushSubscriptions, clean) } });
|
|
return true;
|
|
},
|
|
async webPushUnsubscribe(endpoint) {
|
|
check(endpoint, String);
|
|
if (!this.userId) throw new Meteor.Error('not-authorized');
|
|
await Meteor.users.updateAsync(this.userId, { $pull: { webPushSubscriptions: { endpoint } } });
|
|
return true;
|
|
},
|
|
});
|
|
|
|
for (const name of ['webPushPublicKey', 'webPushSubscribe', 'webPushUnsubscribe']) {
|
|
DDPRateLimiter.addRule({ type: 'method', name, connectionId: () => true }, 10, 10000);
|
|
}
|