overleaf/services/spelling/app/coffee/LearnedWordsManager.coffee

33 lines
918 B
CoffeeScript
Raw Normal View History

2014-08-15 07:13:35 -04:00
db = require("./DB")
2015-03-03 09:28:47 -05:00
mongoCache = require("./MongoCache")
2015-03-03 08:57:16 -05:00
logger = require 'logger-sharelatex'
metrics = require('metrics-sharelatex')
2014-08-15 07:13:35 -04:00
module.exports = LearnedWordsManager =
learnWord: (user_token, word, callback = (error)->) ->
2015-03-03 09:28:47 -05:00
mongoCache.del(user_token)
2014-08-15 07:13:35 -04:00
db.spellingPreferences.update {
token: user_token
}, {
$push: learnedWords: word
}, {
upsert: true
}, callback
getLearnedWords: (user_token, callback = (error, words)->) ->
2015-03-03 09:28:47 -05:00
mongoCachedWords = mongoCache.get(user_token)
if mongoCachedWords?
metrics.inc "mongoCache-hit", 0.1
return callback(null, mongoCachedWords)
2015-03-03 08:57:16 -05:00
2015-03-03 09:28:47 -05:00
metrics.inc "mongoCache-miss", 0.1
logger.info user_token:user_token, "mongoCache miss"
2015-03-03 08:57:16 -05:00
2014-08-15 07:13:35 -04:00
db.spellingPreferences.findOne token: user_token, (error, preferences) ->
return callback error if error?
2015-03-03 08:57:16 -05:00
words = preferences?.learnedWords || []
2015-03-03 09:28:47 -05:00
mongoCache.set(user_token, words)
2015-03-03 08:57:16 -05:00
callback null, words
2014-08-15 07:13:35 -04:00