SpellingAPIManager.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS207: Consider shorter variations of null checks
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. const ASpell = require('./ASpell')
  10. const LearnedWordsManager = require('./LearnedWordsManager')
  11. const { callbackify } = require('util')
  12. const OError = require('@overleaf/o-error')
  13. const Settings = require('@overleaf/settings')
  14. // The max number of words checked in a single request
  15. const REQUEST_LIMIT = 10000
  16. const SpellingAPIManager = {
  17. whitelist: Settings.ignoredMisspellings,
  18. learnWord(token, request, callback) {
  19. if (callback == null) {
  20. callback = () => {}
  21. }
  22. if (request.word == null) {
  23. return callback(new OError('malformed JSON'))
  24. }
  25. if (token == null) {
  26. return callback(new OError('no token provided'))
  27. }
  28. return LearnedWordsManager.learnWord(token, request.word, callback)
  29. },
  30. unlearnWord(token, request, callback) {
  31. if (callback == null) {
  32. callback = () => {}
  33. }
  34. if (request.word == null) {
  35. return callback(new OError('malformed JSON'))
  36. }
  37. if (token == null) {
  38. return callback(new OError('no token provided'))
  39. }
  40. return LearnedWordsManager.unlearnWord(token, request.word, callback)
  41. },
  42. deleteDic(token, callback) {
  43. return LearnedWordsManager.deleteUsersLearnedWords(token, callback)
  44. },
  45. getDic(token, callback) {
  46. return LearnedWordsManager.getLearnedWords(token, callback)
  47. },
  48. }
  49. const promises = {
  50. async runRequest(token, request) {
  51. if (!request.words) {
  52. throw new OError('malformed JSON')
  53. }
  54. const lang = request.language || 'en'
  55. // only the first 10K words are checked
  56. const wordSlice = request.words.slice(0, REQUEST_LIMIT)
  57. const misspellings = await ASpell.promises.checkWords(lang, wordSlice)
  58. if (token) {
  59. const learnedWords = await LearnedWordsManager.promises.getLearnedWords(
  60. token
  61. )
  62. const notLearntMisspellings = misspellings.filter(m => {
  63. const word = wordSlice[m.index]
  64. return (
  65. learnedWords.indexOf(word) === -1 &&
  66. SpellingAPIManager.whitelist.indexOf(word) === -1
  67. )
  68. })
  69. return { misspellings: notLearntMisspellings }
  70. } else {
  71. return { misspellings }
  72. }
  73. },
  74. }
  75. SpellingAPIManager.runRequest = callbackify(promises.runRequest)
  76. SpellingAPIManager.promises = promises
  77. module.exports = SpellingAPIManager