Browse Source

Got mostly everything done for limits on backend. The playlist songs array doesn't get verified for some reason.

KrisVos130 8 years ago
parent
commit
f4f5e1da99

+ 1 - 1
backend/logic/actions/playlists.js

@@ -270,7 +270,7 @@ let lib = {
 				});
 				});
 			},
 			},
 			(newSong, next) => {
 			(newSong, next) => {
-				db.models.playlist.update({ _id: playlistId }, { $push: { songs: newSong } }, (err) => {
+				db.models.playlist.update({_id: playlistId}, {$push: {songs: newSong}}, {runValidators: true}, (err) => {
 					if (err) return next(err);
 					if (err) return next(err);
 					playlists.updatePlaylist(playlistId, (err, playlist) => {
 					playlists.updatePlaylist(playlistId, (err, playlist) => {
 						next(err, playlist, newSong);
 						next(err, playlist, newSong);

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

@@ -767,7 +767,6 @@ module.exports = {
 	 * @param userId
 	 * @param userId
 	 */
 	 */
 	create: hooks.loginRequired((session, data, cb, userId) => {
 	create: hooks.loginRequired((session, data, cb, userId) => {
-		console.log(data);
 		data.name = data.name.toLowerCase();
 		data.name = data.name.toLowerCase();
 		let blacklist = ["country", "edm", "musare", "hip-hop", "rap", "top-hits", "todays-hits", "old-school", "christmas", "about", "support", "staff", "help", "news", "terms", "privacy", "profile", "c", "community", "tos", "login", "register", "p", "official", "o", "trap", "faq", "team", "donate", "buy", "shop", "forums", "explore", "settings", "admin", "auth", "reset_password"];
 		let blacklist = ["country", "edm", "musare", "hip-hop", "rap", "top-hits", "todays-hits", "old-school", "christmas", "about", "support", "staff", "help", "news", "terms", "privacy", "profile", "c", "community", "tos", "login", "register", "p", "official", "o", "trap", "faq", "team", "donate", "buy", "shop", "forums", "explore", "settings", "admin", "auth", "reset_password"];
 		async.waterfall([
 		async.waterfall([
@@ -857,7 +856,6 @@ module.exports = {
 			(station, next) => {
 			(station, next) => {
 				songs.getSong(songId, (err, song) => {
 				songs.getSong(songId, (err, song) => {
 					if (!err && song) return next(null, song);
 					if (!err && song) return next(null, song);
-					console.log(53, songId);
 					utils.getSongFromYouTube(songId, (song) => {
 					utils.getSongFromYouTube(songId, (song) => {
 						song.artists = [];
 						song.artists = [];
 						song.skipDuration = 0;
 						song.skipDuration = 0;
@@ -872,7 +870,7 @@ module.exports = {
 
 
 			(song, next) => {
 			(song, next) => {
 				song.requestedBy = userId;
 				song.requestedBy = userId;
-				db.models.station.update({_id: stationId}, {$push: {queue: song}}, next);
+				db.models.station.update({_id: stationId}, {$push: {queue: song}}, {runValidators: true}, next);
 			},
 			},
 
 
 			(res, next) => {
 			(res, next) => {

+ 67 - 11
backend/logic/db/index.js

@@ -45,6 +45,16 @@ let lib = {
 				report: new mongoose.Schema(require(`./schemas/report`))
 				report: new mongoose.Schema(require(`./schemas/report`))
 			};
 			};
 
 
+			lib.models = {
+				song: mongoose.model('song', lib.schemas.song),
+				queueSong: mongoose.model('queueSong', lib.schemas.queueSong),
+				station: mongoose.model('station', lib.schemas.station),
+				user: mongoose.model('user', lib.schemas.user),
+				playlist: mongoose.model('playlist', lib.schemas.playlist),
+				news: mongoose.model('news', lib.schemas.news),
+				report: mongoose.model('report', lib.schemas.report)
+			};
+
 			lib.schemas.user.path('username').validate((username) => {
 			lib.schemas.user.path('username').validate((username) => {
 				return (isLength(username, 2, 32) && regex.azAZ09_.test(username));
 				return (isLength(username, 2, 32) && regex.azAZ09_.test(username));
 			}, 'Invalid username.');
 			}, 'Invalid username.');
@@ -67,11 +77,52 @@ let lib = {
 				if (!isLength(description, 2, 200)) return false;
 				if (!isLength(description, 2, 200)) return false;
 				let characters = description.split("");
 				let characters = description.split("");
 				return characters.filter((character) => {
 				return characters.filter((character) => {
-					console.log(character.charCodeAt(0), character.charCodeAt(0) === 21328);
 					return character.charCodeAt(0) === 21328;
 					return character.charCodeAt(0) === 21328;
 				}).length === 0;
 				}).length === 0;
 			}, 'Invalid display name.');
 			}, 'Invalid display name.');
 
 
+
+			lib.schemas.station.path('owner').validate((owner, callback) => {
+				lib.models.station.count({owner: owner}, (err, c) => {
+					callback(!(err || c >= 3));
+				});
+			}, 'User already has 3 stations.');
+
+			lib.schemas.station.path('queue').validate((queue, callback) => {
+				let totalDuration = 0;
+				queue.forEach((song) => {
+					totalDuration += song.duration;
+				});
+				return callback(totalDuration <= 3600);
+			}, 'The max length of the queue is 3 hours.');
+
+			lib.schemas.station.path('queue').validate((queue, callback) => {
+				if (queue.length === 0) return callback(true);
+				let totalDuration = 0;
+				const userId = queue[queue.length - 1].requestedBy;
+				queue.forEach((song) => {
+					if (userId === song.requestedBy) {
+						totalDuration += song.duration;
+					}
+				});
+				return callback(totalDuration <= 900);
+			}, 'The max length of songs per user is 15 minutes.');
+
+			lib.schemas.station.path('queue').validate((queue, callback) => {
+				if (queue.length === 0) return callback(true);
+				let totalSongs = 0;
+				const userId = queue[queue.length - 1].requestedBy;
+				queue.forEach((song) => {
+					if (userId === song.requestedBy) {
+						totalSongs++;
+					}
+				});
+				if (totalSongs <= 2) return callback(true);
+				if (totalSongs > 3) return callback(false);
+				if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId) return callback(true);
+				return callback(false);
+			}, 'The max amount of songs per user is 3, and only 2 in a row is allowed.');
+
 			let songTitle = (title) => {
 			let songTitle = (title) => {
 				return (isLength(title, 1, 64) && regex.ascii.test(title));
 				return (isLength(title, 1, 64) && regex.ascii.test(title));
 			};
 			};
@@ -105,20 +156,25 @@ let lib = {
 				return (isLength(displayName, 1, 16) && regex.ascii.test(displayName));
 				return (isLength(displayName, 1, 16) && regex.ascii.test(displayName));
 			}, 'Invalid display name.');
 			}, 'Invalid display name.');
 
 
+			lib.schemas.playlist.path('createdBy').validate((createdBy, callback) => {
+				lib.models.playlist.count({createdBy: createdBy}, (err, c) => {
+					callback(!(err || c >= 10));
+				});
+			}, 'Max 10 playlists per user.');
+
+			lib.schemas.playlist.path('songs').validate((songs) => {
+				return songs.length <= 2000;
+			}, 'Max 2000 songs per playlist.');
+
+			lib.schemas.playlist.path('songs').validate((songs) => {
+				if (songs.length === 0) return true;
+				return songs[0].duration <= 10800;
+			}, 'Max 3 hours per song.');
+
 			lib.schemas.report.path('description').validate((description) => {
 			lib.schemas.report.path('description').validate((description) => {
 				return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
 				return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
 			}, 'Invalid description.');
 			}, 'Invalid description.');
 
 
-			lib.models = {
-				song: mongoose.model('song', lib.schemas.song),
-				queueSong: mongoose.model('queueSong', lib.schemas.queueSong),
-				station: mongoose.model('station', lib.schemas.station),
-				user: mongoose.model('user', lib.schemas.user),
-				playlist: mongoose.model('playlist', lib.schemas.playlist),
-				news: mongoose.model('news', lib.schemas.news),
-				report: mongoose.model('report', lib.schemas.report)
-			};
-
 			cb();
 			cb();
 		});
 		});
 	},
 	},

+ 2 - 2
backend/logic/playlists.js

@@ -119,7 +119,7 @@ module.exports = {
 			}
 			}
 
 
 		], (err, playlist) => {
 		], (err, playlist) => {
-			if (err && err !== true) cb(err);
+			if (err && err !== true) return cb(err);
 			cb(null, playlist);
 			cb(null, playlist);
 		});
 		});
 	},
 	},
@@ -142,7 +142,7 @@ module.exports = {
 			}
 			}
 
 
 		], (err) => {
 		], (err) => {
-			if (err && err !== true) cb(err);
+			if (err && err !== true) return cb(err);
 
 
 			cb(null);
 			cb(null);
 		});
 		});

+ 1 - 1
backend/logic/songs.js

@@ -124,7 +124,7 @@ module.exports = {
 			}
 			}
 
 
 		], (err, song) => {
 		], (err, song) => {
-			if (err && err !== true) cb(err);
+			if (err && err !== true) return cb(err);
 
 
 			cb(null, song);
 			cb(null, song);
 		});
 		});

+ 1 - 1
backend/logic/stations.js

@@ -205,7 +205,7 @@ module.exports = {
 			},
 			},
 
 
 		], (err, station) => {
 		], (err, station) => {
-			if (err && err !== true) cb(err);
+			if (err && err !== true) return cb(err);
 			cb(null, station);
 			cb(null, station);
 		});
 		});
 	},
 	},

+ 1 - 1
frontend/components/Modals/AddSongToQueue.vue

@@ -79,7 +79,7 @@
 			addSongToQueue: function (songId) {
 			addSongToQueue: function (songId) {
 				let _this = this;
 				let _this = this;
 				if (_this.$parent.type === 'community') {
 				if (_this.$parent.type === 'community') {
-					_this.socket.emit('stations.addToQueue', _this.$parent.stationId, songId, data => {
+					_this.socket.emit('stations.addToQueue', _this.$parent.station._id, songId, data => {
 						if (data.status !== 'success') Toast.methods.addToast(`Error: ${data.message}`, 8000);
 						if (data.status !== 'success') Toast.methods.addToast(`Error: ${data.message}`, 8000);
 						else Toast.methods.addToast(`${data.message}`, 4000);
 						else Toast.methods.addToast(`${data.message}`, 4000);
 					});
 					});