forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupsList.js
More file actions
42 lines (36 loc) · 1.17 KB
/
groupsList.js
File metadata and controls
42 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Meteor.methods({
groupsList: function(nameFilter, limit, sort) {
check(nameFilter, Match.Optional(String));
check(limit, Match.Optional(Number));
check(sort, Match.Optional(String));
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'groupsList' });
}
let options = {
fields: { name: 1 },
sort: { name: 1 }
};
//Verify the limit param is a number
if (_.isNumber(limit)) {
options.limit = limit;
}
//Verify there is a sort option and it's a string
if (_.trim(sort)) {
switch (sort) {
case 'name':
options.sort = { name: 1 };
break;
case 'msgs':
options.sort = { msgs: -1 };
break;
}
}
//Determine if they are searching or not, base it upon the name field
if (nameFilter) {
return { groups: RocketChat.models.Rooms.findByTypeAndNameContainingUsername('p', new RegExp(s.trim(s.escapeRegExp(nameFilter)), 'i'), Meteor.user().username, options).fetch() };
} else {
let roomIds = _.pluck(RocketChat.models.Subscriptions.findByTypeAndUserId('p', Meteor.userId()).fetch(), 'rid');
return { groups: RocketChat.models.Rooms.findByIds(roomIds, options).fetch() };
}
}
});