-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
177 lines (161 loc) · 5.23 KB
/
app.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var express = require('express')
, http = require('http')
, path = require('path')
, io = require('socket.io')
, dotenv = require('dotenv')
, mysql = require('mysql')
, googleapis = require('googleapis')
, OAuth2Client = googleapis.OAuth2Client;
var global_socket;
dotenv.load();
var e = module.exports;
e.ENV = process.env.NODE_ENV || 'development';
var sendgrid_apikey = process.env.SENDGRID_APIKEY;
sendgrid = require('sendgrid')(sendgrid_apikey);
var oauth2Client = new OAuth2Client(process.env.GLASS_CLIENT_ID,
process.env.GLASS_SECRET, process.env.GLASS_CALLBACK);
// Setup express
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.use(express.favicon('public/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
// Setup our socket
var serv_io = io.listen(server);
serv_io.sockets.on('connection', function (socket){
global_socket = socket;
socket.emit('message', {'text': 'Reveal.js initialized.'});
socket.on('notes', function(notes){
console.log(notes);
gotToken(notes);
});
});
// We have the token, use it to post to Google Glass timeline
var success = function (data) {
console.log('success', data);
};
var failure = function (data) {
console.log('failure', data);
};
var gotToken = function (msg) {
googleapis
.discover('mirror', 'v1')
.execute(function (err, client) {
if (!!err) {
failure();
return;
}
console.log('mirror client', client);
insertToTimeline(client, failure, success, msg);
});
};
// Setup Google Authentication
var grabToken = function (code, errorCallback, successCallback) {
oauth2Client.getToken(code, function (err, tokens) {
if (!!err) {
errorCallback(err);
} else {
console.log('tokens', tokens);
oauth2Client.credentials = tokens;
successCallback();
}
});
};
// Send the notes onto a timeline card with a delete option
var insertToTimeline = function (client, errorCallback, successCallback, msg) {
client
.mirror.timeline.insert(
{
"text": msg,
"callbackUrl": "https://revealjs-teleprompter.appspot.com/forward?url=http://localhost:3000/reply",
"menuItems": [
{"action": "REPLY"},
{"action": "DELETE"}
]
}
)
.withAuthClient(oauth2Client)
.execute(function (err, data) {
if (!!err)
errorCallback(err);
else
successCallback(data);
});
};
// Routes
app.get('/', function (req, res) {
if (!oauth2Client.credentials) {
// generates a url that allows offline access and asks permissions
// for Mirror API scope.
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/glass.timeline'
});
res.redirect(url);
}
res.redirect('/presentation');
});
app.get('/oauth2callback', function (req, res) {
// if we're able to grab the token, redirect the user back to the main page
grabToken(req.query.code, failure, function () {
res.redirect('/presentation');
});
});
// The slide presentation is served up here
app.get('/presentation', function(req, res){
res.sendfile(__dirname + '/views/index.html');
});
// Handle the incoming email votes, where the text in the subject is the vote
app.post('/inbound', function(req, res){
var parsed_envelope = JSON.parse(req.body.envelope);
var to = parsed_envelope.from
var vote = req.body.subject;
// Store the votes in a DB
var connection = mysql.createConnection({
host : process.env.MYSQL_HOST,
database : process.env.MYSQL_DB,
user : process.env.MYSQL_USER,
password : process.env.MYSQL_PASSWORD
});
connection.connect(function(err){
if(err != null) {
console.log('Error connecting to mysql:' + err+'\n');
}
});
var params = [ to, vote ];
connection.query('INSERT INTO `votes` (`email`, `vote`) VALUES (?, ?);', params, function(err, rows){
if(err != null) {
console.log("Query error:" + err);
}
});
connection.end();
// Live update the chart
global_socket.emit("message", vote);
// Send confirmation to the voter via SendGrid
var Email = sendgrid.Email;
var email = new Email({
to: to,
from: "[email protected]",
subject: "Your vote for " + vote + " has been recorded!",
text: "Contents of the email go here."
});
sendgrid.send(email, function(err, json) {
if (err) {
console.error(err);
console.log(err.message);
} else {
console.log("Email sent to " + to);
}
});
res.send(200);
});