feat(qzone): support post visibility rights when publishing qzone posts (#178)
Add ugc_right and target_uins to the qzone publish path across protocol, bridge, and the send_qzone_msg OneBot action, regenerate the MCP catalog, and cover restricted visibility (16/128) with validation tests.
This commit is contained in:
1 parent
c90a3954e4
commit
2f374ea8c9
8 files changed
+199
-16
No files matched your search
@@ -370,6 +370,8 @@ interface RawPublishResponse {
|
||||
now?: number;
|
||||
}
|
||||
|
||||
export type QzoneUgcRight = 1 | 4 | 16 | 64 | 128;
|
||||
|
||||
/** Result of publishing a 说说. */
|
||||
export interface QzonePublishResult {
|
||||
[key: string]: JsonValue;
|
||||
@@ -379,6 +381,31 @@ export interface QzonePublishResult {
|
||||
time: number;
|
||||
}
|
||||
|
||||
const QZONE_UGC_RIGHTS = new Set<number>([1, 4, 16, 64, 128]);
|
||||
|
||||
function normalizeQzoneUgcRight(ugcRight: number): QzoneUgcRight {
|
||||
if (!QZONE_UGC_RIGHTS.has(ugcRight)) {
|
||||
throw new Error('ugc_right must be one of 1, 4, 16, 64, 128');
|
||||
}
|
||||
return ugcRight as QzoneUgcRight;
|
||||
}
|
||||
|
||||
function normalizeTargetUins(targetUins?: string): string {
|
||||
const raw = (targetUins ?? '').trim();
|
||||
if (!raw) return '';
|
||||
|
||||
const seen = new Set<string>();
|
||||
for (const part of raw.split('|')) {
|
||||
const uin = part.trim();
|
||||
if (!uin) continue;
|
||||
if (!/^\d+$/.test(uin)) {
|
||||
throw new Error('target_uins must contain QQ numbers separated by |');
|
||||
}
|
||||
seen.add(uin);
|
||||
}
|
||||
return [...seen].join('|');
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish a 说说 via taotao.qzone.qq.com's emotion_cgi_publish_v6 CGI
|
||||
* (proxied through h5.qzone.qq.com). POSTs a form-urlencoded body with
|
||||
@@ -399,6 +426,8 @@ export async function publishQzoneMsg(
|
||||
content: string,
|
||||
richType?: number,
|
||||
richval?: string,
|
||||
ugcRight = 1,
|
||||
targetUins?: string,
|
||||
): Promise<QzonePublishResult> {
|
||||
if (!cookieObject || typeof cookieObject !== 'object') {
|
||||
throw new Error('cookieObject is required');
|
||||
@@ -408,9 +437,15 @@ export async function publishQzoneMsg(
|
||||
}
|
||||
assertRichParams(richType, richval);
|
||||
|
||||
const right = normalizeQzoneUgcRight(ugcRight);
|
||||
const effectiveTargetUins = right === 16 || right === 128 ? normalizeTargetUins(targetUins) : '';
|
||||
if ((right === 16 || right === 128) && !effectiveTargetUins) {
|
||||
throw new Error('target_uins is required when ugc_right is 16 or 128');
|
||||
}
|
||||
|
||||
const bkn = getBknFromCookie(cookieObject);
|
||||
const url = `https://h5.qzone.qq.com/proxy/domain/taotao.qzone.qq.com/cgi-bin/emotion_cgi_publish_v6?g_tk=${bkn}`;
|
||||
const body = new URLSearchParams({
|
||||
const bodyParams = new URLSearchParams({
|
||||
syn_tweet_verson: '1',
|
||||
paramstr: '1',
|
||||
pic_template: '',
|
||||
@@ -421,14 +456,16 @@ export async function publishQzoneMsg(
|
||||
con: content,
|
||||
feedversion: '1',
|
||||
ver: '1',
|
||||
ugc_right: '1',
|
||||
ugc_right: String(right),
|
||||
to_sign: '0',
|
||||
who: '1',
|
||||
hostuin: hostUin,
|
||||
code_version: '1',
|
||||
format: 'json',
|
||||
qzreferrer: `https://user.qzone.qq.com/${hostUin}`,
|
||||
}).toString();
|
||||
});
|
||||
if (effectiveTargetUins) bodyParams.set('allow_uins', effectiveTargetUins);
|
||||
const body = bodyParams.toString();
|
||||
|
||||
const text = await RequestUtil.HttpGetText(url, 'POST', body, {
|
||||
Cookie: cookieToString(cookieObject),
|
||||
|
||||
@@ -218,6 +218,8 @@ describe('qzone / publishQzoneMsg (HTTP layer)', () => {
|
||||
expect(form.get('con')).toBe('hello 世界 & friends');
|
||||
expect(form.get('hostuin')).toBe('10000');
|
||||
expect(form.get('who')).toBe('1');
|
||||
expect(form.get('ugc_right')).toBe('1');
|
||||
expect(form.has('allow_uins')).toBe(false);
|
||||
expect(form.get('format')).toBe('json');
|
||||
expect(form.get('qzreferrer')).toBe('https://user.qzone.qq.com/10000');
|
||||
});
|
||||
@@ -235,6 +237,39 @@ describe('qzone / publishQzoneMsg (HTTP layer)', () => {
|
||||
expect(form.get('richval')).toBe(richval);
|
||||
});
|
||||
|
||||
it('threads publish permission fields for restricted visibility', async () => {
|
||||
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue(
|
||||
'{"code":0,"t1_tid":"RIGHTTID","t1_time":"1700000000"}',
|
||||
);
|
||||
|
||||
await expect(publishQzoneMsg(cookies, '10000', 'limited', undefined, undefined, 16, '10001 | 10002|10001')).resolves.toEqual({
|
||||
tid: 'RIGHTTID',
|
||||
time: 1700000000,
|
||||
});
|
||||
|
||||
const [, , body] = spy.mock.calls[0]!;
|
||||
const form = new URLSearchParams(body as string);
|
||||
expect(form.get('ugc_right')).toBe('16');
|
||||
expect(form.get('allow_uins')).toBe('10001|10002');
|
||||
expect(form.get('who')).toBe('1');
|
||||
});
|
||||
|
||||
it('ignores target_uins for non-restricted visibility', async () => {
|
||||
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue(
|
||||
'{"code":0,"t1_tid":"FRIENDTID","t1_time":"1700000000"}',
|
||||
);
|
||||
|
||||
await expect(publishQzoneMsg(cookies, '10000', 'friends', undefined, undefined, 4, 'not-a-uin')).resolves.toEqual({
|
||||
tid: 'FRIENDTID',
|
||||
time: 1700000000,
|
||||
});
|
||||
|
||||
const [, , body] = spy.mock.calls[0]!;
|
||||
const form = new URLSearchParams(body as string);
|
||||
expect(form.get('ugc_right')).toBe('4');
|
||||
expect(form.has('allow_uins')).toBe(false);
|
||||
});
|
||||
|
||||
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 });
|
||||
@@ -253,6 +288,12 @@ describe('qzone / publishQzoneMsg (HTTP layer)', () => {
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects restricted visibility without target_uins before any request', async () => {
|
||||
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
|
||||
await expect(publishQzoneMsg(cookies, '10000', 'hi', undefined, undefined, 128)).rejects.toThrow('target_uins is required');
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws on a non-zero qzone code (rejected/rate-limited)', async () => {
|
||||
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":-10000,"message":"too frequent"}');
|
||||
await expect(publishQzoneMsg(cookies, '10000', 'hi')).rejects.toThrow('code=-10000');
|
||||
|
||||
Reference in new issue
Block a user