聊天:获取私聊历史消息

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
+28 -4
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;
}