Browse Source

Made station show up on homepage after creation.

KrisVos130 8 years ago
parent
commit
36027c18a9
2 changed files with 44 additions and 16 deletions
  1. 25 9
      backend/logic/actions/stations.js
  2. 19 7
      backend/logic/stations.js

+ 25 - 9
backend/logic/actions/stations.js

@@ -13,14 +13,6 @@ const stations = require('../stations');
 const songs = require('../songs');
 const hooks = require('./hooks');
 
-cache.sub('station.locked', stationId => {
-	io.io.to(`station.${stationId}`).emit("event:stations.locked");
-});
-
-cache.sub('station.unlocked', stationId => {
-	io.io.to(`station.${stationId}`).emit("event:stations.unlocked");
-});
-
 cache.sub('station.pause', stationId => {
 	io.io.to(`station.${stationId}`).emit("event:stations.pause");
 });
@@ -42,7 +34,30 @@ cache.sub('station.queueUpdate', stationId => {
 cache.sub('station.create', stationId => {
 	stations.initializeStation(stationId, (err, station) => {
 		//TODO Emit to admin station page
-		io.io.to('home').emit("event:stations.created", station);
+
+		// TODO If community, check if on whitelist
+		if (station.privacy === 'public') io.io.to('home').emit("event:stations.created", station);
+		else {
+			let sockets = io.io.to('home').sockets;
+			for (let socketId in sockets) {
+				let socket = sockets[socketId];
+				let session = sockets[socketId].session;
+				if (session.sessionId) {
+					cache.hget('sessions', session.sessionId, (err, session) => {
+						if (!err && session) {
+							db.models.user.findOne({_id: session.userId}, (err, user) => {
+								if (user.role === 'admin') socket.emit("event:stations.created", station);
+								else if (station.type === "community" && station.owner === session.userId) socket.emit("event:stations.created", station);
+							});
+						}
+					});
+				}
+			}
+		}
+
+		function func() {
+			io.io.to('home').emit("event:stations.created", station);
+		}
 	});
 });
 
@@ -443,6 +458,7 @@ module.exports = {
 
 		], (err, station) => {
 			if (err) console.error(err);
+			console.log(err, station);
 			cache.pub('station.create', data._id);
 			return cb({ 'status': 'success', 'message': 'Successfully created station.' });
 		});

+ 19 - 7
backend/logic/stations.js

@@ -43,7 +43,7 @@ module.exports = {
 	},
 
 	initializeStation: function(stationId, cb) {
-		console.log(112233, stationId);
+		console.log(112233, stationId, cb);
 		if (typeof cb !== 'function') cb = ()=>{};
 		let _this = this;
 		_this.getStation(stationId, (err, station) => {
@@ -63,18 +63,26 @@ module.exports = {
 							if (isNaN(timeLeft)) timeLeft = -1;
 							if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
 								console.log("Test");
-								_this.skipStation(station._id)();
+								this.skipStation(station._id)((err, station) => {
+									console.log(45, err, station);
+									cb(err, station);
+								});
 							} else {
 								notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft);
+								cb(null, station);
 							}
 						} else {
-							_this.skipStation(station._id)();
+							_this.skipStation(station._id)((err, station) => {
+								console.log(47, err, station);
+								cb(err, station);
+							});
 						}
 					} else {
 						notifications.unschedule(`stations.nextSong?id${station._id}`);
+						cb(null, station);
 					}
-				}
-			}
+				} else cb("Station not found.");
+			} else cb(err);
 		});
 	},
 
@@ -175,7 +183,8 @@ module.exports = {
 
 	skipStation: function(stationId) {
 		let _this = this;
-		return () => {
+		return (cb) => {
+			if (typeof cb !== 'function') cb = ()=>{};
 			console.log("###2");
 			console.log("NOTIFICATION!!!");
 			_this.getStation(stationId, (err, station) => {
@@ -349,13 +358,16 @@ module.exports = {
 								console.log("22", !!(station.currentSong));
 								utils.socketsLeaveSongRooms(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
 							}
-						}
+							console.log(33, null, station, cb);
+							cb(null, station);
+						} else cb(err);
 					});
 				}
 				// the station doesn't exist anymore, unsubscribe from it
 				else {
 					console.log(112233445566, "REMOVE NOTIFICATION");
 					notifications.remove(notification);
+					cb("Station not found.");
 				}
 			});
 		}