2016-04-07 09:40:53 -07:00
LevelSession = require ' ./../models/LevelSession '
Handler = require ' ../commons/Handler '
2014-03-31 15:48:22 -07:00
log = require ' winston '
2016-05-03 22:26:48 -07:00
LZString = require ' lz-string '
2014-01-03 10:32:13 -08:00
TIMEOUT = 1000 * 30 # no activity for 30 seconds means it's not active
class LevelSessionHandler extends Handler
modelClass: LevelSession
getByRelationship: (req, res, args...) ->
2014-03-31 15:48:22 -07:00
return @ getActiveSessions req , res if args . length is 2 and args [ 1 ] is ' active '
2015-01-05 15:40:26 -08:00
return @ getRecentSessions req , res if args . length is 2 and args [ 1 ] is ' recent '
2014-08-29 23:09:38 -07:00
return @ getCodeLanguageCounts req , res if args [ 1 ] is ' code_language_counts '
2014-04-08 19:26:19 -07:00
super ( arguments . . . )
2014-06-11 19:38:41 -07:00
2014-05-15 15:18:15 -07:00
formatEntity: (req, document) ->
2016-05-03 22:26:48 -07:00
document = super ( req , document )
2016-05-24 13:42:37 -07:00
submittedCode = document . submittedCode ? { }
2016-05-03 22:26:48 -07:00
unless req . user ? . isAdmin ( ) or
2015-01-31 10:04:02 -08:00
req . user ? . id is document . creator or
( ' employer ' in ( req . user ? . get ( ' permissions ' ) ? [ ] ) ) or
2015-04-07 14:51:18 -07:00
not document . submittedCode # TODO: only allow leaderboard access to non-top-5 solutions
2016-05-03 22:26:48 -07:00
document = _ . omit document , @ privateProperties
if req . query . interpret
2016-05-08 20:59:58 -07:00
plan = submittedCode [ if document . team is ' humans ' then ' hero-placeholder ' else ' hero-placeholder-1 ' ] ? . plan ? ' '
2016-05-03 22:26:48 -07:00
plan = LZString . compressToUTF16 plan
document . interpret = plan
document . code = submittedCode
return document
2014-06-11 19:38:41 -07:00
2014-03-31 15:48:22 -07:00
getActiveSessions: (req, res) ->
2014-11-22 18:40:28 -08:00
return @ sendForbiddenError ( res ) unless req . user ? . isAdmin ( )
2014-01-03 10:32:13 -08:00
start = new Date ( )
start = new Date ( start . getTime ( ) - TIMEOUT )
query = @ modelClass . find ( { ' changed ' : { $gt: start } } )
query . exec (err, documents) =>
return @ sendDatabaseError ( res , err ) if err
documents = ( @ formatEntity ( req , doc ) for doc in documents )
@ sendSuccess ( res , documents )
2015-01-05 15:40:26 -08:00
getRecentSessions: (req, res) ->
return @ sendForbiddenError ( res ) unless req . user ? . isAdmin ( )
levelSlug = req . query . slug or req . body . slug
2016-01-11 09:51:23 -08:00
limit = parseInt req . query . limit or req . body . limit or 7
2015-01-05 15:40:26 -08:00
return @ sendSuccess res , [ ] unless levelSlug ?
2015-01-08 17:00:02 -08:00
today = new Date ( )
today . setUTCMinutes ( today . getUTCMinutes ( ) - 10 )
queryParams = { $and: [ { " changed " : { " $lt " : today } } , { " levelID " : levelSlug } ] }
query = @ modelClass . find ( queryParams ) . sort ( { changed: - 1 } ) . limit ( limit )
2015-01-05 15:40:26 -08:00
query . exec (err, documents) =>
return @ sendDatabaseError ( res , err ) if err
@ sendSuccess res , documents
2014-02-13 16:42:35 -08:00
hasAccessToDocument: (req, document, method=null) ->
2015-01-31 10:04:02 -08:00
get = ( method ? req . method ) . toLowerCase ( ) is ' get '
return true if get and document . get ( ' submitted ' )
return true if get and ( ' employer ' in ( req . user ? . get ( ' permissions ' ) ? [ ] ) )
2015-04-07 14:51:18 -07:00
return true if get and not document . get ( ' submittedCode ' ) # Allow leaderboard access to non-multiplayer sessions
2014-02-13 16:42:35 -08:00
super ( arguments . . . )
2014-08-29 23:09:38 -07:00
getCodeLanguageCounts: (req, res) ->
if @ codeLanguageCache and ( new Date ( ) ) - @ codeLanguageCountCachedSince > 86400 * 1000 # Dumb cache expiration
@codeLanguageCountCache = null
@codeLanguageCountCacheSince = null
if @ codeLanguageCountCache
return @ sendSuccess res , @ codeLanguageCountCache
query = LevelSession . aggregate [
#{$match: {codeLanguage: {$exists: true}}} # actually slows it down
{ $group: { _id: " $codeLanguage " , sessions: { $sum: 1 } } }
{ $sort: { sessions: - 1 } }
]
query . exec (err, data) =>
if err ? then return @ sendDatabaseError res , err
@codeLanguageCountCache = data
@codeLanguageCountCachedSince = new Date ( )
@ sendSuccess res , data
2014-01-03 10:32:13 -08:00
module.exports = new LevelSessionHandler ( )