聊天:获取私聊历史消息
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import {Application, BackendSession} from 'pinus';
|
import {Application, BackendSession} from 'pinus';
|
||||||
import { resResult } from '../../../pubUtils/util';
|
import { resResult } from '../../../pubUtils/util';
|
||||||
import { STATUS } from '../../../consts';
|
import { DEFAULT_MSG_PER_PAGE, STATUS } from '../../../consts';
|
||||||
import { createPrivateMsg, pushMsgToRole } from '../../../services/chatService';
|
import { createPrivateMsg, getPrivateMessages, pushMsgToRole } from '../../../services/chatService';
|
||||||
|
|
||||||
|
|
||||||
export default function(app: Application) {
|
export default function(app: Application) {
|
||||||
@@ -102,7 +102,7 @@ export class ChatHandler {
|
|||||||
* @param {{}} msg
|
* @param {{}} msg
|
||||||
* @memberof ChatHandler
|
* @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 为消息条数
|
* @param {{targetRoleId: string, fromSeqId: number, count: number}} msg 聊天对象;聊天消息的顺序 id,不传为从最新开始;count 为消息条数
|
||||||
* @memberof ChatHandler
|
* @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 { STATUS } from './../consts/statusCode';
|
||||||
import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './../db/PrivateMessage';
|
import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './../db/PrivateMessage';
|
||||||
import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage';
|
import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage';
|
||||||
@@ -27,13 +28,22 @@ export function groupRoomId(channel: string, channelId: string) {
|
|||||||
return `${channel}_${channelId}`;
|
return `${channel}_${channelId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function msgCounterName(roomId: string) {
|
||||||
|
return `chat_${roomId}`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 设置群消息和私聊消息的公共字段
|
* @description 设置群消息和私聊消息的公共字段
|
||||||
* @param {(PrivateMessageParam | GroupMessageParam)} msg
|
* @param {(PrivateMessageParam | GroupMessageParam)} msg
|
||||||
*/
|
*/
|
||||||
function setupBaseMsgParm(msg: PrivateMessageParam | GroupMessageParam ) {
|
async function setupBaseMsgParm(msg: PrivateMessageParam | GroupMessageParam ) {
|
||||||
msg.msgCode = genCode(MSG_CODE_LEN);
|
msg.msgCode = genCode(MSG_CODE_LEN);
|
||||||
msg.status = MSG_STATUS.NORMAL;
|
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
|
* @param {string} targetMsgCode
|
||||||
* @returns
|
* @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};
|
const result: PrivateMessageParam = {roleId, roleName, type, content, targetRoleId, targetMsgCode};
|
||||||
setupBaseMsgParm(result);
|
|
||||||
result.roomId = privateRoomId(roleId, targetRoleId);
|
result.roomId = privateRoomId(roleId, targetRoleId);
|
||||||
|
await setupBaseMsgParm(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +75,7 @@ function createPrivateMsgData(roleId: string, roleName: string, type: number, co
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export async function createPrivateMsg(roleId: string, roleName: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
|
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);
|
const result: PrivateMessageType = await PrivateMessageModel.createMsg(msgData);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -81,4 +91,18 @@ export async function pushMsgToRole(targetRoleId: string, msg: PrivateMessageTyp
|
|||||||
if (sid) {
|
if (sid) {
|
||||||
pinus.app.get('channelService').pushMessageByUids(ON_MSG_ROUTE, resResult(STATUS.SUCCESS, msg), [{uid: targetRoleId, 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;
|
||||||
}
|
}
|
||||||
@@ -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 { indexOf } from 'underscore';
|
||||||
import { COM_TEAM_STATUS, DEFAULT_HEROES } from './../../shared/consts/consts';
|
import { COM_TEAM_STATUS, DEFAULT_HEROES } from './../../shared/consts/consts';
|
||||||
import { Client } from './Client';
|
import { Client } from './Client';
|
||||||
@@ -16,7 +17,7 @@ function sendPrivateMessageParm(targetRoleInfo, msg) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('私聊消息发送和接收', function() {
|
describe('聊天测试', function() {
|
||||||
let pinusClient: PinusWSClient;
|
let pinusClient: PinusWSClient;
|
||||||
let pinusClientT: PinusWSClient; // 用做测试队友
|
let pinusClientT: PinusWSClient; // 用做测试队友
|
||||||
let roleInfo;
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,4 +11,6 @@ export const MSG_TYPE = {
|
|||||||
IMG: 2
|
IMG: 2
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ON_MSG_ROUTE = 'onMessage';
|
export const ON_MSG_ROUTE = 'onMessage';
|
||||||
|
|
||||||
|
export const DEFAULT_MSG_PER_PAGE = 10;
|
||||||
@@ -5,7 +5,7 @@ import { index, getModelForClass, prop, DocumentType, modelOptions } from '@type
|
|||||||
* 私聊信息
|
* 私聊信息
|
||||||
**/
|
**/
|
||||||
@modelOptions({ schemaOptions: { id: false } })
|
@modelOptions({ schemaOptions: { id: false } })
|
||||||
@index({ roomId: 1, seqId: 1 })
|
@index({ roomId: 1, seqId: -1 })
|
||||||
@index({ msgId: 1 })
|
@index({ msgId: 1 })
|
||||||
export default class PrivateMessage extends BaseModel {
|
export default class PrivateMessage extends BaseModel {
|
||||||
@prop({ required: true, default: '' })
|
@prop({ required: true, default: '' })
|
||||||
@@ -36,7 +36,7 @@ export default class PrivateMessage extends BaseModel {
|
|||||||
content: string; // 消息内容
|
content: string; // 消息内容
|
||||||
|
|
||||||
public static async createMsg(data: PrivateMessageParam) {
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +44,11 @@ export default class PrivateMessage extends BaseModel {
|
|||||||
const result = await PrivateMessageModel.findOne(query).lean();
|
const result = await PrivateMessageModel.findOne(query).lean();
|
||||||
return result;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user