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.
204 lines
11 KiB
TypeScript
204 lines
11 KiB
TypeScript
import type { WebHonorType } from '@/bridge/web/group-honor';
|
|
import type { Bridge } from '../bridge/bridge';
|
|
import type { QQInfo } from '../bridge/qq-info';
|
|
import type { ApiActionContext } from './api-handler';
|
|
import type { ConverterContext } from './event-converter';
|
|
import type { MediaStore } from './media-store';
|
|
import type { MessageStore } from './message-store';
|
|
import type { JsonObject, MessageMeta, OneBotConfig } from './types';
|
|
import {
|
|
getDownloadRKeys,
|
|
getFriendList,
|
|
getGroupFiles,
|
|
getGroupInfo,
|
|
getGroupList,
|
|
getGroupMemberInfo,
|
|
getGroupMemberList,
|
|
getGroupSystemMessages,
|
|
getLoginInfo,
|
|
getStrangerInfo,
|
|
} from './modules/contact-actions';
|
|
import {
|
|
deleteMessage,
|
|
forwardSingleMessage,
|
|
getForwardMessage,
|
|
getFriendMsgHistory,
|
|
getGroupMsgHistory,
|
|
sendGroupForwardMessage,
|
|
sendGroupMessage,
|
|
sendPrivateForwardMessage,
|
|
sendPrivateMessage,
|
|
setEssenceMessage,
|
|
uploadForwardMessage,
|
|
} from './modules/message-actions';
|
|
import {
|
|
getImageInfo as getCachedImageInfo,
|
|
getRecordInfo as getCachedRecordInfo,
|
|
} from './modules/media-actions';
|
|
import { handleGroupAddRequest } from './modules/request-actions';
|
|
|
|
/**
|
|
* Single shared context bag that flows through every OneBot instance-internal
|
|
* subsystem: API builder and the per-kind event pipeline.
|
|
*
|
|
* Only fields that are actually read through this bag live here. The API
|
|
* handler and network manager are owned by `OneBotInstance` directly because
|
|
* nothing reads them via ctx, and including them here would force a
|
|
* chicken-and-egg late-bound field dance during construction.
|
|
*
|
|
* `dispatchEvent` is the indirection used by the event pipeline to hand a
|
|
* converted OneBot event back to the instance for caching + adapter fan-out;
|
|
* it lets the pipeline stay decoupled from the network manager.
|
|
*/
|
|
export interface OneBotInstanceContext {
|
|
/** Self UIN as string (matches what's on disk and on the wire). */
|
|
uin: string;
|
|
/** Self UIN parsed once, used in event payloads. */
|
|
selfId: number;
|
|
|
|
qqInfo: QQInfo;
|
|
bridge: Bridge;
|
|
|
|
messageStore: MessageStore;
|
|
mediaStore: MediaStore;
|
|
|
|
converterCtx: ConverterContext;
|
|
|
|
config: OneBotConfig;
|
|
musicSignUrl?: string;
|
|
|
|
/** Persist meta about a message id; safe to call any number of times. */
|
|
cacheMessageMeta(messageId: number, meta: MessageMeta): void;
|
|
/** Hand a fully-converted OneBot event to the network manager + caches. */
|
|
dispatchEvent(event: JsonObject): void;
|
|
}
|
|
|
|
export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext {
|
|
const { bridge, qqInfo, messageStore, mediaStore } = ref;
|
|
|
|
return {
|
|
getLoginInfo: () => getLoginInfo(ref),
|
|
isOnline: () => true,
|
|
getMessage: (messageId) => messageStore.findEvent(messageId),
|
|
getMessageMeta: (messageId) => messageStore.findMeta(messageId),
|
|
canSendImage: () => true,
|
|
canSendRecord: () => true,
|
|
|
|
// OneBot11 message actions.
|
|
sendPrivateMessage: (userId, message, autoEscape) => sendPrivateMessage(ref, userId, message, autoEscape),
|
|
sendGroupMessage: (groupId, message, autoEscape) => sendGroupMessage(ref, groupId, message, autoEscape),
|
|
deleteMessage: (_messageId, meta) => deleteMessage(bridge, meta),
|
|
|
|
// OneBot11 info actions.
|
|
getFriendList: () => getFriendList(bridge, qqInfo),
|
|
getGroupList: (noCache) => getGroupList(bridge, qqInfo, noCache),
|
|
getGroupInfo: (groupId, noCache) => getGroupInfo(bridge, qqInfo, groupId, noCache),
|
|
getGroupMemberList: (groupId, noCache) => getGroupMemberList(bridge, qqInfo, groupId, noCache),
|
|
getGroupMemberInfo: (groupId, userId, noCache) => getGroupMemberInfo(bridge, qqInfo, groupId, userId, noCache),
|
|
getStrangerInfo: (userId) => getStrangerInfo(bridge, qqInfo, userId),
|
|
|
|
// Group admin.
|
|
setGroupKick: (groupId, userId, reject) => bridge.kickGroupMember(groupId, userId, reject),
|
|
setGroupKickMembers: (groupId, userIds, reject) => bridge.kickGroupMembers(groupId, userIds, reject),
|
|
setGroupBan: (groupId, userId, duration) => bridge.muteGroupMember(groupId, userId, duration),
|
|
setGroupWholeBan: (groupId, enable) => bridge.muteGroupAll(groupId, enable),
|
|
setGroupAddOption: (groupId, addType) => bridge.setGroupAddOption(groupId, addType),
|
|
setGroupSearch: (groupId) => bridge.setGroupSearch(groupId),
|
|
setGroupAdmin: (groupId, userId, enable) => bridge.setGroupAdmin(groupId, userId, enable),
|
|
setGroupCard: (groupId, userId, card) => bridge.setGroupCard(groupId, userId, card),
|
|
setGroupName: (groupId, name) => bridge.setGroupName(groupId, name),
|
|
setGroupLeave: (groupId) => bridge.leaveGroup(groupId),
|
|
setGroupSpecialTitle: (groupId, userId, title) => bridge.setGroupSpecialTitle(groupId, userId, title),
|
|
getGroupAtAllRemain: (groupId) => bridge.getGroupAtAllRemain(groupId),
|
|
|
|
// Group file.
|
|
uploadGroupFile: async (groupId, file, name, folderId, uploadFile) => {
|
|
const result = await bridge.uploadGroupFile(groupId, file, name ?? '', folderId ?? '/', uploadFile ?? true);
|
|
return result.fileId;
|
|
},
|
|
uploadPrivateFile: async (userId, file, name, uploadFile) => {
|
|
const result = await bridge.uploadPrivateFile(userId, file, name ?? '', uploadFile ?? true);
|
|
return result.fileId;
|
|
},
|
|
getGroupFileUrl: (groupId, fileId, busId) => bridge.fetchGroupFileUrl(groupId, fileId, busId ?? 102),
|
|
getGroupFiles: (groupId, folderId) => getGroupFiles(bridge, groupId, folderId),
|
|
deleteGroupFile: (groupId, fileId) => bridge.deleteGroupFile(groupId, fileId),
|
|
moveGroupFile: (groupId, fileId, parentDirectory, targetDirectory) => {
|
|
return bridge.moveGroupFile(groupId, fileId, parentDirectory, targetDirectory);
|
|
},
|
|
createGroupFileFolder: (groupId, name, parentId) => bridge.createGroupFileFolder(groupId, name, parentId ?? '/'),
|
|
deleteGroupFileFolder: (groupId, folderId) => bridge.deleteGroupFileFolder(groupId, folderId),
|
|
renameGroupFileFolder: (groupId, folderId, newName) => bridge.renameGroupFileFolder(groupId, folderId, newName),
|
|
getPrivateFileUrl: (userId, fileId, fileHash) => bridge.fetchPrivateFileUrl(userId, fileId, fileHash),
|
|
|
|
// Requests.
|
|
handleFriendRequest: (flag, approve) => bridge.setFriendAddRequest(flag, approve),
|
|
handleGroupRequest: (flag, _subType, approve, reason) => handleGroupAddRequest(bridge, flag, approve, reason),
|
|
|
|
// Extended.
|
|
sendLike: (userId, times) => bridge.sendLike(userId, times),
|
|
sendFriendPoke: (userId, targetId) => bridge.sendPoke(false, userId, targetId),
|
|
sendGroupPoke: (groupId, userId) => bridge.sendPoke(true, groupId, userId),
|
|
setEssenceMsg: (messageId) => setEssenceMessage(bridge, messageStore, messageId, true),
|
|
deleteEssenceMsg: (messageId) => setEssenceMessage(bridge, messageStore, messageId, false),
|
|
getProfileLike: (userId = undefined, start = 0, limit = 10) => bridge.getProfileLike(userId, start, limit),
|
|
fetchCustomFace: (count) => bridge.fetchCustomFace(count),
|
|
getEmojiLikes: (groupId, sequence, emojiId, emojiType, count, cookie) => {
|
|
return bridge.getEmojiLikes(groupId, sequence, emojiId, emojiType, count, cookie);
|
|
},
|
|
getUnidirectionalFriendList: () => bridge.getUnidirectionalFriendList(),
|
|
setSelfLongNick: (longNick) => bridge.setSelfLongNick(longNick),
|
|
setInputStatus: (userId, eventType) => bridge.setInputStatus(userId, eventType),
|
|
translateEn2Zh: (words) => bridge.translateEn2Zh(words),
|
|
getMiniAppArk: (type, title, desc, picUrl, jumpUrl) => bridge.getMiniAppArk(type, title, desc, picUrl, jumpUrl),
|
|
clickInlineKeyboardButton: (groupId, botAppid, buttonId, callbackData, msgSeq) => {
|
|
return bridge.clickInlineKeyboardButton(groupId, botAppid, buttonId, callbackData, msgSeq);
|
|
},
|
|
sendGroupSign: (groupId) => bridge.sendGroupSign(groupId),
|
|
|
|
// NapCat-compatible extended actions.
|
|
setGroupReaction: (groupId, sequence, code, isSet) => bridge.setGroupReaction(groupId, sequence, code, isSet),
|
|
handleDeleteFriend: (userId, block) => bridge.deleteFriend(userId, !!block),
|
|
getGroupMsgHistory: (groupId, messageId, count) => getGroupMsgHistory(messageStore, groupId, messageId, count),
|
|
getFriendMsgHistory: (userId, messageId, count) => getFriendMsgHistory(messageStore, userId, messageId, count),
|
|
handleGetGroupSystemMsg: () => getGroupSystemMessages(bridge),
|
|
getDownloadRKeys: () => getDownloadRKeys(bridge),
|
|
sendGroupForwardMsg: (groupId, messages) => sendGroupForwardMessage(ref, groupId, messages),
|
|
sendPrivateForwardMsg: (userId, messages) => sendPrivateForwardMessage(ref, userId, messages),
|
|
sendForwardMsg: (messages) => uploadForwardMessage(ref, messages),
|
|
getForwardMsg: (resId) => getForwardMessage(ref, resId),
|
|
forwardSingleMsg: (messageId, target) => forwardSingleMessage(ref, messageId, target),
|
|
forceFetchClientKey: () => bridge.forceFetchClientKey(),
|
|
setFriendRemark: (userId, remark) => bridge.setFriendRemark(userId, remark),
|
|
setGroupRemark: (groupId, remark) => bridge.setGroupRemark(groupId, remark),
|
|
getGroupFileCount: (groupId) => bridge.fetchGroupFileCount(groupId),
|
|
setMsgEmojiLike: async (messageId, emojiId, set) => {
|
|
const meta = messageStore.findMeta(messageId);
|
|
if (!meta || !meta.isGroup) throw new Error('message not found or not a group message');
|
|
await bridge.setGroupReaction(meta.targetId, meta.sequence, emojiId, set);
|
|
},
|
|
markGroupMsgAsRead: (groupId, sequence) => bridge.markGroupMsgAsRead(groupId, sequence),
|
|
markPrivateMsgAsRead: (userId, sequence) => bridge.markPrivateMsgAsRead(userId, sequence),
|
|
setOnlineStatus: (status, extStatus, batteryStatus) => bridge.setOnlineStatus(status, extStatus, batteryStatus),
|
|
setProfile: (nickname, personalNote) => bridge.setProfile(nickname, personalNote),
|
|
|
|
// Web-backed actions.
|
|
getGroupHonorInfo: (groupId: number, type: WebHonorType | string) => bridge.getGroupHonorInfo(groupId, type),
|
|
getGroupEssence: (groupId, pageStart = 0, pageLimit = 50) => bridge.getGroupEssence(groupId, pageStart, pageLimit),
|
|
getGroupEssenceAll: (groupId) => bridge.getGroupEssenceAll(groupId),
|
|
sendGroupNotice: (groupId, content, options) => bridge.sendGroupNotice(groupId, content, options),
|
|
getGroupNotice: (groupId) => bridge.getGroupNotice(groupId),
|
|
deleteGroupNotice: (groupId, fid) => bridge.deleteGroupNotice(groupId, fid),
|
|
getCookiesStr: (domain) => bridge.getCookiesStr(domain),
|
|
getCsrfToken: () => bridge.getCsrfToken(),
|
|
getCredentials: (domain) => bridge.getCredentials(domain),
|
|
|
|
// Media lookup.
|
|
getImageInfo: (file) => getCachedImageInfo(mediaStore, file),
|
|
getRecordInfo: (file) => getCachedRecordInfo(bridge, mediaStore, file),
|
|
|
|
// Avatar.
|
|
setAvatar: (source) => bridge.setAvatar(source),
|
|
};
|
|
}
|