From afe0ff89a7479836c237bef627c3ff21e672ee4c Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Sat, 24 Feb 2024 21:40:21 +0800 Subject: [PATCH 01/17] feat: Add chat cache scan & clear to NTQQApi --- src/ntqqapi/ntcall.ts | 49 ++++++++++++++++++++++++++++++++++++++++++- src/ntqqapi/types.ts | 21 ++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index d4cae12..80a81f0 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -15,7 +15,9 @@ import { RawMessage, SelfInfo, SendMessageElement, - User + User, + ChatCacheList, + ChatCacheListItemBasic } from "./types"; import * as fs from "fs"; import {addHistoryMsg, friendRequests, groupNotifies, msgHistory, selfInfo} from "../common/data"; @@ -71,6 +73,17 @@ export enum NTQQApiMethod { SET_MEMBER_ROLE = "nodeIKernelGroupService/modifyMemberRole", PUBLISH_GROUP_BULLETIN = "nodeIKernelGroupService/publishGroupBulletinBulletin", SET_GROUP_NAME = "nodeIKernelGroupService/modifyGroupName", + + CACHE_SET_SILENCE = 'nodeIKernelStorageCleanService/setSilentScan', + CACHE_ADD_SCANNED_PATH = 'nodeIKernelStorageCleanService/addCacheScanedPaths', // TODO: Unused method + CACHE_PATH_HOT_UPDATE = 'getHotUpdateCachePath', // TODO: Unused method + CACHE_PATH_DESKTOP_TEMP = 'getDesktopTmpPath', // TODO: Unused method + CACHE_PATH_SESSION = 'getCleanableAppSessionPathList', // TODO: Unused method + CACHE_SCAN = 'nodeIKernelStorageCleanService/scanCache', + CACHE_CLEAR = '', // TODO + + CACHE_CHAT_SCAN = 'nodeIKernelStorageCleanService/getChatCacheInfo', + CACHE_CHAT_CLEAR = 'nodeIKernelStorageCleanService/clearChatCacheInfo', } enum NTQQApiChannel { @@ -684,4 +697,38 @@ export class NTQQApi { static publishGroupBulletin(groupQQ: string, title: string, content: string) { } + + static async setCacheSilentScan(isSilent: boolean = true) { + return await callNTQQApi({ + methodName: NTQQApiMethod.CACHE_SET_SILENCE, + args: [{ + isSilent + }, null] + }); + } + + static getChatCacheList(type: 1 | 2, pageSize: number = 80, pageIndex: number = 0) { + return new Promise((res, rej) => { + callNTQQApi({ + methodName: NTQQApiMethod.CACHE_CHAT_SCAN, + args: [{ + ChatType: type, + pageSize, + order: 1, + pageIndex + }, null] + }).then(list => res(list)) + .catch(e => rej(e)); + }); + } + + static async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: unknown[] = []) { + return await callNTQQApi({ + methodName: NTQQApiMethod.CACHE_CHAT_CLEAR, + args: [{ + chats, + fileKeys + }, null] + }); + } } \ No newline at end of file diff --git a/src/ntqqapi/types.ts b/src/ntqqapi/types.ts index 2d3f485..4eccd77 100644 --- a/src/ntqqapi/types.ts +++ b/src/ntqqapi/types.ts @@ -354,4 +354,23 @@ export interface FriendRequestNotify { unreadNums: number, buddyReqs: FriendRequest[] } -} \ No newline at end of file +} +export interface ChatCacheList { + pageCount: number, + infos: ChatCacheListItem[] +} + +export interface ChatCacheListItem { + chatType: 1 | 2, + basicChatCacheInfo: ChatCacheListItemBasic, + guildChatCacheInfo: unknown[] // TODO: 没用过频道所以不知道这里边的详细内容 +} + +export interface ChatCacheListItemBasic { + chatSize: string, + chatTime: string, + uid: string, + uin: string, + remarkName: string, + nickName: string +} From f521873ba7f98a1ccca11e4af809b020091eeb32 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 17:47:30 +0800 Subject: [PATCH 02/17] feat: Add `scanCache` to NTQQApi --- src/ntqqapi/hook.ts | 3 ++- src/ntqqapi/ntcall.ts | 17 ++++++++++++++++- src/ntqqapi/types.ts | 20 +++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/ntqqapi/hook.ts b/src/ntqqapi/hook.ts index 48f8047..b08b073 100644 --- a/src/ntqqapi/hook.ts +++ b/src/ntqqapi/hook.ts @@ -24,7 +24,8 @@ export enum ReceiveCmd { MEDIA_DOWNLOAD_COMPLETE = "nodeIKernelMsgListener/onRichMediaDownloadComplete", UNREAD_GROUP_NOTIFY = "nodeIKernelGroupListener/onGroupNotifiesUnreadCountUpdated", GROUP_NOTIFY = "nodeIKernelGroupListener/onGroupSingleScreenNotifies", - FRIEND_REQUEST = "nodeIKernelBuddyListener/onBuddyReqChange" + FRIEND_REQUEST = "nodeIKernelBuddyListener/onBuddyReqChange", + CACHE_SCAN_FINISH = "nodeIKernelStorageCleanListener/onFinishScan", } interface NTQQApiReturnData extends Array { diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 80a81f0..da6276f 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -16,6 +16,7 @@ import { SelfInfo, SendMessageElement, User, + CacheScanResult, ChatCacheList, ChatCacheListItemBasic } from "./types"; @@ -79,7 +80,7 @@ export enum NTQQApiMethod { CACHE_PATH_HOT_UPDATE = 'getHotUpdateCachePath', // TODO: Unused method CACHE_PATH_DESKTOP_TEMP = 'getDesktopTmpPath', // TODO: Unused method CACHE_PATH_SESSION = 'getCleanableAppSessionPathList', // TODO: Unused method - CACHE_SCAN = 'nodeIKernelStorageCleanService/scanCache', + CACHE_SCAN = 'nodeIKernelStorageCleanService/scanCache', CACHE_CLEAR = '', // TODO CACHE_CHAT_SCAN = 'nodeIKernelStorageCleanService/getChatCacheInfo', @@ -707,6 +708,20 @@ export class NTQQApi { }); } + static scanCache() { + callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, + methodName: ReceiveCmd.CACHE_SCAN_FINISH, + classNameIsRegister: true, + }).then(); + return callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, + methodName: NTQQApiMethod.CACHE_SCAN, + args: [null, null], + timeoutSecond: 300, + }); + } + static getChatCacheList(type: 1 | 2, pageSize: number = 80, pageIndex: number = 0) { return new Promise((res, rej) => { callNTQQApi({ diff --git a/src/ntqqapi/types.ts b/src/ntqqapi/types.ts index 4eccd77..01e0a19 100644 --- a/src/ntqqapi/types.ts +++ b/src/ntqqapi/types.ts @@ -355,6 +355,22 @@ export interface FriendRequestNotify { buddyReqs: FriendRequest[] } } + +export interface CacheScanResult { + result: number, + size: [ // 单位为字节 + string, // 系统总存储空间 + string, // 系统可用存储空间 + string, // 系统已用存储空间 + string, // QQ总大小 + string, // 「聊天与文件」大小 + string, // 未知 + string, // 「缓存数据」大小 + string, // 「其他数据」大小 + string, // 未知 + ] +} + export interface ChatCacheList { pageCount: number, infos: ChatCacheListItem[] @@ -372,5 +388,7 @@ export interface ChatCacheListItemBasic { uid: string, uin: string, remarkName: string, - nickName: string + nickName: string, + chatType?: 1 | 2, + isChecked?: boolean } From f110c2d3df74dcd87487848002364ed19befcdcf Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 17:55:19 +0800 Subject: [PATCH 03/17] style: Fix typo --- src/ntqqapi/ntcall.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index da6276f..a85056e 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -83,7 +83,7 @@ export enum NTQQApiMethod { CACHE_SCAN = 'nodeIKernelStorageCleanService/scanCache', CACHE_CLEAR = '', // TODO - CACHE_CHAT_SCAN = 'nodeIKernelStorageCleanService/getChatCacheInfo', + CACHE_CHAT_GET = 'nodeIKernelStorageCleanService/getChatCacheInfo', CACHE_CHAT_CLEAR = 'nodeIKernelStorageCleanService/clearChatCacheInfo', } @@ -725,7 +725,7 @@ export class NTQQApi { static getChatCacheList(type: 1 | 2, pageSize: number = 80, pageIndex: number = 0) { return new Promise((res, rej) => { callNTQQApi({ - methodName: NTQQApiMethod.CACHE_CHAT_SCAN, + methodName: NTQQApiMethod.CACHE_CHAT_GET, args: [{ ChatType: type, pageSize, From b548fd3f0e0040176c3b1627a7e3adc4bfaea7de Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 18:21:24 +0800 Subject: [PATCH 04/17] feat: Add `getCacheSessingPathList` to NTQQApi --- src/ntqqapi/ntcall.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index a85056e..3b5bc0a 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -40,6 +40,7 @@ export type IPCReceiveDetail = [ export enum NTQQApiClass { NT_API = "ns-ntApi", FS_API = "ns-FsApi", + OS_API = "ns-OsApi", GLOBAL_DATA = "ns-GlobalDataApi" } @@ -722,6 +723,16 @@ export class NTQQApi { }); } + static getCacheSessionPathList() { + return callNTQQApi<{ + key: string, + value: string + }[]>({ + className: NTQQApiClass.OS_API, + methodName: NTQQApiMethod.CACHE_PATH_SESSION, + }); + } + static getChatCacheList(type: 1 | 2, pageSize: number = 80, pageIndex: number = 0) { return new Promise((res, rej) => { callNTQQApi({ From 6c267044f04fd70c60ead9f62513f4f45b87fe49 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 19:33:00 +0800 Subject: [PATCH 05/17] fix: Use a specific IPC channel for cache related API --- src/ntqqapi/ntcall.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 3b5bc0a..4a6543c 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -702,6 +702,7 @@ export class NTQQApi { static async setCacheSilentScan(isSilent: boolean = true) { return await callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_SET_SILENCE, args: [{ isSilent @@ -728,6 +729,7 @@ export class NTQQApi { key: string, value: string }[]>({ + channel: NTQQApiChannel.IPC_UP_3, className: NTQQApiClass.OS_API, methodName: NTQQApiMethod.CACHE_PATH_SESSION, }); @@ -736,6 +738,7 @@ export class NTQQApi { static getChatCacheList(type: 1 | 2, pageSize: number = 80, pageIndex: number = 0) { return new Promise((res, rej) => { callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_CHAT_GET, args: [{ ChatType: type, @@ -750,6 +753,7 @@ export class NTQQApi { static async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: unknown[] = []) { return await callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_CHAT_CLEAR, args: [{ chats, From f08c8162865b5164d07143aee25fd5708a27ba4e Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 19:58:10 +0800 Subject: [PATCH 06/17] feat: Add `clearCache` to NTQQApi --- src/ntqqapi/ntcall.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 4a6543c..303b7a3 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -80,9 +80,9 @@ export enum NTQQApiMethod { CACHE_ADD_SCANNED_PATH = 'nodeIKernelStorageCleanService/addCacheScanedPaths', // TODO: Unused method CACHE_PATH_HOT_UPDATE = 'getHotUpdateCachePath', // TODO: Unused method CACHE_PATH_DESKTOP_TEMP = 'getDesktopTmpPath', // TODO: Unused method - CACHE_PATH_SESSION = 'getCleanableAppSessionPathList', // TODO: Unused method + CACHE_PATH_SESSION = 'getCleanableAppSessionPathList', CACHE_SCAN = 'nodeIKernelStorageCleanService/scanCache', - CACHE_CLEAR = '', // TODO + CACHE_CLEAR = 'nodeIKernelStorageCleanService/clearCacheDataByKeys', CACHE_CHAT_GET = 'nodeIKernelStorageCleanService/getChatCacheInfo', CACHE_CHAT_CLEAR = 'nodeIKernelStorageCleanService/clearChatCacheInfo', @@ -735,6 +735,17 @@ export class NTQQApi { }); } + static clearCache(cacheKeys: Array = [ 'tmp', 'hotUpdate' ]) { + console.log(cacheKeys); + return callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, + methodName: NTQQApiMethod.CACHE_CLEAR, + args: [{ + keys: cacheKeys + }, null] + }); + } + static getChatCacheList(type: 1 | 2, pageSize: number = 80, pageIndex: number = 0) { return new Promise((res, rej) => { callNTQQApi({ From 85d648622df95b34510c55ab2a20ef5bfd2cf8bf Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 20:19:17 +0800 Subject: [PATCH 07/17] feat: Add `getHotUpdateCachePath` to NTQQApi --- src/ntqqapi/ntcall.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 303b7a3..6df1a49 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -41,6 +41,7 @@ export enum NTQQApiClass { NT_API = "ns-ntApi", FS_API = "ns-FsApi", OS_API = "ns-OsApi", + HOTUPDATE_API = "ns-HotUpdateApi", GLOBAL_DATA = "ns-GlobalDataApi" } @@ -724,6 +725,13 @@ export class NTQQApi { }); } + static getHotUpdateCachePath() { + return callNTQQApi({ + className: NTQQApiClass.HOTUPDATE_API, + methodName: NTQQApiMethod.CACHE_PATH_HOT_UPDATE + }); + } + static getCacheSessionPathList() { return callNTQQApi<{ key: string, From 2f9cd8ba19e91947ad00485deb21b4b5ee4647a0 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 20:24:06 +0800 Subject: [PATCH 08/17] feat: Add `getDesktopTmpPath` to NTQQApi --- src/ntqqapi/ntcall.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 6df1a49..500a5d0 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -42,6 +42,7 @@ export enum NTQQApiClass { FS_API = "ns-FsApi", OS_API = "ns-OsApi", HOTUPDATE_API = "ns-HotUpdateApi", + BUSINESS_API = "ns-BusinessApi", GLOBAL_DATA = "ns-GlobalDataApi" } @@ -732,6 +733,13 @@ export class NTQQApi { }); } + static getDesktopTmpPath() { + return callNTQQApi({ + className: NTQQApiClass.BUSINESS_API, + methodName: NTQQApiMethod.CACHE_PATH_DESKTOP_TEMP + }); + } + static getCacheSessionPathList() { return callNTQQApi<{ key: string, From 839fd7f1ab3ff163da8aa58ae6447f2d301b4d64 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 21:16:52 +0800 Subject: [PATCH 09/17] feat: Add `addCacheScannedPaths` to NTQQApi --- src/ntqqapi/ntcall.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 500a5d0..fedc440 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -79,9 +79,9 @@ export enum NTQQApiMethod { SET_GROUP_NAME = "nodeIKernelGroupService/modifyGroupName", CACHE_SET_SILENCE = 'nodeIKernelStorageCleanService/setSilentScan', - CACHE_ADD_SCANNED_PATH = 'nodeIKernelStorageCleanService/addCacheScanedPaths', // TODO: Unused method - CACHE_PATH_HOT_UPDATE = 'getHotUpdateCachePath', // TODO: Unused method - CACHE_PATH_DESKTOP_TEMP = 'getDesktopTmpPath', // TODO: Unused method + CACHE_ADD_SCANNED_PATH = 'nodeIKernelStorageCleanService/addCacheScanedPaths', + CACHE_PATH_HOT_UPDATE = 'getHotUpdateCachePath', + CACHE_PATH_DESKTOP_TEMP = 'getDesktopTmpPath', CACHE_PATH_SESSION = 'getCleanableAppSessionPathList', CACHE_SCAN = 'nodeIKernelStorageCleanService/scanCache', CACHE_CLEAR = 'nodeIKernelStorageCleanService/clearCacheDataByKeys', @@ -712,6 +712,15 @@ export class NTQQApi { }); } + static addCacheScannedPaths(pathMap: object = {}) { + return callNTQQApi({ + methodName: NTQQApiMethod.CACHE_ADD_SCANNED_PATH, + args: [{ + pathMap: {...pathMap}, + }, null] + }); + } + static scanCache() { callNTQQApi({ channel: NTQQApiChannel.IPC_UP_3, From 6548876c749354372bbe7e1a32b460012028fab1 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 21:27:20 +0800 Subject: [PATCH 10/17] fix: Use a specific IPC channel for cache related API --- src/ntqqapi/ntcall.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index fedc440..5331110 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -714,6 +714,7 @@ export class NTQQApi { static addCacheScannedPaths(pathMap: object = {}) { return callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_ADD_SCANNED_PATH, args: [{ pathMap: {...pathMap}, @@ -737,6 +738,7 @@ export class NTQQApi { static getHotUpdateCachePath() { return callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, className: NTQQApiClass.HOTUPDATE_API, methodName: NTQQApiMethod.CACHE_PATH_HOT_UPDATE }); @@ -744,6 +746,7 @@ export class NTQQApi { static getDesktopTmpPath() { return callNTQQApi({ + channel: NTQQApiChannel.IPC_UP_3, className: NTQQApiClass.BUSINESS_API, methodName: NTQQApiMethod.CACHE_PATH_DESKTOP_TEMP }); From ededfe0f8c3f099363b96648077e4d9064be2e35 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 23:02:27 +0800 Subject: [PATCH 11/17] fix: Delete specific IPC channel for cache related APIs --- src/ntqqapi/ntcall.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 5331110..3e53749 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -704,7 +704,6 @@ export class NTQQApi { static async setCacheSilentScan(isSilent: boolean = true) { return await callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_SET_SILENCE, args: [{ isSilent @@ -714,7 +713,6 @@ export class NTQQApi { static addCacheScannedPaths(pathMap: object = {}) { return callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_ADD_SCANNED_PATH, args: [{ pathMap: {...pathMap}, @@ -724,12 +722,10 @@ export class NTQQApi { static scanCache() { callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, methodName: ReceiveCmd.CACHE_SCAN_FINISH, classNameIsRegister: true, }).then(); return callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_SCAN, args: [null, null], timeoutSecond: 300, @@ -738,7 +734,6 @@ export class NTQQApi { static getHotUpdateCachePath() { return callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, className: NTQQApiClass.HOTUPDATE_API, methodName: NTQQApiMethod.CACHE_PATH_HOT_UPDATE }); @@ -746,7 +741,6 @@ export class NTQQApi { static getDesktopTmpPath() { return callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, className: NTQQApiClass.BUSINESS_API, methodName: NTQQApiMethod.CACHE_PATH_DESKTOP_TEMP }); @@ -757,7 +751,6 @@ export class NTQQApi { key: string, value: string }[]>({ - channel: NTQQApiChannel.IPC_UP_3, className: NTQQApiClass.OS_API, methodName: NTQQApiMethod.CACHE_PATH_SESSION, }); @@ -766,7 +759,6 @@ export class NTQQApi { static clearCache(cacheKeys: Array = [ 'tmp', 'hotUpdate' ]) { console.log(cacheKeys); return callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_CLEAR, args: [{ keys: cacheKeys @@ -777,7 +769,6 @@ export class NTQQApi { static getChatCacheList(type: 1 | 2, pageSize: number = 80, pageIndex: number = 0) { return new Promise((res, rej) => { callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_CHAT_GET, args: [{ ChatType: type, @@ -792,7 +783,6 @@ export class NTQQApi { static async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: unknown[] = []) { return await callNTQQApi({ - channel: NTQQApiChannel.IPC_UP_3, methodName: NTQQApiMethod.CACHE_CHAT_CLEAR, args: [{ chats, From de41dab846fd7b7d676e814aaee85872d52355d0 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 23:02:45 +0800 Subject: [PATCH 12/17] fix: Fix a typo --- src/ntqqapi/ntcall.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 3e53749..2b48e22 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -771,7 +771,7 @@ export class NTQQApi { callNTQQApi({ methodName: NTQQApiMethod.CACHE_CHAT_GET, args: [{ - ChatType: type, + chatType: type, pageSize, order: 1, pageIndex From 3ec113420435efa711ac59099051655059a7fe60 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 23:25:28 +0800 Subject: [PATCH 13/17] feat: Add `clean_cache` API to OneBot adapter --- src/onebot11/action/CleanCache.ts | 96 +++++++++++++++++++++++++++++++ src/onebot11/action/index.ts | 2 + src/onebot11/action/types.ts | 1 + 3 files changed, 99 insertions(+) create mode 100644 src/onebot11/action/CleanCache.ts diff --git a/src/onebot11/action/CleanCache.ts b/src/onebot11/action/CleanCache.ts new file mode 100644 index 0000000..f2e3abd --- /dev/null +++ b/src/onebot11/action/CleanCache.ts @@ -0,0 +1,96 @@ +import BaseAction from "./BaseAction"; +import {ActionName} from "./types"; +import {NTQQApi} from "../../ntqqapi/ntcall"; +import fs from "fs"; +import Path from "path"; +import { + ChatType, + ChatCacheListItemBasic +} from '../../ntqqapi/types'; + +export default class CleanCache extends BaseAction { + actionName = ActionName.CleanCache + + protected _handle(): Promise { + console.log('Start scanning cache...'); + return new Promise(async (res, rej) => { + try { + const cacheFilePaths: string[] = []; + + await NTQQApi.setCacheSilentScan(false); + + cacheFilePaths.push((await NTQQApi.getHotUpdateCachePath())); + cacheFilePaths.push((await NTQQApi.getDesktopTmpPath())); + (await NTQQApi.getCacheSessionPathList()).forEach(e => cacheFilePaths.push(e.value)); + + // await NTQQApi.addCacheScannedPaths(); // XXX: 调用就崩溃,原因目前还未知 + const cacheScanResult = await NTQQApi.scanCache(); + const cacheSize = parseInt(cacheScanResult.size[6]); + + if (cacheScanResult.result !== 0) { + throw('Something went wrong while scanning cache. Code: ' + cacheScanResult.result); + } + + await NTQQApi.setCacheSilentScan(true); + if (cacheSize > 0 && cacheFilePaths.length > 2) { // 存在缓存文件且大小不为 0 时执行清理动作 + console.log('Cleaning cache...'); + // await NTQQApi.clearCache([ 'tmp', 'hotUpdate', ...cacheScanResult ]) // XXX: 也是调用就崩溃,调用 fs 删除得了 + deleteCachePath(cacheFilePaths); + } + + // 清理聊天记录 + // NOTE: 以防有人不需要删除聊天记录,暂时先注释掉,日后加个开关 + // await clearChatCache(ChatCacheType.PRIVATE); // 私聊消息 + // await clearChatCache(ChatCacheType.GROUP); // 群聊消息 + + + + res(); + } catch(e) { + console.error('清理缓存时发生了错误'); + rej(e); + } + }); + } +} + +function deleteCachePath(pathList: string[]) { + const emptyPath = (path: string) => { + if (!fs.existsSync(path)) return; + const files = fs.readdirSync(path); + files.forEach(file => { + const filePath = Path.resolve(path, file); + const stats = fs.statSync(filePath); + if (stats.isDirectory()) emptyPath(filePath); + else fs.unlinkSync(filePath); + }); + fs.rmdirSync(path); + } + + for (const path of pathList) { + emptyPath(path); + } +} + +async function clearChatCache(type: ChatCacheType) { + const cacheList = await getCacheList(type); + return NTQQApi.clearChatCache(cacheList, []); +} + +function getCacheList(type: ChatCacheType) { // NOTE: 做这个方法主要是因为目前还不支持针对频道消息的清理 + return new Promise>((res, rej) => { + NTQQApi.getChatCacheList(type, 1000, 0) + .then(data => { + console.log(data); + const list = data.infos.filter(e => e.chatType === type && parseInt(e.basicChatCacheInfo.chatSize) > 0); + const result = list.map(e => { + const result = { ...e.basicChatCacheInfo }; + result.chatType = type; + result.isChecked = true; + return result; + }); + res(result); + }) + .catch(e => rej(e)); + }); +} \ No newline at end of file diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index 8928a53..b1fbf57 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -28,6 +28,7 @@ import SetGroupBan from "./SetGroupBan"; import SetGroupKick from "./SetGroupKick"; import SetGroupAdmin from "./SetGroupAdmin"; import SetGroupCard from "./SetGroupCard"; +import CleanCache from "./CleanCache"; export const actionHandlers = [ new Debug(), @@ -51,6 +52,7 @@ export const actionHandlers = [ new SetGroupAdmin(), new SetGroupName(), new SetGroupCard(), + new CleanCache(), //以下为go-cqhttp api new GoCQHTTPSendGroupForwardMsg(), diff --git a/src/onebot11/action/types.ts b/src/onebot11/action/types.ts index d0c6819..a838540 100644 --- a/src/onebot11/action/types.ts +++ b/src/onebot11/action/types.ts @@ -40,6 +40,7 @@ export enum ActionName { SetGroupAdmin = "set_group_admin", SetGroupCard = "set_group_card", SetGroupName = "set_group_name", + CleanCache = "clean_cache", // 以下为go-cqhttp api GoCQHTTP_SendGroupForwardMsg = "send_group_forward_msg", GoCQHTTP_SendPrivateForwardMsg = "send_private_forward_msg", From 11108bc13f5c4c0c8f1a2f9ca664345504d17f03 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Tue, 27 Feb 2024 23:27:00 +0800 Subject: [PATCH 14/17] fix: Fix type --- src/ntqqapi/ntcall.ts | 2 +- src/ntqqapi/types.ts | 4 ++-- src/onebot11/action/CleanCache.ts | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 2b48e22..81974d5 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -766,7 +766,7 @@ export class NTQQApi { }); } - static getChatCacheList(type: 1 | 2, pageSize: number = 80, pageIndex: number = 0) { + static getChatCacheList(type: ChatType, pageSize: number = 80, pageIndex: number = 0) { return new Promise((res, rej) => { callNTQQApi({ methodName: NTQQApiMethod.CACHE_CHAT_GET, diff --git a/src/ntqqapi/types.ts b/src/ntqqapi/types.ts index 01e0a19..4b79e3d 100644 --- a/src/ntqqapi/types.ts +++ b/src/ntqqapi/types.ts @@ -377,7 +377,7 @@ export interface ChatCacheList { } export interface ChatCacheListItem { - chatType: 1 | 2, + chatType: ChatType, basicChatCacheInfo: ChatCacheListItemBasic, guildChatCacheInfo: unknown[] // TODO: 没用过频道所以不知道这里边的详细内容 } @@ -389,6 +389,6 @@ export interface ChatCacheListItemBasic { uin: string, remarkName: string, nickName: string, - chatType?: 1 | 2, + chatType?: ChatType, isChecked?: boolean } diff --git a/src/onebot11/action/CleanCache.ts b/src/onebot11/action/CleanCache.ts index f2e3abd..3012d65 100644 --- a/src/onebot11/action/CleanCache.ts +++ b/src/onebot11/action/CleanCache.ts @@ -40,8 +40,8 @@ export default class CleanCache extends BaseAction { // 清理聊天记录 // NOTE: 以防有人不需要删除聊天记录,暂时先注释掉,日后加个开关 - // await clearChatCache(ChatCacheType.PRIVATE); // 私聊消息 - // await clearChatCache(ChatCacheType.GROUP); // 群聊消息 + // await clearChatCache(ChatType.PRIVATE); // 私聊消息 + // await clearChatCache(ChatType.GROUP); // 群聊消息 @@ -72,12 +72,12 @@ function deleteCachePath(pathList: string[]) { } } -async function clearChatCache(type: ChatCacheType) { +async function clearChatCache(type: ChatType) { const cacheList = await getCacheList(type); return NTQQApi.clearChatCache(cacheList, []); } -function getCacheList(type: ChatCacheType) { // NOTE: 做这个方法主要是因为目前还不支持针对频道消息的清理 +function getCacheList(type: ChatType) { // NOTE: 做这个方法主要是因为目前还不支持针对频道消息的清理 return new Promise>((res, rej) => { NTQQApi.getChatCacheList(type, 1000, 0) .then(data => { From 91075e192b37ffca825b8d4c0b8b6b4dc435b5bc Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Wed, 28 Feb 2024 00:04:55 +0800 Subject: [PATCH 15/17] feat: Add `getFileCacheInfo` to NTQQApi --- src/ntqqapi/ntcall.ts | 22 +++++++++++++++++++--- src/ntqqapi/types.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 81974d5..c8bc214 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -17,8 +17,8 @@ import { SendMessageElement, User, CacheScanResult, - ChatCacheList, - ChatCacheListItemBasic + ChatCacheList, ChatCacheListItemBasic, + CacheFileList, CacheFileListItem, CacheFileType, } from "./types"; import * as fs from "fs"; import {addHistoryMsg, friendRequests, groupNotifies, msgHistory, selfInfo} from "../common/data"; @@ -87,6 +87,7 @@ export enum NTQQApiMethod { CACHE_CLEAR = 'nodeIKernelStorageCleanService/clearCacheDataByKeys', CACHE_CHAT_GET = 'nodeIKernelStorageCleanService/getChatCacheInfo', + CACHE_FILE_GET = 'nodeIKernelStorageCleanService/getFileCacheInfo', CACHE_CHAT_CLEAR = 'nodeIKernelStorageCleanService/clearChatCacheInfo', } @@ -766,7 +767,7 @@ export class NTQQApi { }); } - static getChatCacheList(type: ChatType, pageSize: number = 80, pageIndex: number = 0) { + static getChatCacheList(type: ChatType, pageSize: number = 1000, pageIndex: number = 0) { return new Promise((res, rej) => { callNTQQApi({ methodName: NTQQApiMethod.CACHE_CHAT_GET, @@ -781,6 +782,21 @@ export class NTQQApi { }); } + static getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) { + const _lastRecord = lastRecord ? lastRecord : { fileType: fileType }; + + return callNTQQApi({ + methodName: NTQQApiMethod.CACHE_FILE_GET, + args: [{ + fileType: fileType, + restart: true, + pageSize: pageSize, + order: 1, + lastRecord: _lastRecord, + }, null] + }) + } + static async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: unknown[] = []) { return await callNTQQApi({ methodName: NTQQApiMethod.CACHE_CHAT_CLEAR, diff --git a/src/ntqqapi/types.ts b/src/ntqqapi/types.ts index 4b79e3d..53e1e22 100644 --- a/src/ntqqapi/types.ts +++ b/src/ntqqapi/types.ts @@ -392,3 +392,30 @@ export interface ChatCacheListItemBasic { chatType?: ChatType, isChecked?: boolean } + +export enum CacheFileType { + IMAGE = 0, + VIDEO = 1, + AUDIO = 2, + DOCUMENT = 3, + OTHER = 4, +} + +export interface CacheFileList { + infos: CacheFileListItem[], +} + +export interface CacheFileListItem { + fileSize: string, + fileTime: string, + fileKey: string, + elementId: string, + elementIdStr: string, + fileType: CacheFileType, + path: string, + fileName: string, + senderId: string, + previewPath: string, + senderName: string, + isChecked?: boolean, +} From 5088112864169708105b9e91adbe482a0e7ca997 Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Wed, 28 Feb 2024 00:11:00 +0800 Subject: [PATCH 16/17] feat: Now `clean_cache` API can delete cache files --- src/onebot11/action/CleanCache.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/onebot11/action/CleanCache.ts b/src/onebot11/action/CleanCache.ts index 3012d65..84dd2e6 100644 --- a/src/onebot11/action/CleanCache.ts +++ b/src/onebot11/action/CleanCache.ts @@ -5,14 +5,14 @@ import fs from "fs"; import Path from "path"; import { ChatType, - ChatCacheListItemBasic + ChatCacheListItemBasic, + CacheFileType } from '../../ntqqapi/types'; export default class CleanCache extends BaseAction { actionName = ActionName.CleanCache protected _handle(): Promise { - console.log('Start scanning cache...'); return new Promise(async (res, rej) => { try { const cacheFilePaths: string[] = []; @@ -33,18 +33,31 @@ export default class CleanCache extends BaseAction { await NTQQApi.setCacheSilentScan(true); if (cacheSize > 0 && cacheFilePaths.length > 2) { // 存在缓存文件且大小不为 0 时执行清理动作 - console.log('Cleaning cache...'); // await NTQQApi.clearCache([ 'tmp', 'hotUpdate', ...cacheScanResult ]) // XXX: 也是调用就崩溃,调用 fs 删除得了 deleteCachePath(cacheFilePaths); } - // 清理聊天记录 + // 获取聊天记录列表 // NOTE: 以防有人不需要删除聊天记录,暂时先注释掉,日后加个开关 - // await clearChatCache(ChatType.PRIVATE); // 私聊消息 - // await clearChatCache(ChatType.GROUP); // 群聊消息 + // const privateChatCache = await getCacheList(ChatType.friend); // 私聊消息 + // const groupChatCache = await getCacheList(ChatType.group); // 群聊消息 + // const chatCacheList = [ ...privateChatCache, ...groupChatCache ]; + const chatCacheList: ChatCacheListItemBasic[] = []; + // 获取聊天缓存文件列表 + const cacheFileList: string[] = []; + + for (const name in CacheFileType) { + if (!isNaN(parseInt(name))) continue; + const fileTypeAny: any = CacheFileType[name]; + const fileType: CacheFileType = fileTypeAny; + cacheFileList.push(...(await NTQQApi.getFileCacheInfo(fileType)).infos.map(file => file.fileKey)); + } + + // 一并清除 + await NTQQApi.clearChatCache(chatCacheList, cacheFileList); res(); } catch(e) { console.error('清理缓存时发生了错误'); @@ -72,16 +85,10 @@ function deleteCachePath(pathList: string[]) { } } -async function clearChatCache(type: ChatType) { - const cacheList = await getCacheList(type); - return NTQQApi.clearChatCache(cacheList, []); -} - function getCacheList(type: ChatType) { // NOTE: 做这个方法主要是因为目前还不支持针对频道消息的清理 return new Promise>((res, rej) => { NTQQApi.getChatCacheList(type, 1000, 0) .then(data => { - console.log(data); const list = data.infos.filter(e => e.chatType === type && parseInt(e.basicChatCacheInfo.chatSize) > 0); const result = list.map(e => { const result = { ...e.basicChatCacheInfo }; From 66ca9361484cf719098c2b898849c658f1d78eac Mon Sep 17 00:00:00 2001 From: Misa Liu Date: Wed, 28 Feb 2024 00:14:46 +0800 Subject: [PATCH 17/17] style: Finishing code --- src/ntqqapi/ntcall.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index c8bc214..c86fffc 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -704,7 +704,7 @@ export class NTQQApi { } static async setCacheSilentScan(isSilent: boolean = true) { - return await callNTQQApi({ + return await callNTQQApi({ methodName: NTQQApiMethod.CACHE_SET_SILENCE, args: [{ isSilent @@ -713,7 +713,7 @@ export class NTQQApi { } static addCacheScannedPaths(pathMap: object = {}) { - return callNTQQApi({ + return callNTQQApi({ methodName: NTQQApiMethod.CACHE_ADD_SCANNED_PATH, args: [{ pathMap: {...pathMap}, @@ -758,8 +758,7 @@ export class NTQQApi { } static clearCache(cacheKeys: Array = [ 'tmp', 'hotUpdate' ]) { - console.log(cacheKeys); - return callNTQQApi({ + return callNTQQApi({ // TODO: 目前还不知道真正的返回值是什么 methodName: NTQQApiMethod.CACHE_CLEAR, args: [{ keys: cacheKeys @@ -797,7 +796,7 @@ export class NTQQApi { }) } - static async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: unknown[] = []) { + static async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) { return await callNTQQApi({ methodName: NTQQApiMethod.CACHE_CHAT_CLEAR, args: [{