forked from louiswilliams/queueup-spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
520 lines (425 loc) · 14.6 KB
/
server.js
File metadata and controls
520 lines (425 loc) · 14.6 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/**
* QueueUp Server
*/
var apiRouter = require('./routes/api');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var express = require('express');
var fs = require('fs');
var logger = require('morgan');
var mongo = require('mongodb');
var monk = require('monk');
var passport = require('passport');
var passportRouter = require('./routes/passport');
var path = require('path');
var playlistRouter = require('./routes/playlists');
var routes = require('./routes/index');
var session = require('express-session');
var spotifyRouter = require('./routes/spotify');
var SpotifyWebApi = require('spotify-web-api-node');
var utils = require('./utils');
var spotifyConfig = JSON.parse(fs.readFileSync(__dirname + '/spotify.key', {encoding: 'utf8'}));
var envConf = JSON.parse(fs.readFileSync(__dirname + '/env.json', {encoding: 'utf8'}));
var ObjectId = mongo.ObjectID;
var db = monk('localhost:27017/' + envConf.database);
var Playlists = db.get('playlists');
var Users = db.get('users');
Playlists.index({location: "2dsphere"});
// Initialize Spotify web api
var spotify = new SpotifyWebApi({
clientId: spotifyConfig.clientId,
clientSecret: spotifyConfig.clientSecret,
redirectUri: spotifyConfig.redirectUri
});
var app = express();
// Initialize server and socket.io
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.enable('trust proxy');
app.use(logger('common'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req,res,next) {
req.db = db;
req.Playlists = Playlists;
req.Users = Users;
req.spotify = spotify; // Make the API available to following middleware
req.io = io;
next();
});
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(session({
secret: 'queuemeup',
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
// Routes
app.use('/', routes);
app.use('/playlist', playlistRouter);
app.use('/spotify', spotifyRouter);
app.use('/auth', passportRouter);
app.use('/api', apiRouter);
/* 404 Handler */
app.use(function (req, res, next) {
res.status(404);
if (req.accepts('html')) {
return res.render('404');
}
if (req.accepts('json')) {
return res.json({error: "Page not found"});
}
return res.type('txt').send("Not found");
});
// Start server
server.listen(envConf.port, function() {
console.log("Server (%s) started on port %d", envConf.name, server.address().port);
});
io.use(function (socket, next) {
console.log("SOCKET " + socket.nsp.name);
next();
});
// Handle client connection and authentication
io.on('connection', function(socket) {
console.log("Socket connection...");
/* Wait for socket API authentication */
socket.on("auth", function (data) {
var client_token = data.client_token;
var user_id = data.user_id;
try {
user_id = new ObjectId(user_id);
} catch (err) {
return socket.emit("auth_response", {error: err});
}
/* Both client_token and user_id must be send together */
if (client_token && user_id) {
Users.findOne({
_id: user_id,
client_token: client_token
}).success(function (user) {
/* If the user_id/token pair was found */
if (user) {
console.log("Authenticated socket client");
/* Attach new socket listeners*/
socket.emit("auth_response");
subscribeListen(user, socket);
} else {
console.log("Client not found");
/* Don't proceed to other middleware if client ID isn't verified */
socket.emit("auth_response", {error: {
message: "Client not found"
}});
}
}).error(function (err) {
console.log(err);
socket.emit("auth_response", {error: err});
});
} else {
console.log("client_token and user_id not both sent");
/* Again, Client token is not universally unique, so it must be sent with an user_id */
socket.emit("auth_response", {error: {
message: "client_token and user_id not both sent"
}});
}
});
/*
BEGIN old socket listeners
*/
// Request authentication
socket.emit('auth_request');
// Authentication received
socket.on('auth_send', function(data) {
console.log("Auth response from client...",data);
// Find client key in DB
if (typeof(data) == 'string') {
console.log("Weird. Data coming in as string. Parsing anyways...");
data = JSON.parse(data);
}
Playlists.findOne({_id: new ObjectId(data.id)}).success(function (playlist) {
if(playlist) {
// Success
console.log("Autheticated successfully...");
socket.emit('auth_success');
// Join a Socket room identified by the playlist's key
socket.join(playlist._id);
var queue = (playlist.tracks) ? playlist.tracks : [];
// Send an initial update
utils.sendStateChange({
Playlists: Playlists,
Users: Users
}, socket, playlist, "playlist_connect");
socket.on('client_play_pause', function(play_state) {
if (typeof(play_state) == 'string') {
console.log("Weird. Data coming in as string. Parsing anyways...");
play_state = JSON.parse(play_state);
}
console.log("Client play/pause: ", play_state);
Playlists.findAndModify(
{_id: playlist._id},
{ $set: {
play: play_state.playing
}},
{"new": true}
).success(function (playlist) {
utils.emitStateChange({
io: io,
Playlists: Playlists,
Users: Users
}, playlist, "client_play_pause");
}).error(function (err) {
console.log(err);
});
});
// Capture track_finished event from playlist
socket.on('track_finished', function() {
console.log("Track finished... Going to next");
Playlists.findOne({_id: playlist._id}).success(function (playlist) {
utils.skipTrack({
Users: Users,
Playlists: Playlists
}, playlist, function(result, err) {
console.log(result, err);
// do something?
});
}).error(function (err) {
console.log(err);
});
});
// Capture playing progress
socket.on('track_progress', function(update) {
// console.log("Track at " + update.progress + "/" + update.duration);
io.to(playlist._id).emit('track_progress_update', {
progress: update.progress,
duration: update.duration
});
});
} else {
// Key not found in DB
console.log("Invalid playlist...");
socket.emit('auth_fail', {message: "Playlist ID invalid"});
}
}).error(function (err) {
// DB error
console.log("Error finding playlist:", err);
socket.emit('auth_fail', {message: "DB Error finding playlist" , error: err});
});
});
});
/* New Socket listeners */
function subscribeListen(user, socket) {
var client_subscription;
var player_subscription;
/* Disconnect handler */
socket.on('disconnect', function () {
console.log("Client disconnecting");
/* Clean up */
clientUnsubscribe();
playerUnsubscribe();
});
/* Client requests subscription to a playlist's updates */
socket.on('client_subscribe', function (data) { clientSubscribe(data); });
/* ON "playlist:player:request" */
socket.on('player_subscribe', function (data) { playerSubscribe(data); });
/* Client requests to unsubscribe from updates */
socket.on('client_unsubscribe', function (data) { clientUnsubscribe(); });
/* ON "playlist:player:disconnect" */
socket.on('player_unsubscribe', function (data) { playerUnsubscribe(); });
/* Register listeners for player updates */
function playerListen() {
console.log("Registering player listeners");
/* Player is sending progress updates */
socket.on("track_progress", function (data) {
// console.log("Updating progress", data);
/* Don't do anything if no longer current (possibly from forced override) */
isCurrentPlayer(function () {
io.to(player_subscription).emit('track_progress_update', {
progress: data.progress,
duration: data.duration
});
}, function (reason) {
console.log(reason);
socket.disconnect();
});
});
/* Player track is over and requesting new track*/
socket.on("track_finished", function (data) {
console.log("Track finished... Going to next");
/* Don't do anything if no longer current (from forced override) */
isCurrentPlayer(function () {
/* Get the most recent playlist and skip the track */
Playlists.findOne({_id: player_subscription}).success(function (playlist) {
utils.skipTrack({
Playlists: Playlists,
Users: Users
}, playlist, function(result, err) {
utils.emitStateChange({
io: io,
Playlists: Playlists,
Users: Users
}, result, "track_finished");
});
}).error(function (err) {
console.log(err);
});
}, function (reason) {
console.log(reason);
socket.disconnect();
});
});
/* Player device is paused */
socket.on("track_play_pause", function (data) {
console.log("Client play/pause: ", data);
var playing = data.playing;
/* Don't do anything if no longer current (from forced override) */
isCurrentPlayer(function () {
/* Update the DB */
Playlists.findAndModify(
{
_id: player_subscription
}, {
$set: {
play: playing,
last_updated: new Date().getTime()
}
},
{"new": true}
).success(function (playlist) {
utils.emitStateChange({
io: io,
Playlists: Playlists,
Users: Users
}, playlist, "track_play_pause");
}).error(function (err) {
console.log(err);
});
}, function (reason) {
console.log(reason);
socket.disconnect();
});
});
}
function playerStopListen() {
socket.off("track_progress");
socket.off("track_finished");
socket.off("track_play_pause");
}
function isCurrentPlayer(trueCall, falseCall) {
/* Check the playlist id*/
Playlists.findOne({_id: player_subscription}).success( function (playlist) {
/* If the user is the admin of a playlist */
if (playlist) {
/* Because they are both objects, compare them as strings...*/
if (JSON.stringify(playlist.player) == JSON.stringify(user._id)) {
trueCall();
} else {
falseCall("User " + user._id + " is not current player (" + playlist.player +")");
}
} else {
falseCall("Playlist " + player_subscription + " not found");
}
}).error (function (err) {
console.log(err);
falseCall(err);
});
}
function clientSubscribe(data) {
if (data) {
var playlist_id = data.playlist_id;
/* Find the playlist */
Playlists.findOne({_id: new ObjectId(playlist_id)}).success(function (playlist) {
if (playlist) {
/* Save the currently subscribed playlist */
client_subscription = playlist._id;
/* Join the socket to the this playlists's socket room */
socket.join(playlist._id);
/* Send an initial state_change event to populate */
utils.sendStateChange({
Playlists: Playlists,
Users: Users
}, socket, playlist, "client_subscribe");
}
});
console.log("Client Subscribing");
} else {
console.log("Data undefined");
}
}
function playerSubscribe (data) {
var playlist_id = data.playlist_id;
var force = (data.force) ? true : false;
/* In the case that the server crashes before it can clean up*/
if (force) {
console.log("Forcing play subscription...");
}
/* Check the playlist id*/
Playlists.findOne({_id: playlist_id, admin: user._id}).success( function (playlist) {
/* If the user is the admin of a playlist */
if (playlist) {
/* If the playlist already has a registerd player */
if (playlist.player && !force) {
socket.emit("player_subscribe_response", {error: {
message: "Player already connected. Disconnect first..."
}});
/* Register the player in the DB */
} else {
Playlists.update({_id: playlist._id}, {
$set: {
player: user._id,
play: true
}
}, {"new": true}).success(function (playlist) {
/* Record the subscription */
player_subscription = playlist_id;
/* Subscribe the player as a client */
clientSubscribe(data);
/* Register player listeners */
playerListen();
});
}
} else {
console.log("No playlist " + playlist_id + " with " + user._id + " as an admin");
socket.emit("player_subscribe_response", {error: {message: "No playlist with this user as an admin"}});
}
}).error (function (err) {
console.log(err);
socket.emit("player_subscribe_response", {error: err});
});
console.log("Player subscribing");
}
function clientUnsubscribe() {
/* Check if a client is subscribed */
if (client_subscription) {
/* Remove the socket from the room */
socket.leave(client_subscription);
client_subscription = null;
} else {
console.log("No clent subscription");
}
console.log("Client unsubscribing");
}
function playerUnsubscribe() {
/* Check if a the socket is a player, and if so, invalidate it */
if (player_subscription) {
Playlists.update({_id: player_subscription}, {
$set: {
player: null
}
}).success(function (playlist) {
player_subscription = null;
/* Stop listening to play updates*/
// playerStopListen();
/* Unsubscribe as a client */
clientUnsubscribe();
}).error(function (err) {
console.log(err);
});
} else {
console.log("No player subscription");
}
console.log("Player unsubscribing");
}
}