diff --git a/src/common/fall-back.ts b/src/common/fall-back.ts index c85e6b45..dc47919c 100644 --- a/src/common/fall-back.ts +++ b/src/common/fall-back.ts @@ -1,7 +1,13 @@ type Handler = () => T | Promise; +type Checker = (result: T) => T | Promise; export class Fallback { private handlers: Handler[] = []; + private checker: Checker; + + constructor(checker?: Checker) { + this.checker = checker || (async (result: T) => result); + } add(handler: Handler): this { this.handlers.push(handler); @@ -14,8 +20,10 @@ export class Fallback { for (const handler of this.handlers) { try { const result = await handler(); - if (result !== undefined) { - return result; + try { + return await this.checker(result); + } catch (checkerError) { + errors.push(checkerError instanceof Error ? checkerError : new Error(String(checkerError))); } } catch (error) { errors.push(error instanceof Error ? error : new Error(String(error))); @@ -23,4 +31,13 @@ export class Fallback { } throw new AggregateError(errors, 'All handlers failed'); } +} +export class FallbackUtil{ + static boolchecker(value: T, condition: boolean): T { + if (condition) { + return value; + } else { + throw new Error('Condition is false, throwing error'); + } + } } \ No newline at end of file diff --git a/src/core/apis/user.ts b/src/core/apis/user.ts index 636c05b0..c6add838 100644 --- a/src/core/apis/user.ts +++ b/src/core/apis/user.ts @@ -4,7 +4,7 @@ import { InstanceContext, NapCatCore, ProfileBizType } from '..'; import { solveAsyncProblem } from '@/common/helper'; import { promisify } from 'node:util'; import { LRUCache } from '@/common/lru-cache'; -import { Fallback } from '@/common/fall-back'; +import { Fallback, FallbackUtil } from '@/common/fall-back'; export class NTQQUserApi { context: InstanceContext; @@ -175,16 +175,13 @@ export class NTQQUserApi { return ''; } - let isValidUin = (uin: string | undefined) => { - if (uin !== undefined && uin !== '0' && uin !== '') { return uin; } throw new Error('uin is invalid'); - } + const fallback = + new Fallback((uid) => FallbackUtil.boolchecker(uid, uid !== undefined && uid.indexOf('*') === -1 && uid !== '')) + .add(() => this.context.session.getUixConvertService().getUid([uin]).then((data) => data.uidInfo.get(uin))) + .add(() => this.context.session.getProfileService().getUidByUin('FriendsServiceImpl', [uin]).get(uin)) + .add(() => this.context.session.getGroupService().getUidByUins([uin]).then((data) => data.uids.get(uin))) + .add(() => this.getUserDetailInfoByUin(uin).then((data) => data.detail.uid)); - const fallback = new Fallback() - .add(async () => isValidUin(await this.context.session.getUixConvertService().getUid([uin]).then((data) => data.uidInfo.get(uin)))) - .add(() =>isValidUin(this.context.session.getProfileService().getUidByUin('FriendsServiceImpl', [uin]).get(uin))) - .add(async () => isValidUin(await this.context.session.getGroupService().getUidByUins([uin]).then((data) => data.uids.get(uin)))) - .add(async () => isValidUin(await this.getUserDetailInfoByUin(uin).then((data) => data.detail.uid))); - const uid = await fallback.run().catch(() => '0'); return uid ?? ''; } @@ -194,18 +191,11 @@ export class NTQQUserApi { return '0'; } - let isValidUid = (uid: string | undefined) => { - if (uid !== undefined && uid.indexOf('*') === -1 && uid !== '') { - return uid; - } - throw new Error('uid is invalid'); - } - - const fallback = new Fallback() - .add(async () => isValidUid(await this.context.session.getUixConvertService().getUin([uid]).then((data) => data.uinInfo.get(uid)))) - .add(() =>isValidUid(this.context.session.getProfileService().getUinByUid('FriendsServiceImpl', [uid]).get(uid))) - .add(async () => isValidUid(await this.context.session.getGroupService().getUinByUids([uid]).then((data) => data.uins.get(uid)))) - .add(async () => isValidUid(await this.getUserDetailInfo(uid).then((data) => data.uin))); + const fallback = new Fallback((uin) => FallbackUtil.boolchecker(uin, uin !== undefined && uin !== '0' && uin !== '')) + .add(() => this.context.session.getUixConvertService().getUin([uid]).then((data) => data.uinInfo.get(uid))) + .add(() => this.context.session.getProfileService().getUinByUid('FriendsServiceImpl', [uid]).get(uid)) + .add(() => this.context.session.getGroupService().getUinByUids([uid]).then((data) => data.uins.get(uid))) + .add(() => this.getUserDetailInfo(uid).then((data) => data.uin)); const uin = await fallback.run().catch(() => '0'); return uin ?? '0';