Skip to content

Commit

Permalink
fix(api): report sent message
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Apr 5, 2024
1 parent 373ebf1 commit 312b01a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 34 deletions.
39 changes: 22 additions & 17 deletions packages/engine-chronocat-api/src/api/message/create/messager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Peer, Element as RedElement } from '@chronocat/red'
import type { Peer, Element as RedElement, RedMessage } from '@chronocat/red'
import { AtType, ChatType, FaceType } from '@chronocat/red'
import type {
ChronocatContext,
ChronocatSatoriServerConfig,
Message,
Event,
} from '@chronocat/shell'
import type h from '@satorijs/element'
import type { O } from 'ts-toolbelt'
Expand Down Expand Up @@ -36,7 +36,7 @@ export class Messager {
peer: Partial<Peer>

public errors: Error[] = []
public results: Message[] = []
public results: Event[] = []

stack: State[] = [new State('message')]
children: O.Partial<RedElement, 'deep'>[] = []
Expand All @@ -54,24 +54,27 @@ export class Messager {
await this.flush()

if (this.errors.length) throw this.errors
else return this.results.filter(Boolean) // .map(({ id }) => ({ id }))
else return this.results.filter(Boolean)
}

flush = async () => {
if (!this.children.length) return

this.normalize()
const result = await this.common.send(this.ctx, this.peer, this.children)

await this.pushResult(
await this.common.send(this.ctx, this.peer, this.children),
)

this.children = []
}

pushResult = async (result: RedMessage) => {
const parsedEvents = await this.ctx.chronocat.api[
'chronocat.internal.red.message.parse'
](result, this.config)

if (parsedEvents)
for (const parsedEvent of parsedEvents)
if (parsedEvent.message) this.results.push(parsedEvent.message)

this.children = []
if (parsedEvents) this.results.push(...parsedEvents)
}

private normalize = () => {
Expand Down Expand Up @@ -294,13 +297,15 @@ export class Messager {
await this.render(children)
} else if (children.every((x) => 'id' in x)) {
// 普通合并转发消息
await this.common.sendForward(
this.ctx,
this.peer,
children.map((x) => ({
msgId: x.attrs['id'] as string,
senderShowName: 'QQ用户',
})),
await this.pushResult(
await this.common.sendForward(
this.ctx,
this.peer,
children.map((x) => ({
msgId: x.attrs['id'] as string,
senderShowName: 'QQ用户',
})),
),
)
} else {
//伪造合并转发消息。本引擎不支持,使用兜底行为
Expand Down
8 changes: 4 additions & 4 deletions packages/engine-chronocat-api/src/common/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const defaultSrcContact: Peer = {
export let sendForwardMsgBuffer = Buffer.alloc(0)
export let sendForwardCover = defaultSendForwardCover

let task = Promise.resolve<unknown>(undefined)
let task = Promise.resolve<RedMessage>(undefined as unknown as RedMessage)

export const commonSendForward = async (
ctx: ChronocatContext,
Expand Down Expand Up @@ -78,10 +78,10 @@ export const commonSendForward = async (
: (peer as Peer)

task = task
.then(() => ctx.chronocat.sleep(80))
.then(() => ctx.chronocat.sleep(80) as unknown as RedMessage)
.then(
() =>
new Promise((resolve, reject) => {
new Promise<RedMessage>((resolve, reject) => {
sendForwardMsgBuffer = Buffer.alloc(0)
sendForwardCover = defaultSendForwardCover

Expand All @@ -103,7 +103,7 @@ export const commonSendForward = async (
}, ctx.chronocat.timeout)
}),
)
.catch((e) => console.log(e))
.catch((e) => console.log(e) as unknown as RedMessage)

return task
}
7 changes: 5 additions & 2 deletions packages/engine-chronocat-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ 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('message.create', buildMessageCreate(ctx))
register('chronocat.internal.message.create.forward', buildMessageCreate(ctx))
register('chronocat.internal.message.create2.normal', buildMessageCreate(ctx))
register(
'chronocat.internal.message.create2.forward',
buildMessageCreate(ctx),
)
register('message.get', buildMessageGet(ctx))
register('message.delete', buildMessageDelete(ctx))
register('message.list', buildMessageList(ctx))
Expand Down
49 changes: 38 additions & 11 deletions packages/shell/src/satori/routes/message/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import type { MessageCreatePayload as MessageCreatePayloadRich } from '../../types'
import type { O } from 'ts-toolbelt'
import type {
ChronocatLogCurrentConfig,
ChronocatSatoriEventsConfig,
} from '../../../services/config/configEntity'
import type { ChronocatContext, SatoriDispatchMessage } from '../../../types'
import type {
Event,
MessageCreatePayload as MessageCreatePayloadRich,
} from '../../types'
import type { MessageCreatePayload as MessageCreatePayloadEntity } from '../../types/satoriPayloadEntity'
import type { RouteContext } from '../types'

Expand Down Expand Up @@ -61,11 +70,12 @@ async function messageCreateUsingJson({
}

let method:
| 'message.create'
| 'chronocat.internal.message.create.forward'
| 'chronocat.internal.message.create.forward.fake'
| 'chronocat.internal.message.create.poke'
| 'chronocat.internal.message.create.markdown' = 'message.create'
| 'chronocat.internal.message.create2.normal'
| 'chronocat.internal.message.create2.forward'
| 'chronocat.internal.message.create2.forward.fake'
| 'chronocat.internal.message.create2.poke'
| 'chronocat.internal.message.create2.markdown' =
'chronocat.internal.message.create2.normal'

const forwards = cctx.chronocat.h
.select(payloadRich.content, 'message')
Expand Down Expand Up @@ -103,8 +113,8 @@ async function messageCreateUsingJson({
return

if (forward!.children.every((x) => x.attrs['id']))
method = 'chronocat.internal.message.create.forward'
else method = 'chronocat.internal.message.create.forward.fake'
method = 'chronocat.internal.message.create2.forward'
else method = 'chronocat.internal.message.create2.forward.fake'
}

const pokes = cctx.chronocat.h.select(
Expand All @@ -113,7 +123,7 @@ async function messageCreateUsingJson({
)
if (pokes.length) {
// TODO: 如果单条消息内除了 poke 还有其他元素,打印警告
method = 'chronocat.internal.message.create.poke'
method = 'chronocat.internal.message.create2.poke'
}

const markdowns = cctx.chronocat.h.select(
Expand All @@ -122,8 +132,25 @@ async function messageCreateUsingJson({
)
if (markdowns.length) {
// TODO: 如果单条消息内除了 markdown 还有其他元素,打印警告
method = 'chronocat.internal.message.create.markdown'
method = 'chronocat.internal.message.create2.markdown'
}

return await cctx.chronocat.api[method](payloadRich, config)
const result = await cctx.chronocat.api[method](payloadRich, config)

cctx.chronocat.emit(new MessageCreatedDispatchMessage(result))

return result.map((x) => x.message).filter(Boolean)
}

export class MessageCreatedDispatchMessage implements SatoriDispatchMessage {
constructor(private events: Event[]) {}
type = 'satori' as const

toSatori = async (
_ctx: ChronocatContext,
_config: O.Intersect<
ChronocatLogCurrentConfig,
ChronocatSatoriEventsConfig
>,
) => this.events
}
21 changes: 21 additions & 0 deletions packages/shell/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ export interface CCInternalMethods {
Message[],
]

'chronocat.internal.message.create2.normal': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.message.create2.forward': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.message.create2.forward.fake': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.message.create2.poke': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.message.create2.markdown': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]

'chronocat.internal.red.message.parse': [
[RedMessage, ChronocatSatoriServerConfig],
Event[] | undefined,
Expand Down

0 comments on commit 312b01a

Please sign in to comment.