NotificationsBuilder.coffee 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, ip) ->
  36. key: "ip-matched-affiliation-#{ip}"
  37. create: (callback=()->) ->
  38. return null unless settings?.apis?.v1?.url # service is not configured
  39. _key = @key
  40. request {
  41. method: 'GET'
  42. url: "#{settings.apis.v1.url}/api/v2/users/#{userId}/ip_matcher"
  43. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass }
  44. body: { ip: ip }
  45. json: true
  46. timeout: 20 * 1000
  47. }, (error, response, body) ->
  48. return error if error?
  49. return null unless response.statusCode == 200
  50. messageOpts =
  51. university_id: body.id
  52. university_name: body.name
  53. content: body.enrolment_ad_html
  54. logger.log user_id:userId, key:_key, "creating notification key for user"
  55. NotificationsHandler.createNotification userId, _key, "notification_ip_matched_affiliation", messageOpts, null, false, callback
  56. read: (callback = ->)->
  57. NotificationsHandler.markAsReadWithKey userId, @key, callback