NotificationsBuilder.coffee 1.8 KB

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