feat(protocol): send and receive official my-device chats

Pad and phone device conversations were missing from the friend list
and arrived as empty private messages. They now show up as stable
contacts and support text send/receive.

Fixes #452
This commit is contained in:
DrSmoothl committed 2026-09-14 19:34:42 +08:00
1 parent 60c9669abd
commit dac3deb7bd
16 files changed
+692 -17

No files matched your search

@@ -12,6 +12,7 @@ import type {
// namespaces under @snowluma/protocol/oidb-services/friend. Tests assert
// against the bridge mock's sendRawPacket directly — no need for
// module-level bridge-oidb mocks anymore.
import { DATALINE_UIN_PAD } from '@snowluma/protocol/dataline/device-contacts';
import { FriendApi } from '../../src/bridge/apis/friend';
import { mockBridge } from './_helpers';
@@ -89,6 +90,14 @@ describe('apis/friend', () => {
.toHaveBeenCalledWith('resolved-uid', 10001, 'best-friend');
});
it('refuses to delete or rename a my-device contact', async () => {
const bridge = mockBridge();
const api = new FriendApi(bridge as any);
await expect(api.delete(DATALINE_UIN_PAD)).rejects.toThrow('cannot be deleted');
await expect(api.setRemark(DATALINE_UIN_PAD, 'pad')).rejects.toThrow('does not support remarks');
expect(bridge.sendRawPacket).not.toHaveBeenCalled();
});
it('setRemark uses the dedicated clear command for an empty remark', async () => {
const bridge = mockBridge();
await new FriendApi(bridge as any).setRemark(10001, '');
@@ -1,8 +1,10 @@
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 { FileExtra } from '@snowluma/proto-defs/message';
import { DATALINE_UIN_PHONE } from '@snowluma/protocol/dataline/device-contacts';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbOfflineFileFinalizeResp } from '@snowluma/proto-defs/oidb-actions/media';
@@ -201,4 +203,62 @@ describe('Bridge private media routing', () => {
expect(fileExtra?.file?.fileUuid).toBe('uuid-abc-123');
expect(fileExtra?.field6 ?? undefined).toBeUndefined();
});
it('sends my-device text through trans0x211 instead of ordinary c2c', async () => {
class TestBridge extends Bridge {
capturedBody: Uint8Array | null = null;
override async sendRawPacket(serviceCmd: string, body: Uint8Array): Promise<SendPacketResult> {
expect(serviceCmd).toBe('MessageSvc.PbSendMsg');
this.capturedBody = body;
return {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.from(protobuf_encode<SendMessageResponse>({
result: 0,
errMsg: '',
privateSequence: 88,
timestamp1: 1710000000,
})),
};
}
}
const bridge = new TestBridge(IdentityService.memory('10000'));
await bridge.apis.message.sendPrivate(DATALINE_UIN_PHONE, [{ type: 'text', text: 'hello phone' }]);
const request = protobuf_decode<SendMessageRequest>(bridge.capturedBody as Uint8Array);
expect(request?.routingHead?.trans0x211).toMatchObject({
ccCmd: 7,
uid: 'u_Wcc5rknRRqRO8y5gxMD6sA',
toUin: 10000n,
});
expect(request?.routingHead?.c2c ?? undefined).toBeUndefined();
const body = protobuf_decode<DatalineMsgBody>(request!.messageBody!.msgContent as Uint8Array);
expect(body?.subCmd).toBe(4);
expect(body?.header).toMatchObject({
srcAppId: 1,
dstAppId: 1001,
srcTerType: 1,
dstTerType: 2,
srcUin: 10000n,
dstUin: 10000n,
});
const text = protobuf_decode<DatalineTextMsg>(body!.generic!.buf as Uint8Array);
expect(new TextDecoder().decode(text!.items![0]!.text as Uint8Array)).toBe('hello phone');
});
it('rejects non-text my-device sends', async () => {
class TestBridge extends Bridge {
override async sendRawPacket(): Promise<SendPacketResult> {
throw new Error('should not send');
}
}
const bridge = new TestBridge(IdentityService.memory('10000'));
await expect(bridge.apis.message.sendPrivate(DATALINE_UIN_PHONE, [
{ type: 'image', url: 'file:///tmp/a.png' } as any,
])).rejects.toThrow('only accepts text');
});
});