NotificationsHandler.coffee 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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, callback)->
  28. opts =
  29. uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
  30. timeout: oneSecond
  31. method:"POST"
  32. json: {
  33. key:key
  34. messageOpts:messageOpts
  35. templateKey:templateKey
  36. }
  37. logger.log opts:opts, "creating notification for user"
  38. makeRequest opts, callback
  39. markAsReadWithKey: (user_id, key, callback)->
  40. opts =
  41. uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
  42. method: "DELETE"
  43. timeout: oneSecond
  44. json: {
  45. key:key
  46. }
  47. logger.log user_id:user_id, key:key, "sending mark notification as read with key to notifications api"
  48. makeRequest opts, callback
  49. markAsRead: (user_id, notification_id, callback)->
  50. opts =
  51. method: "DELETE"
  52. uri: "#{settings.apis.notifications?.url}/user/#{user_id}/notification/#{notification_id}"
  53. timeout:oneSecond
  54. logger.log user_id:user_id, notification_id:notification_id, "sending mark notification as read to notifications api"
  55. makeRequest opts, callback