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:
16 files changed
+692
-17
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { findDatalineDeviceByUin } from '@snowluma/protocol/dataline/device-contacts';
|
||||
import { ApproveDoubtBuddyReq } from '@snowluma/protocol/oidb-services/friend/approve-doubt-buddy-req';
|
||||
import { ClearFriendRemark } from '@snowluma/protocol/oidb-services/friend/clear-friend-remark';
|
||||
import { DeleteFriend } from '@snowluma/protocol/oidb-services/friend/delete-friend';
|
||||
@@ -24,6 +25,9 @@ export class FriendApi {
|
||||
}
|
||||
|
||||
async delete(userId: number, block = false): Promise<void> {
|
||||
if (findDatalineDeviceByUin(userId)) {
|
||||
throw new Error('this contact cannot be deleted');
|
||||
}
|
||||
await DeleteFriend.invoke(this.ctx, { userId, block });
|
||||
|
||||
// The server-side delete has already completed. A refresh failure must be
|
||||
@@ -40,11 +44,15 @@ export class FriendApi {
|
||||
}
|
||||
}
|
||||
|
||||
setRemark(userId: number, remark: string): Promise<void> {
|
||||
if (remark === '') {
|
||||
return ClearFriendRemark.invoke(this.ctx, { userId });
|
||||
async setRemark(userId: number, remark: string): Promise<void> {
|
||||
if (findDatalineDeviceByUin(userId)) {
|
||||
throw new Error('this contact does not support remarks');
|
||||
}
|
||||
return SetFriendRemark.invoke(this.ctx, { userId, remark });
|
||||
if (remark === '') {
|
||||
await ClearFriendRemark.invoke(this.ctx, { userId });
|
||||
return;
|
||||
}
|
||||
await SetFriendRemark.invoke(this.ctx, { userId, remark });
|
||||
}
|
||||
|
||||
/** List doubtful friend-add requests (可能认识的人). */
|
||||
|
||||
@@ -14,6 +14,14 @@ import type {
|
||||
import { buildSendElems } 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';
|
||||
import {
|
||||
findDatalineDeviceByUin,
|
||||
type DatalineDeviceContact,
|
||||
} from '@snowluma/protocol/dataline/device-contacts';
|
||||
import type { MessageElement, QQEventVariant } from '@snowluma/protocol/events';
|
||||
import {
|
||||
fetchC2cMessageRange,
|
||||
@@ -417,6 +425,8 @@ export class MessageApi {
|
||||
* (and optionally `c2c.uid` for the media case).
|
||||
*/
|
||||
async sendPrivate(userUin: number, elements: MessageElement[]): Promise<SendMessageReceipt> {
|
||||
const device = findDatalineDeviceByUin(userUin);
|
||||
if (device) return this.sendDatalineText(device, elements);
|
||||
if (elements.length === 0) throw new Error('message is empty');
|
||||
assertWindowShakeSendPolicy(
|
||||
elements.filter((element) => element.type === 'poke').length,
|
||||
@@ -483,6 +493,67 @@ export class MessageApi {
|
||||
return { messageId, sequence: seq, clientSequence: clientSeq, random, timestamp };
|
||||
}
|
||||
|
||||
/**
|
||||
* Send text to an official my-device contact. These peers are not
|
||||
* ordinary friends — they use the same trans0x211 send path as c2c
|
||||
* files, with a device-chat body instead of FileExtra.
|
||||
*/
|
||||
private async sendDatalineText(
|
||||
device: DatalineDeviceContact,
|
||||
elements: MessageElement[],
|
||||
): Promise<SendMessageReceipt> {
|
||||
const text = datalineTextFromElements(elements);
|
||||
const selfUin = parseInt(this.ctx.identity.uin, 10);
|
||||
if (!Number.isSafeInteger(selfUin) || selfUin <= 0) {
|
||||
throw new Error('self uin unavailable');
|
||||
}
|
||||
|
||||
const random = this.ctx.nextMessageRandom();
|
||||
const clientSeq = this.ctx.nextClientSequence();
|
||||
const nowSec = Math.floor(Date.now() / 1000);
|
||||
const request = protobuf_encode<SendMessageRequest>({
|
||||
routingHead: {
|
||||
trans0x211: {
|
||||
toUin: BigInt(selfUin),
|
||||
ccCmd: 7,
|
||||
uid: device.uid,
|
||||
},
|
||||
},
|
||||
contentHead: {
|
||||
type: 1,
|
||||
subType: 0,
|
||||
},
|
||||
messageBody: {
|
||||
msgContent: encodeDatalineText({
|
||||
selfUin,
|
||||
dstTerType: device.terType,
|
||||
text,
|
||||
}),
|
||||
},
|
||||
clientSequence: clientSeq,
|
||||
random,
|
||||
syncCookie: new Uint8Array(0),
|
||||
via: 0,
|
||||
dataStatist: 0,
|
||||
ctrl: { msgFlag: nowSec },
|
||||
multiSendSeq: 0,
|
||||
});
|
||||
|
||||
const result = await this.ctx.sendRawPacket(SEND_MSG_CMD, request);
|
||||
if (!result.success || !result.gotResponse || !result.responseData) {
|
||||
throw new Error(`send device message failed: ${result.errorMessage || 'no response'}`);
|
||||
}
|
||||
const response = protobuf_decode<SendMessageResponse>(result.responseData);
|
||||
if (!response) throw new Error('failed to decode SendMessageResponse');
|
||||
if (response.result != null && response.result !== 0) {
|
||||
throw new Error(`send device message rejected: result=${response.result} err=${response.errMsg ?? ''}`);
|
||||
}
|
||||
const seq = response.privateSequence ?? 0;
|
||||
const messageId = (random & 0x7FFFFFFF) || seq;
|
||||
const timestamp = response.timestamp1 ?? Math.floor(Date.now() / 1000);
|
||||
return { messageId, sequence: seq, clientSequence: clientSeq, random, timestamp };
|
||||
}
|
||||
|
||||
/**
|
||||
* Reply into an existing group temp session (临时会话). Wire primitive only —
|
||||
* it does NOT gate; the caller verifies the session before calling. Routes
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user