codecombat/server/handlers/clan_handler.coffee

173 lines
8.2 KiB
CoffeeScript
Raw Normal View History

async = require 'async'
mongoose = require 'mongoose'
Handler = require '../commons/Handler'
AnalyticsLogEvent = require '../models/AnalyticsLogEvent'
Clan = require './../models/Clan'
EarnedAchievement = require '../models/EarnedAchievement'
2016-04-07 12:40:53 -04:00
EarnedAchievementHandler = require './earned_achievement_handler'
LevelSession = require '../models/LevelSession'
2016-04-07 12:40:53 -04:00
LevelSessionHandler = require './level_session_handler'
User = require '../models/User'
2016-04-07 12:40:53 -04:00
UserHandler = require './user_handler'
memberLimit = 200
ClanHandler = class ClanHandler extends Handler
modelClass: Clan
2015-04-06 15:01:48 -04:00
jsonSchema: require '../../app/schemas/models/clan.schema'
allowedMethods: ['GET', 'POST', 'PUT', 'DELETE']
hasAccess: (req) ->
return true if req.method is 'GET'
return false if req.method is 'POST' and req.body?.type is 'private' and not req.user?.isPremium()
req.method in @allowedMethods or req.user?.isAdmin()
hasAccessToDocument: (req, document, method=null) ->
2015-04-03 17:09:43 -04:00
return false unless document?
return true if req.user?.isAdmin()
return true if (method or req.method).toLowerCase() is 'get'
return true if document.get('ownerID')?.equals req.user?._id
false
makeNewInstance: (req) ->
instance = super(req)
instance.set 'ownerID', req.user._id
2015-04-02 20:00:28 -04:00
instance.set 'members', [req.user._id]
instance.set 'dashboardType', 'premium' if req.body?.type is 'private'
instance
2015-04-02 14:44:18 -04:00
delete: (req, res, clanID) ->
@getDocumentForIdOrSlug clanID, (err, clan) =>
return @sendDatabaseError res, err if err
return @sendNotFoundError res unless clan
return @sendForbiddenError res unless @hasAccessToDocument(req, clan)
2015-04-02 20:00:28 -04:00
memberIDs = clan.get('members')
2015-04-02 14:44:18 -04:00
Clan.remove {_id: clan.get('_id')}, (err) =>
return @sendDatabaseError res, err if err
2015-04-02 20:00:28 -04:00
User.update {_id: {$in: memberIDs}}, {$pull: {clans: clan.get('_id')}}, {multi: true}, (err) =>
return @sendDatabaseError(res, err) if err
@sendNoContent(res)
AnalyticsLogEvent.logEvent req.user, 'Clan deleted', clanID: clanID, type: clan.get('type')
2015-04-02 14:44:18 -04:00
2015-04-01 18:24:45 -04:00
getByRelationship: (req, res, args...) ->
return @joinClan(req, res, args[0]) if args[1] is 'join'
return @leaveClan(req, res, args[0]) if args[1] is 'leave'
2015-04-03 14:05:37 -04:00
return @getMemberAchievements(req, res, args[0]) if args[1] is 'member_achievements'
2015-04-02 20:00:28 -04:00
return @getMembers(req, res, args[0]) if args[1] is 'members'
return @getMemberSessions(req, res, args[0]) if args[1] is 'member_sessions'
return @getPublicClans(req, res) if args[1] is 'public'
2015-04-02 14:01:37 -04:00
return @removeMember(req, res, args[0], args[2]) if args.length is 3 and args[1] is 'remove'
2015-04-01 18:24:45 -04:00
super(arguments...)
joinClan: (req, res, clanID) ->
return @sendForbiddenError(res) unless req.user? and not req.user.isAnonymous()
2015-04-02 20:00:28 -04:00
try
clanID = mongoose.Types.ObjectId(clanID)
catch err
return @sendNotFoundError(res, err)
Clan.findById clanID, (err, clan) =>
2015-04-02 20:00:28 -04:00
return @sendDatabaseError(res, err) if err
2015-04-16 18:26:14 -04:00
return @sendNotFoundError(res) unless clan
return @sendDatabaseError(res, err) unless clanType = clan.get('type')
return @sendForbiddenError(res) unless clanType is 'public' or req.user.isPremium()
Clan.update {_id: clanID}, {$addToSet: {members: req.user._id}}, (err) =>
2015-04-01 18:24:45 -04:00
return @sendDatabaseError(res, err) if err
User.update {_id: req.user._id}, {$addToSet: {clans: clanID}}, (err) =>
return @sendDatabaseError(res, err) if err
@sendSuccess(res)
AnalyticsLogEvent.logEvent req.user, 'Clan joined', clanID: clanID, type: clanType
2015-04-01 18:24:45 -04:00
leaveClan: (req, res, clanID) ->
return @sendForbiddenError(res) unless req.user? and not req.user.isAnonymous()
2015-04-02 20:00:28 -04:00
try
clanID = mongoose.Types.ObjectId(clanID)
catch err
return @sendNotFoundError(res, err)
Clan.findById clanID, (err, clan) =>
return @sendDatabaseError(res, err) if err
return @sendNotFoundError(res) unless clan
return @sendForbiddenError(res) if clan.get('ownerID')?.equals req.user._id
2015-04-02 20:00:28 -04:00
Clan.update {_id: clanID}, {$pull: {members: req.user._id}}, (err) =>
return @sendDatabaseError(res, err) if err
User.update {_id: req.user._id}, {$pull: {clans: clanID}}, (err) =>
return @sendDatabaseError(res, err) if err
@sendSuccess(res)
AnalyticsLogEvent.logEvent req.user, 'Clan left', clanID: clanID, type: clan.get('type')
2015-04-02 20:00:28 -04:00
2015-04-03 14:05:37 -04:00
getMemberAchievements: (req, res, clanID) ->
# TODO: add tests
Clan.findById clanID, (err, clan) =>
2015-04-03 14:05:37 -04:00
return @sendDatabaseError(res, err) if err
2015-04-16 18:26:14 -04:00
return @sendNotFoundError(res) unless clan
memberIDs = _.map clan.get('members') ? [], (memberID) -> memberID.toHexString?() or memberID
User.find {_id: {$in: memberIDs}}, 'nameLower', {limit: memberLimit}, (err, users) =>
2015-04-24 17:21:57 -04:00
return @sendDatabaseError(res, err) if err
memberIDs = []
for user in users
memberIDs.push user.id
EarnedAchievement.find {user: {$in: memberIDs}}, 'achievementName user', (err, documents) =>
return @sendDatabaseError(res, err) if err?
cleandocs = (EarnedAchievementHandler.formatEntity(req, doc) for doc in documents)
@sendSuccess(res, cleandocs)
2015-04-03 14:05:37 -04:00
2015-04-02 20:00:28 -04:00
getMembers: (req, res, clanID) ->
2015-04-03 14:05:37 -04:00
# TODO: add tests
Clan.findById clanID, (err, clan) =>
2015-04-02 20:00:28 -04:00
return @sendDatabaseError(res, err) if err
2015-04-16 18:26:14 -04:00
return @sendNotFoundError(res) unless clan
memberIDs = _.map clan.get('members') ? [], (memberID) -> memberID.toHexString?() or memberID
User.find {_id: {$in: memberIDs}}, 'name nameLower points heroConfig.thangType', {}, (err, users) =>
return @sendDatabaseError(res, err) if err
2015-04-02 20:00:28 -04:00
cleandocs = (UserHandler.formatEntity(req, doc) for doc in users)
@sendSuccess(res, cleandocs)
getMemberSessions: (req, res, clanID) ->
# TODO: add tests
2015-04-16 18:26:14 -04:00
# TODO: restrict information returned based on clan type
Clan.findById clanID, (err, clan) =>
return @sendDatabaseError(res, err) if err
2015-04-16 18:26:14 -04:00
return @sendNotFoundError(res) unless clan
return @sendForbiddenError(res) unless clan.get('dashboardType') is 'premium'
memberIDs = _.map clan.get('members') ? [], (memberID) -> memberID.toHexString?() or memberID
User.find {_id: {$in: memberIDs}}, 'name', {limit: memberLimit}, (err, users) =>
2015-04-24 17:21:57 -04:00
return @sendDatabaseError(res, err) if err
memberIDs = []
for user in users
memberIDs.push user.id
break unless memberIDs.length < memberLimit
LevelSession.find {creator: {$in: memberIDs}}, 'changed codeLanguage creator creatorName levelID levelName playtime state submittedCodeLanguage', (err, documents) =>
return @sendDatabaseError(res, err) if err?
cleandocs = (LevelSessionHandler.formatEntity(req, doc) for doc in documents)
@sendSuccess(res, cleandocs)
getPublicClans: (req, res) ->
# Return 100 public clans, sorted by member count, created date
query = [{ $match : {type : 'public'} }]
query.push {$project : {_id: 1, name: 1, slug: 1, type: 1, description: 1, memberCount: {$size: "$members"}, ownerID: 1}}
query.push {$sort: { memberCount: -1, _id: -1 }}
query.push {$limit: 100}
Clan.aggregate(query).exec (err, documents) =>
return @sendDatabaseError(res, err) if err
@sendSuccess(res, documents)
2015-04-02 14:01:37 -04:00
removeMember: (req, res, clanID, memberID) ->
return @sendForbiddenError(res) unless req.user? and not req.user.isAnonymous()
try
clanID = mongoose.Types.ObjectId(clanID)
memberID = mongoose.Types.ObjectId(memberID)
catch err
2015-04-02 14:44:18 -04:00
return @sendNotFoundError(res, err)
2015-04-02 14:01:37 -04:00
Clan.findById clanID, (err, clan) =>
return @sendDatabaseError(res, err) if err
2015-04-16 18:26:14 -04:00
return @sendNotFoundError(res) unless clan
2015-04-02 14:44:18 -04:00
return @sendForbiddenError res unless @hasAccessToDocument(req, clan)
2015-04-02 14:01:37 -04:00
return @sendForbiddenError(res) if clan.get('ownerID').equals memberID
2015-04-02 20:00:28 -04:00
Clan.update {_id: clanID}, {$pull: {members: memberID}}, (err) =>
2015-04-02 14:01:37 -04:00
return @sendDatabaseError(res, err) if err
2015-04-02 20:00:28 -04:00
User.update {_id: memberID}, {$pull: {clans: clanID}}, (err) =>
return @sendDatabaseError(res, err) if err
@sendSuccess(res)
AnalyticsLogEvent.logEvent req.user, 'Clan member removed', clanID: clanID, type: clan.get('type'), memberID: memberID
2015-04-02 14:01:37 -04:00
module.exports = new ClanHandler()