Files
wekan/client/components/settings/notificationSettingsPopup.js
T
Lauri Ojansivu 0a5ae3e205 Browser push notifications, per device.
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.
2026-10-14 15:44:01 +03:00

214 lines
8.8 KiB
JavaScript

import { ReactiveCache } from '/imports/reactiveCache';
import { NOTIFICATION_SERVICES } from '/models/lib/notificationSettings';
import { Utils } from '/client/lib/utils';
import { ReactiveVar } from 'meteor/reactive-var';
import { TAPi18n } from '/imports/i18n';
import { parseDueReminderInput } from '/models/lib/dueNotificationConfig';
import { GROUP_KEYS, cleanMutedGroups } from '/models/lib/notificationActivityGroups';
// The 3-tier Notification Settings popup (see notificationSettingsPopup.jade
// and models/lib/notificationSettings.js). `this.data().scope` is one of
// 'admin' | 'board' | 'member', set by whichever menu entry opened it
// (Popup.open('notificationSettings', { scope }) in userHeader.js/sidebar.js,
// or passed directly as the pane's data context in peopleBody.jade).
//
// At 'board' scope the board id comes from Utils.getCurrentBoardId() - the
// popup is only ever opened from the currently open board's sidebar.
function currentBoard() {
return ReactiveCache.getBoard(Utils.getCurrentBoardId());
}
function currentValue(scope, service) {
const field = service === 'email' ? 'notifyOverrideEmail' : 'notifyOverrideTray';
const adminField = service === 'email' ? 'notifyDefaultEmail' : 'notifyDefaultTray';
if (scope === 'admin') {
const setting = ReactiveCache.getCurrentSetting();
const value = setting && setting[adminField];
return value === false ? false : true; // defaultValue: true
}
if (scope === 'board') {
const board = currentBoard();
return board ? board[field] : undefined;
}
// member
const user = ReactiveCache.getCurrentUser();
const profileField = service === 'email' ? 'notifyOverrideEmail' : 'notifyOverrideTray';
return user && user.profile ? user.profile[profileField] : undefined;
}
Template.notificationSettingsPopup.onCreated(function () {
this.dueReminderMessage = new ReactiveVar('');
// Toggle state starts from the board and changes locally until Save.
const board = this.data.scope === 'board' ? currentBoard() : null;
this.dueRemindersOff = new ReactiveVar(Boolean(board && Array.isArray(board.dueReminderDays)
&& board.dueReminderDays.length === 0));
this.dueReminderWebhook = new ReactiveVar(Boolean(board && board.dueReminderWebhook));
// #2026 / #1377: whether this device gets browser push.
this.webPushHere = new ReactiveVar(false);
this.webPushMessage = new ReactiveVar('');
if (this.data.scope === 'member') {
import('/client/lib/webPush').then(m => m.webPushEnabledHere()).then(on => this.webPushHere.set(on)).catch(() => {});
}
// #5148: the member's own ntfy topic, through their own methods.
this.ntfy = new ReactiveVar({ enabled: false, url: '', token: '' });
this.ntfyMessage = new ReactiveVar('');
if (this.data.scope === 'member') {
Meteor.call('getNtfySettings', (error, value) => { if (!error && value) this.ntfy.set(value); });
}
});
Template.notificationSettingsPopup.helpers({
ntfy() { return Template.instance().ntfy.get(); },
webPushSupported() { return require('/client/lib/webPush').webPushSupported(); },
webPushHere() { return Template.instance().webPushHere.get(); },
webPushMessage() { return Template.instance().webPushMessage.get(); },
ntfyMessage() { return Template.instance().ntfyMessage.get(); },
// #5323: per-board due-date reminder offsets and webhook delivery.
isBoardScope() {
return Template.instance().data.scope === 'board';
},
dueReminderDaysText() {
const board = currentBoard();
return board && Array.isArray(board.dueReminderDays) ? board.dueReminderDays.join(', ') : '';
},
dueRemindersOff() {
return Template.instance().dueRemindersOff.get();
},
dueReminderWebhook() {
return Template.instance().dueReminderWebhook.get();
},
dueReminderMessage() {
return Template.instance().dueReminderMessage.get();
},
isAdminScope() {
return Template.instance().data.scope === 'admin';
},
// #3695 / #5171: the delivery sections. At board scope only a board admin
// can change them (the server method checks that too).
showDelivery() {
const scope = Template.instance().data.scope;
if (scope !== 'board') return scope === 'admin' || scope === 'member';
const user = ReactiveCache.getCurrentUser();
return !!(user && (user.isBoardAdmin() || user.isAdmin));
},
deliveryTargetId() {
return Template.instance().data.scope === 'board' ? Utils.getCurrentBoardId() : null;
},
deliveryChannels() {
return ['email', 'tray', 'webhook'].map(channel => ({ channel }));
},
// #572: every kind of card activity, ticked unless this member muted it.
isMemberScope() {
return Template.instance().data.scope === 'member';
},
activityGroupRows() {
const user = ReactiveCache.getCurrentUser();
const muted = cleanMutedGroups(user && user.profile && user.profile.notifyMutedActivities);
return GROUP_KEYS.map(key => ({ key, labelKey: `notification-activity-${key}`, enabled: !muted.includes(key) }));
},
notifyServiceRows() {
const scope = Template.instance().data.scope;
return Object.keys(NOTIFICATION_SERVICES).map(service => {
const value = currentValue(scope, service);
return {
service,
icon: service === 'email' ? 'fa-envelope' : 'fa-bell',
labelKey: service === 'email' ? 'email' : 'notification-settings-tray',
onSelected: value === true,
offSelected: value === false,
inheritSelected: value !== true && value !== false,
};
});
},
});
Template.notificationSettingsPopup.events({
async 'click .js-web-push-enable'(event, tpl) {
event.preventDefault();
try {
await require('/client/lib/webPush').enableWebPush();
tpl.webPushHere.set(true);
tpl.webPushMessage.set('');
} catch (error) {
tpl.webPushMessage.set(TAPi18n.__(error.error === 'web-push-denied' ? 'web-push-denied' : 'web-push-failed'));
}
},
async 'click .js-web-push-disable'(event, tpl) {
event.preventDefault();
try {
await require('/client/lib/webPush').disableWebPush();
tpl.webPushHere.set(false);
} catch (error) {
tpl.webPushMessage.set(TAPi18n.__('web-push-failed'));
}
},
'click .js-ntfy-enabled'(event, tpl) {
event.preventDefault();
tpl.ntfy.set({ ...tpl.ntfy.get(), enabled: !tpl.ntfy.get().enabled });
},
'click .js-ntfy-save, submit .js-ntfy-form'(event, tpl) {
event.preventDefault();
const value = { enabled: tpl.ntfy.get().enabled,
url: String(tpl.$('.js-ntfy-url').val() || ''), token: String(tpl.$('.js-ntfy-token').val() || '') };
Meteor.call('saveNtfySettings', value, (error, saved) => {
if (error) { tpl.ntfyMessage.set(TAPi18n.__(error.error) || error.reason || error.message); return; }
tpl.ntfy.set(saved);
tpl.ntfyMessage.set(TAPi18n.__('feature-saved'));
});
},
'click .js-due-reminder-off'(event, instance) {
event.preventDefault();
instance.dueRemindersOff.set(!instance.dueRemindersOff.get());
},
'click .js-due-reminder-webhook'(event, instance) {
event.preventDefault();
instance.dueReminderWebhook.set(!instance.dueReminderWebhook.get());
},
'submit .js-due-reminder-form'(event, instance) {
event.preventDefault();
const board = currentBoard();
if (!board) return;
const off = instance.dueRemindersOff.get();
const days = off ? [] : parseDueReminderInput(instance.$('.js-due-reminder-days').val());
if (days === undefined) {
instance.dueReminderMessage.set(TAPi18n.__('due-reminder-invalid'));
return;
}
const webhook = instance.dueReminderWebhook.get();
Meteor.call('setBoardDueReminders', board._id, days, webhook, error => {
instance.dueReminderMessage.set(TAPi18n.__(error ? 'due-reminder-invalid' : 'due-reminder-saved'));
});
},
'click .js-notify-activity'(event) {
event.preventDefault();
const user = ReactiveCache.getCurrentUser();
if (!user) return;
const group = event.currentTarget.dataset.group;
const muted = cleanMutedGroups(user.profile && user.profile.notifyMutedActivities);
user.setNotifyMutedActivities(muted.includes(group) ? muted.filter(key => key !== group) : [...muted, group]);
},
'click .js-notify-option'(event, instance) {
event.preventDefault();
const target = event.currentTarget;
const service = target.dataset.service;
const rawValue = target.dataset.value;
const value = rawValue === 'true' ? true : rawValue === 'false' ? false : null;
const scope = instance.data.scope;
if (scope === 'admin') {
// Inherit is not offered at admin scope - it IS the base default - so
// only true/false ever reach here.
Meteor.call('setAdminNotifyDefault', service, value === true);
return;
}
if (scope === 'board') {
const board = currentBoard();
if (board) board.setNotifyOverride(service, value);
return;
}
const user = ReactiveCache.getCurrentUser();
if (user) user.setNotifyOverride(service, value);
},
});