Browse Source

Started working on community stations.

KrisVos130 8 years ago
parent
commit
7a3db18100

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

@@ -31,6 +31,14 @@ cache.sub('station.resume', stationId => {
 	});
 });
 
+cache.sub('station.queueUpdate', stationId => {
+	stations.getStation(stationId, (err, station) => {
+		if (!err) {
+			io.io.to(`station.${stationId}`).emit("event:queue.update", station.queue);
+		}
+	});
+});
+
 cache.sub('station.create', stationId => {
 	console.log(111);
 	stations.initializeStation(stationId, (err, station) => {
@@ -332,4 +340,84 @@ module.exports = {
 		});
 	}),
 
+	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) => cache.hget('stations', data._id, next),
+
+			// if the cached version exist
+			(station, next) => {
+				if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });
+				db.models.station.findOne({ _id: data._id }, next);
+			},
+
+			(station, next) => {
+				if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });
+				const { _id, displayName, description } = data;
+				db.models.station.create({
+					_id,
+					displayName,
+					description,
+					type: "community",
+					queue: [],
+					currentSong: stations.defaultSong
+				}, next);
+			}
+
+		], (err, station) => {
+			if (err) throw err;
+			cache.pub('station.create', data._id);
+			return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });
+		});
+	}),
+
+	addToQueue: hooks.loginRequired((session, stationId, songId, cb, userId) => {
+		stations.getStation(stationId, (err, station) => {
+			if (err) return cb(err);
+			if (station.type === 'community') {
+				let has = false;
+				station.queue.forEach((queueSong) => {
+					if (queueSong.songId === songId) {
+						has = true;
+					}
+				});
+				if (has) return cb({'status': 'failure', 'message': 'That song has already been added to the queue.'});
+				db.models.update({_id: stationId}, {$push: {queue: {_id: songId, title: "Title", duration: 100, requestedBy: userId}}}, (err) => {
+					if (err) return cb({'status': 'failure', 'message': 'Something went wrong.'});
+					stations.updateStation(stationId, (err, station) => {
+						if (err) return cb(err);
+						cache.pub('station.queueUpdate', stationId);
+					});
+				});
+			} else cb({'status': 'failure', 'message': 'That station is not a community station.'});
+		});
+	}),
+
+	removeFromQueue: hooks.adminRequired((session, stationId, songId, cb, userId) => {
+		stations.getStation(stationId, (err, station) => {
+			if (err) return cb(err);
+			if (station.type === 'community') {
+				let has = false;
+				station.queue.forEach((queueSong) => {
+					if (queueSong._id === songId) {
+						has = true;
+					}
+				});
+				if (!has) return cb({'status': 'failure', 'message': 'That song is not in the queue.'});
+				db.models.update({_id: stationId}, {$pull: {queue: {_id: songId}}}, (err) => {
+					if (err) return cb({'status': 'failure', 'message': 'Something went wrong.'});
+					stations.updateStation(stationId, (err, station) => {
+						if (err) return cb(err);
+						cache.pub('station.queueUpdate', stationId);
+					});
+				});
+			} else cb({'status': 'failure', 'message': 'That station is not a community station.'});
+		});
+	}),
+
 };

+ 5 - 1
backend/logic/db/schemas/station.js

@@ -21,5 +21,9 @@ module.exports = {
 	playlist: { type: Array, required: true },
 	genres: [{ type: String }],
 	privacy: { type: String, enum: ["public", "unlisted", "private"], default: "private" },
-	locked: { type: Boolean, default: false }
+	locked: { type: Boolean, default: false },
+	queue: [{
+		songId: { type: String, required: true },
+		requestedBy: { type: String, required: true }
+	}]
 };

+ 22 - 1
frontend/App.vue

@@ -5,6 +5,7 @@
 		<what-is-new></what-is-new>
 		<login-modal v-if='isLoginActive'></login-modal>
 		<register-modal v-if='isRegisterActive'></register-modal>
+		<create-community-station v-if='isCCSActive'></create-community-station>
 	</div>
 </template>
 
@@ -14,6 +15,7 @@
 	import WhatIsNew from './components/Modals/WhatIsNew.vue';
 	import LoginModal from './components/Modals/Login.vue';
 	import RegisterModal from './components/Modals/Register.vue';
+	import CreateCommunityStation from './components/Modals/CreateCommunityStation.vue';
 	import auth from './auth';
 
 	export default {
@@ -29,11 +31,17 @@
 					email: '',
 					password: ''
 				},
+				ccs: {
+					name: '',
+					displayName: '',
+					description: ''
+				},
 				loggedIn: false,
 				role: '',
 				username: '',
 				isRegisterActive: false,
 				isLoginActive: false,
+				isCCSActive: false,
 				serverDomain: ''
 			}
 		},
@@ -96,6 +104,16 @@
 					}
 				});
 			},
+			'ccs': function () {
+				let _this = this;
+				this.socket.emit('stations.create', _this.ccs.name, _this.ccs.displayName, _this.ccs.description, result => {
+					if (result.status === 'success') {
+						Toast.methods.addToast(`You have added the station successfully`, 4000);
+					} else {
+						Toast.methods.addToast(result.message, 4000);
+					}
+				});
+			},
 			'toggleModal': function (type) {
 				switch(type) {
 					case 'register':
@@ -104,6 +122,9 @@
 					case 'login':
 						this.isLoginActive = !this.isLoginActive;
 						break;
+					case 'ccs':
+						this.isCCSActive = !this.isCCSActive;
+						break;
 				}
 			}
 			/*'joinStation': function (id) {
@@ -118,6 +139,6 @@
 				});
 			}*/
 		},
-		components: { Toast, WhatIsNew, LoginModal, RegisterModal }
+		components: { Toast, WhatIsNew, LoginModal, RegisterModal, CreateCommunityStation }
 	}
 </script>

+ 43 - 0
frontend/components/Modals/CreateCommunityStation.vue

@@ -0,0 +1,43 @@
+<template>
+	<div class='modal is-active'>
+		<div class='modal-background'></div>
+		<div class='modal-card'>
+			<header class='modal-card-head'>
+				<p class='modal-card-title'>Create community station</p>
+				<button class='delete' @click='toggleModal()'></button>
+			</header>
+			<section class='modal-card-body'>
+				<!-- validation to check if exists http://bulma.io/documentation/elements/form/ -->
+				<label class='label'>Name (lowercase, a-z, used in the url)</label>
+				<p class='control'>
+					<input class='input' type='text' placeholder='Name...' v-model='$parent.ccs.name'>
+				</p>
+				<label class='label'>Display name</label>
+				<p class='control'>
+					<input class='input' type='text' placeholder='Display name...' v-model='$parent.ccs.displayName'>
+				</p>
+				<label class='label'>Description</label>
+				<p class='control'>
+					<input class='input' type='text' placeholder='Description...' v-model='$parent.ccs.description'>
+				</p>
+			</section>
+			<footer class='modal-card-foot'>
+				<a class='button is-primary' @click='submitModal()'>Create</a>
+			</footer>
+		</div>
+	</div>
+</template>
+
+<script>
+	export default {
+		methods: {
+			toggleModal: function () {
+				this.$dispatch('toggleModal', 'ccs');
+			},
+			submitModal: function () {
+				this.$dispatch('ccs');
+				this.toggleModal();
+			}
+		}
+	}
+</script>

+ 6 - 0
frontend/components/pages/Home.vue

@@ -18,6 +18,7 @@
 				</div>
 			</div>
 		</div>
+		<button @click="toggleModal('ccs')">CREATE COMMUNITY STATION</button>
 		<div class="group" v-if="stations.community.length">
 			<div class="group-title">Community Stations</div>
 			<div class="group-stations">
@@ -77,6 +78,11 @@
 				}
 			}, 100);
 		},
+		methods: {
+			toggleModal: function (type) {
+				this.$dispatch('toggleModal', type);
+			}
+		},
 		components: { MainHeader, MainFooter }
 	}
 </script>