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
@@ -16,6 +16,7 @@ import {
|
||||
type MessageMeta,
|
||||
} from '../types';
|
||||
import { defineAction, groupAction, groupUserAction, f } from '../action-kit';
|
||||
import { groupInfoReturnsSchema } from './group-info';
|
||||
import { GROUP_MESSAGE_EVENT, hashMessageIdInt32 } from '../message-id';
|
||||
|
||||
const DOWNLOAD_FILE_MAX_BYTES = 1024 * 1024 * 1024; // 1 GiB
|
||||
@@ -1852,10 +1853,14 @@ export const actions = [
|
||||
name: 'get_group_info_ex',
|
||||
summary: '获取群信息(扩展)',
|
||||
readOnly: true,
|
||||
params: { group_id: f.groupId() },
|
||||
returnsSchema: groupInfoReturnsSchema,
|
||||
params: {
|
||||
group_id: f.groupId(),
|
||||
no_cache: f.bool().default(false),
|
||||
},
|
||||
run: async (p, ctx) => {
|
||||
if (ctx.getGroupInfo) {
|
||||
return okResponse(await ctx.getGroupInfo(p.group_id));
|
||||
return okResponse(await ctx.getGroupInfo(p.group_id, p.no_cache));
|
||||
}
|
||||
return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
|
||||
},
|
||||
@@ -1865,10 +1870,14 @@ export const actions = [
|
||||
name: 'get_group_detail_info',
|
||||
summary: '获取群详细信息',
|
||||
readOnly: true,
|
||||
params: { group_id: f.groupId() },
|
||||
returnsSchema: groupInfoReturnsSchema,
|
||||
params: {
|
||||
group_id: f.groupId(),
|
||||
no_cache: f.bool().default(false),
|
||||
},
|
||||
run: async (p, ctx) => {
|
||||
if (ctx.getGroupInfo) {
|
||||
return okResponse(await ctx.getGroupInfo(p.group_id));
|
||||
return okResponse(await ctx.getGroupInfo(p.group_id, p.no_cache));
|
||||
}
|
||||
return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
|
||||
},
|
||||
|
||||
@@ -3,6 +3,25 @@ import { asString } from '../api-handler';
|
||||
import { RETCODE, failedResponse, okResponse } from '../types';
|
||||
import { WebHonorType } from '@snowluma/protocol/web/group-honor';
|
||||
|
||||
export const groupInfoReturnsSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
group_id: { type: 'integer', description: '群号' },
|
||||
group_name: { type: 'string', description: '群名' },
|
||||
group_remark: { type: 'string', description: '当前账号设置的群备注' },
|
||||
member_count: { type: 'integer', description: '当前成员数' },
|
||||
max_member_count: { type: 'integer', description: '成员上限' },
|
||||
group_create_time: { type: 'integer', description: '建群时间戳(秒)' },
|
||||
group_level: { type: 'integer', description: '群等级' },
|
||||
group_memo: { type: 'string', description: '群简介 / 公告预览' },
|
||||
group_all_shut: { type: 'integer', enum: [-1, 0], description: '是否开启全员禁言(-1 开启,0 关闭)' },
|
||||
},
|
||||
required: [
|
||||
'group_id', 'group_name', 'group_remark', 'member_count',
|
||||
'max_member_count', 'group_all_shut',
|
||||
],
|
||||
};
|
||||
|
||||
export const actions = [
|
||||
defineAction({
|
||||
name: 'get_group_list',
|
||||
@@ -22,8 +41,12 @@ export const actions = [
|
||||
group_create_time: { type: 'integer', description: '建群时间戳(秒)' },
|
||||
group_level: { type: 'integer', description: '群等级(列表批量场景恒 0,详见 get_group_info)' },
|
||||
group_memo: { type: 'string', description: '群简介 / 公告预览' },
|
||||
group_all_shut: { type: 'integer', enum: [-1, 0], description: '是否开启全员禁言(-1 开启,0 关闭)' },
|
||||
},
|
||||
required: ['group_id', 'group_name', 'group_remark', 'member_count', 'max_member_count'],
|
||||
required: [
|
||||
'group_id', 'group_name', 'group_remark', 'member_count',
|
||||
'max_member_count', 'group_all_shut',
|
||||
],
|
||||
},
|
||||
},
|
||||
params: { no_cache: f.bool().default(false) },
|
||||
@@ -41,20 +64,7 @@ export const actions = [
|
||||
summary: '获取群信息',
|
||||
readOnly: true,
|
||||
returns: '群信息对象。',
|
||||
returnsSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
group_id: { type: 'integer', description: '群号' },
|
||||
group_name: { type: 'string', description: '群名' },
|
||||
group_remark: { type: 'string', description: '当前账号设置的群备注' },
|
||||
member_count: { type: 'integer', description: '当前成员数' },
|
||||
max_member_count: { type: 'integer', description: '成员上限' },
|
||||
group_create_time: { type: 'integer', description: '建群时间戳(秒)' },
|
||||
group_level: { type: 'integer', description: '群等级' },
|
||||
group_memo: { type: 'string', description: '群简介 / 公告预览' },
|
||||
},
|
||||
required: ['group_id', 'group_name', 'group_remark', 'member_count', 'max_member_count'],
|
||||
},
|
||||
returnsSchema: groupInfoReturnsSchema,
|
||||
params: { no_cache: f.bool().default(false) },
|
||||
run: async (p, ctx) => {
|
||||
const groupId = p.group_id;
|
||||
@@ -68,6 +78,7 @@ export const actions = [
|
||||
group_create_time: 0,
|
||||
group_level: 0,
|
||||
group_memo: '',
|
||||
group_all_shut: 0,
|
||||
};
|
||||
if (ctx.getGroupInfo) {
|
||||
const info = await ctx.getGroupInfo(groupId, noCache);
|
||||
|
||||
@@ -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