<commit_before>###
HTTP-based User Queries.

These are authenticated using the cookie only.

They allow for evenly distributing all user_queries (except changefeeds)
across the cluster in a stateless way.
###

async   = require('async')

auth    = require('./auth')

misc = require('smc-util/misc')

Cache = require('expiring-lru-cache')

remember_me_cache = new Cache(size:5000, expiry:60000)

get_account_id = (database, remember_me, cb) ->
    if not remember_me?
        cb('not signed in')
        return
    account_id = remember_me_cache.get(remember_me)
    if account_id
        cb(undefined, account_id)
        return
    x = remember_me.split('$')
    database.get_remember_me
        hash : auth.generate_hash(x[0], x[1], x[2], x[3])
        cb   : (err, signed_in_mesg) ->
            if err
                cb(err)
            remember_me_cache.set(remember_me, signed_in_mesg?.account_id)
            if not signed_in_mesg?
                cb('not signed in')
            else
                cb(undefined, signed_in_mesg.account_id)

exports.init = (router, cookie_name, database) ->
    router.post '/user_query', (req, res) ->
        console.log req.body
        if not req.body.query?
            res.send({error:'missing query'})
            return
        locals =
            query      : undefined
            options    : undefined
            account_id : undefined
            result     : undefined
        try
            # TODO -- issues of size?
            locals.query   = misc.from_json(req.body.query)
            locals.options = if req.body.options then misc.from_json(req.body.options)
        catch err
            res.send({error:"JSON parse error -- '#{err}'"})
            return
        async.series([
            (cb) ->
                get_account_id database, req.cookies[cookie_name], (err, account_id) ->
                    locals.account_id = account_id
                    cb(err)
            (cb) ->
                database.user_query<commit_msg>remove some too-verbose logging<commit_after>###
HTTP-based User Queries.

These are authenticated using the cookie only.

They allow for evenly distributing all user_queries (except changefeeds)
across the cluster in a stateless way.
###

async   = require('async')

auth    = require('./auth')

misc = require('smc-util/misc')

Cache = require('expiring-lru-cache')

remember_me_cache = new Cache(size:5000, expiry:60000)

get_account_id = (database, remember_me, cb) ->
    if not remember_me?
        cb('not signed in')
        return
    account_id = remember_me_cache.get(remember_me)
    if account_id
        cb(undefined, account_id)
        return
    x = remember_me.split('$')
    database.get_remember_me
        hash : auth.generate_hash(x[0], x[1], x[2], x[3])
        cb   : (err, signed_in_mesg) ->
            if err
                cb(err)
            remember_me_cache.set(remember_me, signed_in_mesg?.account_id)
            if not signed_in_mesg?
                cb('not signed in')
            else
                cb(undefined, signed_in_mesg.account_id)

exports.init = (router, cookie_name, database) ->
    router.post '/user_query', (req, res) ->
        if not req.body.query?
            res.send({error:'missing query'})
            return
        locals =
            query      : undefined
            options    : undefined
            account_id : undefined
            result     : undefined
        try
            # TODO -- issues of size?
            locals.query   = misc.from_json(req.body.query)
            locals.options = if req.body.options then misc.from_json(req.body.options)
        catch err
            res.send({error:"JSON parse error -- '#{err}'"})
            return
        async.series([
            (cb) ->
                get_account_id database, req.cookies[cookie_name], (err, account_id) ->
                    locals.account_id = account_id
                    cb(err)
            (cb) ->
                database.user_query
{'fraction_non_alphanumeric': 0.10453747806467786, 'fraction_numerical': 0.006517924291802457, 'mean_word_length': 1.89760348583878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6861973', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1161, 'n_words': 276}