diff --git a/app/schemas/models/clan.schema.coffee b/app/schemas/models/clan.schema.coffee index e7a3a4856..9dbc6f5cf 100644 --- a/app/schemas/models/clan.schema.coffee +++ b/app/schemas/models/clan.schema.coffee @@ -4,7 +4,6 @@ ClanSchema = c.object {title: 'Clan', required: ['name', 'type']} c.extendNamedProperties ClanSchema # name first _.extend ClanSchema.properties, - name: c.shortString() type: {type: 'string', 'enum': ['public']} ownerID: c.objectId() members: c.array {title: 'Members'}, c.objectId() diff --git a/app/templates/clans/clan-details.jade b/app/templates/clans/clan-details.jade index 8a5a71875..08ce5a0e4 100644 --- a/app/templates/clans/clan-details.jade +++ b/app/templates/clans/clan-details.jade @@ -42,5 +42,3 @@ block content td if isOwner && member.id !== clan.get('ownerID') button.btn.btn-sm.btn-warning.remove-member-btn(data-id="#{member.id}") Remove Member - - button.btn.btn-sm Load More diff --git a/app/templates/clans/clans.jade b/app/templates/clans/clans.jade index c0082accb..a0cd6a731 100644 --- a/app/templates/clans/clans.jade +++ b/app/templates/clans/clans.jade @@ -45,8 +45,6 @@ block content else if clan.get('ownerID') !== me.id button.btn.btn-sm.btn-warning.leave-clan-btn(data-id="#{clan.id}") Leave Clan - button.btn.btn-sm Load More - .tab-pane#my-clans(role='tabpanel') table.table.table-striped.table-condensed thead @@ -73,5 +71,3 @@ block content td if clan.get('ownerID') !== me.id button.btn.btn-sm.btn-warning.leave-clan-btn(data-id="#{clan.id}") Leave Clan - - button.btn.btn-sm Load More diff --git a/app/views/clans/ClanDetailsView.coffee b/app/views/clans/ClanDetailsView.coffee index 46ba658c7..fe355e556 100644 --- a/app/views/clans/ClanDetailsView.coffee +++ b/app/views/clans/ClanDetailsView.coffee @@ -49,7 +49,7 @@ module.exports = class ClanDetailsView extends RootView @supermodel.loadModel @owner, 'owner', cache: false @supermodel.loadModel @clan, 'clan', cache: false - @members = new CocoCollection([], { url: "/db/clan/#{@clanID}/members", model: User, comparator:'_id' }) + @members = new CocoCollection([], { url: "/db/clan/#{@clanID}/members", model: User, comparator:'slug' }) @listenTo @members, 'sync', => @stats.averageLevel = Math.round(@members.reduce(((sum, member) -> sum + member.level()), 0) / @members.length) @render?() diff --git a/app/views/clans/ClansView.coffee b/app/views/clans/ClansView.coffee index 7c7eca841..0a03bb1cf 100644 --- a/app/views/clans/ClansView.coffee +++ b/app/views/clans/ClansView.coffee @@ -35,12 +35,20 @@ module.exports = class MainAdminView extends RootView initData: -> @idNameMap = {} - @publicClans = new CocoCollection([], { url: '/db/clan', model: Clan, comparator:'_id' }) + + sortClanList = (a, b) -> + if a.get('members').length isnt b.get('members').length + a.get('members').length < b.get('members').length + else + b.id.localeCompare(a.id) + @publicClans = new CocoCollection([], { url: '/db/clan/-/public', model: Clan, comparator: sortClanList }) @listenTo @publicClans, 'sync', => + for clan in @publicClans.models + console.log clan.get('name') @refreshNames @publicClans.models @render?() @supermodel.loadCollection(@publicClans, 'public_clans', {cache: false}) - @myClans = new CocoCollection([], { url: '/db/user/-/clans', model: Clan, comparator:'_id' }) + @myClans = new CocoCollection([], { url: '/db/user/-/clans', model: Clan, comparator: sortClanList }) @listenTo @myClans, 'sync', => @refreshNames @myClans.models @render?() diff --git a/server/clans/clan_handler.coffee b/server/clans/clan_handler.coffee index 3594d9201..3fb8f6626 100644 --- a/server/clans/clan_handler.coffee +++ b/server/clans/clan_handler.coffee @@ -47,6 +47,7 @@ ClanHandler = class ClanHandler extends Handler return @leaveClan(req, res, args[0]) if args[1] is 'leave' return @getMemberAchievements(req, res, args[0]) if args[1] is 'member_achievements' return @getMembers(req, res, args[0]) if args[1] is 'members' + return @getPublicClans(req, res) if args[1] is 'public' return @removeMember(req, res, args[0], args[2]) if args.length is 3 and args[1] is 'remove' super(arguments...) @@ -101,6 +102,16 @@ ClanHandler = class ClanHandler extends Handler cleandocs = (UserHandler.formatEntity(req, doc) for doc in users) @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, members: 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) + removeMember: (req, res, clanID, memberID) -> return @sendForbiddenError(res) unless req.user? and not req.user.isAnonymous() try diff --git a/test/server/functional/clan.spec.coffee b/test/server/functional/clan.spec.coffee index ac5fec3de..b1b49e6f6 100644 --- a/test/server/functional/clan.spec.coffee +++ b/test/server/functional/clan.spec.coffee @@ -69,22 +69,22 @@ describe 'Clans', -> expect(res.statusCode).toBe(422) done() - it 'Get clans', (done) -> + it 'Get public clans', (done) -> loginNewUser (user1) -> createClan user1, 'public', (clan1) -> createClan user1, 'public', (clan2) -> - request.get {uri: clanURL }, (err, res, body) -> + request.get {uri: "#{clanURL}/-/public" }, (err, res, body) -> expect(err).toBeNull() expect(res.statusCode).toBe(200) expect(body.length).toBeGreaterThan(1) done() - it 'Get clans anonymous', (done) -> + it 'Get public clans anonymous', (done) -> loginNewUser (user1) -> createClan user1, 'public', (clan1) -> createClan user1, 'public', (clan2) -> logoutUser -> - request.get {uri: clanURL }, (err, res, body) -> + request.get {uri: "#{clanURL}/-/public" }, (err, res, body) -> expect(err).toBeNull() expect(res.statusCode).toBe(200) expect(body.length).toBeGreaterThan(1)