From 32e8060facb4d0acd0e9f2871b1c100006987d8b Mon Sep 17 00:00:00 2001 From: Il Harper Date: Fri, 8 Mar 2024 12:55:54 +0800 Subject: [PATCH] feat(api): implement channel.list --- .../src/api/channel/list.ts | 19 ++++++++++++ packages/engine-chronocat-api/src/index.ts | 2 ++ .../shell/src/satori/routes/channel/list.ts | 29 +++++++++++++++++++ packages/shell/src/satori/routes/index.ts | 4 +-- .../src/satori/types/satoriPayloadEntity.ts | 6 +++- .../src/satori/types/satoriPayloadRich.ts | 2 ++ packages/shell/src/types.ts | 3 ++ 7 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 packages/engine-chronocat-api/src/api/channel/list.ts create mode 100644 packages/shell/src/satori/routes/channel/list.ts diff --git a/packages/engine-chronocat-api/src/api/channel/list.ts b/packages/engine-chronocat-api/src/api/channel/list.ts new file mode 100644 index 0000000..eddc553 --- /dev/null +++ b/packages/engine-chronocat-api/src/api/channel/list.ts @@ -0,0 +1,19 @@ +import type { + ChannelListPayload, + ChannelListResponse, + ChronocatContext, +} from '@chronocat/shell' + +export const buildChannelList = + (_ctx: ChronocatContext) => + async ({ guild_id }: ChannelListPayload): Promise => { + return { + data: [ + { + id: guild_id, + avatar: `https://p.qlogo.cn/gh/${guild_id}/${guild_id}/640`, + type: 0, // ChannelType.TEXT + }, + ], + } + } diff --git a/packages/engine-chronocat-api/src/index.ts b/packages/engine-chronocat-api/src/index.ts index 8afda54..78f742c 100644 --- a/packages/engine-chronocat-api/src/index.ts +++ b/packages/engine-chronocat-api/src/index.ts @@ -1,6 +1,7 @@ import type { RedIpcArgs } from '@chronocat/red' import type { ChronocatContext } from '@chronocat/shell' import { ipcMan } from 'ipcman' +import { buildChannelList } from './api/channel/list' import { buildChannelMemberMute } from './api/channel/member/mute' import { buildChannelMute } from './api/channel/mute' import { buildAssetsGet } from './api/internal/assets/get' @@ -22,6 +23,7 @@ export const apply = async (ctx: ChronocatContext) => { const register = ctx.chronocat.api.register(name) register('chronocat.internal.assets.get', buildAssetsGet(ctx)) + register('channel.list', buildChannelList(ctx)) register('unsafe.channel.mute', buildChannelMute(ctx)) register('unsafe.channel.member.mute', buildChannelMemberMute(ctx)) register('message.create', buildMessageCreate(ctx)) diff --git a/packages/shell/src/satori/routes/channel/list.ts b/packages/shell/src/satori/routes/channel/list.ts new file mode 100644 index 0000000..4db12f5 --- /dev/null +++ b/packages/shell/src/satori/routes/channel/list.ts @@ -0,0 +1,29 @@ +import type { ChannelListPayload } from '../../types' +import type { RouteContext } from '../types' + +export const channelList = async ({ + cctx, + path, + req, + res, + json, +}: RouteContext) => { + const payload = (await json()) as ChannelListPayload + + const validateResult = + await cctx.chronocat.validate('ChannelListPayload')(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['channel.list'](payload) +} diff --git a/packages/shell/src/satori/routes/index.ts b/packages/shell/src/satori/routes/index.ts index 8737396..d49ed26 100644 --- a/packages/shell/src/satori/routes/index.ts +++ b/packages/shell/src/satori/routes/index.ts @@ -1,5 +1,5 @@ // import { channelGet } from './channel/get' -// import { channelList } from './channel/list' +import { channelList } from './channel/list' import { channelMemberMute } from './channel/member/mute' import { channelMute } from './channel/mute' // import { friendApprove } from './friend/approve' @@ -24,7 +24,7 @@ import type { Route } from './types' const routesIntl = { // 'channel.get': channelGet, - // 'channel.list': channelList, + 'channel.list': channelList, 'channel.create': notImplemented, 'channel.update': notImplemented, 'channel.delete': notImplemented, diff --git a/packages/shell/src/satori/types/satoriPayloadEntity.ts b/packages/shell/src/satori/types/satoriPayloadEntity.ts index 4d8dcc3..82d50c1 100644 --- a/packages/shell/src/satori/types/satoriPayloadEntity.ts +++ b/packages/shell/src/satori/types/satoriPayloadEntity.ts @@ -1,4 +1,4 @@ -import type { Guild, GuildMember, User } from './satoriEntity' +import type { Channel, Guild, GuildMember, User } from './satoriEntity' export interface Next { /** @@ -98,6 +98,10 @@ export interface ChannelListPayload extends Next { guild_id: string } +export interface ChannelListResponse extends Next { + data: Channel[] +} + export interface ChannelGetPayload { channel_id: string } diff --git a/packages/shell/src/satori/types/satoriPayloadRich.ts b/packages/shell/src/satori/types/satoriPayloadRich.ts index 90cd216..534628b 100644 --- a/packages/shell/src/satori/types/satoriPayloadRich.ts +++ b/packages/shell/src/satori/types/satoriPayloadRich.ts @@ -3,6 +3,8 @@ import type { Op } from './satoriEntity' export type { ApprovePayload, + ChannelListPayload, + ChannelListResponse, ChannelMemberMutePayload, ChannelMutePayload, FriendListResponse, diff --git a/packages/shell/src/types.ts b/packages/shell/src/types.ts index bf07884..b5ca40f 100644 --- a/packages/shell/src/types.ts +++ b/packages/shell/src/types.ts @@ -3,6 +3,8 @@ import type h from '@satorijs/element' import type styles from 'ansi-styles' import type { O } from 'ts-toolbelt' import type { + ChannelListPayload, + ChannelListResponse, ChannelMemberMutePayload, ChannelMutePayload, Event, @@ -76,6 +78,7 @@ export interface SelfProfileDispatchMessage { export interface Methods { // Satori + 'channel.list': [[ChannelListPayload], ChannelListResponse] 'unsafe.channel.mute': [[ChannelMutePayload], Record] 'unsafe.channel.member.mute': [ [ChannelMemberMutePayload],