From 334e43e764497b16def6b629f81216753d955f48 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:15:01 +0800 Subject: [PATCH] refactor: MsgId --- src/common/utils/MessageUnique.ts | 16 ++++++------ src/index.ts | 12 +++++++++ .../action/go-cqhttp/GetForwardMsg.ts | 14 ++++------- .../action/go-cqhttp/GetFriendMsgHistory.ts | 6 ++--- .../action/go-cqhttp/GetGroupMsgHistory.ts | 9 +++---- src/onebot11/action/group/DelEssenceMsg.ts | 8 +++--- .../action/group/GetGroupMemberList.ts | 17 ++----------- src/onebot11/action/group/SetEssenceMsg.ts | 8 +++--- src/onebot11/action/msg/DeleteMsg.ts | 6 ++--- src/onebot11/action/msg/ForwardSingleMsg.ts | 16 +++++------- src/onebot11/action/msg/GetMsg.ts | 17 +++++++------ .../action/msg/SendMsg/handle-forward-node.ts | 10 +++++--- src/onebot11/action/msg/SendMsg/index.ts | 8 +++--- src/onebot11/action/msg/SetMsgEmojiLike.ts | 10 +++----- src/onebot11/constructor.ts | 8 +++--- .../event/notice/OB11GroupIncreaseEvent.ts | 3 --- src/onebot11/main.ts | 25 +++++++------------ src/onebot11/server/postOB11Event.ts | 1 - 18 files changed, 90 insertions(+), 104 deletions(-) diff --git a/src/common/utils/MessageUnique.ts b/src/common/utils/MessageUnique.ts index eeec8eed..5a1ff3aa 100644 --- a/src/common/utils/MessageUnique.ts +++ b/src/common/utils/MessageUnique.ts @@ -1,5 +1,5 @@ import { Peer } from '@/core'; -import crypto from 'crypto'; +import crypto, { randomInt, randomUUID } from 'crypto'; class LimitedHashTable { private keyToValue: Map = new Map(); @@ -17,14 +17,17 @@ class LimitedHashTable { } this.keyToValue.set(key, value); this.valueToKey.set(value, key); - while (this.keyToValue.size !== this.valueToKey.size){ - console.log('keyToValue.size !== valueToKey.size'); + while (this.keyToValue.size !== this.valueToKey.size) { + console.log('keyToValue.size !== valueToKey.size Error Atom'); } + // console.log('---------------'); + // console.log(this.keyToValue); + // console.log(this.valueToKey); + // console.log('---------------'); while (this.keyToValue.size > this.maxSize || this.valueToKey.size > this.maxSize) { - //删除旧的值 const oldestKey = this.keyToValue.keys().next().value; + this.valueToKey.delete(this.keyToValue.get(oldestKey)!); this.keyToValue.delete(oldestKey); - this.valueToKey.delete(oldestKey); } } @@ -91,5 +94,4 @@ class MessageUniqueWrapper { } } -export const MessageUnique = new MessageUniqueWrapper(); - +export const MessageUnique = new MessageUniqueWrapper(1000); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index de6bc80f..be79644a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,8 @@ import { deleteOldFiles, UpdateConfig } from './common/utils/helper'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; +import { randomInt } from 'crypto'; +import { MessageUnique } from './common/utils/MessageUnique'; const __filename = fileURLToPath(import.meta.url); @@ -25,6 +27,16 @@ 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 3d02db57..c9ecd139 100644 --- a/src/onebot11/action/go-cqhttp/GetForwardMsg.ts +++ b/src/onebot11/action/go-cqhttp/GetForwardMsg.ts @@ -6,6 +6,7 @@ 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 = { type: 'object', @@ -29,17 +30,12 @@ export class GoCQHTTPGetForwardMsgAction extends BaseAction { if (!msgId) { throw Error('message_id is required'); } - let rootMsg = await dbUtil.getMsgByLongId(msgId); + let rootMsgId = MessageUnique.getShortIdByMsgId(msgId); + let rootMsg = MessageUnique.getMsgIdAndPeerByShortId(rootMsgId || parseInt(msgId)) if (!rootMsg) { - rootMsg = await dbUtil.getMsgByShortId(parseInt(msgId)); - if (!rootMsg) { - throw Error('msg not found'); - } + throw Error('msg not found'); } - const data = await NTQQMsgApi.getMultiMsg({ - chatType: rootMsg.chatType, - peerUid: rootMsg.peerUid - }, rootMsg.msgId, rootMsg.msgId); + const data = await NTQQMsgApi.getMultiMsg(rootMsg.Peer, rootMsg.MsgId, rootMsg.MsgId); if (!data || data.result !== 0) { throw Error('找不到相关的聊天记录' + data?.errMsg); } diff --git a/src/onebot11/action/go-cqhttp/GetFriendMsgHistory.ts b/src/onebot11/action/go-cqhttp/GetFriendMsgHistory.ts index a77598b8..8d17ce80 100644 --- a/src/onebot11/action/go-cqhttp/GetFriendMsgHistory.ts +++ b/src/onebot11/action/go-cqhttp/GetFriendMsgHistory.ts @@ -2,11 +2,11 @@ import BaseAction from '../BaseAction'; import { OB11Message, OB11User } from '../../types'; import { ActionName } from '../types'; import { ChatType } from '@/core/entities'; -import { dbUtil } from '@/common/utils/db'; import { NTQQMsgApi } from '@/core/apis/msg'; import { OB11Constructor } from '../../constructor'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { NTQQFriendApi, NTQQUserApi } from '@/core'; +import { MessageUnique } from '@/common/utils/MessageUnique'; interface Response { messages: OB11Message[]; @@ -32,7 +32,7 @@ export default class GetFriendMsgHistory extends BaseAction { if (!uid) { throw `记录${payload.user_id}不存在`; } - const startMsgId = (await dbUtil.getMsgByShortId(payload.message_seq))?.msgId || '0'; + const startMsgId = (await MessageUnique.getMsgIdAndPeerByShortId(payload.message_seq))?.MsgId || '0'; const friend = await NTQQFriendApi.isBuddy(uid); const historyResult = (await NTQQMsgApi.getMsgHistory({ chatType: friend ? ChatType.friend : ChatType.temp, @@ -41,7 +41,7 @@ export default class GetFriendMsgHistory extends BaseAction { //logDebug(historyResult); const msgList = historyResult.msgList; await Promise.all(msgList.map(async msg => { - msg.id = await dbUtil.addMsg(msg); + msg.id = await MessageUnique.createMsg({ guildId: '', chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId); })); const ob11MsgList = await Promise.all(msgList.map(msg => OB11Constructor.message(msg))); return { 'messages': ob11MsgList }; diff --git a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts index 3dfa86c3..e264cd32 100644 --- a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts +++ b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts @@ -3,11 +3,10 @@ import { OB11Message, OB11User } from '../../types'; import { getGroup, groups } from '@/core/data'; import { ActionName } from '../types'; import { ChatType } from '@/core/entities'; -import { dbUtil } from '@/common/utils/db'; import { NTQQMsgApi } from '@/core/apis/msg'; import { OB11Constructor } from '../../constructor'; -import { logDebug } from '@/common/utils/log'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; +import { MessageUnique } from '@/common/utils/MessageUnique'; interface Response { messages: OB11Message[]; } @@ -15,7 +14,7 @@ interface Response { const SchemaData = { type: 'object', properties: { - group_id: { type: [ 'number' , 'string' ] }, + group_id: { type: ['number', 'string'] }, message_seq: { type: 'number' }, count: { type: 'number' } }, @@ -32,7 +31,7 @@ export default class GoCQHTTPGetGroupMsgHistory extends BaseAction { - msg.id = await dbUtil.addMsg(msg); + msg.id = await MessageUnique.createMsg({ guildId: '', chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId); })); const ob11MsgList = await Promise.all(msgList.map(msg => OB11Constructor.message(msg))); return { 'messages': ob11MsgList }; diff --git a/src/onebot11/action/group/DelEssenceMsg.ts b/src/onebot11/action/group/DelEssenceMsg.ts index c8dbb2d9..2b41022d 100644 --- a/src/onebot11/action/group/DelEssenceMsg.ts +++ b/src/onebot11/action/group/DelEssenceMsg.ts @@ -1,9 +1,9 @@ -import { dbUtil } from '@/common/utils/db'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { NTQQGroupApi } from '@/core'; +import { MessageUnique } from '@/common/utils/MessageUnique'; const SchemaData = { type: 'object', @@ -19,13 +19,13 @@ export default class DelEssenceMsg extends BaseAction { actionName = ActionName.DelEssenceMsg; PayloadSchema = SchemaData; protected async _handle(payload: Payload): Promise { - const msg = await dbUtil.getMsgByShortId(parseInt(payload.message_id.toString())); + const msg = await MessageUnique.getMsgIdAndPeerByShortId(parseInt(payload.message_id.toString())); if (!msg) { throw new Error('msg not found'); } return await NTQQGroupApi.removeGroupEssence( - msg.peerUin, - msg.msgId + msg.Peer.peerUid, + msg.MsgId ); } } diff --git a/src/onebot11/action/group/GetGroupMemberList.ts b/src/onebot11/action/group/GetGroupMemberList.ts index 1e1967ca..42b2e7cc 100644 --- a/src/onebot11/action/group/GetGroupMemberList.ts +++ b/src/onebot11/action/group/GetGroupMemberList.ts @@ -3,13 +3,9 @@ import { OB11GroupMember } from '../../types'; import { OB11Constructor } from '../../constructor'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { napCatCore, NTQQGroupApi, NTQQUserApi } from '@/core'; +import { NTQQGroupApi } from '@/core'; import { WebApi } from '@/core/apis/webapi'; -import { logDebug } from '@/common/utils/log'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import { ob11Config } from '@/onebot11/config'; -import { dbUtil } from '@/common/utils/db'; -import { TypeConvert } from '@/common/utils/type'; const SchemaData = { type: 'object', @@ -63,15 +59,6 @@ class GetGroupMemberList extends BaseAction { MemberMap.set(webGroupMembers[i]?.uin, MemberData); } } - } else if (ob11Config.GroupLocalTime.Record && ob11Config.GroupLocalTime.RecordList[0] === '-1' || ob11Config.GroupLocalTime.RecordList.includes(payload.group_id.toString())) { - const _sendAndJoinRember = await dbUtil.getLastSentTimeAndJoinTime(TypeConvert.toNumber(payload.group_id)); - _sendAndJoinRember.forEach((element) => { - const MemberData = MemberMap.get(element.user_id); - if (MemberData) { - MemberData.join_time = element.join_time; - MemberData.last_sent_time = element.last_sent_time; - } - }); } // 还原索引到Array 一同返回 @@ -82,7 +69,7 @@ class GetGroupMemberList extends BaseAction { // } // _groupMembers = Array.from(retData); - + _groupMembers = Array.from(MemberMap.values()); return _groupMembers; } diff --git a/src/onebot11/action/group/SetEssenceMsg.ts b/src/onebot11/action/group/SetEssenceMsg.ts index dc788338..44bd599a 100644 --- a/src/onebot11/action/group/SetEssenceMsg.ts +++ b/src/onebot11/action/group/SetEssenceMsg.ts @@ -1,8 +1,8 @@ -import { dbUtil } from '@/common/utils/db'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { NTQQGroupApi, NTQQMsgApi } from '@/core'; +import { MessageUnique } from '@/common/utils/MessageUnique'; const SchemaData = { type: 'object', @@ -18,13 +18,13 @@ export default class SetEssenceMsg extends BaseAction { actionName = ActionName.SetEssenceMsg; PayloadSchema = SchemaData; protected async _handle(payload: Payload): Promise { - const msg = await dbUtil.getMsgByShortId(parseInt(payload.message_id.toString())); + const msg = await MessageUnique.getMsgIdAndPeerByShortId(parseInt(payload.message_id.toString())); if (!msg) { throw new Error('msg not found'); } return await NTQQGroupApi.addGroupEssence( - msg.peerUin, - msg.msgId + msg.Peer.peerUid, + msg.MsgId ); } } diff --git a/src/onebot11/action/msg/DeleteMsg.ts b/src/onebot11/action/msg/DeleteMsg.ts index 008cffe5..c7bfd237 100644 --- a/src/onebot11/action/msg/DeleteMsg.ts +++ b/src/onebot11/action/msg/DeleteMsg.ts @@ -1,8 +1,8 @@ import { NTQQMsgApi } from '@/core/apis'; import { ActionName } from '../types'; import BaseAction from '../BaseAction'; -import { dbUtil } from '@/common/utils/db'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; +import { MessageUnique } from '@/common/utils/MessageUnique'; const SchemaData = { type: 'object', @@ -23,9 +23,9 @@ class DeleteMsg extends BaseAction { actionName = ActionName.DeleteMsg; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { - const msg = await dbUtil.getMsgByShortId(Number(payload.message_id)); + const msg = await MessageUnique.getMsgIdAndPeerByShortId(Number(payload.message_id)); if (msg) { - await NTQQMsgApi.recallMsg({ peerUid: msg.peerUid, chatType: msg.chatType }, [msg.msgId]); + await NTQQMsgApi.recallMsg(msg.Peer, [msg.MsgId]); } } } diff --git a/src/onebot11/action/msg/ForwardSingleMsg.ts b/src/onebot11/action/msg/ForwardSingleMsg.ts index 7e71fbb3..1c200437 100644 --- a/src/onebot11/action/msg/ForwardSingleMsg.ts +++ b/src/onebot11/action/msg/ForwardSingleMsg.ts @@ -1,16 +1,16 @@ import BaseAction from '../BaseAction'; import { NTQQMsgApi, NTQQUserApi } from '@/core/apis'; import { ChatType, Peer } from '@/core/entities'; -import { dbUtil } from '@/common/utils/db'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; +import { MessageUnique } from '@/common/utils/MessageUnique'; const SchemaData = { type: 'object', properties: { message_id: { type: 'number' }, - group_id: { type: [ 'number' , 'string' ] }, - user_id: { type: [ 'number' , 'string' ] } + group_id: { type: ['number', 'string'] }, + user_id: { type: ['number', 'string'] } }, required: ['message_id'] } as const satisfies JSONSchema; @@ -30,18 +30,14 @@ class ForwardSingleMsg extends BaseAction { } protected async _handle(payload: Payload): Promise { - const msg = await dbUtil.getMsgByShortId(payload.message_id); + const msg = await MessageUnique.getMsgIdAndPeerByShortId(payload.message_id); if (!msg) { throw new Error(`无法找到消息${payload.message_id}`); } const peer = await this.getTargetPeer(payload); - const ret = await NTQQMsgApi.forwardMsg( - { - chatType: msg.chatType, - peerUid: msg.peerUid, - }, + const ret = await NTQQMsgApi.forwardMsg(msg.Peer, peer, - [msg.msgId], + [msg.MsgId], ); if (ret.result !== 0) { throw new Error(`转发消息失败 ${ret.errMsg}`); diff --git a/src/onebot11/action/msg/GetMsg.ts b/src/onebot11/action/msg/GetMsg.ts index 784a84d0..86c8bd65 100644 --- a/src/onebot11/action/msg/GetMsg.ts +++ b/src/onebot11/action/msg/GetMsg.ts @@ -4,6 +4,8 @@ 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'; export type ReturnDataType = OB11Message @@ -11,7 +13,7 @@ export type ReturnDataType = OB11Message const SchemaData = { type: 'object', properties: { - message_id: { type: ['number','string'] }, + message_id: { type: ['number', 'string'] }, }, required: ['message_id'] } as const satisfies JSONSchema; @@ -26,14 +28,15 @@ class GetMsg extends BaseAction { if (!payload.message_id) { throw Error('参数message_id不能为空'); } - let msg = await dbUtil.getMsgByShortId(parseInt(payload.message_id.toString())); - if (!msg) { - msg = await dbUtil.getMsgByLongId(payload.message_id.toString()); - } - if (!msg) { + let MsgShortId = await MessageUnique.getShortIdByMsgId(payload.message_id.toString()); + let msgIdWithPeer = await MessageUnique.getMsgIdAndPeerByShortId(MsgShortId || parseInt(payload.message_id.toString())); + if (!msgIdWithPeer) { throw ('消息不存在'); } - return await OB11Constructor.message(msg); + let msg = await NTQQMsgApi.getMsgsByMsgId( + { guildId: '', peerUid: msgIdWithPeer?.Peer.peerUid, chatType: msgIdWithPeer.Peer.chatType }, + [msgIdWithPeer?.MsgId || payload.message_id.toString()]); + return await OB11Constructor.message(msg.msgList[0]); } } diff --git a/src/onebot11/action/msg/SendMsg/handle-forward-node.ts b/src/onebot11/action/msg/SendMsg/handle-forward-node.ts index 85ea22fa..20d7178b 100644 --- a/src/onebot11/action/msg/SendMsg/handle-forward-node.ts +++ b/src/onebot11/action/msg/SendMsg/handle-forward-node.ts @@ -7,6 +7,7 @@ import { logDebug, logError } from '@/common/utils/log'; import { sleep } from '@/common/utils/helper'; import fs from 'node:fs'; import { normalize, sendMsg } from '@/onebot11/action/msg/SendMsg/index'; +import { MessageUnique } from '@/common/utils/MessageUnique'; async function cloneMsg(msg: RawMessage): Promise { const selfPeer = { @@ -54,13 +55,14 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag const nodeId = messageNode.data.id; // 有nodeId表示一个子转发消息卡片 if (nodeId) { - const nodeMsg = await dbUtil.getMsgByShortId(parseInt(nodeId)); + const nodeMsg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(nodeId)); if (!needClone) { - nodeMsgIds.push(nodeMsg!.msgId); + nodeMsgIds.push(nodeMsg!.MsgId); } else { - if (nodeMsg!.peerUid !== selfInfo.uid) { + if (nodeMsg!.Peer.peerUid !== selfInfo.uid) { // need cloning - const clonedMsg = await cloneMsg(nodeMsg!); + let rawClone = await NTQQMsgApi.getMsgsByMsgId(nodeMsg?.Peer!,[nodeMsg?.MsgId!]); + const clonedMsg = await cloneMsg(rawClone.msgList[0]); if (clonedMsg) { nodeMsgIds.push(clonedMsg.msgId); } diff --git a/src/onebot11/action/msg/SendMsg/index.ts b/src/onebot11/action/msg/SendMsg/index.ts index 3971a456..4e75936d 100644 --- a/src/onebot11/action/msg/SendMsg/index.ts +++ b/src/onebot11/action/msg/SendMsg/index.ts @@ -8,13 +8,13 @@ import { } from '@/onebot11/types'; import { ActionName, BaseCheckResult } from '@/onebot11/action/types'; import { getGroup } from '@/core/data'; -import { dbUtil } from '@/common/utils/db'; import { ChatType, ElementType, Group, NTQQFileApi, NTQQFriendApi, NTQQMsgApi, NTQQUserApi, Peer, SendMessageElement, } from '@/core'; import fs from 'node:fs'; import { logDebug, logError } from '@/common/utils/log'; import { decodeCQCode } from '@/onebot11/cqcode'; import createSendElements from './create-send-elements'; import { handleForwardNode } from '@/onebot11/action/msg/SendMsg/handle-forward-node'; +import { MessageUnique } from '@/common/utils/MessageUnique'; export interface ReturnDataType { message_id: number; @@ -66,7 +66,7 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de } const returnMsg = await NTQQMsgApi.sendMsg(peer, sendElements, waitComplete, timeout); try { - returnMsg.id = await dbUtil.addMsg(returnMsg, false); + returnMsg.id = await MessageUnique.createMsg({ chatType: peer.chatType, guildId: '', peerUid: peer.peerUid }, returnMsg.msgId); } catch (e: any) { logDebug('发送消息id获取失败', e); returnMsg.id = 0; @@ -155,8 +155,8 @@ export class SendMsg extends BaseAction { if (getSpecialMsgNum(payload, OB11MessageDataType.node)) { const returnMsg = await handleForwardNode(peer, messages as OB11MessageNode[], group); if (returnMsg) { - const msgShortId = await dbUtil.addMsg(returnMsg!, false); - return { message_id: msgShortId }; + const msgShortId = await MessageUnique.createMsg({ guildId: '', peerUid: peer.peerUid, chatType: peer.chatType }, returnMsg!.msgId); + return { message_id: msgShortId! }; } else { throw Error('发送转发消息失败'); } diff --git a/src/onebot11/action/msg/SetMsgEmojiLike.ts b/src/onebot11/action/msg/SetMsgEmojiLike.ts index 691f6b40..4211edb8 100644 --- a/src/onebot11/action/msg/SetMsgEmojiLike.ts +++ b/src/onebot11/action/msg/SetMsgEmojiLike.ts @@ -1,8 +1,8 @@ import { ActionName } from '../types'; import BaseAction from '../BaseAction'; -import { dbUtil } from '@/common/utils/db'; import { NTQQMsgApi } from '@/core/apis'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; +import { MessageUnique } from '@/common/utils/MessageUnique'; const SchemaData = { type: 'object', @@ -19,16 +19,14 @@ export class SetMsgEmojiLike extends BaseAction { actionName = ActionName.SetMsgEmojiLike; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { - const msg = await dbUtil.getMsgByShortId(parseInt(payload.message_id.toString())); + const msg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(payload.message_id.toString())); if (!msg) { throw new Error('msg not found'); } if (!payload.emoji_id){ throw new Error('emojiId not found'); } - return await NTQQMsgApi.setEmojiLike({ - chatType: msg.chatType, - peerUid: msg.peerUid - }, msg.msgSeq, payload.emoji_id.toString(), true); + let msgSeq = (await NTQQMsgApi.getMsgsByMsgId(msg.Peer, [msg.MsgId])).msgList[0].msgSeq; + return await NTQQMsgApi.setEmojiLike(msg.Peer, msgSeq, payload.emoji_id.toString(), true); } } diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index e3e60289..10e47410 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -47,6 +47,7 @@ import { napCatCore } from '@/core'; import { OB11FriendPokeEvent, OB11GroupPokeEvent } from './event/notice/OB11PokeEvent'; import { OB11BaseNoticeEvent } from './event/notice/OB11BaseNoticeEvent'; import { OB11GroupEssenceEvent } from './event/notice/OB11GroupEssenceEvent'; +import { MessageUnique } from '@/common/utils/MessageUnique'; export class OB11Constructor { @@ -408,11 +409,12 @@ 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 (!replyMsg) { + if (msgList.length < 1) { return; } - return new OB11GroupMsgEmojiLikeEvent(parseInt(msg.peerUid), parseInt(senderUin), replyMsg.id!, [{ + return new OB11GroupMsgEmojiLikeEvent(parseInt(msg.peerUid), parseInt(senderUin), MessageUnique.getShortIdByMsgId(replyMsg?.msgId!)!, [{ emoji_id: emojiId, count: 1 }]); @@ -463,7 +465,7 @@ export class OB11Constructor { peerUid: Group! }; let msgData = await NTQQMsgApi.getMsgsBySeqAndCount(Peer, msgSeq.toString(), 1, true, true); - return new OB11GroupEssenceEvent(parseInt(msg.peerUid), await dbUtil.addMsg(msgData.msgList[0]), parseInt(msgData.msgList[0].senderUin)); + return new OB11GroupEssenceEvent(parseInt(msg.peerUid), MessageUnique.getShortIdByMsgId(msgData.msgList[0].msgId)!, parseInt(msgData.msgList[0].senderUin)); // 获取MsgSeq+Peer可获取具体消息 } if (grayTipElement.jsonGrayTipElement.busiId == 2407) { diff --git a/src/onebot11/event/notice/OB11GroupIncreaseEvent.ts b/src/onebot11/event/notice/OB11GroupIncreaseEvent.ts index 8a0dd831..0348109a 100644 --- a/src/onebot11/event/notice/OB11GroupIncreaseEvent.ts +++ b/src/onebot11/event/notice/OB11GroupIncreaseEvent.ts @@ -13,8 +13,5 @@ export class OB11GroupIncreaseEvent extends OB11GroupNoticeEvent { this.operator_id = operatorId; this.user_id = userId; this.sub_type = subType; - - if(ob11Config.GroupLocalTime.Record && (ob11Config.GroupLocalTime.RecordList[0] == '-1' || ob11Config.GroupLocalTime.RecordList.includes(groupId.toString()))) - dbUtil.insertJoinTime(groupId, userId, Math.floor(Date.now() / 1000)); } } diff --git a/src/onebot11/main.ts b/src/onebot11/main.ts index 71a3db09..62a76d2d 100644 --- a/src/onebot11/main.ts +++ b/src/onebot11/main.ts @@ -37,6 +37,7 @@ import { Data as SysData } from '@/proto/SysMessage'; import { Data as DeviceData } from '@/proto/SysMessage.DeviceChange'; import { OB11FriendPokeEvent, OB11GroupPokeEvent } from './event/notice/OB11PokeEvent'; import { isEqual } from '@/common/utils/helper'; +import { MessageUnique } from '@/common/utils/MessageUnique'; //下面几个其实应该移进Core-Data 缓存实现 但是现在在这里方便 // @@ -229,10 +230,8 @@ export class NapCatOnebot11 { // }); // console.log(ret); new Promise((resolve) => { - dbUtil.addMsg(m).then(msgShortId => { - m.id = msgShortId; - this.postReceiveMsg([m]).then().catch(logError); - }).catch(logError); + m.id = MessageUnique.createMsg({ chatType: m.chatType, peerUid: m.peerUid, guildId: '' }, m.msgId); + this.postReceiveMsg([m]).then().catch(logError); }).then(); } }; @@ -245,10 +244,8 @@ export class NapCatOnebot11 { logMessage(_msg as OB11Message).then().catch(logError); }).catch(logError); if (ob11Config.reportSelfMessage) { - dbUtil.addMsg(msg).then(id => { - msg.id = id; - this.postReceiveMsg([msg]).then().catch(logError); - }); + msg.id = MessageUnique.createMsg({ chatType: msg.chatType, peerUid: msg.peerUid, guildId: '' }, msg.msgId); + this.postReceiveMsg([msg]).then().catch(logError); } }; napCatCore.addListener(msgListener); @@ -349,10 +346,6 @@ export class NapCatOnebot11 { } if (msg.post_type === 'message') { logMessage(msg as OB11Message).then().catch(logError); - // 大概测试了一下,10000个以内 includes 和 find 性能差距不大 - if (msg.message_type == 'group' && msg.group_id && ob11Config.GroupLocalTime.Record && (ob11Config.GroupLocalTime.RecordList[0] === '-1' || ob11Config.GroupLocalTime.RecordList.find(gid => gid == msg.group_id?.toString()))) { - dbUtil.insertLastSentTime(msg.group_id, msg.user_id, msg.time); - } } else if (msg.post_type === 'notice') { logNotice(msg).then().catch(logError); } else if (msg.post_type === 'request') { @@ -534,12 +527,12 @@ export class NapCatOnebot11 { // log("message update", message.sendStatus, message.msgId, message.msgSeq) if (message.recallTime != '0') { //todo: 这个判断方法不太好,应该使用灰色消息元素来判断? // 撤回消息上报 - const oriMessage = await dbUtil.getMsgByLongId(message.msgId); - if (!oriMessage) { + const oriMessageId = await MessageUnique.getShortIdByMsgId(message.msgId); + if (!oriMessageId) { continue; } if (message.chatType == ChatType.friend) { - const friendRecallEvent = new OB11FriendRecallNoticeEvent(parseInt(message!.senderUin), oriMessage!.id!); + const friendRecallEvent = new OB11FriendRecallNoticeEvent(parseInt(message!.senderUin), oriMessageId); postOB11Event(friendRecallEvent); } else if (message.chatType == ChatType.group) { let operatorId = message.senderUin; @@ -552,7 +545,7 @@ export class NapCatOnebot11 { parseInt(message.peerUin), parseInt(message.senderUin), parseInt(operatorId), - oriMessage.id! + oriMessageId ); postOB11Event(groupRecallEvent); } diff --git a/src/onebot11/server/postOB11Event.ts b/src/onebot11/server/postOB11Event.ts index 19f733e9..9439ffc9 100644 --- a/src/onebot11/server/postOB11Event.ts +++ b/src/onebot11/server/postOB11Event.ts @@ -125,7 +125,6 @@ export function postOB11Event(msg: QuickActionEvent, reportSelf = false, postWs } async function handleMsg(msg: OB11Message, quickAction: QuickAction) { msg = msg as OB11Message; - const rawMessage = await dbUtil.getMsgByShortId(msg.message_id); const reply = quickAction.reply; const peer: Peer = { chatType: ChatType.friend,