From 37bd51e138de91aea63dd2f5633cd09a306e5bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 24 Jul 2024 10:42:22 +0800 Subject: [PATCH] refactor: getUserInfo --- src/core/src/apis/file.ts | 1 - src/core/src/apis/user.ts | 107 +++++------------- src/onebot11/action/go-cqhttp/DownloadFile.ts | 7 +- 3 files changed, 34 insertions(+), 81 deletions(-) diff --git a/src/core/src/apis/file.ts b/src/core/src/apis/file.ts index 2f6727b7..1794ef43 100644 --- a/src/core/src/apis/file.ts +++ b/src/core/src/apis/file.ts @@ -15,7 +15,6 @@ import * as fileType from 'file-type'; import imageSize from 'image-size'; import { ISizeCalculationResult } from 'image-size/dist/types/interface'; import { sessionConfig } from '@/core/sessionConfig'; -import { randomUUID } from 'crypto'; import { rkeyManager } from '../utils/rkey'; import { NTEventDispatch } from '@/common/utils/EventTask'; diff --git a/src/core/src/apis/user.ts b/src/core/src/apis/user.ts index 5cca854c..be305e05 100644 --- a/src/core/src/apis/user.ts +++ b/src/core/src/apis/user.ts @@ -1,52 +1,26 @@ import { ModifyProfileParams, SelfInfo, User, UserDetailInfoByUin } from '@/core/entities'; import { friends, selfInfo } from '@/core/data'; import { CacheClassFuncAsync, CacheClassFuncAsyncExtend } from '@/common/utils/helper'; -import { GeneralCallResult, napCatCore, NTQQFriendApi } from '@/core'; -import { ProfileListener } from '@/core/listeners'; -import { rejects } from 'assert'; -import { randomUUID } from 'crypto'; +import { napCatCore } from '@/core'; +import { NodeIKernelProfileListener, ProfileListener } from '@/core/listeners'; import { RequestUtil } from '@/common/utils/request'; -import { log, logDebug, logError, logWarn } from '@/common/utils/log'; +import { logWarn } from '@/common/utils/log'; import { NTEventDispatch } from '@/common/utils/EventTask'; -const userInfoCache: Record = {}; // uid: User +import { NodeIKernelProfileService } from '@/core/services'; -const profileListener = new ProfileListener(); - -const userDetailHandlers: Map void)> = new Map(); -profileListener.onProfileDetailInfoChanged = (profile) => { - userInfoCache[profile.uid] = profile; - userDetailHandlers.forEach(handler => handler(profile)); -}; -setTimeout(() => { - napCatCore.onLoginSuccess(() => { - napCatCore.addListener(profileListener); - }); -}, 100); -// 老版本逻辑现已移除 -// console.log('onProfileDetailInfoChanged', profile); -// recevCount++; -// firstProfile = profile; -// if (recevCount === 2) { -// profileService.removeKernelProfileListener(listenerId); -// // if (!completed) { -// completed = true; -// resolve(profile); -// // } -// } -// }; export class NTQQUserApi { static async getProfileLike(uid: string) { return napCatCore.session.getProfileLikeService().getBuddyProfileLike({ - "friendUids": [ + friendUids: [ uid ], - "basic": 1, - "vote": 1, - "favorite": 0, - "userProfile": 1, - "type": 2, - "start": 0, - "limit": 20 + basic: 1, + vote: 1, + favorite: 0, + userProfile: 1, + type: 2, + start: 0, + limit: 20 }); } static async setLongNick(longNick: string) { @@ -87,45 +61,26 @@ export class NTQQUserApi { // KQZONE, // KOTHER // } - static async getUserDetailInfo(uid: string): Promise { - // const existUser = userInfoCache[uid]; - // if (existUser) { - // return existUser; - // } - const profileService = napCatCore.session.getProfileService(); - // console.log('getUserDetailInfo', result); - return new Promise((resolve, reject) => { - const uuid = randomUUID(); - let completed = false; - let retData: User | undefined = undefined; - let isFirst = true; - // 不管返回几次 超时有数据就该返回 兼容就好了 - setTimeout(() => { - if (!completed) { - if (retData) { - resolve(retData); - } else { - reject('getUserDetailInfo timeout'); + static async getUserDetailInfo(uid: string) { + type EventService = NodeIKernelProfileService['getUserDetailInfoWithBizInfo']; + type EventListener = NodeIKernelProfileListener['onProfileDetailInfoChanged']; + let [_retData, profile] = await NTEventDispatch.CallNormalEvent + + ( + 'NodeIKernelProfileService/getUserDetailInfoWithBizInfo', + 'NodeIKernelProfileListener/onProfileDetailInfoChanged', + 2, + 5000, + (profile: User) => { + if (profile.uid === uid) { + return true; } - } - userDetailHandlers.delete(uuid); - }, 5000); - userDetailHandlers.set(uuid, (profile) => { - if (profile.uid === uid) { - if (isFirst) { - retData = profile; - isFirst = false; - // console.log('getUserDetailInfo', profile); - } else { - completed = true; - resolve(profile); - } - } - }); - profileService.getUserDetailInfoWithBizInfo(uid, [0]).then(result => { - // console.log('getUserDetailInfo', result); - }); - }); + return false; + }, + uid, + [0] + ); + return profile; } static async modifySelfProfile(param: ModifyProfileParams) { return napCatCore.session.getProfileService().modifyDesktopMiniProfile(param); diff --git a/src/onebot11/action/go-cqhttp/DownloadFile.ts b/src/onebot11/action/go-cqhttp/DownloadFile.ts index e6916c27..19fc9446 100644 --- a/src/onebot11/action/go-cqhttp/DownloadFile.ts +++ b/src/onebot11/action/go-cqhttp/DownloadFile.ts @@ -1,11 +1,10 @@ import BaseAction from '../BaseAction'; -import { ActionName, BaseCheckResult } from '../types'; +import { ActionName } from '../types'; import fs from 'fs'; import { join as joinPath } from 'node:path'; import { calculateFileMD5, getTempDir, httpDownload } from '@/common/utils/file'; -import { v4 as uuid4 } from 'uuid'; +import { randomUUID } from 'crypto'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import Ajv from 'ajv'; interface FileResponse { file: string; } @@ -32,7 +31,7 @@ export default class GoCQHTTPDownloadFile extends BaseAction { const isRandomName = !payload.name; - const name = payload.name || uuid4(); + const name = payload.name || randomUUID(); const filePath = joinPath(getTempDir(), name); if (payload.base64) {