2
0

notifications.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // a map of notification service, like email, web, IM, qq, etc.
  2. // serviceName -> callback(user, title, description, params)
  3. // expected arguments to callback:
  4. // - user: Meteor user object
  5. // - title: String, TAPi18n key
  6. // - description, String, TAPi18n key
  7. // - params: Object, values extracted from context, to used for above two TAPi18n keys
  8. // see example call to Notifications.notify() in models/activities.js
  9. const notifyServices = {};
  10. Notifications = {
  11. subscribe: (serviceName, callback) => {
  12. notifyServices[serviceName] = callback;
  13. },
  14. unsubscribe: (serviceName) => {
  15. if (typeof notifyServices[serviceName] === 'function')
  16. delete notifyServices[serviceName];
  17. },
  18. // filter recipients according to user settings for notification
  19. getUsers: (participants, watchers) => {
  20. const userMap = {};
  21. participants.forEach((userId) => {
  22. if (userMap[userId]) return;
  23. const user = Users.findOne(userId);
  24. if (user && user.hasTag('notify-participate')) {
  25. userMap[userId] = user;
  26. }
  27. });
  28. watchers.forEach((userId) => {
  29. if (userMap[userId]) return;
  30. const user = Users.findOne(userId);
  31. if (user && user.hasTag('notify-watch')) {
  32. userMap[userId] = user;
  33. }
  34. });
  35. return _.map(userMap, (v) => v);
  36. },
  37. notify: (user, title, description, params) => {
  38. for(const k in notifyServices) {
  39. const notifyImpl = notifyServices[k];
  40. if (notifyImpl && typeof notifyImpl === 'function') notifyImpl(user, title, description, params);
  41. }
  42. },
  43. };