feat(onebot): support moving friends between categories
Add the set_friends_category action with either an existing category ID or an exact unique category name. Validate the current friend and category roster before applying a change, reject missing or ambiguous targets, and surface failed updates instead of reporting false success. Expose the action through the SDK types and cover validation, successful updates, repeat requests, service rejection, and registry completeness. Closes #297
This commit is contained in:
10 files changed
+429
-3
No files matched your search
@@ -13,6 +13,7 @@ import { FetchUserProfile } from '@snowluma/protocol/oidb-services/contacts/fetc
|
||||
import { FetchUserProfileByUid } from '@snowluma/protocol/oidb-services/contacts/fetch-user-profile-by-uid';
|
||||
import { GetBuddyRecommendArk } from '@snowluma/protocol/oidb-services/contacts/get-buddy-recommend-ark';
|
||||
import { GetGroupRecommendArk } from '@snowluma/protocol/oidb-services/contacts/get-group-recommend-ark';
|
||||
import { SetFriendCategory } from '@snowluma/protocol/oidb-services/contacts/set-friend-category';
|
||||
import { toHex } from '@snowluma/common/hex';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { createSingleFlightCache, type SingleFlightCache } from '@snowluma/common/single-flight-cache';
|
||||
@@ -60,6 +61,12 @@ interface FriendRoster {
|
||||
categories: FriendCategoryMeta[];
|
||||
}
|
||||
|
||||
export interface SetFriendCategoryParams {
|
||||
uin: number;
|
||||
categoryId?: number;
|
||||
categoryName?: string;
|
||||
}
|
||||
|
||||
export function buildFriendProperties(raw: FriendPropertySource): Map<number, string> {
|
||||
const props = new Map<number, string>();
|
||||
for (const additional of raw.additional ?? []) {
|
||||
@@ -225,6 +232,52 @@ export class ContactsApi {
|
||||
});
|
||||
}
|
||||
|
||||
async setFriendCategory(params: SetFriendCategoryParams): Promise<void> {
|
||||
const selectorCount = Number(params.categoryId !== undefined)
|
||||
+ Number(params.categoryName !== undefined);
|
||||
if (selectorCount !== 1) {
|
||||
throw new Error('exactly one of categoryId or categoryName is required');
|
||||
}
|
||||
const categories = await this.fetchFriendCategories();
|
||||
const friendCategory = categories.find(category =>
|
||||
category.friends.some(friend => friend.uin === params.uin));
|
||||
const friend = friendCategory?.friends.find(entry => entry.uin === params.uin);
|
||||
if (!friend) {
|
||||
throw new Error(`friend ${params.uin} is not in the live roster`);
|
||||
}
|
||||
if (!friend.uid) {
|
||||
throw new Error(`friend ${params.uin} has no UID in the live roster`);
|
||||
}
|
||||
|
||||
let target: FriendCategoryInfo | undefined;
|
||||
if (params.categoryId !== undefined) {
|
||||
target = categories.find(category => category.categoryId === params.categoryId);
|
||||
if (!target) {
|
||||
throw new Error(`friend category ${params.categoryId} does not exist`);
|
||||
}
|
||||
} else {
|
||||
const matches = categories.filter(category => category.categoryName === params.categoryName);
|
||||
if (matches.length === 0) {
|
||||
throw new Error(`friend category name "${params.categoryName}" does not exist`);
|
||||
}
|
||||
if (matches.length > 1) {
|
||||
throw new Error(`friend category name "${params.categoryName}" is ambiguous`);
|
||||
}
|
||||
target = matches[0]!;
|
||||
}
|
||||
|
||||
await SetFriendCategory.invoke(this.ctx, {
|
||||
uid: friend.uid,
|
||||
categoryId: target.categoryId,
|
||||
});
|
||||
log.info(
|
||||
'friend category updated: uin=%s friend=%d category=%d',
|
||||
this.ctx.identity.uin,
|
||||
params.uin,
|
||||
target.categoryId,
|
||||
);
|
||||
}
|
||||
|
||||
async fetchGroupList(): Promise<QQGroupInfo[]> {
|
||||
const resp = await FetchGroupList.invoke(this.ctx);
|
||||
const groups: QQGroupInfo[] = [];
|
||||
|
||||
@@ -3,6 +3,7 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import type {
|
||||
OidbBase,
|
||||
OidbEmpty,
|
||||
OidbFriend,
|
||||
OidbFriendCategory,
|
||||
OidbRobotUinRangeResponse,
|
||||
@@ -12,6 +13,7 @@ import type {
|
||||
import type {
|
||||
OidbFriendListRequest,
|
||||
OidbRobotUinRangeRequest,
|
||||
OidbSetFriendCategoryRequest,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
|
||||
import { ContactsApi } from '../../src/bridge/apis/contacts';
|
||||
@@ -102,6 +104,33 @@ function apiForPages(pages: OidbSvcTrpcTcp0xFD4_1Response[]) {
|
||||
return { api, sendRawPacket, rememberFriends };
|
||||
}
|
||||
|
||||
function apiForCategoryChange(roster: OidbSvcTrpcTcp0xFD4_1Response) {
|
||||
const sendRawPacket = vi.fn(async (
|
||||
command: string,
|
||||
_body: Uint8Array,
|
||||
): Promise<SendPacketResult> => {
|
||||
if (command === 'OidbSvcTrpcTcp.0xfd4_1') return packet(roster);
|
||||
if (command === 'OidbSvcTrpcTcp.0x1255_0') {
|
||||
return {
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<OidbBase<OidbEmpty>>({
|
||||
command: 0x1255,
|
||||
subCommand: 0,
|
||||
})),
|
||||
};
|
||||
}
|
||||
throw new Error(`unexpected command: ${command}`);
|
||||
});
|
||||
const api = new ContactsApi({
|
||||
sendRawPacket,
|
||||
identity: { uin: '10001', rememberFriends: vi.fn() },
|
||||
} as any);
|
||||
return { api, sendRawPacket };
|
||||
}
|
||||
|
||||
describe('apis/contacts / categorized friend roster', () => {
|
||||
it('keeps fetchFriendList flat while traversing cookie pages', async () => {
|
||||
const cookie = Uint8Array.from([0x01]);
|
||||
@@ -188,6 +217,95 @@ describe('apis/contacts / categorized friend roster', () => {
|
||||
await expect(api.fetchFriendCategories())
|
||||
.rejects.toThrow('repeated friend-list cookie aa');
|
||||
});
|
||||
|
||||
it('moves a live friend to a category selected by ID', async () => {
|
||||
const { api, sendRawPacket } = apiForCategoryChange({
|
||||
friends: [friend(10001, 0, 'u_friend', 'Alice')],
|
||||
categories: [
|
||||
category(0, '我的好友', 1, 0),
|
||||
category(7, 'Work', 0, 1),
|
||||
],
|
||||
});
|
||||
|
||||
await api.setFriendCategory({ uin: 10001, categoryId: 7 });
|
||||
|
||||
expect(sendRawPacket).toHaveBeenCalledTimes(2);
|
||||
const [command, bytes] = sendRawPacket.mock.calls[1]!;
|
||||
expect(command).toBe('OidbSvcTrpcTcp.0x1255_0');
|
||||
const envelope = protobuf_decode<OidbBase<OidbSetFriendCategoryRequest>>(bytes);
|
||||
expect(envelope.body).toEqual({ uid: 'u_friend', categoryId: 7 });
|
||||
});
|
||||
|
||||
it('resolves an exact unique category name before moving the friend', async () => {
|
||||
const { api, sendRawPacket } = apiForCategoryChange({
|
||||
friends: [friend(10001, 0, 'u_friend', 'Alice')],
|
||||
categories: [
|
||||
category(0, '我的好友', 1, 0),
|
||||
category(7, 'Work', 0, 1),
|
||||
],
|
||||
});
|
||||
|
||||
await api.setFriendCategory({ uin: 10001, categoryName: 'Work' });
|
||||
|
||||
const envelope = protobuf_decode<OidbBase<OidbSetFriendCategoryRequest>>(
|
||||
sendRawPacket.mock.calls[1]![1],
|
||||
);
|
||||
expect(envelope.body).toEqual({ uid: 'u_friend', categoryId: 7 });
|
||||
});
|
||||
|
||||
it('rejects unknown friends and categories without sending a mutation', async () => {
|
||||
const unknownFriend = apiForCategoryChange({
|
||||
friends: [friend(10001, 0, 'u_friend', 'Alice')],
|
||||
categories: [category(0, '我的好友', 1, 0)],
|
||||
});
|
||||
await expect(unknownFriend.api.setFriendCategory({
|
||||
uin: 10002,
|
||||
categoryId: 0,
|
||||
})).rejects.toThrow('friend 10002 is not in the live roster');
|
||||
expect(unknownFriend.sendRawPacket).toHaveBeenCalledTimes(1);
|
||||
|
||||
const unknownCategory = apiForCategoryChange({
|
||||
friends: [friend(10001, 0, 'u_friend', 'Alice')],
|
||||
categories: [category(0, '我的好友', 1, 0)],
|
||||
});
|
||||
await expect(unknownCategory.api.setFriendCategory({
|
||||
uin: 10001,
|
||||
categoryId: 7,
|
||||
})).rejects.toThrow('friend category 7 does not exist');
|
||||
expect(unknownCategory.sendRawPacket).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('rejects duplicate category names instead of selecting one arbitrarily', async () => {
|
||||
const { api, sendRawPacket } = apiForCategoryChange({
|
||||
friends: [friend(10001, 0, 'u_friend', 'Alice')],
|
||||
categories: [
|
||||
category(0, 'Work', 1, 0),
|
||||
category(7, 'Work', 0, 1),
|
||||
],
|
||||
});
|
||||
|
||||
await expect(api.setFriendCategory({
|
||||
uin: 10001,
|
||||
categoryName: 'Work',
|
||||
})).rejects.toThrow('friend category name "Work" is ambiguous');
|
||||
expect(sendRawPacket).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('rejects missing or conflicting selectors before reading the roster', async () => {
|
||||
const { api, sendRawPacket } = apiForCategoryChange({
|
||||
friends: [],
|
||||
categories: [],
|
||||
});
|
||||
|
||||
await expect(api.setFriendCategory({ uin: 10001 }))
|
||||
.rejects.toThrow('exactly one of categoryId or categoryName is required');
|
||||
await expect(api.setFriendCategory({
|
||||
uin: 10001,
|
||||
categoryId: 7,
|
||||
categoryName: 'Work',
|
||||
})).rejects.toThrow('exactly one of categoryId or categoryName is required');
|
||||
expect(sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('apis/contacts / robot group-member classification', () => {
|
||||
|
||||
Reference in new issue
Block a user