Skip to content

Commit

Permalink
feat: getUserDetailInfoV2
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed Jan 3, 2025
1 parent f7d0cb0 commit 67afd95
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/common/decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// decoratorAsyncMethod(this,function,wrapper)
async function decoratorMethod<T, R>(
target: T,
method: () => Promise<R>,
wrapper: (result: R) => Promise<any>,
executeImmediately: boolean = true
): Promise<any> {
const execute = async () => {
try {
const result = await method.call(target);
return wrapper(result);
} catch (error) {
return Promise.reject(error instanceof Error ? error : new Error(String(error)));
}
};

if (executeImmediately) {
return execute();
} else {
return execute;
}
}
15 changes: 13 additions & 2 deletions src/core/apis/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { ModifyProfileParams, User, UserDetailSource } from '@/core/types';
import { RequestUtil } from '@/common/request';
import { InstanceContext, NapCatCore, ProfileBizType } from '..';
import { solveAsyncProblem } from '@/common/helper';
import { promisify } from 'node:util';
import { LRUCache } from '@/common/lru-cache';
import { Fallback, FallbackUtil } from '@/common/fall-back';

export class NTQQUserApi {
Expand Down Expand Up @@ -109,6 +107,19 @@ export class NTQQUserApi {
return retUser;
}

async getUserDetailInfoV2(uid: string): Promise<User> {
const fallback = new Fallback<User>((user) => FallbackUtil.boolchecker(user, user !== undefined && user.uin !== '0'))
.add(() => this.fetchUserDetailInfo(uid, UserDetailSource.KDB))
.add(() => this.fetchUserDetailInfo(uid, UserDetailSource.KSERVER));
const retUser = await fallback.run().then(async (user) => {
if (user && user.uin === '0') {
user.uin = await this.core.apis.UserApi.getUidByUinV2(uid) ?? '0';
}
return user;
});
return retUser;
}

async modifySelfProfile(param: ModifyProfileParams) {
return this.context.session.getProfileService().modifyDesktopMiniProfile(param);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/apis/webapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
WebHonorType,
} from '@/core';
import { NapCatCore } from '..';
import { createReadStream, readFileSync, statSync } from 'node:fs';
import { readFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
import { basename } from 'node:path';

Expand Down
7 changes: 7 additions & 0 deletions src/onebot/api/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,13 @@ export class OneBotMsgApi {

private async handlePrivateMessage(resMsg: OB11Message, msg: RawMessage) {
resMsg.sub_type = 'friend';
if (await this.core.apis.FriendApi.isBuddy(msg.senderUid)) {
let nickname = (await this.core.apis.UserApi.getCoreAndBaseInfo([msg.senderUid])).get(msg.senderUid)?.coreInfo.nick;
if (nickname) {
resMsg.sender.nickname = nickname;
return;
}
}
resMsg.sender.nickname = (await this.core.apis.UserApi.getUserDetailInfo(msg.senderUid)).nick;
}

Expand Down
3 changes: 0 additions & 3 deletions src/onebot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,6 @@ export class NapCatOneBot11Adapter {
this.context.logger.logDebug('有加群请求');
try {
let requestUin = await this.core.apis.UserApi.getUinByUidV2(notify.user1.uid);
if (isNaN(parseInt(requestUin))) {
requestUin = (await this.core.apis.UserApi.getUserDetailInfo(notify.user1.uid)).uin;
}
const groupRequestEvent = new OB11GroupRequestEvent(
this.core,
parseInt(notify.group.groupCode),
Expand Down

0 comments on commit 67afd95

Please sign in to comment.