Quellcode durchsuchen

fix(Stations): logged-out user can now interact/view with public stations

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan vor 4 Jahren
Ursprung
Commit
ce5e83c02a
2 geänderte Dateien mit 58 neuen und 42 gelöschten Zeilen
  1. 46 34
      backend/logic/actions/stations.js
  2. 12 8
      frontend/src/pages/Home/index.vue

+ 46 - 34
backend/logic/actions/stations.js

@@ -362,26 +362,31 @@ export default {
 					const filteredStations = [];
 					async.each(
 						items,
-						(station, next) => {
+						(station, nextStation) => {
 							async.waterfall(
 								[
-									next => {
-										StationsModule.runJob(
-											"HAS_USER_FAVORITED_STATION",
-											{
-												userId: session.userId,
-												stationId: station._id
-											},
-											this
-										)
-											.then(isStationFavorited => {
-												station.isFavorited = isStationFavorited;
-												return next();
-											})
-											.catch(err => next(err));
+									callback => {
+										// only relevant if user logged in
+										if (session.userId) {
+											return StationsModule.runJob(
+												"HAS_USER_FAVORITED_STATION",
+												{
+													userId: session.userId,
+													stationId: station._id
+												},
+												this
+											)
+												.then(isStationFavorited => {
+													station.isFavorited = isStationFavorited;
+													return callback();
+												})
+												.catch(err => callback(err));
+										}
+
+										return callback();
 									},
 
-									next => {
+									callback => {
 										StationsModule.runJob(
 											"CAN_USER_VIEW_STATION",
 											{
@@ -391,17 +396,18 @@ export default {
 											},
 											this
 										)
-											.then(exists => next(null, exists))
-											.catch(next);
+											.then(exists => callback(null, exists))
+											.catch(callback);
 									}
 								],
 								(err, exists) => {
-									if (err) this.log(err);
+									if (err) return this.log("ERROR", "STATIONS_INDEX", err);
 
 									station.userCount = StationsModule.usersPerStationCount[station._id] || 0;
 
 									if (exists) filteredStations.push(station);
-									next();
+
+									return nextStation();
 								}
 							);
 						},
@@ -704,20 +710,26 @@ export default {
 						.finally(() => next(null, data));
 				},
 
-				(data, next) =>
-					StationsModule.runJob(
-						"HAS_USER_FAVORITED_STATION",
-						{
-							userId: session.userId,
-							stationId: data._id
-						},
-						this
-					)
-						.then(isStationFavorited => {
-							data.isFavorited = isStationFavorited;
-							return next(null, data);
-						})
-						.catch(err => next(err))
+				(data, next) => {
+					// only relevant if user logged in
+					if (session.userId) {
+						return StationsModule.runJob(
+							"HAS_USER_FAVORITED_STATION",
+							{
+								userId: session.userId,
+								stationId: data._id
+							},
+							this
+						)
+							.then(isStationFavorited => {
+								data.isFavorited = isStationFavorited;
+								return next(null, data);
+							})
+							.catch(err => next(err));
+					}
+
+					return next(null, data);
+				}
 			],
 			async (err, data) => {
 				if (err) {

+ 12 - 8
frontend/src/pages/Home/index.vue

@@ -366,19 +366,23 @@ export default {
 		init() {
 			this.socket.emit("stations.index", data => {
 				this.stations = [];
+
 				if (data.status === "success")
-					data.stations.forEach(s => {
-						const station = s;
-						if (!station.currentSong)
-							station.currentSong = {
+					data.stations.forEach(station => {
+						const modifiableStation = station;
+
+						if (!modifiableStation.currentSong)
+							modifiableStation.currentSong = {
 								thumbnail: "/assets/notes-transparent.png"
 							};
+
 						if (
-							station.currentSong &&
-							!station.currentSong.thumbnail
+							modifiableStation.currentSong &&
+							!modifiableStation.currentSong.thumbnail
 						)
-							station.currentSong.ytThumbnail = `https://img.youtube.com/vi/${station.currentSong.songId}/mqdefault.jpg`;
-						this.stations.push(station);
+							modifiableStation.currentSong.ytThumbnail = `https://img.youtube.com/vi/${station.currentSong.songId}/mqdefault.jpg`;
+
+						this.stations.push(modifiableStation);
 					});
 			});