feat(qzone,onebot): add set_qzone_ban action for qzone blacklist management (#193)
* feat(onebot,qzone): add set_qzone_ban action for qzone blacklist management * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> --------- Co-authored-by: Copilot Autofix powered by AI <[email protected]>
This commit is contained in:
1 parent
6cb7f2c6d0
commit
d3eb3bae14
6 files changed
+269
-14
No files matched your search
@@ -4,6 +4,7 @@ import {
|
||||
getQzoneFeeds,
|
||||
getQzoneMsgList,
|
||||
publishQzoneMsg,
|
||||
setQzoneBlack,
|
||||
setQzoneLike,
|
||||
updateQzoneMsgRight,
|
||||
uploadQzoneImageFromSource,
|
||||
@@ -114,4 +115,14 @@ export class QzoneApi {
|
||||
const cookieObject = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
return commentQzoneMsg(cookieObject, selfUin, owner, tid, content, richType, richval);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉黑或解除拉黑某人(修改机器人自身 QQ 空间黑名单)。
|
||||
* `ban=true` 拉黑,`ban=false` 解除拉黑。
|
||||
*/
|
||||
async setBlack(targetUin: number, ban: boolean): Promise<void> {
|
||||
const selfUin = this.ctx.identity.uin;
|
||||
const cookieObject = await this.ctx.apis.web.getCookies('qzone.qq.com');
|
||||
await setQzoneBlack(cookieObject, selfUin, targetUin.toString(), ban);
|
||||
}
|
||||
}
|
||||
@@ -8986,6 +8986,57 @@
|
||||
},
|
||||
"category": "扩展"
|
||||
},
|
||||
{
|
||||
"name": "set_qzone_ban",
|
||||
"aliases": [],
|
||||
"summary": "拉黑或解除拉黑某人(修改机器人自身 QQ 空间黑名单;enable=true 拉黑,false 解除)",
|
||||
"readOnly": false,
|
||||
"params": [
|
||||
{
|
||||
"name": "user_id",
|
||||
"type": "uint",
|
||||
"required": true,
|
||||
"role": "user_id",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
"desc": "目标 QQ 号"
|
||||
},
|
||||
{
|
||||
"name": "enable",
|
||||
"type": "bool",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"desc": "true 拉黑,false 解除拉黑",
|
||||
"default": true
|
||||
}
|
||||
],
|
||||
"invariants": [],
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"description": "目标 QQ 号",
|
||||
"x-role": "user_id"
|
||||
},
|
||||
"enable": {
|
||||
"type": "boolean",
|
||||
"description": "true 拉黑,false 解除拉黑",
|
||||
"default": true
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"user_id"
|
||||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"category": "空间"
|
||||
},
|
||||
{
|
||||
"name": "set_qzone_msg_right",
|
||||
"aliases": [],
|
||||
@@ -10004,7 +10055,7 @@
|
||||
},
|
||||
{
|
||||
"category": "空间",
|
||||
"count": 8
|
||||
"count": 9
|
||||
},
|
||||
{
|
||||
"category": "流式接口",
|
||||
|
||||
@@ -8989,6 +8989,57 @@ export const ACTIONS: CatalogAction[] = [
|
||||
},
|
||||
"category": "扩展"
|
||||
},
|
||||
{
|
||||
"name": "set_qzone_ban",
|
||||
"aliases": [],
|
||||
"summary": "拉黑或解除拉黑某人(修改机器人自身 QQ 空间黑名单;enable=true 拉黑,false 解除)",
|
||||
"readOnly": false,
|
||||
"params": [
|
||||
{
|
||||
"name": "user_id",
|
||||
"type": "uint",
|
||||
"required": true,
|
||||
"role": "user_id",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
"desc": "目标 QQ 号"
|
||||
},
|
||||
{
|
||||
"name": "enable",
|
||||
"type": "bool",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"desc": "true 拉黑,false 解除拉黑",
|
||||
"default": true
|
||||
}
|
||||
],
|
||||
"invariants": [],
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"description": "目标 QQ 号",
|
||||
"x-role": "user_id"
|
||||
},
|
||||
"enable": {
|
||||
"type": "boolean",
|
||||
"description": "true 拉黑,false 解除拉黑",
|
||||
"default": true
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"user_id"
|
||||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"category": "空间"
|
||||
},
|
||||
{
|
||||
"name": "set_qzone_msg_right",
|
||||
"aliases": [],
|
||||
@@ -10008,7 +10059,7 @@ export const CATEGORIES: CatalogCategory[] = [
|
||||
},
|
||||
{
|
||||
"category": "空间",
|
||||
"count": 8
|
||||
"count": 9
|
||||
},
|
||||
{
|
||||
"category": "流式接口",
|
||||
|
||||
@@ -230,5 +230,20 @@ export const actions = [
|
||||
return okResponse(res as unknown as JsonValue);
|
||||
},
|
||||
}),
|
||||
|
||||
// set_qzone_ban — 拉黑/解除拉黑某人(修改机器人自身 QQ 空间黑名单)。写操作。
|
||||
// enable=true 拉黑,enable=false 解除拉黑。
|
||||
defineAction({
|
||||
name: 'set_qzone_ban',
|
||||
summary: '拉黑或解除拉黑某人(修改机器人自身 QQ 空间黑名单;enable=true 拉黑,false 解除)',
|
||||
params: {
|
||||
user_id: f.userId().describe('目标 QQ 号'),
|
||||
enable: f.bool().describe('true 拉黑,false 解除拉黑').default(true),
|
||||
},
|
||||
run: async (p, ctx) => {
|
||||
await ctx.bridge.apis.qzone.setBlack(p.user_id, p.enable);
|
||||
return okResponse(null);
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
@@ -89,4 +89,34 @@ describe('qzone actions', () => {
|
||||
expect(updateRight).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('set_qzone_ban with enable=true calls setBlack with ban=true', async () => {
|
||||
const setBlack = vi.fn().mockResolvedValue(undefined);
|
||||
const handler = makeHandler({ setBlack });
|
||||
|
||||
const res = await handler.handle('set_qzone_ban', { user_id: 12345, enable: true });
|
||||
|
||||
expect(res).toMatchObject({ status: 'ok', retcode: 0 });
|
||||
expect(setBlack).toHaveBeenCalledWith(12345, true);
|
||||
});
|
||||
|
||||
it('set_qzone_ban with enable=false calls setBlack with ban=false', async () => {
|
||||
const setBlack = vi.fn().mockResolvedValue(undefined);
|
||||
const handler = makeHandler({ setBlack });
|
||||
|
||||
const res = await handler.handle('set_qzone_ban', { user_id: 12345, enable: false });
|
||||
|
||||
expect(res).toMatchObject({ status: 'ok', retcode: 0 });
|
||||
expect(setBlack).toHaveBeenCalledWith(12345, false);
|
||||
});
|
||||
|
||||
it('set_qzone_ban defaults enable to true', async () => {
|
||||
const setBlack = vi.fn().mockResolvedValue(undefined);
|
||||
const handler = makeHandler({ setBlack });
|
||||
|
||||
const res = await handler.handle('set_qzone_ban', { user_id: 12345 });
|
||||
|
||||
expect(res).toMatchObject({ status: 'ok', retcode: 0 });
|
||||
expect(setBlack).toHaveBeenCalledWith(12345, true);
|
||||
});
|
||||
|
||||
});
|
||||
@@ -64,30 +64,53 @@ export interface QzoneMsgListResult {
|
||||
|
||||
/**
|
||||
* Parse a Qzone CGI body that may be raw JSON or a JSONP callback wrapper
|
||||
* (`_Callback({...});` / `callback({...})`). We first attempt to slice from
|
||||
* the first `{` to the last `}`, when failed attempt from the first `back({`
|
||||
* to the last `})` and JSON.parse that — robust to either form without
|
||||
* pinning the callback name, which Qzone varies. Throws if no object body
|
||||
* is present (e.g. an HTML error page), which the caller turns into a
|
||||
* failed response rather than a silent empty list.
|
||||
* (`_Callback({...});` / `callback({...})`). Uses a layered fallback approach:
|
||||
*
|
||||
* 1. Try direct JSON.parse on the first `{` to last `}` slice.
|
||||
* 2. Try `back({...})` pattern (the original approach).
|
||||
* 3. Extract `back(...)` content, trim, then locate `{}` inside.
|
||||
*
|
||||
* This is robust to the various callback name variants Qzone uses.
|
||||
* Throws if no object body is present (e.g. an HTML error page).
|
||||
*/
|
||||
export function parseQzoneJson<T>(text: string): T {
|
||||
const s = text.trim();
|
||||
|
||||
// 1st: try direct JSON parse via {}
|
||||
try {
|
||||
const start = s.indexOf('{');
|
||||
const end = s.lastIndexOf('}');
|
||||
if (start !== -1 && end !== -1 && end > start) {
|
||||
return JSON.parse(s.slice(start, end + 1)) as T;
|
||||
}
|
||||
} catch {
|
||||
} catch { /* fallback */ }
|
||||
|
||||
}
|
||||
const start = s.indexOf('back({');
|
||||
const end = s.lastIndexOf('})');
|
||||
if (start === -1 || end === -1 || end < start) {
|
||||
// 2nd: try back({...}) as a whole
|
||||
try {
|
||||
const start = s.indexOf('back({');
|
||||
const end = s.lastIndexOf('})');
|
||||
if (start !== -1 && end !== -1 && end > start) {
|
||||
return JSON.parse(s.slice(start + 5, end + 1)) as T;
|
||||
}
|
||||
} catch { /* fallback */ }
|
||||
|
||||
// 3rd: find last back(...); occurrence, then extract {} from within
|
||||
const backStart = s.lastIndexOf('back(');
|
||||
if (backStart === -1) {
|
||||
throw new Error('invalid response from qzone api');
|
||||
}
|
||||
return JSON.parse(s.slice(start + 5, end + 1)) as T;
|
||||
// find the nearest ');' after back( to delimit the callback invocation
|
||||
const callEnd = s.indexOf(');', backStart);
|
||||
if (callEnd === -1) {
|
||||
throw new Error('invalid response from qzone api');
|
||||
}
|
||||
const inner = s.slice(backStart + 5, callEnd).trim();
|
||||
const braceStart = inner.indexOf('{');
|
||||
const braceEnd = inner.lastIndexOf('}');
|
||||
if (braceStart === -1 || braceEnd === -1 || braceEnd < braceStart) {
|
||||
throw new Error('invalid response from qzone api');
|
||||
}
|
||||
return JSON.parse(inner.slice(braceStart, braceEnd + 1)) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -988,6 +1011,80 @@ export interface QzoneCommentResult {
|
||||
* transport failure or a non-zero Qzone `code`/`subcode` (e.g. comments
|
||||
* disabled, no permission, or auth failure).
|
||||
*/
|
||||
// ─────────────── 拉黑/解拉黑 (ban/unban) — cgi_black_action_new ───────────────
|
||||
// Adds or removes a user from the bot's QQ-Zone blacklist. The CGI is
|
||||
// w.qzone.qq.com/cgi-bin/right/cgi_black_action_new (proxied through
|
||||
// h5.qzone.qq.com). POST form-urlencoded with `action=1` to ban,
|
||||
// `action=2` to unban. Response is a callback-wrapped body
|
||||
// (frameElement.callback style). Confirmed against
|
||||
// php-qzone/qzone.class.php:setQzoneRight. WRITE OP.
|
||||
|
||||
interface RawBlackActionResponse {
|
||||
code?: number;
|
||||
subcode?: number;
|
||||
message?: string;
|
||||
msg?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ban (blacklist) or unban a user in the bot's QQ-Zone space via
|
||||
* w.qzone.qq.com's cgi_black_action_new CGI (proxied through
|
||||
* h5.qzone.qq.com). `ban=true` adds the target to the blacklist
|
||||
* (action=1); `ban=false` removes them (action=2). Resolves on success;
|
||||
* THROWS on a transport failure or a non-zero Qzone `subcode`.
|
||||
* Confirmed against php-qzone/qzone.class.php:setQzoneRight.
|
||||
*/
|
||||
export async function setQzoneBlack(
|
||||
cookieObject: Record<string, string>,
|
||||
selfUin: string,
|
||||
targetUin: string,
|
||||
ban: boolean,
|
||||
): Promise<void> {
|
||||
if (!cookieObject || typeof cookieObject !== 'object') {
|
||||
throw new Error('cookieObject is required');
|
||||
}
|
||||
if (!targetUin) {
|
||||
throw new Error('targetUin is required');
|
||||
}
|
||||
|
||||
const bkn = getBknFromCookie(cookieObject);
|
||||
const url = `https://h5.qzone.qq.com/proxy/domain/w.qzone.qq.com/cgi-bin/right/cgi_black_action_new?g_tk=${bkn}`;
|
||||
const body = new URLSearchParams({
|
||||
uin: selfUin,
|
||||
act_uin: targetUin,
|
||||
action: ban ? '1' : '2',
|
||||
fupdate: '1',
|
||||
qzreferrer: `https://user.qzone.qq.com/${selfUin}/main`,
|
||||
}).toString();
|
||||
|
||||
const text = await RequestUtil.HttpGetText(url, 'POST', body, {
|
||||
Cookie: cookieToString(cookieObject),
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
});
|
||||
// Response is callback-wrapped (frameElement.callback style) and may contain
|
||||
// a small JS-object-literal fragment like `{name:"Ack"}`. Patch the known
|
||||
// unquoted key, then parse via the tolerant JSON/JSONP extractor.
|
||||
let data: RawBlackActionResponse;
|
||||
try {
|
||||
data = parseQzoneJson<RawBlackActionResponse>(text.replace(/\{\s*name\s*:/, '{"name":'));
|
||||
} catch {
|
||||
const snippet = text.trim().slice(0, 300);
|
||||
throw new Error(`qzone ${ban ? 'ban' : 'unban'} failed: ${snippet}${text.length > 300 ? '…' : ''}`);
|
||||
}
|
||||
|
||||
const verb = ban ? 'ban' : 'unban';
|
||||
if (typeof data.subcode === 'number' && data.subcode !== 0) {
|
||||
const msg = data.message ?? data.msg ?? '';
|
||||
log.warn('setQzoneBlack(%s): non-zero subcode (target=%s) subcode=%d msg=%s', verb, targetUin, data.subcode, msg);
|
||||
throw new Error(`qzone ${verb} failed: subcode=${data.subcode} ${msg}`.trim());
|
||||
}
|
||||
if (typeof data.code === 'number' && data.code !== 0) {
|
||||
const msg = data.message ?? data.msg ?? '';
|
||||
log.warn('setQzoneBlack(%s): non-zero code (target=%s) code=%d msg=%s', verb, targetUin, data.code, msg);
|
||||
throw new Error(`qzone ${verb} failed: code=${data.code} ${msg}`.trim());
|
||||
}
|
||||
}
|
||||
|
||||
export async function commentQzoneMsg(
|
||||
cookieObject: Record<string, string>,
|
||||
selfUin: string,
|
||||
|
||||
Reference in new issue
Block a user