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
|
||||
|
||||
Reference in new issue
Block a user