Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/rocketchat-api/server/v1/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,29 @@ 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
}));

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
});
}
});
4 changes: 2 additions & 2 deletions packages/rocketchat-ui/client/views/app/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 29 additions & 23 deletions server/methods/browseChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}

Expand All @@ -55,34 +55,40 @@ 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
if (!RocketChat.authz.hasPermission(user._id, 'view-outside-room') || !RocketChat.authz.hasPermission(user._id, 'view-d-room')) {
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()
};
}
});

Expand Down
54 changes: 53 additions & 1 deletion tests/end-to-end/api/00-miscellaneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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)
Expand All @@ -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;
Expand Down