2014-06-30 22:16:26 -04: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
|
|
|
|
|
|
|
|
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'
|
2015-01-05 18:40:26 -05:00
|
|
|
return @getRecentSessions req, res if args.length is 2 and args[1] is 'recent'
|
2014-08-30 02:09:38 -04:00
|
|
|
return @getCodeLanguageCounts req, res if args[1] is 'code_language_counts'
|
2014-04-08 22:26:19 -04:00
|
|
|
super(arguments...)
|
2014-06-11 22:38:41 -04:00
|
|
|
|
2014-05-15 18:18:15 -04:00
|
|
|
formatEntity: (req, document) ->
|
|
|
|
documentObject = super(req, document)
|
2014-11-25 11:20:39 -05:00
|
|
|
if req.user?.isAdmin() or req.user?.id is document.creator or ('employer' in (req.user?.get('permissions') ? []))
|
2014-05-15 18:18:15 -04:00
|
|
|
return documentObject
|
|
|
|
else
|
2014-06-19 11:07:30 -04:00
|
|
|
return _.omit documentObject, @privateProperties
|
2014-06-11 22:38:41 -04:00
|
|
|
|
2014-03-31 18:48:22 -04:00
|
|
|
getActiveSessions: (req, res) ->
|
2014-11-22 21:40:28 -05:00
|
|
|
return @sendForbiddenError(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)
|
|
|
|
|
2015-01-05 18:40:26 -05:00
|
|
|
getRecentSessions: (req, res) ->
|
|
|
|
return @sendForbiddenError(res) unless req.user?.isAdmin()
|
|
|
|
|
|
|
|
levelSlug = req.query.slug or req.body.slug
|
|
|
|
limit = req.query.limit or req.body.limit or 7
|
|
|
|
|
|
|
|
return @sendSuccess res, [] unless levelSlug?
|
|
|
|
|
2015-01-08 20:00:02 -05: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 18:40:26 -05:00
|
|
|
query.exec (err, documents) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
@sendSuccess res, documents
|
|
|
|
|
2014-02-13 19:42:35 -05:00
|
|
|
hasAccessToDocument: (req, document, method=null) ->
|
2014-10-20 20:00:46 -04:00
|
|
|
return true if req.method is 'GET' and document.get('submitted')
|
2014-11-25 10:52:20 -05:00
|
|
|
return true if ('employer' in (req.user?.get('permissions') ? [])) and (method ? req.method).toLowerCase() is 'get'
|
2014-02-13 19:42:35 -05:00
|
|
|
super(arguments...)
|
|
|
|
|
2014-08-30 02:09:38 -04: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 13:32:13 -05:00
|
|
|
module.exports = new LevelSessionHandler()
|