NotificationsHandler.coffee 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. settings = require("settings-sharelatex")
  2. request = require("request")
  3. logger = require("logger-sharelatex")
  4. oneSecond = 1000
  5. makeRequest = (opts, callback)->
  6. if !settings.apis.notifications?.url?
  7. return callback(null, statusCode:200)
  8. else
  9. request(opts, callback)
  10. module.exports =
  11. getUserNotifications: (user_id, callback)->
  12. opts =
  13. uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
  14. json: true
  15. timeout: oneSecond
  16. method: "GET"
  17. makeRequest opts, (err, res, unreadNotifications)->
  18. statusCode = if res? then res.statusCode else 500
  19. if err? or statusCode != 200
  20. e = new Error("something went wrong getting notifications, #{err}, #{statusCode}")
  21. logger.err err:err, "something went wrong getting notifications"
  22. callback(null, [])
  23. else
  24. if !unreadNotifications?
  25. unreadNotifications = []
  26. callback(null, unreadNotifications)
  27. createNotification: (user_id, key, templateKey, messageOpts, expiryDateTime, callback)->
  28. payload = {
  29. key:key
  30. messageOpts:messageOpts
  31. templateKey:templateKey
  32. }
  33. if expiryDateTime?
  34. payload.expires = expiryDateTime
  35. opts =
  36. uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
  37. timeout: oneSecond
  38. method:"POST"
  39. json: payload
  40. logger.log opts:opts, "creating notification for user"
  41. makeRequest opts, callback
  42. markAsReadWithKey: (user_id, key, callback)->
  43. opts =
  44. uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
  45. method: "DELETE"
  46. timeout: oneSecond
  47. json: {
  48. key:key
  49. }
  50. logger.log user_id:user_id, key:key, "sending mark notification as read with key to notifications api"
  51. makeRequest opts, callback
  52. markAsRead: (user_id, notification_id, callback)->
  53. opts =
  54. method: "DELETE"
  55. uri: "#{settings.apis.notifications?.url}/user/#{user_id}/notification/#{notification_id}"
  56. timeout:oneSecond
  57. logger.log user_id:user_id, notification_id:notification_id, "sending mark notification as read to notifications api"
  58. makeRequest opts, callback
  59. # removes notification by key, without regard for user_id,
  60. # should not be exposed to user via ui/router
  61. markAsReadByKeyOnly: (key, callback)->
  62. opts =
  63. uri: "#{settings.apis.notifications?.url}/key/#{key}"
  64. method: "DELETE"
  65. timeout: oneSecond
  66. logger.log {key:key}, "sending mark notification as read with key-only to notifications api"
  67. makeRequest opts, callback