feat(qzone): like_qzone / unlike_qzone — like/unlike a 说说 via web API

setQzoneLike POSTs a form body to w.qzone.qq.com's internal_dolike_app
(like) or internal_unlike_app (unlike), proxied through h5.qzone, keyed by
the feed's mood unikey/curkey (http://user.qzone.qq.com/<uin>/mood/<tid>)
and fid=tid, as the bot (opUin). target_uin selects whose 说说 (defaults
to the bot's own); scope is 说说 (mood, appid 311). Throws on non-zero
code/subcode. Rejects empty tid pre-flight. WRITE — rate-limit (风控).

- core: QzoneApi.like(tid, targetUin?, like); onebot: like_qzone +
  unlike_qzone actions; regen catalog.
- tests: dolike vs unlike CGI selection, unikey/curkey/fid/opuin form
  lock, empty-tid preflight, code+subcode throws, and bridge owner-
  defaulting (self vs friend) + like-flag threading.
This commit is contained in:
DrSmoothl committed 2026-06-20 01:56:23 +08:00
1 parent e406a3bc2b
commit d497f1e6c7
6 files changed
+287 -2

No files matched your search

+70
View File
@@ -461,3 +461,73 @@ export async function deleteQzoneMsg(
throw new Error(`qzone delete failed: subcode=${data.subcode} ${data.message ?? ''}`.trim());
}
}
// ─────────────── 点赞/取消赞 (like/unlike) — internal_dolike_app ───────────────
// Likes or unlikes a 说说 (mood, appid 311) on `targetUin`'s space. like →
// internal_dolike_app, unlike → internal_unlike_app, same form body keyed by
// the feed's unikey/curkey (`http://user.qzone.qq.com/<uin>/mood/<tid>`) and
// `fid` (= tid). WRITE OP — rate-limit (likes are an active action,风控'd
// like sending messages). Scope is 说说 only; other feed types use a
// different unikey shape and are out of scope for this slice.
interface RawLikeResponse {
code?: number;
subcode?: number;
message?: string;
}
/**
* Like or unlike a 说说 by `tid` on `targetUin`'s space, as the bot
* (`opUin`). Resolves on success; THROWS on a transport failure or a
* non-zero Qzone `code`/`subcode` (e.g. an unknown tid, no permission, or
* an auth failure). `like=false` hits the unlike CGI.
*/
export async function setQzoneLike(
cookieObject: Record<string, string>,
opUin: string,
targetUin: string,
tid: string,
like: boolean,
): Promise<void> {
if (!cookieObject || typeof cookieObject !== 'object') {
throw new Error('cookieObject is required');
}
if (!tid) {
throw new Error('tid is required');
}
const bkn = getBknFromCookie(cookieObject);
const cgi = like ? 'internal_dolike_app' : 'internal_unlike_app';
const url = `https://h5.qzone.qq.com/proxy/domain/w.qzone.qq.com/cgi-bin/likes/${cgi}?g_tk=${bkn}`;
// unikey/curkey address the 说说 (mood) feed; fid is the tid.
const unikey = `http://user.qzone.qq.com/${targetUin}/mood/${tid}`;
const body = new URLSearchParams({
qzreferrer: `https://user.qzone.qq.com/${opUin}`,
opuin: opUin,
unikey,
curkey: unikey,
appid: '311',
typeid: '0',
fid: tid,
from: '1',
active: '0',
fupdate: '1',
format: 'json',
}).toString();
const text = await RequestUtil.HttpGetText(url, 'POST', body, {
Cookie: cookieToString(cookieObject),
'Content-Type': 'application/x-www-form-urlencoded',
});
const data = parseQzoneJson<RawLikeResponse>(text);
const verb = like ? 'like' : 'unlike';
if (typeof data.code === 'number' && data.code !== 0) {
log.warn('setQzoneLike(%s): non-zero code (tid=%s) code=%d msg=%s', verb, tid, data.code, data.message);
throw new Error(`qzone ${verb} failed: code=${data.code} ${data.message ?? ''}`.trim());
}
if (typeof data.subcode === 'number' && data.subcode !== 0) {
log.warn('setQzoneLike(%s): non-zero subcode (tid=%s) subcode=%d msg=%s', verb, tid, data.subcode, data.message);
throw new Error(`qzone ${verb} failed: subcode=${data.subcode} ${data.message ?? ''}`.trim());
}
}
+50 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { deleteQzoneMsg, getQzoneFeeds, getQzoneMsgList, mapFeeds, mapMsgList, parseQzoneJson, publishQzoneMsg } from '@snowluma/protocol/web/qzone';
import { 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,
@@ -292,3 +292,52 @@ describe('qzone / deleteQzoneMsg (HTTP layer)', () => {
await expect(deleteQzoneMsg(cookies, '10000', 'TID123')).resolves.toBeUndefined();
});
});
describe('qzone / setQzoneLike (HTTP layer)', () => {
afterEach(() => vi.restoreAllMocks());
it('POSTs to internal_dolike_app with the mood unikey/curkey/fid when liking', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"subcode":0}');
await expect(setQzoneLike(cookies, '10000', '20002', 'TIDX', true)).resolves.toBeUndefined();
const [url, method, body, headers] = spy.mock.calls[0]!;
expect(method).toBe('POST');
expect(url).toBe(
`https://h5.qzone.qq.com/proxy/domain/w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?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);
// unikey/curkey address the target's 说说 (mood), fid is the tid, opuin is self
expect(form.get('unikey')).toBe('http://user.qzone.qq.com/20002/mood/TIDX');
expect(form.get('curkey')).toBe('http://user.qzone.qq.com/20002/mood/TIDX');
expect(form.get('fid')).toBe('TIDX');
expect(form.get('opuin')).toBe('10000');
expect(form.get('appid')).toBe('311');
});
it('hits internal_unlike_app when unliking', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0}');
await setQzoneLike(cookies, '10000', '10000', 'TIDX', false);
const [url] = spy.mock.calls[0]!;
expect(url).toContain('/cgi-bin/likes/internal_unlike_app?');
});
it('rejects an empty tid before any request', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
await expect(setQzoneLike(cookies, '10000', '10000', '', true)).rejects.toThrow('tid is required');
expect(spy).not.toHaveBeenCalled();
});
it('throws on a non-zero code (no permission / unknown tid)', async () => {
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":-3000,"message":"no perm"}');
await expect(setQzoneLike(cookies, '10000', '20002', 'TIDX', true)).rejects.toThrow('like failed: code=-3000');
});
it('throws on a non-zero subcode too', async () => {
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"subcode":-7}');
await expect(setQzoneLike(cookies, '10000', '20002', 'TIDX', false)).rejects.toThrow('unlike failed: subcode=-7');
});
});