mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-12 07:30:46 +02:00
Merge pull request #7106 from overleaf/ab-analytics-controller-async
Convert AnalyticsController to async/await GitOrigin-RevId: a38194b2970a15b06fe6a3d95048681d7927bfc3
This commit is contained in:
@@ -4,7 +4,48 @@ const SessionManager = require('../Authentication/SessionManager')
|
||||
const GeoIpLookup = require('../../infrastructure/GeoIpLookup')
|
||||
const Features = require('../../infrastructure/Features')
|
||||
|
||||
const getSegmentation = req => {
|
||||
async function updateEditingSession(req, res, next) {
|
||||
if (!Features.hasFeature('analytics')) {
|
||||
return res.sendStatus(202)
|
||||
}
|
||||
const userId = SessionManager.getLoggedInUserId(req.session)
|
||||
const { projectId } = req.params
|
||||
const segmentation = _getSegmentation(req)
|
||||
let countryCode = null
|
||||
|
||||
if (userId) {
|
||||
try {
|
||||
const geoDetails = await GeoIpLookup.promises.getDetails(req.ip)
|
||||
if (geoDetails && geoDetails.country_code) {
|
||||
countryCode = geoDetails.country_code
|
||||
}
|
||||
AnalyticsManager.updateEditingSession(
|
||||
userId,
|
||||
projectId,
|
||||
countryCode,
|
||||
segmentation
|
||||
)
|
||||
} catch (error) {
|
||||
metrics.inc('analytics_geo_ip_lookup_errors')
|
||||
}
|
||||
}
|
||||
res.sendStatus(202)
|
||||
}
|
||||
|
||||
function recordEvent(req, res, next) {
|
||||
if (!Features.hasFeature('analytics')) {
|
||||
return res.sendStatus(202)
|
||||
}
|
||||
delete req.body._csrf
|
||||
AnalyticsManager.recordEventForSession(
|
||||
req.session,
|
||||
req.params.event,
|
||||
req.body
|
||||
)
|
||||
res.sendStatus(202)
|
||||
}
|
||||
|
||||
function _getSegmentation(req) {
|
||||
const segmentation = req.body ? req.body.segmentation : null
|
||||
const cleanedSegmentation = {}
|
||||
if (
|
||||
@@ -19,43 +60,6 @@ const getSegmentation = req => {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
updateEditingSession(req, res, next) {
|
||||
if (!Features.hasFeature('analytics')) {
|
||||
return res.sendStatus(202)
|
||||
}
|
||||
const userId = SessionManager.getLoggedInUserId(req.session)
|
||||
const { projectId } = req.params
|
||||
const segmentation = getSegmentation(req)
|
||||
let countryCode = null
|
||||
|
||||
if (userId) {
|
||||
GeoIpLookup.getDetails(req.ip, function (err, geoDetails) {
|
||||
if (err) {
|
||||
metrics.inc('analytics_geo_ip_lookup_errors')
|
||||
} else if (geoDetails && geoDetails.country_code) {
|
||||
countryCode = geoDetails.country_code
|
||||
}
|
||||
AnalyticsManager.updateEditingSession(
|
||||
userId,
|
||||
projectId,
|
||||
countryCode,
|
||||
segmentation
|
||||
)
|
||||
})
|
||||
}
|
||||
res.sendStatus(202)
|
||||
},
|
||||
|
||||
recordEvent(req, res, next) {
|
||||
if (!Features.hasFeature('analytics')) {
|
||||
return res.sendStatus(202)
|
||||
}
|
||||
delete req.body._csrf
|
||||
AnalyticsManager.recordEventForSession(
|
||||
req.session,
|
||||
req.params.event,
|
||||
req.body
|
||||
)
|
||||
res.sendStatus(202)
|
||||
},
|
||||
updateEditingSession,
|
||||
recordEvent,
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ const AuthenticationController = require('./../Authentication/AuthenticationCont
|
||||
const AnalyticsController = require('./AnalyticsController')
|
||||
const AnalyticsProxy = require('./AnalyticsProxy')
|
||||
const RateLimiterMiddleware = require('./../Security/RateLimiterMiddleware')
|
||||
const { expressify } = require('../../util/promises')
|
||||
|
||||
module.exports = {
|
||||
apply(webRouter, privateApiRouter, publicApiRouter) {
|
||||
@@ -23,7 +24,7 @@ module.exports = {
|
||||
maxRequests: 20,
|
||||
timeInterval: 60,
|
||||
}),
|
||||
AnalyticsController.updateEditingSession
|
||||
expressify(AnalyticsController.updateEditingSession)
|
||||
)
|
||||
|
||||
publicApiRouter.use(
|
||||
|
||||
Reference in New Issue
Block a user