聊天:查看私聊消息的接口和测试

This commit is contained in:
liangtongchuan
2021-03-11 10:24:10 +08:00
parent 9a2c536288
commit cce6a5af5e
5 changed files with 55 additions and 5 deletions

View File

@@ -2,7 +2,7 @@ import { CHANNEL_PREFIX, MSG_SOURCE } from './../../../consts/constModules/chatC
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';
import { createGroupMsg, createPrivateMsg, getPrivateMessages, groupRoomId, pushGroupMsgToRoom, pushMsgToRole, updatePrivateMsgReadTime } from '../../../services/chatService';
export default function(app: Application) {
@@ -125,4 +125,21 @@ export class ChatHandler {
const msgs = await getPrivateMessages(roleId, targetRoleId, fromSeqId, count);
return resResult(STATUS.SUCCESS, { msgs });
}
/**
* @description 查看私聊,主要用于更新最后查看时间和未读消息数
* @param {{ targetRoleId: string }} msg
* @param {BackendSession} session
* @returns
* @memberof ChatHandler
*/
async readPrivateMessage(msg: { targetRoleId: string }, session: BackendSession) {
const roleId = session.get('roleId');
const { targetRoleId } = msg;
const result = await updatePrivateMsgReadTime(roleId, targetRoleId);
if (!result) {
return resResult(STATUS.UPDATE_PRIVATE_MSG_READ_TIME_ERR);
}
return resResult(STATUS.SUCCESS, result);
}
}

View File

@@ -396,3 +396,13 @@ function shouldPushTowerMsg(lv: number) {
// 100 层之后每 50 层触发
return lv >= 100 && lv % 50 === 0;
}
export async function updatePrivateMsgReadTime(roleId: string, targetRoleId: string) {
const time = new Date();
const chatInfo = await ChatInfoModel.updateReadTime(roleId, targetRoleId, time);
if (!chatInfo || !chatInfo.recentPrivateChats) {
return null;
}
const chatRec = chatInfo.recentPrivateChats.find(rec => { return rec.targetRoleId === targetRoleId });
return chatRec;
}

View File

@@ -348,4 +348,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);
expect(res.data.content).to.equal(TEXT_MSG.content);
});
}
setTimeout(() => {
pinusClientT.request('chat.chatHandler.readPrivateMessage', {targetRoleId: roleInfo.roleId}, (res) => {
checkSuccessResponse(res);
expect(res.data).to.be.an('object');
expect(res.data.targetRoleId).to.be.equal(roleInfo.roleId);
expect(Date.parse(res.data.lastReadTime)).to.be.above(Date.parse(res.data.lastChatTime));
done();
})
}, 1000);
});
});

View File

@@ -272,6 +272,7 @@ export const STATUS = {
// 社交相关状态 40000 - 49999
SYS_CHANNEL_AUTH_NOT_ENOUGH: {code: 40000, simStr: '无法在系统频道发送消息'},
UPDATE_PRIVATE_MSG_READ_TIME_ERR: {code: 40001, simStr: '更新私聊阅读时间失败'},
// 运营模块相关状态 50000 - 59999
// GM后台相关状态 60000 - 69999
GM_ERR_PASSWORD: { code: 60001, simStr: '账号或密码错误' },

View File

@@ -35,23 +35,27 @@ export default class ChatInfo extends BaseModel {
public static async createInfo(data: ChatInfoParam) {
const roleId = data.roleId!;
const docData = new ChatInfoModel();
const result = await ChatInfoModel.findOneAndUpdate({ roleId }, { ...docData.toJSON(), ...data }, { upsert: true, new: true }).select('-_id').lean();
const result: ChatInfoType = await ChatInfoModel.findOneAndUpdate({ roleId }, { ...docData.toJSON(), ...data }, { upsert: true, new: true }).select('-_id').lean();
return result;
}
public static async findInfo(roleId: string) {
let result = await ChatInfoModel.findOne({ roleId }).select('-_id').lean();
let result: ChatInfoType = await ChatInfoModel.findOne({ roleId }).select('-_id').lean();
return result;
}
public static async updateInfo(data: ChatInfoParam) {
const roleId = data.roleId!;
const result = await ChatInfoModel.findOneAndUpdate({ roleId }, { $set: { ...data } }, { new: true }).select('-_id').lean();
const result: ChatInfoType = await ChatInfoModel.findOneAndUpdate({ roleId }, { $set: { ...data } }, { new: true }).select('-_id').lean();
return result;
}
public static async updateReadTime(roleId: string, targetRoleId: string, time: Date) {
const result: ChatInfoType = await ChatInfoModel.findOneAndUpdate({ roleId, 'recentPrivateChats.targetRoleId': targetRoleId }, { 'recentPrivateChats.$.lastReadTime': time }, { new: true }).select('-_id').lean();
return result;
}
}
export const ChatInfoModel = getModelForClass(ChatInfo);
export interface ChatInfoType extends Pick<DocumentType<ChatInfo>, keyof ChatInfo> {};