2023-05-08 20:02:52 +08:00

313 lines
7.4 KiB
JavaScript

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");
}
/**
* @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 channel = this.channelService.getChannel(channelId, true);
if (channel.getMember(uid)) {
next(null, {
code: Code.CHAT.FA_USER_ALREAD_JOINED,
msg: "User already joined the world channel",
});
return;
}
channel.add(session.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"));
}
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}`;
const channel = this.channelService.getChannel(channelId, true);
if (channel.getMember(uid)) {
next(null, {
code: Code.CHAT.FA_USER_ALREAD_JOINED,
msg: "User already joined the team channel",
});
return;
}
channel.add(session.uid, session.get("sid"));
next(null, { code: 200 });
/**
* @api {push} onAdd onAdd 推送加入队伍频道消息
* @apiGroup Chat
* @apiParam {String} user 用户ID
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "route": "onAdd",
* "user": "5f9f9f9f9f9f9f9f9f9f9f9f"
* }
*/
channel.pushMessage({
code: Code.OK,
route: "onAdd",
user: uid,
});
}
/**
* @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 teamId = msg.teamId;
const channelId = `teamChannel_${teamId}`;
// 把this.app.get('channelService') 替换成 this.channelService
const channel = this.app.get("channelService").getChannel(channelId, false);
if (channel) {
channel.leave(uid, session.get("sid"));
}
next(null, { code: 200 });
/**
* @api {push} onLeave onLeave 推送离开队伍频道消息
* @apiGroup Chat
* @apiParam {String} user 用户ID
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "route": "onLeave",
* "user": "5f9f9f9f9f9f9f9f9f9f9f9f"
* }
*/
channel.pushMessage({
code: Code.OK,
route: "onLeave",
user: uid,
});
}
/**
* @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";
const channel = this.app.get("channelService").getChannel(channelId, true);
/**
* @api {push} onChat onChat 推送聊天消息
* @apiGroup Chat
* @apiParam {String} from 发送者信息
* @apiParam {String} content 聊天内容
* @apiParam {String} contentType 内容类型
* @apiParam {String} type 聊天类型 [ world | team ]
*
* @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"
* }
*
*/
channel.pushMessage({
code: Code.OK,
route: "onChat",
from: senderInfo,
content: content,
contentType: contentType,
type: "world",
});
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 channel = this.app.get("channelService").getChannel(channelId, true);
if (channel.getMember(uid)) {
channel.pushMessage({
code: Code.OK,
route: "onChat",
from: senderInfo,
content: content,
contentType: contentType,
type: "team",
teamId: teamId,
});
next(null, { code: 200 });
}
next(null, {
code: Code.CHAT.FA_USER_NOT_TEAM_MEMBER,
msg: "User not team member",
});
}
}