From 087c76b39404438c245bd57cb386c32b0e0ea9e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 22 Jul 2024 11:34:18 +0800 Subject: [PATCH] refactor: msgId stage-2 --- src/common/utils/MessageUnique.ts | 11 +++++++++-- src/index.ts | 10 ---------- src/onebot11/action/go-cqhttp/GetForwardMsg.ts | 4 +--- src/onebot11/action/msg/GetMsg.ts | 1 - .../action/msg/SendMsg/create-send-elements.ts | 7 ++++--- .../action/msg/SendMsg/handle-forward-node.ts | 4 ++-- src/onebot11/constructor.ts | 13 ++++++------- src/onebot11/event/notice/OB11GroupIncreaseEvent.ts | 2 -- src/onebot11/main.ts | 1 - 9 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/common/utils/MessageUnique.ts b/src/common/utils/MessageUnique.ts index 5a1ff3aa..63eafa66 100644 --- a/src/common/utils/MessageUnique.ts +++ b/src/common/utils/MessageUnique.ts @@ -57,9 +57,11 @@ class LimitedHashTable { } class MessageUniqueWrapper { + private msgDataMap: LimitedHashTable; private msgIdMap: LimitedHashTable; constructor(maxMap: number = 1000) { this.msgIdMap = new LimitedHashTable(maxMap); + this.msgDataMap = new LimitedHashTable(maxMap); } createMsg(peer: Peer, msgId: string): number | undefined { @@ -70,8 +72,8 @@ class MessageUniqueWrapper { if (isExist && isExist === msgId) { return undefined; } - - this.msgIdMap.set(key, shortId); + this.msgIdMap.set(msgId, shortId); + this.msgDataMap.set(key, shortId); return shortId; } @@ -92,6 +94,11 @@ class MessageUniqueWrapper { getShortIdByMsgId(msgId: string): number | undefined { return this.msgIdMap.getValue(msgId); } + getPeerByMsgId(msgId: string) { + const shortId = this.msgIdMap.getValue(msgId); + if (!shortId) return undefined; + return this.getMsgIdAndPeerByShortId(shortId); + } } export const MessageUnique = new MessageUniqueWrapper(1000); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index be79644a..b9bd9ae8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,16 +27,6 @@ program //deleteOldFiles(path.join(__dirname, 'logs'), 3).then().catch(); // UpdateConfig().catch(logError); 移除支持 // 启动WebUi - -for (let i = 0; i < 100; i++) { - console.log('---------------'); - let msgid = randomInt(1000000000).toString(); - let shortId = MessageUnique.createMsg({ chatType: 1, peerUid: '123' }, msgid); - console.log(`${msgid}--->${shortId}`); - msgid = MessageUnique.getMsgIdAndPeerByShortId(shortId!)?.MsgId!; - console.log(`${msgid}<---${shortId}`); -} - InitWebUi(); const cmdOptions = program.opts(); // console.log(process.argv); diff --git a/src/onebot11/action/go-cqhttp/GetForwardMsg.ts b/src/onebot11/action/go-cqhttp/GetForwardMsg.ts index c9ecd139..e9635624 100644 --- a/src/onebot11/action/go-cqhttp/GetForwardMsg.ts +++ b/src/onebot11/action/go-cqhttp/GetForwardMsg.ts @@ -1,11 +1,9 @@ import BaseAction from '../BaseAction'; import { OB11ForwardMessage, OB11Message, OB11MessageData } from '../../types'; import { NTQQMsgApi } from '@/core/apis'; -import { dbUtil } from '@/common/utils/db'; import { OB11Constructor } from '../../constructor'; import { ActionName, BaseCheckResult } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import Ajv from 'ajv'; import { MessageUnique } from '@/common/utils/MessageUnique'; const SchemaData = { @@ -42,7 +40,7 @@ export class GoCQHTTPGetForwardMsgAction extends BaseAction { const msgList = data.msgList; const messages = await Promise.all(msgList.map(async msg => { const resMsg = await OB11Constructor.message(msg); - resMsg.message_id = await dbUtil.addMsg(msg); + resMsg.message_id = await MessageUnique.createMsg({guildId:'',chatType:msg.chatType,peerUid:msg.peerUid},msg.msgId)!; return resMsg; })); messages.map(msg => { diff --git a/src/onebot11/action/msg/GetMsg.ts b/src/onebot11/action/msg/GetMsg.ts index 86c8bd65..58abde4d 100644 --- a/src/onebot11/action/msg/GetMsg.ts +++ b/src/onebot11/action/msg/GetMsg.ts @@ -2,7 +2,6 @@ import { OB11Message } from '../../types'; import { OB11Constructor } from '../../constructor'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { dbUtil } from '@/common/utils/db'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { MessageUnique } from '@/common/utils/MessageUnique'; import { NTQQMsgApi } from '@/core'; diff --git a/src/onebot11/action/msg/SendMsg/create-send-elements.ts b/src/onebot11/action/msg/SendMsg/create-send-elements.ts index b5dd63d5..80081f31 100644 --- a/src/onebot11/action/msg/SendMsg/create-send-elements.ts +++ b/src/onebot11/action/msg/SendMsg/create-send-elements.ts @@ -17,6 +17,7 @@ import { uri2local } from '@/common/utils/file'; import { ob11Config } from '@/onebot11/config'; import { RequestUtil } from '@/common/utils/request'; import fs from 'node:fs'; +import { MessageUnique } from '@/common/utils/MessageUnique'; export type MessageContext = { group?: Group, @@ -36,10 +37,10 @@ async function handleOb11FileLikeMessage( } else if (cache.url) { uri = cache.url; } else { - const fileMsg = await dbUtil.getMsgByLongId(cache.msgId); - if (fileMsg) { + const fileMsgPeer = MessageUnique.getPeerByMsgId(cache.msgId); + if (fileMsgPeer) { cache.path = await NTQQFileApi.downloadMedia( - fileMsg.msgId, fileMsg.chatType, fileMsg.peerUid, + fileMsgPeer.MsgId, fileMsgPeer.Peer.chatType, fileMsgPeer.Peer.peerUid, cache.elementId, '', '' ); uri = 'file://' + cache.path; diff --git a/src/onebot11/action/msg/SendMsg/handle-forward-node.ts b/src/onebot11/action/msg/SendMsg/handle-forward-node.ts index 20d7178b..214aa470 100644 --- a/src/onebot11/action/msg/SendMsg/handle-forward-node.ts +++ b/src/onebot11/action/msg/SendMsg/handle-forward-node.ts @@ -1,7 +1,6 @@ import { ChatType, ElementType, Group, NTQQMsgApi, Peer, RawMessage, SendMessageElement } from '@/core'; import { OB11MessageNode } from '@/onebot11/types'; import { selfInfo } from '@/core/data'; -import { dbUtil } from '@/common/utils/db'; import createSendElements from '@/onebot11/action/msg/SendMsg/create-send-elements'; import { logDebug, logError } from '@/common/utils/log'; import { sleep } from '@/common/utils/helper'; @@ -115,7 +114,8 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag let srcPeer: Peer | undefined = undefined; let needSendSelf = false; for (const msgId of nodeMsgIds) { - const nodeMsg = await dbUtil.getMsgByLongId(msgId); + const nodeMsgPeer = await MessageUnique.getPeerByMsgId(msgId); + let nodeMsg = (await NTQQMsgApi.getMsgsByMsgId(nodeMsgPeer?.Peer!, [msgId])).msgList[0]; if (nodeMsg) { nodeMsgArray.push(nodeMsg); if (!srcPeer) { diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index 10e47410..03e8f884 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -27,7 +27,6 @@ import { } from '@/core/entities'; import { EventType } from './event/OB11BaseEvent'; import { encodeCQCode } from './cqcode'; -import { dbUtil } from '@/common/utils/db'; import { OB11GroupIncreaseEvent } from './event/notice/OB11GroupIncreaseEvent'; import { OB11GroupBanEvent } from './event/notice/OB11GroupBanEvent'; import { OB11GroupUploadNoticeEvent } from './event/notice/OB11GroupUploadNoticeEvent'; @@ -150,10 +149,10 @@ export class OB11Constructor { // true // ); // console.log(JSON.stringify(retData, null, 2)); - const replyMsg = await dbUtil.getMsgBySeq(msg.peerUid, element.replyElement.replayMsgSeq); + const replyMsg = await NTQQMsgApi.getMsgsBySeqAndCount({ peerUid: msg.peerUid, guildId: '', chatType: msg.chatType }, element.replyElement.replayMsgSeq, 1, true, true); // log("找到回复消息", replyMsg.msgShortId, replyMsg.msgId) - if (replyMsg && replyMsg.id) { - message_data['data']['id'] = replyMsg.id!.toString(); + if (replyMsg) { + message_data['data']['id'] = MessageUnique.createMsg({ peerUid: msg.peerUid, guildId: '', chatType: msg.chatType }, replyMsg.msgList[0].msgId); } else { continue; @@ -409,11 +408,11 @@ export class OB11Constructor { const senderUin = emojiLikeData.gtip.qq.jp; const msgSeq = emojiLikeData.gtip.url.msgseq; const emojiId = emojiLikeData.gtip.face.id; - let msgList = (await NTQQMsgApi.getMsgsBySeqAndCount({ chatType: ChatType.group, guildId: '', peerUid: msg.peerUid }, msgSeq, 1, true, true)).msgList; - const replyMsg = await dbUtil.getMsgBySeq(msg.peerUid, msgSeq); - if (msgList.length < 1) { + let replyMsgList = (await NTQQMsgApi.getMsgsBySeqAndCount({ chatType: ChatType.group, guildId: '', peerUid: msg.peerUid }, msgSeq, 1, true, true)).msgList; + if (replyMsgList.length < 1) { return; } + let replyMsg = replyMsgList[0]; return new OB11GroupMsgEmojiLikeEvent(parseInt(msg.peerUid), parseInt(senderUin), MessageUnique.getShortIdByMsgId(replyMsg?.msgId!)!, [{ emoji_id: emojiId, count: 1 diff --git a/src/onebot11/event/notice/OB11GroupIncreaseEvent.ts b/src/onebot11/event/notice/OB11GroupIncreaseEvent.ts index 0348109a..b0a86880 100644 --- a/src/onebot11/event/notice/OB11GroupIncreaseEvent.ts +++ b/src/onebot11/event/notice/OB11GroupIncreaseEvent.ts @@ -1,6 +1,4 @@ import { OB11GroupNoticeEvent } from './OB11GroupNoticeEvent'; -import { dbUtil } from '@/common/utils/db'; -import { ob11Config } from '@/onebot11/config'; type GroupIncreaseSubType = 'approve' | 'invite'; export class OB11GroupIncreaseEvent extends OB11GroupNoticeEvent { diff --git a/src/onebot11/main.ts b/src/onebot11/main.ts index 9ba336a0..ca07b2c8 100644 --- a/src/onebot11/main.ts +++ b/src/onebot11/main.ts @@ -20,7 +20,6 @@ import { httpHeart, ob11HTTPServer } from '@/onebot11/server/http'; import { ob11WebsocketServer } from '@/onebot11/server/ws/WebsocketServer'; import { ob11ReverseWebsockets } from '@/onebot11/server/ws/ReverseWebsocket'; import { getGroup, getGroupMember, groupMembers, selfInfo, tempGroupCodeMap } from '@/core/data'; -import { dbUtil } from '@/common/utils/db'; import { BuddyListener, GroupListener, NodeIKernelBuddyListener } from '@/core/listeners'; import { OB11FriendRequestEvent } from '@/onebot11/event/request/OB11FriendRequest'; import { NTQQGroupApi, NTQQUserApi, SignMusicWrapper } from '@/core/apis';