feat(onebot): add Qzone profile read and update actions (#586)
Add actions to read a space name and description and update the current account profile. Preserve omitted fields and support explicit empty values, with matching SDK types, MCP discovery, documentation, and tests.
This commit is contained in:
16 files changed
+862
-3
No files matched your search
@@ -36,6 +36,69 @@ const qzoneCommentProperties = {
|
||||
const qzoneCommentRequired = ['uin', 'name', 'time', 'content', 'comment_id'];
|
||||
|
||||
export const actions = [
|
||||
defineAction({
|
||||
name: 'get_qzone_profile',
|
||||
examples: [
|
||||
{
|
||||
summary: '获取当前账号的空间名称和说明',
|
||||
params: {},
|
||||
},
|
||||
{
|
||||
summary: '获取指定账号的空间名称和说明',
|
||||
params: { target_uin: 10001 },
|
||||
notes: '需要有该空间的访问权限。',
|
||||
},
|
||||
],
|
||||
readOnly: true,
|
||||
summary: '获取 QQ 空间名称和说明(默认当前账号)',
|
||||
returns: '空间名称和说明。',
|
||||
returnsSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: '空间名称' },
|
||||
description: { type: 'string', description: '空间说明' },
|
||||
},
|
||||
required: ['name', 'description'],
|
||||
},
|
||||
params: {
|
||||
target_uin: f.userId().describe('目标 QQ 号,省略则取机器人自己').optional(),
|
||||
},
|
||||
run: async (p, ctx) => okResponse(await ctx.bridge.apis.qzone.getProfile(p.target_uin)),
|
||||
}),
|
||||
|
||||
defineAction({
|
||||
name: 'set_qzone_profile',
|
||||
examples: [
|
||||
{
|
||||
summary: '修改当前账号的空间名称和说明',
|
||||
params: { name: '我的空间', description: '记录日常' },
|
||||
notes: '仅修改提供的字段;空字符串表示清空对应字段。',
|
||||
},
|
||||
{
|
||||
summary: '清空当前账号的空间说明',
|
||||
params: { description: '' },
|
||||
},
|
||||
],
|
||||
readOnly: false,
|
||||
summary: '修改当前账号的 QQ 空间名称或说明',
|
||||
returnsSchema: {
|
||||
type: 'null',
|
||||
description: '成功时 data 为 null;失败另见响应的 status、retcode 和 wording',
|
||||
},
|
||||
params: {
|
||||
name: f.string().describe('新的空间名称,空字符串表示清空,省略则保留').optional(),
|
||||
description: f
|
||||
.string()
|
||||
.describe('新的空间说明,空字符串表示清空,省略则保留')
|
||||
.optional(),
|
||||
},
|
||||
rules: (r) => [r.atLeastOneOf('name', 'description')],
|
||||
run: async (p, ctx) => {
|
||||
await ctx.bridge.apis.qzone.setProfile(p);
|
||||
return okResponse(null);
|
||||
},
|
||||
}),
|
||||
|
||||
// get_qzone_msg_list — 获取 QQ 空间说说列表。无 go-cqhttp 标准,自定义命名。
|
||||
// 默认取机器人自己的空间;传 target_uin 可查看指定账号(需有权限)。
|
||||
// 复用 qzone.qq.com 的 cookie/g_tk 基建(与群相册同源),纯 web,无 trpc。
|
||||
|
||||
@@ -10,6 +10,110 @@ function makeHandler(qzone: Record<string, unknown>): ApiHandler {
|
||||
}
|
||||
|
||||
describe('qzone actions', () => {
|
||||
it.each([undefined, '20002'])(
|
||||
'get_qzone_profile returns the requested profile and normalizes owner %s',
|
||||
async (targetUin) => {
|
||||
const profile = { name: 'My space', description: 'Notes' };
|
||||
const getProfile = vi.fn().mockResolvedValue(profile);
|
||||
const params = targetUin === undefined ? {} : { target_uin: targetUin };
|
||||
|
||||
expect(
|
||||
await makeHandler({ getProfile }).handle('get_qzone_profile', params),
|
||||
).toMatchObject({
|
||||
status: 'ok',
|
||||
retcode: 0,
|
||||
data: profile,
|
||||
});
|
||||
expect(getProfile).toHaveBeenCalledWith(targetUin === undefined ? undefined : 20002);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([0, 'bad'])(
|
||||
'get_qzone_profile rejects invalid owners before fetching (%s)',
|
||||
async (targetUin) => {
|
||||
const getProfile = vi.fn();
|
||||
|
||||
expect(
|
||||
await makeHandler({ getProfile }).handle('get_qzone_profile', {
|
||||
target_uin: targetUin,
|
||||
}),
|
||||
).toMatchObject({
|
||||
status: 'failed',
|
||||
retcode: 1400,
|
||||
});
|
||||
expect(getProfile).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it.each(['get_qzone_profile', 'set_qzone_profile'])(
|
||||
'%s exposes upstream errors',
|
||||
async (action) => {
|
||||
const failure = new Error('qzone profile failed: code=-3000 need login');
|
||||
const method = vi.fn().mockRejectedValue(failure);
|
||||
const handler = makeHandler({ getProfile: method, setProfile: method });
|
||||
const params = action === 'set_qzone_profile' ? { name: 'New name' } : {};
|
||||
const response = await handler.handle(action, params);
|
||||
|
||||
expect(response).toMatchObject({ status: 'failed', retcode: 100 });
|
||||
expect(response.wording).toContain('code=-3000 need login');
|
||||
},
|
||||
);
|
||||
|
||||
it.each([{ name: 'New name' }, { description: '' }, { name: '', description: 'New notes' }])(
|
||||
'set_qzone_profile forwards supplied fields and preserves explicit empty values (%j)',
|
||||
async (params) => {
|
||||
const setProfile = vi.fn().mockResolvedValue(undefined);
|
||||
|
||||
expect(
|
||||
await makeHandler({ setProfile }).handle('set_qzone_profile', params),
|
||||
).toMatchObject({
|
||||
status: 'ok',
|
||||
retcode: 0,
|
||||
data: null,
|
||||
});
|
||||
expect(setProfile).toHaveBeenCalledWith({
|
||||
name: params.name,
|
||||
description: params.description,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([{}, { target_uin: 20002 }, { name: {} }, { description: [] }])(
|
||||
'set_qzone_profile rejects missing or invalid updates before writing (%j)',
|
||||
async (params) => {
|
||||
const setProfile = vi.fn();
|
||||
|
||||
expect(
|
||||
await makeHandler({ setProfile }).handle('set_qzone_profile', params),
|
||||
).toMatchObject({
|
||||
status: 'failed',
|
||||
retcode: 1400,
|
||||
});
|
||||
expect(setProfile).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it('set_qzone_profile cannot select a different account', async () => {
|
||||
const setProfile = vi.fn().mockResolvedValue(undefined);
|
||||
|
||||
await makeHandler({ setProfile }).handle('set_qzone_profile', {
|
||||
name: 'New name',
|
||||
target_uin: 20002,
|
||||
});
|
||||
expect(setProfile).toHaveBeenCalledWith({ name: 'New name', description: undefined });
|
||||
});
|
||||
|
||||
it('classifies profile reads and writes separately', () => {
|
||||
expect(
|
||||
actions.find((action) => action.names.includes('get_qzone_profile'))?.describe()
|
||||
.readOnly,
|
||||
).toBe(true);
|
||||
expect(
|
||||
actions.find((action) => action.names.includes('set_qzone_profile'))?.describe()
|
||||
.readOnly,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('get_qzone_feeds retains defaults and forwards optional continuation without stripping result fields', async () => {
|
||||
const result = { feeds: [], has_more: true, next_cursor: 'next' };
|
||||
const getFeeds = vi.fn().mockResolvedValue(result);
|
||||
|
||||
Reference in new issue
Block a user