refactor(api): move contacts methods to ContactsApi (#6 commit 2/13)

Second of 13 commits decomposing Bridge. Moves the six "fetch the
roster / a profile / outstanding requests / download rkeys" methods
off Bridge and onto a new `apis.contacts.*`.

Methods moved
-------------
* fetchFriendList       — OIDB 0xfd4_1 + identity.rememberFriends
* fetchGroupList        — OIDB 0xfe5_2 + identity.rememberGroups
* fetchGroupMemberList  — OIDB 0xfe7_3 + identity.rememberGroupMembers
                          (includes the 60s TTL + per-group inflight
                          coalesce cache that prevents Tencent
                          risk-control from triggering)
* fetchUserProfile      — OIDB 0xfe1_2 + identity.rememberUserProfile
* fetchGroupRequests    — OIDB 0x10c0_{1,2}
* fetchDownloadRKeys    — OIDB 0x9067_202

Cache fields moved
------------------
The two member-list throttle maps (`memberListInflight_`,
`memberListLastFetch_`) move from Bridge to `ContactsApi` as private
fields. Same TTL, same semantics — just owned by the Api class that
exposes the method.

Constructor ordering
--------------------
`buildApiHub(this)` now runs BEFORE `identity.setFetcher(...)` in the
Bridge constructor because the `fetchProfile` / `fetchGroupMemberList`
callbacks installed there reach into `this.apis.contacts.*`. With
eager construction this is safe; the alternative (pass a thunk that
forwards once apis is built) was strictly worse for readability.

bridge-contacts.ts removed
--------------------------
The file existed solely as the previous module-function delegate
target; ContactsApi inlines every function body so there's nothing
left to keep.

Caller-site updates
-------------------
* `bridge/actions/friend.ts` — `bridge.fetchFriendList()` →
  `bridge.apis.contacts.fetchFriendList()`.
* `onebot/manager.ts`, `onebot/instance-rkey.ts`,
  `onebot/modules/contact-actions.ts`, `modules/request-actions.ts`,
  `actions/extended.ts` — same shape rewrite (6 method names).

Test plumbing
-------------
* `tests/actions/_helpers.ts` — `mockApiHub()` grows a `contacts`
  field built by `mockContactsApi()`.
* `tests/bridge-member-identity.test.ts::RefreshingBridge` — was
  `override async fetchGroupMemberList()`. After the move the method
  lives on `this.apis.contacts`, not `this`. Switched to patching the
  method on the constructed ContactsApi instance directly.
* `tests/actions/friend.test.ts` — `bridge.fetchFriendList` →
  `bridge.apis.contacts.fetchFriendList`; the "refresh throws" branch
  overrides via `bridge.apis.contacts.fetchFriendList = vi.fn(…)`.
* `tests/onebot/contact-actions.test.ts`, `extended-actions.test.ts`,
  `request-actions.test.ts` — local `fakeBridge` helpers gained an
  auto-promotion shim that rewrites flat method stubs (the pre-#6
  test API) into the new `apis.<area>.method` shape. Keeps existing
  test setup readable while the production code uses the new path.

Tests: 587/587 pass (same baseline; pre-existing network-manager
native-addon failure unchanged).
This commit is contained in:
DrSmoothl committed 2026-05-20 22:27:32 +08:00
1 parent 3e5dca1bd4
commit 6bf53890dc
17 files changed
+547 -465

No files matched your search

@@ -13,7 +13,7 @@ export function getLoginInfo(ref: OneBotInstanceContext): { userId: number; nick
// turn one OneBot request into N OIDB calls, which is risky for busy clients.
async function refreshSingleGroupMembers(bridge: BridgeInterface, groupId: number): Promise<void> {
try {
await bridge.fetchGroupMemberList(groupId);
await bridge.apis.contacts.fetchGroupMemberList(groupId);
} catch {
// Use cached data.
}
@@ -21,7 +21,7 @@ async function refreshSingleGroupMembers(bridge: BridgeInterface, groupId: numbe
export async function getFriendList(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const friends = await bridge.fetchFriendList();
const friends = await bridge.apis.contacts.fetchFriendList();
return friends.map(f => ({
user_id: f.uin as any,
nickname: f.nickname as any,
@@ -42,7 +42,7 @@ export async function getGroupList(
): Promise<JsonObject[]> {
try {
if (noCache || bridge.identity.groups.length === 0) {
await bridge.fetchGroupList();
await bridge.apis.contacts.fetchGroupList();
}
} catch {
// Use cached data.
@@ -62,7 +62,7 @@ export async function getGroupInfo(
): Promise<JsonObject | null> {
if (noCache || !bridge.identity.findGroup(groupId)) {
try {
await bridge.fetchGroupList();
await bridge.apis.contacts.fetchGroupList();
} catch {
// Use cached data.
}
@@ -88,7 +88,7 @@ export async function getGroupMemberList(
}
try {
const members = await bridge.fetchGroupMemberList(groupId);
const members = await bridge.apis.contacts.fetchGroupMemberList(groupId);
return members.map(m => formatGroupMember(groupId, m));
} catch {
return getCachedGroupMembers(bridge.identity, groupId);
@@ -146,7 +146,7 @@ export async function getStrangerInfo(
userId: number,
): Promise<JsonObject | null> {
try {
const p = await bridge.fetchUserProfile(userId);
const p = await bridge.apis.contacts.fetchUserProfile(userId);
return {
user_id: p.uin as any,
nickname: p.nickname as any,
@@ -167,7 +167,7 @@ export async function getStrangerInfo(
export async function getGroupSystemMessages(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const reqs = await bridge.fetchGroupRequests();
const reqs = await bridge.apis.contacts.fetchGroupRequests();
return reqs.map(r => ({
group_id: r.groupId,
group_name: r.groupName,
@@ -184,7 +184,7 @@ export async function getGroupSystemMessages(bridge: BridgeInterface): Promise<J
export async function getDownloadRKeys(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const rkeys = await bridge.fetchDownloadRKeys();
const rkeys = await bridge.apis.contacts.fetchDownloadRKeys();
return rkeys.map(r => ({
rkey: r.rkey,
type: r.type,
@@ -15,7 +15,7 @@ export async function handleGroupAddRequest(
if (!groupId) throw new Error('invalid group_id in flag');
if (!targetUid) throw new Error('invalid request target in flag');
const requests = await bridge.fetchGroupRequests();
const requests = await bridge.apis.contacts.fetchGroupRequests();
const matching = requests.find((r) => {
if (r.groupId !== groupId) return false;
if (requestType === 'add') return r.targetUid === targetUid;