This commit is contained in:
手瓜一十雪
2025-01-03 21:07:26 +08:00
parent be9b68a0b1
commit f7d0cb0be7
2 changed files with 31 additions and 24 deletions

View File

@@ -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);
@@ -14,8 +20,10 @@ 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)));
@@ -24,3 +32,12 @@ export class Fallback<T> {
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');
}
}
}

View File

@@ -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,15 +175,12 @@ 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>()
.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 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 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<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';