Skip to content

Commit

Permalink
feat(api): implement guild.member.approve
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Sep 22, 2024
1 parent 36cc8f8 commit 18c5be2
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/engine-chronocat-api/src/api/guild/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export const buildGuildApprove =
},
)

// TODO: 看下是否是真的不支持
if (!approve) {
ctx.chronocat.l.error('暂不支持拒绝群申请。', { code: 2145 })
throw new Error('暂不支持拒绝群申请。')
ctx.chronocat.l.error('暂不支持拒绝群邀请。', { code: 2145 })
throw new Error('暂不支持拒绝群邀请。')
}

// 这个是同意发过来的小卡片
Expand Down
35 changes: 35 additions & 0 deletions packages/engine-chronocat-api/src/api/guild/member/approve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { ApprovePayload, ChronocatContext } from '@chronocat/shell'
import { operateSysNotify } from '../../../definitions/groupService'

export const buildGuildMemberApprove =
(ctx: ChronocatContext) =>
async ({ message_id, approve, comment }: ApprovePayload) => {
if (comment)
ctx.chronocat.l.warn(
'不支持处理群申请时附加备注消息。备注消息将会被忽略。',
{
code: 2144,
},
)

const [seq, groupCode, doubt] = message_id.split(':')

if (!seq || !groupCode || !doubt)
ctx.chronocat.l.warn('message_id 不合法。将仍然尝试处理加群请求。')

// 这个是同意发过来的小卡片
await operateSysNotify({
doubt: doubt! === '1',
operateMsg: {
operateType: approve ? 1 : 2,
targetMsg: {
seq: seq!,
type: 7,
groupCode: groupCode!,
postscript: '',
},
},
})

return {}
}
8 changes: 4 additions & 4 deletions packages/engine-chronocat-api/src/definitions/groupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ export const operateSysNotify = define<
unknown,
[
{
doubt: false
doubt: boolean
operateMsg: {
operateType: 1
operateType: 1 | 2
targetMsg: {
seq: ''
type: 1
seq: string
type: 1 | 7
groupCode: string
postscript: ''
}
Expand Down
2 changes: 2 additions & 0 deletions packages/engine-chronocat-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { buildFriendRemove } from './api/friend/remove'
import { buildGuildApprove } from './api/guild/approve'
import { buildGuildGet } from './api/guild/get'
import { buildGuildList } from './api/guild/list'
import { buildGuildMemberApprove } from './api/guild/member/approve'
import { buildGuildMemberGet } from './api/guild/member/get'
import { buildGuildMemberKick } from './api/guild/member/kick'
import { buildGuildMemberList } from './api/guild/member/list'
Expand Down Expand Up @@ -61,6 +62,7 @@ export const apply = async (ctx: ChronocatContext) => {
register('guild.member.list', buildGuildMemberList(ctx))
register('guild.member.kick', buildGuildMemberKick(ctx))
register('guild.member.mute', buildGuildMemberMute(ctx))
register('guild.member.approve', buildGuildMemberApprove(ctx))
register('chronocat.internal.message.create2.normal', buildMessageCreate(ctx))
register(
'chronocat.internal.message.create2.forward',
Expand Down
6 changes: 4 additions & 2 deletions packages/engine-chronocat-event/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ const dispatcher = async (
}

case 'nodeIKernelGroupListener/onGroupSingleScreenNotifies': {
const { notifies } = payload as OnGroupSingleScreenNotifies
const { doubt, notifies } = payload as OnGroupSingleScreenNotifies

// eslint-disable-next-line @typescript-eslint/no-misused-promises
notifies.forEach(async (x) => {
Expand Down Expand Up @@ -258,7 +258,9 @@ const dispatcher = async (

// x.actionUser 此时一定为空

ctx.chronocat.emit(new GuildMemberRequestDispatchMessage(x, uin))
ctx.chronocat.emit(
new GuildMemberRequestDispatchMessage(x, uin, doubt),
)

return
}
Expand Down
3 changes: 2 additions & 1 deletion packages/engine-chronocat-event/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class GuildMemberRequestDispatchMessage
constructor(
private notify: GroupNotifyGuildMemberRequest,
private uin: string,
private doubt: boolean,
) {}

type = 'satori' as const
Expand Down Expand Up @@ -131,7 +132,7 @@ export class GuildMemberRequestDispatchMessage
member: {},

message: {
id: '',
id: `${this.notify.seq}:${this.notify.group.groupCode}:${this.doubt ? '1' : '2'}`,
content: this.notify.postscript,
},
}
Expand Down
29 changes: 29 additions & 0 deletions packages/shell/src/satori/routes/guild/member/approve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ApprovePayload } from '../../../types'
import type { RouteContext } from '../../types'

export const guildMemberApprove = async ({
cctx,
path,
req,
res,
json,
}: RouteContext) => {
const payload = (await json()) as ApprovePayload

const validateResult =
await cctx.chronocat.validate('ApprovePayload')(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['guild.member.approve'](payload)
}
3 changes: 2 additions & 1 deletion packages/shell/src/satori/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { friendRemove } from './friend/remove'
import { guildApprove } from './guild/approve'
import { guildGet } from './guild/get'
import { guildList } from './guild/list'
import { guildMemberApprove } from './guild/member/approve'
import { guildMemberGet } from './guild/member/get'
import { guildMemberKick } from './guild/member/kick'
import { guildMemberList } from './guild/member/list'
Expand Down Expand Up @@ -41,7 +42,7 @@ const routesIntl = {
'guild.member.get': guildMemberGet,
'guild.member.list': guildMemberList,
'guild.member.kick': guildMemberKick,
'guild.member.approve': notImplemented,
'guild.member.approve': guildMemberApprove,
'guild.member.mute': guildMemberMute,
'guild.member.role.set': notImplemented,
'guild.member.role.unset': notImplemented,
Expand Down
1 change: 1 addition & 0 deletions packages/shell/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export interface SatoriMethods {
'guild.member.list': [[GuildMemberListPayload], GuildMemberListResponse]
'guild.member.kick': [[GuildMemberKickPayload], Record<string, never>]
'guild.member.mute': [[GuildMemberMutePayload], Record<string, never>]
'guild.member.approve': [[ApprovePayload], Record<string, never>]

'chronocat.guild.member.title.set': [
[GuildMemberTitleSetPayload],
Expand Down
19 changes: 19 additions & 0 deletions packages/shell/static/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,25 @@ paths:
'500':
$ref: '#/components/responses/500'

/guild.member.approve:
post:
operationId: guildMemberApprove
tags:
- GuildMember
summary: 通过群组成员申请
description: 处理加群请求。
requestBody:
description: 处理加群请求
content:
application/json:
schema:
$ref: '#/components/schemas/ApprovePayload'
responses:
'200':
description: 成功处理加群请求
'500':
$ref: '#/components/responses/500'

/login.get:
post:
operationId: loginGet
Expand Down

0 comments on commit 18c5be2

Please sign in to comment.