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:
1 parent
9a678442e9
commit
3e5dca1bd4
15 files changed
+598
-332
No files matched your search
@@ -0,0 +1,60 @@
|
||||
// ApiHub — the per-instance bag of typed Api classes hanging off
|
||||
// `bridge.apis`. Mirrors NapCat's `NapCatCore.apis = { MsgApi, …}`
|
||||
// pattern but uses camelCase keys (`apis.message.sendGroup`) to match
|
||||
// the rest of the SnowLuma TypeScript style.
|
||||
//
|
||||
// Each Api class is constructed once when the parent `Bridge` is
|
||||
// constructed (eager — every account that connects gets its own
|
||||
// `apis.*` set immediately). Api classes receive a `BridgeContext`
|
||||
// reference, NOT the concrete `Bridge`, so they remain easy to mock
|
||||
// without standing up a full Bridge instance.
|
||||
//
|
||||
// Adding a new Api:
|
||||
// 1. Create `bridge/apis/<name>.ts` exporting a `class XxxApi`.
|
||||
// 2. Add a property here.
|
||||
// 3. Construct it inside `buildApiHub(ctx)` below.
|
||||
// 4. Move the relevant methods OFF Bridge and onto the new Api.
|
||||
//
|
||||
// This file evolves commit-by-commit as `#6 NapCat-style Api on ctx`
|
||||
// lands the 13 areas. Until all 13 are in place, ApiHub will have a
|
||||
// mix of "done" entries and "TODO" markers below — the TODO list
|
||||
// shrinks as each commit lands.
|
||||
|
||||
import type { BridgeContext } from '../bridge-context';
|
||||
import { MessageApi } from './message';
|
||||
|
||||
export interface ApiHub {
|
||||
/** Send/recall/markRead operations across c2c + group + c2c-file. */
|
||||
readonly message: MessageApi;
|
||||
// Pending — added as later commits land:
|
||||
// readonly contacts: ContactsApi;
|
||||
// readonly groupAdmin: GroupAdminApi;
|
||||
// readonly groupFile: GroupFileApi;
|
||||
// readonly groupAlbum: GroupAlbumApi;
|
||||
// readonly friend: FriendApi;
|
||||
// readonly interaction: InteractionApi;
|
||||
// readonly profile: ProfileApi;
|
||||
// readonly forward: ForwardApi;
|
||||
// readonly misc: MiscApi;
|
||||
// readonly extras: ExtrasApi;
|
||||
// readonly web: WebApi;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the ApiHub for a Bridge. Called once from the Bridge
|
||||
* constructor. Eager construction means every Bridge instance pays
|
||||
* the cost up-front (a few object allocations) — there's no lazy
|
||||
* path because that would require either a thunk-based wrapper or
|
||||
* runtime-mutated `apis.xxx` slots, neither of which is worth the
|
||||
* complexity for ~13 small classes.
|
||||
*/
|
||||
export function buildApiHub(ctx: BridgeContext): ApiHub {
|
||||
return {
|
||||
message: new MessageApi(ctx),
|
||||
};
|
||||
}
|
||||
|
||||
// Re-export the Api classes themselves so callers can write
|
||||
// `import type { MessageApi } from '@snowluma/core/.../apis'` for
|
||||
// signature use. Concrete instances always come from `bridge.apis.*`.
|
||||
export { MessageApi } from './message';
|
||||
Reference in new issue
Block a user