fix(protocol): avoid duplicate private voice elements

Prefer a successfully decoded modern voice record over the empty legacy voice placeholder that can accompany private message pushes.

Preserve legacy-only voice messages and add a protobuf round-trip regression covering the duplicate segment reported in #291.

Fixes #291
This commit is contained in:
DrSmoothl committed 2026-07-28 22:49:06 +08:00
1 parent df3f8c5587
commit 52a3c49752
2 files changed
+56 -2

No files matched your search

@@ -870,8 +870,14 @@ function extractRichtextExtras(
elements: MessageElement[],
isGroup = false
): void {
// Ptt (voice)
if (rt.ptt) {
// QQ's PTT codec selects either the NTV2 CommonElem representation or the
// legacy RichText.ptt representation. Some C2C pushes still carry an empty
// legacy placeholder beside a complete NTV2 record; decoding both produces a
// second, unusable voice segment (#291).
const hasNtv2Record = elements.some((element) => element.type === 'record');
// Ptt (legacy voice)
if (rt.ptt && !hasNtv2Record) {
const p = rt.ptt;
const md5Hex = p.fileMd5 && p.fileMd5.length > 0 ? toHexUpper(p.fileMd5) : undefined;
const me: MessageElementOf<'record'> = {
@@ -252,6 +252,54 @@ describe('decodeRichBody / market face', () => {
});
describe('decodeRichBody / decoded element contract', () => {
it('[#291] prefers a decoded C2C media record over the empty legacy PTT placeholder', () => {
const fileHash = '469e23ff66ae3ebbf4c78d616e0df18b';
const fileSha1 = 'a00c7fbe3bfa92773ae17a2f6d381375810972e2';
const encoded = protobuf_encode<MessageBody>({
richText: {
elems: [{
commonElem: {
serviceType: 48,
businessType: 22,
pbElem: protobuf_encode<MsgInfo>({
msgInfoBody: [{
index: {
fileUuid: 'voice-uuid',
storeId: 1,
uploadTime: 1785116936,
ttl: 604800,
info: {
fileName: `${fileHash}.amr`,
fileSize: 2015,
fileHash,
fileSha1,
time: 1,
type: { type: 3, voiceFormat: 1 },
},
},
}],
}),
},
}],
ptt: {},
},
});
const out = decodeRichBody(protobuf_decode<MessageBody>(encoded), false);
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({
type: 'record',
fileName: `${fileHash}.amr`,
fileId: 'voice-uuid',
fileSize: 2015,
duration: 1,
voiceFormat: 1,
md5Hex: fileHash,
sha1Hex: fileSha1,
});
});
it('omits an absent PTT fingerprint instead of emitting an invalid empty hash', () => {
const body: MessageBody = {
richText: {