fix(qzone): read real t1_tid/t1_time publish fields — was false-throwing every success
Adversarial review (verdict NEEDS-FIXES, CRITICAL): emotion_cgi_publish_v6's success envelope names the new feed id `t1_tid` and post time `t1_time` (a STRING), NOT `tid`/`now`. So every successful publish hit the '缺少 tid' guard and was reported as a failure (risking caller-retry duplicate 说说). The tests passed only because they mocked the wrong field names — they codified the bug. - Read t1_tid (primary) with tid as a defensive fallback; t1_time||now via Number() (t1_time is a string on the wire). Verified against the Go reference lib starim00/AnimeAPI EmotionPublishVo. - Added who=1 to the form body (both working references send it). - Tests now encode the real contract (t1_tid/t1_time string), plus a tid/now fallback case and a no-feed-id throw case.
This commit is contained in:
2 files changed
+30
-11
No files matched your search
@@ -311,9 +311,13 @@ interface RawPublishResponse {
|
||||
code?: number;
|
||||
subcode?: number;
|
||||
message?: string;
|
||||
/** Published feed id on success. */
|
||||
// The publish_v6 SUCCESS envelope names the new feed id `t1_tid` and the
|
||||
// post time `t1_time` (the latter arrives as a STRING). `tid`/`now` are
|
||||
// kept as defensive fallbacks for alternate client builds, but `t1_tid`
|
||||
// is the real primary — reading `tid` alone false-throws on every success.
|
||||
t1_tid?: string;
|
||||
t1_time?: string;
|
||||
tid?: string;
|
||||
/** Post timestamp (unix seconds) on success. */
|
||||
now?: number;
|
||||
}
|
||||
|
||||
@@ -363,6 +367,7 @@ export async function publishQzoneMsg(
|
||||
ver: '1',
|
||||
ugc_right: '1',
|
||||
to_sign: '0',
|
||||
who: '1',
|
||||
hostuin: hostUin,
|
||||
code_version: '1',
|
||||
format: 'json',
|
||||
@@ -379,10 +384,13 @@ export async function publishQzoneMsg(
|
||||
log.warn('publishQzoneMsg: non-zero code (uin=%s) code=%d msg=%s', hostUin, data.code, data.message);
|
||||
throw new Error(`qzone publish failed: code=${data.code} ${data.message ?? ''}`.trim());
|
||||
}
|
||||
if (!data.tid) {
|
||||
const tid = data.t1_tid ?? data.tid;
|
||||
if (!tid) {
|
||||
log.warn('publishQzoneMsg: no tid in response (uin=%s) — publish likely rejected', hostUin);
|
||||
throw new Error('发表说说失败:响应缺少 tid');
|
||||
}
|
||||
|
||||
return { tid: String(data.tid), time: Number(data.now ?? 0) };
|
||||
// t1_time is a string on the wire; Number() coerces it (and the `now`
|
||||
// numeric fallback) uniformly.
|
||||
return { tid: String(tid), time: Number(data.t1_time ?? data.now ?? 0) };
|
||||
}
|
||||
@@ -193,10 +193,15 @@ describe('qzone / getQzoneFeeds (HTTP layer)', () => {
|
||||
describe('qzone / publishQzoneMsg (HTTP layer)', () => {
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
it('POSTs a form-urlencoded body to the proxied publish CGI and returns tid/time', async () => {
|
||||
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"tid":"NEWTID","now":1700000000}');
|
||||
it('POSTs a form-urlencoded body and reads the real t1_tid/t1_time (string) success fields', async () => {
|
||||
// The real publish_v6 success envelope names the new feed id `t1_tid`
|
||||
// and the post time `t1_time` (a STRING) — reading `tid`/`now` would
|
||||
// false-throw on every successful publish.
|
||||
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue(
|
||||
'{"code":0,"t1_tid":"NEWTID","t1_time":"1700000000"}',
|
||||
);
|
||||
|
||||
const out = await publishQzoneMsg(cookies, '10000', 'hello 世界');
|
||||
const out = await publishQzoneMsg(cookies, '10000', 'hello 世界 & friends');
|
||||
|
||||
expect(out).toEqual({ tid: 'NEWTID', time: 1700000000 });
|
||||
|
||||
@@ -208,14 +213,20 @@ describe('qzone / publishQzoneMsg (HTTP layer)', () => {
|
||||
const h = headers as Record<string, string>;
|
||||
expect(h['Content-Type']).toBe('application/x-www-form-urlencoded');
|
||||
expect(h.Cookie).toContain('p_skey=PSK');
|
||||
// form body carries the content (urlencoded) and the host uin
|
||||
// form body carries the content (urlencoded, incl. the `&`) and host uin
|
||||
const form = new URLSearchParams(body as string);
|
||||
expect(form.get('con')).toBe('hello 世界');
|
||||
expect(form.get('con')).toBe('hello 世界 & friends');
|
||||
expect(form.get('hostuin')).toBe('10000');
|
||||
expect(form.get('who')).toBe('1');
|
||||
expect(form.get('format')).toBe('json');
|
||||
expect(form.get('qzreferrer')).toBe('https://user.qzone.qq.com/10000');
|
||||
});
|
||||
|
||||
it('falls back to tid/now for alternate client builds', async () => {
|
||||
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"tid":"ALT","now":1700000001}');
|
||||
await expect(publishQzoneMsg(cookies, '10000', 'hi')).resolves.toEqual({ tid: 'ALT', time: 1700000001 });
|
||||
});
|
||||
|
||||
it('rejects empty content before any request', async () => {
|
||||
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
|
||||
await expect(publishQzoneMsg(cookies, '10000', '')).rejects.toThrow('content is required');
|
||||
@@ -227,8 +238,8 @@ describe('qzone / publishQzoneMsg (HTTP layer)', () => {
|
||||
await expect(publishQzoneMsg(cookies, '10000', 'hi')).rejects.toThrow('code=-10000');
|
||||
});
|
||||
|
||||
it('throws when the success body carries no tid', async () => {
|
||||
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"now":1700000000}');
|
||||
it('throws when the success body carries no feed id (neither t1_tid nor tid)', async () => {
|
||||
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"t1_time":"1700000000"}');
|
||||
await expect(publishQzoneMsg(cookies, '10000', 'hi')).rejects.toThrow('缺少 tid');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user