notifications.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 = ReactiveCache.getUser(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')
  30. notifyImpl(user, title, description, params);
  31. }
  32. },
  33. };