NotificationsController.coffee 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. NotificationsHandler = require("./NotificationsHandler")
  2. NotificationsBuilder = require("./NotificationsBuilder")
  3. AuthenticationController = require("../Authentication/AuthenticationController")
  4. Settings = require 'settings-sharelatex'
  5. UserGetter = require("../User/UserGetter")
  6. logger = require("logger-sharelatex")
  7. async = require "async"
  8. _ = require("underscore")
  9. module.exports =
  10. getAllUnreadNotifications: (req, res)->
  11. ip = req.headers['x-forwarded-for'] ||
  12. req.connection.remoteAddress ||
  13. req.socket.remoteAddress
  14. user_id = AuthenticationController.getLoggedInUserId(req)
  15. # in v2 add notifications for matching university IPs
  16. if Settings.overleaf?
  17. UserGetter.getUser user_id, { 'lastLoginIp': 1 }, (error, user) ->
  18. if ip != user.lastLoginIp
  19. async.series ([
  20. () ->
  21. NotificationsBuilder.ipMatcherAffiliation(user_id, ip).create((err) ->
  22. return err
  23. )
  24. ])
  25. NotificationsHandler.getUserNotifications user_id, (err, unreadNotifications)->
  26. unreadNotifications = _.map unreadNotifications, (notification)->
  27. notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)
  28. return notification
  29. res.send(unreadNotifications)
  30. markNotificationAsRead: (req, res)->
  31. user_id = AuthenticationController.getLoggedInUserId(req)
  32. notification_id = req.params.notification_id
  33. NotificationsHandler.markAsRead user_id, notification_id, ->
  34. res.send()
  35. logger.log user_id:user_id, notification_id:notification_id, "mark notification as read"