const Code = require("shared/code"); module.exports = function (app) { return new ChatHandler(app); }; class ChatHandler { constructor(app) { this.app = app; this.channelService = this.app.get("channelService"); this.gcs = this.app.get("globalChannelService"); } /** * @api {post} chat.chatHandler.joinWorldChannel joinWorldChannel 加入世界频道 * @apiGroup Chat * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to join world channel." * } * * */ async joinWorldChannel(msg, session, next) { const uid = session.uid; const channelId = "worldChannel"; const list = await this.gcs.getMemberByUid(channelId, uid); if (list.length > 0) { next(null, { code: Code.CHAT.FA_USER_ALREAD_JOINED, msg: "User already joined the world channel", }); return; } this.gcs.add(channelId, uid, session.get("sid")); next(null, { code: Code.OK }); } /** * @api {post} chat.chatHandler.leaveWorldChannel leaveWorldChannel 离开世界频道 * @apiGroup Chat * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to leave world channel." * } * */ async leaveWorldChannel(msg, session, next) { const userInfo = session.get("userInfo"); const username = userInfo.name; const channelId = "worldChannel"; const channel = this.channelService.getChannel(channelId, false); if (channel) { channel.leave(session.uid, session.get("sid")); } this.gcs.leave(channelId, session.uid, session.get("sid"), function () {}); next(null, { code: Code.OK }); } /** * @api {post} chat.chatHandler.joinTeamChannel joinTeamChannel 加入队伍频道 * @apiGroup Chat * * @apiParam {String} teamId 队伍ID * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to join team channel." * } */ async joinTeamChannel(msg, session, next) { const uid = session.uid; const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; this.gcs.add(channelId, session.uid, session.get("sid"), function () {}); session.set("teamId", teamId); session.push("teamId", function (err) { if (err) { console.error("set teamId for session service failed! error is : %j", err.stack); } }); next(null, { code: 200 }); /** * @api {push} onAdd onAdd 推送加入队伍频道消息 * @apiGroup Chat * @apiParam {String} user 用户ID * * @apiSuccessExample {json} Success-Response: * { * "code": 200, * "route": "onAdd", * "user": "5f9f9f9f9f9f9f9f9f9f9f9f" * } */ this.gcs.pushMessage( "connector", "onAdd", { code: Code.OK, route: "onAdd", user: uid, }, channelId ); } /** * @api {post} chat.chatHandler.leaveTeamChannel leaveTeamChannel 离开队伍频道 * @apiGroup Chat * * @apiParam {String} teamId 队伍ID * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to leave team channel." * } */ async leaveTeamChannel(msg, session, next) { const uid = session.uid; const sid = session.get("sid"); const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; /** * @api {push} onLeave onLeave 推送离开队伍频道消息 * @apiGroup Chat * @apiParam {String} user 用户ID * * @apiSuccessExample {json} Success-Response: * { * "code": 200, * "route": "onLeave", * "user": "5f9f9f9f9f9f9f9f9f9f9f9f" * } */ this.gcs.pushMessage( "connector", "onLeave", { code: Code.OK, route: "onLeave", user: uid, }, channelId ); this.gcs.leave(channelId, uid, sid, function () {}); next(null, { code: 200 }); } /** * @api {post} chat.chatHandler.worldChat worldChat 世界聊天 * @apiGroup Chat * * @apiParam {String} content 聊天内容 * @apiParam {String} contentType 内容类型 * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to world chat." * } */ async worldChat(msg, session, next) { const userInfo = session.get("userInfo"); const senderInfo = { username: userInfo.name, account_id: userInfo.account_id, head_frame: userInfo.head_frame, head_id: userInfo.head_id, }; const content = msg.content; const contentType = msg.contentType; const channelId = "worldChannel"; /** * @api {push} onChat onChat 推送聊天消息 * @apiGroup Chat * @apiParam {String} from 发送者信息 * @apiParam {String} content 聊天内容 * @apiParam {String} contentType 内容类型 * @apiParam {String} type 聊天类型 [ world | team | private ] * * @apiSuccessExample {json} Success-Response: * { * "code": 200, * "route": "onChat", * "from": { * "username": "username", * "account_id": "account_id", * "head_frame": "head_frame", * "head_id": "head_id" * }, * "content": "content", * "contentType": "contentType", * "type": "world" * } * */ this.gcs.pushMessage( "connector", "onChat", { code: Code.OK, route: "onChat", from: senderInfo, content: content, contentType: contentType, type: "world", }, channelId ); next(null, { code: 200 }); } /** * @api {post} chat.chatHandler.teamChat teamChat 队伍聊天 * @apiGroup Chat * * @apiParam {String} content 聊天内容 * @apiParam {String} contentType 内容类型 * @apiParam {String} teamId 队伍ID * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to team chat." * } */ async teamChat(msg, session, next) { const uid = session.uid; const userInfo = session.get("userInfo"); const senderInfo = { username: userInfo.name, account_id: userInfo.account_id, head_frame: userInfo.head_frame, head_id: userInfo.head_id, }; const content = msg.content; const contentType = msg.contentType; const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; const member = this.gcs.getMemberByUid(channelId, uid); if (member) { this.gcs.pushMessage( "connector", "onChat", { code: Code.OK, route: "onChat", from: senderInfo, content: content, contentType: contentType, type: "team", }, channelId ); next(null, { code: 200 }); } next(null, { code: Code.CHAT.FA_USER_NOT_TEAM_MEMBER, msg: "User not team member", }); } /** @api {post} chat.chatHandler.privateChat privateChat 私聊 * @apiGroup Chat * * @apiParam {String} content 聊天内容 * @apiParam {String} contentType 内容类型 * @apiParam {String} targetuid 目标用户ID * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to private chat." * } * * */ async privateChat(msg, session, next) { const { uid } = session; const userInfo = session.get("userInfo"); const senderInfo = { username: userInfo.name, account_id: userInfo.account_id, head_frame: userInfo.head_frame, head_id: userInfo.head_id, }; const content = msg.content; const contentType = msg.contentType; const targetuid = msg.targetuid; const channelId = `privateChannel_${targetuid}`; this.gcs.pushMessage( "connector", "onChat", { code: Code.OK, route: "onChat", from: senderInfo, content: content, contentType: contentType, type: "private", }, channelId ); next(null, { code: 200 }); } }