feat(onebot): add get_rkey_server (NapCat server-shape rkey)
Reshape the download-rkey list (type 10=private, 20=group, same 0x9067
source) into NapCat's GetRkeyServer shape {private_rkey, group_rkey,
expired_time, name}, expired_time = now + min present ttl. get_rkey /
nc_get_rkey already existed (raw list) and are left unchanged.
This commit is contained in:
2 files changed
+58
No files matched your search
@@ -1215,6 +1215,33 @@ export const actions = [
|
|||||||
: failedResponse(RETCODE.ACTION_FAILED, 'not implemented'),
|
: failedResponse(RETCODE.ACTION_FAILED, 'not implemented'),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
// get_rkey_server — 把下载 rkey 列表(type 10=私聊 / 20=群聊,同 0x9067 来源)
|
||||||
|
// 收敛成 NapCat 的 server 形状。expired_time = now + 在场 rkey ttl 的最小值。
|
||||||
|
defineAction({
|
||||||
|
name: 'get_rkey_server',
|
||||||
|
summary: '获取 rkey 服务器信息',
|
||||||
|
readOnly: true,
|
||||||
|
params: {},
|
||||||
|
run: async (_p, ctx) => {
|
||||||
|
if (!ctx.getDownloadRKeys) return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
|
||||||
|
const rkeys = await ctx.getDownloadRKeys();
|
||||||
|
const pick = (type: number) =>
|
||||||
|
rkeys.find((r) => (r as { type?: number }).type === type) as
|
||||||
|
{ rkey?: string; ttl?: number } | undefined;
|
||||||
|
const priv = pick(10);
|
||||||
|
const group = pick(20);
|
||||||
|
const ttls = [priv?.ttl, group?.ttl].filter((t): t is number => typeof t === 'number' && t > 0);
|
||||||
|
const minTtl = ttls.length ? Math.min(...ttls) : 0;
|
||||||
|
const data: JsonObject = {
|
||||||
|
expired_time: Math.floor(Date.now() / 1000) + minTtl,
|
||||||
|
name: 'SnowLuma',
|
||||||
|
};
|
||||||
|
if (priv?.rkey) data.private_rkey = priv.rkey;
|
||||||
|
if (group?.rkey) data.group_rkey = group.rkey;
|
||||||
|
return okResponse(data);
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
|
||||||
defineAction({
|
defineAction({
|
||||||
name: ['ocr_image', '.ocr_image'],
|
name: ['ocr_image', '.ocr_image'],
|
||||||
summary: 'OCR 图片(未实现)',
|
summary: 'OCR 图片(未实现)',
|
||||||
|
|||||||
@@ -714,3 +714,34 @@ describe('extended-actions / rename_group_file', () => {
|
|||||||
expect(res).toMatchObject({ status: 'failed', retcode: 1200, wording: 'rename rejected' });
|
expect(res).toMatchObject({ status: 'failed', retcode: 1200, wording: 'rename rejected' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ─── Wave 2: get_rkey_server ───
|
||||||
|
|
||||||
|
describe('extended-actions / get_rkey_server', () => {
|
||||||
|
it('reshapes download rkeys into the NapCat server shape (private=10, group=20)', async () => {
|
||||||
|
const nowSec = Math.floor(Date.now() / 1000);
|
||||||
|
const getDownloadRKeys = vi.fn(async () => [
|
||||||
|
{ rkey: '&rkey=PRIV', type: 10, ttl: 3600, create_time: 100 },
|
||||||
|
{ rkey: '&rkey=GRP', type: 20, ttl: 7200, create_time: 200 },
|
||||||
|
]);
|
||||||
|
const res = await makeHandler(fakeCtx(fakeBridge(), { getDownloadRKeys })).handle('get_rkey_server', {});
|
||||||
|
expect(res.status).toBe('ok');
|
||||||
|
const d = res.data as { private_rkey?: string; group_rkey?: string; expired_time: number; name: string };
|
||||||
|
expect(d.private_rkey).toBe('&rkey=PRIV');
|
||||||
|
expect(d.group_rkey).toBe('&rkey=GRP');
|
||||||
|
expect(d.name).toBe('SnowLuma');
|
||||||
|
// expiry = now + min(ttl) = now + 3600 (allow a 1s clock tick)
|
||||||
|
expect(d.expired_time).toBeGreaterThanOrEqual(nowSec + 3600);
|
||||||
|
expect(d.expired_time).toBeLessThanOrEqual(nowSec + 3601);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('leaves a missing scope undefined', async () => {
|
||||||
|
const getDownloadRKeys = vi.fn(async () => [
|
||||||
|
{ rkey: '&rkey=PRIV', type: 10, ttl: 3600, create_time: 100 },
|
||||||
|
]);
|
||||||
|
const res = await makeHandler(fakeCtx(fakeBridge(), { getDownloadRKeys })).handle('get_rkey_server', {});
|
||||||
|
const d = res.data as { private_rkey?: string; group_rkey?: string };
|
||||||
|
expect(d.private_rkey).toBe('&rkey=PRIV');
|
||||||
|
expect(d.group_rkey).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user