Skip to content
Snippets Groups Projects
Commit 07cb9abf authored by Marcos Spessatto Defendi's avatar Marcos Spessatto Defendi Committed by Rodrigo Nascimento
Browse files

[FIX] Missing pagination fields in the response of REST /directory endpoint (#10840)

* Add missing pagination fields in the response of REST /directory endpoint

* Add support to choose sort field in REST directory
parent 2244ba9c
No related branches found
No related tags found
No related merge requests found
......@@ -145,12 +145,17 @@ RocketChat.API.v1.addRoute('directory', { authRequired: true }, {
const { sort, query } = this.parseJsonQuery();
const { text, type } = query;
const sortDirection = sort && sort === 1 ? 'asc' : 'desc';
if (sort && Object.keys(sort).length > 1) {
return RocketChat.API.v1.failure('This method support only one "sort" parameter');
}
const sortBy = sort ? Object.keys(sort)[0] : undefined;
const sortDirection = sort && Object.values(sort)[0] === 1 ? 'asc' : 'desc';
const result = Meteor.runAsUser(this.userId, () => Meteor.call('browseChannels', {
text,
type,
sort: sortDirection,
sortBy,
sortDirection,
page: offset,
limit: count
}));
......@@ -158,6 +163,11 @@ RocketChat.API.v1.addRoute('directory', { authRequired: true }, {
if (!result) {
return RocketChat.API.v1.failure('Please verify the parameters');
}
return RocketChat.API.v1.success({ result });
return RocketChat.API.v1.success({
result: result.results,
count: result.results.length,
offset,
total: result.total
});
}
});
......@@ -12,8 +12,8 @@ function timeAgo(time) {
}
function directorySearch(config, cb) {
return Meteor.call('browseChannels', config, (err, results) => {
cb(results && results.length && results.map(result => {
return Meteor.call('browseChannels', config, (err, result) => {
cb(result.results && result.results.length && result.results.map(result => {
if (config.type === 'channels') {
return {
name: result.name,
......
......@@ -24,7 +24,7 @@ const sortUsers = function(field, direction) {
Meteor.methods({
browseChannels({text='', type = 'channels', sortBy = 'name', sortDirection = 'asc', page = 0, limit = 10}) {
browseChannels({text = '', type = 'channels', sortBy = 'name', sortDirection = 'asc', page = 0, limit = 10}) {
const regex = new RegExp(s.trim(s.escapeRegExp(text)), 'i');
if (!['channels', 'users'].includes(type)) {
......@@ -35,7 +35,7 @@ Meteor.methods({
return;
}
if (!['name', 'createdAt', ...type === 'channels'? ['usernames'] : [], ...type === 'users' ? ['username'] : []].includes(sortBy)) {
if (!['name', 'createdAt', ...type === 'channels' ? ['usernames'] : [], ...type === 'users' ? ['username'] : []].includes(sortBy)) {
return;
}
......@@ -55,17 +55,20 @@ Meteor.methods({
if (!RocketChat.authz.hasPermission(user._id, 'view-c-room')) {
return;
}
return RocketChat.models.Rooms.findByNameAndType(regex, 'c', {
...options,
sort,
fields: {
description: 1,
name: 1,
ts: 1,
archived: 1,
usernames: 1
}
}).fetch();
return {
results: RocketChat.models.Rooms.findByNameAndType(regex, 'c', {
...options,
sort,
fields: {
description: 1,
name: 1,
ts: 1,
archived: 1,
usernames: 1
}
}).fetch(),
total: RocketChat.models.Rooms.findByNameAndType(regex, 'c').count()
};
}
// type === users
......@@ -73,16 +76,19 @@ Meteor.methods({
return;
}
const sort = sortUsers(sortBy, sortDirection);
return RocketChat.models.Users.findByActiveUsersExcept(text, [user.username], {
...options,
sort,
fields: {
username: 1,
name: 1,
createdAt: 1,
emails: 1
}
}).fetch();
return {
results: RocketChat.models.Users.findByActiveUsersExcept(text, [user.username], {
...options,
sort,
fields: {
username: 1,
name: 1,
createdAt: 1,
emails: 1
}
}).fetch(),
total: RocketChat.models.Users.findByActiveUsersExcept(text, [user.username]).count()
};
}
});
......
......@@ -139,6 +139,9 @@ describe('miscellaneous', function() {
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('result').and.to.be.an('array');
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
expect(res.body.result[0]).to.have.property('_id');
expect(res.body.result[0]).to.have.property('createdAt');
expect(res.body.result[0]).to.have.property('username');
......@@ -160,6 +163,36 @@ describe('miscellaneous', function() {
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('result').and.to.be.an('array');
expect(res.body.result[0]).to.have.property('_id');
expect(res.body.result[0]).to.have.property('name');
expect(res.body.result[0]).to.have.property('usernames').and.to.be.an('array');
expect(res.body.result[0]).to.have.property('ts');
})
.end(done);
});
it('should return an array(result) when search by channel with sort params correctly and execute succesfully', (done) => {
request.get(api('directory'))
.set(credentials)
.query({
query: JSON.stringify({
text: testChannel.name,
type: 'channels'
}),
sort: JSON.stringify(({
name: 1
}))
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('result').and.to.be.an('array');
expect(res.body.result[0]).to.have.property('_id');
expect(res.body.result[0]).to.have.property('name');
......@@ -168,7 +201,6 @@ describe('miscellaneous', function() {
})
.end(done);
});
it('should return an error when send invalid query', (done) => {
request.get(api('directory'))
.set(credentials)
......@@ -185,6 +217,26 @@ describe('miscellaneous', function() {
})
.end(done);
});
it('should return an error when have more than one sort parameter', (done) => {
request.get(api('directory'))
.set(credentials)
.query({
query: JSON.stringify({
text: testChannel.name,
type: 'channels'
}),
sort: JSON.stringify(({
name: 1,
test: 1
}))
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
});
});
describe('[/spotlight]', () => {
let user;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment