NotificationsHandler.coffee 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. settings = require("settings-sharelatex")
  2. request = require("request")
  3. logger = require("logger-sharelatex")
  4. oneSecond = 1000
  5. module.exports =
  6. getUserNotifications: (user_id, callback)->
  7. opts =
  8. uri: "#{settings.apis.notifications.url}/user/#{user_id}"
  9. json: true
  10. timeout: 2000
  11. request.get opts, (err, res, unreadNotifications)->
  12. statusCode = if res? then res.statusCode else 500
  13. if err? or statusCode != 200
  14. e = new Error("something went wrong getting notifications, #{err}, #{statusCode}")
  15. logger.err err:err, "something went wrong getting notifications"
  16. callback(null, [])
  17. else
  18. if !unreadNotifications?
  19. unreadNotifications = []
  20. callback(null, unreadNotifications)
  21. createNotification: (user_id, key, templateKey, messageOpts, callback)->
  22. opts =
  23. uri: "#{settings.apis.notifications.url}/user/#{user_id}"
  24. timeout: 1000
  25. json: {
  26. key:key
  27. messageOpts:messageOpts
  28. templateKey:templateKey
  29. }
  30. request.post opts, callback
  31. markAsReadWithKey: (user_id, key, callback)->
  32. opts =
  33. uri: "#{settings.apis.notifications.url}/user/#{user_id}"
  34. timeout: 1000
  35. json: {
  36. key:key
  37. }
  38. request.del opts, callback
  39. markAsRead: (user_id, notification_id, callback)->
  40. opts =
  41. uri: "#{settings.apis.notifications.url}/user/#{user_id}/notification/#{notification_id}"
  42. timeout:oneSecond
  43. logger.log user_id:user_id, notification_id:notification_id, "send mark notification to notifications api"
  44. request.del opts, callback