feat(event): add dispatch messages

This commit is contained in:
Il Harper 2024-03-05 19:33:36 +08:00
parent bb3ce5eaf8
commit 1beaa76587
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC

View File

@ -0,0 +1,75 @@
import type {
ChronocatContext,
ChronocatLogCurrentConfig,
ChronocatSatoriEventsConfig,
Event,
RedMessage,
SatoriDispatchMessage,
} from '@chronocat/shell'
import { PLATFORM } from '@chronocat/shell/lib/utils/consts'
import type { O } from 'ts-toolbelt'
import type { BuddyReq } from './ipcEntity'
import { buildParser } from './parser'
export class MessageCreatedDispatchMessage implements SatoriDispatchMessage {
constructor(private messages: RedMessage[]) {}
type = 'satori' as const
toSatori = async (
ctx: ChronocatContext,
config: O.Intersect<ChronocatLogCurrentConfig, ChronocatSatoriEventsConfig>,
) =>
(await Promise.all(this.messages.map(buildParser(ctx, config))))
.filter(Boolean as unknown as (es: Event[] | undefined) => es is Event[])
.flat()
.filter(Boolean as unknown as (e: Event | undefined) => e is Event)
}
export class MessageDeletedDispatchMessage implements SatoriDispatchMessage {
constructor(private messages: RedMessage[]) {}
type = 'satori' as const
toSatori = async (
ctx: ChronocatContext,
config: O.Intersect<ChronocatLogCurrentConfig, ChronocatSatoriEventsConfig>,
) =>
(await Promise.all(this.messages.map(buildParser(ctx, config))))
.filter(Boolean as unknown as (es: Event[] | undefined) => es is Event[])
.flat()
.filter(Boolean as unknown as (e: Event | undefined) => e is Event)
.map((e) => ((e.type = 'message-deleted'), e))
}
export class FriendRequestDispatchMessage implements SatoriDispatchMessage {
constructor(
private buddyReq: BuddyReq,
private uin: string,
) {}
type = 'satori' as const
toSatori = async (
_ctx: ChronocatContext,
_config: O.Intersect<
ChronocatLogCurrentConfig,
ChronocatSatoriEventsConfig
>,
) => {
const event: Event = {
id: undefined as unknown as number,
type: 'friend-request',
platform: PLATFORM,
self_id: undefined as unknown as string,
timestamp: Number(this.buddyReq.reqTime) * 1000,
}
event.user = {
id: `${this.uin}`,
name: this.buddyReq.friendNick,
avatar: `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${this.uin}&spec=640`,
}
return [event]
}
}