Browse Source

Added users and user number updating functionality

theflametrooper 8 years ago
parent
commit
b29fa4bdbd

+ 14 - 64
backend/logic/coreHandler.js

@@ -160,11 +160,24 @@ module.exports = {
 				playlist: station.playlist,
 				displayName: station.displayName,
 				description: station.description,
-				currentSongIndex: station.currentSongIndex
+				currentSongIndex: station.currentSongIndex,
+				users: station.users
 			}
 		}));
 	},
 
+	'/stations/join/:id': (id, cb) => {
+		stations.getStation(id).users = stations.getStation(id).users + 1;
+		cb(stations.getStation(id).users);
+	},
+
+	'/stations/leave/:id': (id, cb) => {
+		if (stations.getStation(id)) {
+			stations.getStation(id).users = stations.getStation(id).users - 1;
+			if (cb) cb(stations.getStation(id).users);
+		}
+	},
+
 	'/youtube/getVideo/:query': (query, cb) => {
 		const params = [
 			'part=snippet',
@@ -282,68 +295,5 @@ module.exports = {
 		} else {
 			cb({err: "Not logged in."});
 		}
-	},
-
-	'/stations/search/:query': (query, cb) => {
-
-		const params = [
-			'part=snippet',
-			`q=${encodeURIComponent(query)}`,
-			`key=${config.get('apis.youtube.key')}`,
-			'type=video',
-			'maxResults=25'
-		].join('&');
-
-		request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
-			if (err) {
-				return cb({ status: 'error', message: 'Failed to make request' });
-			} else {
-				try {
-					return cb({ status: 'success', body: JSON.parse(body) });
-				}
-				catch (e) {
-					return cb({ status: 'error', message: 'Non JSON response' });
-				}
-			}
-		});
-	},
-
-	'/song/:id/toggleLike': (songId, userId, cb) => {
-
-		var user = global.db.user.findOne(userId);
-		var song = global.db.song.findOne(songId);
-		if (user !== undefined) {
-			if (song !== undefined) {
-				var liked = false;
-				if (song.likes.indexOf(userId) === -1) {
-					liked = true;
-					// Add like
-				} else {
-					// Remove like
-				}
-				if (song.dislikes.indexOf(userId) !== -1) {
-					// Remove dislike
-				}
-				// Emit to all sockets with this user that their likes/dislikes updated.
-				// Emit to all sockets in the room that the likes/dislikes has updated
-				cb({liked: liked, disliked: false});
-			} else {
-				cb({err: "Song not found."});
-			}
-		} else {
-			cb({err: "User not found."});
-		}
-
-	},
-
-	'/user/:id/ratings': (userId, cb) => {
-
-		var user = global.db.user.findOne(userId);
-		if (user !== undefined) {
-			cb({likes: user.likes, dislikes: user.dislikes});
-		} else {
-			cb({err: "User not found."});
-		}
-
 	}
 };

+ 5 - 18
backend/logic/expressHandler.js

@@ -6,36 +6,23 @@ const passport  = require('passport');
 module.exports = (core, app) => {
 
 	app.post('/users/login', passport.authenticate('local'), function(req, res) {
-		console.log("Logged in:", req.user);
 		res.json(JSON.stringify(req.user));
 	});
 
-	app.post('/users/logout', function(req, res) {
-		req.logout();
-		res.end();
-	});
-
 	app.post('/users/register', function(req, res) {
 		core['/users/register'](req.body.username, req.body.email, req.body.password, req.body.recaptcha, result => {
 			res.send(JSON.stringify(result));
 		});
 	});
 
+	app.post('/users/logout', function(req, res) {
+		req.logout();
+		res.end();
+	});
+
 	app.post('/stations', (req, res) => {
 		core['/stations'](result => {
 			res.send(result);
 		});
 	});
-
-	app.get('/stations/join/:id', (req, res) => {
-		core['/stations/join/:id'](req.params.id, result => {
-			res.send(JSON.stringify(result));
-		});
-	});
-
-	app.get('/stations/search/:query', (req, res) => {
-		core['/stations/search/:query'](req.params.query, result => {
-			res.send(JSON.stringify(result));
-		});
-	});
 };

+ 14 - 5
backend/logic/socketHandler.js

@@ -5,18 +5,27 @@ module.exports = (core, io) => {
 	io.on('connection', socket => {
 		console.log("User has connected");
 		let _user = socket.request.user;
+		let _currentStation = "";
 
 		socket.on('disconnect', () => {
+			core['/stations/leave/:id'](_currentStation);
+			_currentStation = "";
 			console.log('User has disconnected');
 		});
 
-		/*socket.on('/station/:id/join', (id, cb) => {
-			console.log("JOINED!!!");
-			core['/station/:id/join'](id, socket.id, result => {
-				console.log("CALLBACK!!!");
+		socket.on('/stations/join/:id', (id, cb) => {
+			core['/stations/join/:id'](id, result => {
+				_currentStation = id;
 				cb(result);
 			});
-		});*/
+		});
+
+		socket.on('/stations/leave/:id', (id, cb) => {
+			core['/stations/leave/:id'](id, result => {
+				_currentStation = "";
+				cb(result);
+			});
+		});
 
 		socket.on('/youtube/getVideo/:query', (query, cb) => {
 			core['/youtube/getVideo/:query'](query, result => {

+ 10 - 9
backend/logic/stations.js

@@ -27,6 +27,7 @@ module.exports = {
 			});
 
 			this.id = id;
+			this.users = 0;
 			this.playlist = data.playlist;
 			this.currentSongIndex = data.currentSongIndex;
 			this.paused = data.paused;
@@ -108,15 +109,15 @@ module.exports = {
 	addStation: (station) => {
 		stations.push(station);
 	},
-	// getStation: id => {
-	// 	let result;
-	// 	stations.forEach(function(station) {
-	// 		if (station.getId() === id) {
-	// 			result = station;
-	// 		}
-	// 	});
-	// 	return result;
-	// },
+	getStation: id => {
+		let result;
+		stations.forEach(function(station) {
+			if (station.id === id) {
+				result = station;
+			}
+		});
+		return result;
+	},
 	// Returns stations list when POSTing to '/stations'
 	getStations: () => {
 		return stations;

+ 19 - 2
frontend/App.vue

@@ -99,8 +99,25 @@
 					}
 				});
 			},
-			'joinStation': id => {
-
+			'joinStation': function(id) {
+				let local = this;
+				local.socket.emit('/stations/join/:id', id, (result) => {
+					local.stations.forEach(function(station) {
+						if (station.id === id) {
+							station.users = result;
+						}
+					});
+				});
+			},
+			'leaveStation': function(id) {
+				let local = this;
+				local.socket.emit('/stations/leave/:id', id, (result) => {
+					local.stations.forEach(function(station) {
+						if (station.id === id) {
+							station.users = result;
+						}
+					});
+				});
 			}
 		}
 	}

+ 1 - 1
frontend/build/index.html

@@ -17,6 +17,6 @@
 	<script src='https://www.google.com/recaptcha/api.js'></script>
 </head>
 <body>
-	<script src="bundle.js"></script>
+	<script src="./bundle.js"></script>
 </body>
 </html>

+ 1 - 1
frontend/components/StationHeader.vue

@@ -1,7 +1,7 @@
 <template>
 	<nav class="nav has-shadow">
 		<div class="nav-left">
-			<a class="nav-item" href="#" v-link="{ path: '/' }">
+			<a class="nav-item" href="#" v-link="{ path: '/' }" @click="this.$dispatch('leaveStation', title)">
 				<span class="icon">
 					<i class="fa fa-home"></i>
 				</span>

+ 2 - 2
frontend/components/pages/Home.vue

@@ -55,7 +55,7 @@
 		<div class="group">
 			<!--<div class="group-title">{{group.name}}</div>-->
 			<div class="group-stations">
-				<div class="stations-station" v-for="station in $parent.stations" v-link="{ path: '/station/' + station.id }">
+				<div class="stations-station" v-for="station in $parent.stations" v-link="{ path: '/station/' + station.id }" @click="this.$dispatch('joinStation', station.id)">
 					<img class="station-image" :src="station.playlist[station.currentSongIndex].thumbnail" />
 					<div class="station-info">
 						<div class="station-grid-left">
@@ -63,7 +63,7 @@
 							<p>{{ station.description }}</p>
 						</div>
 						<div class="station-grid-right">
-							<!--<div>{{ station.users }}&nbsp;&nbsp;<i class="fa fa-user" aria-hidden="true"></i></div>-->
+							<div>{{ station.users }}&nbsp;&nbsp;<i class="fa fa-user" aria-hidden="true"></i></div>
 						</div>
 					</div>
 				</div>