refactor(onebot): drop pure pass-through closures from ApiActionContext
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).
This commit is contained in:
6 files changed
+106
-151
No files matched your search
@@ -36,7 +36,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
const userId = asNumber(params.user_id);
|
||||
const times = asNumber(params.times) || 1;
|
||||
if (!userId) return failedResponse(RETCODE.BAD_REQUEST, 'user_id is required');
|
||||
await ctx.sendLike(userId, times);
|
||||
await ctx.bridge.sendLike(userId, times);
|
||||
return okResponse();
|
||||
});
|
||||
|
||||
@@ -94,9 +94,9 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const essenceDataAll = await ctx.getGroupEssenceAll(groupId);
|
||||
const essenceDataAll = await ctx.bridge.getGroupEssenceAll(groupId);
|
||||
|
||||
const allMsgs = essenceDataAll.flatMap(res => res.data?.msg_list || []);
|
||||
const allMsgs = essenceDataAll.flatMap((res: any) => res.data?.msg_list || []);
|
||||
|
||||
return okResponse(allMsgs);
|
||||
} catch (e) {
|
||||
@@ -125,7 +125,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
return failedResponse(RETCODE.BAD_REQUEST, 'group_id does not match message session');
|
||||
}
|
||||
|
||||
await ctx.setGroupReaction(meta.targetId, meta.sequence, code, isSet);
|
||||
await ctx.bridge.setGroupReaction(meta.targetId, meta.sequence, code, isSet);
|
||||
return okResponse();
|
||||
});
|
||||
|
||||
@@ -168,7 +168,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
}
|
||||
|
||||
|
||||
await ctx.markGroupMsgAsRead(groupId, meta.sequence);
|
||||
await ctx.bridge.markGroupMsgAsRead(groupId, meta.sequence);
|
||||
return okResponse();
|
||||
});
|
||||
|
||||
@@ -191,7 +191,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
}
|
||||
|
||||
|
||||
await ctx.markPrivateMsgAsRead(userId, meta.sequence);
|
||||
await ctx.bridge.markPrivateMsgAsRead(userId, meta.sequence);
|
||||
return okResponse();
|
||||
});
|
||||
|
||||
@@ -211,9 +211,9 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
}
|
||||
|
||||
if (meta.isGroup) {
|
||||
await ctx.markGroupMsgAsRead(targetId, meta.sequence);
|
||||
await ctx.bridge.markGroupMsgAsRead(targetId, meta.sequence);
|
||||
} else {
|
||||
await ctx.markPrivateMsgAsRead(targetId, meta.sequence);
|
||||
await ctx.bridge.markPrivateMsgAsRead(targetId, meta.sequence);
|
||||
}
|
||||
return okResponse();
|
||||
});
|
||||
@@ -258,7 +258,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
confirm_required: params.confirm_required !== undefined ? Number(params.confirm_required) : 1,
|
||||
};
|
||||
|
||||
await ctx.sendGroupNotice(groupId, content, options);
|
||||
await ctx.bridge.sendGroupNotice(groupId, content, options);
|
||||
return okResponse();
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -271,7 +271,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const notices = await ctx.getGroupNotice(groupId);
|
||||
const notices = await ctx.bridge.getGroupNotice(groupId);
|
||||
return okResponse(notices);
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -288,7 +288,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const success = await ctx.deleteGroupNotice(groupId, fid);
|
||||
const success = await ctx.bridge.deleteGroupNotice(groupId, fid);
|
||||
if (success) {
|
||||
return okResponse();
|
||||
} else {
|
||||
@@ -427,7 +427,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const cookies = await ctx.getCookiesStr(domain);
|
||||
const cookies = await ctx.bridge.getCookiesStr(domain);
|
||||
return okResponse({ cookies });
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -437,7 +437,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
h.registerAction('get_csrf_token', async () => {
|
||||
|
||||
try {
|
||||
const token = await ctx.getCsrfToken();
|
||||
const token = await ctx.bridge.getCsrfToken();
|
||||
return okResponse({ token });
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -449,7 +449,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const creds = await ctx.getCredentials(domain);
|
||||
const creds = await ctx.bridge.getCredentials(domain);
|
||||
return okResponse(creds);
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -480,7 +480,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
const userId = asNumber(params.user_id);
|
||||
const remark = asString(params.remark) ?? '';
|
||||
if (!userId) return failedResponse(RETCODE.BAD_REQUEST, 'user_id is required');
|
||||
await ctx.setFriendRemark(userId, remark);
|
||||
await ctx.bridge.setFriendRemark(userId, remark);
|
||||
return okResponse();
|
||||
});
|
||||
|
||||
@@ -488,7 +488,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
const groupId = asNumber(params.group_id);
|
||||
const remark = asString(params.remark) ?? '';
|
||||
if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
|
||||
await ctx.setGroupRemark(groupId, remark);
|
||||
await ctx.bridge.setGroupRemark(groupId, remark);
|
||||
return okResponse();
|
||||
});
|
||||
|
||||
@@ -570,7 +570,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
await ctx.setProfile(nickname, personalNote);
|
||||
await ctx.bridge.setProfile(nickname, personalNote);
|
||||
return okResponse();
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -590,7 +590,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
await ctx.setOnlineStatus(status, extStatus, batteryStatus);
|
||||
await ctx.bridge.setOnlineStatus(status, extStatus, batteryStatus);
|
||||
return okResponse();
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -653,7 +653,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
h.registerAction('fetch_custom_face', async (params) => {
|
||||
const count = asNumber(params.count) || 10;
|
||||
try {
|
||||
const urls = await ctx.fetchCustomFace(count);
|
||||
const urls = await ctx.bridge.fetchCustomFace(count);
|
||||
return okResponse(urls);
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -667,7 +667,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
try {
|
||||
const meta = ctx.getMessageMeta(messageId);
|
||||
if (!meta?.isGroup || !meta?.sequence) return failedResponse(RETCODE.BAD_REQUEST, 'message not found or not a group message');
|
||||
const result = await ctx.getEmojiLikes(meta.targetId, meta.sequence, emojiId);
|
||||
const result = await ctx.bridge.getEmojiLikes(meta.targetId, meta.sequence, emojiId);
|
||||
return okResponse({ emoji_like_list: result.users.map(u => ({ user_id: String(u.uin), nick_name: '' })) });
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -684,7 +684,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
try {
|
||||
const meta = ctx.getMessageMeta(messageId);
|
||||
if (!meta?.isGroup || !meta?.sequence) return failedResponse(RETCODE.BAD_REQUEST, 'message not found or not a group message');
|
||||
const result = await ctx.getEmojiLikes(meta.targetId, meta.sequence, emojiId, emojiType, count, cookie);
|
||||
const result = await ctx.bridge.getEmojiLikes(meta.targetId, meta.sequence, emojiId, emojiType, count, cookie);
|
||||
return okResponse({
|
||||
result: 0,
|
||||
errMsg: '',
|
||||
@@ -733,7 +733,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const data = await ctx.getGroupAtAllRemain(groupId);
|
||||
const data = await ctx.bridge.getGroupAtAllRemain(groupId);
|
||||
return okResponse(data);
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -743,7 +743,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
h.registerAction('get_unidirectional_friend_list', async () => {
|
||||
|
||||
try {
|
||||
const data = await ctx.getUnidirectionalFriendList();
|
||||
const data = await ctx.bridge.getUnidirectionalFriendList();
|
||||
return okResponse(data);
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -759,7 +759,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
await ctx.setSelfLongNick(longNick);
|
||||
await ctx.bridge.setSelfLongNick(longNick);
|
||||
return okResponse({});
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -780,7 +780,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
await ctx.setAvatar(file);
|
||||
await ctx.bridge.setAvatar(file);
|
||||
return okResponse();
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -802,7 +802,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
await ctx.setInputStatus(userId, eventType);
|
||||
await ctx.bridge.setInputStatus(userId, eventType);
|
||||
return okResponse({});
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -820,7 +820,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const translated = await ctx.translateEn2Zh(words);
|
||||
const translated = await ctx.bridge.translateEn2Zh(words);
|
||||
return okResponse({ words: translated });
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -828,11 +828,11 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
});
|
||||
|
||||
h.registerAction('get_clientkey', async () => {
|
||||
const clientKeyInfo = await ctx.forceFetchClientKey();
|
||||
const clientKeyInfo = await ctx.bridge.forceFetchClientKey();
|
||||
if (!clientKeyInfo.clientKey) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, 'get clientkey error');
|
||||
}
|
||||
return okResponse(clientKeyInfo);
|
||||
return okResponse({ ...clientKeyInfo });
|
||||
});
|
||||
|
||||
h.registerAction('get_mini_app_ark', async (params) => {
|
||||
@@ -844,7 +844,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const data = await ctx.getMiniAppArk(
|
||||
const data = await ctx.bridge.getMiniAppArk(
|
||||
String(type),
|
||||
String(title),
|
||||
String(desc),
|
||||
@@ -870,7 +870,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const data = await ctx.clickInlineKeyboardButton(
|
||||
const data = await ctx.bridge.clickInlineKeyboardButton(
|
||||
groupId,
|
||||
botAppid,
|
||||
String(buttonId),
|
||||
@@ -892,7 +892,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
await ctx.sendGroupSign(groupId);
|
||||
await ctx.bridge.sendGroupSign(groupId);
|
||||
return okResponse({});
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
|
||||
Reference in new issue
Block a user