refactor(core): split EventConverter god switch into themed pure functions
event-converter.ts (541 lines) was a class with a 16-case switch
mapping bridge events to OneBot wire JSON, plus a 13-case helper
mapping bridge elements to OneBot segments, plus four lazily-injected
resolver setters (image-url / media-url / message-id / media-segment
sink). Testing one case required instantiating the class and wiring
four setters; 22 of the 29 cases (10 notices + 2 requests + 13 element
types) had zero tests.
This commit kills the class and replaces it with a pure-function
family that takes a ConverterContext (a plain object holding selfId
+ the four resolver callbacks). Each per-event handler becomes a
small typed function that's trivial to call from a test with two
lines of setup.
New layout: packages/core/src/onebot/event-converter/
index.ts 133 ConverterContext + four resolver types +
convertEvent dispatcher + re-exports.
to-message.ts 112 friend / group / temp message (3 handlers)
to-notice.ts 189 group_member_join / leave / mute / admin /
friend_recall / group_recall / friend_poke /
group_poke / group_essence / group_file_upload /
friend_add (11 handlers)
to-request.ts 37 friend_request / group_invite (2 handlers)
to-segment.ts 145 13 element -> segment cases + elementsToJson +
elementsToOneBotSegments
utils.ts 111 parseSelfId / isSameActor / applyMessageIdResolver /
resolveReplyId / segmentsToRawMessage + CQ encoding
Caller migration (3 files, all inside onebot/):
instance.ts: replaces `new EventConverter()` + four setter
calls with a single `converterCtx: ConverterContext`
object built once at construction. The four
closures that used to be passed through setters
are now properties of the context.
instance-context.ts: `eventConverter: EventConverter` field renamed
to `converterCtx: ConverterContext`.
event-pipeline.ts: `ctx.eventConverter.convert(ctx.uin, event)` is
now `convertEvent(ctx.converterCtx, event)`.
External callers (modules/message-actions.ts) keep working unchanged:
elementsToOneBotSegments is re-exported from the new index.ts with
the same signature.
Adds 28 new tests covering every previously-untested case:
message kinds (already partly covered): 7 tests
- friend / group / temp_message peer + self post_type tagging
- messageIdResolver override
notice kinds (new): 11 tests
- all 11 cases, including the sub-type axes
(approve/invite, leave/kick/kick_me, ban/lift_ban,
set/unset, add/delete, etc.)
request kinds (new): 2 tests
- friend_request, group_invite (with sub_type default)
element segments (new): 13 tests
- text / face / image (with + without resolver) / at /
at-all / reply (with + without resolver) / record /
video / json / xml (with + without subType) / file /
mface / poke
mediaSegmentSink wiring (new): 1 test
Plus 3 tests in media-store.test.ts and 1 in event-pipeline.test.ts
adapted to the new API.
317 tests pass (was 285); 3 stability runs, same pre-existing
macOS-arm64 websocket addon failure, unrelated.
This commit is contained in:
13 files changed
+1289
-707
No files matched your search
@@ -0,0 +1,189 @@
|
||||
// Notice-kind handlers (10 cases). These produce OneBot `notice` events
|
||||
// with no message body / no element conversion — pure field projections
|
||||
// from the bridge event into the OneBot wire shape. None of them call
|
||||
// resolvers except the two recall handlers and group_essence, which
|
||||
// need a message-id resolution.
|
||||
|
||||
import type { QQEventVariant } from '../../bridge/events';
|
||||
import type { JsonObject } from '../types';
|
||||
import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT } from '../message-id';
|
||||
import type { ConverterContext } from './index';
|
||||
import { applyMessageIdResolver, isSameActor } from './utils';
|
||||
|
||||
type GroupMemberJoin = Extract<QQEventVariant, { kind: 'group_member_join' }>;
|
||||
type GroupMemberLeave = Extract<QQEventVariant, { kind: 'group_member_leave' }>;
|
||||
type GroupMute = Extract<QQEventVariant, { kind: 'group_mute' }>;
|
||||
type GroupAdmin = Extract<QQEventVariant, { kind: 'group_admin' }>;
|
||||
type FriendRecall = Extract<QQEventVariant, { kind: 'friend_recall' }>;
|
||||
type GroupRecall = Extract<QQEventVariant, { kind: 'group_recall' }>;
|
||||
type FriendPoke = Extract<QQEventVariant, { kind: 'friend_poke' }>;
|
||||
type GroupPoke = Extract<QQEventVariant, { kind: 'group_poke' }>;
|
||||
type GroupEssence = Extract<QQEventVariant, { kind: 'group_essence' }>;
|
||||
type GroupFileUpload = Extract<QQEventVariant, { kind: 'group_file_upload' }>;
|
||||
type FriendAdd = Extract<QQEventVariant, { kind: 'friend_add' }>;
|
||||
|
||||
export function convertGroupMemberJoin(ctx: ConverterContext, event: GroupMemberJoin): JsonObject {
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'group_increase',
|
||||
sub_type: isSameActor(event.operatorUin, event.operatorUid, event.userUin, event.userUid) ? 'approve' : 'invite',
|
||||
group_id: event.groupId,
|
||||
operator_id: event.operatorUin,
|
||||
user_id: event.userUin,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertGroupMemberLeave(ctx: ConverterContext, event: GroupMemberLeave): JsonObject {
|
||||
let subType: string;
|
||||
if (event.isKick) {
|
||||
subType = event.userUin === ctx.selfId ? 'kick_me' : 'kick';
|
||||
} else {
|
||||
subType = 'leave';
|
||||
}
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'group_decrease',
|
||||
sub_type: subType,
|
||||
group_id: event.groupId,
|
||||
operator_id: event.operatorUin,
|
||||
user_id: event.userUin,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertGroupMute(ctx: ConverterContext, event: GroupMute): JsonObject {
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'group_ban',
|
||||
sub_type: event.duration > 0 ? 'ban' : 'lift_ban',
|
||||
group_id: event.groupId,
|
||||
operator_id: event.operatorUin,
|
||||
user_id: event.userUin,
|
||||
duration: event.duration,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertGroupAdmin(ctx: ConverterContext, event: GroupAdmin): JsonObject {
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'group_admin',
|
||||
sub_type: event.set ? 'set' : 'unset',
|
||||
group_id: event.groupId,
|
||||
user_id: event.userUin,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertFriendRecall(ctx: ConverterContext, event: FriendRecall): JsonObject {
|
||||
const messageId = applyMessageIdResolver(
|
||||
ctx.messageIdResolver, false, event.userUin, event.msgSeq, PRIVATE_MESSAGE_EVENT,
|
||||
);
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'friend_recall',
|
||||
user_id: event.userUin,
|
||||
message_id: messageId,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertGroupRecall(ctx: ConverterContext, event: GroupRecall): JsonObject {
|
||||
const messageId = applyMessageIdResolver(
|
||||
ctx.messageIdResolver, true, event.groupId, event.msgSeq, GROUP_MESSAGE_EVENT,
|
||||
);
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'group_recall',
|
||||
group_id: event.groupId,
|
||||
operator_id: event.operatorUin,
|
||||
user_id: event.authorUin,
|
||||
message_id: messageId,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertFriendPoke(ctx: ConverterContext, event: FriendPoke): JsonObject {
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'notify',
|
||||
sub_type: 'poke',
|
||||
user_id: event.userUin,
|
||||
target_id: event.targetUin,
|
||||
action: event.action,
|
||||
suffix: event.suffix,
|
||||
action_img_url: event.actionImgUrl,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertGroupPoke(ctx: ConverterContext, event: GroupPoke): JsonObject {
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'notify',
|
||||
sub_type: 'poke',
|
||||
group_id: event.groupId,
|
||||
user_id: event.userUin,
|
||||
target_id: event.targetUin,
|
||||
action: event.action,
|
||||
suffix: event.suffix,
|
||||
action_img_url: event.actionImgUrl,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertGroupEssence(ctx: ConverterContext, event: GroupEssence): JsonObject {
|
||||
const messageId = applyMessageIdResolver(
|
||||
ctx.messageIdResolver, true, event.groupId, event.msgSeq, GROUP_MESSAGE_EVENT,
|
||||
);
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'essence',
|
||||
sub_type: event.set ? 'add' : 'delete',
|
||||
group_id: event.groupId,
|
||||
user_id: event.senderUin,
|
||||
sender_id: event.senderUin,
|
||||
operator_id: event.operatorUin,
|
||||
message_id: messageId,
|
||||
message_seq: event.msgSeq,
|
||||
random: event.random,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertGroupFileUpload(ctx: ConverterContext, event: GroupFileUpload): JsonObject {
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'group_upload',
|
||||
group_id: event.groupId,
|
||||
user_id: event.userUin,
|
||||
file: {
|
||||
id: event.fileId,
|
||||
name: event.fileName,
|
||||
size: event.fileSize,
|
||||
busid: event.busId,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function convertFriendAdd(ctx: ConverterContext, event: FriendAdd): JsonObject {
|
||||
return {
|
||||
time: event.time,
|
||||
self_id: ctx.selfId,
|
||||
post_type: 'notice',
|
||||
notice_type: 'friend_add',
|
||||
user_id: event.userUin,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user