The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/server/methods/browseChannels.js

104 lines
2.2 KiB

8 years ago
import s from 'underscore.string';
const sortChannels = function(field, direction) {
switch (field) {
case 'createdAt':
return {
ts: direction === 'asc' ? 1 : -1
};
default:
return {
[field]: direction === 'asc' ? 1 : -1
};
}
};
const sortUsers = function(field, direction) {
switch (field) {
default:
return {
[field]: direction === 'asc' ? 1 : -1
};
}
};
Meteor.methods({
browseChannels({text = '', type = 'channels', sortBy = 'name', sortDirection = 'asc', page = 0, limit = 10}) {
8 years ago
const regex = new RegExp(s.trim(s.escapeRegExp(text)), 'i');
8 years ago
if (!['channels', 'users'].includes(type)) {
8 years ago
return;
}
8 years ago
if (!['asc', 'desc'].includes(sortDirection)) {
return;
}
if (!['name', 'createdAt', ...type === 'channels' ? ['usersCount'] : [], ...type === 'users' ? ['username'] : []].includes(sortBy)) {
8 years ago
return;
}
8 years ago
page = page > -1 ? page : 0;
limit = limit > 0 ? limit : 10;
const options = {
skip: limit * page,
limit
};
8 years ago
const user = Meteor.user();
if (type === 'channels') {
8 years ago
const sort = sortChannels(sortBy, sortDirection);
if (!RocketChat.authz.hasPermission(user._id, 'view-c-room')) {
return;
}
return {
results: RocketChat.models.Rooms.findByNameAndType(regex, 'c', {
...options,
sort,
fields: {
description: 1,
topic: 1,
name: 1,
lastMessage: 1,
ts: 1,
archived: 1,
usersCount: 1
}
}).fetch(),
total: RocketChat.models.Rooms.findByNameAndType(regex, 'c').count()
};
8 years ago
}
8 years ago
// type === users
8 years ago
if (!RocketChat.authz.hasPermission(user._id, 'view-outside-room') || !RocketChat.authz.hasPermission(user._id, 'view-d-room')) {
return;
}
const sort = sortUsers(sortBy, sortDirection);
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()
};
8 years ago
}
});
DDPRateLimiter.addRule({
type: 'method',
name: 'browseChannels',
userId(/*userId*/) {
return true;
}
}, 100, 100000);