Browse Source

fix(Websockets): SOCKETS_FROM_USER used promises, not callbacks

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 years ago
parent
commit
8bef198999
3 changed files with 53 additions and 82 deletions
  1. 45 72
      backend/logic/actions/users.js
  2. 6 8
      backend/logic/utils.js
  3. 2 2
      frontend/src/pages/Settings/index.vue

+ 45 - 72
backend/logic/actions/users.js

@@ -21,13 +21,10 @@ const activities = require("../activities");
 cache.runJob("SUB", {
     channel: "user.updateUsername",
     cb: (user) => {
-        utils.runJob("SOCKETS_FROM_USER", {
-            userId: user._id,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit("event:user.username.changed", user.username);
-                });
-            },
+        utils.runJob("SOCKETS_FROM_USER", { userId: user._id }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit("event:user.username.changed", user.username);
+            });
         });
     },
 });
@@ -35,13 +32,10 @@ cache.runJob("SUB", {
 cache.runJob("SUB", {
     channel: "user.removeSessions",
     cb: (userId) => {
-        utils.runJob("SOCKETS_FROM_USER_WITHOUT_CACHE", {
-            userId: userId,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit("keep.event:user.session.removed");
-                });
-            },
+        utils.runJob("SOCKETS_FROM_USER_WITHOUT_CACHE", { userId }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit("keep.event:user.session.removed");
+            });
         });
     },
 });
@@ -49,55 +43,43 @@ cache.runJob("SUB", {
 cache.runJob("SUB", {
     channel: "user.linkPassword",
     cb: (userId) => {
-        utils.runJob("SOCKETS_FROM_USER", {
-            userId: userId,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit("event:user.linkPassword");
-                });
-            },
+        utils.runJob("SOCKETS_FROM_USER", { userId }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit("event:user.linkPassword");
+            });
         });
     },
 });
 
 cache.runJob("SUB", {
-    channel: "user.linkGitHub",
-    cb: (userId) => {
-        utils.runJob("SOCKETS_FROM_USER", {
-            userId: userId,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit("event:user.linkGitHub");
-                });
-            },
+    channel: "user.unlinkPassword",
+    cb: userId => {
+        utils.runJob("SOCKETS_FROM_USER", { userId }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit("event:user.unlinkPassword");
+            });
         });
     },
 });
 
 cache.runJob("SUB", {
-    channel: "user.unlinkPassword",
+    channel: "user.linkGithub",
     cb: (userId) => {
-        utils.runJob("SOCKETS_FROM_USER", {
-            userId: userId,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit("event:user.unlinkPassword");
-                });
-            },
+        utils.runJob("SOCKETS_FROM_USER", { userId }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit("event:user.linkGithub");
+            });
         });
     },
 });
 
 cache.runJob("SUB", {
-    channel: "user.unlinkGitHub",
-    cb: (userId) => {
-        utils.runJob("SOCKETS_FROM_USER", {
-            userId: userId,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit("event:user.unlinkGitHub");
-                });
-            },
+    channel: "user.unlinkGithub",
+    cb: userId => {
+        utils.runJob("SOCKETS_FROM_USER", { userId }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit("event:user.unlinkGithub");
+            });
         });
     },
 });
@@ -105,14 +87,11 @@ cache.runJob("SUB", {
 cache.runJob("SUB", {
     channel: "user.ban",
     cb: (data) => {
-        utils.runJob("SOCKETS_FROM_USER", {
-            userId: data.userId,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit("keep.event:banned", data.punishment);
-                    socket.disconnect(true);
-                });
-            },
+        utils.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit("keep.event:banned", data.punishment);
+                socket.disconnect(true);
+            });
         });
     },
 });
@@ -120,13 +99,10 @@ cache.runJob("SUB", {
 cache.runJob("SUB", {
     channel: "user.favoritedStation",
     cb: (data) => {
-        utils.runJob("SOCKETS_FROM_USER", {
-            userId: data.userId,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit("event:user.favoritedStation", data.stationId);
-                });
-            },
+        utils.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit("event:user.favoritedStation", data.stationId);
+            });
         });
     },
 });
@@ -134,16 +110,13 @@ cache.runJob("SUB", {
 cache.runJob("SUB", {
     channel: "user.unfavoritedStation",
     cb: (data) => {
-        utils.runJob("SOCKETS_FROM_USER", {
-            userId: data.userId,
-            cb: (response) => {
-                response.sockets.forEach((socket) => {
-                    socket.emit(
-                        "event:user.unfavoritedStation",
-                        data.stationId
-                    );
-                });
-            },
+        utils.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
+            response.sockets.forEach((socket) => {
+                socket.emit(
+                    "event:user.unfavoritedStation",
+                    data.stationId
+                );
+            });
         });
     },
 });

+ 6 - 8
backend/logic/utils.js

@@ -193,7 +193,6 @@ class UtilsModule extends CoreClass {
     }
 
     SOCKETS_FROM_SESSION_ID(payload) {
-        //sessionId, cb
         return new Promise(async (resolve, reject) => {
             let io = await this.io.runJob("IO", {});
             let ns = io.of("/");
@@ -216,11 +215,11 @@ class UtilsModule extends CoreClass {
     }
 
     SOCKETS_FROM_USER(payload) {
-        //userId, cb
         return new Promise(async (resolve, reject) => {
             let io = await this.io.runJob("IO", {});
             let ns = io.of("/");
             let sockets = [];
+
             if (ns) {
                 async.each(
                     Object.keys(ns.connected),
@@ -239,12 +238,13 @@ class UtilsModule extends CoreClass {
                                     sockets.push(ns.connected[id]);
                                 next();
                             })
-                            .catch(() => {
-                                next();
+                            .catch(err => {
+                                next(err);
                             });
                     },
-                    () => {
-                        resolve({ sockets });
+                    err => {
+                        if (err) return reject(err);
+                        return resolve({ sockets });
                     }
                 );
             }
@@ -252,7 +252,6 @@ class UtilsModule extends CoreClass {
     }
 
     SOCKETS_FROM_IP(payload) {
-        //ip, cb
         return new Promise(async (resolve, reject) => {
             let io = await this.io.runJob("IO", {});
             let ns = io.of("/");
@@ -288,7 +287,6 @@ class UtilsModule extends CoreClass {
     }
 
     SOCKETS_FROM_USER_WITHOUT_CACHE(payload) {
-        //userId, cb
         return new Promise(async (resolve, reject) => {
             let io = await this.io.runJob("IO", {});
             let ns = io.of("/");

+ 2 - 2
frontend/src/pages/Settings/index.vue

@@ -99,11 +99,11 @@ export default {
 					this.updateOriginalUser("github", false)
 				);
 
-				this.socket.on("event:user.linkGitHub", () =>
+				this.socket.on("event:user.linkGithub", () =>
 					this.updateOriginalUser("github", true)
 				);
 
-				this.socket.on("event:user.unlinkGitHub", () =>
+				this.socket.on("event:user.unlinkGithub", () =>
 					this.updateOriginalUser("github", false)
 				);
 			});