From 17fe2438fbc2479c084ae978e326c3b3689e8c4d Mon Sep 17 00:00:00 2001 From: Arun Galva Date: Fri, 2 Mar 2018 03:16:09 -0500 Subject: [PATCH] Fixed sorting channels by number of users --- .../client/views/app/directory.html | 2 +- server/methods/browseChannels.js | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/rocketchat-ui/client/views/app/directory.html b/packages/rocketchat-ui/client/views/app/directory.html index 1095fa858637..d538528d43b8 100644 --- a/packages/rocketchat-ui/client/views/app/directory.html +++ b/packages/rocketchat-ui/client/views/app/directory.html @@ -23,7 +23,7 @@ Name {{> icon icon="sort" }} - Users {{> icon icon="sort" }} + Users {{> icon icon="sort" }} Created At {{> icon icon="sort" }} diff --git a/server/methods/browseChannels.js b/server/methods/browseChannels.js index a2dc672884b6..1a4011cdd4e0 100644 --- a/server/methods/browseChannels.js +++ b/server/methods/browseChannels.js @@ -22,6 +22,19 @@ const sortUsers = function(field, direction) { } }; +const sortByUsersCount = function(sortDirection) { + if (!['asc', 'desc'].includes(sortDirection)) { + return; + } + return function(roomA, roomB) { + const numUsersInRoomA = roomA.usernames.length; + const numUsersInRoomB = roomB.usernames.length; + + if (sortDirection === 'asc') { return numUsersInRoomA - numUsersInRoomB; } + if (sortDirection === 'desc') { return numUsersInRoomB - numUsersInRoomA; } + }; +}; + Meteor.methods({ browseChannels({text='', type = 'channels', sortBy = 'name', sortDirection = 'asc', page = 0, limit = 10}) { @@ -35,7 +48,7 @@ Meteor.methods({ return; } - if (!['name', 'createdAt', ...type === 'channels'? ['usernames'] : [], ...type === 'users' ? ['username'] : []].includes(sortBy)) { + if (!['name', 'createdAt', 'usersCount', ...type === 'channels'? ['usernames'] : [], ...type === 'users' ? ['username'] : []].includes(sortBy)) { return; } @@ -55,7 +68,7 @@ Meteor.methods({ if (!RocketChat.authz.hasPermission(user._id, 'view-c-room')) { return; } - return RocketChat.models.Rooms.findByNameAndType(regex, 'c', { + const channels = RocketChat.models.Rooms.findByNameAndType(regex, 'c', { ...options, sort, fields: { @@ -66,6 +79,14 @@ Meteor.methods({ usernames: 1 } }).fetch(); + + if (sortBy === 'usersCount') { + const sortChannels = channels.slice(); + sortChannels.sort(sortByUsersCount(sortDirection)); + return sortChannels; + } + + return channels; } // type === users