refactor: msgId stage-2

This commit is contained in:
手瓜一十雪 2024-07-22 11:34:18 +08:00
parent 4f9fb2c8c3
commit 087c76b394
9 changed files with 22 additions and 31 deletions

View File

@ -57,9 +57,11 @@ class LimitedHashTable<K, V> {
} }
class MessageUniqueWrapper { class MessageUniqueWrapper {
private msgDataMap: LimitedHashTable<string, number>;
private msgIdMap: LimitedHashTable<string, number>; private msgIdMap: LimitedHashTable<string, number>;
constructor(maxMap: number = 1000) { constructor(maxMap: number = 1000) {
this.msgIdMap = new LimitedHashTable<string, number>(maxMap); this.msgIdMap = new LimitedHashTable<string, number>(maxMap);
this.msgDataMap = new LimitedHashTable<string, number>(maxMap);
} }
createMsg(peer: Peer, msgId: string): number | undefined { createMsg(peer: Peer, msgId: string): number | undefined {
@ -70,8 +72,8 @@ class MessageUniqueWrapper {
if (isExist && isExist === msgId) { if (isExist && isExist === msgId) {
return undefined; return undefined;
} }
this.msgIdMap.set(msgId, shortId);
this.msgIdMap.set(key, shortId); this.msgDataMap.set(key, shortId);
return shortId; return shortId;
} }
@ -92,6 +94,11 @@ class MessageUniqueWrapper {
getShortIdByMsgId(msgId: string): number | undefined { getShortIdByMsgId(msgId: string): number | undefined {
return this.msgIdMap.getValue(msgId); 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); export const MessageUnique = new MessageUniqueWrapper(1000);

View File

@ -27,16 +27,6 @@ program
//deleteOldFiles(path.join(__dirname, 'logs'), 3).then().catch(); //deleteOldFiles(path.join(__dirname, 'logs'), 3).then().catch();
// UpdateConfig().catch(logError); 移除支持 // UpdateConfig().catch(logError); 移除支持
// 启动WebUi // 启动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(); InitWebUi();
const cmdOptions = program.opts(); const cmdOptions = program.opts();
// console.log(process.argv); // console.log(process.argv);

View File

@ -1,11 +1,9 @@
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { OB11ForwardMessage, OB11Message, OB11MessageData } from '../../types'; import { OB11ForwardMessage, OB11Message, OB11MessageData } from '../../types';
import { NTQQMsgApi } from '@/core/apis'; import { NTQQMsgApi } from '@/core/apis';
import { dbUtil } from '@/common/utils/db';
import { OB11Constructor } from '../../constructor'; import { OB11Constructor } from '../../constructor';
import { ActionName, BaseCheckResult } from '../types'; import { ActionName, BaseCheckResult } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import Ajv from 'ajv';
import { MessageUnique } from '@/common/utils/MessageUnique'; import { MessageUnique } from '@/common/utils/MessageUnique';
const SchemaData = { const SchemaData = {
@ -42,7 +40,7 @@ export class GoCQHTTPGetForwardMsgAction extends BaseAction<Payload, any> {
const msgList = data.msgList; const msgList = data.msgList;
const messages = await Promise.all(msgList.map(async msg => { const messages = await Promise.all(msgList.map(async msg => {
const resMsg = await OB11Constructor.message(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; return resMsg;
})); }));
messages.map(msg => { messages.map(msg => {

View File

@ -2,7 +2,6 @@ import { OB11Message } from '../../types';
import { OB11Constructor } from '../../constructor'; import { OB11Constructor } from '../../constructor';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { dbUtil } from '@/common/utils/db';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { MessageUnique } from '@/common/utils/MessageUnique'; import { MessageUnique } from '@/common/utils/MessageUnique';
import { NTQQMsgApi } from '@/core'; import { NTQQMsgApi } from '@/core';

View File

@ -17,6 +17,7 @@ import { uri2local } from '@/common/utils/file';
import { ob11Config } from '@/onebot11/config'; import { ob11Config } from '@/onebot11/config';
import { RequestUtil } from '@/common/utils/request'; import { RequestUtil } from '@/common/utils/request';
import fs from 'node:fs'; import fs from 'node:fs';
import { MessageUnique } from '@/common/utils/MessageUnique';
export type MessageContext = { export type MessageContext = {
group?: Group, group?: Group,
@ -36,10 +37,10 @@ async function handleOb11FileLikeMessage(
} else if (cache.url) { } else if (cache.url) {
uri = cache.url; uri = cache.url;
} else { } else {
const fileMsg = await dbUtil.getMsgByLongId(cache.msgId); const fileMsgPeer = MessageUnique.getPeerByMsgId(cache.msgId);
if (fileMsg) { if (fileMsgPeer) {
cache.path = await NTQQFileApi.downloadMedia( cache.path = await NTQQFileApi.downloadMedia(
fileMsg.msgId, fileMsg.chatType, fileMsg.peerUid, fileMsgPeer.MsgId, fileMsgPeer.Peer.chatType, fileMsgPeer.Peer.peerUid,
cache.elementId, '', '' cache.elementId, '', ''
); );
uri = 'file://' + cache.path; uri = 'file://' + cache.path;

View File

@ -1,7 +1,6 @@
import { ChatType, ElementType, Group, NTQQMsgApi, Peer, RawMessage, SendMessageElement } from '@/core'; import { ChatType, ElementType, Group, NTQQMsgApi, Peer, RawMessage, SendMessageElement } from '@/core';
import { OB11MessageNode } from '@/onebot11/types'; import { OB11MessageNode } from '@/onebot11/types';
import { selfInfo } from '@/core/data'; import { selfInfo } from '@/core/data';
import { dbUtil } from '@/common/utils/db';
import createSendElements from '@/onebot11/action/msg/SendMsg/create-send-elements'; import createSendElements from '@/onebot11/action/msg/SendMsg/create-send-elements';
import { logDebug, logError } from '@/common/utils/log'; import { logDebug, logError } from '@/common/utils/log';
import { sleep } from '@/common/utils/helper'; import { sleep } from '@/common/utils/helper';
@ -115,7 +114,8 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag
let srcPeer: Peer | undefined = undefined; let srcPeer: Peer | undefined = undefined;
let needSendSelf = false; let needSendSelf = false;
for (const msgId of nodeMsgIds) { 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) { if (nodeMsg) {
nodeMsgArray.push(nodeMsg); nodeMsgArray.push(nodeMsg);
if (!srcPeer) { if (!srcPeer) {

View File

@ -27,7 +27,6 @@ import {
} from '@/core/entities'; } from '@/core/entities';
import { EventType } from './event/OB11BaseEvent'; import { EventType } from './event/OB11BaseEvent';
import { encodeCQCode } from './cqcode'; import { encodeCQCode } from './cqcode';
import { dbUtil } from '@/common/utils/db';
import { OB11GroupIncreaseEvent } from './event/notice/OB11GroupIncreaseEvent'; import { OB11GroupIncreaseEvent } from './event/notice/OB11GroupIncreaseEvent';
import { OB11GroupBanEvent } from './event/notice/OB11GroupBanEvent'; import { OB11GroupBanEvent } from './event/notice/OB11GroupBanEvent';
import { OB11GroupUploadNoticeEvent } from './event/notice/OB11GroupUploadNoticeEvent'; import { OB11GroupUploadNoticeEvent } from './event/notice/OB11GroupUploadNoticeEvent';
@ -150,10 +149,10 @@ export class OB11Constructor {
// true // true
// ); // );
// console.log(JSON.stringify(retData, null, 2)); // 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) // log("找到回复消息", replyMsg.msgShortId, replyMsg.msgId)
if (replyMsg && replyMsg.id) { if (replyMsg) {
message_data['data']['id'] = replyMsg.id!.toString(); message_data['data']['id'] = MessageUnique.createMsg({ peerUid: msg.peerUid, guildId: '', chatType: msg.chatType }, replyMsg.msgList[0].msgId);
} }
else { else {
continue; continue;
@ -409,11 +408,11 @@ export class OB11Constructor {
const senderUin = emojiLikeData.gtip.qq.jp; const senderUin = emojiLikeData.gtip.qq.jp;
const msgSeq = emojiLikeData.gtip.url.msgseq; const msgSeq = emojiLikeData.gtip.url.msgseq;
const emojiId = emojiLikeData.gtip.face.id; const emojiId = emojiLikeData.gtip.face.id;
let msgList = (await NTQQMsgApi.getMsgsBySeqAndCount({ chatType: ChatType.group, guildId: '', peerUid: msg.peerUid }, msgSeq, 1, true, true)).msgList; let replyMsgList = (await NTQQMsgApi.getMsgsBySeqAndCount({ chatType: ChatType.group, guildId: '', peerUid: msg.peerUid }, msgSeq, 1, true, true)).msgList;
const replyMsg = await dbUtil.getMsgBySeq(msg.peerUid, msgSeq); if (replyMsgList.length < 1) {
if (msgList.length < 1) {
return; return;
} }
let replyMsg = replyMsgList[0];
return new OB11GroupMsgEmojiLikeEvent(parseInt(msg.peerUid), parseInt(senderUin), MessageUnique.getShortIdByMsgId(replyMsg?.msgId!)!, [{ return new OB11GroupMsgEmojiLikeEvent(parseInt(msg.peerUid), parseInt(senderUin), MessageUnique.getShortIdByMsgId(replyMsg?.msgId!)!, [{
emoji_id: emojiId, emoji_id: emojiId,
count: 1 count: 1

View File

@ -1,6 +1,4 @@
import { OB11GroupNoticeEvent } from './OB11GroupNoticeEvent'; import { OB11GroupNoticeEvent } from './OB11GroupNoticeEvent';
import { dbUtil } from '@/common/utils/db';
import { ob11Config } from '@/onebot11/config';
type GroupIncreaseSubType = 'approve' | 'invite'; type GroupIncreaseSubType = 'approve' | 'invite';
export class OB11GroupIncreaseEvent extends OB11GroupNoticeEvent { export class OB11GroupIncreaseEvent extends OB11GroupNoticeEvent {

View File

@ -20,7 +20,6 @@ import { httpHeart, ob11HTTPServer } from '@/onebot11/server/http';
import { ob11WebsocketServer } from '@/onebot11/server/ws/WebsocketServer'; import { ob11WebsocketServer } from '@/onebot11/server/ws/WebsocketServer';
import { ob11ReverseWebsockets } from '@/onebot11/server/ws/ReverseWebsocket'; import { ob11ReverseWebsockets } from '@/onebot11/server/ws/ReverseWebsocket';
import { getGroup, getGroupMember, groupMembers, selfInfo, tempGroupCodeMap } from '@/core/data'; import { getGroup, getGroupMember, groupMembers, selfInfo, tempGroupCodeMap } from '@/core/data';
import { dbUtil } from '@/common/utils/db';
import { BuddyListener, GroupListener, NodeIKernelBuddyListener } from '@/core/listeners'; import { BuddyListener, GroupListener, NodeIKernelBuddyListener } from '@/core/listeners';
import { OB11FriendRequestEvent } from '@/onebot11/event/request/OB11FriendRequest'; import { OB11FriendRequestEvent } from '@/onebot11/event/request/OB11FriendRequest';
import { NTQQGroupApi, NTQQUserApi, SignMusicWrapper } from '@/core/apis'; import { NTQQGroupApi, NTQQUserApi, SignMusicWrapper } from '@/core/apis';