ApiActionContext had 81 optional method fields (\`name?: ...\`) plus 4
required ones — but every single optional field was wired in
buildApiContext (verified by diffing the two lists). The optionality
was dead: nothing else implemented this interface, no test ever
swapped fields out, and the action handlers all paid for the
fiction with defensive guards like:
if (!ctx.sendGroupMessage) {
return failedResponse(RETCODE.ACTION_FAILED, 'send_group_msg is not implemented');
}
— 80 such guards total across the action files (78 in the 3-line
form, 1 single-line, 1 \`if (!(ctx.a && ctx.b))\` compound). None of
them could ever fire.
This commit deletes the dead optionality. Two mechanical changes:
1. api-handler.ts: 81 \`name?: ...\` -> \`name: ...\` in ApiActionContext.
The MessageSendResult optionals (meta?, echoEvent?) are unrelated
and stay as-is. After: 85 required fields, 0 optionals.
2. actions/*.ts: drop the 80 guard blocks. Net -129 lines across
extended, group-admin, group-file, group-info, friend, message,
request. info.ts had none to begin with.
What this buys:
- The type system now enforces the wiring contract. If
buildApiContext forgets to wire a field, tsc fails at the call
site — the bug we were ostensibly catching at runtime in 80 places
is now caught at compile time in 0 places, for free.
- Action handler bodies shrink visibly (avg ~3 lines per handler).
extended.ts goes 1021 -> 935.
- AI / human readers no longer skim past dead code.
Caught one TS2774 ("This condition will always return true since
this function is always defined") on a compound guard in
extended.ts:186 that the regex missed; removed it manually. That
diagnostic is the type system actually doing its job — exactly the
point of the change.
244 tests pass (was 244). Same pre-existing macOS-arm64 websocket
addon failure, unrelated.
110 lines
4.5 KiB
TypeScript
110 lines
4.5 KiB
TypeScript
import type { ApiHandler, ApiActionContext } from '../api-handler';
|
|
import { asNumber, asString, asBoolean } from '../api-handler';
|
|
import { RETCODE, failedResponse, okResponse } from '../types';
|
|
|
|
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
|
h.registerAction('set_group_kick', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const userId = asNumber(params.user_id);
|
|
const reject = asBoolean(params.reject_add_request, false);
|
|
if (!groupId || !userId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id are required');
|
|
await ctx.setGroupKick(groupId, userId, reject);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_kick_members', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const userIds = Array.isArray(params.user_id) ? params.user_id.map(asNumber).filter(Boolean) : [];
|
|
const reject = asBoolean(params.reject_add_request, false);
|
|
if (!groupId || userIds.length === 0) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id array are required');
|
|
await ctx.setGroupKickMembers(groupId, userIds, reject);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_ban', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const userId = asNumber(params.user_id);
|
|
const duration = asNumber(params.duration) || 1800;
|
|
if (!groupId || !userId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id are required');
|
|
await ctx.setGroupBan(groupId, userId, duration);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_whole_ban', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const enable = asBoolean(params.enable, true);
|
|
if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
|
|
await ctx.setGroupWholeBan(groupId, enable);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_add_option', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const addType = asNumber(params.add_type);
|
|
if (!groupId || addType === undefined) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and add_type are required');
|
|
await ctx.setGroupAddOption(groupId, addType);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_search', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
|
|
await ctx.setGroupSearch(groupId);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_admin', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const userId = asNumber(params.user_id);
|
|
const enable = asBoolean(params.enable, true);
|
|
if (!groupId || !userId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id are required');
|
|
await ctx.setGroupAdmin(groupId, userId, enable);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_card', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const userId = asNumber(params.user_id);
|
|
const card = asString(params.card);
|
|
if (!groupId || !userId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id are required');
|
|
await ctx.setGroupCard(groupId, userId, card);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_name', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const name = asString(params.group_name);
|
|
if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
|
|
await ctx.setGroupName(groupId, name);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_leave', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
|
|
await ctx.setGroupLeave(groupId);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_special_title', async (params) => {
|
|
const groupId = asNumber(params.group_id);
|
|
const userId = asNumber(params.user_id);
|
|
const title = asString(params.special_title);
|
|
if (!groupId || !userId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id are required');
|
|
await ctx.setGroupSpecialTitle(groupId, userId, title);
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_anonymous', async () => {
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_anonymous_ban', async () => {
|
|
return okResponse();
|
|
});
|
|
|
|
h.registerAction('set_group_portrait', async () => {
|
|
return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented');
|
|
});
|
|
}
|