instance-context.ts used to declare ~130 closures inside buildApiContext,
mirrored by ~130 method signatures on ApiActionContext. About 38 of
those were pure 1:1 pass-throughs — `setGroupAdmin: (gid, uid, en) =>
bridge.setGroupAdmin(gid, uid, en)`, `setOnlineStatus`, `setProfile`,
`setAvatar`, `markGroupMsgAsRead`, `getCookiesStr`, `getCsrfToken`,
`getCredentials`, the lot of them. Each was three places to edit (the
closure, the interface signature, the action call site) and zero added
value: the OneBot name matched the Bridge name and no defaults / rename
/ composition was happening.
Expose `bridge: BridgeInterface` on ApiActionContext and have action
handlers route through `ctx.bridge.<name>` for those cases. The 38
closure declarations go away; ApiActionContext shrinks from ~70 method
signatures to ~30; instance-context.ts loses ~50 lines of boilerplate.
The ~30 adapter closures stay — every one of them does real work:
- Renames: setGroupKick → bridge.kickGroupMember, sendFriendPoke →
bridge.sendPoke(false, …), handleDeleteFriend → bridge.deleteFriend
- Field extraction: uploadGroupFile returns result.fileId, not the
full {fileId} envelope
- Baked constants: setEssenceMsg passes `true`, deleteEssenceMsg
passes `false` to the shared setEssenceMessage module function
- Compositions: sendPrivateMessage / sendGroupMessage / setMsgEmojiLike
need messageStore + mediaStore + bridge wired together
- OneBot-specific defaults: getProfileLike defaults start=0 limit=10,
getGroupEssence defaults pageStart=0 pageLimit=50
Two small downstream nudges that fell out of the type changes:
- extended.ts:99 — `essenceDataAll.flatMap(res => …)` now needs an
explicit `(res: any)` since bridge.getGroupEssenceAll returns
`Promise<any>`, not the previously-asserted `GroupEssenceMsgRet[]`.
- extended.ts:835 — `okResponse(clientKeyInfo)` widened via spread to
`okResponse({ ...clientKeyInfo })` because `ClientKeyInfo` is a
named interface (no index signature) and okResponse expects
JsonValue/JsonObject. Behavior identical; the response shape is
unchanged.
402 tests pass (the OneBot contact-actions test added in PR6 doubles as
regression coverage for the seam).
228 lines
9.9 KiB
TypeScript
228 lines
9.9 KiB
TypeScript
import type { JsonObject, JsonValue, MessageMeta } from './types';
|
|
import type { ForwardPreviewMeta } from './modules/message-actions';
|
|
import type { BridgeInterface } from '../bridge/bridge-interface';
|
|
import { RETCODE, failedResponse } from './types';
|
|
|
|
import { register as registerInfo } from './actions/info';
|
|
import { register as registerMessage } from './actions/message';
|
|
import { register as registerFriend } from './actions/friend';
|
|
import { register as registerGroupInfo } from './actions/group-info';
|
|
import { register as registerGroupAdmin } from './actions/group-admin';
|
|
import { register as registerGroupFile } from './actions/group-file';
|
|
import { register as registerRequest } from './actions/request';
|
|
import { register as registerExtended } from './actions/extended';
|
|
|
|
import { WebHonorType } from '@/bridge/web/group-honor';
|
|
// import {ClientKeyInfo} from "@/bridge/bridge";
|
|
|
|
export interface MessageSendResult {
|
|
messageId: number;
|
|
meta?: MessageMeta;
|
|
echoEvent?: JsonObject;
|
|
}
|
|
|
|
export interface GroupEssenceMsgRet {
|
|
retcode: number;
|
|
data: {
|
|
is_end: boolean;
|
|
msg_list: any[];
|
|
[key: string]: any;
|
|
};
|
|
[key: string]: any;
|
|
}
|
|
|
|
export interface ApiActionContext {
|
|
/**
|
|
* Direct access to the BridgeInterface. Use for actions that are pure
|
|
* 1:1 calls to a Bridge method — no rename, no default args, no
|
|
* composition with messageStore / mediaStore. When translation IS
|
|
* needed, route through the named adapters declared below instead.
|
|
*/
|
|
bridge: BridgeInterface;
|
|
|
|
getLoginInfo: () => { userId: number; nickname: string };
|
|
isOnline: () => boolean;
|
|
getMessage: (messageId: number) => JsonObject | null;
|
|
getMessageMeta: (messageId: number) => MessageMeta | null;
|
|
sendPrivateMessage: (userId: number, message: JsonValue, autoEscape: boolean) => Promise<MessageSendResult>;
|
|
sendGroupMessage: (groupId: number, message: JsonValue, autoEscape: boolean) => Promise<MessageSendResult>;
|
|
deleteMessage: (messageId: number, meta: MessageMeta) => Promise<void>;
|
|
canSendImage: () => boolean;
|
|
canSendRecord: () => boolean;
|
|
// Info retrieval (async — triggers OIDB fetch)
|
|
getFriendList: () => Promise<JsonObject[]>;
|
|
getGroupList: (noCache?: boolean) => Promise<JsonObject[]>;
|
|
getGroupInfo: (groupId: number, noCache?: boolean) => Promise<JsonObject | null>;
|
|
getGroupMemberList: (groupId: number, noCache?: boolean) => Promise<JsonObject[]>;
|
|
getGroupMemberInfo: (groupId: number, userId: number, noCache?: boolean) => Promise<JsonObject | null>;
|
|
getStrangerInfo: (userId: number) => Promise<JsonObject | null>;
|
|
// Group admin (adapters: rename Bridge methods to OneBot vocabulary)
|
|
setGroupKick: (groupId: number, userId: number, rejectAdd: boolean) => Promise<void>;
|
|
setGroupKickMembers: (groupId: number, userIds: number[], rejectAdd: boolean) => Promise<void>;
|
|
setGroupBan: (groupId: number, userId: number, duration: number) => Promise<void>;
|
|
setGroupWholeBan: (groupId: number, enable: boolean) => Promise<void>;
|
|
setGroupLeave: (groupId: number) => Promise<void>;
|
|
// Group file (adapters: field extraction + Bridge name translation)
|
|
uploadGroupFile: (groupId: number, file: string, name?: string, folderId?: string, uploadFile?: boolean) => Promise<string | null>;
|
|
uploadPrivateFile: (userId: number, file: string, name?: string, uploadFile?: boolean) => Promise<string | null>;
|
|
getGroupFileUrl: (groupId: number, fileId: string, busId?: number) => Promise<string>;
|
|
getGroupFiles: (groupId: number, folderId?: string) => Promise<JsonObject>;
|
|
createGroupFileFolder: (groupId: number, name: string, parentId?: string) => Promise<void>;
|
|
getPrivateFileUrl: (userId: number, fileId: string, fileHash: string) => Promise<string>;
|
|
getGroupFileCount: (groupId: number) => Promise<{ fileCount: number; maxCount: number }>;
|
|
// Requests (adapters: rename to OneBot vocabulary)
|
|
handleFriendRequest: (flag: string, approve: boolean) => Promise<void>;
|
|
handleGroupRequest: (flag: string, subType: string, approve: boolean, reason: string) => Promise<void>;
|
|
// Pokes (adapters: flatten isGroup into bridge.sendPoke signature)
|
|
sendFriendPoke: (userId: number, targetId?: number) => Promise<void>;
|
|
sendGroupPoke: (groupId: number, userId: number) => Promise<void>;
|
|
// Essence (adapter: bakes the set/unset boolean)
|
|
setEssenceMsg: (messageId: number) => Promise<void>;
|
|
deleteEssenceMsg: (messageId: number) => Promise<void>;
|
|
// Profile reads with OneBot-specific default args
|
|
getProfileLike: (userId?: number, start?: number, limit?: number) => Promise<any>;
|
|
getGroupEssence: (groupId: number, pageStart?: number, pageLimit?: number) => Promise<GroupEssenceMsgRet>;
|
|
// Friend deletion (adapter: rename + force-boolean)
|
|
handleDeleteFriend: (userId: number, block?: boolean) => Promise<void>;
|
|
// Module wrappers / multi-dep compositions
|
|
getGroupMsgHistory: (groupId: number, messageId?: number, count?: number) => Promise<JsonObject[]>;
|
|
getFriendMsgHistory: (userId: number, messageId?: number, count?: number) => Promise<JsonObject[]>;
|
|
handleGetGroupSystemMsg: () => Promise<JsonObject[]>;
|
|
getDownloadRKeys: () => Promise<JsonObject[]>;
|
|
sendGroupForwardMsg: (groupId: number, messages: JsonValue, meta?: ForwardPreviewMeta) => Promise<{ messageId: number; forwardId: string }>;
|
|
sendPrivateForwardMsg: (userId: number, messages: JsonValue, meta?: ForwardPreviewMeta) => Promise<{ messageId: number; forwardId: string }>;
|
|
sendForwardMsg: (messages: JsonValue) => Promise<{ forwardId: string }>;
|
|
getForwardMsg: (resId: string) => Promise<JsonObject[]>;
|
|
forwardSingleMsg: (messageId: number, target: { groupId?: number; userId?: number }) => Promise<{ messageId: number }>;
|
|
setMsgEmojiLike: (messageId: number, emojiId: string, set: boolean) => Promise<void>;
|
|
// Media lookup (populated from previously dispatched message segments)
|
|
getImageInfo: (file: string) => Promise<JsonObject | null>;
|
|
getRecordInfo: (file: string) => Promise<JsonObject | null>;
|
|
}
|
|
|
|
type ActionHandler = (params: JsonObject) => Promise<import('./types').ApiResponse>;
|
|
|
|
export class ApiHandler {
|
|
private readonly handlers = new Map<string, ActionHandler>();
|
|
|
|
constructor(context: ApiActionContext) {
|
|
registerInfo(this, context);
|
|
registerMessage(this, context);
|
|
registerFriend(this, context);
|
|
registerGroupInfo(this, context);
|
|
registerGroupAdmin(this, context);
|
|
registerGroupFile(this, context);
|
|
registerRequest(this, context);
|
|
registerExtended(this, context);
|
|
}
|
|
|
|
registerAction(action: string, handler: ActionHandler): void {
|
|
this.handlers.set(action, handler);
|
|
}
|
|
|
|
async handle(action: string, params: JsonObject): Promise<import('./types').ApiResponse> {
|
|
const handler = this.handlers.get(action);
|
|
if (!handler) {
|
|
return failedResponse(RETCODE.UNKNOWN_ACTION, 'unknown action');
|
|
}
|
|
|
|
try {
|
|
return await handler(params);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : 'internal error';
|
|
return failedResponse(RETCODE.INTERNAL_ERROR, message);
|
|
}
|
|
}
|
|
|
|
async processRequest(rawRequest: string): Promise<string> {
|
|
if (!rawRequest.trim()) {
|
|
return JSON.stringify(failedResponse(RETCODE.BAD_REQUEST, 'bad request'));
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(rawRequest) as unknown;
|
|
if (!isJsonObject(parsed)) {
|
|
return JSON.stringify(failedResponse(RETCODE.BAD_REQUEST, 'bad request'));
|
|
}
|
|
|
|
const action = asString(parsed.action);
|
|
if (!action) {
|
|
return JSON.stringify(failedResponse(RETCODE.BAD_REQUEST, 'bad request'));
|
|
}
|
|
|
|
const params = isJsonObject(parsed.params) ? parsed.params : {};
|
|
const echo = parsed.echo;
|
|
const response = await this.handle(action, params);
|
|
if (echo !== undefined) {
|
|
response.echo = toJsonValue(echo);
|
|
}
|
|
|
|
return JSON.stringify(response);
|
|
} catch {
|
|
return JSON.stringify(failedResponse(RETCODE.BAD_REQUEST, 'bad request'));
|
|
}
|
|
}
|
|
}
|
|
|
|
function isJsonObject(value: unknown): value is JsonObject {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
export function asString(value: unknown, fallback = ''): string {
|
|
if (typeof value === 'string') return value;
|
|
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
|
|
return fallback;
|
|
}
|
|
|
|
export function asNumber(value: unknown): number {
|
|
if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value);
|
|
if (typeof value === 'string' && value.trim()) {
|
|
const n = Number(value);
|
|
if (Number.isFinite(n)) return Math.trunc(n);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
export function asBoolean(value: unknown, fallback: boolean): boolean {
|
|
if (typeof value === 'boolean') return value;
|
|
if (typeof value === 'number') return value !== 0;
|
|
if (typeof value === 'string') {
|
|
const text = value.trim().toLowerCase();
|
|
if (text === 'true' || text === '1' || text === 'yes' || text === 'on') return true;
|
|
if (text === 'false' || text === '0' || text === 'no' || text === 'off') return false;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
export function toJsonValue(value: unknown): JsonValue {
|
|
if (value === null) return null;
|
|
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') return value;
|
|
if (Array.isArray(value)) return value.map(toJsonValue);
|
|
if (isJsonObject(value)) {
|
|
const obj: JsonObject = {};
|
|
for (const [key, item] of Object.entries(value)) {
|
|
obj[key] = toJsonValue(item);
|
|
}
|
|
return obj;
|
|
}
|
|
return String(value);
|
|
}
|
|
|
|
export function asMessage(value: unknown): import('./types').JsonValue | undefined {
|
|
if (value === undefined) return undefined;
|
|
if (typeof value === 'string') {
|
|
const trimmed = value.trim();
|
|
if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
|
|
try {
|
|
const parsed = JSON.parse(trimmed);
|
|
if (Array.isArray(parsed)) {
|
|
return toJsonValue(parsed);
|
|
}
|
|
} catch {
|
|
// Fallback to literal text if it just looks like an array but is invalid JSON
|
|
}
|
|
}
|
|
}
|
|
return toJsonValue(value);
|
|
}
|