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.
165 lines
7.3 KiB
JavaScript
165 lines
7.3 KiB
JavaScript
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { TAPi18n } from '/imports/i18n';
|
|
|
|
import { emailOutbox, emailDelayMs } from '/server/notifications/emailQueue';
|
|
import { Notifications } from '/server/notifications/notifications';
|
|
import { formatActivityNotificationTitle } from '/server/lib/activityNotificationTitle';
|
|
import { resolveNotificationSetting } from '/models/lib/notificationSettings';
|
|
const {
|
|
escapeEmailHtml,
|
|
safeEmailSubject,
|
|
buildHtmlNotificationLine,
|
|
} = require('/models/lib/emailNotificationSafety');
|
|
const { substituteVars } = require('/models/lib/ruleVarsSubstitute');
|
|
const {
|
|
channelOf,
|
|
groupKeyFor,
|
|
nextDeliveryAt,
|
|
redactForRecipient,
|
|
renderNotificationItem,
|
|
resolveChannelSettings,
|
|
} = require('/models/lib/notificationDelivery');
|
|
|
|
// Prepare a self-contained, rendered job without enqueuing it. Recovery planning
|
|
// can persist this result and later reuse the exact text and language.
|
|
export async function prepareActivityEmail(user, title, description, params) {
|
|
if (
|
|
!user ||
|
|
typeof user.getLanguage !== 'function'
|
|
) {
|
|
throw new Error('invalid-email-notification-user');
|
|
}
|
|
|
|
// Capture caller-owned values before the first asynchronous lookup. A saved
|
|
// job must not mix later language/profile/template changes into its content.
|
|
const userId = user._id;
|
|
const lan = user.getLanguage();
|
|
const memberOverride = user.profile && user.profile.notifyOverrideEmail;
|
|
const memberDelivery = channelOf(user.profile && user.profile.notificationDelivery, 'email');
|
|
params = structuredClone(params);
|
|
|
|
// 3-tier Notification Settings: admin default -> board override ->
|
|
// member override (see models/lib/notificationSettings.js).
|
|
const currentSetting = await ReactiveCache.getCurrentSetting();
|
|
const setting = currentSetting && {
|
|
notifyDefaultEmail: currentSetting.notifyDefaultEmail,
|
|
activityEmailSubjectTemplate: currentSetting.activityEmailSubjectTemplate,
|
|
activityEmailBodyTemplate: currentSetting.activityEmailBodyTemplate,
|
|
notificationDelivery: currentSetting.notificationDelivery,
|
|
};
|
|
const board = params.boardId
|
|
? await ReactiveCache.getBoard(params.boardId)
|
|
: null;
|
|
const enabled = resolveNotificationSetting('email', {
|
|
adminDefault: setting && setting.notifyDefaultEmail,
|
|
boardOverride: board && board.notifyOverrideEmail,
|
|
memberOverride,
|
|
});
|
|
if (!enabled) return null;
|
|
|
|
// #5171: how this recipient's e-mail is delivered - Member Settings, then
|
|
// the board, then the Admin Panel, then the built-in behaviour (the classic
|
|
// sentence, combined per recipient after EMAIL_NOTIFICATION_TIMEOUT).
|
|
const delivery = resolveChannelSettings('email', [
|
|
['member', memberDelivery],
|
|
['board', channelOf(board && board.notificationDelivery, 'email')],
|
|
['admin', channelOf(setting && setting.notificationDelivery, 'email')],
|
|
]);
|
|
// A value of an admin-only custom field (#3141) reaches only board admins.
|
|
const isBoardAdmin = !!(board && typeof board.hasAdmin === 'function' && board.hasAdmin(userId));
|
|
params = redactForRecipient(params, { isBoardAdmin });
|
|
|
|
// #5875: the server only loads the English bundle at startup, so make sure
|
|
// the recipient's language is loaded before translating the subject/body —
|
|
// otherwise notification emails silently fall back to English.
|
|
await TAPi18n.ensureLanguageLoaded(lan);
|
|
|
|
// add quote to make titles easier to read in email text
|
|
const quoteParams = { ...params };
|
|
['card', 'list', 'oldList', 'board', 'comment'].forEach(key => {
|
|
if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
|
|
});
|
|
['timeValue', 'timeOldValue'].forEach(key => {
|
|
quoteParams[key] = quoteParams[key] ? `${params[key]}` : '';
|
|
});
|
|
|
|
let subject = safeEmailSubject(formatActivityNotificationTitle(
|
|
title,
|
|
params,
|
|
TAPi18n.__.bind(TAPi18n),
|
|
lan,
|
|
));
|
|
const existing = await emailOutbox.hasPending(userId);
|
|
const actorName = params.user || '';
|
|
const descriptionText = TAPi18n.__(description, quoteParams, lan);
|
|
|
|
// #2022: an admin-customized activity-notification template (Admin
|
|
// Panel -> Email Templates) overrides the built subject/body line,
|
|
// using the same {token} substitution the #3304 rule "send email"
|
|
// action uses (substituteVars). Unset (the default on every existing
|
|
// install) falls through to the exact content built above, unchanged.
|
|
const templateSetting = setting;
|
|
const templateVars = {
|
|
board: params.board || '',
|
|
card: params.card || '',
|
|
list: params.list || '',
|
|
username: actorName,
|
|
url: params.url || '',
|
|
comment: params.comment || '',
|
|
action: descriptionText,
|
|
};
|
|
if (templateSetting && templateSetting.activityEmailSubjectTemplate) {
|
|
subject = safeEmailSubject(
|
|
substituteVars(templateSetting.activityEmailSubjectTemplate, templateVars),
|
|
);
|
|
}
|
|
const bodyTemplate = templateSetting && templateSetting.activityEmailBodyTemplate;
|
|
// #5171: the clearly arranged item - board, actor and card in bold and
|
|
// linked, only the chosen parts, with a plain-text alternative. An admin's
|
|
// own body template still wins: it is the more specific instruction.
|
|
const clear = !bodyTemplate && delivery.layout === 'clear'
|
|
? renderNotificationItem({ description, params, parts: delivery.parts,
|
|
translate: (key, values) => TAPi18n.__(key, values, lan) })
|
|
: null;
|
|
const text = bodyTemplate
|
|
? `${existing ? `\n${subject}\n` : ''}${substituteVars(bodyTemplate, templateVars)}`
|
|
: `${existing ? `\n${subject}\n` : ''}${
|
|
actorName
|
|
} ${descriptionText}\n${params.url}`;
|
|
|
|
// MailTitleBleed, again (2026-10-02): the admin's body template is trusted
|
|
// and may carry HTML, but the values substituted into it - card, board and
|
|
// list titles, the username, a comment - are written by members. Sent as
|
|
// the HTML body unescaped, markup in a card title reached other members'
|
|
// mail. The HTML body substitutes escaped values; the text body is unchanged.
|
|
const htmlVars = Object.fromEntries(Object.entries(templateVars).map(([key, value]) => [key, escapeEmailHtml(value)]));
|
|
const html = clear ? clear.html : bodyTemplate
|
|
? `${existing ? `<br/>\n${escapeEmailHtml(subject)}<br/>\n` : ''}${substituteVars(bodyTemplate, htmlVars)}`
|
|
: buildHtmlNotificationLine({
|
|
existing,
|
|
subject,
|
|
actorName,
|
|
descriptionText,
|
|
url: params.url,
|
|
});
|
|
const job = { userId, eventId: params.activityId,
|
|
subject, html, language: lan, cardId: params.cardId || null, boardId: params.boardId || null };
|
|
// #5171: only what differs from the built-in delivery is added, so a job
|
|
// under default settings is exactly the job WeKan always queued.
|
|
if (clear) job.text = clear.text;
|
|
const groupKey = delivery.from.grouping === 'default' ? null
|
|
: groupKeyFor(delivery.grouping, { boardId: job.boardId, cardId: job.cardId, eventId: job.eventId });
|
|
if (groupKey && delivery.grouping !== 'all') job.groupKey = groupKey;
|
|
if (delivery.schedule !== 'immediate' || delivery.quietStart) {
|
|
job.deliverAt = nextDeliveryAt(new Date(), delivery, { baseDelayMs: emailDelayMs }).getTime();
|
|
}
|
|
return job;
|
|
}
|
|
|
|
Meteor.startup(() => {
|
|
Notifications.subscribe('email', async (user, title, description, params) => {
|
|
const job = await prepareActivityEmail(user, title, description, params);
|
|
if (job) await emailOutbox.enqueue(job);
|
|
});
|
|
});
|