Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Add support to queries in channels.members and groups.members endpoints #21414

Merged
merged 11 commits into from
May 14, 2021
Merged
12 changes: 11 additions & 1 deletion app/api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ API.v1.addRoute('channels.members', { authRequired: true }, {

const { offset, count } = this.getPaginationItems();
const { sort = {} } = this.parseJsonQuery();
const { status, username, name } = this.queryParams;

if (status && !Array.isArray(status)) {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
throw new Meteor.Error('error-status-param-not-an-array', 'The parameter "status" should be an array of strings');
}

const subscriptions = Subscriptions.findByRoomId(findResult._id, {
fields: { 'u._id': 1 },
sampaiodiego marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -588,8 +593,13 @@ API.v1.addRoute('channels.members', { authRequired: true }, {
const total = subscriptions.count();

const members = subscriptions.fetch().map((s) => s.u && s.u._id);
const query = {
name,
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
username,
status: status ? { $in: status } : undefined,
};

const users = Users.find({ _id: { $in: members } }, {
const users = Users.find({ ...query, _id: { $in: members } }, {
fields: { _id: 1, username: 1, name: 1, status: 1, statusText: 1, utcOffset: 1 },
sort: { username: sort.username != null ? sort.username : 1 },
}).fetch();
Expand Down
12 changes: 11 additions & 1 deletion app/api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,11 @@ API.v1.addRoute('groups.members', { authRequired: true }, {

const { offset, count } = this.getPaginationItems();
const { sort = {} } = this.parseJsonQuery();
const { status, username, name } = this.queryParams;
KevLehman marked this conversation as resolved.
Show resolved Hide resolved

if (status && !Array.isArray(status)) {
throw new Meteor.Error('error-status-param-not-an-array', 'The parameter "status" should be an array of strings');
}

const subscriptions = Subscriptions.findByRoomId(findResult.rid, {
fields: { 'u._id': 1 },
Expand All @@ -511,8 +516,13 @@ API.v1.addRoute('groups.members', { authRequired: true }, {
const total = subscriptions.count();

const members = subscriptions.fetch().map((s) => s.u && s.u._id);
const query = {
name,
username,
status: status ? { $in: status } : undefined,
};

const users = Users.find({ _id: { $in: members } }, {
const users = Users.find({ ...query, _id: { $in: members } }, {
fields: { _id: 1, username: 1, name: 1, status: 1, statusText: 1, utcOffset: 1 },
sort: { username: sort.username != null ? sort.username : 1 },
}).fetch();
Expand Down