129 lines
5.1 KiB
TypeScript
129 lines
5.1 KiB
TypeScript
import { CHANNEL_PREFIX, MSG_SOURCE } from './../../../consts/constModules/chatConst';
|
||
import {Application, BackendSession} from 'pinus';
|
||
import { resResult } from '../../../pubUtils/util';
|
||
import { DEFAULT_MSG_PER_PAGE, STATUS } from '../../../consts';
|
||
import { createGroupMsg, createPrivateMsg, getPrivateMessages, groupRoomId, pushGroupMsgToRoom, pushMsgToRole } from '../../../services/chatService';
|
||
|
||
|
||
export default function(app: Application) {
|
||
return new ChatHandler(app);
|
||
}
|
||
|
||
export class ChatHandler {
|
||
constructor(private app: Application) {
|
||
}
|
||
|
||
/**
|
||
* Send messages to users
|
||
*
|
||
* @param {Object} msg message from client
|
||
* @param {Object} session
|
||
*
|
||
*/
|
||
async send(msg: {content: string , target: string}, session: BackendSession) {
|
||
let rid = session.get('rid');
|
||
let username = session.uid.split('*')[0];
|
||
let channelService = this.app.get('channelService');
|
||
let param = {
|
||
msg: msg.content,
|
||
from: username,
|
||
target: msg.target
|
||
};
|
||
let channel = channelService.getChannel(rid, false);
|
||
|
||
// the target is all users
|
||
if (msg.target === '*') {
|
||
channel.pushMessage('onChat', resResult(STATUS.SUCCESS, param));
|
||
}
|
||
// the target is specific user
|
||
else {
|
||
let tuid = msg.target + '*' + rid;
|
||
let tsid = channel.getMember(tuid)['sid'];
|
||
channelService.pushMessageByUids('onChat', resResult(STATUS.SUCCESS, param), [{
|
||
uid: tuid,
|
||
sid: tsid
|
||
}]);
|
||
}
|
||
}
|
||
|
||
async send2(msg: {content: string , target: string}, session: BackendSession) {
|
||
let rid = session.get('rid');
|
||
let username = session.uid.split('*')[0];
|
||
let channelService = this.app.get('channelService');
|
||
let param = {
|
||
msg: msg.content,
|
||
from: username,
|
||
target: msg.target
|
||
};
|
||
let channel = channelService.getChannel(rid, false);
|
||
|
||
// the target is all users
|
||
if (msg.target === '*') {
|
||
channel.pushMessage('onChat', resResult(STATUS.SUCCESS, param));
|
||
}
|
||
// the target is specific user
|
||
else {
|
||
let tuid = msg.target + '*' + rid;
|
||
let tsid = channel.getMember(tuid)['sid'];
|
||
channelService.pushMessageByUids('onChat', resResult(STATUS.SUCCESS, param), [{
|
||
uid: tuid,
|
||
sid: tsid
|
||
}]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description 群聊发送接口
|
||
* @param {{ channel: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }} msg
|
||
* @param {BackendSession} session
|
||
* @memberof ChatHandler
|
||
*/
|
||
async sendGroupMessage(msg: { channel: string, channelId: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
|
||
const { channel, channelId, type, content, targetRoleId, targetMsgCode } = msg;
|
||
if (channel === CHANNEL_PREFIX.SYS) return resResult(STATUS.SYS_CHANNEL_AUTH_NOT_ENOUGH);
|
||
const roleId = session.get('roleId');
|
||
const roleName = session.get('roleName');
|
||
const msgData = await createGroupMsg(roleId, roleName, channel, channelId, type, MSG_SOURCE.ROLE_SEND_TEXT, content, targetRoleId, targetMsgCode);
|
||
await pushGroupMsgToRoom(groupRoomId(channel, channelId), msgData);
|
||
if (!msgData) return resResult(STATUS.WRONG_PARMS);
|
||
return resResult(STATUS.SUCCESS);
|
||
}
|
||
|
||
/**
|
||
* @description 私聊发送接口
|
||
* @param {{ type: number, content: string, targetRoleId: string, targetMsgCode: string }} msg
|
||
* @param {BackendSession} session
|
||
* @memberof ChatHandler
|
||
*/
|
||
async sendPrivateMessage(msg: { type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
|
||
const { type, content, targetRoleId, targetMsgCode } = msg;
|
||
const roleId = session.get('roleId');
|
||
const roleName = session.get('roleName');
|
||
const msgData = await createPrivateMsg(roleId, roleName, type, MSG_SOURCE.ROLE_SEND_TEXT, content, targetRoleId, targetMsgCode);
|
||
await pushMsgToRole(targetRoleId, msgData);
|
||
if (!msgData) return resResult(STATUS.WRONG_PARMS);
|
||
return resResult(STATUS.SUCCESS, msgData);
|
||
}
|
||
|
||
/**
|
||
* @description 获取初始聊天消息,数据和登录时返回的一致
|
||
* @param {{}} msg
|
||
* @memberof ChatHandler
|
||
*/
|
||
async getInitMessage(msg: {}, session: BackendSession) {
|
||
|
||
}
|
||
|
||
/**
|
||
* @description 获取私聊历史消息
|
||
* @param {{targetRoleId: string, fromSeqId: number, count: number}} msg 聊天对象;聊天消息的顺序 id,不传为从最新开始;count 为消息条数
|
||
* @memberof ChatHandler
|
||
*/
|
||
async getPrivateMessage(msg: {targetRoleId: string, fromSeqId: number, count: number}, session: BackendSession) {
|
||
const roleId = session.get('roleId');
|
||
const { targetRoleId, fromSeqId = Infinity, count = DEFAULT_MSG_PER_PAGE } = msg;
|
||
const msgs = await getPrivateMessages(roleId, targetRoleId, fromSeqId, count);
|
||
return resResult(STATUS.SUCCESS, { msgs });
|
||
}
|
||
}
|