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:
DrSmoothl committed 2026-06-20 02:05:56 +08:00
1 parent 870504ae3a
commit 95f24ffc4a
6 files changed
+253 -2

No files matched your search

+84
View File
@@ -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) : '' };
}
+51 -1
View File
@@ -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');
});
});