From 6efc21489de1e2ab19ac8f63d31a93d351ec3a57 Mon Sep 17 00:00:00 2001 From: Il Harper Date: Fri, 8 Mar 2024 12:40:20 +0800 Subject: [PATCH] feat(api): implement unsafe.channel.mute --- .../src/api/channel/mute.ts | 13 +++++++++ packages/engine-chronocat-api/src/index.ts | 2 ++ .../shell/src/satori/routes/channel/mute.ts | 29 +++++++++++++++++++ packages/shell/src/satori/routes/index.ts | 4 +-- packages/shell/src/types.ts | 2 ++ 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 packages/engine-chronocat-api/src/api/channel/mute.ts create mode 100644 packages/shell/src/satori/routes/channel/mute.ts diff --git a/packages/engine-chronocat-api/src/api/channel/mute.ts b/packages/engine-chronocat-api/src/api/channel/mute.ts new file mode 100644 index 0000000..0fcb935 --- /dev/null +++ b/packages/engine-chronocat-api/src/api/channel/mute.ts @@ -0,0 +1,13 @@ +import type { ChannelMutePayload, ChronocatContext } from '@chronocat/shell' +import { setGroupShutUp } from '../../definitions/groupService' + +export const buildChannelMute = + (_ctx: ChronocatContext) => + async ({ channel_id, enable }: ChannelMutePayload) => { + await setGroupShutUp({ + groupCode: channel_id, + shutUp: enable, + }) + + return {} + } diff --git a/packages/engine-chronocat-api/src/index.ts b/packages/engine-chronocat-api/src/index.ts index 9dc5412..8afda54 100644 --- a/packages/engine-chronocat-api/src/index.ts +++ b/packages/engine-chronocat-api/src/index.ts @@ -2,6 +2,7 @@ import type { RedIpcArgs } from '@chronocat/red' import type { ChronocatContext } from '@chronocat/shell' import { ipcMan } from 'ipcman' import { buildChannelMemberMute } from './api/channel/member/mute' +import { buildChannelMute } from './api/channel/mute' import { buildAssetsGet } from './api/internal/assets/get' import { qfaceGet, qfaceList } from './api/internal/qface' import { buildLoginGet } from './api/login/get' @@ -21,6 +22,7 @@ export const apply = async (ctx: ChronocatContext) => { const register = ctx.chronocat.api.register(name) register('chronocat.internal.assets.get', buildAssetsGet(ctx)) + register('unsafe.channel.mute', buildChannelMute(ctx)) register('unsafe.channel.member.mute', buildChannelMemberMute(ctx)) register('message.create', buildMessageCreate(ctx)) register('login.get', buildLoginGet(ctx)) diff --git a/packages/shell/src/satori/routes/channel/mute.ts b/packages/shell/src/satori/routes/channel/mute.ts new file mode 100644 index 0000000..f25b061 --- /dev/null +++ b/packages/shell/src/satori/routes/channel/mute.ts @@ -0,0 +1,29 @@ +import type { ChannelMutePayload } from '../../types' +import type { RouteContext } from '../types' + +export const channelMute = async ({ + cctx, + path, + req, + res, + json, +}: RouteContext) => { + const payload = (await json()) as ChannelMutePayload + + const validateResult = + await cctx.chronocat.validate('ChannelMutePayload')(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['unsafe.channel.mute'](payload) +} diff --git a/packages/shell/src/satori/routes/index.ts b/packages/shell/src/satori/routes/index.ts index 7d9fdda..8737396 100644 --- a/packages/shell/src/satori/routes/index.ts +++ b/packages/shell/src/satori/routes/index.ts @@ -1,7 +1,7 @@ // import { channelGet } from './channel/get' // import { channelList } from './channel/list' import { channelMemberMute } from './channel/member/mute' -// import { channelMute } from './channel/mute' +import { channelMute } from './channel/mute' // import { friendApprove } from './friend/approve' // import { friendList } from './friend/list' // import { friendRemove } from './friend/remove' @@ -28,7 +28,7 @@ const routesIntl = { 'channel.create': notImplemented, 'channel.update': notImplemented, 'channel.delete': notImplemented, - // 'unsafe.channel.mute': channelMute, + 'unsafe.channel.mute': channelMute, 'unsafe.channel.member.mute': channelMemberMute, // 'user.channel.create': userChannelCreate, // 'guild.get': guildGet, diff --git a/packages/shell/src/types.ts b/packages/shell/src/types.ts index 010e326..bf07884 100644 --- a/packages/shell/src/types.ts +++ b/packages/shell/src/types.ts @@ -4,6 +4,7 @@ import type styles from 'ansi-styles' import type { O } from 'ts-toolbelt' import type { ChannelMemberMutePayload, + ChannelMutePayload, Event, Login, Message, @@ -75,6 +76,7 @@ export interface SelfProfileDispatchMessage { export interface Methods { // Satori + 'unsafe.channel.mute': [[ChannelMutePayload], Record] 'unsafe.channel.member.mute': [ [ChannelMemberMutePayload], Record,