chore: NTAPI

This commit is contained in:
手瓜一十雪 2024-08-09 13:42:04 +08:00
parent 9ef3edabce
commit 52ab93013c
2 changed files with 40 additions and 28 deletions

View File

@ -1,5 +1,12 @@
import { InstanceContext, NapCatCore } from "..";
export class NTQQCollectionApi { export class NTQQCollectionApi {
context: InstanceContext;
core: NapCatCore;
constructor(context: InstanceContext, core: NapCatCore) {
this.context = context;
this.core = core;
}
async createCollection(authorUin: string, authorUid: string, authorName: string, brief: string, rawData: string) { async createCollection(authorUin: string, authorUid: string, authorName: string, brief: string, rawData: string) {
let param = { let param = {
commInfo: { commInfo: {
@ -35,7 +42,7 @@ export class NTQQCollectionApi {
}, },
need_share_url: false need_share_url: false
}; };
return napCatCore.session.getCollectionService().createNewCollectionItem(param); return this.context.session.getCollectionService().createNewCollectionItem(param);
} }
async getAllCollection(category: number = 0, count: number = 50) { async getAllCollection(category: number = 0, count: number = 50) {
let param = { let param = {
@ -47,6 +54,6 @@ export class NTQQCollectionApi {
count: count, count: count,
searchDown: true searchDown: true
}; };
return napCatCore.session.getCollectionService().getCollectionItemList(param); return this.context.session.getCollectionService().getCollectionItemList(param);
} }
} }

View File

@ -8,37 +8,39 @@ import {
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import fsPromises from 'fs/promises'; import fsPromises from 'fs/promises';
import { GeneralCallResult, napCatCore, OnRichMediaDownloadCompleteParams } from '@/core'; import { InstanceContext, NapCatCore } from '@/core';
import { calculateFileMD5 } from '@/common/utils/file';
import * as fileType from 'file-type'; import * as fileType from 'file-type';
import imageSize from 'image-size'; import imageSize from 'image-size';
import { ISizeCalculationResult } from 'image-size/dist/types/interface'; import { ISizeCalculationResult } from 'image-size/dist/types/interface';
import { sessionConfig } from '@/core/sessionConfig';
import { rkeyManager } from '../utils/rkey';
import { NTEventDispatch } from '@/common/utils/EventTask';
import { NodeIKernelSearchService } from '../services/NodeIKernelSearchService'; import { NodeIKernelSearchService } from '../services/NodeIKernelSearchService';
export class NTQQFileApi { export class NTQQFileApi {
context: InstanceContext;
core: NapCatCore;
constructor(context: InstanceContext, core: NapCatCore) {
this.context = context;
this.core = core;
}
async getFileType(filePath: string) { async getFileType(filePath: string) {
return fileType.fileTypeFromFile(filePath); return fileType.fileTypeFromFile(filePath);
} }
async copyFile(filePath: string, destPath: string) { async copyFile(filePath: string, destPath: string) {
await napCatCore.util.copyFile(filePath, destPath); await this.context.wrapper.util.copyFile(filePath, destPath);
} }
async getFileSize(filePath: string): Promise<number> { async getFileSize(filePath: string): Promise<number> {
return await napCatCore.util.getFileSize(filePath); return await this.context.wrapper.util.getFileSize(filePath);
} }
async getVideoUrl(peer: Peer, msgId: string, elementId: string) { async getVideoUrl(peer: Peer, msgId: string, elementId: string) {
return (await napCatCore.session.getRichMediaService().getVideoPlayUrlV2(peer, msgId, elementId, 0, { downSourceType: 1, triggerType: 1 })).urlResult.domainUrl; return (await this.context.session.getRichMediaService().getVideoPlayUrlV2(peer, msgId, elementId, 0, { downSourceType: 1, triggerType: 1 })).urlResult.domainUrl;
} }
// 上传文件到QQ的文件夹 // 上传文件到QQ的文件夹
async uploadFile(filePath: string, elementType: ElementType = ElementType.PIC, elementSubType: number = 0) { async uploadFile(filePath: string, elementType: ElementType = ElementType.PIC, elementSubType: number = 0) {
// napCatCore.wrapper.util. // napCatCore.wrapper.util.
const fileMd5 = await calculateFileMD5(filePath); const fileMd5 = await calculateFileMD5(filePath);
let ext: string = (await NTQQFileApi.getFileType(filePath))?.ext as string || ''; let ext: string = (await this.getFileType(filePath))?.ext as string || '';
if (ext) { if (ext) {
ext = '.' + ext; ext = '.' + ext;
} }
@ -46,7 +48,7 @@ export class NTQQFileApi {
if (fileName.indexOf('.') === -1) { if (fileName.indexOf('.') === -1) {
fileName += ext; fileName += ext;
} }
const mediaPath = napCatCore.session.getMsgService().getRichMediaFilePathForGuild({ const mediaPath = this.context.session.getMsgService().getRichMediaFilePathForGuild({
md5HexStr: fileMd5, md5HexStr: fileMd5,
fileName: fileName, fileName: fileName,
elementType: elementType, elementType: elementType,
@ -56,8 +58,8 @@ export class NTQQFileApi {
downloadType: 1, downloadType: 1,
file_uuid: '' file_uuid: ''
}); });
await NTQQFileApi.copyFile(filePath, mediaPath!); await this.copyFile(filePath, mediaPath!);
const fileSize = await NTQQFileApi.getFileSize(filePath); const fileSize = await this.getFileSize(filePath);
return { return {
md5: fileMd5, md5: fileMd5,
fileName, fileName,
@ -83,7 +85,7 @@ export class NTQQFileApi {
return sourcePath; return sourcePath;
} }
} }
let data = await NTEventDispatch.CallNormalEvent< let data = await this.core.eventWrapper.CallNormalEvent<
( (
params: { params: {
fileModelId: string, fileModelId: string,
@ -168,7 +170,7 @@ export class NTQQFileApi {
return undefined; return undefined;
} }
return napCatCore.session.getSearchService().addSearchHistory({ return this.context.session.getSearchService().addSearchHistory({
type: 4, type: 4,
contactList: [], contactList: [],
id: -1, id: -1,
@ -242,9 +244,9 @@ export class NTQQFileApi {
}[] }[]
}[] }[]
}; };
const Event = await NTEventDispatch.CreatEventFunction<EventType>('NodeIKernelSearchService/searchFileWithKeywords'); const Event = this.core.eventWrapper.createEventFunction<EventType>('NodeIKernelSearchService/searchFileWithKeywords');
let id = ''; let id = '';
const Listener = NTEventDispatch.RegisterListen<(params: OnListener) => void>('NodeIKernelSearchListener/onSearchFileKeywordsResult', 1, 20000, (params) => { const Listener = this.core.eventWrapper.RegisterListen<(params: OnListener) => void>('NodeIKernelSearchListener/onSearchFileKeywordsResult', 1, 20000, (params) => {
if (id !== '' && params.searchId == id) { if (id !== '' && params.searchId == id) {
return true return true
} }
@ -283,12 +285,18 @@ export class NTQQFileApi {
// 没有url需要自己拼接 // 没有url需要自己拼接
return `${IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${(fileMd5 || md5HexStr)!.toUpperCase()}/0`; return `${IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${(fileMd5 || md5HexStr)!.toUpperCase()}/0`;
} }
logDebug('图片url获取失败', element); this.context.logger.logDebug('图片url获取失败', element);
return ''; return '';
} }
} }
export class NTQQFileCacheApi { export class NTQQFileCacheApi {
context: InstanceContext;
core: NapCatCore;
constructor(context: InstanceContext, core: NapCatCore) {
this.context = context;
this.core = core;
}
async setCacheSilentScan(isSilent: boolean = true) { async setCacheSilentScan(isSilent: boolean = true) {
return ''; return '';
} }
@ -299,18 +307,15 @@ export class NTQQFileCacheApi {
clearCache(cacheKeys: Array<string> = ['tmp', 'hotUpdate']) { clearCache(cacheKeys: Array<string> = ['tmp', 'hotUpdate']) {
// 参数未验证 // 参数未验证
return napCatCore.session.getStorageCleanService().clearCacheDataByKeys(cacheKeys); return this.context.session.getStorageCleanService().clearCacheDataByKeys(cacheKeys);
} }
addCacheScannedPaths(pathMap: object = {}) { addCacheScannedPaths(pathMap: object = {}) {
return napCatCore.session.getStorageCleanService().addCacheScanedPaths(pathMap); return this.context.session.getStorageCleanService().addCacheScanedPaths(pathMap);
} }
scanCache(): Promise<GeneralCallResult & { scanCache() {
size: string[] //return (await this.context.session.getStorageCleanService().scanCache()).size;
}> {
// 需要注册Listener onFinishScan
return napCatCore.session.getStorageCleanService().scanCache();
} }
getHotUpdateCachePath() { getHotUpdateCachePath() {
@ -324,7 +329,7 @@ export class NTQQFileCacheApi {
} }
getChatCacheList(type: ChatType, pageSize: number = 1000, pageIndex: number = 0) { getChatCacheList(type: ChatType, pageSize: number = 1000, pageIndex: number = 0) {
return napCatCore.session.getStorageCleanService().getChatCacheInfo(type, pageSize, 1, pageIndex); return this.context.session.getStorageCleanService().getChatCacheInfo(type, pageSize, 1, pageIndex);
} }
getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) { getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) {
@ -334,6 +339,6 @@ export class NTQQFileCacheApi {
} }
async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) { async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) {
return napCatCore.session.getStorageCleanService().clearChatCacheInfo(chats, fileKeys); return this.context.session.getStorageCleanService().clearChatCacheInfo(chats, fileKeys);
} }
} }