Procházet zdrojové kódy

Fixed issue where editing a song would directly change the songlist, even when cancelling.

KrisVos130 před 8 roky
rodič
revize
ef8abb2cc5

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

@@ -133,15 +133,13 @@ module.exports = {
 
 	getPlaylist: (session, stationId, cb) => {
 		stations.getStation(stationId, (err, station) => {
-			let playlist = [];
-
-			for (let s = 0; s < station.playlist.length; s++) {
-				songs.getSong(station.playlist[s], (err, song) => {
-					playlist.push(song);
-				});
-			}
-
-			cb({ status: 'success', data: playlist });
+			if (err) return cb({ status: 'failure', message: 'Something went wrong when getting the station.' });
+			if (station.type === 'official') {
+				cache.hget("officialPlaylists", stationId, (err, playlist) => {
+					if (err) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.' });
+					cb({ status: 'success', data: playlist.songs })
+				})
+			} else cb({ status: 'failure', message: 'This is not an official station.' })
 		});
 	},
 

+ 1 - 0
backend/logic/cache/index.js

@@ -16,6 +16,7 @@ const lib = {
 		session: require('./schemas/session'),
 		station: require('./schemas/station'),
 		playlist: require('./schemas/playlist'),
+		officialPlaylist: require('./schemas/officialPlaylist'),
 		song: require('./schemas/song')
 	},
 

+ 8 - 0
backend/logic/cache/schemas/officialPlaylist.js

@@ -0,0 +1,8 @@
+'use strict';
+
+module.exports = (stationId, songs) => {
+	return {
+		stationId,
+		songs
+	}
+};

+ 42 - 1
backend/logic/stations.js

@@ -116,6 +116,13 @@ module.exports = {
 				station.playlist.filter((songId) => {
 					if (songList.indexOf(songId) !== -1) playlist.push(songId);
 				});
+
+				_this.calculateOfficialPlaylistList(station._id, playlist, () => {
+					next(null, playlist);
+				});
+			},
+
+			(playlist, lessInfoPlaylist, next) => {
 				db.models.station.update({_id: station._id}, {$set: {playlist: playlist}}, (err) => {
 					_this.updateStation(station._id, () => {
 						next(err, playlist);
@@ -130,6 +137,7 @@ module.exports = {
 
 	// Attempts to get the station from Redis. If it's not in Redis, get it from Mongo and add it to Redis.
 	getStation: function(stationId, cb) {
+		let _this = this;
 		async.waterfall([
 
 			(next) => {
@@ -143,6 +151,9 @@ module.exports = {
 
 			(station, next) => {
 				if (station) {
+					if (station.type === 'official') {
+						_this.calculateOfficialPlaylistList(station._id, station.playlist, ()=>{});
+					}
 					station = cache.schemas.station(station);
 					cache.hset('stations', stationId, station);
 					next(true, station);
@@ -155,7 +166,8 @@ module.exports = {
 		});
 	},
 
-	updateStation: (stationId, cb) => {
+	updateStation: function(stationId, cb) {
+		let _this = this;
 		async.waterfall([
 
 			(next) => {
@@ -164,6 +176,9 @@ module.exports = {
 
 			(station, next) => {
 				if (!station) return next('Station not found');
+				if (station.type === 'official') {
+					_this.calculateOfficialPlaylistList(station._id, station.playlist, ()=>{});
+				}
 				cache.hset('stations', stationId, station, (err) => {
 					if (err) return next(err);
 					next(null, station);
@@ -176,6 +191,32 @@ module.exports = {
 		});
 	},
 
+	calculateOfficialPlaylistList: (stationId, songList, cb) => {
+		let lessInfoPlaylist = [];
+
+		function getSongInfo(index) {
+			if (songList.length > index) {
+				songs.getSong(songList[index], (err, song) => {
+					if (!err && song) {
+						let newSong = {
+							_id: song._id,
+							title: song.title,
+							artists: song.artists,
+							duration: song.duration
+						};
+						lessInfoPlaylist.push(newSong);
+					}
+					getSongInfo(index + 1);
+				})
+			} else {
+				cache.hset("officialPlaylists", stationId, cache.schemas.officialPlaylist(stationId, lessInfoPlaylist), () => {
+					cb();
+				});
+			}
+		}
+		getSongInfo(0);
+	},
+
 	skipStation: function(stationId) {
 		let _this = this;
 		return (cb) => {