forked from dudeofea/ratt-signal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ratt-signal.js
136 lines (130 loc) · 3.89 KB
/
ratt-signal.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
AllUsers = new Mongo.Collection('users');
//process.env.COMPE_SLACK = ""; //Uncomment and put key here
//process.env.WE_NEED_A_SLACK = ""; //Uncomment and put key here
if (Meteor.isClient) {
var db_ready = false;
Session.set('light_image', 'signal.png');
//wait for db to be ready
Meteor.subscribe('db_ready', function(){
if(!db_ready){
db_ready = true;
//get previous user_id
//create a new user if doesn't exist
var user_id = Session.get('user_id');
if(AllUsers.find({"_id": user_id}).count() < 1){
user_id = Random.id();
AllUsers.insert({_id: user_id, checked_in: 0});
Session.setPersistent('user_id', user_id);
}
}
});
Template.registerHelper('format_date', function(date) {
if (!date){
return "";
}
return date.getHours() % 12 + ":" +
(date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes() )
+ " "
+ (date.getHours() >= 12 ? "PM" : "AM");
});
Template.ratt.helpers({
user_count: function () {
return AllUsers.find({checked_in: 1}).count();
},
light_image: function() {
var turnOn = AllUsers.find({checked_in: 1}).count();
//console.log(turnOn);
if(turnOn > 0){
//Turn on
return 'signal-filled.png';
}
else{
//Turn off
return 'signal.png';
}
},
activate_status: function() {
var user_id = Session.get('user_id');
var info = AllUsers.findOne({_id: user_id});
if(info == null){
//default
return 'Activate';
}
if(info['checked_in'] == 1){
//turn on
return 'Deactivate';
}else{
//turn off
return 'Activate';
}
},
user_list: function(){
return AllUsers.find({checked_in: 1}).fetch();
}
});
Template.ratt.events({
'submit .nameForm': function (event) {
event.preventDefault();
//debugger;
//console.log("Triggered");
var user_id = Session.get("user_id");
var info = AllUsers.findOne({_id: user_id});
if(!(info['checked_in'] == 1) && !event.target.name.value){
alert("Please enter your name!");
}
else{
//toggle in the user status (and lightbulb)
AllUsers.update({_id: user_id}, {checked_in: 1 - info['checked_in'], name: event.target.name.value, time: new Date() });
if(!(info['checked_in'] == 1) && event.target.name.value){
Meteor.call('activateRattSignalSlack', event.target.name.value, function(err,response) {
if(err) {
Console.log("Error:" + err.reason);
return;
}
});
}
}
}
});
}
if (Meteor.isServer) {
var rattSignalBotCompE;
var rattSignalBotWeNeedASlack;
var slackParams = {
icon_emoji: ':rotating_light:'
};
Meteor.startup(function() {
var SlackBot = Meteor.npmRequire('slackbots');
var slackParams = {
icon_emoji: ':rotating_light:'
};
rattSignalBotCompE = new SlackBot({
token: process.env.COMPE_SLACK,
name: "rattsignal"
});
rattSignalBotWeNeedASlack = new SlackBot({
token: process.env.WE_NEED_A_SLACK,
name: "rattsignal"
});
/* For commands later
rattSignalBotCompE.on('start', function() {
//rattSignalBot.postMessageToChannel('ratt-signal', 'Hello World, I am the Ratt Signal!', slackParams);
});
rattSignalBotCompE.on('message', function(data) {
//console.log(data);
});
*/
Meteor.methods({
activateRattSignalSlack: function (name) {
rattSignalBotCompE.postMessageToChannel('ratt-signal', ':rotating_light: ' + name + ' has activated the Ratt Signal! :rotating_light:', slackParams);
rattSignalBotWeNeedASlack.postMessageToChannel('ratt-signal', ':rotating_light: ' + name + ' has activated the Ratt Signal! :rotating_light:', slackParams);
}
});
});
Meteor.publish('db_ready', function(){
return AllUsers.find({});
});
Meteor.setInterval(function() {
AllUsers.remove({$and:[{checked_in: 1}, {time: {$lt: new Date((new Date())-1000*60*60*3) }}]});
}, 300000);
}