NotificationsHandler.coffee 2.4 KB

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