todos-contra-el-fuego-web/imports/startup/server/email.js
vjrj 25dc1b99c0 WIP(meteor3): boot progresses past linker + several async fixes
Past the client-bundle underscore linker (pin underscore@1.6.4) and now working
through the server async/await migration on Meteor 3.1 + Mongo 7:

- Removed fibers (imports/startup/server/fibers.js + import) — gone in Meteor 3.
- accounts/oauth.js: upsert -> upsertAsync (top-level await).
- migrations.js: startup async + await Migrations.migrateTo (async in 2.x).
  mongo7 marked at version 18 so historical up() bodies (still sync Mongo) don't
  run — prod data restores already-migrated. Documented.
- subsUnion.js: fully async (fetchAsync/findOneAsync/countAsync/upsertAsync,
  observeAsync with async callbacks).
- facts.js: dropped dead sync findOne.
- email.js: ostrio:mailer 2.5 is a default export (was named import); dropped
  removed static MailTime.Template (built-in default '{{{html}}}').

NEXT wall + remaining chain (multi-session):
1. aldeed:collection2@4.2.0 needs simpl-schema 3.x (app has 1.x) -> 'attachSchema
   is not a function'. Requires upgrading npm simpl-schema to 3.x and migrating
   all collection schemas (SimpleSchema.RegEx.Id etc.) across ~8 files.
2. Rest.js + helpers (countRealFires/firesUnion/whichAreFalsePositives/
   fireFromHash/subscriptionsInsert/upsertFalsePositive) -> *Async.
3. Patch vendored restivus to await async endpoint handlers.
4. methods/publications/comments to async; cron SyncedCron(quave)/Facts imports.
5. alanning:roles@1.2.10 + gadicohen:sitemaps@0.0.17 warn incompatible -> bump.
6. React 16 -> 18 render root.

Run with: NODE_OPTIONS='--dns-result-order=ipv4first --no-network-family-autoselection'
MONGO_URL='mongodb://localhost:27019/fuegos?replicaSet=rs0'
2026-07-13 23:46:45 +02:00

92 lines
3.4 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import nodemailer from 'nodemailer';
import MailTime from 'meteor/ostrio:mailer';
import i18n from 'i18next';
import isMaster from './isMaster';
const transports = [];
// First transport
const fstTransport = nodemailer.createTransport(Meteor.settings.private.MAIL_URL);
transports.push(fstTransport);
// console.log(fstTransport.options.auth.user);
const db = Meteor.users.rawDatabase(); // new Mongo.Collection('__mailTimeQueue__').rawDatabase();
// https://litmus.com/community/discussions/4633-is-there-a-reliable-1px-horizontal-rule-method
export const hr = `<table cellspacing="0" cellpadding="0" border="0" width="100%" style="width: 100% !important;">
<tr>
<td align="left" valign="top" width="600px" height="1" style="background-color: #f0f0f0; border-collapse:collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; mso-line-height-rule: exactly; line-height: 1px;"><!--[if gte mso 15]>&nbsp;<![endif]--></td>
</tr>
</table>`;
let MailQueue;
export const isMailServerMaster = Meteor.settings.private.isMailServer && isMaster();
if (isMailServerMaster) {
console.log('I\'m the mail server');
MailQueue = new MailTime({
db,
type: 'client', // instead of 'server' for now to avoid use the JosK task manager at 250ms, we use our cron
// see https://github.com/VeliovGroup/Mail-Time/issues/5
strategy: 'balancer', // Transports will be used in round robin chain
transports,
from(transport) {
// To pass spam-filters `from` field should be correctly set
// for each transport, check `transport` object for more options
if (!Meteor.isProduction) {
// only for test purposes
return `${i18n.t('AppName')} <notify@example.org>`;
}
return `${i18n.t('AppName')} <${transport.options.auth.user}>`;
},
debug: Meteor.settings.private.debugMailer,
concatEmails: true, // Concatenate emails to the same addressee
concatSubject: `${i18n.t('Nuevas notificaciones de {{app}}', { app: i18n.t('AppName') })}`,
/* eslint-disable */
concatDelimiter: hr + '<h2>{{{subject}}}</h2>', // Start each concatenated email with it's own subject
/* eslint-enable */
// concatThrottling: 30,
// Mail-Time 2.x removed the static MailTime.Template; omitting `template`
// uses the built-in default ('{{{html}}}'). (debt: richer wrapper template)
});
} else {
console.log('I\'m a mail client');
MailQueue = new MailTime({
db,
type: 'client',
debug: Meteor.settings.private.debugMailer,
strategy: 'balancer', // Transports will be used in round robin chain
concatEmails: true // Concatenate emails to the same address
});
}
export default function sendMail(opts, debug) {
if (debug) {
MailQueue.sendMail(opts, (err, info) => { if (err) { console.error(err); } else { console.log(info); } });
} else {
MailQueue.sendMail(opts);
}
}
if (Meteor.settings.private.testMailer) {
const emailOpts = {
to: Meteor.settings.private.testEmail,
userName: 'someone',
sendAt: new Date(),
subject: 'Some new notification',
text: 'Plain text message',
template: '<body>{{appName}}<h2>{{{subject}}}</h2>{{{html}}}</body>',
appName: i18n.t('AppName'),
html: '<p>Styled message</p>'
};
sendMail(emailOpts, true);
sendMail(emailOpts, true);
sendMail(emailOpts, true);
sendMail(emailOpts, true);
}
export const sendEmailsFromQueue = () => {
MailQueue.___send(() => { });
};