refactor: message id

This commit is contained in:
手瓜一十雪 2024-07-23 15:10:39 +08:00
parent 8db294efe6
commit 2d76bcf0cf
3 changed files with 33 additions and 24 deletions

View File

@ -1,5 +1,6 @@
import { Peer } from '@/core';
import crypto, { randomInt, randomUUID } from 'crypto';
import { logError } from './log';
class LimitedHashTable<K, V> {
private keyToValue: Map<K, V> = new Map();
@ -75,7 +76,7 @@ class MessageUniqueWrapper {
const hash = crypto.createHash('sha1').update(key);
const shortId = parseInt(hash.digest('hex').slice(0, 8), 16);
const isExist = this.msgIdMap.getKey(shortId);
console.log(`${peer.peerUid} ${msgId} ------- ${shortId}`);
//console.log(`${peer.peerUid} ${msgId} ------- ${shortId}`);
if (isExist && isExist === msgId) {
return shortId;
}

View File

@ -8,30 +8,39 @@ import { GeneralCallResult } from '@/core/services/common';
import { randomUUID } from 'crypto';
import { MessageUnique } from '../../../common/utils/MessageUnique';
import { NTEventDispatch } from '@/common/utils/EventTask';
async function LoadMessageIdList(Peer: Peer, msgId: string) {
let msgList = await NTQQMsgApi.getMsgHistory(Peer, msgId, 50);
for (let j = 0; j < msgList.msgList.length; j++) {
let shortId = MessageUnique.createMsg(Peer, msgList.msgList[j].msgId);
//console.log(`开始创建 ${shortId}<------>${msgList.msgList[j].msgId}`)
}
}
async function loadMessageUnique() {
if (groups.size > 100) {
logWarn('群数量大于100可能会导致性能问题');
}
let predict = (groups.size + friends.size) / 5;
predict = predict < 20 ? 20 : predict;
predict = predict > 50 ? 50 : predict;
//let waitpromise: Array<Promise<{ msgList: RawMessage[]; }>> = [];
MessageUnique.resize(predict * 50);
let RecentContact = await NTQQUserApi.getRecentContactListSnapShot(predict);
let LoadMessageIdDo: Array<Promise<void>> = new Array<Promise<void>>();
if (RecentContact?.info?.changedList && RecentContact?.info?.changedList?.length > 0) {
for (let i = 0; i < RecentContact.info.changedList.length; i++) {
let Peer: Peer = { chatType: RecentContact.info.changedList[i].chatType, peerUid: RecentContact.info.changedList[i].peerUid, guildId: '' };
LoadMessageIdDo.push(LoadMessageIdList(Peer, RecentContact.info.changedList[i].msgId));
}
}
await Promise.all(LoadMessageIdDo).then(() => {
log('消息列表加载完成');
});
}
setTimeout(() => {
napCatCore.onLoginSuccess(() => {
setTimeout(async () => {
if (groups.size > 100) {
logWarn('群数量大于100可能会导致性能问题');
}
let predict = (groups.size + friends.size) / 5;
predict = predict < 20 ? 20 : predict;
predict = predict > 50 ? 50 : predict;
//let waitpromise: Array<Promise<{ msgList: RawMessage[]; }>> = [];
MessageUnique.resize(predict * 50);
let RecentContact = await NTQQUserApi.getRecentContactListSnapShot(predict);
if (RecentContact?.info?.changedList && RecentContact?.info?.changedList?.length > 0) {
for (let i = 0; i < RecentContact.info.changedList.length; i++) {
let Peer: Peer = { chatType: RecentContact.info.changedList[i].chatType, peerUid: RecentContact.info.changedList[i].peerUid, guildId: '' };
let msgList = await NTQQMsgApi.getMsgHistory(Peer, RecentContact.info.changedList[i].msgId, 50);
for (let j = 0; j < msgList.msgList.length; j++) {
let shortId = MessageUnique.createMsg(Peer, msgList.msgList[j].msgId);
//console.log(`开始创建 ${shortId}<------>${msgList.msgList[j].msgId}`)
}
}
}
}, 500);
napCatCore.onLoginSuccess(async () => {
await sleep(100);
loadMessageUnique().then().catch();
});
}, 100);

View File

@ -117,7 +117,6 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag
for (const msgId of nodeMsgIds) {
const nodeMsgPeer = await MessageUnique.getPeerByMsgId(msgId);
const nodeMsg = (await NTQQMsgApi.getMsgsByMsgId(nodeMsgPeer?.Peer!, [msgId])).msgList[0];
console.log("4545",nodeMsgPeer);
if (nodeMsg) {
nodeMsgArray.push(nodeMsg);
if (!srcPeer) {