NotificationsBuilder.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. logger = require("logger-sharelatex")
  2. NotificationsHandler = require("./NotificationsHandler")
  3. request = require "request"
  4. settings = require "settings-sharelatex"
  5. module.exports =
  6. # Note: notification keys should be url-safe
  7. featuresUpgradedByAffiliation: (affiliation, user) ->
  8. key: "features-updated-by=#{affiliation.institutionId}"
  9. create: (callback=()->) ->
  10. messageOpts =
  11. institutionName: affiliation.institutionName
  12. NotificationsHandler.createNotification user._id, @key, "notification_features_upgraded_by_affiliation", messageOpts, null, false, callback
  13. read: (callback=()->) ->
  14. NotificationsHandler.markAsRead @key, callback
  15. redundantPersonalSubscription: (affiliation, user) ->
  16. key: "redundant-personal-subscription-#{affiliation.institutionId}"
  17. create: (callback=()->) ->
  18. messageOpts =
  19. institutionName: affiliation.institutionName
  20. NotificationsHandler.createNotification user._id, @key, "notification_personal_subscription_not_required_due_to_affiliation", messageOpts, null, false, callback
  21. read: (callback=()->) ->
  22. NotificationsHandler.markAsRead @key, callback
  23. projectInvite: (invite, project, sendingUser, user) ->
  24. key: "project-invite-#{invite._id}"
  25. create: (callback=()->) ->
  26. messageOpts =
  27. userName: sendingUser.first_name
  28. projectName: project.name
  29. projectId: project._id.toString()
  30. token: invite.token
  31. logger.log {user_id: user._id, project_id: project._id, invite_id: invite._id, key: @key}, "creating project invite notification for user"
  32. NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, invite.expires, callback
  33. read: (callback=()->) ->
  34. NotificationsHandler.markAsReadByKeyOnly @key, callback
  35. ipMatcherAffiliation: (userId) ->
  36. create: (ip, callback=()->) ->
  37. return null unless settings?.apis?.v1?.url # service is not configured
  38. request {
  39. method: 'GET'
  40. url: "#{settings.apis.v1.url}/api/v2/users/#{userId}/ip_matcher"
  41. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass }
  42. body: { ip: ip }
  43. json: true
  44. timeout: 20 * 1000
  45. }, (error, response, body) ->
  46. return error if error?
  47. return null unless response.statusCode == 200
  48. key = "ip-matched-affiliation-#{body.id}"
  49. messageOpts =
  50. university_name: body.name
  51. content: body.enrolment_ad_html
  52. logger.log user_id:userId, key:key, "creating notification key for user"
  53. NotificationsHandler.createNotification userId, key, "notification_ip_matched_affiliation", messageOpts, null, false, callback
  54. read: (university_id, callback = ->)->
  55. key = "ip-matched-affiliation-#{university_id}"
  56. NotificationsHandler.markAsReadWithKey userId, key, callback