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,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