feat(shell): satori: implement message.create

This commit is contained in:
Il Harper 2024-03-05 23:53:00 +08:00
parent e59f2668f5
commit c983cb77ba
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 75 additions and 2 deletions

View File

@ -13,7 +13,7 @@
// import { guildMemberList } from './guild/member/list'
// import { guildRemove } from './guild/remove'
// import { loginGet } from './login/get'
// import { messageCreate } from './message/create'
import { messageCreate } from './message/create'
// import { messageDelete } from './message/delete'
// import { messageGet } from './message/get'
// import { messageList } from './message/list'
@ -46,7 +46,7 @@ const routesIntl = {
'guild.role.update': notImplemented,
'guild.role.delete': notImplemented,
// 'login.get': loginGet,
// 'message.create': messageCreate,
'message.create': messageCreate,
// 'message.get': messageGet,
// 'message.delete': messageDelete,
'message.update': notImplemented,

View File

@ -0,0 +1,73 @@
import type { MessageCreatePayload } from '../../types'
import type { RouteContext } from '../types'
export const messageCreate = async (rctx: RouteContext) => {
let useForm = false
const contentType = rctx.req.headers['content-type']
if (contentType) {
if (contentType.startsWith('application/x-www-form-urlencoded'))
useForm = true
if (contentType.startsWith('multipart/form-data')) useForm = true
}
if (useForm) return messageCreateUsingForm(rctx)
else return messageCreateUsingJson(rctx)
}
async function messageCreateUsingForm({ cctx, path, req, res }: RouteContext) {
cctx.chronocat.l.error(
`${path} 失败,来自 ${req.socket.remoteAddress}。Form 请求尚未实现。`,
{
code: 501,
},
)
res.writeHead(501)
res.end('501 not implemented')
return
}
async function messageCreateUsingJson({
cctx,
path,
req,
res,
json,
}: RouteContext) {
const payload = (await json()) as MessageCreatePayload
const validateResult = await cctx.chronocat.validate('MessageCreatePayload')(
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
}
try {
return await cctx.chronocat.api['message.create'](payload)
} catch (e) {
cctx.chronocat.l.error(
new Error(`${path} 失败,来自 ${req.socket.remoteAddress}`, {
cause: e,
}),
{
code: 500,
},
)
res.writeHead(500)
res.end(`500 internal server error\n${e as string}`)
return
}
}