fix(onebot): restore image links inside merged forwards

get_forward_msg returned image segments with empty url and file when
the long-msg payload omitted a download path. Rebuild the URL from
the file fingerprint the payload still carries.

Fixes #412
This commit is contained in:
DrSmoothl committed 2026-08-21 18:26:15 +08:00
1 parent 6ec368f4b5
commit 39e6b45b81
3 files changed
+102 -11

No files matched your search

+13
View File
@@ -23,6 +23,19 @@ export function makeImageUrl(origUrl: string): string {
return 'http://gchat.qpic.cn' + origUrl;
}
/** Classic group-pic URL used when the wire carries an MD5 and no download path. */
export function imageUrlFromMd5(md5Hex: string): string {
if (!/^[0-9a-fA-F]{32}$/.test(md5Hex)) return '';
return `http://gchat.qpic.cn/gchatpic_new/0/0-0-${md5Hex.toUpperCase()}/0`;
}
/** NT multimedia download URL reconstructed from a file id when the path is omitted. */
export function ntImageUrlFromFileId(fileId: string, isGroup: boolean): string {
if (!fileId) return '';
const appid = isGroup ? 1407 : 1406;
return `https://multimedia.nt.qq.com.cn/download?appid=${appid}&fileid=${encodeURIComponent(fileId)}`;
}
export function decompressData(
data: Uint8Array,
maxOutputBytes = MAX_RICH_CARD_OUTPUT_BYTES,
@@ -19,9 +19,11 @@ import type { MarkdownData } from '@snowluma/proto-defs/action';
import type { FileExtra, MessageBody, PushMsgBody as PushMsgBodyFull, RichText } from '@snowluma/proto-defs/message';
import {
decompressData,
imageUrlFromMd5,
makeImageUrl,
MAX_RICH_CARD_MESSAGE_OUTPUT_BYTES,
MAX_RICH_CARD_OUTPUT_BYTES,
ntImageUrlFromFileId,
} from './helpers';
type ElemDecoded = Elem;
@@ -476,7 +478,7 @@ export function decodeRichBody(body: PushMsgBody | undefined, isGroup: boolean):
if (body?.richText) {
const rt = body.richText;
logUnknownWireFields(rt, 'body.richText');
if (rt.elems) elements.push(...convertElements(rt.elems as ElemDecoded[]));
if (rt.elems) elements.push(...convertElements(rt.elems as ElemDecoded[], isGroup));
extractRichtextExtras(rt, elements, isGroup);
}
if (body?.msgContent && body.msgContent.length > 0) {
@@ -485,7 +487,7 @@ export function decodeRichBody(body: PushMsgBody | undefined, isGroup: boolean):
return elements;
}
function convertElements(elems: ElemDecoded[]): MessageElement[] {
function convertElements(elems: ElemDecoded[], isGroup: boolean): MessageElement[] {
const result: MessageElement[] = [];
// [#146/#337] Rich cards and bot markdown arrive beside a plain compatibility
// `text` element for older clients. That sibling has no independent wire
@@ -562,7 +564,7 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
);
if (nested) decoded.push(nested);
}
if (decoded.length) reply.replyElements = convertElements(decoded);
if (decoded.length) reply.replyElements = convertElements(decoded, isGroup);
}
// A C2C quoted FILE lives in RichText.notOnlineFile (message level), not
// in elems[] — recover it from sourceMsg (field 9) when elems carried no
@@ -666,11 +668,12 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
if (elem.notOnlineImage) {
const img = elem.notOnlineImage;
if (img.picMd5?.length === 16) {
const md5Hex = toHexUpper(img.picMd5);
const urlPath = img.origUrl || img.bigUrl || '';
result.push({
type: 'image',
imageUrl: makeImageUrl(urlPath),
fileId: img.filePath ?? '',
imageUrl: makeImageUrl(urlPath) || (!urlPath ? imageUrlFromMd5(md5Hex) : ''),
fileId: img.filePath || md5Hex,
fileSize: img.fileLen ?? 0,
width: img.picWidth ?? 0,
height: img.picHeight ?? 0,
@@ -680,7 +683,7 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
// expect these literal Chinese strings when the wire
// doesn't carry a per-image override.
summary: img.pbRes?.summary || (img.pbRes?.subType === 1 ? '[动画表情]' : '[图片]'),
md5Hex: toHexUpper(img.picMd5),
md5Hex,
});
}
}
@@ -689,16 +692,18 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
if (elem.customFace) {
const img = elem.customFace;
if (img.md5?.length === 16) {
const md5Hex = toHexUpper(img.md5);
const origUrl = img.origUrl ?? '';
result.push({
type: 'image',
imageUrl: makeImageUrl(img.origUrl ?? ''),
fileId: img.filePath ?? '',
imageUrl: makeImageUrl(origUrl) || (!origUrl ? imageUrlFromMd5(md5Hex) : ''),
fileId: img.filePath || md5Hex,
fileSize: img.size ?? 0,
width: img.width ?? 0,
height: img.height ?? 0,
subType: img.pbRes?.subType ?? 0,
summary: img.pbRes?.summary || (img.pbRes?.subType === 1 ? '[动画表情]' : '[图片]'),
md5Hex: toHexUpper(img.md5),
md5Hex,
});
}
}
@@ -849,8 +854,10 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
}
}
}
const fileId = fi.fileName || idx.fileUuid || '';
if (!url && idx.fileUuid) url = ntImageUrlFromFileId(idx.fileUuid, isGroup);
const me: MessageElement = {
type: 'image', fileId: fi.fileName ?? '',
type: 'image', fileId,
fileSize: fi.fileSize ?? 0, width: fi.width ?? 0,
height: fi.height ?? 0, imageUrl: url,
};
@@ -25,7 +25,7 @@ import type {
SrcMsgPbReserve,
TextElem,
} from '@snowluma/proto-defs/element';
import { makeImageUrl } from '../../src/msg-push/helpers';
import { imageUrlFromMd5, makeImageUrl, ntImageUrlFromFileId } from '../../src/msg-push/helpers';
function lightAppBytes(json: unknown): Uint8Array {
const buf = deflateSync(Buffer.from(JSON.stringify(json), 'utf8'));
@@ -1035,6 +1035,64 @@ describe('decodeRichBody / unknown wire element observability', () => {
expect(out).toEqual([]);
});
it('rebuilds a download URL from md5 when a customFace has no path', () => {
const out = decodeRichBody({
richText: {
elems: [customFaceElem(fileName, '', otherMd5)],
},
}, true);
expect(out).toEqual([{
type: 'image',
imageUrl: 'http://gchat.qpic.cn/gchatpic_new/0/0-0-0102030405060708090A0B0C0D0E0F10/0',
fileId: fileName,
fileSize: 10711,
width: 1080,
height: 1080,
subType: 0,
summary: '[图片]',
md5Hex: '0102030405060708090A0B0C0D0E0F10',
}]);
});
it('rebuilds an NT download URL from file id when the path is omitted', () => {
const msgInfo: MsgInfo = {
msgInfoBody: [{
index: {
fileUuid: 'EhRforward',
info: {
fileSize: 10711,
fileHash: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
fileSha1: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
fileName: '',
width: 1080,
height: 1080,
type: { picFormat: 1001 },
},
},
}],
extBizInfo: { pic: { bizType: 0, textSummary: '[图片]' } },
};
const out = decodeRichBody({
richText: {
elems: [{
commonElem: {
serviceType: 48,
businessType: 20,
pbElem: protobuf_encode<MsgInfo>(msgInfo),
},
}],
},
}, true);
expect(out).toEqual([expect.objectContaining({
type: 'image',
fileId: 'EhRforward',
imageUrl: 'https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=EhRforward',
summary: '[图片]',
})]);
});
it('keeps two distinct pictures', () => {
const second = 'other.png';
const out = decodeRichBody({
@@ -1068,6 +1126,19 @@ describe('decodeRichBody / unknown wire element observability', () => {
expect(makeImageUrl('/gchatpic_new/0/0-0-AABB/0'))
.toBe('http://gchat.qpic.cn/gchatpic_new/0/0-0-AABB/0');
});
it('rebuilds a gchat URL from a 32-char md5', () => {
expect(imageUrlFromMd5('0102030405060708090a0b0c0d0e0f10'))
.toBe('http://gchat.qpic.cn/gchatpic_new/0/0-0-0102030405060708090A0B0C0D0E0F10/0');
expect(imageUrlFromMd5('short')).toBe('');
});
it('rebuilds an NT URL from a file id', () => {
expect(ntImageUrlFromFileId('EhRforward', true))
.toBe('https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=EhRforward');
expect(ntImageUrlFromFileId('EhRprivate', false))
.toBe('https://multimedia.nt.qq.com.cn/download?appid=1406&fileid=EhRprivate');
});
});
it('bounds retained card output across the entire message', () => {