2019-07-03 08:41:01 -04:00
|
|
|
// 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')
|
2019-07-11 06:29:00 -04:00
|
|
|
const { callbackify } = require('util')
|
2020-06-03 05:14:44 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2020-10-08 04:36:47 -04:00
|
|
|
const Settings = require('settings-sharelatex')
|
2019-07-03 08:41:01 -04:00
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
// The max number of words checked in a single request
|
|
|
|
const REQUEST_LIMIT = 10000
|
2019-07-03 08:41:01 -04:00
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
const SpellingAPIManager = {
|
2020-10-08 04:36:47 -04:00
|
|
|
whitelist: Settings.ignoredMisspellings,
|
2019-07-03 08:41:01 -04:00
|
|
|
|
|
|
|
learnWord(token, request, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = () => {}
|
|
|
|
}
|
|
|
|
if (request.word == null) {
|
2020-06-03 05:14:44 -04:00
|
|
|
return callback(new OError('malformed JSON'))
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
if (token == null) {
|
2020-06-03 05:14:44 -04:00
|
|
|
return callback(new OError('no token provided'))
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return LearnedWordsManager.learnWord(token, request.word, callback)
|
|
|
|
},
|
|
|
|
|
2019-07-20 09:04:08 -04:00
|
|
|
unlearnWord(token, request, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = () => {}
|
|
|
|
}
|
|
|
|
if (request.word == null) {
|
2020-06-03 05:14:44 -04:00
|
|
|
return callback(new OError('malformed JSON'))
|
2019-07-20 09:04:08 -04:00
|
|
|
}
|
|
|
|
if (token == null) {
|
2020-06-03 05:14:44 -04:00
|
|
|
return callback(new OError('no token provided'))
|
2019-07-20 09:04:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return LearnedWordsManager.unlearnWord(token, request.word, callback)
|
|
|
|
},
|
|
|
|
|
2019-07-03 08:41:01 -04:00
|
|
|
deleteDic(token, callback) {
|
|
|
|
return LearnedWordsManager.deleteUsersLearnedWords(token, callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
getDic(token, callback) {
|
|
|
|
return LearnedWordsManager.getLearnedWords(token, callback)
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 06:29:00 -04:00
|
|
|
|
|
|
|
const promises = {
|
|
|
|
async runRequest(token, request) {
|
|
|
|
if (!request.words) {
|
2020-06-03 05:14:44 -04:00
|
|
|
throw new OError('malformed JSON')
|
2019-07-11 06:29:00 -04:00
|
|
|
}
|
|
|
|
const lang = request.language || 'en'
|
|
|
|
|
|
|
|
// only the first 10K words are checked
|
|
|
|
const wordSlice = request.words.slice(0, REQUEST_LIMIT)
|
|
|
|
|
2019-07-16 02:51:15 -04:00
|
|
|
const misspellings = await ASpell.promises.checkWords(lang, wordSlice)
|
2019-07-11 06:29:00 -04:00
|
|
|
|
|
|
|
if (token) {
|
|
|
|
const learnedWords = await LearnedWordsManager.promises.getLearnedWords(
|
|
|
|
token
|
|
|
|
)
|
2020-08-10 12:23:15 -04:00
|
|
|
const notLearntMisspellings = misspellings.filter((m) => {
|
2019-07-11 06:29:00 -04:00
|
|
|
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
|