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

+33 -11
View File
@@ -1,6 +1,7 @@
import { mapWithConcurrency } from '@snowluma/common/concurrency';
import { createLogger } from '@snowluma/common/logger';
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
import { datalineFriendListEntries, findDatalineDeviceByUin } from '@snowluma/protocol/dataline/device-contacts';
import {
formatGroupRequestFlag,
type GroupMemberInfo,
@@ -45,21 +46,31 @@ async function fetchSingleGroupMembers(
}
}
function toFriendRow(friend: { uin: number; nickname: string; remark: string }): JsonObject {
return {
user_id: friend.uin,
nickname: friend.nickname,
remark: friend.remark,
};
}
export async function getFriendList(bridge: BridgeInterface): Promise<JsonObject[]> {
let rows: JsonObject[];
try {
const friends = await bridge.apis.contacts.fetchFriendList();
return friends.map(f => ({
user_id: f.uin,
nickname: f.nickname,
remark: f.remark,
}));
rows = (await bridge.apis.contacts.fetchFriendList()).map(toFriendRow);
} catch {
return bridge.identity.friends.map(f => ({
user_id: f.uin,
nickname: f.nickname,
remark: f.remark,
}));
rows = bridge.identity.friends.map(toFriendRow);
}
const occupied = new Set(rows.map((row) => Number(row.user_id)));
for (const extra of datalineFriendListEntries()) {
if (occupied.has(extra.user_id)) continue;
rows.push({
user_id: extra.user_id,
nickname: extra.nickname,
remark: extra.remark,
});
}
return rows;
}
export async function getGroupList(
@@ -332,6 +343,17 @@ export async function getStrangerInfo(
bridge: BridgeInterface,
userId: number,
): Promise<JsonObject | null> {
const device = findDatalineDeviceByUin(userId);
if (device) {
return {
user_id: device.uin,
nickname: device.name,
remark: device.name,
sex: 'unknown',
age: 0,
long_nick: '',
};
}
try {
const p = await bridge.apis.contacts.fetchUserProfile(userId);
const corp = await strangerCorpInfo(bridge, p);
@@ -1,5 +1,6 @@
import { createLogger } from '@snowluma/common/logger';
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
import { findDatalineDeviceByUin } from '@snowluma/protocol/dataline/device-contacts';
import type { ForwardNodePayload, FriendMessage, GroupMessage, MessageElement, MessageElementOf, QQEventVariant } from '@snowluma/protocol/events';
import { getVideoSourceSize, MAX_VIDEO_SIZE } from '@snowluma/protocol/highway/video-upload';
import { guessFileNameFromUrl } from '@snowluma/protocol/highway/utils';
@@ -870,6 +871,17 @@ export async function sendPrivateMessage(
);
}
if (findDatalineDeviceByUin(userId)) {
const unsupported = elements.find((element) => element.type !== 'text');
if (unsupported) {
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
`message element "${unsupported.type}" cannot be sent to this contact`,
unsupported.type,
);
}
}
// Temp sessions only have the message-element transport. File elements use
// the friend c2c upload path, so reject them before sending any preceding
// text/media batch; discovering this after sendText would create a partial
+23 -2
View File
@@ -5,6 +5,7 @@
import { describe, expect, it, vi } from 'vitest';
import type { BridgeInterface } from '../../src/bridge/bridge-interface';
import { datalineFriendListEntries, DATALINE_UIN_PAD } from '@snowluma/protocol/dataline/device-contacts';
import type {
FriendInfo, GroupMemberInfo, GroupRequestInfo, QQGroupInfo, UserProfileInfo,
} from '@snowluma/protocol/qq-info';
@@ -158,7 +159,10 @@ describe('onebot/contact-actions / getFriendList', () => {
fetchFriendList: vi.fn(async () => [makeFriend(22222, 'alice', 'best-friend')]),
});
const out = await getFriendList(bridge);
expect(out).toEqual([{ user_id: 22222, nickname: 'alice', remark: 'best-friend' }]);
expect(out).toEqual([
{ user_id: 22222, nickname: 'alice', remark: 'best-friend' },
...datalineFriendListEntries(),
]);
expect(bridge.apis.contacts.fetchFriendList).toHaveBeenCalledOnce();
});
@@ -169,7 +173,10 @@ describe('onebot/contact-actions / getFriendList', () => {
identity: fakeIdentity({ friends: cached }),
});
const out = await getFriendList(bridge);
expect(out).toEqual([{ user_id: 33333, nickname: 'bob', remark: '' }]);
expect(out).toEqual([
{ user_id: 33333, nickname: 'bob', remark: '' },
...datalineFriendListEntries(),
]);
});
});
@@ -709,6 +716,20 @@ describe('onebot/contact-actions / getStrangerInfo', () => {
});
expect(await getStrangerInfo(bridge, 99999)).toBeNull();
});
it('returns the official my-device contact without a profile fetch', async () => {
const fetchUserProfile = vi.fn(async () => { throw new Error('should not fetch'); });
const bridge = fakeBridge({ fetchUserProfile });
expect(await getStrangerInfo(bridge, DATALINE_UIN_PAD)).toEqual({
user_id: DATALINE_UIN_PAD,
nickname: '我的Pad',
remark: '我的Pad',
sex: 'unknown',
age: 0,
long_nick: '',
});
expect(fetchUserProfile).not.toHaveBeenCalled();
});
});
describe('onebot/contact-actions / getGroupSystemMessages', () => {
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from 'vitest';
import { datalineFriendListEntries } from '@snowluma/protocol/dataline/device-contacts';
import type { GroupMemberInfo, GroupRequestInfo, QQGroupInfo } from '@snowluma/protocol/qq-info';
import { buildApiContext, type OneBotInstanceContext } from '../src/instance-context';
import {
@@ -688,6 +689,7 @@ describe('buildApiContext contact reads', () => {
await expect(api.getFriendList()).resolves.toEqual([
{ user_id: 22222, nickname: 'alice', remark: 'best-friend' },
...datalineFriendListEntries(),
]);
});