forked from BenjaminRH/meteor-event-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (58 loc) · 1.74 KB
/
server.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
//////////////////////////////////
//= SETUP HOOKS OBJECT
//////////////////////////////////
Hooks = {
onLoseFocus: function(){},
onGainFocus: function(){},
onCloseSession: function(){},
onLoggedIn: function(){},
onLoggedOut: function(){},
onCreateUser: function(){},
onDeleteUser: function(){}
};
//////////////////////////////////
//= SETUP METEOR METHODS
//////////////////////////////////
Meteor.methods({
eventsOnLoseFocus: function () {
// Fire the loseFocus event
Hooks.onLoseFocus(this.userId);
},
eventsOnGainFocus: function () {
// Fire the gainFocus event
Hooks.onGainFocus(this.userId);
},
eventsOnCloseSession: function () {
// Fire the closeSession event
Hooks.onCloseSession(this.userId);
},
eventsOnLoggedIn: function () {
// Fire the loggedIn event
Hooks.onLoggedIn(this.userId);
},
eventsOnLoggedOut: function (userId) {
// Fire the loggedOut event
Hooks.onLoggedOut(userId);
}
});
//////////////////////////////////
//= SETUP USER MONITORING
//////////////////////////////////
var currentUsers = Meteor.users.find().count();
var userCount;
// Begin monitoring users
Meteor.users.find({}, { limit: 1, sort: { createdAt: -1 } }).observeChanges({
added: function (id, fields) {
userCount = Meteor.users.find().count();
if (userCount > currentUsers) {
currentUsers = userCount;
Hooks.onCreateUser(id); // Fire the event on the server
}
}
});
Meteor.users.find().observeChanges({
removed: function (id) {
currentUsers = Meteor.users.find().count();
Hooks.onDeleteUser(id); // Fire the event on the server
}
});