2019-07-03 08:41:01 -04:00
|
|
|
const SpellingAPIManager = require('./SpellingAPIManager')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const metrics = require('metrics-sharelatex')
|
|
|
|
|
2019-07-11 06:29:00 -04: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 08:41:01 -04:00
|
|
|
module.exports = {
|
2019-07-11 06:29:00 -04:00
|
|
|
check(req, res) {
|
2019-07-03 08:41:01 -04:00
|
|
|
metrics.inc('spelling-check', 0.1)
|
2019-07-11 06:29:00 -04:00
|
|
|
const { token, wordCount } = extractCheckRequestData(req)
|
|
|
|
logger.info({ token, wordCount }, 'running check')
|
|
|
|
SpellingAPIManager.runRequest(token, req.body, function(error, result) {
|
2019-07-03 08:41:01 -04:00
|
|
|
if (error != null) {
|
|
|
|
logger.err(
|
|
|
|
{
|
|
|
|
err: error,
|
2019-07-11 06:29:00 -04:00
|
|
|
user_id: token,
|
|
|
|
wordCount
|
2019-07-03 08:41:01 -04:00
|
|
|
},
|
|
|
|
'error processing spelling request'
|
|
|
|
)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
2019-07-11 06:29:00 -04:00
|
|
|
res.send(result)
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
learn(req, res, next) {
|
|
|
|
metrics.inc('spelling-learn', 0.1)
|
2019-07-11 06:29:00 -04:00
|
|
|
const { token, word } = extractLearnRequestData(req)
|
|
|
|
logger.info({ token, word }, 'learning word')
|
|
|
|
SpellingAPIManager.learnWord(token, req.body, function(error) {
|
2019-07-03 08:41:01 -04:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
res.sendStatus(200)
|
2019-07-11 06:29:00 -04:00
|
|
|
next()
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-07-20 09:04:08 -04:00
|
|
|
unlearn(req, res, next) {
|
|
|
|
metrics.inc('spelling-unlearn', 0.1)
|
|
|
|
const { token, word } = extractLearnRequestData(req)
|
|
|
|
logger.info({ token, word }, 'unlearning word')
|
|
|
|
SpellingAPIManager.unlearnWord(token, req.body, function(error) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
res.sendStatus(200)
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-07-03 08:41:01 -04:00
|
|
|
deleteDic(req, res, next) {
|
2019-07-11 06:29:00 -04:00
|
|
|
const { token, word } = extractLearnRequestData(req)
|
|
|
|
logger.log({ token, word }, 'deleting user dictionary')
|
|
|
|
SpellingAPIManager.deleteDic(token, function(error) {
|
2019-07-03 08:41:01 -04:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2019-07-11 06:29:00 -04:00
|
|
|
res.sendStatus(204)
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getDic(req, res, next) {
|
2019-07-11 06:29:00 -04:00
|
|
|
const token = req.params ? req.params.user_id : undefined
|
2019-07-03 08:41:01 -04:00
|
|
|
logger.info(
|
|
|
|
{
|
2019-07-11 06:29:00 -04:00
|
|
|
token
|
2019-07-03 08:41:01 -04:00
|
|
|
},
|
|
|
|
'getting user dictionary'
|
|
|
|
)
|
2019-07-11 06:29:00 -04:00
|
|
|
SpellingAPIManager.getDic(token, function(error, words) {
|
2019-07-03 08:41:01 -04:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2019-07-11 06:29:00 -04:00
|
|
|
res.send(words)
|
2019-07-03 08:41:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|