From 5d649b3687e0980767c487a62ec1536518767784 Mon Sep 17 00:00:00 2001 From: Alen Date: Mon, 19 Aug 2024 00:55:32 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96API=E6=95=88=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化get_group_member_info效率 --- src/core/apis/group.ts | 39 +++++++++++++++---- src/core/apis/user.ts | 12 ++++-- src/core/entities/group.ts | 3 +- src/core/entities/user.ts | 1 + src/onebot/action/group/GetGroupMemberInfo.ts | 16 -------- src/onebot/helper/data.ts | 2 +- 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/core/apis/group.ts b/src/core/apis/group.ts index c700b9ee..237df7db 100644 --- a/src/core/apis/group.ts +++ b/src/core/apis/group.ts @@ -253,22 +253,45 @@ export class NTQQGroupApi { } async getGroupMemberV2(GroupCode: string, uid: string, forced = false) { - type ListenerType = NodeIKernelGroupListener['onMemberInfoChange']; + //type ListenerType = NodeIKernelGroupListener['onMemberInfoChange']; type EventType = NodeIKernelGroupService['getMemberInfo']; // NTEventDispatch.CreatListenerFunction('NodeIKernelGroupListener/onGroupMemberInfoUpdate', //return napCatCore.session.getGroupService().getMemberInfo(GroupCode, [uid], forced); - const [, , , _members] = await this.core.eventWrapper.CallNormalEvent + const Listener = this.core.eventWrapper.RegisterListen<(params: any) => void> ( - 'NodeIKernelGroupService/getMemberInfo', 'NodeIKernelGroupListener/onMemberInfoChange', 1, - 5000, - (groupCode: string, changeType: number, members: Map) => { - return groupCode == GroupCode && members.has(uid); + forced ? 5000 : 500, + (params) => { + return params === GroupCode; }, - GroupCode, [uid], forced, ); - return _members.get(uid); + const EventFunc = this.core.eventWrapper.createEventFunction('NodeIKernelGroupService/getMemberInfo'); + const retData = await EventFunc!(GroupCode, [uid], forced); + if (retData.result !== 0) { + throw new Error(`获取群成员信息失败: ${retData.errMsg}`); + } + const result = await Listener as unknown; + let member: GroupMember | undefined; + if (Array.isArray(result) && result?.[2] instanceof Map) { + let members = result[2] as Map; + member = members.get(uid); + }; + return member; + + // 原本的方法: (no_cache 下效率很高, cache 下效率一致) + // const [, , , _members] = await this.core.eventWrapper.CallNormalEvent + // ( + // 'NodeIKernelGroupService/getMemberInfo', + // 'NodeIKernelGroupListener/onMemberInfoChange', + // 1, + // 5000, + // (groupCode: string, changeType: number, members: Map) => { + // return groupCode == GroupCode && members.has(uid); + // }, + // GroupCode, [uid], forced, + // ); + // return _members.get(uid); } async getGroupMembers(groupQQ: string, num = 3000): Promise> { diff --git a/src/core/apis/user.ts b/src/core/apis/user.ts index 7766fb7b..f75fc340 100644 --- a/src/core/apis/user.ts +++ b/src/core/apis/user.ts @@ -100,7 +100,7 @@ export class NTQQUserApi { return retData; } - async fetchUserDetailInfo(uid: string) { + async fetchUserDetailInfo(uid: string, mode: UserDetailSource = UserDetailSource.KDB) { type EventService = NodeIKernelProfileService['fetchUserDetailInfo']; type EventListener = NodeIKernelProfileListener['onUserDetailInfoChanged']; const [_retData, profile] = await this.core.eventWrapper.CallNormalEvent( @@ -111,7 +111,7 @@ export class NTQQUserApi { (profile) => profile.uid === uid, 'BuddyProfileStore', [uid], - UserDetailSource.KSERVER, + mode, [ProfileBizType.KALL], ); const RetUser: User = { @@ -121,13 +121,19 @@ export class NTQQUserApi { ...profile.commonExt, ...profile.simpleInfo.baseInfo, qqLevel: profile.commonExt.qqLevel, + age: profile.simpleInfo.baseInfo.age, pendantId: '', }; return RetUser; } async getUserDetailInfo(uid: string) { - return this.fetchUserDetailInfo(uid); + const ret = await this.fetchUserDetailInfo(uid, UserDetailSource.KDB); + if (ret.uin === '0') { + console.log('[NapCat] [Mark] getUserDetailInfo Mode1 Failed.') + return await this.fetchUserDetailInfo(uid, UserDetailSource.KSERVER); + } + return ret; } async modifySelfProfile(param: ModifyProfileParams) { diff --git a/src/core/entities/group.ts b/src/core/entities/group.ts index a4aeea84..b926e1fb 100644 --- a/src/core/entities/group.ts +++ b/src/core/entities/group.ts @@ -1,4 +1,4 @@ -import { QQLevel, Sex } from './user'; +import { QQLevel, Sex, User } from './user'; export enum GroupListUpdateType { REFRESHALL, @@ -65,6 +65,7 @@ export interface GroupMember { uin: string; // QQ号 isRobot: boolean; sex?: Sex; + age?: number; qqLevel?: QQLevel; isChangeRole: boolean; joinTime: string; diff --git a/src/core/entities/user.ts b/src/core/entities/user.ts index 073f0cd8..3eb5a966 100644 --- a/src/core/entities/user.ts +++ b/src/core/entities/user.ts @@ -231,6 +231,7 @@ export interface User { longNick?: string; // 签名 remark?: string; sex?: Sex; + age?: number; qqLevel?: QQLevel; qid?: string; birthday_year?: number; diff --git a/src/onebot/action/group/GetGroupMemberInfo.ts b/src/onebot/action/group/GetGroupMemberInfo.ts index a628586c..e3fecfb5 100644 --- a/src/onebot/action/group/GetGroupMemberInfo.ts +++ b/src/onebot/action/group/GetGroupMemberInfo.ts @@ -38,22 +38,6 @@ class GetGroupMemberInfo extends BaseAction { } const date = Math.round(Date.now() / 1000); const retMember = OB11Constructor.groupMember(payload.group_id.toString(), member); - const SelfInfoInGroup = await NTQQGroupApi.getGroupMemberV2(payload.group_id.toString(), this.CoreContext.selfInfo.uid, isNocache); - let isPrivilege = false; - if (SelfInfoInGroup) { - isPrivilege = SelfInfoInGroup.role === 3 || SelfInfoInGroup.role === 4; - } - if (isPrivilege) { - const webGroupMembers = await NTQQWebApi.getGroupMembers(payload.group_id.toString()); - for (let i = 0, len = webGroupMembers.length; i < len; i++) { - if (webGroupMembers[i]?.uin && webGroupMembers[i].uin === retMember.user_id) { - retMember.join_time = webGroupMembers[i]?.join_time; - retMember.last_sent_time = webGroupMembers[i]?.last_speak_time; - retMember.qage = webGroupMembers[i]?.qage; - retMember.level = webGroupMembers[i]?.lv.level.toString(); - } - } - } retMember.last_sent_time = parseInt((await this.CoreContext.apis.GroupApi.getGroupMember(payload.group_id.toString(), retMember.user_id))?.lastSpeakTime || date.toString()); retMember.join_time = parseInt((await this.CoreContext.apis.GroupApi.getGroupMember(payload.group_id.toString(), retMember.user_id))?.joinTime || date.toString()); return retMember; diff --git a/src/onebot/helper/data.ts b/src/onebot/helper/data.ts index 359c642d..b9592b14 100644 --- a/src/onebot/helper/data.ts +++ b/src/onebot/helper/data.ts @@ -711,7 +711,7 @@ export class OB11Constructor { nickname: member.nick, card: member.cardName, sex: OB11Constructor.sex(member.sex!), - age: 0, + age: member.age ?? 0, area: '', level: '0', qq_level: member.qqLevel && calcQQLevel(member.qqLevel) || 0,