Browse Source

fix: punishment creation emits to [admin] socket room

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 5 years ago
parent
commit
21d084c2e4

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

@@ -91,7 +91,7 @@ module.exports = {
 	 * @param cb
 	 */
 	joinAdminRoom: hooks.adminRequired((session, page, cb) => {
-		if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports' || page === 'news' || page === 'users' || page === 'statistics') {
+		if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports' || page === 'news' || page === 'users' || page === 'statistics' || page === 'punishments') {
 			utils.socketJoinRoom(session.socketId, `admin.${page}`);
 		}
 		cb({});

+ 9 - 14
backend/logic/actions/punishments.js

@@ -12,9 +12,9 @@ const db = moduleManager.modules["db"];
 const punishments = moduleManager.modules["punishments"];
 
 cache.sub('ip.ban', data => {
+	utils.emitToRoom('admin.punishments', 'event:admin.punishment.added', data.punishment);
 	utils.socketsFromIP(data.ip, sockets => {
 		sockets.forEach(socket => {
-			socket.emit('keep.event:banned', data.punishment);
 			socket.disconnect(true);
 		});
 	});
@@ -102,24 +102,19 @@ module.exports = {
 
 			(next) => {
 				punishments.addPunishment('banUserIp', value, reason, expiresAt, userId, next)
-			},
-
-			(punishment, next) => {
-				cache.pub('ip.ban', {ip: value, punishment});
-				next();
-			},
-		], async (err) => {
+			}
+		], async (err, punishment) => {
 			if (err && err !== true) {
 				err = await utils.getError(err);
 				logger.error("BAN_IP", `User ${userId} failed to ban IP address ${value} with the reason ${reason}. '${err}'`);
 				cb({ status: 'failure', message: err });
-			} else {
-				logger.success("BAN_IP", `User ${userId} has successfully banned Ip address ${value} with the reason ${reason}.`);
-				cb({
-					status: 'success',
-					message: 'Successfully banned IP address.'
-				});
 			}
+			logger.success("BAN_IP", `User ${userId} has successfully banned IP address ${value} with the reason ${reason}.`);
+			cache.pub('ip.ban', { ip: value, punishment });
+			return cb({
+				status: 'success',
+				message: 'Successfully banned IP address.'
+			});
 		});
 	}),
 

+ 4 - 1
frontend/components/Admin/Punishments.vue

@@ -157,7 +157,7 @@ export default {
 			this.socket.emit("punishments.index", res => {
 				if (res.status === "success") this.punishments = res.data;
 			});
-			// this.socket.emit('apis.joinAdminRoom', 'punishments', () => {});
+			this.socket.emit("apis.joinAdminRoom", "punishments", () => {});
 		},
 		...mapActions("modals", ["openModal"]),
 		...mapActions("admin/punishments", ["viewPunishment"])
@@ -167,6 +167,9 @@ export default {
 			this.socket = socket;
 			if (this.socket.connected) this.init();
 			io.onConnect(() => this.init());
+			socket.on("event:admin.punishment.added", punishment => {
+				this.punishments.push(punishment);
+			});
 		});
 	}
 };

+ 1 - 1
frontend/main.js

@@ -121,7 +121,7 @@ lofig.get("serverDomain", res => {
 			});
 		});
 		socket.on("keep.event:banned", ban => {
-			store.dispatch("user/auth/banned", ban);
+			store.dispatch("user/auth/banUser", ban);
 		});
 		socket.on("event:user.username.changed", username => {
 			store.dispatch("user/auth/updateUsername", username);

+ 3 - 3
frontend/store/modules/user.js

@@ -161,8 +161,8 @@ const modules = {
 			authData: ({ commit }, data) => {
 				commit("authData", data);
 			},
-			banned: ({ commit }, ban) => {
-				commit("banned", ban);
+			banUser: ({ commit }, ban) => {
+				commit("banUser", ban);
 			},
 			updateUsername: ({ commit }, username) => {
 				commit("updateUsername", username);
@@ -193,7 +193,7 @@ const modules = {
 				state.userId = data.userId;
 				state.gotData = true;
 			},
-			banned(state, ban) {
+			banUser(state, ban) {
 				state.banned = true;
 				state.ban = ban;
 			},