forked from serrynaimo/getaroom_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
146 lines (126 loc) · 4.92 KB
/
server.js
File metadata and controls
146 lines (126 loc) · 4.92 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
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
// Require modules
var express = require('express'),
cors = require('cors'),
redis = require('redis'),
nodemailer = require('nodemailer'),
ses = require('nodemailer-ses-transport'),
sns = require('sns-mobile'),
extend = require('extend');
// App/Express settings
app = exports.app = express();
app.set('port', process.env.PORT || 8001)
.set('env', process.env.NODE_ENV || 'local');
app.use(cors());
// Configuration and Constants
var config = require('./config/' + app.get('env') + '.js'),
emailPattern = /^[a-z0-9_+.-]+@[a-z0-9.-]+\.[a-z0-9]{2,}$/;
// Transport initilization
var notificationTransport = new sns(config.aws),
mailTransport = nodemailer.createTransport(ses(extend(config.aws, {
region: 'us-west-2', // ap-southeast-1 not supported
rateLimit: 5
}))),
client = redis.createClient();
// Helper functions
var decodeId = function (id) {
if(!id || id.length <= 6)
return "";
id = id.replace("-", "/").replace("_","+") + "=";
return new Buffer(id, 'base64').toString('ascii');
},
sendEmail = function(caller, callee, room, callback) {
mailTransport.sendMail({
from: caller + ' <' + config.sendFromEmail + '>',
replyTo: caller,
to: callee,
subject: "Join me on a video call right now",
text: "Hey there,\n\n" + caller + " is waiting for you on " + config.domain + " to be joined for a video call. It's a free tool and you don't need to sign up or install anything. Just follow this link and put pants on ;)\n\nhttp://" + config.domain + "/" + room + "\n\n"
}, callback);
},
sendNotification = function(caller, endpointArn, room, callback) {
var notification = {
default: caller + ' is calling. Go to http://' + config.domain + '/' + room + ' to accept the call.',
ADM: JSON.stringify({
data: {
message: caller + ' is calling ...',
room: room,
email: caller
},
expiresAfter: 60
}),
GCM: JSON.stringify({
data: {
message: caller + ' is calling ...',
room: room,
email: caller
},
time_to_live: 60
})
};
notificationTransport.sendMessage(endpointArn, notification, callback);
};
// Event subscriptions
client.on("error", function(err) {
console.log("Redis error: " + err);
});
// Endpoint definitions
app.get('/register', function (req, res) {
if(req.query.id && req.query.device) {
if(req.query.device.indexOf('.adm-registration.') < 0) {
console.log("Did not register device request for '" + req.query.device + "'");
res.status(200).send('OK');
return;
}
notificationTransport.addUser(req.query.device, null, function(err, endpointArn) {
if(err) {
console.log("SNS Error:" + err);
return res.status(500).send('SNS Error');
}
client.set(req.query.id, endpointArn, redis.print);
console.log("Registered '" + req.query.device + "' with endpoint " + endpointArn);
res.status(200).send('OK');
});
}
else {
res.status(400).send('Bad Request');
}
});
app.get('/call', function (req, res) {
var caller = decodeId(req.query.caller),
callee = decodeId(req.query.callee);
if(!caller.match(emailPattern) || !callee.match(emailPattern)) {
res.status(400).send('Bad Request');
return;
}
client.get(req.query.callee, function(err, endpointArn){
var mailCallback = function(err) {
if(err) {
console.log('SES Error: Email could not be delivered to ' + callee + '. ' + err);
res.status(500).send('Server Error');
}
else {
console.log("Sent Email to " + callee);
res.status(200).send('OK');
}
},
notificationCallback = function(err, messageId) {
if(err) {
console.log('SNS Error: Notification could not be delivered to ' + endpointArn + '. ' + err);
sendEmail(caller, callee, req.query.caller, mailCallback);
} else {
console.log("Sent Notification from " + caller + " to " + endpointArn);
res.status(200).send('OK');
}
};
if(endpointArn) {
sendNotification(caller, endpointArn, req.query.caller, notificationCallback);
}
else {
sendEmail(caller, callee, req.query.caller, mailCallback);
}
});
});
// Startup
app.listen(app.get('port'));
console.log('Server envorinment: ' + app.get('env') +
' - API listening on port: ' + app.get('port'));