聊天:获取私聊历史消息

This commit is contained in:
liangtongchuan
2021-03-04 21:41:22 +08:00
parent 0bb4caf291
commit ca2d597947
5 changed files with 68 additions and 16 deletions

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
import { MSG_TYPE, ON_MSG_ROUTE } from './../app/consts';
import { PrivateMessageParam, PrivateMessageType } from './../app/db/PrivateMessage';
import { DEFAULT_MSG_PER_PAGE, MSG_TYPE, ON_MSG_ROUTE } from './../app/consts';
import { indexOf } from 'underscore';
import { COM_TEAM_STATUS, DEFAULT_HEROES } from './../../shared/consts/consts';
import { Client } from './Client';
@@ -16,7 +17,7 @@ function sendPrivateMessageParm(targetRoleInfo, msg) {
return result;
}
describe('私聊消息发送和接收', function() {
describe('聊天测试', function() {
let pinusClient: PinusWSClient;
let pinusClientT: PinusWSClient; // 用做测试队友
let roleInfo;
@@ -58,4 +59,22 @@ describe('私聊消息发送和接收', function() {
});
});
it('获取私聊历史消息', function(done) {
for (let i = 0; i < DEFAULT_MSG_PER_PAGE; i++) {
pinusClient.request('chat.chatHandler.sendPrivateMessage', sendPrivateMessageParm(roleInfoT, TEXT_MSG), (res) => {
checkSuccessResponse(res, false);
});
}
setTimeout(() => {
pinusClient.request('chat.chatHandler.getPrivateMessage', {targetRoleId: roleInfoT.roleId}, (res) => {
checkSuccessResponse(res);
expect(res.data.msgs).to.be.an('array');
expect(res.data.msgs.length).to.be.equal(DEFAULT_MSG_PER_PAGE);
res.data.msgs.forEach(msg => {
expect(msg.content).to.equal(TEXT_MSG.content);
});
done();
})
}, 1000);
});
});

View File

@@ -11,4 +11,6 @@ export const MSG_TYPE = {
IMG: 2
}
export const ON_MSG_ROUTE = 'onMessage';
export const ON_MSG_ROUTE = 'onMessage';
export const DEFAULT_MSG_PER_PAGE = 10;

View File

@@ -5,7 +5,7 @@ import { index, getModelForClass, prop, DocumentType, modelOptions } from '@type
* 私聊信息
**/
@modelOptions({ schemaOptions: { id: false } })
@index({ roomId: 1, seqId: 1 })
@index({ roomId: 1, seqId: -1 })
@index({ msgId: 1 })
export default class PrivateMessage extends BaseModel {
@prop({ required: true, default: '' })
@@ -36,7 +36,7 @@ export default class PrivateMessage extends BaseModel {
content: string; // 消息内容
public static async createMsg(data: PrivateMessageParam) {
const result = await PrivateMessageModel.findOneAndUpdate({}, {...data}, {upsert: true, new: true}).lean();
const result = await PrivateMessageModel.findOneAndUpdate({ msgCode: data.msgCode }, {...data}, {upsert: true, new: true}).lean();
return result;
}
@@ -44,6 +44,11 @@ export default class PrivateMessage extends BaseModel {
const result = await PrivateMessageModel.findOne(query).lean();
return result;
}
public static async getMsgs(roomId: string, fromSeqId: number, count: number) {
const result = await PrivateMessageModel.find({ roomId, seqId: { $lt: fromSeqId } }).sort({ seqId: -1 }).limit(count).lean();
return result;
}
}