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

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
+36 -1
View File
@@ -9,12 +9,13 @@ import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './
import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage';
import { genCode, resResult } from '../pubUtils/util';
import { pinus } from 'pinus';
import { CHANNEL_PREFIX, MSG_CODE_LEN, MSG_SOURCE, MSG_STATUS, MSG_TYPE, ON_GROUP_MSG_ROUTE, ON_MSG_ROUTE, RICH_TEXT_TABLE } from '../consts';
import { CHANNEL_PREFIX, MSG_CODE_LEN, MSG_SOURCE, MSG_STATUS, MSG_TYPE, ON_GROUP_MSG_ROUTE, ON_MSG_ROUTE, RICH_TEXT_TABLE, RECENT_PRIVATE_CHATS_CNT, MAX_PRIVATE_MSGS } from '../consts';
import { addRedisChannel, getRoleOnlineInfo, redisChannelServer } from './redisService';
import { crc32 } from 'crc';
import { HeroType } from '../db/Hero';
import { GuildType } from '../db/Guild';
import { GuildTrainType } from '../db/GuildTrain';
import { ChatInfoModel } from '../db/ChatInfo';
/**
* @description 生成私聊房间号
@@ -272,3 +273,37 @@ export async function pushGuildBossSucMsg(roleId: string, roleName: string, guil
await pushGroupMsgToRoom(groupRoomId(CHANNEL_PREFIX.GUILD, guildCode), msgData);
return msgData;
}
export async function roleChatInfos(roleId: string, roleName: string) {
let result = await ChatInfoModel.findInfo(roleId);
if (!result) {
result = await ChatInfoModel.createInfo({ roleId, roleName });
}
return result;
}
export async function recentPrivateChatInfos(roleId: string, roleName: string) {
const { recentPrivateChats } = await roleChatInfos(roleId, roleName);
const sortedChats = recentPrivateChats
.sort((a, b) => a.lastChatTime.getTime() - b.lastChatTime.getTime())
.splice(RECENT_PRIVATE_CHATS_CNT);
if (!sortedChats || !sortedChats.length) return null;
const unreadChats = sortedChats.filter(chat => { return chat.lastChatTime > chat.lastReadTime });
let unreadMsgs = {};
for (let { targetRoleId, lastReadTime } of unreadChats) {
const msgs = await PrivateMessageModel.getMsgsByTime(privateRoomId(roleId, targetRoleId), lastReadTime, MAX_PRIVATE_MSGS);
unreadMsgs[targetRoleId] = msgs;
}
const roleInfos = await RoleModel.findRoleByField('roleId', sortedChats.map(chat => { return chat.targetRoleId }));
const chatInfos = sortedChats.map( chat => {
for (let { roleId: targetRoleId, quitTime, roleName: targetRoleName, title, guildName, headHid, sHid, lv } of roleInfos) {
if (targetRoleId === chat.targetRoleId) {
const unreadCnt = unreadMsgs[targetRoleId] ? unreadMsgs[targetRoleId].length : 0;
return { ...chat, quitTime, targetRoleName, title, guildName, headHid, sHid, lv, unreadCnt };
}
}
});
return chatInfos;
}