2014-01-22 17:57:41 -05:00
|
|
|
LevelSession = require('./LevelSession')
|
|
|
|
Handler = require('../../commons/Handler')
|
2014-03-31 18:48:22 -04:00
|
|
|
log = require 'winston'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
TIMEOUT = 1000 * 30 # no activity for 30 seconds means it's not active
|
|
|
|
|
|
|
|
class LevelSessionHandler extends Handler
|
|
|
|
modelClass: LevelSession
|
2014-05-14 19:41:01 -04:00
|
|
|
editableProperties: ['multiplayer', 'players', 'code', 'codeLanguage', 'completed', 'state',
|
2014-01-03 13:32:13 -05:00
|
|
|
'levelName', 'creatorName', 'levelID', 'screenshot',
|
2014-03-10 16:20:00 -04:00
|
|
|
'chat', 'teamSpells', 'submitted', 'unsubscribed']
|
2014-04-12 13:51:02 -04:00
|
|
|
jsonSchema: require '../../../app/schemas/models/level_session'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
getByRelationship: (req, res, args...) ->
|
2014-03-31 18:48:22 -04:00
|
|
|
return @getActiveSessions req, res if args.length is 2 and args[1] is 'active'
|
2014-04-08 22:26:19 -04:00
|
|
|
super(arguments...)
|
2014-05-15 18:18:15 -04:00
|
|
|
|
|
|
|
formatEntity: (req, document) ->
|
|
|
|
documentObject = super(req, document)
|
|
|
|
if req.user.isAdmin() or req.user.id is document.creator
|
|
|
|
return documentObject
|
|
|
|
else
|
|
|
|
return _.omit documentObject, ['submittedCode','code']
|
|
|
|
|
2014-03-31 18:48:22 -04:00
|
|
|
getActiveSessions: (req, res) ->
|
|
|
|
return @sendUnauthorizedError(res) unless req.user.isAdmin()
|
2014-01-03 13:32:13 -05: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)
|
|
|
|
|
2014-02-13 19:42:35 -05:00
|
|
|
hasAccessToDocument: (req, document, method=null) ->
|
|
|
|
return true if req.method is 'GET' and document.get('totalScore')
|
|
|
|
super(arguments...)
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
module.exports = new LevelSessionHandler()
|