feat(qzone): comment_qzone — comment on a 说说 via web API
commentQzoneMsg POSTs a form body to taotao.qzone.qq.com's emotion_cgi_re_feeds (proxied through h5.qzone), keyed by topicId <hostUin>_<tid>__1, as the bot (uin=self) on target_uin's feed (defaults to the bot's own space). Returns the new comment id best-effort — the response field name varies (commentid/commentId) so a missing id with code 0 is still success. Throws on non-zero code/subcode; rejects empty tid/content pre-flight. WRITE — rate-limit (风控). topicId shape + comment-id field are extrapolated from community impls, pending a live capture (labeled in the helper comment). - core: QzoneApi.comment; onebot: comment_qzone action; regen catalog. - tests: re_feeds form lock (topicId/uin/hostUin/content/format=fs), commentid + camelCase commentId fallback + missing-id success, empty tid/content preflight, non-zero code throw, bridge owner-defaulting.
This commit is contained in:
6 files changed
+253
-2
No files matched your search
@@ -1,9 +1,11 @@
|
||||
import {
|
||||
commentQzoneMsg,
|
||||
deleteQzoneMsg,
|
||||
getQzoneFeeds,
|
||||
getQzoneMsgList,
|
||||
publishQzoneMsg,
|
||||
setQzoneLike,
|
||||
type QzoneCommentResult,
|
||||
type QzoneFeedsResult,
|
||||
type QzoneMsgListResult,
|
||||
type QzonePublishResult,
|
||||
@@ -60,4 +62,15 @@ export class QzoneApi {
|
||||
const cookieObject = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
await setQzoneLike(cookieObject, selfUin, owner, tid, like, abstime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 评论一条说说。`targetUin` 为说说所属 QQ 号(省略=机器人自己空间);
|
||||
* 始终以机器人身份发表评论。返回新评论 id(尽力而为)。
|
||||
*/
|
||||
async comment(tid: string, content: string, targetUin: number | undefined): Promise<QzoneCommentResult> {
|
||||
const selfUin = this.ctx.identity.uin;
|
||||
const owner = targetUin && targetUin > 0 ? targetUin.toString() : selfUin;
|
||||
const cookieObject = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return commentQzoneMsg(cookieObject, selfUin, owner, tid, content);
|
||||
}
|
||||
}
|
||||
@@ -85,4 +85,22 @@ describe('apis/qzone', () => {
|
||||
// abstime defaults to 0 when omitted
|
||||
expect(likeSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', '20002', 'TIDX', false, 0);
|
||||
});
|
||||
|
||||
it('comment defaults the feed owner to self and posts as self', async () => {
|
||||
const getCookies = vi.fn(async () => ({ p_skey: 'PSK' }));
|
||||
const bridge = mockBridge({ apis: { ...mockApiHub(), web: { getCookies } } as never });
|
||||
const cmtSpy = vi.spyOn(qzoneWeb, 'commentQzoneMsg').mockResolvedValue({ comment_id: '1' });
|
||||
const out = await new QzoneApi(bridge as never).comment('TIDX', 'nice', undefined);
|
||||
// selfUin='10001' commenter; owner defaults to self
|
||||
expect(cmtSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', '10001', 'TIDX', 'nice');
|
||||
expect(out).toEqual({ comment_id: '1' });
|
||||
});
|
||||
|
||||
it('comment targets a friend\'s feed (owner = target_uin), commenter stays self', async () => {
|
||||
const getCookies = vi.fn(async () => ({ p_skey: 'PSK' }));
|
||||
const bridge = mockBridge({ apis: { ...mockApiHub(), web: { getCookies } } as never });
|
||||
const cmtSpy = vi.spyOn(qzoneWeb, 'commentQzoneMsg').mockResolvedValue({ comment_id: '2' });
|
||||
await new QzoneApi(bridge as never).comment('TIDX', 'nice', 20002);
|
||||
expect(cmtSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', '20002', 'TIDX', 'nice');
|
||||
});
|
||||
});
|
||||
@@ -532,6 +532,71 @@ export const ACTIONS: CatalogAction[] = [
|
||||
},
|
||||
"category": "扩展"
|
||||
},
|
||||
{
|
||||
"name": "comment_qzone",
|
||||
"aliases": [],
|
||||
"summary": "评论一条说说(QQ 空间)",
|
||||
"readOnly": false,
|
||||
"params": [
|
||||
{
|
||||
"name": "tid",
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"desc": "说说 tid"
|
||||
},
|
||||
{
|
||||
"name": "content",
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"desc": "评论内容"
|
||||
},
|
||||
{
|
||||
"name": "target_uin",
|
||||
"type": "uint",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
"desc": "说说所属 QQ 号,省略则为机器人自己"
|
||||
}
|
||||
],
|
||||
"invariants": [],
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"tid": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"description": "说说 tid"
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"description": "评论内容"
|
||||
},
|
||||
"target_uin": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"description": "说说所属 QQ 号,省略则为机器人自己"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"tid",
|
||||
"content"
|
||||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"category": "空间"
|
||||
},
|
||||
{
|
||||
"name": "complete_group_todo",
|
||||
"aliases": [],
|
||||
@@ -6893,6 +6958,6 @@ export const CATEGORIES: CatalogCategory[] = [
|
||||
},
|
||||
{
|
||||
"category": "空间",
|
||||
"count": 6
|
||||
"count": 7
|
||||
}
|
||||
];
|
||||
@@ -128,6 +128,27 @@ export const actions = [
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
// comment_qzone — 评论一条说说。target_uin=说说所属 QQ 号(省略=机器人自己空间)。
|
||||
// 写操作;高频评论会被 Qzone 风控,调用方需限流。
|
||||
defineAction({
|
||||
name: 'comment_qzone',
|
||||
summary: '评论一条说说(QQ 空间)',
|
||||
params: {
|
||||
tid: f.string({ allowEmpty: false }).describe('说说 tid'),
|
||||
content: f.string({ allowEmpty: false }).describe('评论内容'),
|
||||
target_uin: f.uint().describe('说说所属 QQ 号,省略则为机器人自己').optional(),
|
||||
},
|
||||
run: async (p, ctx) => {
|
||||
try {
|
||||
const res = await ctx.bridge.apis.qzone.comment(p.tid, p.content, p.target_uin);
|
||||
return okResponse(res as unknown as JsonValue);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'failed to comment qzone msg';
|
||||
return failedResponse(RETCODE.INTERNAL_ERROR, message);
|
||||
}
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
@@ -545,3 +545,87 @@ export async function setQzoneLike(
|
||||
throw new Error(`qzone ${verb} failed: subcode=${data.subcode} ${data.message ?? ''}`.trim());
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────── 评论说说 (comment) — emotion_cgi_re_feeds ───────────────
|
||||
// Posts a comment on a 说说 owned by `hostUin`, as the bot (`selfUin`). Same
|
||||
// form-POST mechanics as publish/delete. The `topicId` keys the target feed
|
||||
// as `<hostUin>_<tid>__1`. Success is `code 0` (throw on non-zero code/
|
||||
// subcode); the new comment id is returned best-effort (the response field
|
||||
// name varies — commentid/commentId — so a missing id is NOT a failure when
|
||||
// code is 0). WRITE OP — rate-limit. topicId shape + comment-id field are
|
||||
// EXTRAPOLATED from community impls, pending a live capture.
|
||||
|
||||
interface RawCommentResponse {
|
||||
code?: number;
|
||||
subcode?: number;
|
||||
message?: string;
|
||||
commentid?: string | number;
|
||||
commentId?: string | number;
|
||||
}
|
||||
|
||||
/** Result of commenting on a 说说. */
|
||||
export interface QzoneCommentResult {
|
||||
[key: string]: JsonValue;
|
||||
/** New comment id when the response carries one ('' if absent). */
|
||||
comment_id: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Comment on a 说说 (`tid`, owned by `hostUin`) as the bot (`selfUin`) via
|
||||
* taotao.qzone.qq.com's emotion_cgi_re_feeds CGI (proxied through h5.qzone).
|
||||
* Resolves with the new comment id (best-effort) on success; THROWS on a
|
||||
* transport failure or a non-zero Qzone `code`/`subcode` (e.g. comments
|
||||
* disabled, no permission, or auth failure).
|
||||
*/
|
||||
export async function commentQzoneMsg(
|
||||
cookieObject: Record<string, string>,
|
||||
selfUin: string,
|
||||
hostUin: string,
|
||||
tid: string,
|
||||
content: string,
|
||||
): Promise<QzoneCommentResult> {
|
||||
if (!cookieObject || typeof cookieObject !== 'object') {
|
||||
throw new Error('cookieObject is required');
|
||||
}
|
||||
if (!tid) {
|
||||
throw new Error('tid is required');
|
||||
}
|
||||
if (!content) {
|
||||
throw new Error('content is required');
|
||||
}
|
||||
|
||||
const bkn = getBknFromCookie(cookieObject);
|
||||
const url = `https://h5.qzone.qq.com/proxy/domain/taotao.qzone.qq.com/cgi-bin/emotion_cgi_re_feeds?g_tk=${bkn}`;
|
||||
const body = new URLSearchParams({
|
||||
qzreferrer: `https://user.qzone.qq.com/${hostUin}`,
|
||||
inCharset: 'utf-8',
|
||||
outCharset: 'utf-8',
|
||||
hostUin,
|
||||
format: 'fs',
|
||||
ref: 'feeds',
|
||||
topicId: `${hostUin}_${tid}__1`,
|
||||
uin: selfUin,
|
||||
content,
|
||||
plat: 'qzone',
|
||||
source: 'ic',
|
||||
platformid: '52',
|
||||
}).toString();
|
||||
|
||||
const text = await RequestUtil.HttpGetText(url, 'POST', body, {
|
||||
Cookie: cookieToString(cookieObject),
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
});
|
||||
const data = parseQzoneJson<RawCommentResponse>(text);
|
||||
|
||||
if (typeof data.code === 'number' && data.code !== 0) {
|
||||
log.warn('commentQzoneMsg: non-zero code (host=%s tid=%s) code=%d msg=%s', hostUin, tid, data.code, data.message);
|
||||
throw new Error(`qzone comment failed: code=${data.code} ${data.message ?? ''}`.trim());
|
||||
}
|
||||
if (typeof data.subcode === 'number' && data.subcode !== 0) {
|
||||
log.warn('commentQzoneMsg: non-zero subcode (host=%s tid=%s) subcode=%d msg=%s', hostUin, tid, data.subcode, data.message);
|
||||
throw new Error(`qzone comment failed: subcode=${data.subcode} ${data.message ?? ''}`.trim());
|
||||
}
|
||||
|
||||
const commentId = data.commentid ?? data.commentId;
|
||||
return { comment_id: commentId !== undefined ? String(commentId) : '' };
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest';
|
||||
import { deleteQzoneMsg, getQzoneFeeds, getQzoneMsgList, mapFeeds, mapMsgList, parseQzoneJson, publishQzoneMsg, setQzoneLike } from '@snowluma/protocol/web/qzone';
|
||||
import { commentQzoneMsg, deleteQzoneMsg, getQzoneFeeds, getQzoneMsgList, mapFeeds, mapMsgList, parseQzoneJson, publishQzoneMsg, setQzoneLike } from '@snowluma/protocol/web/qzone';
|
||||
import { RequestUtil } from '@snowluma/protocol/web/request-util';
|
||||
|
||||
// The 说说 list comes from taotao.qzone.qq.com's emotion_cgi_msglist_v6 CGI,
|
||||
@@ -351,3 +351,53 @@ describe('qzone / setQzoneLike (HTTP layer)', () => {
|
||||
await expect(setQzoneLike(cookies, '10000', '20002', 'TIDX', false)).rejects.toThrow('unlike failed: subcode=-7');
|
||||
});
|
||||
});
|
||||
|
||||
describe('qzone / commentQzoneMsg (HTTP layer)', () => {
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
it('POSTs to re_feeds with the topicId/uin/hostUin/content and returns the comment id', async () => {
|
||||
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"commentid":987}');
|
||||
|
||||
const out = await commentQzoneMsg(cookies, '10000', '20002', 'TIDX', '说得好 & 顶');
|
||||
|
||||
expect(out).toEqual({ comment_id: '987' });
|
||||
|
||||
const [url, method, body, headers] = spy.mock.calls[0]!;
|
||||
expect(method).toBe('POST');
|
||||
expect(url).toBe(
|
||||
`https://h5.qzone.qq.com/proxy/domain/taotao.qzone.qq.com/cgi-bin/emotion_cgi_re_feeds?g_tk=${expectedGtk}`,
|
||||
);
|
||||
const h = headers as Record<string, string>;
|
||||
expect(h['Content-Type']).toBe('application/x-www-form-urlencoded');
|
||||
expect(h.Cookie).toContain('p_skey=PSK');
|
||||
const form = new URLSearchParams(body as string);
|
||||
// topicId keys the target feed as <hostUin>_<tid>__1; uin=commenter(self)
|
||||
expect(form.get('topicId')).toBe('20002_TIDX__1');
|
||||
expect(form.get('uin')).toBe('10000');
|
||||
expect(form.get('hostUin')).toBe('20002');
|
||||
expect(form.get('content')).toBe('说得好 & 顶');
|
||||
expect(form.get('format')).toBe('fs');
|
||||
});
|
||||
|
||||
it('resolves with empty comment_id on success when the response carries no id (field name varies)', async () => {
|
||||
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0}');
|
||||
await expect(commentQzoneMsg(cookies, '10000', '20002', 'TIDX', 'hi')).resolves.toEqual({ comment_id: '' });
|
||||
});
|
||||
|
||||
it('reads the commentId (camelCase) fallback field', async () => {
|
||||
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"commentId":"abc"}');
|
||||
await expect(commentQzoneMsg(cookies, '10000', '20002', 'TIDX', 'hi')).resolves.toEqual({ comment_id: 'abc' });
|
||||
});
|
||||
|
||||
it('rejects empty tid or content before any request', async () => {
|
||||
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
|
||||
await expect(commentQzoneMsg(cookies, '10000', '20002', '', 'hi')).rejects.toThrow('tid is required');
|
||||
await expect(commentQzoneMsg(cookies, '10000', '20002', 'TIDX', '')).rejects.toThrow('content is required');
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws on a non-zero code (comments disabled / no permission)', async () => {
|
||||
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":-3000,"message":"forbidden"}');
|
||||
await expect(commentQzoneMsg(cookies, '10000', '20002', 'TIDX', 'hi')).rejects.toThrow('comment failed: code=-3000');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user