/
user.publications.js
74 lines (63 loc) · 1.78 KB
/
user.publications.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { _ } from 'meteor/underscore';
import { check, Match } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { Roles } from 'meteor/alanning:roles';
import deepExtend from 'deep-extend';
import { MANAGER_ROLES } from '../../lib/roles';
// publish current user
Meteor.publish('user', function(...args) {
check(args, Match.Maybe({}));
return Meteor.users.find(
{ _id: this.userId },
{ fields: { services: 1, history: 1, status: 1 } },
);
});
// publish all users
Meteor.smartPublish('users', function(opts) {
check(opts, {
room: Match.Maybe(String), // the current room
contacts: Match.Maybe([Match.Any]), // include contacts
});
const userProjection = {
fields: { profile: 1, history: 1, status: 1, services: 1 },
};
if (Roles.userIsInRole(this.userId, MANAGER_ROLES)) {
return Meteor.users.find({}, userProjection);
}
const res = [];
res.push(Meteor.users.find(this.userId, userProjection));
if (opts.room) {
res.push(Roles.getUsersInRole(opts.room, Roles.GLOBAL_GROUP, deepExtend({
fields: {
'roles.__global_roles__.$': 1,
},
}, userProjection)));
}
if (opts.contacts) {
res.push(Meteor.users.find({
'services.google.email': { $in: _.pluck(opts.contacts, 'email') },
}, {
fields: {
profile: 1,
status: 1,
},
}));
}
return res;
// TODO: we should really restrict which users are visible
// this.ready()
});
Meteor.publish('contacts', function(contacts) {
check(contacts, Match.Maybe([{ email: Match.Maybe(String) }]));
if (!contacts || !contacts.length) {
this.ready();
}
return Meteor.users.find({
'services.google.email': { $in: _.pluck(contacts, 'email') },
}, {
fields: {
profile: 1,
status: 1,
},
});
});