notifications.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. getUsers: (watchers) => {
  19. const users = [];
  20. watchers.forEach((userId) => {
  21. const user = Users.findOne(userId);
  22. if (user) users.push(user);
  23. });
  24. return users;
  25. },
  26. notify: (user, title, description, params) => {
  27. for(const k in notifyServices) {
  28. const notifyImpl = notifyServices[k];
  29. if (notifyImpl && typeof notifyImpl === 'function') notifyImpl(user, title, description, params);
  30. }
  31. },
  32. };