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).
236 lines
6.5 KiB
TypeScript
236 lines
6.5 KiB
TypeScript
import type { BridgeInterface } from '../../bridge/bridge-interface';
|
|
import type { IdentityService } from '../../bridge/identity-service';
|
|
import type { OneBotInstanceContext } from '../instance-context';
|
|
import type { JsonObject } from '../types';
|
|
|
|
export function getLoginInfo(ref: OneBotInstanceContext): { userId: number; nickname: string } {
|
|
const userId = parseInt(ref.uin, 10) || 0;
|
|
const nickname = ref.bridge.identity.nickname || ref.uin;
|
|
return { userId, nickname };
|
|
}
|
|
|
|
// Keep refreshes scoped. A broad "refresh all members in all groups" call can
|
|
// 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.apis.contacts.fetchGroupMemberList(groupId);
|
|
} catch {
|
|
// Use cached data.
|
|
}
|
|
}
|
|
|
|
export async function getFriendList(bridge: BridgeInterface): Promise<JsonObject[]> {
|
|
try {
|
|
const friends = await bridge.apis.contacts.fetchFriendList();
|
|
return friends.map(f => ({
|
|
user_id: f.uin as any,
|
|
nickname: f.nickname as any,
|
|
remark: f.remark as any,
|
|
}));
|
|
} catch {
|
|
return bridge.identity.friends.map(f => ({
|
|
user_id: f.uin as any,
|
|
nickname: f.nickname as any,
|
|
remark: f.remark as any,
|
|
}));
|
|
}
|
|
}
|
|
|
|
export async function getGroupList(
|
|
bridge: BridgeInterface,
|
|
noCache?: boolean,
|
|
): Promise<JsonObject[]> {
|
|
try {
|
|
if (noCache || bridge.identity.groups.length === 0) {
|
|
await bridge.apis.contacts.fetchGroupList();
|
|
}
|
|
} catch {
|
|
// Use cached data.
|
|
}
|
|
return bridge.identity.groups.map(g => ({
|
|
group_id: g.groupId as any,
|
|
group_name: g.groupName as any,
|
|
member_count: g.memberCount as any,
|
|
max_member_count: g.memberMax as any,
|
|
}));
|
|
}
|
|
|
|
export async function getGroupInfo(
|
|
bridge: BridgeInterface,
|
|
groupId: number,
|
|
noCache?: boolean,
|
|
): Promise<JsonObject | null> {
|
|
if (noCache || !bridge.identity.findGroup(groupId)) {
|
|
try {
|
|
await bridge.apis.contacts.fetchGroupList();
|
|
} catch {
|
|
// Use cached data.
|
|
}
|
|
}
|
|
const g = bridge.identity.findGroup(groupId);
|
|
if (!g) return null;
|
|
return {
|
|
group_id: g.groupId as any,
|
|
group_name: g.groupName as any,
|
|
member_count: g.memberCount as any,
|
|
max_member_count: g.memberMax as any,
|
|
};
|
|
}
|
|
|
|
export async function getGroupMemberList(
|
|
bridge: BridgeInterface,
|
|
groupId: number,
|
|
noCache?: boolean,
|
|
): Promise<JsonObject[]> {
|
|
if (noCache) {
|
|
await refreshSingleGroupMembers(bridge, groupId);
|
|
return getCachedGroupMembers(bridge.identity, groupId);
|
|
}
|
|
|
|
try {
|
|
const members = await bridge.apis.contacts.fetchGroupMemberList(groupId);
|
|
return members.map(m => formatGroupMember(groupId, m));
|
|
} catch {
|
|
return getCachedGroupMembers(bridge.identity, groupId);
|
|
}
|
|
}
|
|
|
|
export async function getGroupMemberInfo(
|
|
bridge: BridgeInterface,
|
|
groupId: number,
|
|
userId: number,
|
|
noCache?: boolean,
|
|
): Promise<JsonObject | null> {
|
|
if (noCache || !bridge.identity.findGroupMember(groupId, userId)) {
|
|
await refreshSingleGroupMembers(bridge, groupId);
|
|
}
|
|
const m = bridge.identity.findGroupMember(groupId, userId);
|
|
if (!m) return null;
|
|
return formatGroupMember(groupId, m);
|
|
}
|
|
|
|
export async function getGroupFiles(
|
|
bridge: BridgeInterface,
|
|
groupId: number,
|
|
folderId?: string,
|
|
): Promise<JsonObject> {
|
|
const result = await bridge.fetchGroupFiles(groupId, folderId ?? '/');
|
|
return {
|
|
files: result.files.map((file) => ({
|
|
group_id: groupId as any,
|
|
file_id: file.fileId as any,
|
|
file_name: file.fileName as any,
|
|
busid: file.busId as any,
|
|
file_size: file.fileSize as any,
|
|
upload_time: file.uploadTime as any,
|
|
dead_time: file.deadTime as any,
|
|
modify_time: file.modifyTime as any,
|
|
download_times: file.downloadTimes as any,
|
|
uploader: file.uploader as any,
|
|
uploader_name: file.uploaderName as any,
|
|
} as JsonObject)) as any,
|
|
folders: result.folders.map((folder) => ({
|
|
group_id: groupId as any,
|
|
folder_id: folder.folderId as any,
|
|
folder_name: folder.folderName as any,
|
|
create_time: folder.createTime as any,
|
|
creator: folder.creator as any,
|
|
create_name: folder.creatorName as any,
|
|
total_file_count: folder.totalFileCount as any,
|
|
} as JsonObject)) as any,
|
|
};
|
|
}
|
|
|
|
export async function getStrangerInfo(
|
|
bridge: BridgeInterface,
|
|
userId: number,
|
|
): Promise<JsonObject | null> {
|
|
try {
|
|
const p = await bridge.apis.contacts.fetchUserProfile(userId);
|
|
return {
|
|
user_id: p.uin as any,
|
|
nickname: p.nickname as any,
|
|
sex: p.sex as any,
|
|
age: p.age as any,
|
|
};
|
|
} catch {
|
|
const p = bridge.identity.findUserProfile(userId);
|
|
if (!p) return null;
|
|
return {
|
|
user_id: p.uin as any,
|
|
nickname: p.nickname as any,
|
|
sex: p.sex as any,
|
|
age: p.age as any,
|
|
};
|
|
}
|
|
}
|
|
|
|
export async function getGroupSystemMessages(bridge: BridgeInterface): Promise<JsonObject[]> {
|
|
try {
|
|
const reqs = await bridge.apis.contacts.fetchGroupRequests();
|
|
return reqs.map(r => ({
|
|
group_id: r.groupId,
|
|
group_name: r.groupName,
|
|
request_id: r.sequence,
|
|
requester_uin: r.targetUin,
|
|
requester_nick: r.targetName,
|
|
message: r.comment,
|
|
flag: `${r.eventType}:${r.groupId}:${r.targetUid}`,
|
|
} as JsonObject));
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function getDownloadRKeys(bridge: BridgeInterface): Promise<JsonObject[]> {
|
|
try {
|
|
const rkeys = await bridge.apis.contacts.fetchDownloadRKeys();
|
|
return rkeys.map(r => ({
|
|
rkey: r.rkey,
|
|
type: r.type,
|
|
ttl: r.ttlSeconds,
|
|
create_time: r.createTime,
|
|
} as JsonObject));
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function getCachedGroupMembers(identity: IdentityService, groupId: number): JsonObject[] {
|
|
const g = identity.findGroup(groupId);
|
|
if (!g) return [];
|
|
const result: JsonObject[] = [];
|
|
for (const [, member] of g.members) {
|
|
result.push(formatGroupMember(groupId, member));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function formatGroupMember(
|
|
groupId: number,
|
|
member: {
|
|
uin: number;
|
|
nickname: string;
|
|
card: string;
|
|
joinTime: number;
|
|
lastSentTime: number;
|
|
level: number;
|
|
role: string;
|
|
title: string;
|
|
},
|
|
): JsonObject {
|
|
return {
|
|
group_id: groupId as any,
|
|
user_id: member.uin as any,
|
|
nickname: member.nickname as any,
|
|
card: member.card as any,
|
|
sex: 'unknown' as any,
|
|
age: 0 as any,
|
|
join_time: member.joinTime as any,
|
|
last_sent_time: member.lastSentTime as any,
|
|
level: String(member.level) as any,
|
|
role: member.role as any,
|
|
title: member.title as any,
|
|
};
|
|
}
|