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); } }); } }