diff --git a/src/ntqqapi/api/friend.ts b/src/ntqqapi/api/friend.ts index d9bb92e..fd65f5a 100644 --- a/src/ntqqapi/api/friend.ts +++ b/src/ntqqapi/api/friend.ts @@ -101,52 +101,20 @@ export class NTQQFriendApi extends Service { return retMap } - async getBuddyV2ExWithCate(refresh = false) { - const session = getSession() - if (session) { - const uids: string[] = [] - const categoryMap: Map = new Map() - const buddyService = session.getBuddyService() - const buddyListV2 = (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data - uids.push( - ...buddyListV2.flatMap(item => { - item.buddyUids.forEach(uid => { - categoryMap.set(uid, { categoryId: item.categoryId, categroyName: item.categroyName }) - }) - return item.buddyUids - })) - const data = await session.getProfileService().getCoreAndBaseInfo('nodeStore', uids) - return Array.from(data).map(([key, value]) => { - const category = categoryMap.get(key) - return category ? { ...value, categoryId: category.categoryId, categroyName: category.categroyName } : value - }) - } else { - const data = await invoke<{ - buddyCategory: CategoryFriend[] - userSimpleInfos: Record - }>( - 'getBuddyList', - [refresh], - { - className: NTClass.NODE_STORE_API, - cbCmd: ReceiveCmdS.FRIENDS, - afterFirstCmd: false, - } - ) - const category: Map> = new Map() - for (const item of data.buddyCategory) { - category.set(item.categoryId, pick(item, ['buddyUids', 'categroyName'])) + async getBuddyV2WithCate(refresh = false) { + const data = await invoke<{ + buddyCategory: CategoryFriend[] + userSimpleInfos: Record + }>( + 'getBuddyList', + [refresh], + { + className: NTClass.NODE_STORE_API, + cbCmd: ReceiveCmdS.FRIENDS, + afterFirstCmd: false, } - return Object.values(data.userSimpleInfos) - .filter(v => v.baseInfo && category.get(v.baseInfo.categoryId)?.buddyUids.includes(v.uid!)) - .map(value => { - return { - ...value, - categoryId: value.baseInfo.categoryId, - categroyName: category.get(value.baseInfo.categoryId)?.categroyName - } - }) - } + ) + return data } async isBuddy(uid: string): Promise { diff --git a/src/onebot11/action/llonebot/GetFriendWithCategory.ts b/src/onebot11/action/llonebot/GetFriendWithCategory.ts index 7451d4f..6015f58 100644 --- a/src/onebot11/action/llonebot/GetFriendWithCategory.ts +++ b/src/onebot11/action/llonebot/GetFriendWithCategory.ts @@ -4,14 +4,35 @@ import { OB11Entities } from '../../entities' import { ActionName } from '../types' import { getBuildVersion } from '@/common/utils' -export class GetFriendWithCategory extends BaseAction { +interface Category { + categoryId: number + categorySortId: number + categoryName: string + categoryMbCount: number + onlineCount: number + buddyList: OB11User[] +} + +export class GetFriendWithCategory extends BaseAction { actionName = ActionName.GetFriendsWithCategory protected async _handle() { - if (getBuildVersion() >= 26702) { - return OB11Entities.friendsV2(await this.ctx.ntFriendApi.getBuddyV2ExWithCate(true)) - } else { + if (getBuildVersion() < 26702) { throw new Error('this ntqq version not support, must be 26702 or later') } + const data = await this.ctx.ntFriendApi.getBuddyV2WithCate(true) + return data.buddyCategory.map(item => { + return { + categoryId: item.categoryId, + categorySortId: item.categorySortId, + categoryName: item.categroyName, + categoryMbCount: item.categroyMbCount, + onlineCount: item.onlineCount, + buddyList: item.buddyUids.map(uid => { + const info = data.userSimpleInfos[uid] + return OB11Entities.friendV2(info) + }) + } + }) } } diff --git a/src/onebot11/entities.ts b/src/onebot11/entities.ts index fa99b1e..fbd1928 100644 --- a/src/onebot11/entities.ts +++ b/src/onebot11/entities.ts @@ -649,23 +649,20 @@ export namespace OB11Entities { return friends.map(friend) } - export function friendsV2(friends: FriendV2[]): OB11User[] { - const data: OB11User[] = [] - for (const friend of friends) { - const sexValue = sex(friend.baseInfo.sex!) - data.push({ - ...omit(friend.baseInfo, ['richBuffer']), - ...friend.coreInfo, - user_id: parseInt(friend.coreInfo.uin), - nickname: friend.coreInfo.nick, - remark: friend.coreInfo.nick, - sex: sexValue, - level: 0, - categroyName: friend.categroyName, - categoryId: friend.categoryId - }) + export function friendV2(raw: FriendV2): OB11User { + return { + ...omit(raw.baseInfo, ['richBuffer', 'phoneNum']), + ...omit(raw.coreInfo, ['nick']), + user_id: parseInt(raw.coreInfo.uin), + nickname: raw.coreInfo.nick, + remark: raw.coreInfo.remark || raw.coreInfo.nick, + sex: sex(raw.baseInfo.sex), + level: 0 } - return data + } + + export function friendsV2(raw: FriendV2[]): OB11User[] { + return raw.map(friendV2) } export function groupMemberRole(role: number): OB11GroupMemberRole | undefined {