feat: check action data 3

This commit is contained in:
手瓜一十雪 2024-05-18 12:40:41 +08:00
parent 62eee5f05c
commit d6175acd38
12 changed files with 151 additions and 78 deletions

View File

@ -6,17 +6,23 @@ import { ChatType, SendFileElement } from '@/core/entities';
import fs from 'fs';
import { NTQQMsgApi } from '@/core/apis/msg';
import { uri2local } from '@/common/utils/file';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
file: { type: 'string' },
name: { type: 'string' },
folder: { type: 'string' }
},
required: ['group_id', 'file', 'name', 'folder']
} as const satisfies JSONSchema;
interface Payload {
group_id: number;
file: string;
name: string;
folder: string;
}
type Payload = FromSchema<typeof SchemaData>;
export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> {
actionName = ActionName.GoCQHTTP_UploadGroupFile;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> {
const group = await getGroup(payload.group_id.toString());
if (!group) {

View File

@ -5,16 +5,23 @@ import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { NTQQMsgApi } from '@/core/apis/msg';
import { GroupEssenceMsgRet, WebApi } from '@/core/apis/webapi';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface PayloadType {
group_id: number;
pages: number;
}
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
pages: { type: 'number' },
},
required: ['group_id', 'pages']
} as const satisfies JSONSchema;
export class GetGroupEssence extends BaseAction<PayloadType, GroupEssenceMsgRet> {
type Payload = FromSchema<typeof SchemaData>;
export class GetGroupEssence extends BaseAction<Payload, GroupEssenceMsgRet> {
actionName = ActionName.GoCQHTTP_GetEssenceMsg;
protected async _handle(payload: PayloadType) {
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
const ret = await WebApi.getGroupEssenceMsg(payload.group_id.toString(), payload.pages.toString());
if (!ret) {
throw new Error('获取失败');

View File

@ -3,15 +3,22 @@ import { OB11Group } from '../../types';
import { OB11Constructor } from '../../constructor';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface PayloadType {
group_id: number
}
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
},
required: ['group_id']
} as const satisfies JSONSchema;
class GetGroupInfo extends BaseAction<PayloadType, OB11Group> {
type Payload = FromSchema<typeof SchemaData>;
class GetGroupInfo extends BaseAction<Payload, OB11Group> {
actionName = ActionName.GetGroupInfo;
protected async _handle(payload: PayloadType) {
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
const group = await getGroup(payload.group_id.toString());
if (group) {
return OB11Constructor.group(group);

View File

@ -6,17 +6,23 @@ import { groups } from '@/core/data';
import { NTQQGroupApi } from '@/core/apis';
import { Group } from '@/core/entities';
import { log } from '@/common/utils/log';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
// no_cache get时传字符串
const SchemaData = {
type: 'object',
properties: {
no_cache: { type: 'boolean' },
}
} as const satisfies JSONSchema;
interface Payload {
no_cache: boolean | string;
}
type Payload = FromSchema<typeof SchemaData>;
class GetGroupList extends BaseAction<Payload, OB11Group[]> {
actionName = ActionName.GetGroupList;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
let groupList: Group[] = Array.from(groups.values());
if (groupList.length === 0 || payload?.no_cache === true || payload?.no_cache === 'true') {
if (groupList.length === 0 || payload?.no_cache === true /*|| payload.no_cache === 'true'*/) {
groupList = await NTQQGroupApi.getGroups(true);
// log('get groups', groups);
}

View File

@ -8,24 +8,31 @@ import { log, logDebug } from '@/common/utils/log';
import { isNull } from '../../../common/utils/helper';
import { WebApi } from '@/core/apis/webapi';
import { NTQQGroupApi } from '@/core';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
// no_cache get时传字符串
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
user_id: { type: 'number' },
no_cache: { type: 'boolean' },
},
required: ['group_id', 'user_id']
} as const satisfies JSONSchema;
export interface PayloadType {
group_id: number;
user_id: number;
no_cache?: boolean | string;
}
type Payload = FromSchema<typeof SchemaData>;
class GetGroupMemberInfo extends BaseAction<PayloadType, OB11GroupMember> {
class GetGroupMemberInfo extends BaseAction<Payload, OB11GroupMember> {
actionName = ActionName.GetGroupMemberInfo;
protected async _handle(payload: PayloadType) {
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
const group = await getGroup(payload.group_id.toString());
if (!group) {
throw (`群(${payload.group_id})不存在`);
}
const webGroupMembers = await WebApi.getGroupMembers(payload.group_id.toString());
if (payload.no_cache == true || payload.no_cache === 'true') {
if (payload.no_cache == true /*|| payload.no_cache === 'true'*/) {
groupMembers.set(group.groupCode, await NTQQGroupApi.getGroupMembers(payload.group_id.toString()));
}
const member = await getGroupMember(payload.group_id.toString(), payload.user_id.toString());

View File

@ -6,24 +6,29 @@ import { ActionName } from '../types';
import { napCatCore, NTQQGroupApi } from '@/core';
import { WebApi } from '@/core/apis/webapi';
import { logDebug } from '@/common/utils/log';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
no_cache: { type: 'boolean' },
},
required: ['group_id', 'user_id']
} as const satisfies JSONSchema;
export interface PayloadType {
group_id: number,
no_cache?: boolean | string
}
type Payload = FromSchema<typeof SchemaData>;
class GetGroupMemberList extends BaseAction<PayloadType, OB11GroupMember[]> {
class GetGroupMemberList extends BaseAction<Payload, OB11GroupMember[]> {
actionName = ActionName.GetGroupMemberList;
protected async _handle(payload: PayloadType) {
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
const MemberMap: Map<number, OB11GroupMember> = new Map<number, OB11GroupMember>();
const webGroupMembers = await WebApi.getGroupMembers(payload.group_id.toString());
const group = await getGroup(payload.group_id.toString());
if (!group) {
throw (`${payload.group_id}不存在`);
}
if (payload.no_cache == true || payload.no_cache === 'true') {
if (payload.no_cache == true /*|| payload.no_cache === 'true'*/) {
// webGroupMembers = await WebApi.getGroupMembers(payload.group_id.toString());
const _groupMembers = await NTQQGroupApi.getGroupMembers(payload.group_id.toString());
groupMembers.set(group.groupCode, _groupMembers);

View File

@ -1,10 +1,7 @@
import { WebApi, WebApiGroupNoticeFeed, WebApiGroupNoticeRet } from '@/core/apis/webapi';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
interface PayloadType {
group_id: number
}
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface GroupNotice {
sender_id: number
publish_time: number
@ -17,11 +14,22 @@ interface GroupNotice {
}>
}
}
type ApiGroupNotice = GroupNotice & WebApiGroupNoticeFeed;
export class GetGroupNotice extends BaseAction<PayloadType, GroupNotice[]> {
actionName = ActionName.GoCQHTTP_GetGroupNotice;
protected async _handle(payload: PayloadType) {
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
},
required: ['group_id']
} as const satisfies JSONSchema;
type Payload = FromSchema<typeof SchemaData>;
type ApiGroupNotice = GroupNotice & WebApiGroupNoticeFeed;
export class GetGroupNotice extends BaseAction<Payload, GroupNotice[]> {
actionName = ActionName.GoCQHTTP_GetGroupNotice;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
const group = payload.group_id.toString();
const ret = await WebApi.getGrouptNotice(group);
if (!ret) {

View File

@ -2,7 +2,7 @@ import SendMsg from '../msg/SendMsg';
import { ActionName, BaseCheckResult } from '../types';
import { OB11PostSendMsg } from '../../types';
// 未检测参数
class SendGroupMsg extends SendMsg {
actionName = ActionName.SendGroupMsg;

View File

@ -3,18 +3,23 @@ import { GroupRequestOperateTypes } from '@/core/entities';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';
import { groupNotifies } from '@/core/data';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface Payload {
flag: string,
// sub_type: "add" | "invite",
// type: "add" | "invite"
approve: boolean,
reason: string
}
const SchemaData = {
type: 'object',
properties: {
flag: { type: 'string' },
approve: { type: 'boolean' },
reason: { type: 'string' }
},
required: ['flag', 'approve', 'reson']
} as const satisfies JSONSchema;
type Payload = FromSchema<typeof SchemaData>;
export default class SetGroupAddRequest extends BaseAction<Payload, null> {
actionName = ActionName.SetGroupAddRequest;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> {
const flag = payload.flag.toString();
const approve = payload.approve.toString() === 'true';

View File

@ -3,18 +3,26 @@ import { getGroupMember } from '@/core/data';
import { GroupMemberRole } from '@/core/entities';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface Payload {
group_id: number,
user_id: number,
enable: boolean
}
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
user_id: { type: 'number' },
enable: { type: 'boolean' }
},
required: ['group_id', 'user_id', 'enable']
} as const satisfies JSONSchema;
type Payload = FromSchema<typeof SchemaData>;
export default class SetGroupAdmin extends BaseAction<Payload, null> {
actionName = ActionName.SetGroupAdmin;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> {
const member = await getGroupMember(payload.group_id, payload.user_id);
// 已经前置验证类型
const enable = payload.enable.toString() === 'true';
if (!member) {
throw `群成员${payload.user_id}不存在`;

View File

@ -2,16 +2,23 @@ import BaseAction from '../BaseAction';
import { getGroupMember } from '@/core/data';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface Payload {
group_id: number,
user_id: number,
duration: number
}
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
user_id: { type: 'number' },
duration: { type: 'number' }
},
required: ['group_id', 'user_id', 'duration']
} as const satisfies JSONSchema;
type Payload = FromSchema<typeof SchemaData>;
export default class SetGroupBan extends BaseAction<Payload, null> {
actionName = ActionName.SetGroupBan;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> {
const member = await getGroupMember(payload.group_id, payload.user_id);
if (!member) {

View File

@ -2,16 +2,23 @@ import BaseAction from '../BaseAction';
import { getGroupMember } from '@/core/data';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface Payload {
group_id: number,
user_id: number,
card: string
}
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
user_id: { type: 'number' },
card: { type: 'string' }
},
required: ['group_id', 'user_id', 'card']
} as const satisfies JSONSchema;
type Payload = FromSchema<typeof SchemaData>;
export default class SetGroupCard extends BaseAction<Payload, null> {
actionName = ActionName.SetGroupCard;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> {
const member = await getGroupMember(payload.group_id, payload.user_id);
if (!member) {