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[] = [];
|
||||
|
||||
Reference in new issue
Block a user