聊天:精简未读消息数的处理逻辑
This commit is contained in:
@@ -82,8 +82,8 @@ async function createGroupMsgData(roleId: string, roleName: string, channel: str
|
||||
|
||||
function createChatRec(sender: boolean, curTime: Date, targetRoleId) {
|
||||
const result: PrivateChatRec = {
|
||||
// 接收者没有阅读时间
|
||||
targetRoleId, lastChatTime: curTime, lastReadTime: sender ? curTime : null
|
||||
// 接收者没有阅读时间,发送者未读数为 0
|
||||
targetRoleId, lastChatTime: curTime, lastReadTime: sender ? curTime : null, unreadCnt: sender ? 0 : 1
|
||||
};
|
||||
return result;
|
||||
}
|
||||
@@ -95,8 +95,10 @@ async function updateRecentChats(sender: boolean, roleId: string, targetRoleId:
|
||||
recentPrivateChats.push(createChatRec(sender, curTime, targetRoleId));
|
||||
} else {
|
||||
recentPrivateChats[recIdx].lastChatTime = curTime;
|
||||
// 发送者的最后阅读时间也更新
|
||||
recentPrivateChats[recIdx].lastReadTime = sender ? curTime : recentPrivateChats[recIdx].lastReadTime;
|
||||
const { lastReadTime, unreadCnt } = recentPrivateChats[recIdx];
|
||||
// 发送者的最后阅读时间也更新,接收者的未读消息数增加
|
||||
recentPrivateChats[recIdx].lastReadTime = sender ? curTime : lastReadTime;
|
||||
recentPrivateChats[recIdx].unreadCnt = sender ? 0 : unreadCnt + 1;
|
||||
}
|
||||
await ChatInfoModel.updateInfo({ roleId, recentPrivateChats});
|
||||
}
|
||||
@@ -353,19 +355,11 @@ export async function recentPrivateChatInfos(roleId: string, roleName: string) {
|
||||
.splice(RECENT_PRIVATE_CHATS_CNT);
|
||||
if (!recentPrivateChats || !recentPrivateChats.length) return null;
|
||||
|
||||
const unreadChats = recentPrivateChats.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', recentPrivateChats.map(chat => { return chat.targetRoleId }));
|
||||
const chatInfos = recentPrivateChats.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 { ...chat, quitTime, targetRoleName, title, guildName, headHid, sHid, lv };
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -397,9 +391,9 @@ function shouldPushTowerMsg(lv: number) {
|
||||
return lv >= 100 && lv % 50 === 0;
|
||||
}
|
||||
|
||||
export async function updatePrivateMsgReadTime(roleId: string, targetRoleId: string) {
|
||||
export async function updatePrivateMsgReadInfo(roleId: string, targetRoleId: string) {
|
||||
const time = new Date();
|
||||
const chatInfo = await ChatInfoModel.updateReadTime(roleId, targetRoleId, time);
|
||||
const chatInfo = await ChatInfoModel.updateReadInfo(roleId, targetRoleId, time);
|
||||
if (!chatInfo || !chatInfo.recentPrivateChats) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user