Ver código fonte

Refactored Commuity Stations in backend

theflametrooper 8 anos atrás
pai
commit
acac12fc07

+ 1 - 1
backend/logic/actions/hooks/index.js

@@ -3,5 +3,5 @@
 module.exports = {
 	loginRequired: require('./loginRequired'),
 	adminRequired: require('./adminRequired'),
-	csOwnerRequired: require('./csOwnerRequired'),
+	ownerRequired: require('./ownerRequired')
 };

+ 0 - 0
backend/logic/actions/hooks/csOwnerRequired.js → backend/logic/actions/hooks/ownerRequired.js


+ 38 - 74
backend/logic/actions/stations.js

@@ -146,9 +146,7 @@ module.exports = {
 
 		stations.getStation(stationId, (err, station) => {
 
-			if (err && err !== true) {
-				return cb({ status: 'error', message: 'An error occurred while joining the station' });
-			}
+			if (err && err !== true) return cb({ status: 'error', message: 'An error occurred while joining the station' });
 
 			if (station) {
 
@@ -255,7 +253,7 @@ module.exports = {
 		});
 	},*/
 
-	forceSkip: hooks.csOwnerRequired((session, stationId, cb) => {
+	forceSkip: hooks.ownerRequired((session, stationId, cb) => {
 		stations.getStation(stationId, (err, station) => {
 
 			if (err && err !== true) {
@@ -301,7 +299,7 @@ module.exports = {
 		});
 	},
 
-	updateDisplayName: hooks.csOwnerRequired((session, stationId, newDisplayName, cb) => {
+	updateDisplayName: hooks.ownerRequired((session, stationId, newDisplayName, cb) => {
 		db.models.station.update({_id: stationId}, {$set: {displayName: newDisplayName}}, (err) => {
 			if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the station.' });
 			stations.updateStation(stationId, () => {
@@ -311,7 +309,7 @@ module.exports = {
 		});
 	}),
 
-	updateDescription: hooks.csOwnerRequired((session, stationId, newDescription, cb) => {
+	updateDescription: hooks.ownerRequired((session, stationId, newDescription, cb) => {
 		db.models.station.update({_id: stationId}, {$set: {description: newDescription}}, (err) => {
 			if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the station.' });
 			stations.updateStation(stationId, () => {
@@ -321,7 +319,7 @@ module.exports = {
 		});
 	}),
 
-	updatePrivacy: hooks.csOwnerRequired((session, stationId, newPrivacy, cb) => {
+	updatePrivacy: hooks.ownerRequired((session, stationId, newPrivacy, cb) => {
 		db.models.station.update({_id: stationId}, {$set: {privacy: newPrivacy}}, (err) => {
 			if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the station.' });
 			stations.updateStation(stationId, () => {
@@ -331,7 +329,7 @@ module.exports = {
 		});
 	}),
 
-	updatePartyMode: hooks.csOwnerRequired((session, stationId, newPartyMode, cb) => {
+	updatePartyMode: hooks.ownerRequired((session, stationId, newPartyMode, cb) => {
 		stations.getStation(stationId, (err, station) => {
 			if (err) return cb({ status: 'failure', message: err });
 			if (station.partyMode === newPartyMode) return cb({ status: 'failure', message: 'The party mode was already ' + ((newPartyMode) ? 'enabled.' : 'disabled.') });
@@ -346,7 +344,7 @@ module.exports = {
 		});
 	}),
 
-	pause: hooks.csOwnerRequired((session, stationId, cb) => {
+	pause: hooks.ownerRequired((session, stationId, cb) => {
 		stations.getStation(stationId, (err, station) => {
 			if (err && err !== true) {
 				return cb({ status: 'error', message: 'An error occurred while pausing the station' });
@@ -372,7 +370,7 @@ module.exports = {
 		});
 	}),
 
-	resume: hooks.csOwnerRequired((session, stationId, cb) => {
+	resume: hooks.ownerRequired((session, stationId, cb) => {
 		stations.getStation(stationId, (err, station) => {
 			if (err && err !== true) {
 				return cb({ status: 'error', message: 'An error occurred while resuming the station' });
@@ -396,7 +394,7 @@ module.exports = {
 		});
 	}),
 
-	remove: hooks.csOwnerRequired((session, stationId, cb) => {
+	remove: hooks.ownerRequired((session, stationId, cb) => {
 		db.models.station.remove({ _id: stationId }, (err) => {
 			console.log(err, stationId);
 			if (err) return cb({status: 'failure', message: 'Something went wrong when deleting that station.'});
@@ -406,7 +404,7 @@ module.exports = {
 		});
 	}),
 
-	create: hooks.adminRequired((session, data, cb) => {
+	create: hooks.loginRequired((session, data, cb) => {
 		async.waterfall([
 
 			(next) => {
@@ -424,18 +422,32 @@ module.exports = {
 
 			(station, next) => {
 				if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });
-				const { _id, displayName, description, genres, playlist } = data;
-				db.models.station.create({
-					_id,
-					displayName,
-					description,
-					type: 'official',
-					privacy: 'private',
-					playlist,
-					genres,
-					currentSong: stations.defaultSong,
-					partyMode: true
-				}, next);
+				const { _id, displayName, description, genres, playlist, type } = data;
+				if (type == 'official') {
+					db.models.station.create({
+						_id,
+						displayName,
+						description,
+						type,
+						privacy: 'private',
+						playlist,
+						genres,
+						currentSong: stations.defaultSong
+					}, next);
+				} else if (type == 'community') {
+					cache.hget('sessions', session.sessionId, (err, session) => {
+						db.models.station.create({
+							_id,
+							displayName,
+							description,
+							type,
+							privacy: 'private',
+							owner: session.userId,
+							queue: [],
+							currentSong: null
+						}, next);
+					});
+				}
 			}
 
 		], (err, station) => {
@@ -444,55 +456,7 @@ module.exports = {
 			cache.pub('station.create', data._id);
 		});
 	}),
-
-	createCommunity: hooks.loginRequired((session, data, cb) => {
-		async.waterfall([
-
-			(next) => {
-				return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
-			},
-
-			// check the cache for the station
-			(next) => {
-				data._id = data._id.toLowerCase();
-				cache.hget('stations', data._id, next)
-			},
-
-			// if the cached version exist
-			(station, next) => {
-				if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists' });
-				db.models.station.findOne({$or: [{_id: data._id}, {displayName: new RegExp(`^${data.displayName}$`, 'i')}] }, next);
-			},
-
-			(station, next) => {
-				cache.hget('sessions', session.sessionId, (err, session) => {
-					next(null, station, session.userId);
-				});
-			},
-
-			(station, userId, next) => {
-				if (station) return next({ 'status': 'failure', 'message': 'A station with that name or display name already exists' });
-				const { _id, displayName, description } = data;
-				db.models.station.create({
-					_id,
-					displayName,
-					owner: userId,
-					privacy: 'private',
-					description,
-					type: "community",
-					queue: [],
-					currentSong: null
-				}, next);
-			}
-
-		], (err, station) => {
-			if (err) return cb(err);
-			console.log(err, station);
-			cache.pub('station.create', data._id);
-			return cb({ 'status': 'success', 'message': 'Successfully created station.' });
-		});
-	}),
-
+	
 	addToQueue: hooks.loginRequired((session, stationId, songId, cb, userId) => {
 		stations.getStation(stationId, (err, station) => {
 			if (err) return cb(err);
@@ -534,7 +498,7 @@ module.exports = {
 		});
 	}),
 
-	removeFromQueue: hooks.csOwnerRequired((session, stationId, songId, cb, userId) => {
+	removeFromQueue: hooks.ownerRequired((session, stationId, songId, cb, userId) => {
 		stations.getStation(stationId, (err, station) => {
 			if (err) return cb(err);
 			if (station.type === 'community') {

+ 19 - 7
frontend/components/Modals/CreateCommunityStation.vue

@@ -29,6 +29,8 @@
 </template>
 
 <script>
+	import { Toast } from 'vue-roaster';
+
 	export default {
 		data() {
 			return {
@@ -39,19 +41,29 @@
 				}
 			}
 		},
+		ready: function () {
+			let _this = this;
+			let socketInterval = setInterval(() => {
+				if (!!_this.$parent.socket) {
+					_this.socket = _this.$parent.socket;
+					clearInterval(socketInterval);
+				}
+			}, 100);
+		},
 		methods: {
 			toggleModal: function () {
 				this.$dispatch('toggleModal', 'createCommunityStation');
 			},
 			submitModal: function () {
 				let _this = this;
-				if (_this.community._id == '') return Toast.methods.addToast('ID cannot be a blank field', 3000);
-				if (_this.community.displayName == '') return Toast.methods.addToast('Display Name cannot be a blank field', 3000);
-				if (_this.community.description == '') return Toast.methods.addToast('Description cannot be a blank field', 3000);
-				this.socket.emit('stations.createCommunity', {
-					_id: _this.community._id,
-					displayName: _this.community.displayName,
-					description: _this.community.description
+				if (_this.newCommunity._id == '') return Toast.methods.addToast('ID cannot be a blank field', 3000);
+				if (_this.newCommunity.displayName == '') return Toast.methods.addToast('Display Name cannot be a blank field', 3000);
+				if (_this.newCommunity.description == '') return Toast.methods.addToast('Description cannot be a blank field', 3000);
+				this.socket.emit('stations.create', {
+					_id: _this.newCommunity._id,
+					type: 'community',
+					displayName: _this.newCommunity.displayName,
+					description: _this.newCommunity.description
 				}, res => {
 					if (res.status === 'success') Toast.methods.addToast(`You have added the station successfully`, 4000);
 					else Toast.methods.addToast(res.message, 4000);

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

@@ -19,7 +19,7 @@
 			</div>
 		</div>
 		<div class="group">
-			<div class="group-title">Community Stations <i class="material-icons ccs-button" @click="toggleModal('ccs')" v-if="$parent.loggedIn">add</i></div>
+			<div class="group-title">Community Stations <i class="material-icons ccs-button" @click="toggleModal('createCommunityStation')" v-if="$parent.loggedIn">add</i></div>
 			<div class="group-stations">
 				<div class="stations-station" v-for="station in stations.community" v-link="{ path: '/community/' + station._id }" @click="this.$dispatch('joinStation', station._id)" v-bind:class="station.class">
 					<img class="station-image" :src="station.currentSong.thumbnail" onerror="this.src='/assets/notes.png'" />