From bf14ad477f4b18e68bc82d485ae2db0b4ae6798e Mon Sep 17 00:00:00 2001 From: Il Harper Date: Fri, 8 Mar 2024 16:53:50 +0800 Subject: [PATCH] feat(api): implement message.delete --- .../src/api/message/delete.ts | 23 ++++++++++++++ packages/engine-chronocat-api/src/index.ts | 2 ++ packages/shell/src/satori/routes/index.ts | 4 +-- .../shell/src/satori/routes/message/delete.ts | 30 +++++++++++++++++++ packages/shell/src/types.ts | 2 ++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 packages/engine-chronocat-api/src/api/message/delete.ts create mode 100644 packages/shell/src/satori/routes/message/delete.ts diff --git a/packages/engine-chronocat-api/src/api/message/delete.ts b/packages/engine-chronocat-api/src/api/message/delete.ts new file mode 100644 index 0000000..7690afd --- /dev/null +++ b/packages/engine-chronocat-api/src/api/message/delete.ts @@ -0,0 +1,23 @@ +import { ChatType } from '@chronocat/red' +import type { ChronocatContext, MessageDeletePayload } from '@chronocat/shell' +import { recallMsg } from '../../definitions/msgService' + +export const buildMessageDelete = + (ctx: ChronocatContext) => + async ({ channel_id, message_id }: MessageDeletePayload) => { + await recallMsg({ + peer: channel_id.startsWith('private:') + ? { + chatType: ChatType.Private, + peerUid: ctx.chronocat.uix.getUid(channel_id.slice(8))!, // private: + } + : { + chatType: ChatType.Group, + peerUid: channel_id, + guildId: '', + }, + msgIds: [message_id], + }) + + return {} + } diff --git a/packages/engine-chronocat-api/src/index.ts b/packages/engine-chronocat-api/src/index.ts index 287e378..a253801 100644 --- a/packages/engine-chronocat-api/src/index.ts +++ b/packages/engine-chronocat-api/src/index.ts @@ -16,6 +16,7 @@ import { buildAssetsGet } from './api/internal/assets/get' import { qfaceGet, qfaceList } from './api/internal/qface' import { buildLoginGet } from './api/login/get' import { buildMessageCreate } from './api/message/create' +import { buildMessageDelete } from './api/message/delete' import { buildMessageGet } from './api/message/get' import { buildUserChannelCreate } from './api/user/channel/create' import { buildHandler } from './handler' @@ -47,6 +48,7 @@ export const apply = async (ctx: ChronocatContext) => { register('guild.member.kick', buildGuildMemberKick(ctx)) register('message.create', buildMessageCreate(ctx)) register('message.get', buildMessageGet(ctx)) + register('message.delete', buildMessageDelete(ctx)) register('login.get', buildLoginGet(ctx)) register('chronocat.internal.message.create.forward', buildMessageCreate(ctx)) diff --git a/packages/shell/src/satori/routes/index.ts b/packages/shell/src/satori/routes/index.ts index c161719..b1c910d 100644 --- a/packages/shell/src/satori/routes/index.ts +++ b/packages/shell/src/satori/routes/index.ts @@ -14,7 +14,7 @@ import { guildMemberList } from './guild/member/list' import { guildRemove } from './guild/remove' // import { loginGet } from './login/get' import { messageCreate } from './message/create' -// import { messageDelete } from './message/delete' +import { messageDelete } from './message/delete' import { messageGet } from './message/get' // import { messageList } from './message/list' import { notImplemented } from './notimpl' @@ -48,7 +48,7 @@ const routesIntl = { // 'login.get': loginGet, 'message.create': messageCreate, 'message.get': messageGet, - // 'message.delete': messageDelete, + 'message.delete': messageDelete, 'message.update': notImplemented, // 'message.list': messageList, 'reaction.create': notImplemented, diff --git a/packages/shell/src/satori/routes/message/delete.ts b/packages/shell/src/satori/routes/message/delete.ts new file mode 100644 index 0000000..895ac31 --- /dev/null +++ b/packages/shell/src/satori/routes/message/delete.ts @@ -0,0 +1,30 @@ +import type { MessageDeletePayload } from '../../types' +import type { RouteContext } from '../types' + +export const messageDelete = async ({ + cctx, + path, + req, + res, + json, +}: RouteContext) => { + const payload = (await json()) as MessageDeletePayload + + const validateResult = await cctx.chronocat.validate('MessageDeletePayload')( + 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['message.delete'](payload) +} diff --git a/packages/shell/src/types.ts b/packages/shell/src/types.ts index 5df8eee..9fb3dd0 100644 --- a/packages/shell/src/types.ts +++ b/packages/shell/src/types.ts @@ -25,6 +25,7 @@ import type { Login, Message, MessageCreatePayload, + MessageDeletePayload, MessageGetPayload, UserPayload, } from './satori/types' @@ -116,6 +117,7 @@ export interface Methods { Message[], ] 'message.get': [[MessageGetPayload], Message] + 'message.delete': [[MessageDeletePayload], Record] 'login.get': [[], Login]