Преглед на файлове

update changeVoiceStatus

gemercheung преди 3 години
родител
ревизия
b17c6e5192
променени са 1 файла, в които са добавени 70 реда и са изтрити 66 реда
  1. 70 66
      src/controller/syncDeviceController.js

+ 70 - 66
src/controller/syncDeviceController.js

@@ -1,66 +1,70 @@
-import { BasicController } from "./basicController.js";
-// import { RoomAssistant } from "./assistant.js";
-import { getAllRoomUsers, updateRoomUser } from "../service/userService.js";
-import { FROMTYPE, EVENT } from "../enum/index.js";
-
-const prefix = process.env.REDIS_PREFIX || "chat";
-const getInKey = (realKey) => {
-  return `${prefix}:${realKey}`;
-};
-
-export class SyncDeviceController extends BasicController {
-  constructor(...args) {
-    super(...args);
-    this.room = args[3];
-  }
-  async run() {
-    if (this.room.user) {
-      const syncId = this.room.syncId;
-      const userId = this.room.userId;
-      const from = this.room.user.from;
-      this.joinSyncClient(syncId, userId, from);
-      this.initAction();
-    } else {
-      this.logger.error("roomController:init: 初始化失败!");
-    }
-  }
-  async joinSyncClient(syncId, userId, from) {
-    this.logger.info("syncId", { syncId, userId, from });
-    try {
-      await this.redisCli.hSet(getInKey(syncId), from, userId);
-      this.socket.join(syncId);
-    } catch (error) {
-      this.logger.error("joinSyncClient", error);
-    }
-  }
-  initAction() {
-    this.socket.on(EVENT.clientSyncAction, (data) => {
-      this.socket.broadcast.to(this.room.syncId).emit(EVENT.clientSyncAction, data);
-    });
-    this.socket.on(EVENT.changeVoiceStatus, async ({ status }) => {
-      try {
-        const current = await this.room.currentUser();
-        const updateUser = {
-          ...current,
-          onlineStatus: 1,
-          voiceStatus: status,
-        };
-
-        
-        const isUpdate = await updateRoomUser(this.room.roomId, this.room.userId, updateUser);
-        const AllRoomUsers = await getAllRoomUsers(this.room.roomId);
-        this.logger.info("changeVoiceStatus", { roomId: this.room.roomId, updateUser, AllRoomUsers: AllRoomUsers.length });
-        this.socket.broadcast.to(this.room.roomId).emit(EVENT.changeVoiceStatus, {
-          user: updateUser,
-          roomsPerson: AllRoomUsers,
-        });
-        this.socket.broadcast.to(this.room.syncId).emit(EVENT.changeVoiceStatus, {
-          user: updateUser,
-          roomsPerson: AllRoomUsers,
-        });
-      } catch (error) {
-        this.logger.error("event::changeVoiceStatus", error);
-      }
-    });
-  }
-}
+import { BasicController } from "./basicController.js";
+// import { RoomAssistant } from "./assistant.js";
+import { getAllRoomUsers, updateRoomUser, isUserInRoom } from "../service/userService.js";
+import { FROMTYPE, EVENT } from "../enum/index.js";
+
+const prefix = process.env.REDIS_PREFIX || "chat";
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
+
+export class SyncDeviceController extends BasicController {
+  constructor(...args) {
+    super(...args);
+    this.room = args[3];
+  }
+  async run() {
+    if (this.room.user) {
+      const syncId = this.room.syncId;
+      const userId = this.room.userId;
+      const from = this.room.user.from;
+      this.joinSyncClient(syncId, userId, from);
+      this.initAction();
+    } else {
+      this.logger.error("roomController:init: 初始化失败!");
+    }
+  }
+  async joinSyncClient(syncId, userId, from) {
+    this.logger.info("syncId", { syncId, userId, from });
+    try {
+      await this.redisCli.hSet(getInKey(syncId), from, userId);
+      this.socket.join(syncId);
+    } catch (error) {
+      this.logger.error("joinSyncClient", error);
+    }
+  }
+  initAction() {
+    this.socket.on(EVENT.clientSyncAction, (data) => {
+      this.socket.broadcast.to(this.room.syncId).emit(EVENT.clientSyncAction, data);
+    });
+    this.socket.on(EVENT.changeVoiceStatus, async ({ status }) => {
+      try {
+        const from = this.room.user.from;
+        const roomId = this.room.roomId;
+        const user = await this.room.currentUser();
+        const isInRoom = await isUserInRoom(roomId, user.userId);
+        if (isInRoom) {
+          const updateUser = {
+            ...isInRoom,
+            onlineStatus: 1,
+            voiceStatus: Number(status),
+          };
+
+          const isUpdate = await updateRoomUser(this.room.roomId, this.room.userId, updateUser);
+          const AllRoomUsers = await getAllRoomUsers(this.room.roomId);
+          this.logger.info("changeVoiceStatus", { roomId: this.room.roomId, updateUser, AllRoomUsers: AllRoomUsers.length });
+          this.socket.broadcast.to(this.room.roomId).emit(EVENT.changeVoiceStatus, {
+            user: updateUser,
+            roomsPerson: AllRoomUsers,
+          });
+          this.socket.broadcast.to(this.room.syncId).emit(EVENT.changeVoiceStatus, {
+            user: updateUser,
+            roomsPerson: AllRoomUsers,
+          });
+        }
+      } catch (error) {
+        this.logger.error("event::changeVoiceStatus", error);
+      }
+    });
+  }
+}