mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
One delivery model (models/lib/notificationDelivery.js) for every channel - e-mail, the notification tray and outgoing webhooks - set in the Notifications popup at Admin Panel, board and Member Settings scope, and in each one-way webhook's form: - Content. Webhooks: the text on or off, and which property groups are sent (standard, ids, names, links, actor, people, details), with checklist, list, swimlane and board names, before/after values, the card description and links added as opt-in groups. E-mail and tray: the classic sentence or a clear layout - the board as a link first, the actor and card in bold - and which parts are shown. - Grouping: one message per event, per card, per board, or all together. - Scheduling: immediately, every 5 minutes to 24 hours, or once a day, with optional quiet hours, in the time zone of whoever set it. The most specific explicit setting wins: member, then board, then Admin Panel for e-mail and tray; the webhook, then its board, then Admin Panel for webhooks. A member can only leave their own name out of outgoing webhooks. The built-in defaults keep today's behaviour. Grouped and scheduled webhooks and e-mail go through durable queues that survive restarts; queued webhooks use the SSRF guard and log a blocked address in Admin Panel - Problems. Values of admin-only custom fields now reach only board admins in e-mail and tray, and never a webhook. The new strings are in English in every locale, pending Transifex. Thanks to HaKePlan, Meeques and xet7 ! Fixes #3695, Fixes #5171.
122 lines
5.9 KiB
JavaScript
122 lines
5.9 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { check, Match } from 'meteor/check';
|
|
import Boards from '/models/boards';
|
|
import Integrations from '/models/integrations';
|
|
import Settings from '/models/settings';
|
|
import { allowIsBoardAdminOrSiteAdmin } from '/server/lib/utils';
|
|
const { CHANNELS, MEMBER_CHANNELS, normalizeChannelSettings } = require('/models/lib/notificationDelivery');
|
|
const { normalizeMemberWebhookSettings } = require('/models/lib/webhookPayload');
|
|
|
|
// #3695 / #5171: notification delivery (content, grouping, schedule) per
|
|
// channel, set from the three Notification Settings places and each webhook's
|
|
// own form. models/lib/notificationDelivery.js says how the levels combine.
|
|
// Every value is validated by that pure module before anything is written.
|
|
//
|
|
// scope 'admin' instance default, any channel - instance admins only
|
|
// scope 'board' targetId = board id, any channel - that board's admins (or an instance admin)
|
|
// scope 'integration' targetId = integration id,
|
|
// channel 'webhook' - the webhook's board admins; a global
|
|
// webhook only instance admins
|
|
// scope 'member' the CALLER's own profile:
|
|
// channel 'email' | 'tray' - their own delivery
|
|
// channel 'webhook' - only { hideIdentity }: a member cannot
|
|
// change a board's webhooks
|
|
//
|
|
// `settings` is a CHANGE to that level: each key given is set, a key given as
|
|
// null is cleared (inherits), keys not given are kept. The merge happens here,
|
|
// on the stored value, so two quick changes from one form never overwrite
|
|
// each other with a stale copy. `settings` null clears the whole level.
|
|
const SCOPES = ['admin', 'board', 'integration', 'member'];
|
|
|
|
function invalid(e) {
|
|
return new Meteor.Error('invalid-notification-delivery', e && e.message);
|
|
}
|
|
|
|
Meteor.methods({
|
|
async setNotificationDelivery(scope, targetId, channel, settings) {
|
|
check(scope, String);
|
|
check(targetId, Match.Maybe(String));
|
|
check(channel, String);
|
|
check(settings, Match.Maybe(Object));
|
|
if (!this.userId) throw new Meteor.Error('not-authorized');
|
|
if (!SCOPES.includes(scope)) throw new Meteor.Error('invalid-scope');
|
|
if (!CHANNELS.includes(channel)) throw new Meteor.Error('invalid-channel');
|
|
|
|
if (scope === 'member') {
|
|
if (targetId && targetId !== this.userId) throw new Meteor.Error('not-authorized');
|
|
if (channel === 'webhook') {
|
|
let member;
|
|
try { member = normalizeMemberWebhookSettings(settings || {}); } catch (e) { throw invalid(e); }
|
|
await Meteor.users.updateAsync(this.userId, member.hideIdentity
|
|
? { $set: { 'profile.webhookHideIdentity': true } }
|
|
: { $unset: { 'profile.webhookHideIdentity': '' } });
|
|
return member;
|
|
}
|
|
if (!MEMBER_CHANNELS.includes(channel)) throw new Meteor.Error('invalid-channel');
|
|
}
|
|
if (scope === 'integration' && channel !== 'webhook') throw new Meteor.Error('invalid-channel');
|
|
|
|
// Validate the change itself first, so an unknown key or value is
|
|
// refused even when it would be merged away.
|
|
try { normalizeChannelSettings(channel, settings); } catch (e) { throw invalid(e); }
|
|
const prefix = scope === 'member' ? 'profile.notificationDelivery' : 'notificationDelivery';
|
|
const field = `${prefix}.${channel}`;
|
|
const merged = stored => {
|
|
if (settings === null) return null;
|
|
const next = { ...(stored || {}) };
|
|
for (const [key, value] of Object.entries(settings)) {
|
|
if (value === null || value === undefined) delete next[key];
|
|
else next[key] = value;
|
|
}
|
|
try { return normalizeChannelSettings(channel, next); } catch (e) { throw invalid(e); }
|
|
};
|
|
const modifierFor = normalized => (normalized ? { $set: { [field]: normalized } } : { $unset: { [field]: '' } });
|
|
|
|
if (scope === 'member') {
|
|
const me = await Meteor.users.findOneAsync(this.userId, { fields: { 'profile.notificationDelivery': 1 } });
|
|
const normalized = merged(me?.profile?.notificationDelivery?.[channel]);
|
|
await Meteor.users.updateAsync(this.userId, modifierFor(normalized));
|
|
return normalized;
|
|
}
|
|
|
|
const user = await Meteor.users.findOneAsync(this.userId, { fields: { isAdmin: 1 } });
|
|
const isInstanceAdmin = user?.isAdmin === true;
|
|
|
|
if (scope === 'admin') {
|
|
if (!isInstanceAdmin) throw new Meteor.Error('not-authorized');
|
|
const setting = await Settings.findOneAsync({});
|
|
if (!setting) throw new Meteor.Error('settings-not-found');
|
|
const normalized = merged(setting.notificationDelivery?.[channel]);
|
|
await Settings.updateAsync(setting._id, modifierFor(normalized));
|
|
return normalized;
|
|
}
|
|
|
|
if (!targetId) throw new Meteor.Error('not-found');
|
|
|
|
if (scope === 'board') {
|
|
const board = await Boards.findOneAsync(targetId);
|
|
if (!board || !(await allowIsBoardAdminOrSiteAdmin(this.userId, board))) {
|
|
throw new Meteor.Error('not-authorized');
|
|
}
|
|
const normalized = merged(board.notificationDelivery?.[channel]);
|
|
await Boards.updateAsync(targetId, modifierFor(normalized));
|
|
return normalized;
|
|
}
|
|
|
|
// scope === 'integration'
|
|
const integration = await Integrations.findOneAsync(targetId);
|
|
if (!integration) throw new Meteor.Error('not-authorized');
|
|
if (integration.boardId === Integrations.Const.GLOBAL_WEBHOOK_ID) {
|
|
if (!isInstanceAdmin) throw new Meteor.Error('not-authorized');
|
|
} else {
|
|
const board = await Boards.findOneAsync(integration.boardId);
|
|
if (!board || !(await allowIsBoardAdminOrSiteAdmin(this.userId, board))) {
|
|
throw new Meteor.Error('not-authorized');
|
|
}
|
|
}
|
|
const normalized = merged(integration.notificationDelivery?.webhook);
|
|
await Integrations.updateAsync({ _id: targetId, boardId: integration.boardId }, modifierFor(normalized));
|
|
return normalized;
|
|
},
|
|
});
|