Skip to content

Commit

Permalink
Merge pull request #8635 from RocketChat/hotfix/channel-members-sort
Browse files Browse the repository at this point in the history
[FIX] API channel/group.members not sorting
  • Loading branch information
rodrigok committed Oct 24, 2017
2 parents e2b0379 + 6818826 commit 7fb054f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 10 additions & 4 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,20 @@ RocketChat.API.v1.addRoute('channels.members', { authRequired: true }, {
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();

const members = RocketChat.models.Rooms.processQueryOptionsOnResult(Array.from(findResult.usernames), {
sort: sort ? sort : -1,
let sortFn = (a, b) => a > b;
if (Match.test(sort, Object) && Match.test(sort.username, Number) && sort.username === -1) {
sortFn = (a, b) => b < a;
}

const members = RocketChat.models.Rooms.processQueryOptionsOnResult(Array.from(findResult.usernames).sort(sortFn), {
skip: offset,
limit: count
});

const users = RocketChat.models.Users.find({ username: { $in: members } },
{ fields: { _id: 1, username: 1, name: 1, status: 1, utcOffset: 1 } }).fetch();
const users = RocketChat.models.Users.find({ username: { $in: members } }, {
fields: { _id: 1, username: 1, name: 1, status: 1, utcOffset: 1 },
sort: sort ? sort : { username: 1 }
}).fetch();

return RocketChat.API.v1.success({
members: users,
Expand Down
14 changes: 10 additions & 4 deletions packages/rocketchat-api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,20 @@ RocketChat.API.v1.addRoute('groups.members', { authRequired: true }, {
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();

const members = RocketChat.models.Rooms.processQueryOptionsOnResult(Array.from(findResult._room.usernames), {
sort: sort ? sort : -1,
let sortFn = (a, b) => a > b;
if (Match.test(sort, Object) && Match.test(sort.username, Number) && sort.username === -1) {
sortFn = (a, b) => b < a;
}

const members = RocketChat.models.Rooms.processQueryOptionsOnResult(Array.from(findResult._room.usernames).sort(sortFn), {
skip: offset,
limit: count
});

const users = RocketChat.models.Users.find({ username: { $in: members } },
{ fields: { _id: 1, username: 1, name: 1, status: 1, utcOffset: 1 } }).fetch();
const users = RocketChat.models.Users.find({ username: { $in: members } }, {
fields: { _id: 1, username: 1, name: 1, status: 1, utcOffset: 1 },
sort: sort ? sort : { username: 1 }
}).fetch();

return RocketChat.API.v1.success({
members: users,
Expand Down

0 comments on commit 7fb054f

Please sign in to comment.