overleaf/services/spelling/app/js/SpellingAPIController.js

89 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-07-03 12:41:01 +00:00
const SpellingAPIManager = require('./SpellingAPIManager')
const logger = require('logger-sharelatex')
const metrics = require('@overleaf/metrics')
2020-06-03 09:14:44 +00:00
const OError = require('@overleaf/o-error')
2019-07-03 12:41:01 +00:00
function extractCheckRequestData(req) {
const token = req.params ? req.params.user_id : undefined
const wordCount =
req.body && req.body.words ? req.body.words.length : undefined
return { token, wordCount }
}
function extractLearnRequestData(req) {
const token = req.params ? req.params.user_id : undefined
const word = req.body ? req.body.word : undefined
return { token, word }
}
2019-07-03 12:41:01 +00:00
module.exports = {
check(req, res) {
2019-07-03 12:41:01 +00:00
metrics.inc('spelling-check', 0.1)
const { token, wordCount } = extractCheckRequestData(req)
logger.info({ token, wordCount }, 'running check')
2020-08-10 16:23:15 +00:00
SpellingAPIManager.runRequest(token, req.body, function (error, result) {
2019-07-03 12:41:01 +00:00
if (error != null) {
2020-06-03 09:14:44 +00:00
logger.error(
OError.tag(error, 'error processing spelling request', {
user_id: token,
2021-07-13 11:04:47 +00:00
wordCount,
2020-06-03 09:14:44 +00:00
})
2019-07-03 12:41:01 +00:00
)
return res.sendStatus(500)
}
res.send(result)
2019-07-03 12:41:01 +00:00
})
},
learn(req, res, next) {
metrics.inc('spelling-learn', 0.1)
const { token, word } = extractLearnRequestData(req)
logger.info({ token, word }, 'learning word')
2020-08-10 16:23:15 +00:00
SpellingAPIManager.learnWord(token, req.body, function (error) {
2019-07-03 12:41:01 +00:00
if (error != null) {
2020-06-03 09:14:44 +00:00
return next(OError.tag(error))
2019-07-03 12:41:01 +00:00
}
res.sendStatus(204)
2019-07-03 12:41:01 +00:00
})
},
2019-07-20 13:04:08 +00:00
unlearn(req, res, next) {
metrics.inc('spelling-unlearn', 0.1)
const { token, word } = extractLearnRequestData(req)
logger.info({ token, word }, 'unlearning word')
2020-08-10 16:23:15 +00:00
SpellingAPIManager.unlearnWord(token, req.body, function (error) {
2019-07-20 13:04:08 +00:00
if (error != null) {
2020-06-03 09:14:44 +00:00
return next(OError.tag(error))
2019-07-20 13:04:08 +00:00
}
res.sendStatus(204)
2019-07-20 13:04:08 +00:00
})
},
2019-07-03 12:41:01 +00:00
deleteDic(req, res, next) {
const { token, word } = extractLearnRequestData(req)
logger.log({ token, word }, 'deleting user dictionary')
2020-08-10 16:23:15 +00:00
SpellingAPIManager.deleteDic(token, function (error) {
2019-07-03 12:41:01 +00:00
if (error != null) {
2020-06-03 09:14:44 +00:00
return next(OError.tag(error))
2019-07-03 12:41:01 +00:00
}
res.sendStatus(204)
2019-07-03 12:41:01 +00:00
})
},
getDic(req, res, next) {
const token = req.params ? req.params.user_id : undefined
2019-07-03 12:41:01 +00:00
logger.info(
{
2021-07-13 11:04:47 +00:00
token,
2019-07-03 12:41:01 +00:00
},
'getting user dictionary'
)
2020-08-10 16:23:15 +00:00
SpellingAPIManager.getDic(token, function (error, words) {
2019-07-03 12:41:01 +00:00
if (error != null) {
2020-06-03 09:14:44 +00:00
return next(OError.tag(error))
2019-07-03 12:41:01 +00:00
}
res.send(words)
2019-07-03 12:41:01 +00:00
})
2021-07-13 11:04:47 +00:00
},
2019-07-03 12:41:01 +00:00
}