Forráskód Böngészése

Added admin pages pub/sub.

KrisVos130 8 éve
szülő
commit
bc32a8273a

+ 10 - 2
backend/logic/actions/apis.js

@@ -2,7 +2,8 @@
 
 const request = require('request'),
 	  config  = require('config'),
-		utils = require('../utils');
+		utils = require('../utils'),
+		hooks = require('./hooks');
 
 module.exports = {
 
@@ -40,6 +41,13 @@ module.exports = {
 			utils.socketJoinRoom(session.socketId, page);
 		}
 		cb({});
-	}
+	},
+
+	joinAdminRoom: hooks.adminRequired((session, page, cb) => {
+		if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports') {
+			utils.socketJoinRoom(session.socketId, `admin.${page}`);
+		}
+		cb({});
+	})
 
 };

+ 10 - 6
backend/logic/actions/queueSongs.js

@@ -9,15 +9,19 @@ const config = require('config');
 const request = require('request');
 const hooks = require('./hooks');
 
-notifications.subscribe('queue.newSong', songId => {
-	utils.emitToRoom('admin.queue', 'event:song.new', { songId });
+cache.sub('queue.newSong', songId => {
+	console.log(123321);
+	db.models.queueSong.findOne({_id: songId}, (err, song) => {
+		console.log(err, song);
+		utils.emitToRoom('admin.queue', 'event:admin.queueSong.added', song);
+	});
 });
 
-notifications.subscribe('queue.removedSong', songId => {
-	utils.emitToRoom('admin.queue', 'event:song.removed', { songId });
+cache.sub('queue.removedSong', songId => {
+	utils.emitToRoom('admin.queue', 'event:admin.queueSong.removed', songId);
 });
 
-notifications.subscribe('queue.updatedSong', songId => {
+cache.sub('queue.updatedSong', songId => {
 	//TODO Retrieve new Song object
 	utils.emitToRoom('admin.queue', 'event:song.updated', { songId });
 });
@@ -51,7 +55,7 @@ module.exports = {
 	remove: hooks.adminRequired((session, songId, cb) => {
 		db.models.queueSong.remove({ _id: songId }, (err, res) => {
 			if (err) return cb({ status: 'failure', message: err.message });
-			//TODO Pub/sub for (queue)songs on admin pages.
+			cache.pub('queue.removedSong', songId);
 			cb({ status: 'success', message: 'Song was removed successfully' });
 		});
 	}),

+ 21 - 9
backend/logic/actions/songs.js

@@ -8,12 +8,13 @@ const utils = require('../utils');
 const hooks = require('./hooks');
 const queueSongs = require('./queueSongs');
 
-cache.sub('song.like', (data) => {
-	utils.emitToRoom(`song.${data.songId}`, 'event:song.like', {songId: data.songId, undisliked: data.undisliked});
-	utils.socketsFromUser(data.userId, (sockets) => {
-		sockets.forEach((socket) => {
-			socket.emit('event:song.newRatings', {songId: data.songId, liked: true, disliked: false});
-		});
+cache.sub('song.removed', songId => {
+	utils.emitToRoom('admin.songs', 'event:admin.song.removed', songId);
+});
+
+cache.sub('song.added', songId => {
+	db.models.queueSong.findOne({_id: songId}, (err, song) => {
+		utils.emitToRoom('admin.songs', 'event:admin.song.added', song);
 	});
 });
 
@@ -64,7 +65,14 @@ module.exports = {
 	}),
 
 	remove: hooks.adminRequired((session, songId, cb) => {
-		db.models.song.remove({ _id: songId });
+		db.models.song.remove({ _id: songId }, (err) => {
+			if (err) return cb({status: 'failure', message: err.message});
+			cache.hdel('songs', songId, (err) => {
+				if (err) return cb({status: 'failure', message: err.message});
+				cache.pub('song.removed', songId);
+				cb({status: 'success', message: 'Successfully removed the song.'});
+			});
+		});
 	}),
 
 	add: hooks.adminRequired((session, song, cb, userId) => {
@@ -76,8 +84,12 @@ module.exports = {
 				newSong.acceptedAt = Date.now();
 				if (!existingSong) newSong.save(err => {
 					console.log(err, 1);
-					if (err) console.error(err);
-					else cb({ status: 'success', message: 'Song has been moved from Queue' })
+					if (err) {
+						cb({ status: 'failure', message: 'Something went wrong while adding the song to the queue.' });
+					} else {
+						cache.pub('song.added', songId);
+						cb({ status: 'success', message: 'Song has been moved from Queue' });
+					}
 				});
 			});
 			//TODO Check if video is in queue and Add the song to the appropriate stations

+ 6 - 0
backend/logic/actions/stations.js

@@ -35,10 +35,15 @@ cache.sub('station.voteSkipSong', stationId => {
 	utils.emitToRoom(`station.${stationId}`, "event:song.voteSkipSong");
 });
 
+cache.sub('station.remove', stationId => {
+	utils.emitToRoom('admin.stations', 'event:admin.station.removed', stationId);
+});
+
 cache.sub('station.create', stationId => {
 	stations.initializeStation(stationId, (err, station) => {
 		console.log("*************", err, station);
 		//TODO Emit to admin station page
+		utils.emitToRoom('admin.stations', 'event:admin.station.added', station);
 
 		// TODO If community, check if on whitelist
 		console.log("*************", station.privacy);
@@ -391,6 +396,7 @@ module.exports = {
 			console.log(err, stationId);
 			if (err) return cb({status: 'failure', message: 'Something went wrong when deleting that station.'});
 			cache.hdel('stations', stationId, () => {
+				cache.pub('station.remove', stationId);
 				return cb({ status: 'success', message: 'Station successfully removed' });
 			});
 		});

+ 20 - 3
frontend/components/Admin/QueueSongs.vue

@@ -130,18 +130,35 @@
 				});
 			},
 			remove: function (id, index) {
-				this.songs.splice(index, 1);
 				this.socket.emit('queueSongs.remove', id, res => {
 					if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
 				});
+			},
+			init: function() {
+				let _this = this;
+				_this.socket.emit('queueSongs.index', data => {
+					_this.songs = data;
+				});
+				_this.socket.emit('apis.joinAdminRoom', 'queue', data => {});
 			}
 		},
 		ready: function () {
 			let _this = this;
 			io.getSocket((socket) => {
 				_this.socket = socket;
-				_this.socket.emit('queueSongs.index', data => {
-					_this.songs = data;
+				if (_this.socket.connected) {
+					_this.init();
+					_this.socket.on('event:admin.queueSong.added', queueSong => {
+						_this.songs.push(queueSong);
+					});
+					_this.socket.on('event:admin.queueSong.removed', songId => {
+						_this.songs = _this.songs.filter(function(song) {
+							return song._id !== songId;
+						});
+					});
+				}
+				io.onConnect(() => {
+					_this.init();
 				});
 			});
 

+ 23 - 4
frontend/components/Admin/Songs.vue

@@ -41,6 +41,7 @@
 	import { Toast } from 'vue-roaster';
 
 	import EditSong from '../Modals/EditSong.vue';
+	import io from '../../io';
 
 	export default {
 		components: { EditSong },
@@ -123,18 +124,36 @@
 				});
 			},
 			remove: function (id, index) {
-				this.songs.splice(index, 1);
 				this.socket.emit('songs.remove', id, res => {
-					if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
+					if (res.status == 'success') Toast.methods.addToast(res.message, 4000);
+					else Toast.methods.addToast(res.message, 8000);
 				});
+			},
+			init: function() {
+				let _this = this;
+				_this.socket.emit('songs.index', data => {
+					_this.songs = data;
+				});
+				_this.socket.emit('apis.joinAdminRoom', 'songs', data => {});
 			}
 		},
 		ready: function () {
 			let _this = this;
 			io.getSocket((socket) => {
 				_this.socket = socket;
-				_this.socket.emit('songs.index', data => {
-					_this.songs = data;
+				if (_this.socket.connected) {
+					_this.init();
+					_this.socket.on('event:admin.song.added', song => {
+						_this.songs.push(song);
+					});
+					_this.socket.on('event:admin.song.removed', songId => {
+						_this.songs = _this.songs.filter(function(song) {
+							return song._id !== songId;
+						});
+					});
+				}
+				io.onConnect(() => {
+					_this.init();
 				});
 			});
 

+ 22 - 4
frontend/components/Admin/Stations.vue

@@ -119,7 +119,7 @@
 			},
 			removeStation: function (index) {
 				this.socket.emit('stations.remove', this.stations[index]._id, res => {
-					if (res.status == 'success') this.stations.splice(index, 1); Toast.methods.addToast(res.message, 3000);
+					Toast.methods.addToast(res.message, 3000);
 				});
 			},
 			addGenre: function () {
@@ -137,14 +137,32 @@
 				if (genre) this.newStation.blacklistedGenres.push(genre);
 				else Toast.methods.addToast('Genre cannot be empty', 3000);
 			},
-			removeBlacklistedGenre: function (index) { this.newStation.blacklistedGenres.splice(index, 1); }
+			removeBlacklistedGenre: function (index) { this.newStation.blacklistedGenres.splice(index, 1); },
+			init: function() {
+				let _this = this;
+				_this.socket.emit('stations.index', data => {
+					_this.stations = data.stations;
+				});
+				_this.socket.emit('apis.joinAdminRoom', 'stations', data => {});
+			}
 		},
 		ready: function () {
 			let _this = this;
 			io.getSocket((socket) => {
 				_this.socket = socket;
-				_this.socket.emit('stations.index', data => {
-					_this.stations = data.stations;
+				if (_this.socket.connected) {
+					_this.init();
+				}
+				_this.socket.on('event:admin.station.added', station => {
+					_this.stations.push(station);
+				});
+				_this.socket.on('event:admin.station.removed', stationId => {
+					_this.stations = _this.stations.filter(function(station) {
+						return station._id !== stationId;
+					});
+				});
+				io.onConnect(() => {
+					_this.init();
 				});
 			});
 		}