Files
wekan/server/notifications/emailQueue.js
T
Lauri Ojansivu 765e2c305d Compact terminal email metadata while retaining replay receipts
Retain detailed metadata for 30 days by default, then replace terminal jobs and commands atomically with minimal permanent identities. Preserve unfinished work, holds, generation fencing and actor-bound command replays. Sweep bounded batches independently and reconcile uncertain writes.

Validated with six Node suites, real MongoDB faults and replay tests, the Meteor startup scheduler, and Chromium Recovery replay and authorization checks.

Thanks to xet7.
2026-09-28 14:06:15 +03:00

86 lines
4.8 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { ReactiveCache } from '/imports/reactiveCache';
import EmailLocalization from '/server/lib/emailLocalization';
import { resolveNotificationSetting } from '/models/lib/notificationSettings';
import { ensureIndex } from '/server/lib/mongoStartup';
const { createEmailOutbox, migrateLegacyEmailBuffer } = require('/server/lib/emailOutbox');
const { createEmailReceiptMaintenance, emailReceiptPolicy } = require('/server/lib/emailReceiptRetention');
const { createEmailSendSlots } = require('/server/lib/emailSendSlots');
const { buildReplyToAddress } = require('/server/lib/inboundEmailReplyToken');
// Mail payloads remain private; admin methods expose only recipient summaries.
export const EmailJobs = new Mongo.Collection('notificationEmailJobs');
export const EmailLeases = new Mongo.Collection('notificationEmailLeases');
export const EmailControls = new Mongo.Collection('notificationEmailControls');
export const EmailCommands = new Mongo.Collection('notificationEmailCommands');
export const EmailSendSlots = new Mongo.Collection('notificationEmailSendSlots');
for (const collection of [EmailJobs, EmailLeases, EmailControls, EmailCommands, EmailSendSlots]) {
collection.deny({ insert: () => true, update: () => true, remove: () => true });
}
const configuredDelay = Number(process.env.EMAIL_NOTIFICATION_TIMEOUT);
const delayMs = Number.isFinite(configuredDelay) && configuredDelay > 0 ? configuredDelay : 30000;
export const emailOutbox = createEmailOutbox({
withDeliverySlot: createEmailSendSlots(EmailSendSlots.rawCollection()),
jobs: EmailJobs.rawCollection(), controls: EmailControls.rawCollection(), leases: EmailLeases.rawCollection(), delayMs,
getUser: id => ReactiveCache.getUser(id),
send: mail => EmailLocalization.sendEmail(mail),
canReceive: async (user, job) => {
// Legacy profile buffers have no board identity to re-check. New jobs
// must still belong to an active member when the deferred send occurs.
const board = job.boardId ? await ReactiveCache.getBoard(job.boardId) : null;
if (job.boardId && !board?.members?.some(member => member.userId === user._id && member.isActive === true)) return false;
if (!job.boardId) return user.profile?.notifyOverrideEmail !== false;
const setting = await ReactiveCache.getCurrentSetting();
return resolveNotificationSetting('email', { adminDefault: setting?.notifyDefaultEmail,
boardOverride: board?.notifyOverrideEmail, memberOverride: user.profile?.notifyOverrideEmail });
},
from: () => Accounts.emailTemplates.from,
replyTo: cardId => {
const secret = process.env.INBOUND_EMAIL_HMAC_SECRET, domain = process.env.INBOUND_EMAIL_DOMAIN;
if (!cardId || !secret || !domain) return '';
try { return buildReplyToAddress(cardId, secret, domain); } catch (error) { return ''; }
},
});
const receiptPolicy = emailReceiptPolicy();
export const maintainEmailReceipts = createEmailReceiptMaintenance({
jobs: EmailJobs.rawCollection(), commands: EmailCommands.rawCollection(), days: receiptPolicy.days,
});
Meteor.startup(async () => {
await ensureIndex(EmailJobs, { state: 1, nextAttemptAt: 1, _id: 1 });
await ensureIndex(EmailJobs, { userId: 1, state: 1, createdAt: 1, _id: 1 });
await ensureIndex(EmailControls, { paused: 1, _id: 1 });
await ensureIndex(EmailJobs, { state: 1, compactReceiptVersion: 1, finishedAt: 1, _id: 1 });
await ensureIndex(EmailCommands, { status: 1, compactReceiptVersion: 1, finishedAt: 1, _id: 1 });
async function maintainReceipts() {
try { await maintainEmailReceipts(); }
catch (error) { console.error('Email receipt maintenance failed; replay protection retained'); }
finally { Meteor.setTimeout(maintainReceipts, receiptPolicy.intervalMs); }
}
Meteor.setTimeout(maintainReceipts, receiptPolicy.intervalMs);
await ensureIndex(Meteor.users, { 'profile.emailBuffer.0': 1 });
// Polling discovers both new jobs and pre-restart work. Schedule after each
// pass so a slow SMTP server cannot accumulate overlapping scans.
async function scan() {
try {
const users = await Meteor.users.find({ 'profile.emailBuffer.0': { $exists: true } },
{ fields: { 'profile.emailBuffer': 1, 'profile.language': 1 }, limit: 100 }).fetchAsync();
for (const user of users) {
try {
await migrateLegacyEmailBuffer(emailOutbox, user, (id, texts) => Meteor.users.rawCollection()
.updateOne({ _id: id }, { $pullAll: { 'profile.emailBuffer': texts } }));
} catch (error) {
console.error('Legacy email buffer migration failed; original text retained');
}
}
await emailOutbox.drain();
} catch (error) {
console.error('Email outbox scan failed; pending mail will be retried');
} finally {
Meteor.setTimeout(scan, 1000);
}
}
await scan();
});