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

41 lines
1.2 KiB
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
2017-10-30 12:57:34 -04:00
deleteUsersLearnedWords: (user_token, callback =(error)->)->
db.spellingPreferences.remove token: user_token, callback
2017-03-16 12:35:51 -04:00
2017-03-17 11:19:25 -04:00
[
'learnWord',
'getLearnedWords'
].map (method) ->
metrics.timeAsyncMethod(LearnedWordsManager, method, 'mongo.LearnedWordsManager', logger)