email.js 1.4 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. try {
  25. Email.send({
  26. to: user.emails[0].address.toLowerCase(),
  27. from: Accounts.emailTemplates.from,
  28. subject: TAPi18n.__('act-activity-notify', {}, user.getLanguage()),
  29. text,
  30. });
  31. } catch (e) {
  32. return;
  33. }
  34. }, 30000);
  35. });
  36. });