Call university ip matcher api when checking notifications

This commit is contained in:
hugh-obrien 2018-09-02 13:47:16 +01:00
parent d432b6799f
commit fa23ea75b8
2 changed files with 25 additions and 0 deletions

View file

@ -1,5 +1,7 @@
logger = require("logger-sharelatex") logger = require("logger-sharelatex")
NotificationsHandler = require("./NotificationsHandler") NotificationsHandler = require("./NotificationsHandler")
request = require "request"
settings = require "settings-sharelatex"
module.exports = module.exports =
@ -29,3 +31,16 @@ module.exports =
NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, invite.expires, callback NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, invite.expires, callback
read: (callback=()->) -> read: (callback=()->) ->
NotificationsHandler.markAsReadByKeyOnly @key, callback NotificationsHandler.markAsReadByKeyOnly @key, callback
ipMatcherAffiliation: (userId, ip) ->
return null unless settings?.apis?.v1?.url # service is not configured
request {
method: 'GET'
url: "#{settings.apis.v1.url}/api/v2/users/ip_matcher/#{userId}"
auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass }
body: { ip: ip }
json: true
timeout: 20 * 1000
}, (error, response, body) ->
return error if error?
return null if response.statusCode == 204

View file

@ -1,12 +1,22 @@
NotificationsHandler = require("./NotificationsHandler") NotificationsHandler = require("./NotificationsHandler")
NotificationsBuilder = require("./NotificationsBuilder")
AuthenticationController = require("../Authentication/AuthenticationController") AuthenticationController = require("../Authentication/AuthenticationController")
Settings = require 'settings-sharelatex'
logger = require("logger-sharelatex") logger = require("logger-sharelatex")
_ = require("underscore") _ = require("underscore")
module.exports = module.exports =
getAllUnreadNotifications: (req, res)-> getAllUnreadNotifications: (req, res)->
ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress
user_id = AuthenticationController.getLoggedInUserId(req) user_id = AuthenticationController.getLoggedInUserId(req)
# in v2 add notifications for matching university IPs
if Settings.overleaf?
NotificationsBuilder.ipMatcherAffiliation(user_id, ip)
NotificationsHandler.getUserNotifications user_id, (err, unreadNotifications)-> NotificationsHandler.getUserNotifications user_id, (err, unreadNotifications)->
unreadNotifications = _.map unreadNotifications, (notification)-> unreadNotifications = _.map unreadNotifications, (notification)->
notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts) notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)