From eabe89183801dc5c4694ba1def65049411563cb9 Mon Sep 17 00:00:00 2001 From: idranme Date: Wed, 21 Aug 2024 23:36:35 +0800 Subject: [PATCH] opt --- src/ntqqapi/api/file.ts | 57 ++++++++++++++++++----------------- src/ntqqapi/api/friend.ts | 8 ++--- src/ntqqapi/api/group.ts | 21 +++++++------ src/ntqqapi/api/msg.ts | 11 ++++--- src/ntqqapi/api/rkey.ts | 2 -- src/ntqqapi/api/user.ts | 9 +++--- src/ntqqapi/api/window.ts | 11 ++++--- src/ntqqapi/hook.ts | 16 +++++----- src/ntqqapi/ntcall.ts | 29 ++++++++---------- src/ntqqapi/services/index.ts | 1 + 10 files changed, 83 insertions(+), 82 deletions(-) diff --git a/src/ntqqapi/api/file.ts b/src/ntqqapi/api/file.ts index 14a16d4..5a4bd24 100644 --- a/src/ntqqapi/api/file.ts +++ b/src/ntqqapi/api/file.ts @@ -1,4 +1,5 @@ -import { callNTQQApi, GeneralCallResult, NTQQApiClass, NTQQApiMethod } from '../ntcall' +import { invoke, NTClass, NTMethod } from '../ntcall' +import { GeneralCallResult } from '../services' import { CacheFileList, CacheFileListItem, @@ -144,9 +145,9 @@ export class NTQQFileApi { } static async getImageSize(filePath: string) { - return await callNTQQApi<{ width: number; height: number }>({ - className: NTQQApiClass.FS_API, - methodName: NTQQApiMethod.IMAGE_SIZE, + return await invoke<{ width: number; height: number }>({ + className: NTClass.FS_API, + methodName: NTMethod.IMAGE_SIZE, args: [filePath], }) } @@ -186,8 +187,8 @@ export class NTQQFileApi { export class NTQQFileCacheApi { static async setCacheSilentScan(isSilent: boolean = true) { - return await callNTQQApi({ - methodName: NTQQApiMethod.CACHE_SET_SILENCE, + return await invoke({ + methodName: NTMethod.CACHE_SET_SILENCE, args: [ { isSilent, @@ -198,21 +199,21 @@ export class NTQQFileCacheApi { } static getCacheSessionPathList() { - return callNTQQApi< + return invoke< { key: string value: string }[] >({ - className: NTQQApiClass.OS_API, - methodName: NTQQApiMethod.CACHE_PATH_SESSION, + className: NTClass.OS_API, + methodName: NTMethod.CACHE_PATH_SESSION, }) } static clearCache(cacheKeys: Array = ['tmp', 'hotUpdate']) { - return callNTQQApi({ + return invoke({ // TODO: 目前还不知道真正的返回值是什么 - methodName: NTQQApiMethod.CACHE_CLEAR, + methodName: NTMethod.CACHE_CLEAR, args: [ { keys: cacheKeys, @@ -223,8 +224,8 @@ export class NTQQFileCacheApi { } static addCacheScannedPaths(pathMap: object = {}) { - return callNTQQApi({ - methodName: NTQQApiMethod.CACHE_ADD_SCANNED_PATH, + return invoke({ + methodName: NTMethod.CACHE_ADD_SCANNED_PATH, args: [ { pathMap: { ...pathMap }, @@ -235,35 +236,35 @@ export class NTQQFileCacheApi { } static scanCache() { - callNTQQApi({ + invoke({ methodName: ReceiveCmdS.CACHE_SCAN_FINISH, classNameIsRegister: true, }).then() - return callNTQQApi({ - methodName: NTQQApiMethod.CACHE_SCAN, + return invoke({ + methodName: NTMethod.CACHE_SCAN, args: [null, null], timeout: 300 * Time.second, }) } static getHotUpdateCachePath() { - return callNTQQApi({ - className: NTQQApiClass.HOTUPDATE_API, - methodName: NTQQApiMethod.CACHE_PATH_HOT_UPDATE, + return invoke({ + className: NTClass.HOTUPDATE_API, + methodName: NTMethod.CACHE_PATH_HOT_UPDATE, }) } static getDesktopTmpPath() { - return callNTQQApi({ - className: NTQQApiClass.BUSINESS_API, - methodName: NTQQApiMethod.CACHE_PATH_DESKTOP_TEMP, + return invoke({ + className: NTClass.BUSINESS_API, + methodName: NTMethod.CACHE_PATH_DESKTOP_TEMP, }) } static getChatCacheList(type: ChatType, pageSize: number = 1000, pageIndex: number = 0) { return new Promise((res, rej) => { - callNTQQApi({ - methodName: NTQQApiMethod.CACHE_CHAT_GET, + invoke({ + methodName: NTMethod.CACHE_CHAT_GET, args: [ { chatType: type, @@ -282,8 +283,8 @@ export class NTQQFileCacheApi { static getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) { const _lastRecord = lastRecord ? lastRecord : { fileType: fileType } - return callNTQQApi({ - methodName: NTQQApiMethod.CACHE_FILE_GET, + return invoke({ + methodName: NTMethod.CACHE_FILE_GET, args: [ { fileType: fileType, @@ -298,8 +299,8 @@ export class NTQQFileCacheApi { } static async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) { - return await callNTQQApi({ - methodName: NTQQApiMethod.CACHE_CHAT_CLEAR, + return await invoke({ + methodName: NTMethod.CACHE_CHAT_CLEAR, args: [ { chats, diff --git a/src/ntqqapi/api/friend.ts b/src/ntqqapi/api/friend.ts index b9454ac..3981e5f 100644 --- a/src/ntqqapi/api/friend.ts +++ b/src/ntqqapi/api/friend.ts @@ -1,6 +1,6 @@ import { Friend, FriendV2 } from '../types' import { ReceiveCmdS } from '../hook' -import { callNTQQApi, NTQQApiMethod } from '../ntcall' +import { invoke, NTMethod } from '../ntcall' import { getSession } from '@/ntqqapi/wrapper' import { BuddyListReqType, NodeIKernelProfileService } from '../services' import { NTEventDispatch } from '@/common/utils/EventTask' @@ -9,7 +9,7 @@ import { LimitedHashTable } from '@/common/utils/table' export class NTQQFriendApi { /** 大于或等于 26702 应使用 getBuddyV2 */ static async getFriends(forced = false) { - const data = await callNTQQApi<{ + const data = await invoke<{ data: { categoryId: number categroyName: string @@ -17,7 +17,7 @@ export class NTQQFriendApi { buddyList: Friend[] }[] }>({ - methodName: NTQQApiMethod.FRIENDS, + methodName: NTMethod.FRIENDS, args: [{ force_update: forced }, undefined], cbCmd: ReceiveCmdS.FRIENDS, afterFirstCmd: false, @@ -101,7 +101,7 @@ export class NTQQFriendApi { if (session) { return session.getBuddyService().isBuddy(uid) } else { - return await callNTQQApi({ + return await invoke({ methodName: 'nodeIKernelBuddyService/isBuddy', args: [ { uid }, diff --git a/src/ntqqapi/api/group.ts b/src/ntqqapi/api/group.ts index 6025d74..f05d320 100644 --- a/src/ntqqapi/api/group.ts +++ b/src/ntqqapi/api/group.ts @@ -1,6 +1,7 @@ import { ReceiveCmdS } from '../hook' import { Group, GroupMember, GroupMemberRole, GroupNotifies, GroupRequestOperateTypes, GroupNotify } from '../types' -import { callNTQQApi, GeneralCallResult, NTQQApiMethod } from '../ntcall' +import { invoke, NTMethod } from '../ntcall' +import { GeneralCallResult } from '../services' import { NTQQWindowApi, NTQQWindows } from './window' import { getSession } from '../wrapper' import { NTEventDispatch } from '@/common/utils/EventTask' @@ -31,8 +32,8 @@ export class NTQQGroupApi { const sceneId = groupService.createMemberListScene(groupQQ, 'groupMemberList_MainWindow') result = await groupService.getNextMemberList(sceneId, undefined, num) } else { - const sceneId = await callNTQQApi({ - methodName: NTQQApiMethod.GROUP_MEMBER_SCENE, + const sceneId = await invoke({ + methodName: NTMethod.GROUP_MEMBER_SCENE, args: [ { groupCode: groupQQ, @@ -40,10 +41,10 @@ export class NTQQGroupApi { }, ], }) - result = await callNTQQApi< + result = await invoke< ReturnType >({ - methodName: NTQQApiMethod.GROUP_MEMBERS, + methodName: NTMethod.GROUP_MEMBERS, args: [ { sceneId, @@ -62,12 +63,12 @@ export class NTQQGroupApi { static async getGroupNotifies() { // 获取管理员变更 // 加群通知,退出通知,需要管理员权限 - callNTQQApi({ + invoke({ methodName: ReceiveCmdS.GROUP_NOTIFY, classNameIsRegister: true, }).then() - return await callNTQQApi({ - methodName: NTQQApiMethod.GET_GROUP_NOTICE, + return await invoke({ + methodName: NTMethod.GET_GROUP_NOTICE, cbCmd: ReceiveCmdS.GROUP_NOTIFY, afterFirstCmd: false, args: [{ doubt: false, startSeq: '', number: 14 }, null], @@ -167,7 +168,7 @@ export class NTQQGroupApi { } static async getGroupAtAllRemainCount(groupCode: string) { - return await callNTQQApi< + return await invoke< GeneralCallResult & { atInfo: { canAtAll: boolean @@ -178,7 +179,7 @@ export class NTQQGroupApi { } } >({ - methodName: NTQQApiMethod.GROUP_AT_ALL_REMAIN_COUNT, + methodName: NTMethod.GROUP_AT_ALL_REMAIN_COUNT, args: [ { groupCode, diff --git a/src/ntqqapi/api/msg.ts b/src/ntqqapi/api/msg.ts index b6d4fb2..c95b66c 100644 --- a/src/ntqqapi/api/msg.ts +++ b/src/ntqqapi/api/msg.ts @@ -1,4 +1,5 @@ -import { callNTQQApi, GeneralCallResult, NTQQApiMethod } from '../ntcall' +import { invoke, NTMethod } from '../ntcall' +import { GeneralCallResult } from '../services' import { RawMessage, SendMessageElement, Peer, ChatType2 } from '../types' import { getSelfNick, getSelfUid } from '../../common/data' import { getBuildVersion } from '../../common/utils' @@ -28,8 +29,8 @@ export class NTQQMsgApi { static async activateChat(peer: Peer) { // await this.fetchRecentContact(); // await sleep(500); - return await callNTQQApi({ - methodName: NTQQApiMethod.ACTIVE_CHAT_PREVIEW, + return await invoke({ + methodName: NTMethod.ACTIVE_CHAT_PREVIEW, args: [{ peer, cnt: 20 }, null], }) } @@ -37,8 +38,8 @@ export class NTQQMsgApi { static async activateChatAndGetHistory(peer: Peer) { // await this.fetchRecentContact(); // await sleep(500); - return await callNTQQApi({ - methodName: NTQQApiMethod.ACTIVE_CHAT_HISTORY, + return await invoke({ + methodName: NTMethod.ACTIVE_CHAT_HISTORY, // 参数似乎不是这样 args: [{ peer, cnt: 20 }, null], }) diff --git a/src/ntqqapi/api/rkey.ts b/src/ntqqapi/api/rkey.ts index c3a59cf..1fb1b7d 100644 --- a/src/ntqqapi/api/rkey.ts +++ b/src/ntqqapi/api/rkey.ts @@ -1,5 +1,3 @@ -//远端rkey获取 - import { log } from '@/common/utils' interface ServerRkeyData { diff --git a/src/ntqqapi/api/user.ts b/src/ntqqapi/api/user.ts index 8b23416..e88495a 100644 --- a/src/ntqqapi/api/user.ts +++ b/src/ntqqapi/api/user.ts @@ -1,4 +1,5 @@ -import { callNTQQApi, GeneralCallResult, NTQQApiMethod } from '../ntcall' +import { invoke, NTMethod } from '../ntcall' +import { GeneralCallResult } from '../services' import { User, UserDetailInfoByUin, UserDetailInfoByUinV2, UserDetailInfoListenerArg } from '../types' import { friends, groupMembers, getSelfUin } from '@/common/data' import { CacheClassFuncAsync, getBuildVersion } from '@/common/utils' @@ -12,8 +13,8 @@ import { Time } from 'cosmokit' export class NTQQUserApi { static async setQQAvatar(filePath: string) { - return await callNTQQApi({ - methodName: NTQQApiMethod.SET_QQ_AVATAR, + return await invoke({ + methodName: NTMethod.SET_QQ_AVATAR, args: [ { path: filePath, @@ -44,7 +45,7 @@ export class NTQQUserApi { ) info = profile } else { - const result = await callNTQQApi<{ info: UserDetailInfoListenerArg }>({ + const result = await invoke<{ info: UserDetailInfoListenerArg }>({ methodName: 'nodeIKernelProfileService/fetchUserDetailInfo', cbCmd: 'nodeIKernelProfileListener/onUserDetailInfoChanged', afterFirstCmd: false, diff --git a/src/ntqqapi/api/window.ts b/src/ntqqapi/api/window.ts index 3d37138..e585e20 100644 --- a/src/ntqqapi/api/window.ts +++ b/src/ntqqapi/api/window.ts @@ -1,4 +1,5 @@ -import { callNTQQApi, GeneralCallResult, NTQQApiClass, NTQQApiMethod } from '../ntcall' +import { invoke, NTClass, NTMethod } from '../ntcall' +import { GeneralCallResult } from '../services' import { ReceiveCmd } from '../hook' import { BrowserWindow } from 'electron' @@ -27,12 +28,12 @@ export class NTQQWindowApi { static async openWindow( ntQQWindow: NTQQWindow, args: any[], - cbCmd: ReceiveCmd | null = null, + cbCmd: ReceiveCmd | undefined, autoCloseSeconds: number = 2, ) { - const result = await callNTQQApi({ - className: NTQQApiClass.WINDOW_API, - methodName: NTQQApiMethod.OPEN_EXTRA_WINDOW, + const result = await invoke({ + className: NTClass.WINDOW_API, + methodName: NTMethod.OPEN_EXTRA_WINDOW, cbCmd, afterFirstCmd: false, args: [ntQQWindow.windowName, ...args], diff --git a/src/ntqqapi/hook.ts b/src/ntqqapi/hook.ts index 494b3db..b2e4fbb 100644 --- a/src/ntqqapi/hook.ts +++ b/src/ntqqapi/hook.ts @@ -1,5 +1,5 @@ import type { BrowserWindow } from 'electron' -import { NTQQApiClass, NTQQApiMethod } from './ntcall' +import { NTClass, NTMethod } from './ntcall' import { NTQQMsgApi } from './api/msg' import { CategoryFriend, @@ -53,16 +53,16 @@ export let ReceiveCmdS = { export type ReceiveCmd = (typeof ReceiveCmdS)[keyof typeof ReceiveCmdS] -interface NTQQApiReturnData extends Array { +interface NTQQApiReturnData extends Array { 0: { type: 'request' - eventName: NTQQApiClass + eventName: NTClass callbackId?: string } 1: { cmdName: ReceiveCmd cmdType: 'event' - payload: PayloadType + payload: Payload }[] } @@ -73,7 +73,7 @@ let receiveHooks: Array<{ }> = [] let callHooks: Array<{ - method: NTQQApiMethod[] + method: NTMethod[] hookFunc: (callParams: unknown[]) => void | Promise }> = [] @@ -141,7 +141,7 @@ export function hookNTQQApiCall(window: BrowserWindow) { } catch (e) { } try { const _args: unknown[] = args[3][1] - const cmdName: NTQQApiMethod = _args[0] as NTQQApiMethod + const cmdName: NTMethod = _args[0] as NTMethod const callParams = _args.slice(1) callHooks.forEach((hook) => { if (hook.method.includes(cmdName)) { @@ -207,7 +207,7 @@ export function registerReceiveHook( } export function registerCallHook( - method: NTQQApiMethod | NTQQApiMethod[], + method: NTMethod | NTMethod[], hookFunc: (callParams: unknown[]) => void | Promise, ): void { if (!Array.isArray(method)) { @@ -499,7 +499,7 @@ export async function startHook() { } }) - registerCallHook(NTQQApiMethod.DELETE_ACTIVE_CHAT, async (payload) => { + registerCallHook(NTMethod.DELETE_ACTIVE_CHAT, async (payload) => { const peerUid = payload[0] as string log('激活的聊天窗口被删除,准备重新激活', peerUid) let chatType = ChatType.friend diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index e172c57..54863f3 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -2,9 +2,9 @@ import { ipcMain } from 'electron' import { hookApiCallbacks, ReceiveCmd, registerReceiveHook, removeReceiveHook } from './hook' import { log } from '../common/utils/log' import { randomUUID } from 'node:crypto' -import { GeneralCallResult } from './services/common' +import { GeneralCallResult } from './services' -export enum NTQQApiClass { +export enum NTClass { NT_API = 'ns-ntApi', FS_API = 'ns-FsApi', OS_API = 'ns-OsApi', @@ -18,8 +18,7 @@ export enum NTQQApiClass { NODE_STORE_API = 'ns-NodeStoreApi' } -export enum NTQQApiMethod { - TEST = 'NodeIKernelTipOffService/getPskey', +export enum NTMethod { RECENT_CONTACT = 'nodeIKernelRecentContactService/fetchAndSubscribeABatchOfRecentContact', ACTIVE_CHAT_PREVIEW = 'nodeIKernelMsgService/getAioFirstViewLatestMsgsAndAddActiveChat', // 激活聊天窗口,有时候必须这样才能收到消息, 并返回最新预览消息 ACTIVE_CHAT_HISTORY = 'nodeIKernelMsgService/getMsgsIncludeSelfAndAddActiveChat', // 激活聊天窗口,有时候必须这样才能收到消息, 并返回历史消息 @@ -94,27 +93,27 @@ export enum NTQQApiMethod { FETCH_UNITED_COMMEND_CONFIG = 'nodeIKernelUnitedConfigService/fetchUnitedCommendConfig', // 发包需要调用的 } -enum NTQQApiChannel { +export enum NTChannel { IPC_UP_2 = 'IPC_UP_2', IPC_UP_3 = 'IPC_UP_3', IPC_UP_1 = 'IPC_UP_1', } -interface NTQQApiParams { - methodName: NTQQApiMethod | string - className?: NTQQApiClass - channel?: NTQQApiChannel +interface InvokeParams { + methodName: string + className?: NTClass + channel?: NTChannel classNameIsRegister?: boolean args?: unknown[] - cbCmd?: ReceiveCmd | ReceiveCmd[] | null + cbCmd?: string | string[] cmdCB?: (payload: any) => boolean afterFirstCmd?: boolean // 是否在methodName调用完之后再去hook cbCmd timeout?: number } -export function callNTQQApi(params: NTQQApiParams) { - const className = params.className ?? NTQQApiClass.NT_API - const channel = params.channel ?? NTQQApiChannel.IPC_UP_2 +export function invoke(params: InvokeParams) { + const className = params.className ?? NTClass.NT_API + const channel = params.channel ?? NTChannel.IPC_UP_2 const timeout = params.timeout ?? 5000 const afterFirstCmd = params.afterFirstCmd ?? true const uuid = randomUUID() @@ -184,6 +183,4 @@ export function callNTQQApi(params: NTQQApiParams) { apiArgs, ) }) -} - -export { GeneralCallResult } \ No newline at end of file +} \ No newline at end of file diff --git a/src/ntqqapi/services/index.ts b/src/ntqqapi/services/index.ts index e7997a3..22f46ab 100644 --- a/src/ntqqapi/services/index.ts +++ b/src/ntqqapi/services/index.ts @@ -1,3 +1,4 @@ +export * from './common' export * from './NodeIKernelBuddyService' export * from './NodeIKernelProfileService' export * from './NodeIKernelGroupService'