email.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // buffer each user's email text in a queue, then flush them in single email
  2. Meteor.startup(() => {
  3. Notifications.subscribe('email', (user, title, description, params) => {
  4. // add quote to make titles easier to read in email text
  5. const quoteParams = _.clone(params);
  6. ['card', 'list', 'oldList', 'board', 'comment'].forEach((key) => {
  7. if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
  8. });
  9. const text = `${params.user} ${TAPi18n.__(description, quoteParams, user.getLanguage())}\n${params.url}`;
  10. user.addEmailBuffer(text);
  11. // unlike setTimeout(func, delay, args),
  12. // Meteor.setTimeout(func, delay) does not accept args :-(
  13. // so we pass userId with closure
  14. const userId = user._id;
  15. Meteor.setTimeout(() => {
  16. const user = Users.findOne(userId);
  17. // for each user, in the timed period, only the first call will get the cached content,
  18. // other calls will get nothing
  19. const texts = user.getEmailBuffer();
  20. if (texts.length === 0) return;
  21. // merge the cached content into single email and flush
  22. const text = texts.join('\n\n');
  23. user.clearEmailBuffer();
  24. if (Settings.findOne().mailUrl()) {
  25. try {
  26. Email.send({
  27. to: user.emails[0].address.toLowerCase(),
  28. from: Accounts.emailTemplates.from,
  29. subject: TAPi18n.__('act-activity-notify', {}, user.getLanguage()),
  30. text,
  31. });
  32. } catch (e) {
  33. return;
  34. }
  35. }
  36. }, 30000);
  37. });
  38. });