NotificationsHandler.coffee 2.4 KB

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