refctor: getBuddyV2 支持分类

This commit is contained in:
手瓜一十雪
2024-08-04 16:27:25 +08:00
parent 58f22b24e4
commit 690a2f7d34
5 changed files with 67 additions and 12 deletions

View File

@@ -1,22 +1,61 @@
import { FriendRequest, SimpleInfo, User } from '@/core/entities'; import { FriendRequest, FriendV2, SimpleInfo, User } from '@/core/entities';
import { BuddyListReqType, napCatCore, NodeIKernelBuddyListener, NodeIKernelProfileService, OnBuddyChangeParams } from '@/core'; import { BuddyListReqType, napCatCore, NodeIKernelBuddyListener, NodeIKernelProfileService, OnBuddyChangeParams } from '@/core';
import { NTEventDispatch } from '@/common/utils/EventTask'; import { NTEventDispatch } from '@/common/utils/EventTask';
export class NTQQFriendApi { export class NTQQFriendApi {
static async getBuddyV2(refresh = false): Promise<SimpleInfo[]> { static async getBuddyV2(refresh = false): Promise<FriendV2[]> {
// NTEventDispatch.RegisterListen<NodeIKernelBuddyListener['onBuddyListChange']>('NodeIKernelBuddyListener/onBuddyListChange', 1, 5000, (arg: OnBuddyChangeParams) => {
// console.log(arg);
// return true;
// }).catch().then();
let uids: string[]; let uids: string[];
let categoryMap: Map<string, any> = new Map();
if (!refresh) { if (!refresh) {
uids = (await napCatCore.session.getBuddyService().getBuddyListFromCache('0')).flatMap((item) => item.buddyUids); uids = (await napCatCore.session.getBuddyService().getBuddyListFromCache('0')).flatMap((item) => {
for (let i = 0; i < item.buddyUids.length; i++) {
categoryMap.set(item.buddyUids[i], { categoryId: item.categoryId, categroyName: item.categroyName });
}
return item.buddyUids;
});
} }
uids = (await (napCatCore.session.getBuddyService().getBuddyListV2('0', BuddyListReqType.KNOMAL))).data.flatMap((item) => item.buddyUids); uids = (await (napCatCore.session.getBuddyService().getBuddyListV2('0', BuddyListReqType.KNOMAL))).data.flatMap((item) => item.buddyUids);
let data = await NTEventDispatch.CallNoListenerEvent<NodeIKernelProfileService['getCoreAndBaseInfo']>('NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids); let data = await NTEventDispatch.CallNoListenerEvent<NodeIKernelProfileService['getCoreAndBaseInfo']>('NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids);
//遍历data //遍历data
let retArr = Array.from(data.values()); let retArr: FriendV2[] = [];
data.forEach((value, key) => {
let category = categoryMap.get(key);
if (category) {
retArr.push({
...value,
categoryId: category.categoryId,
categroyName: category.categroyName
});
}
})
return retArr; return retArr;
}
static async getBuddyV2Ex(refresh = false): Promise<FriendV2[]> {
let uids: string[] = [];
let categoryMap: Map<string, any> = new Map();
const buddyService = napCatCore.session.getBuddyService();
if (!refresh) {
const cachedBuddyList = await buddyService.getBuddyListFromCache('0');
cachedBuddyList.forEach(item => {
item.buddyUids.forEach(uid => {
categoryMap.set(uid, { categoryId: item.categoryId, categroyName: item.categroyName });
});
uids.push(...item.buddyUids);
});
}
const buddyListV2 = await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL);
uids.push(...buddyListV2.data.flatMap(item => item.buddyUids));
const data = await NTEventDispatch.CallNoListenerEvent<NodeIKernelProfileService['getCoreAndBaseInfo']>(
'NodeIKernelProfileService/getCoreAndBaseInfo', 5000, 'nodeStore', uids
);
return Array.from(data).map(([key, value]) => {
const category = categoryMap.get(key);
return category ? { ...value, categoryId: category.categoryId, categroyName: category.categroyName } : value;
});
} }
static async isBuddy(uid: string) { static async isBuddy(uid: string) {
return napCatCore.session.getBuddyService().isBuddy(uid); return napCatCore.session.getBuddyService().isBuddy(uid);

View File

@@ -173,7 +173,10 @@ export interface SimpleInfo {
otherFlags: any | null; otherFlags: any | null;
intimate: any | null; intimate: any | null;
} }
export interface FriendV2 extends SimpleInfo {
categoryId?: number;
categroyName?: string;
}
export interface UserDetailInfoListenerArg { export interface UserDetailInfoListenerArg {
uid: string; uid: string;
uin: string; uin: string;

View File

@@ -23,7 +23,7 @@ export default class GetFriendList extends BaseAction<Payload, OB11User[]> {
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
if (requireMinNTQQBuild('26702')) { if (requireMinNTQQBuild('26702')) {
//全新逻辑 //全新逻辑
return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2(payload?.no_cache === true || payload?.no_cache === 'true')); return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2Ex(payload?.no_cache === true || payload?.no_cache === 'true'));
} }
if (friends.size === 0 || payload?.no_cache === true || payload?.no_cache === 'true') { if (friends.size === 0 || payload?.no_cache === true || payload?.no_cache === 'true') {
const _friends = await NTQQFriendApi.getFriends(true); const _friends = await NTQQFriendApi.getFriends(true);

View File

@@ -14,6 +14,7 @@ import {
ChatType, ChatType,
FaceIndex, FaceIndex,
Friend, Friend,
FriendV2,
GrayTipElementSubType, GrayTipElementSubType,
Group, Group,
GroupMember, GroupMember,
@@ -553,11 +554,21 @@ export class OB11Constructor {
nickname: selfInfo.nick, nickname: selfInfo.nick,
}; };
} }
static friendsV2(friends: SimpleInfo[]): OB11User[] { static friendsV2(friends: FriendV2[]): OB11User[] {
const data: OB11User[] = []; const data: OB11User[] = [];
friends.forEach(friend => { friends.forEach(friend => {
const sexValue = this.sex(friend.baseInfo.sex!); const sexValue = this.sex(friend.baseInfo.sex!);
data.push({ ...friend.baseInfo, ...friend.coreInfo, user_id: parseInt(friend.coreInfo.uin), nickname: friend.coreInfo.nick, remark: friend.coreInfo.nick, sex: sexValue, level: 0 }); data.push({
...friend.baseInfo,
...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
});
}); });
return data; return data;
} }

View File

@@ -7,6 +7,8 @@ export interface OB11User {
age?: number; age?: number;
qid?: string; qid?: string;
login_days?: number; login_days?: number;
categroyName?:string;
categoryId?:number;
} }
export enum OB11UserSex { export enum OB11UserSex {