feat(onebot): add public Qzone feeds and interactions (#587)
Add public Qzone discovery, pagination, and interactions with SDK and MCP support.
This commit is contained in:
20 files changed
+4997
-6
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import type { JsonObject } from '@snowluma/common/json';
|
||||
import {
|
||||
commentQzoneMsg,
|
||||
deleteQzoneMsg,
|
||||
@@ -23,11 +24,29 @@ import {
|
||||
type QzoneUpdateRightResult,
|
||||
type QzoneUploadImageResult,
|
||||
} from '@snowluma/protocol/web/qzone';
|
||||
import {
|
||||
getQzonePublicCapabilities,
|
||||
getQzonePublicFeeds,
|
||||
getQzonePublicFeedDetail,
|
||||
getQzonePublicComments,
|
||||
getQzonePublicReplies,
|
||||
setQzonePublicUpvote,
|
||||
setQzonePublicLike,
|
||||
setQzonePublicCommentLike,
|
||||
commentQzonePublicFeed,
|
||||
replyQzonePublicFeed,
|
||||
deleteQzonePublicComment,
|
||||
deleteQzonePublicReply,
|
||||
setQzonePublicFollow,
|
||||
forwardQzonePublicFeed,
|
||||
type QzonePublicContent,
|
||||
type QzonePublicFeedsPage,
|
||||
} from '@snowluma/protocol/web/qzone-public';
|
||||
import type { BridgeContext } from '../bridge-context';
|
||||
|
||||
/**
|
||||
* Personal QQ-Zone (个人空间) web API: 说说 (feed) read/write, likes,
|
||||
* comments, image upload — all over the cookie-backed qzone.qq.com CGIs,
|
||||
* Personal QQ-Zone (个人空间) web API: 说说, public recommendations, likes,
|
||||
* comments and image upload over the cookie-backed Qzone web interfaces,
|
||||
* reusing the same `getCookies('qzone.qq.com')` plumbing as GroupAlbumApi.
|
||||
* Distinct from the group-album surface, which lives on GroupAlbumApi.
|
||||
*/
|
||||
@@ -44,6 +63,105 @@ export class QzoneApi {
|
||||
await setQzoneProfile(cookieObject, this.ctx.identity.uin, profile);
|
||||
}
|
||||
|
||||
async getPublicCapabilities(): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return getQzonePublicCapabilities(cookies, this.ctx.identity.uin);
|
||||
}
|
||||
|
||||
async getPublicFeeds(cursor?: string): Promise<QzonePublicFeedsPage> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return getQzonePublicFeeds(cookies, this.ctx.identity.uin, cursor);
|
||||
}
|
||||
|
||||
async getPublicFeedDetail(feed: JsonObject): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return getQzonePublicFeedDetail(cookies, feed);
|
||||
}
|
||||
|
||||
async getPublicComments(feed: JsonObject, cursor: string): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return getQzonePublicComments(cookies, feed, cursor);
|
||||
}
|
||||
|
||||
async getPublicReplies(
|
||||
feed: JsonObject,
|
||||
comment: JsonObject,
|
||||
cursor = '',
|
||||
pageSize = 20,
|
||||
): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return getQzonePublicReplies(cookies, feed, comment, cursor, pageSize);
|
||||
}
|
||||
|
||||
async setPublicUpvote(feed: JsonObject, enable = true): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return setQzonePublicUpvote(cookies, feed, enable);
|
||||
}
|
||||
|
||||
async setPublicLike(feed: JsonObject, enable = true): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return setQzonePublicLike(cookies, feed, enable);
|
||||
}
|
||||
|
||||
async setPublicCommentLike(
|
||||
feed: JsonObject,
|
||||
comment: JsonObject,
|
||||
enable = true,
|
||||
reply?: JsonObject,
|
||||
): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return setQzonePublicCommentLike(cookies, feed, comment, enable, reply);
|
||||
}
|
||||
|
||||
async commentPublicFeed(feed: JsonObject, content: QzonePublicContent): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
const user = {
|
||||
uin: this.ctx.identity.uin,
|
||||
uid: this.ctx.identity.selfUid ?? '',
|
||||
nick: this.ctx.identity.nickname,
|
||||
};
|
||||
return commentQzonePublicFeed(cookies, feed, user, content);
|
||||
}
|
||||
|
||||
async replyPublicFeed(
|
||||
feed: JsonObject,
|
||||
comment: JsonObject,
|
||||
content: QzonePublicContent,
|
||||
reply?: JsonObject,
|
||||
): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
const user = {
|
||||
uin: this.ctx.identity.uin,
|
||||
uid: this.ctx.identity.selfUid ?? '',
|
||||
nick: this.ctx.identity.nickname,
|
||||
};
|
||||
return replyQzonePublicFeed(cookies, feed, user, comment, content, reply);
|
||||
}
|
||||
|
||||
async deletePublicComment(feed: JsonObject, comment: JsonObject): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return deleteQzonePublicComment(cookies, feed, comment);
|
||||
}
|
||||
|
||||
async deletePublicReply(
|
||||
feed: JsonObject,
|
||||
comment: JsonObject,
|
||||
reply: JsonObject,
|
||||
): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return deleteQzonePublicReply(cookies, feed, comment, reply);
|
||||
}
|
||||
|
||||
async setPublicFollow(feed: JsonObject, enable = true): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return setQzonePublicFollow(cookies, feed, enable);
|
||||
}
|
||||
|
||||
async forwardPublicFeed(feed: JsonObject, reason = ''): Promise<JsonObject> {
|
||||
const cookies = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return forwardQzonePublicFeed(cookies, feed, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取说说列表。`targetUin` 省略时取机器人自己的空间。
|
||||
* `pos` 为起始偏移,`num` 为本页数量(服务端上限约 20/页)。
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import * as qzonePublic from '@snowluma/protocol/web/qzone-public';
|
||||
import { QzoneApi } from '../../src/bridge/apis/qzone';
|
||||
import { mockApiHub, mockBridge } from './_helpers';
|
||||
|
||||
const cookies = { p_skey: 'PSK' };
|
||||
const feed = {
|
||||
cell_common: { union_id: { feed_id: 'opaque-id' } },
|
||||
cell_feed_transfer: { future: true },
|
||||
};
|
||||
const comment = { id: 'comment-id', user: { uid: 'commenter' }, unknown: [1, 2] };
|
||||
const reply = { id: 'reply-id', user: { uid: 'recipient' } };
|
||||
const content = [{ type: 0, content: 'text', future: { enabled: true } }];
|
||||
const user = { uin: '10001', uid: 'self-uid', nick: 'self-nick' };
|
||||
const result = { feeds: [], has_more: false, next_cursor: null, unknown: { preserved: true } };
|
||||
|
||||
function makeApi() {
|
||||
const getCookies = vi.fn().mockResolvedValue(cookies);
|
||||
const bridge = mockBridge({ apis: { ...mockApiHub(), web: { getCookies } } as never });
|
||||
return { api: new QzoneApi(bridge as never), bridge, getCookies };
|
||||
}
|
||||
|
||||
const cases = [
|
||||
{
|
||||
method: 'getQzonePublicCapabilities',
|
||||
invoke: (api: QzoneApi) => api.getPublicCapabilities(),
|
||||
args: [cookies, '10001'],
|
||||
},
|
||||
{
|
||||
method: 'getQzonePublicFeeds',
|
||||
invoke: (api: QzoneApi) => api.getPublicFeeds('cursor'),
|
||||
args: [cookies, '10001', 'cursor'],
|
||||
},
|
||||
{
|
||||
method: 'getQzonePublicFeedDetail',
|
||||
invoke: (api: QzoneApi) => api.getPublicFeedDetail(feed),
|
||||
args: [cookies, feed],
|
||||
},
|
||||
{
|
||||
method: 'getQzonePublicComments',
|
||||
invoke: (api: QzoneApi) => api.getPublicComments(feed, 'cursor'),
|
||||
args: [cookies, feed, 'cursor'],
|
||||
},
|
||||
{
|
||||
method: 'getQzonePublicReplies',
|
||||
invoke: (api: QzoneApi) => api.getPublicReplies(feed, comment, 'cursor', 5),
|
||||
args: [cookies, feed, comment, 'cursor', 5],
|
||||
},
|
||||
{
|
||||
method: 'setQzonePublicUpvote',
|
||||
invoke: (api: QzoneApi) => api.setPublicUpvote(feed, false),
|
||||
args: [cookies, feed, false],
|
||||
},
|
||||
{
|
||||
method: 'setQzonePublicLike',
|
||||
invoke: (api: QzoneApi) => api.setPublicLike(feed, false),
|
||||
args: [cookies, feed, false],
|
||||
},
|
||||
{
|
||||
method: 'setQzonePublicCommentLike',
|
||||
invoke: (api: QzoneApi) => api.setPublicCommentLike(feed, comment, false, reply),
|
||||
args: [cookies, feed, comment, false, reply],
|
||||
},
|
||||
{
|
||||
method: 'commentQzonePublicFeed',
|
||||
invoke: (api: QzoneApi) => api.commentPublicFeed(feed, content),
|
||||
args: [cookies, feed, user, content],
|
||||
},
|
||||
{
|
||||
method: 'replyQzonePublicFeed',
|
||||
invoke: (api: QzoneApi) => api.replyPublicFeed(feed, comment, content, reply),
|
||||
args: [cookies, feed, user, comment, content, reply],
|
||||
},
|
||||
{
|
||||
method: 'deleteQzonePublicComment',
|
||||
invoke: (api: QzoneApi) => api.deletePublicComment(feed, comment),
|
||||
args: [cookies, feed, comment],
|
||||
},
|
||||
{
|
||||
method: 'deleteQzonePublicReply',
|
||||
invoke: (api: QzoneApi) => api.deletePublicReply(feed, comment, reply),
|
||||
args: [cookies, feed, comment, reply],
|
||||
},
|
||||
{
|
||||
method: 'setQzonePublicFollow',
|
||||
invoke: (api: QzoneApi) => api.setPublicFollow(feed, false),
|
||||
args: [cookies, feed, false],
|
||||
},
|
||||
{
|
||||
method: 'forwardQzonePublicFeed',
|
||||
invoke: (api: QzoneApi) => api.forwardPublicFeed(feed, 'reason'),
|
||||
args: [cookies, feed, 'reason'],
|
||||
},
|
||||
] as const;
|
||||
|
||||
describe('apis/qzone public', () => {
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
it.each(cases)(
|
||||
'$method uses current-account cookies and preserves upstream objects',
|
||||
async ({ method, invoke, args }) => {
|
||||
const { api, getCookies } = makeApi();
|
||||
const request = vi.spyOn(qzonePublic, method).mockResolvedValue(result);
|
||||
|
||||
expect(await invoke(api)).toBe(result);
|
||||
expect(getCookies).toHaveBeenCalledWith('qzone.qq.com');
|
||||
expect(request).toHaveBeenCalledExactlyOnceWith(...args);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(cases)(
|
||||
'$method propagates cookie and upstream failures without replay',
|
||||
async ({ method, invoke }) => {
|
||||
const { api, getCookies } = makeApi();
|
||||
const request = vi
|
||||
.spyOn(qzonePublic, method)
|
||||
.mockRejectedValue(new Error('upstream denied'));
|
||||
|
||||
await expect(invoke(api)).rejects.toThrow('upstream denied');
|
||||
getCookies.mockRejectedValueOnce(new Error('cookie failure'));
|
||||
await expect(invoke(api)).rejects.toThrow('cookie failure');
|
||||
expect(request).toHaveBeenCalledTimes(1);
|
||||
},
|
||||
);
|
||||
|
||||
it('keeps documented defaults and omits an optional reply target', async () => {
|
||||
const { api } = makeApi();
|
||||
const feeds = vi.spyOn(qzonePublic, 'getQzonePublicFeeds').mockResolvedValue(result);
|
||||
const replies = vi.spyOn(qzonePublic, 'getQzonePublicReplies').mockResolvedValue(result);
|
||||
const upvote = vi.spyOn(qzonePublic, 'setQzonePublicUpvote').mockResolvedValue(result);
|
||||
const like = vi.spyOn(qzonePublic, 'setQzonePublicLike').mockResolvedValue(result);
|
||||
const commentLike = vi
|
||||
.spyOn(qzonePublic, 'setQzonePublicCommentLike')
|
||||
.mockResolvedValue(result);
|
||||
const follow = vi.spyOn(qzonePublic, 'setQzonePublicFollow').mockResolvedValue(result);
|
||||
const forward = vi.spyOn(qzonePublic, 'forwardQzonePublicFeed').mockResolvedValue(result);
|
||||
const replyRequest = vi
|
||||
.spyOn(qzonePublic, 'replyQzonePublicFeed')
|
||||
.mockResolvedValue(result);
|
||||
|
||||
await api.getPublicFeeds();
|
||||
await api.getPublicReplies(feed, comment);
|
||||
await api.setPublicUpvote(feed);
|
||||
await api.setPublicLike(feed);
|
||||
await api.setPublicCommentLike(feed, comment);
|
||||
await api.setPublicFollow(feed);
|
||||
await api.forwardPublicFeed(feed);
|
||||
await api.replyPublicFeed(feed, comment, 'text');
|
||||
|
||||
expect(feeds).toHaveBeenCalledWith(cookies, '10001', undefined);
|
||||
expect(replies).toHaveBeenCalledWith(cookies, feed, comment, '', 20);
|
||||
expect(upvote).toHaveBeenCalledWith(cookies, feed, true);
|
||||
expect(like).toHaveBeenCalledWith(cookies, feed, true);
|
||||
expect(commentLike).toHaveBeenCalledWith(cookies, feed, comment, true, undefined);
|
||||
expect(follow).toHaveBeenCalledWith(cookies, feed, true);
|
||||
expect(forward).toHaveBeenCalledWith(cookies, feed, '');
|
||||
expect(replyRequest).toHaveBeenCalledWith(cookies, feed, user, comment, 'text', undefined);
|
||||
});
|
||||
|
||||
it('binds comment authors to the current account even when its uid is unavailable', async () => {
|
||||
const { api, bridge } = makeApi();
|
||||
Object.assign(bridge.identity, { selfUid: undefined });
|
||||
const request = vi.spyOn(qzonePublic, 'commentQzonePublicFeed').mockResolvedValue(result);
|
||||
|
||||
await api.commentPublicFeed(feed, 'text');
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
cookies,
|
||||
feed,
|
||||
{ uin: '10001', uid: '', nick: 'self-nick' },
|
||||
'text',
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user