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:
1 parent
3e5dca1bd4
commit
6bf53890dc
17 files changed
+547
-465
No files matched your search
@@ -712,7 +712,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
// OneBot dialects clients use in the wild.
|
||||
const fetchFilteredGroupRequests = async () => {
|
||||
try {
|
||||
return await ctx.bridge.fetchGroupRequests(true);
|
||||
return await ctx.bridge.apis.contacts.fetchGroupRequests(true);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ export class RKeyCache {
|
||||
private lastRefreshAttempt = 0;
|
||||
|
||||
warmUp(bridge: BridgeInterface, uin: string): void {
|
||||
bridge.fetchDownloadRKeys().then(
|
||||
bridge.apis.contacts.fetchDownloadRKeys().then(
|
||||
(rkeys) => {
|
||||
this.updateCache(rkeys);
|
||||
log.info('rkeys loaded: UIN=%s count=%d', uin, rkeys.length);
|
||||
@@ -117,7 +117,7 @@ export class RKeyCache {
|
||||
this.lastRefreshAttempt = now;
|
||||
|
||||
// Schedule async refresh
|
||||
bridge.fetchDownloadRKeys().then(
|
||||
bridge.apis.contacts.fetchDownloadRKeys().then(
|
||||
(rkeys) => this.updateCache(rkeys),
|
||||
() => { /* ignore */ },
|
||||
);
|
||||
|
||||
@@ -89,7 +89,7 @@ async function warmUpBridgeState(uin: string, bridge: BridgeInterface): Promise<
|
||||
// which used to leave identity.nickname empty — see step 1b for the
|
||||
// explicit fallback.
|
||||
try {
|
||||
const friends = await bridge.fetchFriendList();
|
||||
const friends = await bridge.apis.contacts.fetchFriendList();
|
||||
log.info('friends loaded: UIN=%s count=%d', uin, friends.length);
|
||||
|
||||
for (const f of friends) {
|
||||
@@ -115,7 +115,7 @@ async function warmUpBridgeState(uin: string, bridge: BridgeInterface): Promise<
|
||||
// back in the friend list.
|
||||
if (!selfResolved && selfUin > 0) {
|
||||
try {
|
||||
const profile = await bridge.fetchUserProfile(selfUin);
|
||||
const profile = await bridge.apis.contacts.fetchUserProfile(selfUin);
|
||||
bridge.identity.setSelfProfile(profile);
|
||||
bridge.identity.nickname = profile.nickname || uin;
|
||||
log.debug('self info via profile: UIN=%s uid=%s nickname=%s',
|
||||
@@ -129,7 +129,7 @@ async function warmUpBridgeState(uin: string, bridge: BridgeInterface): Promise<
|
||||
// Step 2: Fetch group list
|
||||
let groups: { groupId: number }[] = [];
|
||||
try {
|
||||
groups = await bridge.fetchGroupList();
|
||||
groups = await bridge.apis.contacts.fetchGroupList();
|
||||
log.info('groups loaded: UIN=%s count=%d', uin, groups.length);
|
||||
} catch (e) {
|
||||
log.warn('failed to load groups for UIN %s: %s', uin, e instanceof Error ? e.message : String(e));
|
||||
@@ -141,7 +141,7 @@ async function warmUpBridgeState(uin: string, bridge: BridgeInterface): Promise<
|
||||
let failedGroupCount = 0;
|
||||
for (const g of groups) {
|
||||
try {
|
||||
const members = await bridge.fetchGroupMemberList(g.groupId);
|
||||
const members = await bridge.apis.contacts.fetchGroupMemberList(g.groupId);
|
||||
loadedGroupCount += 1;
|
||||
loadedMemberCount += members.length;
|
||||
if (VERBOSE_WARMUP) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user