feat(qzone): delete_qzone_msg — delete a 说说 by tid via web API
deleteQzoneMsg POSTs a form-urlencoded tid to taotao.qzone.qq.com's emotion_cgi_delete_v6 (proxied through h5.qzone), on the bot's own space. Success has no positive payload beyond code 0, so the contract is throw-on-non-zero-code (foreign/unknown tid or auth failure); the server only deletes feeds the hostUin owns. Rejects empty tid pre-flight. - core: QzoneApi.delete; onebot: delete_qzone_msg action; regen catalog. - tests: tid form-body/url/headers lock, empty-tid preflight, non-zero code throw, and bridge self-space threading.
This commit is contained in:
6 files changed
+159
-2
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
deleteQzoneMsg,
|
||||||
getQzoneFeeds,
|
getQzoneFeeds,
|
||||||
getQzoneMsgList,
|
getQzoneMsgList,
|
||||||
publishQzoneMsg,
|
publishQzoneMsg,
|
||||||
@@ -41,4 +42,10 @@ export class QzoneApi {
|
|||||||
const cookieObject = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
const cookieObject = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||||
return publishQzoneMsg(cookieObject, this.ctx.identity.uin, content);
|
return publishQzoneMsg(cookieObject, this.ctx.identity.uin, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 删除机器人自己空间的一条说说(按 tid)。 */
|
||||||
|
async delete(tid: string): Promise<void> {
|
||||||
|
const cookieObject = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||||
|
await deleteQzoneMsg(cookieObject, this.ctx.identity.uin, tid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -58,4 +58,13 @@ describe('apis/qzone', () => {
|
|||||||
expect(pubSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', 'hello');
|
expect(pubSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', 'hello');
|
||||||
expect(out).toEqual({ tid: 'T', time: 1 });
|
expect(out).toEqual({ tid: 'T', time: 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('delete removes a feed by tid from the bot\'s own space', async () => {
|
||||||
|
const getCookies = vi.fn(async () => ({ p_skey: 'PSK' }));
|
||||||
|
const bridge = mockBridge({ apis: { ...mockApiHub(), web: { getCookies } } as never });
|
||||||
|
const delSpy = vi.spyOn(qzoneWeb, 'deleteQzoneMsg').mockResolvedValue();
|
||||||
|
await new QzoneApi(bridge as never).delete('TID9');
|
||||||
|
expect(getCookies).toHaveBeenCalledWith('qzone.qq.com');
|
||||||
|
expect(delSpy).toHaveBeenCalledWith({ p_skey: 'PSK' }, '10001', 'TID9');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -1015,6 +1015,40 @@ export const ACTIONS: CatalogAction[] = [
|
|||||||
},
|
},
|
||||||
"category": "消息"
|
"category": "消息"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "delete_qzone_msg",
|
||||||
|
"aliases": [],
|
||||||
|
"summary": "删除一条说说(QQ 空间,按 tid)",
|
||||||
|
"readOnly": false,
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "tid",
|
||||||
|
"type": "string",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string",
|
||||||
|
"minLength": 1
|
||||||
|
},
|
||||||
|
"desc": "说说 tid(来自 get_qzone_msg_list / send_qzone_msg)"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"invariants": [],
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"tid": {
|
||||||
|
"type": "string",
|
||||||
|
"minLength": 1,
|
||||||
|
"description": "说说 tid(来自 get_qzone_msg_list / send_qzone_msg)"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"tid"
|
||||||
|
],
|
||||||
|
"additionalProperties": true
|
||||||
|
},
|
||||||
|
"category": "空间"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "do_group_album_comment",
|
"name": "do_group_album_comment",
|
||||||
"aliases": [],
|
"aliases": [],
|
||||||
@@ -6727,6 +6761,6 @@ export const CATEGORIES: CatalogCategory[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"category": "空间",
|
"category": "空间",
|
||||||
"count": 3
|
"count": 4
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
@@ -67,6 +67,24 @@ export const actions = [
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
// delete_qzone_msg — 删除机器人自己空间的一条说说(按 tid)。写操作。
|
||||||
|
defineAction({
|
||||||
|
name: 'delete_qzone_msg',
|
||||||
|
summary: '删除一条说说(QQ 空间,按 tid)',
|
||||||
|
params: {
|
||||||
|
tid: f.string({ allowEmpty: false }).describe('说说 tid(来自 get_qzone_msg_list / send_qzone_msg)'),
|
||||||
|
},
|
||||||
|
run: async (p, ctx) => {
|
||||||
|
try {
|
||||||
|
await ctx.bridge.apis.qzone.delete(p.tid);
|
||||||
|
return okResponse(null);
|
||||||
|
} catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : 'failed to delete qzone msg';
|
||||||
|
return failedResponse(RETCODE.INTERNAL_ERROR, message);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||||
|
|||||||
@@ -394,3 +394,58 @@ export async function publishQzoneMsg(
|
|||||||
// numeric fallback) uniformly.
|
// numeric fallback) uniformly.
|
||||||
return { tid: String(tid), time: Number(data.t1_time ?? data.now ?? 0) };
|
return { tid: String(tid), time: Number(data.t1_time ?? data.now ?? 0) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─────────────── 删说说 (delete emotion) — emotion_cgi_delete_v6 ───────────────
|
||||||
|
// Deletes one of the bot's OWN 说说 by tid. Same form-POST mechanics as
|
||||||
|
// publish. No positive payload on success beyond `code: 0`, so the contract
|
||||||
|
// is throw-on-non-zero-code; a clean parse with code 0 (or absent) is a
|
||||||
|
// success. WRITE OP.
|
||||||
|
|
||||||
|
interface RawDeleteResponse {
|
||||||
|
code?: number;
|
||||||
|
subcode?: number;
|
||||||
|
message?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a 说说 by `tid` via taotao.qzone.qq.com's emotion_cgi_delete_v6 CGI
|
||||||
|
* (proxied through h5.qzone.qq.com), on the bot's own space. Resolves on
|
||||||
|
* success; THROWS on a transport failure or a non-zero Qzone `code` (e.g. an
|
||||||
|
* unknown/foreign tid, or an auth failure). Only the bot's own feeds can be
|
||||||
|
* deleted — the server rejects a tid the `hostUin` doesn't own.
|
||||||
|
*/
|
||||||
|
export async function deleteQzoneMsg(
|
||||||
|
cookieObject: Record<string, string>,
|
||||||
|
hostUin: string,
|
||||||
|
tid: string,
|
||||||
|
): 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 url = `https://h5.qzone.qq.com/proxy/domain/taotao.qzone.qq.com/cgi-bin/emotion_cgi_delete_v6?g_tk=${bkn}`;
|
||||||
|
const body = new URLSearchParams({
|
||||||
|
hostuin: hostUin,
|
||||||
|
tid,
|
||||||
|
t1_source: '1',
|
||||||
|
code_version: '1',
|
||||||
|
format: 'json',
|
||||||
|
json: '1',
|
||||||
|
qzreferrer: `https://user.qzone.qq.com/${hostUin}`,
|
||||||
|
}).toString();
|
||||||
|
|
||||||
|
const text = await RequestUtil.HttpGetText(url, 'POST', body, {
|
||||||
|
Cookie: cookieToString(cookieObject),
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
});
|
||||||
|
const data = parseQzoneJson<RawDeleteResponse>(text);
|
||||||
|
|
||||||
|
if (typeof data.code === 'number' && data.code !== 0) {
|
||||||
|
log.warn('deleteQzoneMsg: non-zero code (uin=%s tid=%s) code=%d msg=%s', hostUin, tid, data.code, data.message);
|
||||||
|
throw new Error(`qzone delete failed: code=${data.code} ${data.message ?? ''}`.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect, vi, afterEach } from 'vitest';
|
import { describe, it, expect, vi, afterEach } from 'vitest';
|
||||||
import { getQzoneFeeds, getQzoneMsgList, mapFeeds, mapMsgList, parseQzoneJson, publishQzoneMsg } from '@snowluma/protocol/web/qzone';
|
import { deleteQzoneMsg, getQzoneFeeds, getQzoneMsgList, mapFeeds, mapMsgList, parseQzoneJson, publishQzoneMsg } from '@snowluma/protocol/web/qzone';
|
||||||
import { RequestUtil } from '@snowluma/protocol/web/request-util';
|
import { RequestUtil } from '@snowluma/protocol/web/request-util';
|
||||||
|
|
||||||
// The 说说 list comes from taotao.qzone.qq.com's emotion_cgi_msglist_v6 CGI,
|
// The 说说 list comes from taotao.qzone.qq.com's emotion_cgi_msglist_v6 CGI,
|
||||||
@@ -243,3 +243,37 @@ describe('qzone / publishQzoneMsg (HTTP layer)', () => {
|
|||||||
await expect(publishQzoneMsg(cookies, '10000', 'hi')).rejects.toThrow('缺少 tid');
|
await expect(publishQzoneMsg(cookies, '10000', 'hi')).rejects.toThrow('缺少 tid');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('qzone / deleteQzoneMsg (HTTP layer)', () => {
|
||||||
|
afterEach(() => vi.restoreAllMocks());
|
||||||
|
|
||||||
|
it('POSTs the tid form body to the proxied delete CGI and resolves on code 0', async () => {
|
||||||
|
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"subcode":0,"message":""}');
|
||||||
|
|
||||||
|
await expect(deleteQzoneMsg(cookies, '10000', 'TID123')).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/taotao.qzone.qq.com/cgi-bin/emotion_cgi_delete_v6?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);
|
||||||
|
expect(form.get('tid')).toBe('TID123');
|
||||||
|
expect(form.get('hostuin')).toBe('10000');
|
||||||
|
expect(form.get('format')).toBe('json');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects an empty tid before any request', async () => {
|
||||||
|
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
|
||||||
|
await expect(deleteQzoneMsg(cookies, '10000', '')).rejects.toThrow('tid is required');
|
||||||
|
expect(spy).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws on a non-zero qzone code (foreign/unknown tid or auth failure)', async () => {
|
||||||
|
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":-4001,"message":"no permission"}');
|
||||||
|
await expect(deleteQzoneMsg(cookies, '10000', 'TID123')).rejects.toThrow('code=-4001');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user