fix(protocol): restore forward image media and drop file receipts
Bot-built merged forwards left hollow NT images after long-msg fetch, so get_forward_msg returned empty file/url. Decode now rebuilds identity from md5, ignores stubs, and forward uploads keep a legacy image sibling. C2C offline-file download receipts were reported as the peer sending a file. Treat downloadFlag=2 as a receipt and drop it from the chat stream.
This commit is contained in:
10 files changed
+340
-24
No files matched your search
@@ -2,8 +2,8 @@ import type {
|
||||
MarkdownData,
|
||||
MentionExtraSend,
|
||||
} from '@snowluma/proto-defs/action';
|
||||
import type { Elem, GroupFileExtra, MarketFacePbReserve, PokeExtra, QFaceExtra, QSmallFaceExtra } from '@snowluma/proto-defs/element';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import type { Elem, GroupFileExtra, MarketFacePbReserve, MsgInfo, PokeExtra, QFaceExtra, QSmallFaceExtra } from '@snowluma/proto-defs/element';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { deflateSync } from 'zlib';
|
||||
import type { BridgeContext } from './bridge-context';
|
||||
@@ -316,7 +316,69 @@ function makeForwardElem(element: MessageElement): ProtoElem {
|
||||
};
|
||||
}
|
||||
|
||||
async function makeImageElem(ctx: SendContext, element: MessageElement): Promise<ProtoElem> {
|
||||
function md5BytesFromHex(hex: string | undefined): Uint8Array | null {
|
||||
if (!hex || !/^[0-9a-fA-F]{32}$/.test(hex)) return null;
|
||||
const bytes = hexToBytes(hex);
|
||||
return bytes.length === 16 ? bytes : null;
|
||||
}
|
||||
|
||||
function fingerprintsFromEncodedMsgInfo(msgInfo: Uint8Array): {
|
||||
md5Hex: string;
|
||||
fileName: string;
|
||||
fileSize: number;
|
||||
width: number;
|
||||
height: number;
|
||||
} | null {
|
||||
let info: MsgInfo;
|
||||
try {
|
||||
info = protobuf_decode<MsgInfo>(msgInfo);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const fi = info.msgInfoBody?.[0]?.index?.info;
|
||||
if (!fi?.fileHash || !/^[0-9a-fA-F]{32}$/.test(fi.fileHash)) return null;
|
||||
return {
|
||||
md5Hex: fi.fileHash,
|
||||
fileName: fi.fileName || `${fi.fileHash.toLowerCase()}.png`,
|
||||
fileSize: fi.fileSize ?? 0,
|
||||
width: fi.width ?? 0,
|
||||
height: fi.height ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Long-msg storage often strips NT image download paths. A CustomFace /
|
||||
* NotOnlineImage sibling with the md5 lets get_forward_msg rebuild a
|
||||
* fetchable URL the way native forwards do (#441).
|
||||
*/
|
||||
function makeLegacyForwardImageElem(
|
||||
isGroup: boolean,
|
||||
msgInfo: Uint8Array,
|
||||
element: MessageElement,
|
||||
): ProtoElem | null {
|
||||
const fromInfo = fingerprintsFromEncodedMsgInfo(msgInfo);
|
||||
const md5 = md5BytesFromHex(element.md5Hex) ?? md5BytesFromHex(fromInfo?.md5Hex);
|
||||
if (!md5) return null;
|
||||
const md5Hex = (element.md5Hex || fromInfo!.md5Hex);
|
||||
const fileName = element.fileName || fromInfo?.fileName || `${md5Hex.toLowerCase()}.png`;
|
||||
const fileSize = element.fileSize ?? fromInfo?.fileSize ?? 0;
|
||||
const width = element.width ?? fromInfo?.width ?? 0;
|
||||
const height = element.height ?? fromInfo?.height ?? 0;
|
||||
if (isGroup) {
|
||||
return { customFace: { filePath: fileName, md5, size: fileSize, width, height } };
|
||||
}
|
||||
return {
|
||||
notOnlineImage: {
|
||||
filePath: fileName,
|
||||
picMd5: md5,
|
||||
fileLen: fileSize,
|
||||
picWidth: width,
|
||||
picHeight: height,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function makeImageElem(ctx: SendContext, element: MessageElement): Promise<ProtoElem[]> {
|
||||
const isGroup = ctx.groupId !== undefined;
|
||||
const targetIdOrUid = isGroup ? ctx.groupId! : (ctx.userUid ?? '');
|
||||
if (!isGroup && !targetIdOrUid) {
|
||||
@@ -324,14 +386,16 @@ async function makeImageElem(ctx: SendContext, element: MessageElement): Promise
|
||||
}
|
||||
|
||||
const msgInfo = await uploadImageMsgInfo(ctx.bridge, isGroup, targetIdOrUid, element);
|
||||
|
||||
return {
|
||||
const nt: ProtoElem = {
|
||||
commonElem: {
|
||||
serviceType: 48,
|
||||
pbElem: msgInfo,
|
||||
businessType: isGroup ? 20 : 10,
|
||||
},
|
||||
};
|
||||
if (!ctx.forwardFake) return [nt];
|
||||
const legacy = makeLegacyForwardImageElem(isGroup, msgInfo, element);
|
||||
return legacy ? [nt, legacy] : [nt];
|
||||
}
|
||||
|
||||
async function makePttElem(ctx: SendContext, element: MessageElement): Promise<ProtoElem> {
|
||||
@@ -539,7 +603,7 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
|
||||
break;
|
||||
|
||||
case 'image':
|
||||
result.push(await makeImageElem(ctx!, elem));
|
||||
result.push(...await makeImageElem(ctx!, elem));
|
||||
break;
|
||||
|
||||
case 'forward':
|
||||
|
||||
@@ -2,7 +2,7 @@ import { hexPreview } from '@snowluma/common/hex';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import type { Elem } from '@snowluma/proto-defs/element';
|
||||
import type { MsgPushHead, PushMsgBody } from './context';
|
||||
import { hasDecodableContent } from './rich-body-decoder';
|
||||
import { hasDecodableContent, isOfflineFileReceipt } from './rich-body-decoder';
|
||||
|
||||
const log = createLogger('MsgPush');
|
||||
|
||||
@@ -18,7 +18,7 @@ const C2C_CONTROL_TYPES = new Set<number>([141, 166, 167]);
|
||||
// phantom (#102) is one of them.
|
||||
const C2C_CONTROL_CMDS = new Set<number>([1, 73, 75, 129, 131, 133, 135, 192]);
|
||||
|
||||
export type MessageSurvival = 'drop-control' | 'drop-blank' | 'keep-undecoded' | 'keep';
|
||||
export type MessageSurvival = 'drop-control' | 'drop-receipt' | 'drop-blank' | 'keep-undecoded' | 'keep';
|
||||
|
||||
/**
|
||||
* Whether a push is a C2C control/system signal that QQ NT never renders as a
|
||||
@@ -55,6 +55,7 @@ export function classifyMessageSurvival(
|
||||
body: PushMsgBody | undefined,
|
||||
): MessageSurvival {
|
||||
if (isC2cControlPush(head)) return 'drop-control';
|
||||
if (isOfflineFileReceipt(body)) return 'drop-receipt';
|
||||
if (elements.length !== 0) return 'keep';
|
||||
if (hasDecodableContent(body)) return 'keep-undecoded';
|
||||
return 'drop-blank';
|
||||
@@ -90,7 +91,7 @@ export function keepDecodedMessage(
|
||||
fromUin: number,
|
||||
): boolean {
|
||||
const survival = classifyMessageSurvival(head, elements, body);
|
||||
if (survival === 'drop-control' || survival === 'drop-blank') return false;
|
||||
if (survival === 'drop-control' || survival === 'drop-receipt' || survival === 'drop-blank') return false;
|
||||
if (survival === 'keep-undecoded') {
|
||||
warnUndecodedMessage({
|
||||
kind,
|
||||
|
||||
@@ -179,6 +179,21 @@ function parseMsgPushInternal(
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (survival === 'drop-receipt') {
|
||||
log.trace(() => [
|
||||
'packet_branch serviceCmd=%j seqId=%d branch=offline_file_receipt msgType=%d subType=%d messageSeq=%d',
|
||||
pkt.serviceCmd,
|
||||
pkt.seqId,
|
||||
ctx.head.msgType,
|
||||
ctx.head.subType,
|
||||
ctx.head.sequence,
|
||||
]);
|
||||
log.debug(
|
||||
'dropped offline file receipt (kind=%s seq=%d from=%d msgType=%d)',
|
||||
ev.kind, ctx.head.sequence, ctx.fromUin, ctx.head.msgType,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
if (survival === 'drop-blank') {
|
||||
log.trace(() => [
|
||||
'packet_branch serviceCmd=%j seqId=%d branch=empty_message msgType=%d subType=%d messageSeq=%d',
|
||||
|
||||
@@ -854,7 +854,7 @@ function convertElements(elems: ElemDecoded[], isGroup: boolean): MessageElement
|
||||
}
|
||||
}
|
||||
}
|
||||
const fileId = fi.fileName || idx.fileUuid || '';
|
||||
let fileId = fi.fileName || idx.fileUuid || '';
|
||||
if (!url && idx.fileUuid) url = ntImageUrlFromFileId(idx.fileUuid, isGroup);
|
||||
const me: MessageElement = {
|
||||
type: 'image', fileId,
|
||||
@@ -862,13 +862,21 @@ function convertElements(elems: ElemDecoded[], isGroup: boolean): MessageElement
|
||||
height: fi.height ?? 0, imageUrl: url,
|
||||
};
|
||||
assignValidFingerprints(me, fi.fileHash, fi.fileSha1, 'commonElem image');
|
||||
if (!fileId && me.md5Hex) fileId = `${me.md5Hex.toLowerCase()}.png`;
|
||||
if (!url && me.md5Hex) url = imageUrlFromMd5(me.md5Hex);
|
||||
me.fileId = fileId;
|
||||
me.imageUrl = url;
|
||||
if (fi.type?.picFormat) me.picFormat = fi.type.picFormat;
|
||||
if (info.extBizInfo?.pic) {
|
||||
me.subType = info.extBizInfo.pic.bizType ?? 0;
|
||||
me.summary = info.extBizInfo.pic.textSummary
|
||||
|| (me.subType === 1 ? '[动画表情]' : '[图片]');
|
||||
}
|
||||
result.push(me);
|
||||
// Long-msg fetch of a bot-built forward can leave a stub NT
|
||||
// image (format only, no id/url/md5). Do not emit that hollow
|
||||
// bubble — it would also suppress the customFace sibling that
|
||||
// still carries a usable md5 (#441).
|
||||
if (hasUsableImageIdentity(me)) result.push(me);
|
||||
} else if (bizType === 12 || bizType === 22) {
|
||||
// Record
|
||||
const record: MessageElementOf<'record'> = {
|
||||
@@ -960,8 +968,15 @@ function convertElements(elems: ElemDecoded[], isGroup: boolean): MessageElement
|
||||
return dropLegacyImageSiblings(result);
|
||||
}
|
||||
|
||||
function isNtImage(element: MessageElement): boolean {
|
||||
function hasUsableImageIdentity(element: MessageElement): boolean {
|
||||
if (element.type !== 'image') return false;
|
||||
if (element.fileId) return true;
|
||||
if (element.md5Hex && /^[0-9a-fA-F]{32}$/.test(element.md5Hex)) return true;
|
||||
return isUsableImageUrl(element.imageUrl ?? '');
|
||||
}
|
||||
|
||||
function isNtImage(element: MessageElement): boolean {
|
||||
if (!hasUsableImageIdentity(element)) return false;
|
||||
if (element.picFormat != null || element.sha1Hex) return true;
|
||||
const url = element.imageUrl ?? '';
|
||||
return url.includes('://multimedia.nt.qq.com.cn/');
|
||||
@@ -1185,7 +1200,7 @@ function extractRichtextExtras(
|
||||
}
|
||||
|
||||
// NotOnlineFile (C2C file)
|
||||
if (rt.notOnlineFile) {
|
||||
if (rt.notOnlineFile && !isOfflineFileReceiptFlag(rt.notOnlineFile.downloadFlag)) {
|
||||
const f = rt.notOnlineFile;
|
||||
elements.push({
|
||||
type: 'file', fileId: f.fileUuid ?? '',
|
||||
@@ -1213,6 +1228,7 @@ function extractMsgContent(msgContent: Uint8Array, elements: MessageElement[]):
|
||||
);
|
||||
if (!extra?.file) return;
|
||||
const f = extra.file;
|
||||
if (isOfflineFileReceiptFlag(f.downloadFlag)) return;
|
||||
if (!f.fileUuid) return;
|
||||
elements.push({
|
||||
type: 'file',
|
||||
@@ -1222,3 +1238,20 @@ function extractMsgContent(msgContent: Uint8Array, elements: MessageElement[]):
|
||||
fileHash: f.fileHash ?? '',
|
||||
});
|
||||
}
|
||||
|
||||
/** QQ treats downloadFlag=2 as an offline-file receipt gray tip, not a chat bubble. */
|
||||
function isOfflineFileReceiptFlag(flag: number | undefined): boolean {
|
||||
return flag === 2;
|
||||
}
|
||||
|
||||
/** True when this body is an offline-file download receipt, not a new file message. */
|
||||
export function isOfflineFileReceipt(body: PushMsgBody | undefined): boolean {
|
||||
if (isOfflineFileReceiptFlag(body?.richText?.notOnlineFile?.downloadFlag)) return true;
|
||||
if (!body?.msgContent || body.msgContent.length === 0) return false;
|
||||
const extra = decodeProtobufPayload(
|
||||
'messageBody.msgContent',
|
||||
body.msgContent,
|
||||
() => protobuf_decode<FileExtra>(body.msgContent!),
|
||||
);
|
||||
return isOfflineFileReceiptFlag(extra?.file?.downloadFlag);
|
||||
}
|
||||
Reference in new issue
Block a user