feat(onebot): expose group-wide mute status
Report whether group-wide mute is enabled through the existing group list and group information responses. Keep standard and extended queries aligned, support explicit refreshes in the SDK, and update the generated action documentation without adding another endpoint. Preserve established compatibility values, isolate cached non-member results by account, and add coverage across the request, mapping, action, and client layers. Closes #313
This commit is contained in:
18 files changed
+687
-56
No files matched your search
@@ -68,8 +68,13 @@ export async function getGroupList(
|
||||
if (noCache || bridge.identity.groups.length === 0) {
|
||||
await bridge.apis.contacts.fetchGroupList();
|
||||
}
|
||||
} catch {
|
||||
// Use cached data.
|
||||
} catch (err) {
|
||||
if (noCache) throw err;
|
||||
log.warn(
|
||||
'group list refresh failed, using cached roster: uin=%s err=%s',
|
||||
bridge.identity.uin,
|
||||
err instanceof Error ? (err.stack ?? err.message) : String(err),
|
||||
);
|
||||
}
|
||||
return bridge.identity.groups.map(g => ({
|
||||
group_id: g.groupId,
|
||||
@@ -83,24 +88,36 @@ export async function getGroupList(
|
||||
group_create_time: g.createTime ?? 0,
|
||||
group_level: g.level ?? 0,
|
||||
group_memo: g.memo ?? '',
|
||||
group_all_shut: g.allMuted ? -1 : 0,
|
||||
}));
|
||||
}
|
||||
|
||||
// group_level lives only in the 0x88D_0 detail. Memoize it (short TTL) so a
|
||||
// joined-group get_group_info doesn't fetch the detail on every call (#197).
|
||||
// group_level lives only in the per-group detail response. Keep its existing
|
||||
// short cache, but read mutable group state from the roster refreshed above.
|
||||
const GROUP_LEVEL_TTL_MS = 5 * 60 * 1000;
|
||||
const groupLevelCache = new Map<number, { level: number; at: number }>();
|
||||
async function getGroupLevel(bridge: BridgeInterface, groupId: number, noCache?: boolean): Promise<number> {
|
||||
async function getGroupLevel(
|
||||
bridge: BridgeInterface,
|
||||
groupId: number,
|
||||
noCache?: boolean,
|
||||
): Promise<number> {
|
||||
if (!noCache) {
|
||||
const c = groupLevelCache.get(groupId);
|
||||
if (c && Date.now() - c.at < GROUP_LEVEL_TTL_MS) return c.level;
|
||||
const cached = groupLevelCache.get(groupId);
|
||||
if (cached && Date.now() - cached.at < GROUP_LEVEL_TTL_MS) return cached.level;
|
||||
}
|
||||
try {
|
||||
const detail = await bridge.apis.contacts.fetchGroupDetail(groupId);
|
||||
const level = detail?.level ?? 0;
|
||||
groupLevelCache.set(groupId, { level, at: Date.now() });
|
||||
return level;
|
||||
} catch {
|
||||
} catch (err) {
|
||||
if (noCache) throw err;
|
||||
log.warn(
|
||||
'group detail refresh failed, using roster metadata: uin=%s group=%d err=%s',
|
||||
bridge.identity.uin,
|
||||
groupId,
|
||||
err instanceof Error ? (err.stack ?? err.message) : String(err),
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -110,7 +127,11 @@ async function getGroupLevel(bridge: BridgeInterface, groupId: number, noCache?:
|
||||
// here; this only memoizes the per-id server query so a burst of invites for the
|
||||
// same group doesn't hammer 0x88D_0 (which would risk a rate-limit / kick).
|
||||
const NON_MEMBER_GROUP_TTL_MS = 5 * 60 * 1000;
|
||||
const nonMemberGroupCache = new Map<number, { info: JsonObject; at: number }>();
|
||||
const nonMemberGroupCache = new Map<string, { info: JsonObject; at: number }>();
|
||||
|
||||
function nonMemberGroupCacheKey(bridge: BridgeInterface, groupId: number): string {
|
||||
return `${bridge.identity.uin}:${groupId}`;
|
||||
}
|
||||
|
||||
export async function getGroupInfo(
|
||||
bridge: BridgeInterface,
|
||||
@@ -120,8 +141,14 @@ export async function getGroupInfo(
|
||||
if (noCache || !bridge.identity.findGroup(groupId)) {
|
||||
try {
|
||||
await bridge.apis.contacts.fetchGroupList();
|
||||
} catch {
|
||||
// Use cached data.
|
||||
} catch (err) {
|
||||
if (noCache) throw err;
|
||||
log.warn(
|
||||
'group list refresh failed, using cached roster: uin=%s group=%d err=%s',
|
||||
bridge.identity.uin,
|
||||
groupId,
|
||||
err instanceof Error ? (err.stack ?? err.message) : String(err),
|
||||
);
|
||||
}
|
||||
}
|
||||
const g = bridge.identity.findGroup(groupId);
|
||||
@@ -135,13 +162,15 @@ export async function getGroupInfo(
|
||||
group_create_time: g.createTime ?? 0,
|
||||
group_level: await getGroupLevel(bridge, groupId, noCache),
|
||||
group_memo: g.memo ?? '',
|
||||
group_all_shut: g.allMuted ? -1 : 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Not a joined group — fall back to the by-id server lookup so a group invite
|
||||
// can still resolve its name. Cached with a short TTL (skipped when noCache).
|
||||
const cacheKey = nonMemberGroupCacheKey(bridge, groupId);
|
||||
if (!noCache) {
|
||||
const cached = nonMemberGroupCache.get(groupId);
|
||||
const cached = nonMemberGroupCache.get(cacheKey);
|
||||
if (cached && Date.now() - cached.at < NON_MEMBER_GROUP_TTL_MS) return { ...cached.info };
|
||||
}
|
||||
try {
|
||||
@@ -156,12 +185,19 @@ export async function getGroupInfo(
|
||||
group_create_time: detail.createTime ?? 0,
|
||||
group_level: detail.level ?? 0,
|
||||
group_memo: detail.memo ?? '',
|
||||
group_all_shut: detail.allMuted ? -1 : 0,
|
||||
};
|
||||
nonMemberGroupCache.set(groupId, { info, at: Date.now() });
|
||||
nonMemberGroupCache.set(cacheKey, { info, at: Date.now() });
|
||||
return { ...info };
|
||||
}
|
||||
} catch {
|
||||
// Server lookup failed (no such group / denied) — fall through to null.
|
||||
} catch (err) {
|
||||
if (noCache) throw err;
|
||||
log.warn(
|
||||
'non-member group lookup failed: uin=%s group=%d err=%s',
|
||||
bridge.identity.uin,
|
||||
groupId,
|
||||
err instanceof Error ? (err.stack ?? err.message) : String(err),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user