Pārlūkot izejas kodu

Added promises library for backend (bluebird). Added logic to generate playlist off of YouTube ID. To test, you will need to add song to db

theflametrooper 8 gadi atpakaļ
vecāks
revīzija
4ea39bb959

+ 5 - 5
backend/app.js

@@ -45,17 +45,17 @@ function setupExpress() {
 	const server = app.listen(80);
 	global.io = require('socket.io')(server);
 
-	// other custom modules
-	const coreHandler = require('./logic/coreHandler'),
-		  socketHandler = require('./logic/socketHandler'),
-		  expressHandler = require('./logic/expressHandler');
-
 	global.db = {
 		user: require('./schemas/user')(mongoose),
 		station: require('./schemas/station')(mongoose),
 		song: require('./schemas/song')(mongoose)
 	};
 
+	// other custom modules
+	const coreHandler = require('./logic/coreHandler'),
+		  socketHandler = require('./logic/socketHandler'),
+		  expressHandler = require('./logic/expressHandler');
+
 	const mongoStore = new MongoStore({'mongooseConnection': MongoDB});
 
 	app.use(session({

+ 1 - 18
backend/logic/coreHandler.js

@@ -22,24 +22,7 @@ var eventEmitter = new events.EventEmitter();
 const edmStation = new stations.Station("edm", {
 	"genres": ["edm"],
 	playlist: [
-		{
-			id: "dQw4w9WgXcQ",
-			title: "Never gonna give you up",
-			artists: ["Rick Astley"],
-			duration: '00:01:18',
-			thumbnail: "https://yt3.ggpht.com/-CGlBu6kDEi8/AAAAAAAAAAI/AAAAAAAAAAA/Pi679mvyyyU/s88-c-k-no-mo-rj-c0xffffff/photo.jpg",
-			likes: 0,
-			dislikes: 1
-		},
-		{
-			id: "GxBSyx85Kp8",
-			title: "Yeah!",
-			artists: ["Usher", "test"],
-			duration: '00:00:08',
-			thumbnail: "https://yt3.ggpht.com/-CGlBu6kDEi8/AAAAAAAAAAI/AAAAAAAAAAA/Pi679mvyyyU/s88-c-k-no-mo-rj-c0xffffff/photo.jpg",
-			likes: 0,
-			dislikes: 1
-		}
+		'gCYcHz2k5x0'
 	],
 	currentSongIndex: 0,
 	paused: false,

+ 25 - 9
backend/logic/stations.js

@@ -1,6 +1,7 @@
 'use strict';
 
 const global = require('./global');
+const Promise = require('bluebird');
 const io = global.io;
 let stations = [];
 
@@ -28,17 +29,32 @@ module.exports = {
 
 			this.id = id;
 			this.users = 0;
-			this.playlist = data.playlist;
-			this.currentSongIndex = data.currentSongIndex;
-			this.paused = data.paused;
-			this.displayName = data.displayName;
-			this.description = data.description;
 
-			this.timePaused = 0;
-			this.timer = undefined;
-			this.currentSong = this.playlist[this.currentSongIndex];
+			function generatePlaylist(arr) {
+				local.playlist = [];
+				return arr.reduce((promise, id) => {
+					return promise.then(() => {
+						return global.db.song.find({ id }, (err, song) => {
+							if (err) throw err;
+							local.playlist.push(song[0]);
+						});
+					});
+				}, Promise.resolve());
+			}
+
+			generatePlaylist(data.playlist).then(() => {
+				local.currentSongIndex = data.currentSongIndex;
+				local.paused = data.paused;
+				local.displayName = data.displayName;
+				local.description = data.description;
 
-			this.nextSong();
+				local.timePaused = 0;
+				local.timer = undefined;
+				local.currentSong = local.playlist[this.currentSongIndex]
+				console.log(local.playlist);
+
+				local.nextSong();
+			});
 		}
 
 		nextSong() {

+ 0 - 52
backend/schema.json

@@ -1,52 +0,0 @@
-[
-  // Station
-  {
-    "id": "edm",
-    "displayName": "EDM",
-    "description": "EDM Music",
-    "paused": false,
-    "playlist": [
-		{
-			"id": "dQw4w9WgXcQ",
-			"title": "Never Gonna Give You Up",
-			"artists": ["Rick Astley"],
-			"duration": 254.21,
-			"thumbnail": "https://upload.wikimedia.org/wikipedia/en/3/34/RickAstleyNeverGonnaGiveYouUp7InchSingleCover.jpg"
-		}
-	],
-	"currentSongIndex": 1,
-	"genres": ["edm"]
-  },
-
-  // User
-  {
-    "id": "kF9u3jf9eu3nf84rh47gf7474gf",
-    "username": "KrisVos130",
-    "email": {
-      "verified": false,
-      "verificationToken": "d83ejd9u93urjf87j8fj49f2d83ejd9u93urjf87j8fj49f2d83ejd9u93urjf87j8fj49f2d83ejd9u93urjf87j8fj49f2d83ejd9u93urjf87j8fj49f2",
-      "address": "krisvos130@gmail.com"
-    },
-    "services": {
-      "password": {
-        // Token
-      },
-      "github": {
-        // Token
-      },
-      "discord": {
-        // Token
-      }
-    },
-    "ban": {
-      "banned": true,
-      "reason": "Spamming",
-      "bannedAt": 1472926465,
-      "bannedUntil": 1472926466
-    },
-    "statistics": {
-      "songsRequested": 34
-    },
-    "createdAt": 1472926465
-  }
-]

+ 1 - 1
backend/schemas/station.js

@@ -13,7 +13,7 @@ module.exports = mongoose => mongoose.model('station', new mongoose.Schema({
 	},
 	currentSongIndex: { type: Number, default: 0, required: true },
 	timePaused: { type: Number, default: 0, required: true },
-	playlist: { type: Object, required: true },
+	playlist: { type: Array, required: true },
 	genres: [{ type: String }]
 }));
 

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

@@ -146,7 +146,7 @@
 					local.videoLoading = true;
 					local.player.loadVideoById(local.currentSong.id);
 
-					local.currentSong.artists = local.currentSong.artists.join(", ");
+					if (local.currentSong.artists) local.currentSong.artists = local.currentSong.artists.join(", ");
 
 					if (local.interval !== 0) {
 						clearInterval(local.interval);