chore: OBAPI

This commit is contained in:
手瓜一十雪
2024-08-09 16:22:35 +08:00
parent d9e016db8b
commit ff29b62398
16 changed files with 28 additions and 165 deletions

View File

@@ -20,9 +20,7 @@ export default class SetGroupKick extends BaseAction<Payload, null> {
actionName = ActionName.SetGroupKick; actionName = ActionName.SetGroupKick;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
const rejectReq = payload.reject_add_request?.toString() == 'true'; const rejectReq = payload.reject_add_request?.toString() == 'true';
await NTQQGroupApi.kickMember(payload.group_id.toString(), [member.uid], rejectReq); await NTQQGroupApi.kickMember(payload.group_id.toString(), [member.uid], rejectReq);
return null; return null;

View File

@@ -17,7 +17,7 @@ export default class SetGroupName extends BaseAction<Payload, null> {
actionName = ActionName.SetGroupName; actionName = ActionName.SetGroupName;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
await NTQQGroupApi.setGroupName(payload.group_id.toString(), payload.group_name); await NTQQGroupApi.setGroupName(payload.group_id.toString(), payload.group_name);
return null; return null;
} }

View File

@@ -1,8 +1,6 @@
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';
const SchemaData = { const SchemaData = {
type: 'object', type: 'object',
properties: { properties: {
@@ -19,6 +17,7 @@ export default class SetGroupWholeBan extends BaseAction<Payload, null> {
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const enable = payload.enable?.toString() !== 'false'; const enable = payload.enable?.toString() !== 'false';
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
await NTQQGroupApi.banGroup(payload.group_id.toString(), enable); await NTQQGroupApi.banGroup(payload.group_id.toString(), enable);
return null; return null;
} }

View File

@@ -1,10 +1,7 @@
import { NTQQMsgApi } from '@/core/apis';
import { ActionName } from '../types'; import { ActionName } from '../types';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { MessageUnique } from '@/common/utils/MessageUnique'; import { MessageUnique } from '@/common/utils/MessageUnique';
import { sleep } from '@/common/utils/helper';
import { NTEventDispatch } from '@/common/utils/EventTask';
import { NodeIKernelMsgListener } from '@/core'; import { NodeIKernelMsgListener } from '@/core';
const SchemaData = { const SchemaData = {
@@ -26,9 +23,10 @@ class DeleteMsg extends BaseAction<Payload, void> {
actionName = ActionName.DeleteMsg; actionName = ActionName.DeleteMsg;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
const msg = MessageUnique.getMsgIdAndPeerByShortId(Number(payload.message_id)); const msg = MessageUnique.getMsgIdAndPeerByShortId(Number(payload.message_id));
if (msg) { if (msg) {
let ret = NTEventDispatch.RegisterListen<NodeIKernelMsgListener['onMsgInfoListUpdate']> let ret = this.CoreContext.eventWrapper.RegisterListen<NodeIKernelMsgListener['onMsgInfoListUpdate']>
( (
'NodeIKernelMsgListener/onMsgInfoListUpdate', 'NodeIKernelMsgListener/onMsgInfoListUpdate',
1, 1,

View File

@@ -1,5 +1,4 @@
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { NTQQMsgApi, NTQQUserApi } from '@/core/apis';
import { ChatType, Peer } from '@/core/entities'; import { ChatType, Peer } from '@/core/entities';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
@@ -19,6 +18,7 @@ type Payload = FromSchema<typeof SchemaData>;
class ForwardSingleMsg extends BaseAction<Payload, null> { class ForwardSingleMsg extends BaseAction<Payload, null> {
protected async getTargetPeer(payload: Payload): Promise<Peer> { protected async getTargetPeer(payload: Payload): Promise<Peer> {
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
if (payload.user_id) { if (payload.user_id) {
const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString()); const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString());
if (!peerUid) { if (!peerUid) {
@@ -30,7 +30,8 @@ class ForwardSingleMsg extends BaseAction<Payload, null> {
} }
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const msg = await MessageUnique.getMsgIdAndPeerByShortId(payload.message_id); const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
const msg = MessageUnique.getMsgIdAndPeerByShortId(payload.message_id);
if (!msg) { if (!msg) {
throw new Error(`无法找到消息${payload.message_id}`); throw new Error(`无法找到消息${payload.message_id}`);
} }

View File

@@ -1,10 +1,9 @@
import { OB11Message } from '../../types'; import { OB11Message } from '../../types';
import { OB11Constructor } from '../../constructor'; import { OB11Constructor } from '../../helper/constructor';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { MessageUnique } from '@/common/utils/MessageUnique'; import { MessageUnique } from '@/common/utils/MessageUnique';
import { NTQQMsgApi } from '@/core';
export type ReturnDataType = OB11Message export type ReturnDataType = OB11Message
@@ -23,6 +22,7 @@ class GetMsg extends BaseAction<Payload, OB11Message> {
actionName = ActionName.GetMsg; actionName = ActionName.GetMsg;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
// log("history msg ids", Object.keys(msgHistory)); // log("history msg ids", Object.keys(msgHistory));
if (!payload.message_id) { if (!payload.message_id) {
throw Error('参数message_id不能为空'); throw Error('参数message_id不能为空');

View File

@@ -16,6 +16,7 @@ type PlayloadType = FromSchema<typeof SchemaData>;
class MarkMsgAsRead extends BaseAction<PlayloadType, null> { class MarkMsgAsRead extends BaseAction<PlayloadType, null> {
async getPeer(payload: PlayloadType): Promise<Peer> { async getPeer(payload: PlayloadType): Promise<Peer> {
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
if (payload.user_id) { if (payload.user_id) {
const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString()); const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString());
if (!peerUid) { if (!peerUid) {
@@ -30,6 +31,7 @@ class MarkMsgAsRead extends BaseAction<PlayloadType, null> {
return { chatType: ChatType.group, peerUid: payload.group_id.toString() }; return { chatType: ChatType.group, peerUid: payload.group_id.toString() };
} }
protected async _handle(payload: PlayloadType): Promise<null> { protected async _handle(payload: PlayloadType): Promise<null> {
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
// 调用API // 调用API
const ret = await NTQQMsgApi.setMsgRead(await this.getPeer(payload)); const ret = await NTQQMsgApi.setMsgRead(await this.getPeer(payload));
if (ret.result != 0) { if (ret.result != 0) {
@@ -65,6 +67,7 @@ export class MarkAllMsgAsRead extends BaseAction<Payload, null> {
actionName = ActionName._MarkAllMsgAsRead; actionName = ActionName._MarkAllMsgAsRead;
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
await NTQQMsgApi.markallMsgAsRead(); await NTQQMsgApi.markallMsgAsRead();
return null; return null;
} }

View File

@@ -1,4 +1,4 @@
import { OB11MessageData } from '@/onebot11/types'; import { OB11MessageData } from '@/onebot/types';
function checkSendMessage(sendMsgList: OB11MessageData[]) { function checkSendMessage(sendMsgList: OB11MessageData[]) {
function checkUri(uri: string): boolean { function checkUri(uri: string): boolean {

View File

@@ -19,6 +19,7 @@ export class SetMsgEmojiLike extends BaseAction<Payload, any> {
actionName = ActionName.SetMsgEmojiLike; actionName = ActionName.SetMsgEmojiLike;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
const msg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(payload.message_id.toString())); const msg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(payload.message_id.toString()));
if (!msg) { if (!msg) {
throw new Error('msg not found'); throw new Error('msg not found');

View File

@@ -1,93 +0,0 @@
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import fs from 'fs';
import Path from 'path';
import {
ChatType,
ChatCacheListItemBasic,
CacheFileType
} from '@/core/entities';
import { NTQQFileApi, NTQQFileCacheApi } from '@/core/apis/file';
import { logError } from '@/common/utils/log';
export default class CleanCache extends BaseAction<void, void> {
actionName = ActionName.CleanCache;
protected _handle(): Promise<void> {
return new Promise<void>(async (res, rej) => {
try {
// dbUtil.clearCache();
const cacheFilePaths: string[] = [];
await NTQQFileCacheApi.setCacheSilentScan(false);
cacheFilePaths.push((await NTQQFileCacheApi.getHotUpdateCachePath()));
cacheFilePaths.push((await NTQQFileCacheApi.getDesktopTmpPath()));
(await NTQQFileCacheApi.getCacheSessionPathList()).forEach((e: { value: string; }) => cacheFilePaths.push(e.value));
// await NTQQApi.addCacheScannedPaths(); // XXX: 调用就崩溃,原因目前还未知
const cacheScanResult = await NTQQFileCacheApi.scanCache();
const cacheSize = parseInt(cacheScanResult.size[6]);
if (cacheScanResult.result !== 0) {
throw('Something went wrong while scanning cache. Code: ' + cacheScanResult.result);
}
await NTQQFileCacheApi.setCacheSilentScan(true);
if (cacheSize > 0 && cacheFilePaths.length > 2) { // 存在缓存文件且大小不为 0 时执行清理动作
// await NTQQApi.clearCache([ 'tmp', 'hotUpdate', ...cacheScanResult ]) // XXX: 也是调用就崩溃,调用 fs 删除得了
deleteCachePath(cacheFilePaths);
}
// 获取聊天记录列表
// NOTE: 以防有人不需要删除聊天记录,暂时先注释掉,日后加个开关
// 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 NTQQFileCacheApi.getFileCacheInfo(fileType)).infos.map((file: { fileKey: any; }) => file.fileKey));
}
// 一并清除
await NTQQFileCacheApi.clearChatCache(chatCacheList, cacheFileList);
res();
} catch(e) {
logError('清理缓存时发生了错误');
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);
}
}
function getCacheList(type: ChatType) { // NOTE: 做这个方法主要是因为目前还不支持针对频道消息的清理
return new Promise<Array<ChatCacheListItemBasic>>((res, rej) => {
});
}

View File

@@ -1,16 +1,14 @@
import { selfInfo } from '@/core/data';
import { OB11User } from '../../types'; import { OB11User } from '../../types';
import { OB11Constructor } from '../../constructor'; import { OB11Constructor } from '../../helper/constructor';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { napCatCore } from '@/core';
class GetLoginInfo extends BaseAction<null, OB11User> { class GetLoginInfo extends BaseAction<null, OB11User> {
actionName = ActionName.GetLoginInfo; actionName = ActionName.GetLoginInfo;
protected async _handle(payload: null) { protected async _handle(payload: null) {
return OB11Constructor.selfInfo(selfInfo); return OB11Constructor.selfInfo(this.CoreContext.selfInfo);
} }
} }

View File

@@ -1,17 +1,14 @@
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { OB11Status } from '../../types';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { selfInfo, stat } from '@/core/data';
export default class GetStatus extends BaseAction<any, any> {
export default class GetStatus extends BaseAction<any, OB11Status> {
actionName = ActionName.GetStatus; actionName = ActionName.GetStatus;
protected async _handle(payload: any): Promise<OB11Status> { protected async _handle(payload: any): Promise<any> {
return { return {
online: !!selfInfo.online, online: !!this.CoreContext.selfInfo.online,
good: true, good: true,
stat stat:{}
}; };
} }
} }

View File

@@ -1,43 +0,0 @@
import { rebootWithNormolLogin, rebootWithQuickLogin } from '@/common/utils/reboot';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { selfInfo } from '@/core/data';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
const SchemaData = {
type: 'object',
properties: {
delay: { type: 'number' }
}
} as const satisfies JSONSchema;
type Payload = FromSchema<typeof SchemaData>;
export class Reboot extends BaseAction<Payload, null> {
actionName = ActionName.Reboot;
protected async _handle(payload: Payload): Promise<null> {
if (payload.delay) {
setTimeout(() => {
rebootWithQuickLogin(selfInfo.uin);
}, payload.delay);
} else {
rebootWithQuickLogin(selfInfo.uin);
}
return null;
}
}
export class RebootNormal extends BaseAction<Payload, null> {
actionName = ActionName.RebootNormal;
protected async _handle(payload: Payload): Promise<null> {
if (payload.delay) {
setTimeout(() => {
rebootWithNormolLogin();
}, payload.delay);
} else {
rebootWithNormolLogin();
}
return null;
}
}

View File

@@ -1,6 +1,6 @@
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { NTQQUserApi, WebApi } from '@/core/apis';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface Response { interface Response {
cookies: string, cookies: string,
@@ -20,6 +20,8 @@ export class GetCookies extends BaseAction<Payload, Response> {
actionName = ActionName.GetCookies; actionName = ActionName.GetCookies;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
const NTQQWebApi = this.CoreContext.getApiContext().WebApi;
// if (!payload.domain) { // if (!payload.domain) {
// throw new Error('缺少参数 domain'); // throw new Error('缺少参数 domain');
// } // }
@@ -63,7 +65,7 @@ export class GetCookies extends BaseAction<Payload, Response> {
const cookiesObject = await NTQQUserApi.getCookies(payload.domain); const cookiesObject = await NTQQUserApi.getCookies(payload.domain);
//把获取到的cookiesObject转换成 k=v; 格式字符串拼接在一起 //把获取到的cookiesObject转换成 k=v; 格式字符串拼接在一起
const cookies = Object.entries(cookiesObject).map(([key, value]) => `${key}=${value}`).join('; '); const cookies = Object.entries(cookiesObject).map(([key, value]) => `${key}=${value}`).join('; ');
const bkn = WebApi.genBkn(cookiesObject.p_skey); const bkn = NTQQWebApi.getBknFromCookie(cookiesObject.p_skey);
return { cookies, bkn }; return { cookies, bkn };
} }
} }

View File

@@ -1,4 +1,4 @@
import { NTQQUserApi } from '@/core/apis';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
@@ -18,6 +18,7 @@ export default class SendLike extends BaseAction<Payload, null> {
actionName = ActionName.SendLike; actionName = ActionName.SendLike;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
//logDebug('点赞参数', payload); //logDebug('点赞参数', payload);
try { try {
const qq = payload.user_id.toString(); const qq = payload.user_id.toString();

View File

@@ -19,6 +19,7 @@ export default class SetFriendAddRequest extends BaseAction<Payload, null> {
actionName = ActionName.SetFriendAddRequest; actionName = ActionName.SetFriendAddRequest;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const NTQQFriendApi = this.CoreContext.getApiContext().FriendApi;
const approve = payload.approve?.toString() !== 'false'; const approve = payload.approve?.toString() !== 'false';
await NTQQFriendApi.handleFriendRequest(payload.flag, approve); await NTQQFriendApi.handleFriendRequest(payload.flag, approve);
return null; return null;