Browse Source

refactor: Moved homeView from CAN_USER_VIEW_STATION to stations.index as adminFilter and added query toggle

Owen Diffey 2 năm trước cách đây
mục cha
commit
8bd25b1173
3 tập tin đã thay đổi với 53 bổ sung27 xóa
  1. 26 4
      backend/logic/actions/stations.js
  2. 3 5
      backend/logic/stations.js
  3. 24 18
      frontend/src/pages/Home.vue

+ 26 - 4
backend/logic/actions/stations.js

@@ -380,9 +380,10 @@ export default {
 	 * Get a list of all the stations
 	 *
 	 * @param {object} session - user session
+	 * @param {boolean} adminFilter - whether to filter out stations admins do not own
 	 * @param {Function} cb - callback
 	 */
-	async index(session, cb) {
+	async index(session, adminFilter, cb) {
 		const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" });
 
 		async.waterfall(
@@ -425,12 +426,33 @@ export default {
 											"CAN_USER_VIEW_STATION",
 											{
 												station,
-												userId: session.userId,
-												homeView: true
+												userId: session.userId
 											},
 											this
 										)
-											.then(exists => callback(null, exists))
+											.then(exists => {
+												if (exists && session.userId && station.privacy !== "public") {
+													DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
+														.then(userModel => {
+															userModel.findOne({ _id: session.userId }, (err, user) => {
+																if (err) return callback(err);
+																if (
+																	(user.role !== "admin" &&
+																		station.owner !== session.userId) ||
+																	(adminFilter &&
+																		user.role === "admin" &&
+																		station.owner !== session.userId)
+																) {
+																	return callback(null, false);
+																}
+																return callback(null, exists);
+															});
+														})
+														.catch(callback);
+												} else if (exists && !session.userId && station.privacy !== "public")
+													return callback(null, false);
+												else return callback(null, exists);
+											})
 											.catch(callback);
 									}
 								],

+ 3 - 5
backend/logic/stations.js

@@ -964,7 +964,6 @@ class _StationsModule extends CoreClass {
 	 * @param {object} payload - object that contains the payload
 	 * @param {object} payload.station - the station object of the station in question
 	 * @param {string} payload.userId - the id of the user in question
-	 * @param {boolean} payload.homeView - whether to modify output for homepage usage
 	 * @returns {Promise} - returns a promise (resolve, reject)
 	 */
 	CAN_USER_VIEW_STATION(payload) {
@@ -972,8 +971,8 @@ class _StationsModule extends CoreClass {
 			async.waterfall(
 				[
 					next => {
-						if (payload.station.privacy === "public") return next(true);
-						if (payload.station.privacy === "unlisted" && !payload.homeView) return next(true);
+						if (payload.station.privacy === "public" || payload.station.privacy === "unlisted")
+							return next(true);
 						if (!payload.userId) return next("Not allowed");
 
 						return next();
@@ -987,9 +986,8 @@ class _StationsModule extends CoreClass {
 
 					(user, next) => {
 						if (!user) return next("Not allowed");
-						if (!payload.homeView && user.role === "admin") return next(true);
+						if (user.role === "admin" || payload.station.owner === payload.userId) return next(true);
 						if (payload.station.type === "official") return next("Not allowed");
-						if (payload.station.owner === payload.userId) return next(true);
 
 						return next("Not allowed");
 					}

+ 24 - 18
frontend/src/pages/Home.vue

@@ -597,6 +597,8 @@ export default {
 	async mounted() {
 		this.siteSettings = await lofig.get("siteSettings");
 
+		// if (this.$route.query.query) this.searchQuery = this.$route.query.query;
+
 		if (
 			!this.loggedIn &&
 			this.$route.redirectedFrom &&
@@ -745,30 +747,34 @@ export default {
 	},
 	methods: {
 		init() {
-			this.socket.dispatch("stations.index", res => {
-				this.stations = [];
+			this.socket.dispatch(
+				"stations.index",
+				this.$route.query.adminFilter !== "false",
+				res => {
+					this.stations = [];
 
-				if (res.status === "success") {
-					res.data.stations.forEach(station => {
-						const modifiableStation = station;
+					if (res.status === "success") {
+						res.data.stations.forEach(station => {
+							const modifiableStation = station;
 
-						if (!modifiableStation.currentSong)
-							modifiableStation.currentSong = {
-								thumbnail: "/assets/notes-transparent.png"
-							};
+							if (!modifiableStation.currentSong)
+								modifiableStation.currentSong = {
+									thumbnail: "/assets/notes-transparent.png"
+								};
 
-						if (
-							modifiableStation.currentSong &&
-							!modifiableStation.currentSong.thumbnail
-						)
-							modifiableStation.currentSong.ytThumbnail = `https://img.youtube.com/vi/${station.currentSong.youtubeId}/mqdefault.jpg`;
+							if (
+								modifiableStation.currentSong &&
+								!modifiableStation.currentSong.thumbnail
+							)
+								modifiableStation.currentSong.ytThumbnail = `https://img.youtube.com/vi/${station.currentSong.youtubeId}/mqdefault.jpg`;
 
-						this.stations.push(modifiableStation);
-					});
+							this.stations.push(modifiableStation);
+						});
 
-					this.orderOfFavoriteStations = res.data.favorited;
+						this.orderOfFavoriteStations = res.data.favorited;
+					}
 				}
-			});
+			);
 
 			this.socket.dispatch("apis.joinRoom", "home");
 		},