overleaf/services/spelling/app/js/SpellingAPIManager.js
Jakob Ackermann 17eb841b31 Merge pull request #6151 from overleaf/jpa-jel-ta-spelling-client-cache
[misc] filter out saved words from users dict client side

GitOrigin-RevId: 01b496c60d25954c8e65a71c06fd90a6c428a698
2022-01-05 09:02:59 +00:00

91 lines
2.6 KiB
JavaScript

// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const ASpell = require('./ASpell')
const LearnedWordsManager = require('./LearnedWordsManager')
const { callbackify } = require('util')
const OError = require('@overleaf/o-error')
const Settings = require('@overleaf/settings')
// The max number of words checked in a single request
const REQUEST_LIMIT = 10000
const SpellingAPIManager = {
whitelist: Settings.ignoredMisspellings,
learnWord(token, request, callback) {
if (callback == null) {
callback = () => {}
}
if (request.word == null) {
return callback(new OError('malformed JSON'))
}
if (token == null) {
return callback(new OError('no token provided'))
}
return LearnedWordsManager.learnWord(token, request.word, callback)
},
unlearnWord(token, request, callback) {
if (callback == null) {
callback = () => {}
}
if (request.word == null) {
return callback(new OError('malformed JSON'))
}
if (token == null) {
return callback(new OError('no token provided'))
}
return LearnedWordsManager.unlearnWord(token, request.word, callback)
},
deleteDic(token, callback) {
return LearnedWordsManager.deleteUsersLearnedWords(token, callback)
},
getDic(token, callback) {
return LearnedWordsManager.getLearnedWordsNoCache(token, callback)
},
}
const promises = {
async runRequest(token, request) {
if (!request.words) {
throw new OError('malformed JSON')
}
const lang = request.language || 'en'
// only the first 10K words are checked
const wordSlice = request.words.slice(0, REQUEST_LIMIT)
const misspellings = await ASpell.promises.checkWords(lang, wordSlice)
if (token && !request.skipLearnedWords) {
const learnedWords = await LearnedWordsManager.promises.getLearnedWords(
token
)
const notLearntMisspellings = misspellings.filter(m => {
const word = wordSlice[m.index]
return (
learnedWords.indexOf(word) === -1 &&
SpellingAPIManager.whitelist.indexOf(word) === -1
)
})
return { misspellings: notLearntMisspellings }
} else {
return { misspellings }
}
},
}
SpellingAPIManager.runRequest = callbackify(promises.runRequest)
SpellingAPIManager.promises = promises
module.exports = SpellingAPIManager