fix(onebot): expose robot status for group members

Fetch QQ's versioned robot UIN ranges, classify and persist member status, and surface is_robot from both group member actions.

Preserve unknown legacy cache entries and fail visibly when classification is unavailable.

Closes #265
This commit is contained in:
DrSmoothl committed 2026-07-21 23:27:01 +08:00
1 parent ff60221330
commit eed2e692b1
16 files changed
+596 -36

No files matched your search

+118 -1
View File
@@ -5,9 +5,14 @@ import type {
OidbBase,
OidbFriend,
OidbFriendCategory,
OidbRobotUinRangeResponse,
OidbSvcTrpcTcp0xFD4_1Response,
OidbSvcTrpcTcp0xFE7_3Response,
} from '@snowluma/proto-defs/oidb';
import type { OidbFriendListRequest } from '@snowluma/proto-defs/oidb-actions/base';
import type {
OidbFriendListRequest,
OidbRobotUinRangeRequest,
} from '@snowluma/proto-defs/oidb-actions/base';
import { ContactsApi } from '../../src/bridge/apis/contacts';
@@ -55,6 +60,30 @@ function packet(body: OidbSvcTrpcTcp0xFD4_1Response): SendPacketResult {
};
}
function robotRangePacket(body: OidbRobotUinRangeResponse): SendPacketResult {
return {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.from(
protobuf_encode<OidbBase<OidbRobotUinRangeResponse>>({ body }),
),
};
}
function memberListPacket(body: OidbSvcTrpcTcp0xFE7_3Response): SendPacketResult {
return {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.from(
protobuf_encode<OidbBase<OidbSvcTrpcTcp0xFE7_3Response>>({ body }),
),
};
}
function apiForPages(pages: OidbSvcTrpcTcp0xFD4_1Response[]) {
let index = 0;
const sendRawPacket = vi.fn(async (
@@ -160,3 +189,91 @@ describe('apis/contacts / categorized friend roster', () => {
.rejects.toThrow('repeated friend-list cookie aa');
});
});
describe('apis/contacts / robot group-member classification', () => {
it('loads QQ robot ranges once and marks every fetched member', async () => {
const rememberGroupMembers = vi.fn();
const sendRawPacket = vi.fn(async (cmd: string, _data: Uint8Array): Promise<SendPacketResult> => {
if (cmd === 'OidbSvcTrpcTcp.0x496_0') {
return robotRangePacket({
robotConfig: {
version: 206,
ranges: [{ minUin: 3_889_000_000n, maxUin: 3_889_999_999n }],
},
});
}
if (cmd === 'OidbSvcTrpcTcp.0xfe7_3') {
return memberListPacket({
groupUin: 42,
members: [
{ uin: { uid: 'u_robot', uin: 3_889_054_356 }, memberName: 'robot' },
{ uin: { uid: 'u_person', uin: 1_234_567_890 }, memberName: 'person' },
],
});
}
throw new Error(`unexpected command: ${cmd}`);
});
const api = new ContactsApi({
sendRawPacket,
identity: { uin: '10001', rememberGroupMembers },
} as any);
const first = await api.fetchGroupMemberList(42);
const second = await api.fetchGroupMemberList(42, { force: true });
expect(first).toEqual([
expect.objectContaining({ uin: 3_889_054_356, isRobot: true }),
expect.objectContaining({ uin: 1_234_567_890, isRobot: false }),
]);
expect(second).toEqual(first);
expect(rememberGroupMembers).toHaveBeenCalledWith(42, first);
expect(sendRawPacket.mock.calls.map(([cmd]) => cmd).sort()).toEqual([
'OidbSvcTrpcTcp.0x496_0',
'OidbSvcTrpcTcp.0xfe7_3',
'OidbSvcTrpcTcp.0xfe7_3',
]);
expect(sendRawPacket.mock.calls.filter(([cmd]) => cmd === 'OidbSvcTrpcTcp.0x496_0'))
.toHaveLength(1);
const rangeCall = sendRawPacket.mock.calls.find(([cmd]) => cmd === 'OidbSvcTrpcTcp.0x496_0')!;
const request = protobuf_decode<OidbBase<OidbRobotUinRangeRequest>>(rangeCall[1]);
expect(request.body).toMatchObject({ justFetchMsgConfig: 1, type: 1 });
});
it('propagates an invalid range snapshot and retries it on the next fetch', async () => {
let rangeAttempts = 0;
const sendRawPacket = vi.fn(async (cmd: string, _data: Uint8Array): Promise<SendPacketResult> => {
if (cmd === 'OidbSvcTrpcTcp.0x496_0') {
rangeAttempts += 1;
return rangeAttempts === 1
? robotRangePacket({})
: robotRangePacket({
robotConfig: {
version: 206,
ranges: [{ minUin: 3_889_000_000n, maxUin: 3_889_999_999n }],
},
});
}
if (cmd === 'OidbSvcTrpcTcp.0xfe7_3') {
return memberListPacket({
groupUin: 42,
members: [{ uin: { uid: 'u_robot', uin: 3_889_054_356 }, memberName: 'robot' }],
});
}
throw new Error(`unexpected command: ${cmd}`);
});
const rememberGroupMembers = vi.fn();
const api = new ContactsApi({
sendRawPacket,
identity: { uin: '10001', rememberGroupMembers },
} as any);
await expect(api.fetchGroupMemberList(42))
.rejects.toThrow('0x496_0 response missing robot range config');
await expect(api.fetchGroupMemberList(42))
.resolves.toEqual([expect.objectContaining({ isRobot: true })]);
expect(rangeAttempts).toBe(2);
expect(rememberGroupMembers).toHaveBeenCalledOnce();
});
});
@@ -14,6 +14,7 @@ function makeGroupMember(uin: number, uid: string): GroupMemberInfo {
uid,
nickname: '',
card: '',
isRobot: false,
role: 'member',
level: 0,
title: '',
+78 -1
View File
@@ -51,12 +51,13 @@ function makeGroup(): QQGroupInfo {
};
}
function makeMember(uin: number, uid: string, card = ''): GroupMemberInfo {
function makeMember(uin: number, uid: string, card = '', isRobot = false): GroupMemberInfo {
return {
uin,
uid,
nickname: `nick-${uin}`,
card,
isRobot,
role: 'member',
level: 1,
title: '',
@@ -96,6 +97,82 @@ describe('IdentityService', () => {
}
});
it('persists group member robot classification across restarts', () => {
const dbPath = tempDbPath('persist-robot');
{
const identity = new IdentityService(SELF_UIN, dbPath);
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [makeMember(33333, 'u_robot', '', true)]);
identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
expect(identity.findGroupMember(GROUP_ID, 33333)?.isRobot).toBe(true);
identity.close();
}
});
it('migrates legacy member caches without inventing a robot classification', () => {
const dbPath = tempDbPath('migrate-robot');
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
const legacy = new DatabaseSync(dbPath);
legacy.exec(`
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
uid TEXT UNIQUE,
uin INTEGER UNIQUE,
nickname TEXT NOT NULL DEFAULT '',
remark TEXT NOT NULL DEFAULT '',
is_friend INTEGER NOT NULL DEFAULT 0,
source TEXT NOT NULL DEFAULT '',
updated_at INTEGER NOT NULL
);
CREATE TABLE groups (
group_id INTEGER PRIMARY KEY,
group_name TEXT NOT NULL DEFAULT '',
remark TEXT NOT NULL DEFAULT '',
member_count INTEGER NOT NULL DEFAULT 0,
member_max INTEGER NOT NULL DEFAULT 0,
active INTEGER NOT NULL DEFAULT 1,
updated_at INTEGER NOT NULL
);
CREATE TABLE group_members (
id INTEGER PRIMARY KEY AUTOINCREMENT,
group_id INTEGER NOT NULL,
uid TEXT,
uin INTEGER,
nickname TEXT NOT NULL DEFAULT '',
card TEXT NOT NULL DEFAULT '',
role TEXT NOT NULL DEFAULT 'member',
level INTEGER NOT NULL DEFAULT 0,
title TEXT NOT NULL DEFAULT '',
join_time INTEGER NOT NULL DEFAULT 0,
last_sent_time INTEGER NOT NULL DEFAULT 0,
shut_up_time INTEGER NOT NULL DEFAULT 0,
active INTEGER NOT NULL DEFAULT 1,
updated_at INTEGER NOT NULL,
UNIQUE(group_id, uid),
UNIQUE(group_id, uin)
);
INSERT INTO groups (group_id, group_name, updated_at)
VALUES (${GROUP_ID}, 'legacy-group', 1);
INSERT INTO group_members (group_id, uid, uin, nickname, updated_at)
VALUES (${GROUP_ID}, 'u_legacy', 33333, 'legacy-member', 1);
`);
legacy.close();
const identity = new IdentityService(SELF_UIN, dbPath);
expect(identity.findGroupMember(GROUP_ID, 33333)?.isRobot).toBeUndefined();
identity.close();
const migrated = new DatabaseSync(dbPath);
const columns = migrated.prepare('PRAGMA table_info(group_members)').all() as Array<{ name: string }>;
expect(columns.map((column) => column.name)).toContain('is_robot');
migrated.close();
});
it('keeps updateGroupMember changes after reopening the persistent cache', () => {
const dbPath = tempDbPath('update-member');
const member = makeMember(33333, 'u_member', 'old-card');
@@ -39,6 +39,7 @@ function makeGroupMember(uin: number, uid: string): GroupMemberInfo {
uid,
nickname: '',
card: '',
isRobot: false,
role: 'member',
level: 0,
title: '',