fix(protocol): drop leftover picture copies in robot messages
Some group-bot pictures arrive twice: one working download and one unusable leftover for older clients. Only the working picture is kept. Fixes #389
This commit is contained in:
3 files changed
+195
-2
No files matched your search
@@ -13,7 +13,13 @@ export type DecompressDataResult =
|
|||||||
|
|
||||||
export function makeImageUrl(origUrl: string): string {
|
export function makeImageUrl(origUrl: string): string {
|
||||||
if (!origUrl) return '';
|
if (!origUrl) return '';
|
||||||
if (origUrl.includes('rkey')) return 'https://multimedia.nt.qq.com.cn' + origUrl;
|
if (/^https?:\/\//i.test(origUrl)) return origUrl;
|
||||||
|
// Compatibility faces sometimes carry a query fragment without a path.
|
||||||
|
// Prefixing the host onto that produces an unusable URL.
|
||||||
|
if (!origUrl.startsWith('/')) return '';
|
||||||
|
if (origUrl.includes('rkey') || origUrl.includes('fileid')) {
|
||||||
|
return 'https://multimedia.nt.qq.com.cn' + origUrl;
|
||||||
|
}
|
||||||
return 'http://gchat.qpic.cn' + origUrl;
|
return 'http://gchat.qpic.cn' + origUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -950,7 +950,46 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return dropLegacyImageSiblings(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isNtImage(element: MessageElement): boolean {
|
||||||
|
if (element.type !== 'image') return false;
|
||||||
|
if (element.picFormat != null || element.sha1Hex) return true;
|
||||||
|
const url = element.imageUrl ?? '';
|
||||||
|
return url.includes('://multimedia.nt.qq.com.cn/');
|
||||||
|
}
|
||||||
|
|
||||||
|
function isUsableImageUrl(url: string): boolean {
|
||||||
|
let parsed: URL;
|
||||||
|
try {
|
||||||
|
parsed = new URL(url);
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return false;
|
||||||
|
if (parsed.hostname === 'multimedia.nt.qq.com.cn') return parsed.pathname.length > 1;
|
||||||
|
return parsed.pathname.length > 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sameImageFile(left: MessageElement, right: MessageElement): boolean {
|
||||||
|
if (left.type !== 'image' || right.type !== 'image') return false;
|
||||||
|
if (left.fileId && right.fileId && left.fileId === right.fileId) return true;
|
||||||
|
return Boolean(left.md5Hex && right.md5Hex && left.md5Hex === right.md5Hex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// [#389] NT pictures arrive as CommonElem plus a CustomFace / NotOnlineImage
|
||||||
|
// sibling for older clients. QQ shows one picture. Keep the NT image and drop
|
||||||
|
// the sibling when it names the same file, or when its URL cannot be fetched.
|
||||||
|
function dropLegacyImageSiblings(elements: MessageElement[]): MessageElement[] {
|
||||||
|
const ntImages = elements.filter(isNtImage);
|
||||||
|
return elements.filter((element) => {
|
||||||
|
if (element.type !== 'image') return true;
|
||||||
|
if (isNtImage(element)) return true;
|
||||||
|
const url = element.imageUrl ?? '';
|
||||||
|
if (!url || !isUsableImageUrl(url)) return false;
|
||||||
|
return !ntImages.some((nt) => sameImageFile(nt, element));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import type {
|
|||||||
SrcMsgPbReserve,
|
SrcMsgPbReserve,
|
||||||
TextElem,
|
TextElem,
|
||||||
} from '@snowluma/proto-defs/element';
|
} from '@snowluma/proto-defs/element';
|
||||||
|
import { makeImageUrl } from '../../src/msg-push/helpers';
|
||||||
|
|
||||||
function lightAppBytes(json: unknown): Uint8Array {
|
function lightAppBytes(json: unknown): Uint8Array {
|
||||||
const buf = deflateSync(Buffer.from(JSON.stringify(json), 'utf8'));
|
const buf = deflateSync(Buffer.from(JSON.stringify(json), 'utf8'));
|
||||||
@@ -922,6 +923,153 @@ describe('decodeRichBody / unknown wire element observability', () => {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function ntImageElem(fileName: string): Elem {
|
||||||
|
const msgInfo: MsgInfo = {
|
||||||
|
msgInfoBody: [{
|
||||||
|
index: {
|
||||||
|
info: {
|
||||||
|
fileSize: 10711,
|
||||||
|
fileHash: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
||||||
|
fileSha1: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
|
||||||
|
fileName,
|
||||||
|
width: 1080,
|
||||||
|
height: 1080,
|
||||||
|
type: { picFormat: 1001 },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
picture: {
|
||||||
|
urlPath: '/download?appid=1407&fileid=EhRgood',
|
||||||
|
domain: 'multimedia.nt.qq.com.cn',
|
||||||
|
ext: { originalParameter: '&rkey=good' },
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
extBizInfo: { pic: { bizType: 0, textSummary: '[图片]' } },
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
commonElem: {
|
||||||
|
serviceType: 48,
|
||||||
|
businessType: 20,
|
||||||
|
pbElem: protobuf_encode<MsgInfo>(msgInfo),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function customFaceElem(filePath: string, origUrl: string, md5: Uint8Array): Elem {
|
||||||
|
return {
|
||||||
|
customFace: {
|
||||||
|
filePath,
|
||||||
|
origUrl,
|
||||||
|
md5,
|
||||||
|
size: 10711,
|
||||||
|
width: 1080,
|
||||||
|
height: 1080,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('decodeRichBody / NT image compatibility siblings (#389)', () => {
|
||||||
|
const fileName = 'bcc7bbcc76a1a9d940c57346a5d223e8.png';
|
||||||
|
const otherMd5 = Uint8Array.from({ length: 16 }, (_, i) => i + 1);
|
||||||
|
|
||||||
|
it('keeps the NT image and drops the query-only customFace sibling', () => {
|
||||||
|
const out = decodeRichBody({
|
||||||
|
richText: {
|
||||||
|
elems: [
|
||||||
|
ntImageElem(fileName),
|
||||||
|
customFaceElem(fileName, '&rkey=xxx&spec=0', otherMd5),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
expect(out).toHaveLength(1);
|
||||||
|
expect(out[0]).toMatchObject({
|
||||||
|
type: 'image',
|
||||||
|
fileId: fileName,
|
||||||
|
imageUrl: 'https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=EhRgood&rkey=good',
|
||||||
|
picFormat: 1001,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('drops a same-file customFace even when its legacy URL is well-formed', () => {
|
||||||
|
const out = decodeRichBody({
|
||||||
|
richText: {
|
||||||
|
elems: [
|
||||||
|
ntImageElem(fileName),
|
||||||
|
customFaceElem(fileName, '/gchatpic_new/0/0-0-AABB/0', otherMd5),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
expect(out).toEqual([
|
||||||
|
expect.objectContaining({ type: 'image', fileId: fileName, picFormat: 1001 }),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps a standalone customFace with a usable download path', () => {
|
||||||
|
const out = decodeRichBody({
|
||||||
|
richText: {
|
||||||
|
elems: [customFaceElem(fileName, '/gchatpic_new/0/0-0-AABB/0', otherMd5)],
|
||||||
|
},
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
expect(out).toEqual([{
|
||||||
|
type: 'image',
|
||||||
|
imageUrl: 'http://gchat.qpic.cn/gchatpic_new/0/0-0-AABB/0',
|
||||||
|
fileId: fileName,
|
||||||
|
fileSize: 10711,
|
||||||
|
width: 1080,
|
||||||
|
height: 1080,
|
||||||
|
subType: 0,
|
||||||
|
summary: '[图片]',
|
||||||
|
md5Hex: '0102030405060708090A0B0C0D0E0F10',
|
||||||
|
}]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('drops a standalone customFace whose URL cannot be fetched', () => {
|
||||||
|
const out = decodeRichBody({
|
||||||
|
richText: {
|
||||||
|
elems: [customFaceElem(fileName, '&rkey=xxx&spec=0', otherMd5)],
|
||||||
|
},
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
expect(out).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps two distinct pictures', () => {
|
||||||
|
const second = 'other.png';
|
||||||
|
const out = decodeRichBody({
|
||||||
|
richText: {
|
||||||
|
elems: [
|
||||||
|
ntImageElem(fileName),
|
||||||
|
customFaceElem(second, '/gchatpic_new/0/0-0-CCDD/0', otherMd5),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
expect(out.map((el) => el.type === 'image' ? el.fileId : el.type)).toEqual([
|
||||||
|
fileName,
|
||||||
|
second,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('makeImageUrl', () => {
|
||||||
|
it('prefixes NT paths that already include a download path', () => {
|
||||||
|
expect(makeImageUrl('/download?appid=1407&fileid=abc&rkey=x'))
|
||||||
|
.toBe('https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=abc&rkey=x');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not glue the NT host onto a query fragment (#389)', () => {
|
||||||
|
expect(makeImageUrl('&rkey=xxx&spec=0')).toBe('');
|
||||||
|
expect(makeImageUrl('rkey=xxx&spec=0')).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('prefixes legacy gchat paths', () => {
|
||||||
|
expect(makeImageUrl('/gchatpic_new/0/0-0-AABB/0'))
|
||||||
|
.toBe('http://gchat.qpic.cn/gchatpic_new/0/0-0-AABB/0');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('bounds retained card output across the entire message', () => {
|
it('bounds retained card output across the entire message', () => {
|
||||||
const previousLevel = getLogLevel();
|
const previousLevel = getLogLevel();
|
||||||
const captured: Array<{ scope: string; message: string }> = [];
|
const captured: Array<{ scope: string; message: string }> = [];
|
||||||
|
|||||||
Reference in new issue
Block a user