fix(onebot): accept async quick-operation alias
NoneBot 1 request handlers call `.handle_quick_operation_async`. Treat that name as the existing quick-operation action so group invites are approved instead of ignored. Fixes #434
This commit is contained in:
4 files changed
+66
-9
No files matched your search
@@ -4,8 +4,9 @@
|
||||
// structured doc list + a Markdown view. The metadata is carried on the same
|
||||
// values that drive runtime validation, so docs cannot drift from behavior.
|
||||
//
|
||||
// The reserved `.handle_quick_operation` raw handler deliberately has no
|
||||
// ActionSpec/doc entry; every catalogued action comes from ACTION_REGISTRY.
|
||||
// The reserved `.handle_quick_operation` raw handler (and its `_async` alias)
|
||||
// deliberately has no ActionSpec/doc entry; every catalogued action comes
|
||||
// from ACTION_REGISTRY.
|
||||
//
|
||||
// The serving SURFACE (WebUI panel / OpenAPI export / static markdown) is a
|
||||
// deferred product decision; this module produces format-agnostic data plus a
|
||||
@@ -58,7 +59,7 @@ export function renderActionDocsMarkdown(docs: readonly ActionDoc[] = collectAct
|
||||
'# OneBot Actions',
|
||||
'',
|
||||
'> 由 `packages/onebot/src/action-docs.ts` 从各 `ActionSpec.describe()` 自动生成,请勿手改。',
|
||||
'> 覆盖完整声明式 Action registry;保留的 raw `.handle_quick_operation` handler 不生成文档。',
|
||||
'> 覆盖完整声明式 Action registry;保留的 raw `.handle_quick_operation` / `.handle_quick_operation_async` handler 不生成文档。',
|
||||
'',
|
||||
`共 ${docs.length} 个声明式 action。`,
|
||||
'',
|
||||
|
||||
@@ -211,14 +211,14 @@ function bindActionRegistry(
|
||||
|
||||
for (const claim of executableNames) {
|
||||
if (claim.kind === 'raw') {
|
||||
const factory = rawFactories[claim.name];
|
||||
const factory = rawFactories[claim.canonical];
|
||||
if (!factory) {
|
||||
throw new Error(
|
||||
`Action registry raw factory missing: canonical "${claim.canonical}" `
|
||||
+ `(name "${claim.name}", kind raw)`,
|
||||
);
|
||||
}
|
||||
factoryNames.delete(claim.name);
|
||||
factoryNames.delete(claim.canonical);
|
||||
handlers.set(claim.name, Object.freeze({
|
||||
handler: factory(api),
|
||||
canonical: claim.canonical,
|
||||
@@ -262,10 +262,12 @@ export const ACTION_GROUPS: readonly ActionGroup[] = [
|
||||
{ category: '流式接口', actions: [...streamFileActions, ...streamDownloadActions] },
|
||||
];
|
||||
|
||||
/** The sole non-ActionSpec handler; reserved in the same namespace up front. */
|
||||
/** Hidden OneBot v11 quick-operation handler; `_async` is the NoneBot 1 alias. */
|
||||
export const HANDLE_QUICK_OPERATION_ACTION = '.handle_quick_operation';
|
||||
export const HANDLE_QUICK_OPERATION_ASYNC_ACTION = '.handle_quick_operation_async';
|
||||
export const RAW_ACTION_RESERVATIONS: readonly RawActionReservation[] = [
|
||||
{ name: HANDLE_QUICK_OPERATION_ACTION, canonical: HANDLE_QUICK_OPERATION_ACTION },
|
||||
{ name: HANDLE_QUICK_OPERATION_ASYNC_ACTION, canonical: HANDLE_QUICK_OPERATION_ACTION },
|
||||
];
|
||||
|
||||
/** Complete, validated runtime/docs registry. Compilation happens on import. */
|
||||
|
||||
@@ -147,13 +147,13 @@ export class ApiHandler {
|
||||
this.log = typeof uin === 'number' && uin > 0 ? moduleLog.child({ uin }) : moduleLog;
|
||||
const rawFactories: Record<string, (api: ApiHandler) => ActionHandler> = {};
|
||||
for (const raw of registry.rawActions) {
|
||||
if (raw.name !== HANDLE_QUICK_OPERATION_ACTION) {
|
||||
if (raw.canonical !== HANDLE_QUICK_OPERATION_ACTION) {
|
||||
throw new Error(
|
||||
`Action registry has no factory for raw action canonical "${raw.canonical}" `
|
||||
+ `(name "${raw.name}", kind raw)`,
|
||||
);
|
||||
}
|
||||
rawFactories[raw.name] = (api) => async (params) => {
|
||||
rawFactories[raw.canonical] = (api) => async (params) => {
|
||||
const opContext = params.context as JsonObject | undefined;
|
||||
const operation = params.operation as Record<string, unknown> | undefined;
|
||||
if (!opContext || !operation) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { ApiHandler, type ApiActionContext } from '../src/api-handler';
|
||||
import {
|
||||
ACTION_REGISTRY,
|
||||
HANDLE_QUICK_OPERATION_ACTION,
|
||||
HANDLE_QUICK_OPERATION_ASYNC_ACTION,
|
||||
RAW_ACTION_RESERVATIONS,
|
||||
compileActionRegistry,
|
||||
type ActionGroup,
|
||||
@@ -79,6 +80,15 @@ describe('compileActionRegistry namespace conflicts', () => {
|
||||
expect(message).toContain('kind raw, role raw');
|
||||
});
|
||||
|
||||
it('reserves the async quick-operation alias against declarative names', () => {
|
||||
const message = conflict(
|
||||
groups(normal(HANDLE_QUICK_OPERATION_ASYNC_ACTION)),
|
||||
RAW_ACTION_RESERVATIONS,
|
||||
);
|
||||
expect(message).toContain(`name "${HANDLE_QUICK_OPERATION_ASYNC_ACTION}"`);
|
||||
expect(message).toContain('kind raw, role raw');
|
||||
});
|
||||
|
||||
it('rejects duplicate raw reservations', () => {
|
||||
const message = conflict([], [
|
||||
{ name: 'raw', canonical: 'first-raw' },
|
||||
@@ -103,7 +113,11 @@ describe('compiled production Action registry', () => {
|
||||
for (const executable of ACTION_REGISTRY.executableNames) {
|
||||
expect(ACTION_REGISTRY.resolve(executable.name)).toBe(executable);
|
||||
if (executable.kind === 'raw') {
|
||||
expect(executable.name).toBe(HANDLE_QUICK_OPERATION_ACTION);
|
||||
expect(executable.canonical).toBe(HANDLE_QUICK_OPERATION_ACTION);
|
||||
expect([
|
||||
HANDLE_QUICK_OPERATION_ACTION,
|
||||
HANDLE_QUICK_OPERATION_ASYNC_ACTION,
|
||||
]).toContain(executable.name);
|
||||
expect(docs.has(executable.canonical)).toBe(false);
|
||||
continue;
|
||||
}
|
||||
@@ -130,6 +144,46 @@ describe('compiled production Action registry', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('resolves the async quick-operation alias to the reserved raw handler', () => {
|
||||
const syncName = ACTION_REGISTRY.resolve(HANDLE_QUICK_OPERATION_ACTION);
|
||||
const asyncName = ACTION_REGISTRY.resolve(HANDLE_QUICK_OPERATION_ASYNC_ACTION);
|
||||
expect(syncName).toMatchObject({
|
||||
name: HANDLE_QUICK_OPERATION_ACTION,
|
||||
canonical: HANDLE_QUICK_OPERATION_ACTION,
|
||||
kind: 'raw',
|
||||
role: 'raw',
|
||||
});
|
||||
expect(asyncName).toMatchObject({
|
||||
name: HANDLE_QUICK_OPERATION_ASYNC_ACTION,
|
||||
canonical: HANDLE_QUICK_OPERATION_ACTION,
|
||||
kind: 'raw',
|
||||
role: 'raw',
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
HANDLE_QUICK_OPERATION_ACTION,
|
||||
HANDLE_QUICK_OPERATION_ASYNC_ACTION,
|
||||
])('dispatches %s to set_group_add_request', async (action) => {
|
||||
const handleGroupRequest = vi.fn(async () => undefined);
|
||||
const handler = new ApiHandler({ handleGroupRequest } as unknown as ApiActionContext);
|
||||
|
||||
const response = await handler.handle(action, {
|
||||
context: {
|
||||
post_type: 'request',
|
||||
request_type: 'group',
|
||||
sub_type: 'invite',
|
||||
group_id: 100000002,
|
||||
user_id: 10001,
|
||||
flag: 'invite-flag',
|
||||
},
|
||||
operation: { approve: true, remark: '' },
|
||||
});
|
||||
|
||||
expect(response.status).toBe('ok');
|
||||
expect(handleGroupRequest).toHaveBeenCalledWith('invite-flag', 'invite', true, '');
|
||||
});
|
||||
|
||||
it('validates and forwards get_group_system_msg filters', async () => {
|
||||
const handleGetGroupSystemMsg = vi.fn(async () => []);
|
||||
const handler = new ApiHandler({ handleGetGroupSystemMsg } as unknown as ApiActionContext);
|
||||
|
||||
Reference in new issue
Block a user