feat: 聊天记录对聊天记录合并转发修复 & fix #1673

This commit is contained in:
手瓜一十雪 committed 2026-03-05 13:40:16 +08:00
1 parent ae53d27313
commit e0617efe9c
4 files changed
+55 -10

No files matched your search

@@ -230,8 +230,8 @@ export class PacketOperationContext {
} }
async UploadForwardMsgV2 (msg: UploadForwardMsgParams[], groupUin: number = 0) { async UploadForwardMsgV2 (msg: UploadForwardMsgParams[], groupUin: number = 0) {
//await this.SendPreprocess(msg, groupUin); //await this.SendPreprocess(msg, groupUin);
// 遍历上传资源 // 遍历上传资源(跳过已有原始数据的项)
await Promise.allSettled(msg.map(async (item) => { return await this.SendPreprocess(item.actionMsg, groupUin); })); await Promise.allSettled(msg.filter(item => item.actionMsg).map(async (item) => { return await this.SendPreprocess(item.actionMsg!, groupUin); }));
const req = trans.UploadForwardMsgV2.build(this.context.napcore.basicInfo.uid, msg, groupUin); const req = trans.UploadForwardMsgV2.build(this.context.napcore.basicInfo.uid, msg, groupUin);
const resp = await this.context.client.sendOidbPacket(req, true); const resp = await this.context.client.sendOidbPacket(req, true);
const res = trans.UploadForwardMsg.parse(resp); const res = trans.UploadForwardMsg.parse(resp);
@@ -312,6 +312,15 @@ export class PacketOperationContext {
} }
} }
async FetchForwardMsgRaw (resId: string) {
const req = trans.DownloadForwardMsg.build(this.context.napcore.basicInfo.uid, resId);
const resp = await this.context.client.sendOidbPacket(req, true);
const res = trans.DownloadForwardMsg.parse(resp);
const inflate = gunzipSync(res.result.payload);
const result = new NapProtoMsg(LongMsgResult).decode(inflate);
return result.action;
}
async FetchForwardMsg (res_id: string): Promise<RawMessage[]> { async FetchForwardMsg (res_id: string): Promise<RawMessage[]> {
const req = trans.DownloadForwardMsg.build(this.context.napcore.basicInfo.uid, res_id); const req = trans.DownloadForwardMsg.build(this.context.napcore.basicInfo.uid, res_id);
const resp = await this.context.client.sendOidbPacket(req, true); const resp = await this.context.client.sendOidbPacket(req, true);
@@ -1,3 +1,4 @@
import * as crypto from 'node:crypto';
import * as zlib from 'node:zlib'; import * as zlib from 'node:zlib';
import { NapProtoDecodeStructType, NapProtoEncodeStructType, NapProtoMsg } from 'napcat-protobuf'; import { NapProtoDecodeStructType, NapProtoEncodeStructType, NapProtoMsg } from 'napcat-protobuf';
import { import {
@@ -665,11 +666,13 @@ export class PacketMsgMarkDownElement extends IPacketMsgElement<SendMarkdownElem
export class PacketMultiMsgElement extends IPacketMsgElement<SendMultiForwardMsgElement> { export class PacketMultiMsgElement extends IPacketMsgElement<SendMultiForwardMsgElement> {
resid: string; resid: string;
message: PacketMsg[]; message: PacketMsg[];
uuid: string;
constructor (rawElement: SendMultiForwardMsgElement, message?: PacketMsg[]) { constructor (rawElement: SendMultiForwardMsgElement, message?: PacketMsg[]) {
super(rawElement); super(rawElement);
this.resid = rawElement.multiForwardMsgElement.resId; this.resid = rawElement.multiForwardMsgElement.resId;
this.message = message ?? []; this.message = message ?? [];
this.uuid = rawElement.multiForwardMsgElement.fileName || crypto.randomUUID();
} }
override buildElement (): NapProtoEncodeStructType<typeof Elem>[] { override buildElement (): NapProtoEncodeStructType<typeof Elem>[] {
@@ -677,7 +680,7 @@ export class PacketMultiMsgElement extends IPacketMsgElement<SendMultiForwardMsg
lightAppElem: { lightAppElem: {
data: Buffer.concat([ data: Buffer.concat([
Buffer.from([0x01]), Buffer.from([0x01]),
zlib.deflateSync(Buffer.from(JSON.stringify(ForwardMsgBuilder.fromPacketMsg(this.resid, this.message)), 'utf-8')), zlib.deflateSync(Buffer.from(JSON.stringify(ForwardMsgBuilder.fromPacketMsg(this.resid, this.message, undefined, undefined, undefined, undefined, this.uuid)), 'utf-8')),
]), ]),
}, },
}]; }];
@@ -1,19 +1,20 @@
import zlib from 'node:zlib'; import zlib from 'node:zlib';
import * as proto from '@/napcat-core/packet/transformer/proto'; import * as proto from '@/napcat-core/packet/transformer/proto';
import { NapProtoMsg } from 'napcat-protobuf'; import { NapProtoEncodeStructType, NapProtoMsg } from 'napcat-protobuf';
import { OidbPacket, PacketBufBuilder, PacketTransformer } from '@/napcat-core/packet/transformer/base'; import { OidbPacket, PacketBufBuilder, PacketTransformer } from '@/napcat-core/packet/transformer/base';
import { PacketMsg } from '@/napcat-core/packet/message/message'; import { PacketMsg } from '@/napcat-core/packet/message/message';
export interface UploadForwardMsgParams { export interface UploadForwardMsgParams {
actionCommand: string; actionCommand: string;
actionMsg: PacketMsg[]; actionMsg?: PacketMsg[];
actionMsgBody?: NapProtoEncodeStructType<typeof proto.PushMsgBody>[];
} }
class UploadForwardMsgV2 extends PacketTransformer<typeof proto.SendLongMsgResp> { class UploadForwardMsgV2 extends PacketTransformer<typeof proto.SendLongMsgResp> {
build (selfUid: string, msg: UploadForwardMsgParams[], groupUin: number = 0): OidbPacket { build (selfUid: string, msg: UploadForwardMsgParams[], groupUin: number = 0): OidbPacket {
const reqdata = msg.map((item) => ({ const reqdata = msg.map((item) => ({
actionCommand: item.actionCommand, actionCommand: item.actionCommand,
actionData: { actionData: {
msgBody: this.msgBuilder.buildFakeMsg(selfUid, item.actionMsg), msgBody: item.actionMsgBody ?? this.msgBuilder.buildFakeMsg(selfUid, item.actionMsg ?? []),
} }
})); }));
const longMsgResultData = new NapProtoMsg(proto.LongMsgResult).encode( const longMsgResultData = new NapProtoMsg(proto.LongMsgResult).encode(
+36 -4
View File
@@ -215,11 +215,11 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
uuid?: string, uuid?: string,
packetMsg: PacketMsg[], packetMsg: PacketMsg[],
deleteAfterSentFiles: string[], deleteAfterSentFiles: string[],
innerPacketMsg?: Array<{ uuid: string, packetMsg: PacketMsg[]; }>; innerPacketMsg?: Array<{ uuid: string, packetMsg?: PacketMsg[], actionMsgBody?: UploadForwardMsgParams['actionMsgBody']; }>;
} | null> { } | null> {
const packetMsg: PacketMsg[] = []; const packetMsg: PacketMsg[] = [];
const delFiles: string[] = []; const delFiles: string[] = [];
const innerMsg: Array<{ uuid: string, packetMsg: PacketMsg[]; }> = new Array(); const innerMsg: Array<{ uuid: string, packetMsg?: PacketMsg[], actionMsgBody?: UploadForwardMsgParams['actionMsgBody']; }> = new Array();
for (const node of messageNodes) { for (const node of messageNodes) {
if (dp >= 3) { if (dp >= 3) {
this.core.context.logger.logWarn('转发消息深度超过3层,将停止解析!'); this.core.context.logger.logWarn('转发消息深度超过3层,将停止解析!');
@@ -240,7 +240,7 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
if (uploadReturnData?.uuid) { if (uploadReturnData?.uuid) {
innerMsg.push({ uuid: uploadReturnData.uuid, packetMsg: uploadReturnData.packetMsg }); innerMsg.push({ uuid: uploadReturnData.uuid, packetMsg: uploadReturnData.packetMsg });
uploadReturnData.innerPacketMsg?.forEach(m => { uploadReturnData.innerPacketMsg?.forEach(m => {
innerMsg.push({ uuid: m.uuid, packetMsg: m.packetMsg }); innerMsg.push(m);
}); });
} }
@@ -274,6 +274,37 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
const msgCache = await this.core.apis.FileApi.downloadRawMsgMedia([msg]); const msgCache = await this.core.apis.FileApi.downloadRawMsgMedia([msg]);
delFiles.push(...msgCache); delFiles.push(...msgCache);
const transformedMsg = this.core.apis.PacketApi.pkt.msgConverter.rawMsgToPacketMsg(msg, msgPeer); const transformedMsg = this.core.apis.PacketApi.pkt.msgConverter.rawMsgToPacketMsg(msg, msgPeer);
// 检测嵌套合并转发消息,拉取内层protobuf数据
for (const element of msg.elements) {
let resId: string | undefined;
let uuid: string | undefined;
if (element.multiForwardMsgElement?.resId) {
resId = element.multiForwardMsgElement.resId;
uuid = element.multiForwardMsgElement.fileName;
} else if (element.arkElement?.bytesData) {
try {
const json = JSON.parse(element.arkElement.bytesData);
if (json.app === 'com.tencent.multimsg') {
resId = json.meta?.detail?.resid;
uuid = json.meta?.detail?.uniseq || json.extra?.filename;
}
} catch { /* not a valid json */ }
}
if (resId && uuid) {
try {
const rawActions = await this.core.apis.PacketApi.pkt.operation.FetchForwardMsgRaw(resId);
for (const action of rawActions) {
if (action.actionCommand === 'MultiMsg') {
innerMsg.push({ uuid, actionMsgBody: action.actionData.msgBody });
} else {
innerMsg.push({ uuid: action.actionCommand, actionMsgBody: action.actionData.msgBody });
}
}
} catch (e) {
this.core.context.logger.logError(`获取合并转发内层消息失败: ${(e as Error)?.stack}`);
}
}
}
this.core.context.logger.logDebug(`handleForwardedNodesPacket[PureRaw] 转换为 ${stringifyWithBigInt(transformedMsg)}`); this.core.context.logger.logDebug(`handleForwardedNodesPacket[PureRaw] 转换为 ${stringifyWithBigInt(transformedMsg)}`);
packetMsg.push(transformedMsg); packetMsg.push(transformedMsg);
} }
@@ -289,10 +320,11 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
actionCommand: 'MultiMsg', actionCommand: 'MultiMsg',
actionMsg: packetMsg, actionMsg: packetMsg,
}]; }];
innerMsg.forEach(({ uuid, packetMsg: msg }) => { innerMsg.forEach(({ uuid, packetMsg: msg, actionMsgBody: raw }) => {
uploadMsgData.push({ uploadMsgData.push({
actionCommand: uuid, actionCommand: uuid,
actionMsg: msg, actionMsg: msg,
actionMsgBody: raw,
}); });
}); });
const resid = await this.core.apis.PacketApi.pkt.operation.UploadForwardMsgV2(uploadMsgData, msgPeer.chatType === ChatType.KCHATTYPEGROUP ? +msgPeer.peerUid : 0); const resid = await this.core.apis.PacketApi.pkt.operation.UploadForwardMsgV2(uploadMsgData, msgPeer.chatType === ChatType.KCHATTYPEGROUP ? +msgPeer.peerUid : 0);