fix(protocol): preserve private voice playback on older clients

Keep private voice messages playable on older clients while retaining the existing voice message for current clients. Cover the reported upload response in a regression test.

Refs #614
This commit is contained in:
DrSmoothl committed 2026-10-31 18:28:05 +08:00
1 parent eb119ecb92
commit a557bf72e7
11 files changed
+223 -39

No files matched your search

+6 -10
View File
@@ -8,7 +8,7 @@ import type {
SsoReadedReportReq,
SsoReadedReportResp,
} from '@snowluma/proto-defs/oidb-actions/base';
import { buildSendElems } from '@snowluma/protocol/element-builder';
import { buildSendElems, encodePrivateRichText } from '@snowluma/protocol/element-builder';
import { assertWindowShakeSendPolicy } from '@snowluma/protocol/element-manifest';
import { FinalizeOfflineFile } from '@snowluma/protocol/oidb-services/group-file/finalize-offline-file';
import { datalineTextFromElements, encodeDatalineText } from '@snowluma/protocol/dataline/codec';
@@ -22,7 +22,7 @@ import {
fetchC2cRoamMessagePage,
fetchGroupMessageRange,
} from '@snowluma/protocol/msg-push';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import { type bytes, protobuf_decode, protobuf_encode } from '@snowluma/proton';
import { createLogger } from '@snowluma/common/logger';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import type { BridgeContext } from '../bridge-context';
@@ -548,15 +548,15 @@ export class MessageApi {
userUid = await this.ctx.resolveUserUid(userUin);
}
const protoElems = await buildSendElems(elements, {
bridge: this.ctx as unknown as Bridge,
const richText = await encodePrivateRichText(elements, {
bridge: this.ctx,
userUid,
scene: 'direct-private',
});
const random = this.ctx.nextMessageRandom();
const clientSeq = this.ctx.nextClientSequence();
const request = protobuf_encode<SendMessageRequest>({
const request = protobuf_encode<SendMessageRequest<bytes>>({
routingHead: {
c2c: {
uin: userUin,
@@ -568,11 +568,7 @@ export class MessageApi {
subType: 0,
c2cCmd: 11,
},
messageBody: {
richText: {
elems: protoElems,
},
},
messageBody: { richText },
clientSequence: clientSeq,
random,
syncCookie: new Uint8Array(0),
@@ -6,7 +6,7 @@ import { ForwardApi } from '../../src/bridge/apis/forward';
import { mockBridge } from './_helpers';
vi.mock('@snowluma/protocol/highway/image-upload', () => ({ uploadImageMsgInfo: vi.fn() }));
vi.mock('@snowluma/protocol/highway/ptt-upload', () => ({ uploadPttMsgInfo: vi.fn() }));
vi.mock('@snowluma/protocol/highway/ptt-upload', () => ({ uploadPtt: vi.fn() }));
vi.mock('@snowluma/protocol/highway/video-upload', () => ({ uploadVideoMsgInfo: vi.fn() }));
describe('forward metadata through upload and cached reads', () => {
@@ -33,7 +33,7 @@ vi.mock('@snowluma/protocol/highway/image-upload', () => ({
uploadImageMsgInfo: vi.fn(async () => new Uint8Array()),
}));
vi.mock('@snowluma/protocol/highway/ptt-upload', () => ({
uploadPttMsgInfo: vi.fn(async () => new Uint8Array()),
uploadPtt: vi.fn(async () => ({ msgInfo: new Uint8Array() })),
}));
vi.mock('@snowluma/protocol/highway/video-upload', () => ({
uploadVideoMsgInfo: vi.fn(async () => new Uint8Array()),
@@ -2,7 +2,11 @@ import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import type { DatalineMsgBody, DatalineTextMsg } from '@snowluma/proto-defs/dataline';
import type { SendMessageRequest, SendMessageResponse } from '@snowluma/proto-defs/action';
import type {
SendMessageRequest,
SendMessageResponse,
SendRichText,
} from '@snowluma/proto-defs/action';
import type { FileExtra } from '@snowluma/proto-defs/message';
import { DATALINE_UIN_PHONE } from '@snowluma/protocol/dataline/device-contacts';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
@@ -10,6 +14,9 @@ import type { OidbOfflineFileFinalizeResp } from '@snowluma/proto-defs/oidb-acti
vi.mock('@snowluma/protocol/element-builder', () => ({
buildSendElems: vi.fn(async () => [{ text: { str: 'stub media elem' } }]),
encodePrivateRichText: vi.fn(async () =>
protobuf_encode<SendRichText>({ elems: [{ text: { str: 'stub media elem' } }] }),
),
}));
// Imported statically (not via `await import` inside each `it`) so the heavy
+35 -3
View File
@@ -1,3 +1,4 @@
import type { SendRichText } from '@snowluma/proto-defs/action';
// Regression test for the c2c-vs-group businessType asymmetry that made
// private-chat video / record sends bounce with `send private message
// rejected: result=79`. The receive-side decoder
@@ -22,15 +23,16 @@ vi.mock('@snowluma/protocol/highway/image-upload', () => ({
uploadImageMsgInfo: vi.fn(async () => new Uint8Array([7, 8, 9])),
}));
vi.mock('@snowluma/protocol/highway/ptt-upload', () => ({
uploadPttMsgInfo: vi.fn(async () => new Uint8Array([4, 5, 6])),
uploadPtt: vi.fn(async () => ({ msgInfo: new Uint8Array([4, 5, 6]) })),
}));
vi.mock('@snowluma/protocol/highway/video-upload', () => ({
uploadVideoMsgInfo: vi.fn(async () => new Uint8Array([1, 2, 3])),
}));
import { protobuf_encode } from '@snowluma/proton';
import { type bytes, type pb, protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { MsgInfo } from '@snowluma/proto-defs/element';
import { buildSendElems } from '@snowluma/protocol/element-builder';
import { buildSendElems, encodePrivateRichText } from '@snowluma/protocol/element-builder';
import { uploadPtt } from '@snowluma/protocol/highway/ptt-upload';
import { MessageElementValidationError } from '@snowluma/protocol/element-manifest';
import { uploadImageMsgInfo } from '@snowluma/protocol/highway/image-upload';
import { uploadVideoMsgInfo } from '@snowluma/protocol/highway/video-upload';
@@ -569,3 +571,33 @@ describe('element-builder / forward preview', () => {
).rejects.toBeInstanceOf(MessageElementValidationError);
});
});
describe('private voice compatibility', () => {
const context = { bridge: fakeBridge, userUid: 'u_peer', scene: 'direct-private' } as const;
const record = { type: 'record', url: 'voice.amr' } as const;
const modern = Uint8Array.of(4, 5, 6);
it('keeps all modern clips when one legacy voice slot cannot represent the message', async () => {
const compat = protobuf_encode<{ ptt?: pb<4, bytes> }>({ ptt: Uint8Array.of(8, 4) });
vi.mocked(uploadPtt)
.mockResolvedValueOnce({ msgInfo: modern, compatQMsg: compat })
.mockResolvedValueOnce({ msgInfo: modern, compatQMsg: compat });
const rich = await encodePrivateRichText([record, record], context);
expect(protobuf_decode<{ ptt?: pb<4, bytes> }>(rich).ptt).toBeNull();
expect(protobuf_decode<SendRichText>(rich).elems).toHaveLength(2);
});
it('sends modern voice when no compatibility data is available and rejects malformed data', async () => {
vi.mocked(uploadPtt).mockResolvedValueOnce({ msgInfo: modern });
expect(
protobuf_decode<SendRichText>(await encodePrivateRichText([record], context)).elems,
).toHaveLength(1);
vi.mocked(uploadPtt).mockResolvedValueOnce({
msgInfo: modern,
compatQMsg: Uint8Array.of(0x12, 0),
});
await expect(encodePrivateRichText([record], context)).rejects.toThrow(
'incomplete playback information',
);
});
});
@@ -0,0 +1,14 @@
08ed241064180022ef0412ec0432b9020af6010af3010a7d08fb3d122030303030303030303030303030303030303030
303030303030303030303030301a28303030303030303030303030303030303030303030303030303030303030303030
30303030303030222430303030303030303030303030303030303030303030303030303030303030302e616d722a0408
032001400312637878787878787878787878787878787878787878787878787878787878787878787878787878787878
787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878
7878787878787878787818012081808080012881800138fa0a123e0a0612047465737412001a3208818080800110012a
22081e121e0505050505050505050505050505050505050505050505050505050505055a040800380042ad02122baa02
289a0125aa0322081e121e05050505050505050505050505050505050505050505050505050505050522fd0108041081
808080011a97017878787878787878787878787878787878787878787878787878787878787878787878787878787878
787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878
787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878
78787878787878787878787878782210000000000000000000000000000000002a243030303030303030303030303030
3030303030303030303030303030303030302e616d7230fb3d3a16000000000000000000000000000000000000000000
005801980103e80101
@@ -0,0 +1,91 @@
import { readFileSync } from 'node:fs';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import type { SendMessageResponse } from '@snowluma/proto-defs/action';
import type { Elem } from '@snowluma/proto-defs/element';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import { IdentityService } from '@snowluma/protocol/identity-service';
import {
type bytes,
type pb,
type pb_repeated,
protobuf_decode,
protobuf_encode,
} from '@snowluma/proton';
import { expect, it } from 'vitest';
import { Bridge } from '../src/bridge/bridge';
interface VoiceParts {
elems?: pb_repeated<2, bytes>;
ptt?: pb<4, bytes>;
}
interface UploadReply {
upload?: pb<2, { compat?: pb<8, bytes> }>;
}
interface OutgoingMessage {
body?: pb<3, { richText?: pb<1, VoiceParts> }>;
}
it('preserves the private voice returned for older clients alongside the modern voice', async () => {
// The upload reply from #614, with identifiers redacted and transport credentials removed.
const upload = Buffer.from(
readFileSync(
new URL('./fixtures/issue-614-private-voice-upload.hex', import.meta.url),
'utf8',
).replace(/\s/g, ''),
'hex',
);
const captured = protobuf_decode<OidbBase<UploadReply>>(Uint8Array.from(upload)).body!.upload!
.compat!;
const expected = protobuf_decode<VoiceParts>(captured);
expect(captured).toHaveLength(301);
expect(expected.ptt).toHaveLength(253);
let sent: VoiceParts | undefined;
class VoiceBridge extends Bridge {
override async resolveUserUid(): Promise<string> {
return 'u_peer';
}
override async sendRawPacket(cmd: string, body: Uint8Array): Promise<SendPacketResult> {
let response: Uint8Array;
if (cmd === 'OidbSvcTrpcTcp.0x126d_100') {
response = upload;
} else if (cmd === 'MessageSvc.PbSendMsg') {
sent = protobuf_decode<OutgoingMessage>(body).body!.richText!;
response = protobuf_encode<SendMessageResponse>({ privateSequence: 88 });
} else {
throw new Error(`Unexpected packet ${cmd}`);
}
return {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.from(response),
};
}
}
const bridge = new VoiceBridge(IdentityService.memory('10000'));
await bridge.apis.message.sendPrivate(10001, [
{
type: 'record',
noByteFallback: true,
fileSize: 7931,
duration: 3,
md5Hex: '0'.repeat(32),
sha1Hex: '0'.repeat(40),
fileName: 'voice.amr',
},
]);
expect(protobuf_decode<Elem>(sent!.elems![0]!).commonElem).toMatchObject({
serviceType: 48,
businessType: 12,
});
expect(sent!.ptt).toEqual(expected.ptt);
expect(sent!.elems!.slice(1)).toEqual(expected.elems);
});