2020-09-03 12:05:13 -04:00
|
|
|
const { db } = require('./mongodb')
|
2019-07-03 08:41:01 -04:00
|
|
|
const mongoCache = require('./MongoCache')
|
|
|
|
const logger = require('logger-sharelatex')
|
2020-11-25 06:57:24 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2019-07-11 06:29:00 -04:00
|
|
|
const { promisify } = require('util')
|
2020-06-03 05:14:44 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-07-03 08:41:01 -04:00
|
|
|
|
2019-07-11 06:29:00 -04:00
|
|
|
const LearnedWordsManager = {
|
2019-07-03 08:41:01 -04:00
|
|
|
learnWord(userToken, word, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = () => {}
|
|
|
|
}
|
|
|
|
mongoCache.del(userToken)
|
2020-09-03 12:05:13 -04:00
|
|
|
return db.spellingPreferences.updateOne(
|
2019-07-03 08:41:01 -04:00
|
|
|
{
|
2021-07-13 07:04:47 -04:00
|
|
|
token: userToken,
|
2019-07-03 08:41:01 -04:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:47 -04:00
|
|
|
$addToSet: { learnedWords: word },
|
2019-07-03 08:41:01 -04:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:47 -04:00
|
|
|
upsert: true,
|
2019-07-03 08:41:01 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-07-20 09:04:08 -04:00
|
|
|
unlearnWord(userToken, word, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = () => {}
|
|
|
|
}
|
|
|
|
mongoCache.del(userToken)
|
2020-09-03 12:05:13 -04:00
|
|
|
return db.spellingPreferences.updateOne(
|
2019-07-20 09:04:08 -04:00
|
|
|
{
|
2021-07-13 07:04:47 -04:00
|
|
|
token: userToken,
|
2019-07-20 09:04:08 -04:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:47 -04:00
|
|
|
$pull: { learnedWords: word },
|
2019-07-20 09:04:08 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-07-03 08:41:01 -04:00
|
|
|
getLearnedWords(userToken, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = () => {}
|
|
|
|
}
|
|
|
|
const mongoCachedWords = mongoCache.get(userToken)
|
|
|
|
if (mongoCachedWords != null) {
|
|
|
|
metrics.inc('mongoCache', 0.1, { status: 'hit' })
|
|
|
|
return callback(null, mongoCachedWords)
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.inc('mongoCache', 0.1, { status: 'miss' })
|
|
|
|
logger.info({ userToken }, 'mongoCache miss')
|
|
|
|
|
2021-07-13 07:04:47 -04:00
|
|
|
db.spellingPreferences.findOne(
|
|
|
|
{ token: userToken },
|
|
|
|
function (error, preferences) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(OError.tag(error))
|
|
|
|
}
|
|
|
|
let words =
|
|
|
|
(preferences != null ? preferences.learnedWords : undefined) || []
|
|
|
|
if (words) {
|
|
|
|
// remove duplicates
|
|
|
|
words = words.filter(
|
|
|
|
(value, index, self) => self.indexOf(value) === index
|
|
|
|
)
|
|
|
|
}
|
|
|
|
mongoCache.set(userToken, words)
|
|
|
|
callback(null, words)
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
2021-07-13 07:04:47 -04:00
|
|
|
)
|
2019-07-03 08:41:01 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteUsersLearnedWords(userToken, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = () => {}
|
|
|
|
}
|
2020-09-03 12:05:13 -04:00
|
|
|
db.spellingPreferences.deleteOne({ token: userToken }, callback)
|
2021-07-13 07:04:47 -04:00
|
|
|
},
|
2019-07-03 08:41:01 -04:00
|
|
|
}
|
2019-07-11 06:29:00 -04:00
|
|
|
|
|
|
|
const promises = {
|
|
|
|
learnWord: promisify(LearnedWordsManager.learnWord),
|
2019-07-20 09:04:08 -04:00
|
|
|
unlearnWord: promisify(LearnedWordsManager.unlearnWord),
|
2019-07-11 06:29:00 -04:00
|
|
|
getLearnedWords: promisify(LearnedWordsManager.getLearnedWords),
|
|
|
|
deleteUsersLearnedWords: promisify(
|
|
|
|
LearnedWordsManager.deleteUsersLearnedWords
|
2021-07-13 07:04:47 -04:00
|
|
|
),
|
2019-07-11 06:29:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
LearnedWordsManager.promises = promises
|
|
|
|
|
|
|
|
module.exports = LearnedWordsManager
|
2021-07-13 07:04:47 -04:00
|
|
|
;['learnWord', 'unlearnWord', 'getLearnedWords'].map(method =>
|
2019-07-03 08:41:01 -04:00
|
|
|
metrics.timeAsyncMethod(
|
|
|
|
LearnedWordsManager,
|
|
|
|
method,
|
|
|
|
'mongo.LearnedWordsManager',
|
|
|
|
logger
|
|
|
|
)
|
|
|
|
)
|