refactor: inline getApiContext()

This commit is contained in:
Wesley F. Young
2024-08-11 19:10:42 +08:00
parent e88c0346f3
commit 89e777f99e
71 changed files with 173 additions and 167 deletions

View File

@@ -1,14 +1,17 @@
import {
ChatType,
GeneralCallResult,
Group,
GroupMember,
GroupMemberRole,
GroupNotify,
GroupRequestOperateTypes,
InstanceContext,
MemberExtSourceType,
NapCatCore,
NodeIKernelGroupListener,
NodeIKernelGroupService,
} from '@/core';
import { GeneralCallResult, InstanceContext, NapCatCore, NodeIKernelGroupService } from '@/core';
import { isNumeric, runAllWithTimeout } from '@/common/utils/helper';
export class NTQQGroupApi {
@@ -164,7 +167,7 @@ export class NTQQGroupApi {
async getLatestMsg(GroupCode: string, uins: string[]) {
const uids: Array<string> = [];
for (const uin of uins) {
const uid = await this.core.getApiContext().UserApi.getUidByUin(uin);
const uid = await this.core.apis.UserApi.getUidByUin(uin);
if (uid) {
uids.push(uid);
}
@@ -313,7 +316,7 @@ export class NTQQGroupApi {
//需要异常处理
async uploadGroupBulletinPic(GroupCode: string, imageurl: string) {
const _Pskey = (await this.core.getApiContext().UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com')!;
const _Pskey = (await this.core.apis.UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com')!;
return this.context.session.getGroupService().uploadGroupBulletinPic(GroupCode, _Pskey, imageurl);
}
@@ -377,7 +380,7 @@ export class NTQQGroupApi {
width: number,
height: number
} | undefined = undefined, pinned: number = 0, confirmRequired: number = 0) {
const _Pskey = (await this.core.getApiContext().UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com');
const _Pskey = (await this.core.apis.UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com');
//text是content内容url编码
const data = {
text: encodeURI(content),

View File

@@ -210,7 +210,7 @@ export class NTQQMsgApi {
async sendMsg(peer: Peer, msgElements: SendMessageElement[], waitComplete = true, timeout = 10000) {
//唉? !我有个想法
if (peer.chatType === ChatType.temp && peer.guildId && peer.guildId !== '') {
let member = await this.core.ApiContext.GroupApi.getGroupMember(peer.guildId, peer.peerUid!);
let member = await this.core.apis.GroupApi.getGroupMember(peer.guildId, peer.peerUid!);
if (member) {
await this.PrepareTempChat(peer.peerUid, peer.guildId, member.nick);
}

View File

@@ -185,8 +185,8 @@ export class NTQQMusicSignApi {
// "view": "eventshare",
// "ver": "0.0.0.1"
// };
const data = (await this.core.getApiContext().UserApi.getQzoneCookies());
const Bkn = this.core.getApiContext().WebApi.getBknFromCookie(data.p_skey);
const data = (await this.core.apis.UserApi.getQzoneCookies());
const Bkn = this.core.apis.WebApi.getBknFromCookie(data.p_skey);
const CookieValue = 'p_skey=' + data.p_skey + '; skey=' + data.skey + '; p_uin=o' + this.core.selfInfo.uin + '; uin=o' + this.core.selfInfo.uin;

View File

@@ -238,9 +238,9 @@ export class NTQQUserApi {
uid = (await this.context.session.getUixConvertService().getUid([Uin])).uidInfo.get(Uin);
if (uid) return uid;
// console.log((await this.core.getApiContext().FriendApi.getBuddyIdMapCache(true)));
uid = (await this.core.getApiContext().FriendApi.getBuddyIdMapCache(true)).getValue(Uin);//从Buddy缓存获取Uid
uid = (await this.core.apis.FriendApi.getBuddyIdMapCache(true)).getValue(Uin);//从Buddy缓存获取Uid
if (uid) return uid;
uid = (await this.core.getApiContext().FriendApi.getBuddyIdMap(true)).getValue(Uin);
uid = (await this.core.apis.FriendApi.getBuddyIdMap(true)).getValue(Uin);
if (uid) return uid;
const unveifyUid = (await this.getUserDetailInfoByUinV2(Uin)).detail.uid;//从QQ Native 特殊转换
if (unveifyUid.indexOf('*') == -1) uid = unveifyUid;
@@ -256,9 +256,9 @@ export class NTQQUserApi {
if (uin) return uin;
uin = (await this.context.session.getUixConvertService().getUin([Uid])).uinInfo.get(Uid);
if (uin) return uin;
uin = (await this.core.getApiContext().FriendApi.getBuddyIdMapCache(true)).getKey(Uid);//从Buddy缓存获取Uin
uin = (await this.core.apis.FriendApi.getBuddyIdMapCache(true)).getKey(Uid);//从Buddy缓存获取Uin
if (uin) return uin;
uin = (await this.core.getApiContext().FriendApi.getBuddyIdMap(true)).getKey(Uid);
uin = (await this.core.apis.FriendApi.getBuddyIdMap(true)).getKey(Uid);
if (uin) return uin;
uin = (await this.getUserDetailInfo(Uid)).uin; //从QQ Native 转换
return uin;

View File

@@ -19,7 +19,7 @@ export class NTQQWebApi {
}
async shareDigest(groupCode: string, msgSeq: string, msgRandom: string, targetGroupCode: string) {
const cookieObject = await this.core.getApiContext().UserApi.getCookies('qun.qq.com');
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
const url = `https://qun.qq.com/cgi-bin/group_digest/share_digest?${
new URLSearchParams({
bkn: this.getBknFromCookie(cookieObject),
@@ -37,7 +37,7 @@ export class NTQQWebApi {
}
async getGroupEssenceMsg(GroupCode: string, page_start: string) {
const cookieObject = await this.core.getApiContext().UserApi.getCookies('qun.qq.com');
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
const url = `https://qun.qq.com/cgi-bin/group_digest/digest_list?${
new URLSearchParams({
bkn: this.getBknFromCookie(cookieObject),
@@ -62,7 +62,7 @@ export class NTQQWebApi {
async getGroupMembers(GroupCode: string, cached: boolean = true): Promise<WebApiGroupMember[]> {
//logDebug('webapi 获取群成员', GroupCode);
const memberData: Array<WebApiGroupMember> = new Array<WebApiGroupMember>();
const cookieObject = await this.core.getApiContext().UserApi.getCookies('qun.qq.com');
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
const retList: Promise<WebApiGroupMemberRet>[] = [];
const fastRet = await RequestUtil.HttpGetJson<WebApiGroupMemberRet>
(`https://qun.qq.com/cgi-bin/qun_mgr/search_group_members?${
@@ -123,7 +123,7 @@ export class NTQQWebApi {
// }
async setGroupNotice(GroupCode: string, Content: string) {
const cookieObject = await this.core.getApiContext().UserApi.getCookies('qun.qq.com');
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
let ret: any = undefined;
try {
ret = await RequestUtil.HttpGetJson<any>
@@ -144,7 +144,7 @@ export class NTQQWebApi {
}
async getGroupNotice(GroupCode: string): Promise<undefined | WebApiGroupNoticeRet> {
const cookieObject = await this.core.getApiContext().UserApi.getCookies('qun.qq.com');
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
let ret: WebApiGroupNoticeRet | undefined = undefined;
try {
ret = await RequestUtil.HttpGetJson<WebApiGroupNoticeRet>(`https://web.qun.qq.com/cgi-bin/announce/get_t_list?${
@@ -166,7 +166,7 @@ export class NTQQWebApi {
}
async getGroupHonorInfo(groupCode: string, getType: WebHonorType) {
const cookieObject = await this.core.getApiContext().UserApi.getCookies('qun.qq.com');
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
const getDataInternal = async (Internal_groupCode: string, Internal_type: number) => {
const url = `https://qun.qq.com/interactive/honorlist?${
new URLSearchParams({

View File

@@ -28,7 +28,7 @@ export function loadQQWrapper(QQVersion: string): WrapperNodeApi {
export class NapCatCore {
readonly context: InstanceContext;
readonly ApiContext: NTApiContext;
readonly apis: NTApiContext;
readonly eventWrapper: LegacyNTEventWrapper;
// readonly eventChannel: NTEventChannel;
NapCatDataPath: string;
@@ -43,7 +43,7 @@ export class NapCatCore {
this.context = context;
this.util = new this.context.wrapper.NodeQQNTWrapperUtil();
this.eventWrapper = new LegacyNTEventWrapper(context.wrapper, context.session);
this.ApiContext = {
this.apis = {
FileApi: new NTQQFileApi(this.context, this),
SystemApi: new NTQQSystemApi(this.context, this),
CollectionApi: new NTQQCollectionApi(this.context, this),
@@ -63,10 +63,6 @@ export class NapCatCore {
this.initNapCatCoreListeners().then().catch(this.context.logger.logError);
}
getApiContext() {
return this.ApiContext;
}
get dataPath(): string {
let result = this.util.getNTUserDataInfoConfig();
if (!result) {
@@ -107,13 +103,13 @@ export class NapCatCore {
groupListener.onGroupListUpdate = (updateType, groupList) => {
// console.log("onGroupListUpdate", updateType, groupList)
groupList.map(g => {
const existGroup = this.ApiContext.GroupApi.groupCache.get(g.groupCode);
const existGroup = this.apis.GroupApi.groupCache.get(g.groupCode);
//群成员数量变化 应该刷新缓存
if (existGroup && g.memberCount === existGroup.memberCount) {
Object.assign(existGroup, g);
}
else {
this.ApiContext.GroupApi.groupCache.set(g.groupCode, g);
this.apis.GroupApi.groupCache.set(g.groupCode, g);
// 获取群成员
}
const sceneId = this.context.session.getGroupService().createMemberListScene(g.groupCode, 'groupMemberList_MainWindow');
@@ -128,8 +124,8 @@ export class NapCatCore {
groupListener.onMemberListChange = (arg) => {
// todo: 应该加一个内部自己维护的成员变动callback用于判断成员变化通知
const groupCode = arg.sceneId.split('_')[0];
if (this.ApiContext.GroupApi.groupMemberCache.has(groupCode)) {
const existMembers = this.ApiContext.GroupApi.groupMemberCache.get(groupCode)!;
if (this.apis.GroupApi.groupMemberCache.has(groupCode)) {
const existMembers = this.apis.GroupApi.groupMemberCache.get(groupCode)!;
arg.infos.forEach((member, uid) => {
//console.log('onMemberListChange', member);
const existMember = existMembers.get(uid);
@@ -146,7 +142,7 @@ export class NapCatCore {
});
}
else {
this.ApiContext.GroupApi.groupMemberCache.set(groupCode, arg.infos);
this.apis.GroupApi.groupMemberCache.set(groupCode, arg.infos);
}
// console.log('onMemberListChange', groupCode, arg);
};
@@ -155,11 +151,11 @@ export class NapCatCore {
if (changeType === 0 && members.get(this.selfInfo.uid)?.isDelete) {
// 自身退群或者被踢退群 5s用于Api操作 之后不再出现
setTimeout(() => {
this.ApiContext.GroupApi.groupCache.delete(groupCode);
this.apis.GroupApi.groupCache.delete(groupCode);
}, 5000);
}
const existMembers = this.ApiContext.GroupApi.groupMemberCache.get(groupCode);
const existMembers = this.apis.GroupApi.groupMemberCache.get(groupCode);
if (existMembers) {
members.forEach((member, uid) => {
const existMember = existMembers.get(uid);
@@ -179,7 +175,7 @@ export class NapCatCore {
});
}
else {
this.ApiContext.GroupApi.groupMemberCache.set(groupCode, members);
this.apis.GroupApi.groupMemberCache.set(groupCode, members);
}
};
}