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;
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ describe('onebot/contact-actions / getGroupFiles', () => {
|
||||
|
||||
describe('onebot/contact-actions / getGroupList', () => {
|
||||
it('triggers fetch when the in-memory roster is empty', async () => {
|
||||
const fetched = [{ ...makeGroup(100, 'Group A'), remark: '工作群' }];
|
||||
const fetched = [{ ...makeGroup(100, 'Group A'), remark: '工作群', allMuted: true }];
|
||||
// `groups` starts empty; the fetch callback flips it to mimic
|
||||
// bridge.apis.contacts.fetchGroupList writing back through identity.rememberGroups.
|
||||
let groups: QQGroupInfo[] = [];
|
||||
@@ -219,6 +219,7 @@ describe('onebot/contact-actions / getGroupList', () => {
|
||||
group_remark: '工作群',
|
||||
member_count: 0, max_member_count: 500,
|
||||
group_create_time: 0, group_level: 0, group_memo: '',
|
||||
group_all_shut: -1,
|
||||
}]);
|
||||
});
|
||||
|
||||
@@ -242,14 +243,13 @@ describe('onebot/contact-actions / getGroupList', () => {
|
||||
expect(bridge.apis.contacts.fetchGroupList).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('serves the stale cache when the fetch path throws', async () => {
|
||||
it('propagates fetch failures when noCache=true', async () => {
|
||||
const cached = [makeGroup(400, 'Group D')];
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupList: vi.fn(async () => { throw new Error('boom'); }),
|
||||
identity: fakeIdentity({ groups: cached }),
|
||||
});
|
||||
const out = await getGroupList(bridge, true);
|
||||
expect(out[0]).toMatchObject({ group_id: 400, group_name: 'Group D' });
|
||||
await expect(getGroupList(bridge, true)).rejects.toThrow('boom');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -259,6 +259,7 @@ describe('onebot/contact-actions / getGroupInfo', () => {
|
||||
const findGroup = vi.fn((groupId: number) => groupId === 500 ? cached : null);
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupList: vi.fn(async () => []),
|
||||
fetchGroupDetail: vi.fn(async () => cached),
|
||||
identity: fakeIdentity({ findGroup }),
|
||||
});
|
||||
const out = await getGroupInfo(bridge, 500);
|
||||
@@ -270,6 +271,38 @@ describe('onebot/contact-actions / getGroupInfo', () => {
|
||||
expect(bridge.apis.contacts.fetchGroupList).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps joined-group mute state on the roster source across level-cache hits', async () => {
|
||||
const cached = { ...makeGroup(510, 'Muted Group'), allMuted: true };
|
||||
const fetchGroupDetail = vi.fn(async () => ({
|
||||
...cached,
|
||||
level: 6,
|
||||
allMuted: false,
|
||||
}));
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupList: vi.fn(async () => []),
|
||||
fetchGroupDetail,
|
||||
identity: fakeIdentity({ findGroup: (groupId: number) => groupId === 510 ? cached : null }),
|
||||
});
|
||||
|
||||
const first = await getGroupInfo(bridge, 510);
|
||||
const second = await getGroupInfo(bridge, 510);
|
||||
|
||||
expect(first).toMatchObject({ group_level: 6, group_all_shut: -1 });
|
||||
expect(second).toMatchObject({ group_level: 6, group_all_shut: -1 });
|
||||
expect(fetchGroupDetail).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('propagates detail refresh failures when noCache=true', async () => {
|
||||
const cached = makeGroup(520, 'Refresh Group');
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupList: vi.fn(async () => [cached]),
|
||||
fetchGroupDetail: vi.fn(async () => { throw new Error('detail unavailable'); }),
|
||||
identity: fakeIdentity({ findGroup: (groupId: number) => groupId === 520 ? cached : null }),
|
||||
});
|
||||
|
||||
await expect(getGroupInfo(bridge, 520, true)).rejects.toThrow('detail unavailable');
|
||||
});
|
||||
|
||||
it('triggers fetch when the group is unknown to the cache', async () => {
|
||||
const cached = makeGroup(600, 'Group F');
|
||||
let known = false;
|
||||
@@ -277,6 +310,7 @@ describe('onebot/contact-actions / getGroupInfo', () => {
|
||||
const fetchGroupList = vi.fn(async () => { known = true; return [cached]; });
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupList,
|
||||
fetchGroupDetail: vi.fn(async () => cached),
|
||||
identity: fakeIdentity({ findGroup }),
|
||||
});
|
||||
const out = await getGroupInfo(bridge, 600);
|
||||
@@ -294,7 +328,10 @@ describe('onebot/contact-actions / getGroupInfo', () => {
|
||||
});
|
||||
|
||||
it('resolves a non-member group via the by-id server lookup (e.g. a group invite name)', async () => {
|
||||
const fetchGroupDetail = vi.fn(async () => makeGroup(7100, '邀请来的群'));
|
||||
const fetchGroupDetail = vi.fn(async () => ({
|
||||
...makeGroup(7100, '邀请来的群'),
|
||||
allMuted: true,
|
||||
}));
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupList: vi.fn(async () => []),
|
||||
fetchGroupDetail,
|
||||
@@ -306,6 +343,7 @@ describe('onebot/contact-actions / getGroupInfo', () => {
|
||||
group_id: 7100,
|
||||
group_name: '邀请来的群',
|
||||
group_remark: '',
|
||||
group_all_shut: -1,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -334,6 +372,36 @@ describe('onebot/contact-actions / getGroupInfo', () => {
|
||||
await getGroupInfo(bridge, 7300, true);
|
||||
expect(fetchGroupDetail).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('isolates non-member group details between accounts', async () => {
|
||||
const groupId = 7400;
|
||||
const firstFetch = vi.fn(async () => ({
|
||||
...makeGroup(groupId, 'Account A View'),
|
||||
allMuted: true,
|
||||
}));
|
||||
const secondFetch = vi.fn(async () => ({
|
||||
...makeGroup(groupId, 'Account B View'),
|
||||
allMuted: false,
|
||||
}));
|
||||
const firstBridge = fakeBridge({
|
||||
fetchGroupList: vi.fn(async () => []),
|
||||
fetchGroupDetail: firstFetch,
|
||||
identity: fakeIdentity({ uin: '10001', findGroup: () => null }),
|
||||
});
|
||||
const secondBridge = fakeBridge({
|
||||
fetchGroupList: vi.fn(async () => []),
|
||||
fetchGroupDetail: secondFetch,
|
||||
identity: fakeIdentity({ uin: '10002', findGroup: () => null }),
|
||||
});
|
||||
|
||||
const first = await getGroupInfo(firstBridge, groupId);
|
||||
const second = await getGroupInfo(secondBridge, groupId);
|
||||
|
||||
expect(first).toMatchObject({ group_name: 'Account A View', group_all_shut: -1 });
|
||||
expect(second).toMatchObject({ group_name: 'Account B View', group_all_shut: 0 });
|
||||
expect(firstFetch).toHaveBeenCalledOnce();
|
||||
expect(secondFetch).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
describe('onebot/contact-actions / getGroupMemberList', () => {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { ACTION_REGISTRY } from '../src/actions';
|
||||
|
||||
const getGroupInfo = ACTION_REGISTRY.actions.find(action => action.canonical === 'get_group_info')?.spec;
|
||||
const getGroupList = ACTION_REGISTRY.actions.find(action => action.canonical === 'get_group_list')?.spec;
|
||||
const getGroupInfoEx = ACTION_REGISTRY.actions.find(action => action.canonical === 'get_group_info_ex')?.spec;
|
||||
const getGroupDetailInfo = ACTION_REGISTRY.actions.find(action => action.canonical === 'get_group_detail_info')?.spec;
|
||||
if (!getGroupInfo || !getGroupList || !getGroupInfoEx || !getGroupDetailInfo) {
|
||||
throw new Error('group info actions missing');
|
||||
}
|
||||
|
||||
describe('group information actions', () => {
|
||||
it('returns and documents the group-wide mute state', async () => {
|
||||
const response = await getGroupInfo.toHandler({} as any)({ group_id: 123456 });
|
||||
const infoSchema = getGroupInfo.describe().returnsSchema;
|
||||
const listSchema = getGroupList.describe().returnsSchema;
|
||||
|
||||
expect(response).toMatchObject({
|
||||
status: 'ok',
|
||||
data: { group_id: 123456, group_all_shut: 0 },
|
||||
});
|
||||
expect(infoSchema?.properties).toHaveProperty('group_all_shut');
|
||||
expect(infoSchema?.required).toContain('group_all_shut');
|
||||
expect(listSchema?.items?.properties).toHaveProperty('group_all_shut');
|
||||
expect(listSchema?.items?.required).toContain('group_all_shut');
|
||||
});
|
||||
|
||||
it.each([
|
||||
['get_group_info_ex', getGroupInfoEx],
|
||||
['get_group_detail_info', getGroupDetailInfo],
|
||||
])('%s exposes the shared group information result', async (_name, action) => {
|
||||
const getGroupInfoResult = {
|
||||
group_id: 123456,
|
||||
group_name: 'Muted Group',
|
||||
group_all_shut: -1,
|
||||
};
|
||||
const getGroupInfoProvider = vi.fn(async () => getGroupInfoResult);
|
||||
const response = await action.toHandler({
|
||||
getGroupInfo: getGroupInfoProvider,
|
||||
} as any)({ group_id: 123456, no_cache: true });
|
||||
|
||||
expect(response).toMatchObject({ status: 'ok', data: getGroupInfoResult });
|
||||
expect(getGroupInfoProvider).toHaveBeenCalledWith(123456, true);
|
||||
expect(action.describe().returnsSchema?.properties).toHaveProperty('group_all_shut');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user