Files
wekan/server/notifications/trayQueue.js
T
Lauri Ojansivu ab785acd1e Choose what notifications contain, and group and schedule them, for e-mail, the tray and webhooks.
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.
2026-10-10 15:28:26 +03:00

70 lines
3.4 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import Users from '/models/users';
import Activities from '/models/activities';
import Boards from '/models/boards';
import Settings from '/models/settings';
const { channelOf, nextDeliveryAt, resolveChannelSettings } = require('/models/lib/notificationDelivery');
import { ensureIndex } from '/server/lib/mongoStartup';
const { createTrayDelivery, PENDING } = require('/server/lib/trayDelivery');
// Receipts outlive visible notifications and have no TTL. Never publish them
// or permit client mutation; the user's transient marker is server-owned too.
export const trayDeliveryReceipts = new Mongo.Collection('notificationTrayReceipts');
trayDeliveryReceipts.deny({ insert: () => true, update: () => true, remove: () => true });
const trayReceipts = createTrayDelivery({
users: Users.rawCollection(), receipts: trayDeliveryReceipts.rawCollection(),
});
// #5171: the time a tray entry starts showing, from the recipient's tray
// schedule (Member Settings -> board -> Admin Panel -> built-in "at once").
// Computed from the activity's own time, so a retried delivery stamps the
// same value, and stored on the entry, so a restart changes nothing.
export async function trayShowAt(userId, activityId) {
const [user, activity, setting] = await Promise.all([
Users.findOneAsync(userId, { fields: { 'profile.notificationDelivery': 1 } }),
Activities.findOneAsync(activityId, { fields: { createdAt: 1, boardId: 1 } }),
Settings.findOneAsync({}, { fields: { notificationDelivery: 1 } }),
]);
const board = activity && activity.boardId
? await Boards.findOneAsync(activity.boardId, { fields: { notificationDelivery: 1 } }) : null;
const delivery = resolveChannelSettings('tray', [
['member', channelOf(user && user.profile && user.profile.notificationDelivery, 'tray')],
['board', channelOf(board && board.notificationDelivery, 'tray')],
['admin', channelOf(setting && setting.notificationDelivery, 'tray')],
]);
if (delivery.schedule === 'immediate' && !delivery.quietStart) return undefined;
const createdAt = activity && activity.createdAt ? new Date(activity.createdAt) : new Date();
return nextDeliveryAt(createdAt, delivery).getTime();
}
export const trayDelivery = {
async deliver(userId, activityId) {
const showAt = await trayShowAt(userId, activityId);
return trayReceipts.deliver(userId, activityId, showAt === undefined ? {} : { showAt });
},
recover: userId => trayReceipts.recover(userId),
};
let afterId = null;
export async function recoverTrayDeliveries() {
const rows = await Users.rawCollection().find({ [PENDING]: { $exists: true },
...(afterId ? { _id: { $gt: afterId } } : {}) }, { projection: { _id: 1 } })
.sort({ _id: 1 }).limit(100).toArray();
for (const row of rows) {
try { await trayDelivery.recover(row._id); }
catch (error) { console.error('Tray receipt recovery failed; pending evidence retained'); }
}
// Advance past damaged rows so they cannot starve later recipients.
afterId = rows.length === 100 ? rows[rows.length - 1]._id : null;
}
Meteor.startup(async () => {
await ensureIndex(trayDeliveryReceipts, { userId: 1 });
await ensureIndex(Users, { [PENDING]: 1 }, { sparse: true });
async function scan() {
try { await recoverTrayDeliveries(); }
catch (error) { console.error('Tray receipt scan failed; recovery will retry'); }
finally { Meteor.setTimeout(scan, 1000); }
}
await scan();
});