refactor(api): move 4 misc methods to MiscApi (#6 commit 10/13)
Inlines `actions/misc.ts` into `apis/misc.ts::MiscApi`. The Api holds the methods that didn't naturally group into any other area: translation, mini-app ARK card builder, inline-keyboard callback, group sign-in. No renaming — these names are already descriptive at the call site under `apis.misc.X`. Callers updated: `actions/extended.ts` (4 sites). Tests rewritten to use `new MiscApi(bridge as any).method(...)`. 587/587 tests pass.
This commit is contained in:
1 parent
d953b9824c
commit
e5ac2a61b1
7 files changed
+168
-188
No files matched your search
@@ -1,153 +0,0 @@
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { Bridge } from '../bridge';
|
||||
import { makeOidbEnvelope, runOidb } from '../bridge-oidb';
|
||||
import { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
MiniAppShareReq,
|
||||
MiniAppShareResp,
|
||||
Oidb0x112eReq,
|
||||
Oidb0x112eResp,
|
||||
Oidb0x990Req,
|
||||
Oidb0x990Resp,
|
||||
Oidb0xeb7Req,
|
||||
Oidb0xeb7Resp,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
|
||||
export async function translateEn2Zh(
|
||||
bridge: Bridge,
|
||||
words: string[],
|
||||
) {
|
||||
const req = {
|
||||
translateReq: {
|
||||
srcLang: 'en',
|
||||
dstLang: 'zh',
|
||||
words: words,
|
||||
},
|
||||
tag10: 1,
|
||||
tag12: 1,
|
||||
};
|
||||
|
||||
const env = makeOidbEnvelope<Oidb0x990Req>(0x990, 2, req);
|
||||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x990_2', protobuf_encode<OidbBase<Oidb0x990Req>>(env));
|
||||
const result = protobuf_decode<OidbBase<Oidb0x990Resp>>(respBytes).body;
|
||||
|
||||
const resp = result?.translateResp;
|
||||
if (!resp) {
|
||||
throw new Error('translate response empty');
|
||||
}
|
||||
|
||||
return resp.dstWords || [];
|
||||
}
|
||||
|
||||
export async function getMiniAppArk(
|
||||
bridge: Bridge,
|
||||
type: string,
|
||||
title: string,
|
||||
desc: string,
|
||||
picUrl: string,
|
||||
jumpUrl: string,
|
||||
) {
|
||||
let appid = '1109937557'; // default: bilibili
|
||||
let iconUrl = 'http://miniapp.gtimg.cn/public/appicon/51f90239b78a2e4994c11215f4c4ba15_200.jpg';
|
||||
|
||||
if (type === 'weibo') {
|
||||
appid = '1109224783';
|
||||
iconUrl = 'http://miniapp.gtimg.cn/public/appicon/35bbb44dc68e65194cfacfb206b8f1f7_200.jpg';
|
||||
} else if (type !== 'bili') {
|
||||
throw new Error(`unsupported type: ${type}, only support bili and weibo`);
|
||||
}
|
||||
|
||||
const request = protobuf_encode<MiniAppShareReq>({
|
||||
sdkVersion: 'V1_PC_MINISDK_99.99.99_1_APP_A',
|
||||
body: {
|
||||
appid,
|
||||
title,
|
||||
desc,
|
||||
picUrl,
|
||||
jumpUrl,
|
||||
iconUrl,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await bridge.sendRawPacket('LightAppSvc.mini_app_share.AdaptShareInfo', request);
|
||||
|
||||
if (!result.success || !result.responseData) {
|
||||
throw new Error(result.errorMessage || 'get mini app ark failed');
|
||||
}
|
||||
|
||||
const decoded = protobuf_decode<MiniAppShareResp>(result.responseData);
|
||||
const jsonStr = decoded?.body?.jsonStr;
|
||||
|
||||
if (!jsonStr) {
|
||||
throw new Error('mini app share json empty');
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(jsonStr);
|
||||
|
||||
return {
|
||||
data: {
|
||||
ver: parsed.ver,
|
||||
prompt: parsed.prompt,
|
||||
config: parsed.config,
|
||||
app: parsed.appName,
|
||||
view: parsed.appView,
|
||||
meta: parsed.metaData,
|
||||
miniappShareOrigin: 3,
|
||||
miniappOpenRefer: '10002',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function clickInlineKeyboardButton(
|
||||
bridge: Bridge,
|
||||
groupId: number,
|
||||
botAppid: number,
|
||||
buttonId: string,
|
||||
callbackData: string,
|
||||
msgSeq: number,
|
||||
) {
|
||||
const req = {
|
||||
botAppid: BigInt(botAppid),
|
||||
msgSeq: BigInt(msgSeq),
|
||||
buttonId: String(buttonId),
|
||||
callbackData: String(callbackData || ''),
|
||||
unknown7: 0,
|
||||
groupId: BigInt(groupId),
|
||||
unknown9: 1,
|
||||
};
|
||||
|
||||
const env = makeOidbEnvelope<Oidb0x112eReq>(0x112E, 1, req);
|
||||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x112e_1', protobuf_encode<OidbBase<Oidb0x112eReq>>(env));
|
||||
const result = protobuf_decode<OidbBase<Oidb0x112eResp>>(respBytes).body;
|
||||
|
||||
if (!result) {
|
||||
throw new Error('click inline keyboard button result empty');
|
||||
}
|
||||
|
||||
return {
|
||||
result: Number(result.result || 0),
|
||||
errMsg: result.errMsg || '',
|
||||
status: 0,
|
||||
promptText: result.promptText || '',
|
||||
promptType: 0,
|
||||
promptIcon: 0,
|
||||
};
|
||||
}
|
||||
|
||||
export async function sendGroupSign(
|
||||
bridge: Bridge,
|
||||
groupId: number,
|
||||
) {
|
||||
const req = {
|
||||
signInInfo: {
|
||||
uin: String(bridge.identity.uin),
|
||||
groupId: String(groupId),
|
||||
version: '9.0.90',
|
||||
},
|
||||
};
|
||||
|
||||
const env = makeOidbEnvelope<Oidb0xeb7Req>(0xEB7, 1, req);
|
||||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0xEB7_1', protobuf_encode<OidbBase<Oidb0xeb7Req>>(env));
|
||||
// Decode just to maintain the original behaviour of "consume the response".
|
||||
protobuf_decode<OidbBase<Oidb0xeb7Resp>>(respBytes);
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import { GroupAlbumApi } from './group-album';
|
||||
import { GroupFileApi } from './group-file';
|
||||
import { InteractionApi } from './interaction';
|
||||
import { MessageApi } from './message';
|
||||
import { MiscApi } from './misc';
|
||||
import { ProfileApi } from './profile';
|
||||
|
||||
export interface ApiHub {
|
||||
@@ -50,6 +51,8 @@ export interface ApiHub {
|
||||
readonly profile: ProfileApi;
|
||||
/** Long-message (forward / 聊天记录) upload + retrieval with NapCat piggyback. */
|
||||
readonly forward: ForwardApi;
|
||||
/** Odds & ends: translate / mini-app ARK / inline-keyboard / group sign. */
|
||||
readonly misc: MiscApi;
|
||||
// Pending — added as later commits land:
|
||||
// readonly interaction: InteractionApi;
|
||||
// readonly profile: ProfileApi;
|
||||
@@ -78,6 +81,7 @@ export function buildApiHub(ctx: BridgeContext): ApiHub {
|
||||
friend: new FriendApi(ctx),
|
||||
profile: new ProfileApi(ctx),
|
||||
forward: new ForwardApi(ctx),
|
||||
misc: new MiscApi(ctx),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -92,4 +96,5 @@ export { GroupAlbumApi } from './group-album';
|
||||
export { GroupFileApi } from './group-file';
|
||||
export { InteractionApi } from './interaction';
|
||||
export { MessageApi } from './message';
|
||||
export { MiscApi } from './misc';
|
||||
export { ProfileApi } from './profile';
|
||||
@@ -0,0 +1,146 @@
|
||||
// MiscApi — odds & ends that don't fit the other Apis: translation,
|
||||
// mini-app ARK build, inline-keyboard button click, group sign-in.
|
||||
// Inlined from `actions/misc.ts` (deleted alongside actions/* in
|
||||
// commit 13).
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
MiniAppShareReq,
|
||||
MiniAppShareResp,
|
||||
Oidb0x112eReq,
|
||||
Oidb0x112eResp,
|
||||
Oidb0x990Req,
|
||||
Oidb0x990Resp,
|
||||
Oidb0xeb7Req,
|
||||
Oidb0xeb7Resp,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import type { BridgeContext } from '../bridge-context';
|
||||
import type { Bridge } from '../bridge';
|
||||
import { makeOidbEnvelope, runOidb } from '../bridge-oidb';
|
||||
|
||||
function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; }
|
||||
|
||||
export class MiscApi {
|
||||
constructor(private readonly ctx: BridgeContext) {}
|
||||
|
||||
async translateEn2Zh(words: string[]): Promise<string[]> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const req = {
|
||||
translateReq: {
|
||||
srcLang: 'en',
|
||||
dstLang: 'zh',
|
||||
words,
|
||||
},
|
||||
tag10: 1,
|
||||
tag12: 1,
|
||||
};
|
||||
|
||||
const env = makeOidbEnvelope<Oidb0x990Req>(0x990, 2, req);
|
||||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x990_2', protobuf_encode<OidbBase<Oidb0x990Req>>(env));
|
||||
const result = protobuf_decode<OidbBase<Oidb0x990Resp>>(respBytes).body;
|
||||
|
||||
const resp = result?.translateResp;
|
||||
if (!resp) {
|
||||
throw new Error('translate response empty');
|
||||
}
|
||||
|
||||
return resp.dstWords || [];
|
||||
}
|
||||
|
||||
async getMiniAppArk(type: string, title: string, desc: string, picUrl: string, jumpUrl: string): Promise<any> {
|
||||
let appid = '1109937557'; // default: bilibili
|
||||
let iconUrl = 'http://miniapp.gtimg.cn/public/appicon/51f90239b78a2e4994c11215f4c4ba15_200.jpg';
|
||||
|
||||
if (type === 'weibo') {
|
||||
appid = '1109224783';
|
||||
iconUrl = 'http://miniapp.gtimg.cn/public/appicon/35bbb44dc68e65194cfacfb206b8f1f7_200.jpg';
|
||||
} else if (type !== 'bili') {
|
||||
throw new Error(`unsupported type: ${type}, only support bili and weibo`);
|
||||
}
|
||||
|
||||
const request = protobuf_encode<MiniAppShareReq>({
|
||||
sdkVersion: 'V1_PC_MINISDK_99.99.99_1_APP_A',
|
||||
body: { appid, title, desc, picUrl, jumpUrl, iconUrl },
|
||||
});
|
||||
|
||||
const result = await this.ctx.sendRawPacket('LightAppSvc.mini_app_share.AdaptShareInfo', request);
|
||||
|
||||
if (!result.success || !result.responseData) {
|
||||
throw new Error(result.errorMessage || 'get mini app ark failed');
|
||||
}
|
||||
|
||||
const decoded = protobuf_decode<MiniAppShareResp>(result.responseData);
|
||||
const jsonStr = decoded?.body?.jsonStr;
|
||||
|
||||
if (!jsonStr) {
|
||||
throw new Error('mini app share json empty');
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(jsonStr);
|
||||
|
||||
return {
|
||||
data: {
|
||||
ver: parsed.ver,
|
||||
prompt: parsed.prompt,
|
||||
config: parsed.config,
|
||||
app: parsed.appName,
|
||||
view: parsed.appView,
|
||||
meta: parsed.metaData,
|
||||
miniappShareOrigin: 3,
|
||||
miniappOpenRefer: '10002',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async clickInlineKeyboardButton(
|
||||
groupId: number,
|
||||
botAppid: number,
|
||||
buttonId: string,
|
||||
callbackData: string,
|
||||
msgSeq: number,
|
||||
): Promise<any> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const req = {
|
||||
botAppid: BigInt(botAppid),
|
||||
msgSeq: BigInt(msgSeq),
|
||||
buttonId: String(buttonId),
|
||||
callbackData: String(callbackData || ''),
|
||||
unknown7: 0,
|
||||
groupId: BigInt(groupId),
|
||||
unknown9: 1,
|
||||
};
|
||||
|
||||
const env = makeOidbEnvelope<Oidb0x112eReq>(0x112E, 1, req);
|
||||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x112e_1', protobuf_encode<OidbBase<Oidb0x112eReq>>(env));
|
||||
const result = protobuf_decode<OidbBase<Oidb0x112eResp>>(respBytes).body;
|
||||
|
||||
if (!result) {
|
||||
throw new Error('click inline keyboard button result empty');
|
||||
}
|
||||
|
||||
return {
|
||||
result: Number(result.result || 0),
|
||||
errMsg: result.errMsg || '',
|
||||
status: 0,
|
||||
promptText: result.promptText || '',
|
||||
promptType: 0,
|
||||
promptIcon: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async sendGroupSign(groupId: number): Promise<void> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const req = {
|
||||
signInInfo: {
|
||||
uin: String(this.ctx.identity.uin),
|
||||
groupId: String(groupId),
|
||||
version: '9.0.90',
|
||||
},
|
||||
};
|
||||
|
||||
const env = makeOidbEnvelope<Oidb0xeb7Req>(0xEB7, 1, req);
|
||||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0xEB7_1', protobuf_encode<OidbBase<Oidb0xeb7Req>>(env));
|
||||
protobuf_decode<OidbBase<Oidb0xeb7Resp>>(respBytes);
|
||||
}
|
||||
}
|
||||
@@ -87,11 +87,8 @@ export interface BridgeInterface {
|
||||
// - setAvatar / setGroupAvatar / fetchCustomFace
|
||||
// - getLike / getUnidirectionalFriendList
|
||||
|
||||
// ─── Misc ───
|
||||
translateEn2Zh(words: string[]): Promise<any>;
|
||||
getMiniAppArk(type: string, title: string, desc: string, picUrl: string, jumpUrl: string): Promise<any>;
|
||||
clickInlineKeyboardButton(groupId: number, botAppid: number, buttonId: string, callbackData: string, msgSeq: number): Promise<any>;
|
||||
sendGroupSign(groupId: number): Promise<any>;
|
||||
// ─── Misc (moved to apis.misc) ───
|
||||
// - translateEn2Zh / getMiniAppArk / clickInlineKeyboardButton / sendGroupSign
|
||||
|
||||
// ─── Tier-2 napcat parity extras ───
|
||||
setGroupTodo(groupId: number, msgSeq: bigint | number | string): Promise<void>;
|
||||
|
||||
@@ -35,12 +35,7 @@ import type { GroupFilesResult } from './apis/group-file';
|
||||
// were absorbed into MessageApi back in commit 1.
|
||||
// actions/interaction.ts removed — sendPoke/sendLike/setReaction/
|
||||
// getEmojiLikes moved to apis/interaction.ts::InteractionApi.
|
||||
import {
|
||||
clickInlineKeyboardButton as clickInlineKeyboardButton_,
|
||||
getMiniAppArk as getMiniAppArk_,
|
||||
sendGroupSign as sendGroupSign_,
|
||||
translateEn2Zh as translateEn2Zh_,
|
||||
} from './actions/misc';
|
||||
// actions/misc.ts removed — moved to apis/misc.ts::MiscApi.
|
||||
// actions/profile.ts removed — moved to apis/profile.ts::ProfileApi.
|
||||
// `bridge-contacts.ts` removed — its 6 functions are now methods on
|
||||
// `apis.contacts` (see `apis/contacts.ts::ContactsApi`).
|
||||
@@ -344,18 +339,8 @@ export class Bridge implements BridgeInterface {
|
||||
async getCookiesStr(domain: string): Promise<string> { return getCookiesStr_(this, domain); }
|
||||
async getCsrfToken(): Promise<number> { return getCsrfToken_(this); }
|
||||
async getCredentials(domain: string) { return getCredentials_(this, domain); }
|
||||
async translateEn2Zh(words: string[]) {
|
||||
return translateEn2Zh_(this, words);
|
||||
}
|
||||
async getMiniAppArk(type: string, title: string, desc: string, picUrl: string, jumpUrl: string) {
|
||||
return getMiniAppArk_(this, type, title, desc, picUrl, jumpUrl);
|
||||
}
|
||||
async clickInlineKeyboardButton(groupId: number, botAppid: number, buttonId: string, callbackData: string, msgSeq: number) {
|
||||
return clickInlineKeyboardButton_(this, groupId, botAppid, buttonId, callbackData, msgSeq);
|
||||
}
|
||||
async sendGroupSign(groupId: number) {
|
||||
return sendGroupSign_(this, groupId);
|
||||
}
|
||||
// Misc methods (translateEn2Zh / getMiniAppArk /
|
||||
// clickInlineKeyboardButton / sendGroupSign) moved to apis.misc.
|
||||
|
||||
// --- Tier-2 napcat-parity extras (group todo, stranger status, AI voice) ---
|
||||
|
||||
|
||||
@@ -975,7 +975,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const translated = await ctx.bridge.translateEn2Zh(words);
|
||||
const translated = await ctx.bridge.apis.misc.translateEn2Zh(words);
|
||||
return okResponse({ words: translated });
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
@@ -999,7 +999,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const data = await ctx.bridge.getMiniAppArk(
|
||||
const data = await ctx.bridge.apis.misc.getMiniAppArk(
|
||||
String(type),
|
||||
String(title),
|
||||
String(desc),
|
||||
@@ -1025,7 +1025,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
const data = await ctx.bridge.clickInlineKeyboardButton(
|
||||
const data = await ctx.bridge.apis.misc.clickInlineKeyboardButton(
|
||||
groupId,
|
||||
botAppid,
|
||||
String(buttonId),
|
||||
@@ -1047,7 +1047,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
|
||||
try {
|
||||
await ctx.bridge.sendGroupSign(groupId);
|
||||
await ctx.bridge.apis.misc.sendGroupSign(groupId);
|
||||
return okResponse({});
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, String(e));
|
||||
|
||||
@@ -23,10 +23,10 @@ vi.mock('../../src/bridge/bridge-oidb', async () => {
|
||||
});
|
||||
|
||||
import * as oidb from '../../src/bridge/bridge-oidb';
|
||||
import * as misc from '../../src/bridge/actions/misc';
|
||||
import { MiscApi } from '../../src/bridge/apis/misc';
|
||||
import { mockBridge } from './_helpers';
|
||||
|
||||
describe('actions/misc', () => {
|
||||
describe('apis/misc', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(oidb.runOidb).mockReset();
|
||||
vi.mocked(oidb.runOidb).mockResolvedValue(new Uint8Array());
|
||||
@@ -40,7 +40,7 @@ describe('actions/misc', () => {
|
||||
body: { translateResp: { dstWords: ['你好', '世界'] } } as any,
|
||||
}),
|
||||
);
|
||||
const out = await misc.translateEn2Zh(bridge as any, ['hello', 'world']);
|
||||
const out = await new MiscApi(bridge as any).translateEn2Zh( ['hello', 'world']);
|
||||
expect(out).toEqual(['你好', '世界']);
|
||||
});
|
||||
|
||||
@@ -49,14 +49,14 @@ describe('actions/misc', () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<Oidb0x990Resp>>({ body: {} }),
|
||||
);
|
||||
await expect(misc.translateEn2Zh(bridge as any, ['hello']))
|
||||
await expect(new MiscApi(bridge as any).translateEn2Zh( ['hello']))
|
||||
.rejects.toThrow(/empty/);
|
||||
});
|
||||
|
||||
it('getMiniAppArk rejects unsupported types', async () => {
|
||||
const bridge = mockBridge();
|
||||
await expect(
|
||||
misc.getMiniAppArk(bridge as any, 'youtube', 't', 'd', 'p', 'u'),
|
||||
new MiscApi(bridge as any).getMiniAppArk( 'youtube', 't', 'd', 'p', 'u'),
|
||||
).rejects.toThrow(/unsupported type/);
|
||||
});
|
||||
|
||||
@@ -75,7 +75,7 @@ describe('actions/misc', () => {
|
||||
});
|
||||
// With null responseData, the action should throw on decode — that
|
||||
// still proves the right serviceCmd was used.
|
||||
await expect(misc.getMiniAppArk(bridge as any, 'bili', 't', 'd', 'p', 'u')).rejects.toThrow();
|
||||
await expect(new MiscApi(bridge as any).getMiniAppArk( 'bili', 't', 'd', 'p', 'u')).rejects.toThrow();
|
||||
expect(bridge.sendRawPacket).toHaveBeenCalledOnce();
|
||||
expect(bridge.sendRawPacket.mock.calls[0]![0])
|
||||
.toBe('LightAppSvc.mini_app_share.AdaptShareInfo');
|
||||
@@ -88,13 +88,13 @@ describe('actions/misc', () => {
|
||||
body: { result: 1, errMsg: 'ok', promptText: 'hi' } as any,
|
||||
}),
|
||||
);
|
||||
const out = await misc.clickInlineKeyboardButton(bridge as any, 1, 2, 'btn', 'data', 100);
|
||||
const out = await new MiscApi(bridge as any).clickInlineKeyboardButton( 1, 2, 'btn', 'data', 100);
|
||||
expect(out).toMatchObject({ result: 1, errMsg: 'ok', promptText: 'hi', status: 0 });
|
||||
});
|
||||
|
||||
it('sendGroupSign dispatches to 0xEB7_1', async () => {
|
||||
const bridge = mockBridge();
|
||||
await misc.sendGroupSign(bridge as any, 12345);
|
||||
await new MiscApi(bridge as any).sendGroupSign( 12345);
|
||||
const [, cmd] = vi.mocked(oidb.runOidb).mock.calls[0]!;
|
||||
expect(cmd).toBe('OidbSvcTrpcTcp.0xEB7_1');
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user