SpellingAPIController.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const SpellingAPIManager = require('./SpellingAPIManager')
  2. const logger = require('logger-sharelatex')
  3. const metrics = require('metrics-sharelatex')
  4. const OError = require('@overleaf/o-error')
  5. function extractCheckRequestData(req) {
  6. const token = req.params ? req.params.user_id : undefined
  7. const wordCount =
  8. req.body && req.body.words ? req.body.words.length : undefined
  9. return { token, wordCount }
  10. }
  11. function extractLearnRequestData(req) {
  12. const token = req.params ? req.params.user_id : undefined
  13. const word = req.body ? req.body.word : undefined
  14. return { token, word }
  15. }
  16. module.exports = {
  17. check(req, res) {
  18. metrics.inc('spelling-check', 0.1)
  19. const { token, wordCount } = extractCheckRequestData(req)
  20. logger.info({ token, wordCount }, 'running check')
  21. SpellingAPIManager.runRequest(token, req.body, function (error, result) {
  22. if (error != null) {
  23. logger.error(
  24. OError.tag(error, 'error processing spelling request', {
  25. user_id: token,
  26. wordCount
  27. })
  28. )
  29. return res.sendStatus(500)
  30. }
  31. res.send(result)
  32. })
  33. },
  34. learn(req, res, next) {
  35. metrics.inc('spelling-learn', 0.1)
  36. const { token, word } = extractLearnRequestData(req)
  37. logger.info({ token, word }, 'learning word')
  38. SpellingAPIManager.learnWord(token, req.body, function (error) {
  39. if (error != null) {
  40. return next(OError.tag(error))
  41. }
  42. res.sendStatus(204)
  43. })
  44. },
  45. unlearn(req, res, next) {
  46. metrics.inc('spelling-unlearn', 0.1)
  47. const { token, word } = extractLearnRequestData(req)
  48. logger.info({ token, word }, 'unlearning word')
  49. SpellingAPIManager.unlearnWord(token, req.body, function (error) {
  50. if (error != null) {
  51. return next(OError.tag(error))
  52. }
  53. res.sendStatus(204)
  54. })
  55. },
  56. deleteDic(req, res, next) {
  57. const { token, word } = extractLearnRequestData(req)
  58. logger.log({ token, word }, 'deleting user dictionary')
  59. SpellingAPIManager.deleteDic(token, function (error) {
  60. if (error != null) {
  61. return next(OError.tag(error))
  62. }
  63. res.sendStatus(204)
  64. })
  65. },
  66. getDic(req, res, next) {
  67. const token = req.params ? req.params.user_id : undefined
  68. logger.info(
  69. {
  70. token
  71. },
  72. 'getting user dictionary'
  73. )
  74. SpellingAPIManager.getDic(token, function (error, words) {
  75. if (error != null) {
  76. return next(OError.tag(error))
  77. }
  78. res.send(words)
  79. })
  80. }
  81. }