From 18c5be2098d5e3a4530a5ae4b17647e5cfbe7401 Mon Sep 17 00:00:00 2001 From: Il Harper Date: Sun, 22 Sep 2024 21:50:49 +0800 Subject: [PATCH] feat(api): implement `guild.member.approve` --- .../src/api/guild/approve.ts | 5 +-- .../src/api/guild/member/approve.ts | 35 +++++++++++++++++++ .../src/definitions/groupService.ts | 8 ++--- packages/engine-chronocat-api/src/index.ts | 2 ++ .../engine-chronocat-event/src/handler.ts | 6 ++-- .../engine-chronocat-event/src/messages.ts | 3 +- .../src/satori/routes/guild/member/approve.ts | 29 +++++++++++++++ packages/shell/src/satori/routes/index.ts | 3 +- packages/shell/src/types.ts | 1 + packages/shell/static/openapi.yaml | 19 ++++++++++ 10 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 packages/engine-chronocat-api/src/api/guild/member/approve.ts create mode 100644 packages/shell/src/satori/routes/guild/member/approve.ts diff --git a/packages/engine-chronocat-api/src/api/guild/approve.ts b/packages/engine-chronocat-api/src/api/guild/approve.ts index 8f4f31f..3068825 100644 --- a/packages/engine-chronocat-api/src/api/guild/approve.ts +++ b/packages/engine-chronocat-api/src/api/guild/approve.ts @@ -12,9 +12,10 @@ export const buildGuildApprove = }, ) + // TODO: 看下是否是真的不支持 if (!approve) { - ctx.chronocat.l.error('暂不支持拒绝群申请。', { code: 2145 }) - throw new Error('暂不支持拒绝群申请。') + ctx.chronocat.l.error('暂不支持拒绝群邀请。', { code: 2145 }) + throw new Error('暂不支持拒绝群邀请。') } // 这个是同意发过来的小卡片 diff --git a/packages/engine-chronocat-api/src/api/guild/member/approve.ts b/packages/engine-chronocat-api/src/api/guild/member/approve.ts new file mode 100644 index 0000000..0981a73 --- /dev/null +++ b/packages/engine-chronocat-api/src/api/guild/member/approve.ts @@ -0,0 +1,35 @@ +import type { ApprovePayload, ChronocatContext } from '@chronocat/shell' +import { operateSysNotify } from '../../../definitions/groupService' + +export const buildGuildMemberApprove = + (ctx: ChronocatContext) => + async ({ message_id, approve, comment }: ApprovePayload) => { + if (comment) + ctx.chronocat.l.warn( + '不支持处理群申请时附加备注消息。备注消息将会被忽略。', + { + code: 2144, + }, + ) + + const [seq, groupCode, doubt] = message_id.split(':') + + if (!seq || !groupCode || !doubt) + ctx.chronocat.l.warn('message_id 不合法。将仍然尝试处理加群请求。') + + // 这个是同意发过来的小卡片 + await operateSysNotify({ + doubt: doubt! === '1', + operateMsg: { + operateType: approve ? 1 : 2, + targetMsg: { + seq: seq!, + type: 7, + groupCode: groupCode!, + postscript: '', + }, + }, + }) + + return {} + } diff --git a/packages/engine-chronocat-api/src/definitions/groupService.ts b/packages/engine-chronocat-api/src/definitions/groupService.ts index 0d8b3b2..5859c4f 100644 --- a/packages/engine-chronocat-api/src/definitions/groupService.ts +++ b/packages/engine-chronocat-api/src/definitions/groupService.ts @@ -154,12 +154,12 @@ export const operateSysNotify = define< unknown, [ { - doubt: false + doubt: boolean operateMsg: { - operateType: 1 + operateType: 1 | 2 targetMsg: { - seq: '' - type: 1 + seq: string + type: 1 | 7 groupCode: string postscript: '' } diff --git a/packages/engine-chronocat-api/src/index.ts b/packages/engine-chronocat-api/src/index.ts index 13383e8..0f891ce 100644 --- a/packages/engine-chronocat-api/src/index.ts +++ b/packages/engine-chronocat-api/src/index.ts @@ -11,6 +11,7 @@ import { buildFriendRemove } from './api/friend/remove' import { buildGuildApprove } from './api/guild/approve' import { buildGuildGet } from './api/guild/get' import { buildGuildList } from './api/guild/list' +import { buildGuildMemberApprove } from './api/guild/member/approve' import { buildGuildMemberGet } from './api/guild/member/get' import { buildGuildMemberKick } from './api/guild/member/kick' import { buildGuildMemberList } from './api/guild/member/list' @@ -61,6 +62,7 @@ export const apply = async (ctx: ChronocatContext) => { register('guild.member.list', buildGuildMemberList(ctx)) register('guild.member.kick', buildGuildMemberKick(ctx)) register('guild.member.mute', buildGuildMemberMute(ctx)) + register('guild.member.approve', buildGuildMemberApprove(ctx)) register('chronocat.internal.message.create2.normal', buildMessageCreate(ctx)) register( 'chronocat.internal.message.create2.forward', diff --git a/packages/engine-chronocat-event/src/handler.ts b/packages/engine-chronocat-event/src/handler.ts index 2c93403..235e31c 100644 --- a/packages/engine-chronocat-event/src/handler.ts +++ b/packages/engine-chronocat-event/src/handler.ts @@ -218,7 +218,7 @@ const dispatcher = async ( } case 'nodeIKernelGroupListener/onGroupSingleScreenNotifies': { - const { notifies } = payload as OnGroupSingleScreenNotifies + const { doubt, notifies } = payload as OnGroupSingleScreenNotifies // eslint-disable-next-line @typescript-eslint/no-misused-promises notifies.forEach(async (x) => { @@ -258,7 +258,9 @@ const dispatcher = async ( // x.actionUser 此时一定为空 - ctx.chronocat.emit(new GuildMemberRequestDispatchMessage(x, uin)) + ctx.chronocat.emit( + new GuildMemberRequestDispatchMessage(x, uin, doubt), + ) return } diff --git a/packages/engine-chronocat-event/src/messages.ts b/packages/engine-chronocat-event/src/messages.ts index 0095980..e67b30b 100644 --- a/packages/engine-chronocat-event/src/messages.ts +++ b/packages/engine-chronocat-event/src/messages.ts @@ -97,6 +97,7 @@ export class GuildMemberRequestDispatchMessage constructor( private notify: GroupNotifyGuildMemberRequest, private uin: string, + private doubt: boolean, ) {} type = 'satori' as const @@ -131,7 +132,7 @@ export class GuildMemberRequestDispatchMessage member: {}, message: { - id: '', + id: `${this.notify.seq}:${this.notify.group.groupCode}:${this.doubt ? '1' : '2'}`, content: this.notify.postscript, }, } diff --git a/packages/shell/src/satori/routes/guild/member/approve.ts b/packages/shell/src/satori/routes/guild/member/approve.ts new file mode 100644 index 0000000..c649869 --- /dev/null +++ b/packages/shell/src/satori/routes/guild/member/approve.ts @@ -0,0 +1,29 @@ +import type { ApprovePayload } from '../../../types' +import type { RouteContext } from '../../types' + +export const guildMemberApprove = async ({ + cctx, + path, + req, + res, + json, +}: RouteContext) => { + const payload = (await json()) as ApprovePayload + + const validateResult = + await cctx.chronocat.validate('ApprovePayload')(payload) + + if (validateResult) { + const err = `解析 ${path} 请求时出现问题,来自 ${req.socket.remoteAddress}。${validateResult}` + + cctx.chronocat.l.error(err, { + code: 400, + }) + + res.writeHead(400) + res.end(`400 bad request\n${err}`) + return + } + + return await cctx.chronocat.api['guild.member.approve'](payload) +} diff --git a/packages/shell/src/satori/routes/index.ts b/packages/shell/src/satori/routes/index.ts index b241b27..a1805b4 100644 --- a/packages/shell/src/satori/routes/index.ts +++ b/packages/shell/src/satori/routes/index.ts @@ -8,6 +8,7 @@ import { friendRemove } from './friend/remove' import { guildApprove } from './guild/approve' import { guildGet } from './guild/get' import { guildList } from './guild/list' +import { guildMemberApprove } from './guild/member/approve' import { guildMemberGet } from './guild/member/get' import { guildMemberKick } from './guild/member/kick' import { guildMemberList } from './guild/member/list' @@ -41,7 +42,7 @@ const routesIntl = { 'guild.member.get': guildMemberGet, 'guild.member.list': guildMemberList, 'guild.member.kick': guildMemberKick, - 'guild.member.approve': notImplemented, + 'guild.member.approve': guildMemberApprove, 'guild.member.mute': guildMemberMute, 'guild.member.role.set': notImplemented, 'guild.member.role.unset': notImplemented, diff --git a/packages/shell/src/types.ts b/packages/shell/src/types.ts index afbe86f..0c450c2 100644 --- a/packages/shell/src/types.ts +++ b/packages/shell/src/types.ts @@ -129,6 +129,7 @@ export interface SatoriMethods { 'guild.member.list': [[GuildMemberListPayload], GuildMemberListResponse] 'guild.member.kick': [[GuildMemberKickPayload], Record] 'guild.member.mute': [[GuildMemberMutePayload], Record] + 'guild.member.approve': [[ApprovePayload], Record] 'chronocat.guild.member.title.set': [ [GuildMemberTitleSetPayload], diff --git a/packages/shell/static/openapi.yaml b/packages/shell/static/openapi.yaml index 64e62d7..ef4226d 100644 --- a/packages/shell/static/openapi.yaml +++ b/packages/shell/static/openapi.yaml @@ -287,6 +287,25 @@ paths: '500': $ref: '#/components/responses/500' + /guild.member.approve: + post: + operationId: guildMemberApprove + tags: + - GuildMember + summary: 通过群组成员申请 + description: 处理加群请求。 + requestBody: + description: 处理加群请求 + content: + application/json: + schema: + $ref: '#/components/schemas/ApprovePayload' + responses: + '200': + description: 成功处理加群请求 + '500': + $ref: '#/components/responses/500' + /login.get: post: operationId: loginGet