聊天:最近私聊历史相关逻辑

This commit is contained in:
liangtongchuan
2021-03-10 12:49:12 +08:00
parent 253efb4966
commit 41025cacf9
5 changed files with 75 additions and 8 deletions
+2
View File
@@ -1,4 +1,6 @@
export const MSG_CODE_LEN = 8;
export const RECENT_PRIVATE_CHATS_CNT = 20;
export const MAX_PRIVATE_MSGS = 99;
export const MSG_STATUS = {
NORMAL: 0,
+21 -6
View File
@@ -1,6 +1,12 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
class PrivateChatRec {
targetRoleId: string; // 聊天玩家 roleId
lastReadTime: Date; // 最后查看时间,用来检查未读消息数
lastChatTime: Date; // 最后聊天时间
}
/**
* 记录聊天信息,包含配置、最近聊天等
**/
@@ -20,15 +26,24 @@ export default class ChatInfo extends BaseModel {
BSSources: [string]; // 弹幕打开的消息来源
@prop({ required: true, default: '' })
bubbleId: string; // 气泡编号
@prop({ required: true, type: String, default: [] })
recentPrivateChats: [{ // 最近聊天列表
targetRoleId: string; // 聊天玩家 roleId
roomId: string; // 聊天 Id
lastReadTime: Date; // 最后查看时间
}];
@prop({ required: true, type: PrivateChatRec, default: [] })
recentPrivateChats: [PrivateChatRec]; // 最近聊天列表
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();
return result;
}
public static async findInfo(roleId: string) {
let result = await ChatInfoModel.findOne({ roleId }).select('-_id').lean();
return result;
}
}
export const ChatInfoModel = getModelForClass(ChatInfo);
export interface ChatInfoType extends Pick<DocumentType<ChatInfo>, keyof ChatInfo> {};
export type ChatInfoParam = Partial<ChatInfoType>;
+10
View File
@@ -51,6 +51,16 @@ export default class PrivateMessage extends BaseModel {
const result = await PrivateMessageModel.find({ roomId, seqId: { $lt: fromSeqId } }).sort({ seqId: -1 }).limit(count).lean();
return result;
}
public static async getMsgsByTime(roomId: string, time: Date, count: number) {
const result = await PrivateMessageModel.find({ roomId, createdAt: { $gt: time } }).sort({ seqId: -1 }).limit(count).lean();
return result;
}
public static async getMsgByRooms(roomIds: string[], count: number) {
const result = await PrivateMessageModel.find({ roomId: { $in: roomIds } }).sort({ seqId: -1 }).limit(count).lean();
return result;
}
}