refactor: getUserInfo

This commit is contained in:
手瓜一十雪 2024-07-24 10:42:22 +08:00
parent c367728c43
commit 37bd51e138
3 changed files with 34 additions and 81 deletions

View File

@ -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';

View File

@ -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<string, User> = {}; // uid: User
import { NodeIKernelProfileService } from '@/core/services';
const profileListener = new ProfileListener();
const userDetailHandlers: Map<string, ((profile: User) => 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<User> {
// 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');
}
}
userDetailHandlers.delete(uuid);
}, 5000);
userDetailHandlers.set(uuid, (profile) => {
static async getUserDetailInfo(uid: string) {
type EventService = NodeIKernelProfileService['getUserDetailInfoWithBizInfo'];
type EventListener = NodeIKernelProfileListener['onProfileDetailInfoChanged'];
let [_retData, profile] = await NTEventDispatch.CallNormalEvent
<EventService, EventListener>
(
'NodeIKernelProfileService/getUserDetailInfoWithBizInfo',
'NodeIKernelProfileListener/onProfileDetailInfoChanged',
2,
5000,
(profile: User) => {
if (profile.uid === uid) {
if (isFirst) {
retData = profile;
isFirst = false;
// console.log('getUserDetailInfo', profile);
} else {
completed = true;
resolve(profile);
return true;
}
}
});
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);

View File

@ -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<Payload, FileRespon
PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<FileResponse> {
const isRandomName = !payload.name;
const name = payload.name || uuid4();
const name = payload.name || randomUUID();
const filePath = joinPath(getTempDir(), name);
if (payload.base64) {