mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
feat: action check data
This commit is contained in:
@@ -48,6 +48,7 @@
|
|||||||
"vite-tsconfig-paths": "^4.3.2"
|
"vite-tsconfig-paths": "^4.3.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"ajv": "^8.13.0",
|
||||||
"commander": "^12.0.0",
|
"commander": "^12.0.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^5.0.0-beta.2",
|
"express": "^5.0.0-beta.2",
|
||||||
@@ -55,6 +56,7 @@
|
|||||||
"file-type": "^19.0.0",
|
"file-type": "^19.0.0",
|
||||||
"fluent-ffmpeg": "^2.1.2",
|
"fluent-ffmpeg": "^2.1.2",
|
||||||
"image-size": "^1.1.1",
|
"image-size": "^1.1.1",
|
||||||
|
"json-schema-to-ts": "^3.1.0",
|
||||||
"log4js": "^6.9.1",
|
"log4js": "^6.9.1",
|
||||||
"qrcode-terminal": "^0.12.0",
|
"qrcode-terminal": "^0.12.0",
|
||||||
"silk-wasm": "^3.3.4",
|
"silk-wasm": "^3.3.4",
|
||||||
|
31
src/common/utils/type.ts
Normal file
31
src/common/utils/type.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* 运行时类型转换与检查类
|
||||||
|
*/
|
||||||
|
export class TypeCheck {
|
||||||
|
static isEmpty(value: any): boolean {
|
||||||
|
return value === null || value === undefined || value === '' ||
|
||||||
|
(Array.isArray(value) && value.length === 0) || (typeof value === 'object' && Object.keys(value).length === 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TypeConvert {
|
||||||
|
static toNumber(value: any): number {
|
||||||
|
const num = Number(value);
|
||||||
|
if (isNaN(num)) {
|
||||||
|
throw new Error(`无法将输入转换为数字: ${value}`);
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
static toString(value: any): string {
|
||||||
|
return String(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static toBoolean(value: any): boolean {
|
||||||
|
return Boolean(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static toArray(value: any): any[] {
|
||||||
|
return Array.isArray(value) ? value : [value];
|
||||||
|
}
|
||||||
|
}
|
@@ -1,16 +1,25 @@
|
|||||||
import { ActionName, BaseCheckResult } from './types';
|
import { ActionName, BaseCheckResult } from './types';
|
||||||
import { OB11Response } from './OB11Response';
|
import { OB11Response } from './OB11Response';
|
||||||
import { OB11Return } from '../types';
|
import { OB11Return } from '../types';
|
||||||
|
|
||||||
import { log, logError } from '../../common/utils/log';
|
import { log, logError } from '../../common/utils/log';
|
||||||
|
import Ajv from 'ajv';
|
||||||
|
|
||||||
class BaseAction<PayloadType, ReturnDataType> {
|
class BaseAction<PayloadType, ReturnDataType> {
|
||||||
actionName!: ActionName;
|
actionName!: ActionName;
|
||||||
|
private validate: any = undefined;
|
||||||
|
PayloadSchema: any = undefined;
|
||||||
protected async check(payload: PayloadType): Promise<BaseCheckResult> {
|
protected async check(payload: PayloadType): Promise<BaseCheckResult> {
|
||||||
|
if (this.validate && !this.validate(payload)) {
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: this.validate.errors?.map((e: { message: any; }) => e.message).join(',') as string
|
||||||
|
}
|
||||||
|
} else if (this.PayloadSchema) {
|
||||||
|
this.validate = new Ajv().compile(this.PayloadSchema);
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
valid: true,
|
valid: true
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
|
public async handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
|
||||||
|
@@ -2,17 +2,27 @@ import { OB11User } from '../../types';
|
|||||||
import { OB11Constructor } from '../../constructor';
|
import { OB11Constructor } from '../../constructor';
|
||||||
import { friends } from '@/core/data';
|
import { friends } from '@/core/data';
|
||||||
import BaseAction from '../BaseAction';
|
import BaseAction from '../BaseAction';
|
||||||
import { ActionName } from '../types';
|
import { ActionName, BaseCheckResult } from '../types';
|
||||||
import { NTQQUserApi } from '@/core/apis';
|
import { NTQQUserApi } from '@/core/apis';
|
||||||
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||||
|
import Ajv from "ajv"
|
||||||
// 设置在线状态
|
// 设置在线状态
|
||||||
interface Payload {
|
|
||||||
status: number;
|
const SchemaData = {
|
||||||
extStatus: number;
|
type: 'object',
|
||||||
batteryStatus: number;
|
properties: {
|
||||||
}
|
status: { type: 'number' },
|
||||||
|
extStatus: { type: 'number' },
|
||||||
|
batteryStatus: { type: 'number' }
|
||||||
|
},
|
||||||
|
required: ['status', 'extStatus', 'batteryStatus'],
|
||||||
|
} as const satisfies JSONSchema;
|
||||||
|
|
||||||
|
type Payload = FromSchema<typeof SchemaData>;
|
||||||
|
|
||||||
export class SetOnlineStatus extends BaseAction<Payload, null> {
|
export class SetOnlineStatus extends BaseAction<Payload, null> {
|
||||||
actionName = ActionName.SetOnlineStatus;
|
actionName = ActionName.SetOnlineStatus;
|
||||||
|
PayloadSchema = SchemaData;
|
||||||
protected async _handle(payload: Payload) {
|
protected async _handle(payload: Payload) {
|
||||||
// 可设置状态
|
// 可设置状态
|
||||||
// { status: 10, extStatus: 1027, batteryStatus: 0 }
|
// { status: 10, extStatus: 1027, batteryStatus: 0 }
|
||||||
|
@@ -1,27 +1,38 @@
|
|||||||
import BaseAction from '../BaseAction';
|
import BaseAction from '../BaseAction';
|
||||||
import { ActionName } from '../types';
|
import { ActionName, BaseCheckResult } from '../types';
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import { NTQQUserApi } from '@/core/apis/user';
|
import { NTQQUserApi } from '@/core/apis/user';
|
||||||
import { checkFileReceived, uri2local } from '@/common/utils/file';
|
import { checkFileReceived, uri2local } from '@/common/utils/file';
|
||||||
// import { log } from "../../../common/utils";
|
// import { log } from "../../../common/utils";
|
||||||
|
|
||||||
interface Payload {
|
interface Payload {
|
||||||
file: string
|
file: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class SetAvatar extends BaseAction<Payload, null> {
|
export default class SetAvatar extends BaseAction<Payload, null> {
|
||||||
actionName = ActionName.SetQQAvatar;
|
actionName = ActionName.SetQQAvatar;
|
||||||
|
// 用不着复杂检测
|
||||||
|
protected async check(payload: Payload): Promise<BaseCheckResult> {
|
||||||
|
if (!payload.file || typeof payload.file != "string") {
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: 'file字段不能为空或者类型错误',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
valid: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
protected async _handle(payload: Payload): Promise<null> {
|
protected async _handle(payload: Payload): Promise<null> {
|
||||||
const { path, isLocal, errMsg } = (await uri2local(payload.file));
|
const { path, isLocal, errMsg } = (await uri2local(payload.file));
|
||||||
if (errMsg){
|
if (errMsg) {
|
||||||
throw `头像${payload.file}设置失败,file字段可能格式不正确`;
|
throw `头像${payload.file}设置失败,file字段可能格式不正确`;
|
||||||
}
|
}
|
||||||
if (path) {
|
if (path) {
|
||||||
await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃,需要提前判断
|
await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃,需要提前判断
|
||||||
const ret = await NTQQUserApi.setQQAvatar(path);
|
const ret = await NTQQUserApi.setQQAvatar(path);
|
||||||
if (!isLocal){
|
if (!isLocal) {
|
||||||
fs.unlink(path, () => {});
|
fs.unlink(path, () => { });
|
||||||
}
|
}
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
throw `头像${payload.file}设置失败,api无返回`;
|
throw `头像${payload.file}设置失败,api无返回`;
|
||||||
@@ -29,15 +40,15 @@ export default class SetAvatar extends BaseAction<Payload, null> {
|
|||||||
// log(`头像设置返回:${JSON.stringify(ret)}`)
|
// log(`头像设置返回:${JSON.stringify(ret)}`)
|
||||||
if (ret['result'] == 1004022) {
|
if (ret['result'] == 1004022) {
|
||||||
throw `头像${payload.file}设置失败,文件可能不是图片格式`;
|
throw `头像${payload.file}设置失败,文件可能不是图片格式`;
|
||||||
} else if(ret['result'] != 0) {
|
} else if (ret['result'] != 0) {
|
||||||
throw `头像${payload.file}设置失败,未知的错误,${ret['result']}:${ret['errMsg']}`;
|
throw `头像${payload.file}设置失败,未知的错误,${ret['result']}:${ret['errMsg']}`;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!isLocal){
|
if (!isLocal) {
|
||||||
fs.unlink(path, () => {});
|
fs.unlink(path, () => { });
|
||||||
}
|
}
|
||||||
throw `头像${payload.file}设置失败,无法获取头像,文件可能不存在`;
|
throw `头像${payload.file}设置失败,无法获取头像,文件可能不存在`;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,9 +5,11 @@ import { ob11Config } from '@/onebot11/config';
|
|||||||
import { log, logDebug } from '@/common/utils/log';
|
import { log, logDebug } from '@/common/utils/log';
|
||||||
import { sleep } from '@/common/utils/helper';
|
import { sleep } from '@/common/utils/helper';
|
||||||
import { uri2local } from '@/common/utils/file';
|
import { uri2local } from '@/common/utils/file';
|
||||||
import { ActionName } from '../types';
|
import { ActionName, BaseCheckResult } from '../types';
|
||||||
import { FileElement, RawMessage, VideoElement } from '@/core/entities';
|
import { FileElement, RawMessage, VideoElement } from '@/core/entities';
|
||||||
import { NTQQFileApi } from '@/core/apis';
|
import { NTQQFileApi } from '@/core/apis';
|
||||||
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||||
|
import Ajv from 'ajv';
|
||||||
|
|
||||||
export interface GetFilePayload {
|
export interface GetFilePayload {
|
||||||
file: string; // 文件名或者fileUuid
|
file: string; // 文件名或者fileUuid
|
||||||
@@ -20,8 +22,16 @@ export interface GetFileResponse {
|
|||||||
file_name?: string;
|
file_name?: string;
|
||||||
base64?: string;
|
base64?: string;
|
||||||
}
|
}
|
||||||
|
const GetFileBase_PayloadSchema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
file: { type: 'string' }
|
||||||
|
},
|
||||||
|
required: ['file']
|
||||||
|
} as const satisfies JSONSchema;
|
||||||
|
|
||||||
export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
|
export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
|
||||||
|
PayloadSchema: any = GetFileBase_PayloadSchema;
|
||||||
private getElement(msg: RawMessage): { id: string, element: VideoElement | FileElement } {
|
private getElement(msg: RawMessage): { id: string, element: VideoElement | FileElement } {
|
||||||
let element = msg.elements.find(e => e.fileElement);
|
let element = msg.elements.find(e => e.fileElement);
|
||||||
if (!element) {
|
if (!element) {
|
||||||
@@ -34,7 +44,6 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
|
|||||||
}
|
}
|
||||||
return { id: element.elementId, element: element.fileElement };
|
return { id: element.elementId, element: element.fileElement };
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async _handle(payload: GetFilePayload): Promise<GetFileResponse> {
|
protected async _handle(payload: GetFilePayload): Promise<GetFileResponse> {
|
||||||
let cache = await dbUtil.getFileCacheByName(payload.file);
|
let cache = await dbUtil.getFileCacheByName(payload.file);
|
||||||
if (!cache) {
|
if (!cache) {
|
||||||
@@ -102,13 +111,25 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GetFile_PayloadSchema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
file_id: { type: 'string' },
|
||||||
|
file: { type: 'string' }
|
||||||
|
},
|
||||||
|
required: ['file_id']
|
||||||
|
} as const satisfies JSONSchema;
|
||||||
|
|
||||||
|
type GetFile_Payload_Internal = FromSchema<typeof GetFile_PayloadSchema>;
|
||||||
|
|
||||||
|
interface GetFile_Payload extends GetFile_Payload_Internal {
|
||||||
|
file: string
|
||||||
|
}
|
||||||
|
|
||||||
export default class GetFile extends GetFileBase {
|
export default class GetFile extends GetFileBase {
|
||||||
actionName = ActionName.GetFile;
|
actionName = ActionName.GetFile;
|
||||||
|
PayloadSchema = GetFile_PayloadSchema;
|
||||||
protected async _handle(payload: { file_id: string, file: string }): Promise<GetFileResponse> {
|
protected async _handle(payload: GetFile_Payload): Promise<GetFileResponse> {
|
||||||
if (!payload.file_id) {
|
|
||||||
throw new Error('file_id 不能为空');
|
|
||||||
}
|
|
||||||
payload.file = payload.file_id;
|
payload.file = payload.file_id;
|
||||||
return super._handle(payload);
|
return super._handle(payload);
|
||||||
}
|
}
|
||||||
|
@@ -1,25 +1,46 @@
|
|||||||
import BaseAction from '../BaseAction';
|
import BaseAction from '../BaseAction';
|
||||||
import { ActionName } from '../types';
|
import { ActionName, BaseCheckResult } from '../types';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { join as joinPath } from 'node:path';
|
import { join as joinPath } from 'node:path';
|
||||||
import { calculateFileMD5, getTempDir, httpDownload } from '@/common/utils/file';
|
import { calculateFileMD5, getTempDir, httpDownload } from '@/common/utils/file';
|
||||||
import { v4 as uuid4 } from 'uuid';
|
import { v4 as uuid4 } from 'uuid';
|
||||||
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||||
interface Payload {
|
import Ajv from 'ajv';
|
||||||
thread_count?: number;
|
|
||||||
url?: string;
|
|
||||||
base64?: string;
|
|
||||||
name?: string;
|
|
||||||
headers?: string | string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface FileResponse {
|
interface FileResponse {
|
||||||
file: string;
|
file: string;
|
||||||
}
|
}
|
||||||
|
const PayloadSchema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
thread_count: { type: 'number' },
|
||||||
|
url: { type: 'string' },
|
||||||
|
base64: { type: 'string' },
|
||||||
|
name: { type: 'string' },
|
||||||
|
headers: {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
type: "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
} as const satisfies JSONSchema;
|
||||||
|
|
||||||
|
type Payload = FromSchema<typeof PayloadSchema>;
|
||||||
|
|
||||||
export default class GoCQHTTPDownloadFile extends BaseAction<Payload, FileResponse> {
|
export default class GoCQHTTPDownloadFile extends BaseAction<Payload, FileResponse> {
|
||||||
actionName = ActionName.GoCQHTTP_DownloadFile;
|
actionName = ActionName.GoCQHTTP_DownloadFile;
|
||||||
|
validateDownload = new Ajv().compile(PayloadSchema);
|
||||||
|
// 这里重写是为了兼容 headers可能出现 string string[]
|
||||||
|
protected async check(payload: Payload): Promise<BaseCheckResult> {
|
||||||
|
if (payload.headers) {
|
||||||
|
// 如果存在headers 为数组则开始兼容string string[]
|
||||||
|
payload.headers = payload?.headers && Array.isArray(payload.headers) ? payload.headers : [payload.headers as unknown as string];
|
||||||
|
}
|
||||||
|
if (!this.validateDownload(payload)) {
|
||||||
|
return { valid: false, message: this.validateDownload.errors?.map(e => e.message).join(', ') as string };
|
||||||
|
}
|
||||||
|
return { valid: true };
|
||||||
|
}
|
||||||
protected async _handle(payload: Payload): Promise<FileResponse> {
|
protected async _handle(payload: Payload): Promise<FileResponse> {
|
||||||
const isRandomName = !payload.name;
|
const isRandomName = !payload.name;
|
||||||
const name = payload.name || uuid4();
|
const name = payload.name || uuid4();
|
||||||
|
@@ -3,12 +3,19 @@ import { OB11ForwardMessage, OB11Message, OB11MessageData } from '../../types';
|
|||||||
import { NTQQMsgApi } from '@/core/apis';
|
import { NTQQMsgApi } from '@/core/apis';
|
||||||
import { dbUtil } from '@/core/utils/db';
|
import { dbUtil } from '@/core/utils/db';
|
||||||
import { OB11Constructor } from '../../constructor';
|
import { OB11Constructor } from '../../constructor';
|
||||||
import { ActionName } from '../types';
|
import { ActionName, BaseCheckResult } from '../types';
|
||||||
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||||
|
import Ajv from 'ajv';
|
||||||
|
|
||||||
interface Payload {
|
const SchemaData = {
|
||||||
message_id: string; // long msg id
|
type: 'object',
|
||||||
id?: string; // short msg id
|
properties: {
|
||||||
}
|
message_id: { type: 'string' },
|
||||||
|
id: { type: 'string' }
|
||||||
|
},
|
||||||
|
} as const satisfies JSONSchema;
|
||||||
|
|
||||||
|
type Payload = FromSchema<typeof SchemaData>;
|
||||||
|
|
||||||
interface Response {
|
interface Response {
|
||||||
messages: (OB11Message & { content: OB11MessageData })[];
|
messages: (OB11Message & { content: OB11MessageData })[];
|
||||||
@@ -16,7 +23,7 @@ interface Response {
|
|||||||
|
|
||||||
export class GoCQHTTGetForwardMsgAction extends BaseAction<Payload, any> {
|
export class GoCQHTTGetForwardMsgAction extends BaseAction<Payload, any> {
|
||||||
actionName = ActionName.GoCQHTTP_GetForwardMsg;
|
actionName = ActionName.GoCQHTTP_GetForwardMsg;
|
||||||
|
PayloadSchema = SchemaData;
|
||||||
protected async _handle(payload: Payload): Promise<any> {
|
protected async _handle(payload: Payload): Promise<any> {
|
||||||
const msgId = payload.message_id || payload.id;
|
const msgId = payload.message_id || payload.id;
|
||||||
if (!msgId) {
|
if (!msgId) {
|
||||||
@@ -25,7 +32,7 @@ export class GoCQHTTGetForwardMsgAction extends BaseAction<Payload, any> {
|
|||||||
let rootMsg = await dbUtil.getMsgByLongId(msgId);
|
let rootMsg = await dbUtil.getMsgByLongId(msgId);
|
||||||
if (!rootMsg) {
|
if (!rootMsg) {
|
||||||
rootMsg = await dbUtil.getMsgByShortId(parseInt(msgId));
|
rootMsg = await dbUtil.getMsgByShortId(parseInt(msgId));
|
||||||
if (!rootMsg){
|
if (!rootMsg) {
|
||||||
throw Error('msg not found');
|
throw Error('msg not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user