Browse Source

Fixed issue with station not updating after opening manage station

Kristian Vos 3 years ago
parent
commit
7aea73fd40

+ 41 - 4
backend/logic/actions/apis.js

@@ -137,6 +137,28 @@ export default {
 		cb({ status: "success", message: "Successfully joined room." });
 	},
 
+	/**
+	 * Leaves a room
+	 *
+	 * @param {object} session - user session
+	 * @param {string} room - the room to leave
+	 * @param {Function} cb - callback
+	 */
+	 leaveRoom(session, room, cb) {
+		if (room === "home" || room.startsWith("profile.") || room.startsWith("manage-station.")) {
+			WSModule.runJob("SOCKET_LEAVE_ROOM", {
+				socketId: session.socketId,
+				room
+			})
+				.then(() => {})
+				.catch(err => {
+					this.log("ERROR", `Leaving room failed: ${err.message}`);
+				});
+		}
+
+		cb({ status: "success", message: "Successfully left room." });
+	},
+
 	/**
 	 * Joins an admin room
 	 *
@@ -155,15 +177,30 @@ export default {
 			page === "users" ||
 			page === "statistics" ||
 			page === "punishments"
-		)
-			WSModule.runJob("SOCKET_JOIN_ROOM", {
-				socketId: session.socketId,
-				room: `admin.${page}`
+		) {
+			WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session.socketId }).then(() => {
+				WSModule.runJob("SOCKET_JOIN_ROOM", {
+					socketId: session.socketId,
+					room: `admin.${page}`
+				});
 			});
+		}
 
 		cb({ status: "success", message: "Successfully joined admin room." });
 	}),
 
+	/**
+	 * Leaves all rooms
+	 *
+	 * @param {object} session - user session
+	 * @param {Function} cb - callback
+	 */
+	 leaveRooms(session, cb) {
+		WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session.socketId });
+
+		cb({ status: "success", message: "Successfully left all rooms." });
+	},
+
 	/**
 	 * Returns current date
 	 *

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

@@ -1473,7 +1473,7 @@ export default {
 
 				this.log("SUCCESS", "STATIONS_LEAVE", `Left station "${stationId}" successfully.`);
 
-				WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session });
+				WSModule.runJob("SOCKET_LEAVE_ROOM", { socketId: session.socketId, room: `station.${stationId}` });
 
 				delete StationsModule.userList[session.socketId];
 

+ 19 - 1
backend/logic/actions/utils.js

@@ -5,6 +5,7 @@ import { isAdminRequired } from "./hooks";
 import moduleManager from "../../index";
 
 const UtilsModule = moduleManager.modules.utils;
+const WSModule = moduleManager.modules.ws;
 
 export default {
 	getModules: isAdminRequired(function getModules(session, cb) {
@@ -78,5 +79,22 @@ export default {
 				}
 			}
 		);
-	})
+	}),
+
+	getRooms(session, cb) {
+		WSModule.runJob("GET_ROOMS_FOR_SOCKET", { socketId: session.socketId }).then(response => {
+			this.log("SUCCESS", "GET_ROOMS", `User ${session.userId} has successfully got the module info.`);
+			cb({
+				status: "success",
+				message: "Successfully got rooms.",
+				data: {
+					rooms: response
+				}
+			});
+		}).catch(async err => {
+			err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+			this.log("ERROR", "GET_ROOMS", `Failed to get rooms. '${err}'`);
+			cb({ status: "error", message: err });
+		});
+	}
 };

+ 17 - 4
backend/logic/ws.js

@@ -263,6 +263,23 @@ class _WSModule extends CoreClass {
 		});
 	}
 
+	/**
+	 * Allows a socket to leave a specific room they are connected to
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {string} payload.socketId - the id of the socket which should leave a room
+	 * @param {string} payload.room - the room
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	 async SOCKET_LEAVE_ROOM(payload) {
+		return new Promise(resolve => {
+			// filter out rooms that the user is in
+			if (WSModule.rooms[payload.room]) WSModule.rooms[payload.room] = WSModule.rooms[payload.room].filter(participant => participant !== payload.socketId);
+
+			return resolve();
+		});
+	}
+
 	/**
 	 * Allows a socket to join a specified room (this will remove them from any rooms they are currently in)
 	 *
@@ -273,10 +290,6 @@ class _WSModule extends CoreClass {
 	 */
 	async SOCKET_JOIN_ROOM(payload) {
 		const { room, socketId } = payload;
-
-		// leave all other rooms
-		await WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId }, this);
-
 		return new Promise(resolve => {
 			// create room if it doesn't exist, and add socketId to array
 			if (WSModule.rooms[room]) WSModule.rooms[room].push(socketId);

+ 6 - 0
frontend/src/components/modals/ManageStation/index.vue

@@ -429,6 +429,12 @@ export default {
 		);
 	},
 	beforeDestroy() {
+		this.socket.dispatch(
+			"apis.leaveRoom",
+			`manage-station.${this.stationId}`,
+			() => {}
+		);
+
 		this.repositionSongInList([]);
 		this.clearStation();
 		this.showTab("settings");

+ 3 - 0
frontend/src/pages/Admin/index.vue

@@ -151,6 +151,9 @@ export default {
 	mounted() {
 		this.changeTab(this.$route.path);
 	},
+	beforeDestroy() {
+		this.socket.dispatch("apis.leaveRooms", () => {});
+	},
 	methods: {
 		changeTab(path) {
 			switch (path) {

+ 3 - 0
frontend/src/pages/Home.vue

@@ -680,6 +680,9 @@ export default {
 			this.orderOfFavoriteStations = res.data.order;
 		});
 	},
+	beforeDestroy() {
+		this.socket.dispatch("apis.leaveRoom", "home", () => {});
+	},
 	methods: {
 		init() {
 			this.socket.dispatch("stations.index", res => {

+ 7 - 0
frontend/src/pages/Profile/tabs/Playlists.vue

@@ -142,6 +142,13 @@ export default {
 			this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
 		});
 	},
+	beforeDestroy() {
+		this.socket.dispatch(
+			"apis.leaveRoom",
+			`profile.${this.userId}.playlists`,
+			() => {}
+		);
+	},
 	methods: {
 		showPlaylist(playlistId) {
 			this.editPlaylist(playlistId);

+ 7 - 0
frontend/src/pages/Profile/tabs/RecentActivity.vue

@@ -123,6 +123,13 @@ export default {
 			this.offsettedFromNextSet = 0;
 		});
 	},
+	beforeDestroy() {
+		this.socket.dispatch(
+			"apis.leaveRoom",
+			`profile.${this.userId}.activities`,
+			() => {}
+		);
+	},
 	methods: {
 		hideActivity(activityId) {
 			this.socket.dispatch("activities.hideActivity", activityId, res => {

+ 2 - 0
frontend/src/pages/Station/index.vue

@@ -1020,6 +1020,8 @@ export default {
 
 		clearInterval(this.activityWatchVideoDataInterval);
 
+		this.socket.dispatch("stations.leave", this.station._id, () => {});
+
 		this.leaveStation();
 	},
 	methods: {