todos-contra-el-fuego-web/imports/api/Comments/server/onCommentAdd.js
vjrj 66bab7a43b meteor3: Comments methods and mail fan-out to async
comments.insert/edit/remove/like/dislike use *Async collection APIs;
requireOwner/toggle helpers are async; onCommentAdd iterates users with
forEachAsync. subscriptions.* methods were already async. REST smoke stays
byte-identical (12/12).
2026-07-14 06:52:32 +02:00

42 lines
1.6 KiB
JavaScript

/* eslint-disable import/no-absolute-path */
import { Meteor } from 'meteor/meteor';
import i18n from 'i18next';
import sendEmail, { subjectTruncate } from '/imports/modules/server/send-email';
import getEmailOf from '/imports/modules/get-email-of-user';
import Comments from '/imports/api/Comments/Comments';
/*
* When a comment is added to a fire, email the other users who commented on the
* same fire (uniq, excluding the author). Ported from the old
* startup/server/comments.js `onEvent` handler.
*/
export default function onCommentAdd(payload) {
const { referenceId, userId } = payload;
const query = { referenceId, userId: { $ne: userId } };
Comments.rawCollection().distinct('userId', query).then(async (users) => {
const path = referenceId.replace(/fire-/, 'fire/archive/');
const fireUrl = Meteor.absoluteUrl(path);
await Meteor.users.find({ _id: { $in: users } }).forEachAsync((user) => {
const { firstName, emailAddress } = getEmailOf(user);
if (emailAddress) {
const emailOpts = {
to: emailAddress,
subject: subjectTruncate.apply(i18n.t('Hay más información sobre un fuego')),
lang: user.lang,
template: 'new-fire-comment',
templateVars: {
applicationName: i18n.t('AppName'),
firstName,
fireUrl
}
};
sendEmail(emailOpts).catch((error) => {
console.warn(`comments new-fire-comment mail failed: ${error}`);
});
}
});
}).catch((error) => {
console.warn(`comments distinct() failed: ${error}`);
});
}