NotificationsHandler.coffee 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. forceCreate: true
  33. }
  34. if expiryDateTime?
  35. payload.expires = expiryDateTime
  36. opts =
  37. uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
  38. timeout: oneSecond
  39. method:"POST"
  40. json: payload
  41. logger.log opts:opts, "creating notification for user"
  42. makeRequest opts, callback
  43. markAsReadWithKey: (user_id, key, callback)->
  44. opts =
  45. uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
  46. method: "DELETE"
  47. timeout: oneSecond
  48. json: {
  49. key:key
  50. }
  51. logger.log user_id:user_id, key:key, "sending mark notification as read with key to notifications api"
  52. makeRequest opts, callback
  53. markAsRead: (user_id, notification_id, callback)->
  54. opts =
  55. method: "DELETE"
  56. uri: "#{settings.apis.notifications?.url}/user/#{user_id}/notification/#{notification_id}"
  57. timeout:oneSecond
  58. logger.log user_id:user_id, notification_id:notification_id, "sending mark notification as read to notifications api"
  59. makeRequest opts, callback
  60. # removes notification by key, without regard for user_id,
  61. # should not be exposed to user via ui/router
  62. markAsReadByKeyOnly: (key, callback)->
  63. opts =
  64. uri: "#{settings.apis.notifications?.url}/key/#{key}"
  65. method: "DELETE"
  66. timeout: oneSecond
  67. logger.log {key:key}, "sending mark notification as read with key-only to notifications api"
  68. makeRequest opts, callback