refactor(api): introduce MessageApi + BridgeContext + ApiHub skeleton (#6 commit 1/13)

First of 13 commits decomposing the Bridge god-object into typed Api
classes following NapCat's `ctx.apis.XxxApi.method()` pattern. This
commit lays the foundation (BridgeContext, ApiHub, eager construction)
plus moves the message-send/recall/mark-read methods onto MessageApi.
Subsequent commits will move the other 12 areas (group admin, files,
profile, …) similarly.

Architecture
------------
* `bridge-context.ts` — new interface, the surface Api classes see.
  Carries identity / events / apis / sendRawPacket / resolveUserUid /
  next{Message,Client}Sequence helpers / upload meta cache. Replaces
  the previous `BridgeInterface` for Api-class consumption (the
  legacy interface still exists, just slimmed; full removal happens
  in commit 13).

* `apis/index.ts` — `ApiHub` interface + `buildApiHub(ctx)` factory.
  Each commit grows `ApiHub` by one entry; today only `message` is
  populated.

* `apis/message.ts` — `MessageApi` with seven methods inlined from
  the previous Bridge implementation:
    sendGroup        ← Bridge.sendGroupMessage
    sendPrivate      ← Bridge.sendPrivateMessage
    sendC2cFile      ← Bridge.sendC2cFileMessage
    recallGroup      ← actions/group-message.ts::recallGroupMessage
    recallPrivate    ← actions/group-message.ts::recallPrivateMessage
    markGroupRead    ← actions/group-message.ts::markGroupMessageRead
    markPrivateRead  ← actions/group-message.ts::markPrivateMessageRead
  (Names trimmed: the `apis.message` prefix already signals the
  context, so `apis.message.sendGroupMessage` would be redundant.)

Bridge changes
--------------
* Removed the 7 methods listed above (moved to MessageApi).
* Removed unused imports + `SEND_MSG_CMD` constant + `buildSendElems`
  + `protobuf_encode/decode` (now used only inside MessageApi).
* Added `readonly apis: ApiHub` field, built eagerly in constructor.
* Promoted `nextMessageRandom` / `nextClientSequence` from private →
  public (Api classes need them to build SendMessageRequest packets).
* `BridgeInterface` now also lists `apis: ApiHub` + the two
  generators; the 7 deleted method signatures are gone.

Caller-site updates (~9 files)
------------------------------
* `bridge/actions/group-file.ts` — `bridge.sendC2cFileMessage(...)`
  → `bridge.apis.message.sendC2cFile(...)`.
* `onebot/instance-context.ts` — markGroupMsgAsRead / markPrivateMsgAsRead
  bindings now route through `apis.message.markGroup/PrivateRead`.
* `onebot/modules/message-actions.ts` — every send/recall site
  rerouted; `typeof ref.bridge.sendXxx` type refs also updated.
* `onebot/actions/extended.ts` — same.

Tests (~5 files)
----------------
* `tests/actions/_helpers.ts` — `mockBridge()` grows an `apis` field
  built by `mockApiHub()`; the previous flat `sendGroupMessage` etc.
  vi.fn stubs are replaced with `mockMessageApi()`.
* All test files using direct `bridge.sendXxx` / `bridge.recallXxx` /
  `bridge.markXxxAsRead` calls or stubs updated to `bridge.apis.message.*`.
* No behaviour changes — 587/587 pass (same baseline; pre-existing
  network-manager native-addon failure unchanged).
This commit is contained in:
DrSmoothl committed 2026-05-20 22:17:47 +08:00
1 parent 9a678442e9
commit 3e5dca1bd4
15 files changed
+598 -332

No files matched your search

+16 -21
View File
@@ -1,14 +1,14 @@
import type { SendPacketResult } from '../protocol/packet-sender';
import type { ApiHub } from './apis';
import type { GroupFilesResult } from './actions/group-file';
import type { MediaIndexNode } from './actions/shared';
import type {
ClientKeyInfo,
DownloadRKeyInfo,
SendMessageReceipt,
UploadedFileMeta,
} from './bridge';
import type { BridgeEventBus } from './event-bus';
import type { ForwardNodePayload, MessageElement } from './events';
import type { ForwardNodePayload } from './events';
import type { IdentityService } from './identity-service';
import type {
FriendInfo,
@@ -24,6 +24,13 @@ export interface BridgeInterface {
readonly identity: IdentityService;
readonly events: BridgeEventBus;
readonly activePid: number | null;
/**
* Typed Api hub — `apis.message.sendGroup(...)`, `apis.message.recallGroup(...)`,
* etc. The shape of `ApiHub` grows commit-by-commit as the #6
* refactor moves the remaining areas (group-admin, group-file,
* profile, …) off Bridge and onto their dedicated Api classes.
*/
readonly apis: ApiHub;
// ─── Resolution ───
resolveUserUid(uin: number, groupId?: number): Promise<string>;
@@ -34,21 +41,13 @@ export interface BridgeInterface {
// routes through here.
sendRawPacket(serviceCmd: string, body: Uint8Array, timeoutMs?: number): Promise<SendPacketResult>;
// ─── Send-side protocol helpers (used by Api classes) ───
nextMessageRandom(): number;
nextClientSequence(): number;
// ─── Send (messages) ───
sendGroupMessage(groupId: number, elements: MessageElement[]): Promise<SendMessageReceipt>;
sendPrivateMessage(userUin: number, elements: MessageElement[]): Promise<SendMessageReceipt>;
/**
* Send a c2c file as a chat message. Bypasses the regular elems[]
* pipeline because c2c files live on `RichText.notOnlineFile`, not
* inside the elems array. Group files go through `sendGroupMessage`
* with a `{type:'file', fileId, fileName, fileSize, md5Hex, sha1Hex}`
* element instead.
*/
sendC2cFileMessage(
userUin: number,
userUid: string,
info: { fileId: string; fileName: string; fileSize: number; fileMd5: Uint8Array; fileHash?: string },
): Promise<SendMessageReceipt>;
// Moved to `apis.message.{sendGroup, sendPrivate, sendC2cFile,
// recallGroup, recallPrivate, markGroupRead, markPrivateRead}`.
// ─── Fetch (contacts / profile / system) ───
fetchFriendList(): Promise<FriendInfo[]>;
@@ -119,11 +118,7 @@ export interface BridgeInterface {
uploadForwardNodes(nodes: ForwardNodePayload[], groupId?: number, userId?: number): Promise<string>;
fetchForwardNodes(resId: string): Promise<ForwardNodePayload[]>;
// ─── Message ops ───
recallGroupMessage(groupId: number, sequence: number): Promise<void>;
recallPrivateMessage(userUin: number, clientSeq: number, msgSeq: number, random: number, timestamp: number): Promise<void>;
markGroupMsgAsRead(groupId: number, sequence: number): Promise<void>;
markPrivateMsgAsRead(userId: number, sequence: number): Promise<void>;
// ─── Message ops (moved to apis.message) ───
// ─── Interaction ───
sendPoke(isGroup: boolean, peerUin: number, targetUin?: number): Promise<void>;