فهرست منبع

Fixed issue with rating in rooms, cleaned up code a bit, added better song functions.

KrisVos130 8 سال پیش
والد
کامیت
94fcb53826
5فایلهای تغییر یافته به همراه89 افزوده شده و 21 حذف شده
  1. 5 4
      backend/logic/actions/songs.js
  2. 11 1
      backend/logic/actions/stations.js
  3. 0 2
      backend/logic/db/schemas/station.js
  4. 48 2
      backend/logic/songs.js
  5. 25 12
      backend/logic/stations.js

+ 5 - 4
backend/logic/actions/songs.js

@@ -2,6 +2,7 @@
 
 const db = require('../db');
 const io = require('../io');
+const songs = require('../songs');
 const cache = require('../cache');
 const utils = require('../utils');
 
@@ -89,8 +90,8 @@ module.exports = {
 							db.models.user.update({_id: userSession.userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, (err) => {
 								if (!err) {
 									console.log(JSON.stringify({songId, userId: userSession.userId}));
+									songs.updateSong(songId, (err, song) => {});
 									cache.pub('song.like', JSON.stringify({songId, userId: userSession.userId, undisliked: (dislikes === -1)}));
-									//TODO Update song object in Redis
 								} else db.models.song.update({_id: songId}, {$inc: {likes: -1, dislikes: -dislikes}}, (err) => {
 									return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
 								});
@@ -115,8 +116,8 @@ module.exports = {
 						if (!err) {
 							db.models.user.update({_id: userSession.userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, (err) => {
 								if (!err) {
+									songs.updateSong(songId, (err, song) => {});
 									cache.pub('song.dislike', JSON.stringify({songId, userId: userSession.userId, unliked: (likes === -1)}));
-									//TODO Update song object in Redis
 								} else db.models.song.update({_id: songId}, {$inc: {likes: -likes, dislikes: -1}}, (err) => {
 									return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
 								});
@@ -139,8 +140,8 @@ module.exports = {
 						if (!err) {
 							db.models.user.update({_id: userSession.userId}, {$pull: {disliked: songId}}, (err) => {
 								if (!err) {
+									songs.updateSong(songId, (err, song) => {});
 									cache.pub('song.undislike', JSON.stringify({songId, userId: userSession.userId}));
-									//TODO Update song object in Redis
 								} else db.models.song.update({_id: songId}, {$inc: {dislikes: 1}}, (err) => {
 									return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
 								});
@@ -163,8 +164,8 @@ module.exports = {
 						if (!err) {
 							db.models.user.update({_id: userSession.userId}, {$pull: {liked: songId}}, (err) => {
 								if (!err) {
+									songs.updateSong(songId, (err, song) => {});
 									cache.pub('song.unlike', JSON.stringify({songId, userId: userSession.userId}));
-									//TODO Update song object in Redis
 								} else db.models.song.update({_id: songId}, {$inc: {likes: 1}}, (err) => {
 									return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
 								});

+ 11 - 1
backend/logic/actions/stations.js

@@ -10,6 +10,7 @@ const cache = require('../cache');
 const notifications = require('../notifications');
 const utils = require('../utils');
 const stations = require('../stations');
+const songs = require('../songs');
 
 cache.sub('station.locked', stationId => {
 	io.io.to(`station.${stationId}`).emit("event:stations.locked");
@@ -93,7 +94,16 @@ module.exports = {
 					utils.socketJoinRoom(sessionId, `station.${stationId}`);
 					utils.socketJoinSongRoom(sessionId, `song.${station.currentSong._id}`);
 					//TODO Emit to cache, listen on cache
-					cb({ status: 'success', currentSong: station.currentSong, startedAt: station.startedAt, paused: station.paused, timePaused: station.timePaused });
+					songs.getSong(station.currentSong._id, (err, song) => {
+						if (!err) {
+							station.currentSong.likes = song.likes;
+							station.currentSong.dislikes = song.dislikes;
+						} else {
+							station.currentSong.likes = -1;
+							station.currentSong.dislikes = -1;
+						}
+						cb({ status: 'success', currentSong: station.currentSong, startedAt: station.startedAt, paused: station.paused, timePaused: station.timePaused });
+					});
 				});
 			}
 			else {

+ 0 - 2
backend/logic/db/schemas/station.js

@@ -10,8 +10,6 @@ module.exports = {
 		artists: [{ type: String }],
 		duration: { type: Number, required: true },
 		skipDuration: { type: Number, required: true },
-		likes: { type: Number, required: true },
-		dislikes: { type: Number, required: true },
 		thumbnail: { type: String, required: true }
 	},
 	currentSongIndex: { type: Number, default: 0, required: true },

+ 48 - 2
backend/logic/songs.js

@@ -18,8 +18,54 @@ module.exports = {
 				cb();
 			}
 		});
-	}
+	},
+
+	// Attempts to get the song from Reids. If it's not in Redis, get it from Mongo and add it to Redis.
+	getSong: function(songId, cb) {
+		async.waterfall([
+
+			(next) => {
+				cache.hget('songs', songId, next);
+			},
+
+			(song, next) => {
+				if (song) return next(true, song);
+
+				db.models.song.findOne({_id: songId}, next);
+			},
+
+			(song, next) => {
+				if (song) {
+					cache.hset('songs', songId, song);
+					next(true, song);
+				} else next('Song not found.');
+			},
+
+		], (err, song) => {
+			if (err && err !== true) cb(err);
+
+			cb(null, song);
+		});
+	},
 
-	//TODO Add way to get song, which adds song to Redis if not in Redis and returns the song
+	updateSong: (songId, cb) => {
+		async.waterfall([
+
+			(next) => {
+				db.models.song.findOne({_id: songId}, next);
+			},
+
+			(song, next) => {
+				if (!song) return next('Song not found.');
+
+				cache.hset('songs', songId, song, next);
+			}
+
+		], (err, song) => {
+			if (err && err !== true) cb(err);
+
+			cb(null, song);
+		});
+	}
 
 };

+ 25 - 12
backend/logic/stations.js

@@ -4,6 +4,7 @@ const cache = require('./cache');
 const db = require('./db');
 const io = require('./io');
 const utils = require('./utils');
+const songs = require('./songs');
 const notifications = require('./notifications');
 const async = require('async');
 let skipTimeout = null;
@@ -112,8 +113,8 @@ module.exports = {
 									function func() {
 										if (station.currentSongIndex < station.playlist.length - 1) {
 											station.currentSongIndex++;
-											db.models.song.findOne({_id: station.playlist[station.currentSongIndex]}, (err, song) => {
-												if (!err && song) {
+											songs.getSong(station.playlist[station.currentSongIndex], (err, song) => {
+												if (!err) {
 													station.currentSong = {
 														_id: song._id,
 														title: song.title,
@@ -137,7 +138,7 @@ module.exports = {
 											_this.calculateSongForStation(station, (err, newPlaylist) => {
 												console.log('New playlist: ', newPlaylist);
 												if (!err) {
-													db.models.song.findOne({_id: newPlaylist[0]}, (err, song) => {
+													songs.getSong(newPlaylist[0], (err, song) => {
 														if (song) {
 															station.currentSong = {
 																_id: song._id,
@@ -176,12 +177,27 @@ module.exports = {
 											station.timePaused = 0;
 											next(null, station);
 										} else {
-											station.currentSongIndex = 0;
-											station.currentSong = playlist[0];
-											station.startedAt = Date.now();
-											station.timePaused = 0;
-											station.playlist = playlist;
-											next(null, station);
+											songs.getSong(playlist[0], (err, song) => {
+												if (!err) {
+													station.currentSong = {
+														_id: song._id,
+														title: song.title,
+														artists: song.artists,
+														duration: song.duration,
+														likes: song.likes,
+														dislikes: song.dislikes,
+														skipDuration: song.skipDuration,
+														thumbnail: song.thumbnail
+													};
+												} else {
+													station.currentSong = _this.defaultSong;
+												}
+												station.currentSongIndex = 0;
+												station.startedAt = Date.now();
+												station.timePaused = 0;
+												station.playlist = playlist;
+												next(null, station);
+											});
 										}
 									});
 								}
@@ -200,7 +216,6 @@ module.exports = {
 								paused: station.paused,
 								timePaused: 0
 							});
-							console.log(io.io.to(`station.${stationId}`).sockets);
 							utils.socketsJoinSongRoom(io.io.to(`station.${stationId}`).sockets, `song.${station.currentSong._id}`);
 							// schedule a notification to be dispatched when the next song ends
 							notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
@@ -244,8 +259,6 @@ module.exports = {
 		artists: ['Alan Walker'],
 		duration: 212,
 		skipDuration: 0,
-		likes: 0,
-		dislikes: 0,
 		thumbnail: 'https://i.scdn.co/image/2ddde58427f632037093857ebb71a67ddbdec34b'
 	}