NotificationsController.coffee 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. console.log(user.lastLoginIp)
  19. if ip != user.lastLoginIp
  20. async.series ([
  21. () ->
  22. NotificationsBuilder.ipMatcherAffiliation(user_id, ip).create((err) ->
  23. return err
  24. )
  25. ])
  26. NotificationsHandler.getUserNotifications user_id, (err, unreadNotifications)->
  27. unreadNotifications = _.map unreadNotifications, (notification)->
  28. notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)
  29. return notification
  30. res.send(unreadNotifications)
  31. markNotificationAsRead: (req, res)->
  32. user_id = AuthenticationController.getLoggedInUserId(req)
  33. notification_id = req.params.notification_id
  34. NotificationsHandler.markAsRead user_id, notification_id, ->
  35. res.send()
  36. logger.log user_id:user_id, notification_id:notification_id, "mark notification as read"