feat(api): implement channel.list

This commit is contained in:
Il Harper 2024-03-08 12:55:54 +08:00
parent 6efc21489d
commit 32e8060fac
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
7 changed files with 62 additions and 3 deletions

View File

@ -0,0 +1,19 @@
import type {
ChannelListPayload,
ChannelListResponse,
ChronocatContext,
} from '@chronocat/shell'
export const buildChannelList =
(_ctx: ChronocatContext) =>
async ({ guild_id }: ChannelListPayload): Promise<ChannelListResponse> => {
return {
data: [
{
id: guild_id,
avatar: `https://p.qlogo.cn/gh/${guild_id}/${guild_id}/640`,
type: 0, // ChannelType.TEXT
},
],
}
}

View File

@ -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))

View File

@ -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)
}

View File

@ -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,

View File

@ -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
}

View File

@ -3,6 +3,8 @@ import type { Op } from './satoriEntity'
export type {
ApprovePayload,
ChannelListPayload,
ChannelListResponse,
ChannelMemberMutePayload,
ChannelMutePayload,
FriendListResponse,

View File

@ -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<string, never>]
'unsafe.channel.member.mute': [
[ChannelMemberMutePayload],