Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed Jan 3, 2025
1 parent be9b68a commit f7d0cb0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
21 changes: 19 additions & 2 deletions src/common/fall-back.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
type Handler<T> = () => T | Promise<T>;
type Checker<T> = (result: T) => T | Promise<T>;

export class Fallback<T> {
private handlers: Handler<T>[] = [];
private checker: Checker<T>;

constructor(checker?: Checker<T>) {
this.checker = checker || (async (result: T) => result);
}

add(handler: Handler<T>): this {
this.handlers.push(handler);
Expand All @@ -14,13 +20,24 @@ export class Fallback<T> {
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)));
}
}
throw new AggregateError(errors, 'All handlers failed');
}
}
export class FallbackUtil{
static boolchecker<T>(value: T, condition: boolean): T {
if (condition) {
return value;
} else {
throw new Error('Condition is false, throwing error');
}
}
}
34 changes: 12 additions & 22 deletions src/core/apis/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string | undefined>((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<string>()
.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 ?? '';
}
Expand All @@ -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<string>()
.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<string | undefined>((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';
Expand Down

0 comments on commit f7d0cb0

Please sign in to comment.