聊天:获取私聊历史消息
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import {Application, BackendSession} from 'pinus';
|
||||
import { resResult } from '../../../pubUtils/util';
|
||||
import { STATUS } from '../../../consts';
|
||||
import { createPrivateMsg, pushMsgToRole } from '../../../services/chatService';
|
||||
import { DEFAULT_MSG_PER_PAGE, STATUS } from '../../../consts';
|
||||
import { createPrivateMsg, getPrivateMessages, pushMsgToRole } from '../../../services/chatService';
|
||||
|
||||
|
||||
export default function(app: Application) {
|
||||
@@ -102,7 +102,7 @@ export class ChatHandler {
|
||||
* @param {{}} msg
|
||||
* @memberof ChatHandler
|
||||
*/
|
||||
async getInitMessage(msg: {}) {
|
||||
async getInitMessage(msg: {}, session: BackendSession) {
|
||||
|
||||
}
|
||||
|
||||
@@ -111,8 +111,10 @@ export class ChatHandler {
|
||||
* @param {{targetRoleId: string, fromSeqId: number, count: number}} msg 聊天对象;聊天消息的顺序 id,不传为从最新开始;count 为消息条数
|
||||
* @memberof ChatHandler
|
||||
*/
|
||||
async getPrivateMessage(msg: {targetRoleId: string, fromSeqId: number, count: number}) {
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { CounterModel } from './../db/Counter';
|
||||
import { STATUS } from './../consts/statusCode';
|
||||
import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './../db/PrivateMessage';
|
||||
import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage';
|
||||
@@ -27,13 +28,22 @@ export function groupRoomId(channel: string, channelId: string) {
|
||||
return `${channel}_${channelId}`;
|
||||
}
|
||||
|
||||
function msgCounterName(roomId: string) {
|
||||
return `chat_${roomId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置群消息和私聊消息的公共字段
|
||||
* @param {(PrivateMessageParam | GroupMessageParam)} msg
|
||||
*/
|
||||
function setupBaseMsgParm(msg: PrivateMessageParam | GroupMessageParam ) {
|
||||
async function setupBaseMsgParm(msg: PrivateMessageParam | GroupMessageParam ) {
|
||||
msg.msgCode = genCode(MSG_CODE_LEN);
|
||||
msg.status = MSG_STATUS.NORMAL;
|
||||
if (!msg.roomId) {
|
||||
console.error('roomId needed while setup msg parameter');
|
||||
return;
|
||||
}
|
||||
msg.seqId = await CounterModel.getNewCounter({name: msgCounterName(msg.roomId), def: 1});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,10 +56,10 @@ function setupBaseMsgParm(msg: PrivateMessageParam | GroupMessageParam ) {
|
||||
* @param {string} targetMsgCode
|
||||
* @returns
|
||||
*/
|
||||
function createPrivateMsgData(roleId: string, roleName: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
|
||||
async function createPrivateMsgData(roleId: string, roleName: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
|
||||
const result: PrivateMessageParam = {roleId, roleName, type, content, targetRoleId, targetMsgCode};
|
||||
setupBaseMsgParm(result);
|
||||
result.roomId = privateRoomId(roleId, targetRoleId);
|
||||
await setupBaseMsgParm(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -65,7 +75,7 @@ function createPrivateMsgData(roleId: string, roleName: string, type: number, co
|
||||
* @returns
|
||||
*/
|
||||
export async function createPrivateMsg(roleId: string, roleName: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
|
||||
const msgData: PrivateMessageParam = createPrivateMsgData(roleId, roleName, type, content, targetRoleId, targetMsgCode);
|
||||
const msgData: PrivateMessageParam = await createPrivateMsgData(roleId, roleName, type, content, targetRoleId, targetMsgCode);
|
||||
const result: PrivateMessageType = await PrivateMessageModel.createMsg(msgData);
|
||||
return result;
|
||||
}
|
||||
@@ -81,4 +91,18 @@ export async function pushMsgToRole(targetRoleId: string, msg: PrivateMessageTyp
|
||||
if (sid) {
|
||||
pinus.app.get('channelService').pushMessageByUids(ON_MSG_ROUTE, resResult(STATUS.SUCCESS, msg), [{uid: targetRoleId, sid}]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取私聊历史消息
|
||||
* @export
|
||||
* @param {string} roleId
|
||||
* @param {string} targetRoleId
|
||||
* @param {number} fromSeqId
|
||||
* @param {number} count
|
||||
* @returns
|
||||
*/
|
||||
export async function getPrivateMessages(roleId: string, targetRoleId: string, fromSeqId: number, count: number) {
|
||||
const result = await PrivateMessageModel.getMsgs(privateRoomId(roleId, targetRoleId), fromSeqId, count);
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user