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:
6 files changed
+287
-2
No files matched your search
@@ -3,6 +3,7 @@ import {
|
||||
getQzoneFeeds,
|
||||
getQzoneMsgList,
|
||||
publishQzoneMsg,
|
||||
setQzoneLike,
|
||||
type QzoneFeedsResult,
|
||||
type QzoneMsgListResult,
|
||||
type QzonePublishResult,
|
||||
@@ -48,4 +49,15 @@ export class QzoneApi {
|
||||
const cookieObject = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
await deleteQzoneMsg(cookieObject, this.ctx.identity.uin, tid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给一条说说点赞/取消赞。`targetUin` 省略时点赞机器人自己空间的说说;
|
||||
* 点赞好友说说时传好友 uin(tid 来自 get_qzone_feeds / get_qzone_msg_list)。
|
||||
*/
|
||||
async like(tid: string, targetUin: number | undefined, like: boolean): Promise<void> {
|
||||
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');
|
||||
await setQzoneLike(cookieObject, selfUin, owner, tid, like);
|
||||
}
|
||||
}
|
||||
@@ -67,4 +67,21 @@ describe('apis/qzone', () => {
|
||||
expect(getCookies).toHaveBeenCalledWith('qzone.qq.com');
|
||||
expect(delSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', 'TID9');
|
||||
});
|
||||
|
||||
it('like defaults the feed owner to self and passes opUin=self + like flag', async () => {
|
||||
const getCookies = vi.fn(async () => ({ p_skey: 'PSK' }));
|
||||
const bridge = mockBridge({ apis: { ...mockApiHub(), web: { getCookies } } as never });
|
||||
const likeSpy = vi.spyOn(qzoneWeb, 'setQzoneLike').mockResolvedValue();
|
||||
await new QzoneApi(bridge as never).like('TIDX', undefined, true);
|
||||
// opUin = self ('10001'), owner defaults to self when target_uin absent
|
||||
expect(likeSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', '10001', 'TIDX', true);
|
||||
});
|
||||
|
||||
it('like targets a friend\'s space (owner = target_uin) and threads the unlike flag', async () => {
|
||||
const getCookies = vi.fn(async () => ({ p_skey: 'PSK' }));
|
||||
const bridge = mockBridge({ apis: { ...mockApiHub(), web: { getCookies } } as never });
|
||||
const likeSpy = vi.spyOn(qzoneWeb, 'setQzoneLike').mockResolvedValue();
|
||||
await new QzoneApi(bridge as never).like('TIDX', 20002, false);
|
||||
expect(likeSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', '20002', 'TIDX', false);
|
||||
});
|
||||
});
|
||||
@@ -3380,6 +3380,55 @@ export const ACTIONS: CatalogAction[] = [
|
||||
},
|
||||
"category": "扩展"
|
||||
},
|
||||
{
|
||||
"name": "like_qzone",
|
||||
"aliases": [],
|
||||
"summary": "给一条说说点赞(QQ 空间)",
|
||||
"readOnly": false,
|
||||
"params": [
|
||||
{
|
||||
"name": "tid",
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"desc": "说说 tid"
|
||||
},
|
||||
{
|
||||
"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"
|
||||
},
|
||||
"target_uin": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"description": "说说所属 QQ 号,省略则为机器人自己"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"tid"
|
||||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"category": "空间"
|
||||
},
|
||||
{
|
||||
"name": "mark_group_msg_as_read",
|
||||
"aliases": [],
|
||||
@@ -6369,6 +6418,55 @@ export const ACTIONS: CatalogAction[] = [
|
||||
},
|
||||
"category": "扩展"
|
||||
},
|
||||
{
|
||||
"name": "unlike_qzone",
|
||||
"aliases": [],
|
||||
"summary": "取消对一条说说的点赞(QQ 空间)",
|
||||
"readOnly": false,
|
||||
"params": [
|
||||
{
|
||||
"name": "tid",
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"desc": "说说 tid"
|
||||
},
|
||||
{
|
||||
"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"
|
||||
},
|
||||
"target_uin": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"description": "说说所属 QQ 号,省略则为机器人自己"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"tid"
|
||||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"category": "空间"
|
||||
},
|
||||
{
|
||||
"name": "upload_forward_msg",
|
||||
"aliases": [],
|
||||
@@ -6761,6 +6859,6 @@ export const CATEGORIES: CatalogCategory[] = [
|
||||
},
|
||||
{
|
||||
"category": "空间",
|
||||
"count": 4
|
||||
"count": 6
|
||||
}
|
||||
];
|
||||
@@ -85,6 +85,45 @@ export const actions = [
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
// like_qzone — 给一条说说点赞。target_uin 省略=机器人自己空间;点赞好友说说传其 uin。
|
||||
// 写操作;高频点赞会被 Qzone 风控,调用方需限流。
|
||||
defineAction({
|
||||
name: 'like_qzone',
|
||||
summary: '给一条说说点赞(QQ 空间)',
|
||||
params: {
|
||||
tid: f.string({ allowEmpty: false }).describe('说说 tid'),
|
||||
target_uin: f.uint().describe('说说所属 QQ 号,省略则为机器人自己').optional(),
|
||||
},
|
||||
run: async (p, ctx) => {
|
||||
try {
|
||||
await ctx.bridge.apis.qzone.like(p.tid, p.target_uin, true);
|
||||
return okResponse(null);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'failed to like qzone msg';
|
||||
return failedResponse(RETCODE.INTERNAL_ERROR, message);
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
// unlike_qzone — 取消对一条说说的点赞。参数同 like_qzone。写操作。
|
||||
defineAction({
|
||||
name: 'unlike_qzone',
|
||||
summary: '取消对一条说说的点赞(QQ 空间)',
|
||||
params: {
|
||||
tid: f.string({ allowEmpty: false }).describe('说说 tid'),
|
||||
target_uin: f.uint().describe('说说所属 QQ 号,省略则为机器人自己').optional(),
|
||||
},
|
||||
run: async (p, ctx) => {
|
||||
try {
|
||||
await ctx.bridge.apis.qzone.like(p.tid, p.target_uin, false);
|
||||
return okResponse(null);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'failed to unlike qzone msg';
|
||||
return failedResponse(RETCODE.INTERNAL_ERROR, message);
|
||||
}
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user