email.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // cache the email text in a queue, and send them in a batch
  2. Meteor.startup(() => {
  3. Notifications.subscribe('cachedEmail', (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.addEmailCache(text);
  11. const userId = user._id;
  12. Meteor.setTimeout(() => {
  13. const user = Users.findOne(userId);
  14. const emailCache = user.getEmailCache();
  15. if (emailCache.length === 0) return;
  16. const text = emailCache.join('\n\n');
  17. user.clearEmailCache();
  18. try {
  19. Email.send({
  20. to: user.emails[0].address,
  21. from: Accounts.emailTemplates.from,
  22. subject : TAPi18n.__('act-activity-notify', {}, user.getLanguage()),
  23. text,
  24. });
  25. } catch (e) {
  26. return;
  27. }
  28. }, 30000, user._id);
  29. });
  30. });