NotificationsBuilder.coffee 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. groupPlan: (user, licence)->
  8. key : "join-sub-#{licence.subscription_id}"
  9. create: (callback = ->)->
  10. messageOpts =
  11. groupName: licence.name
  12. subscription_id: licence.subscription_id
  13. logger.log user_id:user._id, key:@key, "creating notification key for user"
  14. NotificationsHandler.createNotification user._id, @key, "notification_group_invite", messageOpts, null, callback
  15. read: (callback = ->)->
  16. NotificationsHandler.markAsReadWithKey user._id, @key, callback
  17. projectInvite: (invite, project, sendingUser, user) ->
  18. key: "project-invite-#{invite._id}"
  19. create: (callback=()->) ->
  20. messageOpts =
  21. userName: sendingUser.first_name
  22. projectName: project.name
  23. projectId: project._id.toString()
  24. token: invite.token
  25. logger.log {user_id: user._id, project_id: project._id, invite_id: invite._id, key: @key}, "creating project invite notification for user"
  26. NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, invite.expires, callback
  27. read: (callback=()->) ->
  28. NotificationsHandler.markAsReadByKeyOnly @key, callback
  29. ipMatcherAffiliation: (userId, ip) ->
  30. key: "ip-matched-affiliation-#{ip}"
  31. create: (callback=()->) ->
  32. return null unless settings?.apis?.v1?.url # service is not configured
  33. _key = @key
  34. request {
  35. method: 'GET'
  36. url: "#{settings.apis.v1.url}/api/v2/users/#{userId}/ip_matcher"
  37. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass }
  38. body: { ip: ip }
  39. json: true
  40. timeout: 20 * 1000
  41. }, (error, response, body) ->
  42. return error if error?
  43. return null unless response.statusCode == 200
  44. messageOpts =
  45. university_id: body.id
  46. university_name: body.name
  47. content: body.enrolment_ad_html
  48. logger.log user_id:userId, key:_key, "creating notification key for user"
  49. NotificationsHandler.createNotification userId, _key, "notification_ip_matched_affiliation", messageOpts, null, false, callback
  50. read: (callback = ->)->
  51. NotificationsHandler.markAsReadWithKey userId, @key, callback