feat: Qzone支持上传图片(发说说、评论附图) (#152)

* feat(core,onebot,protocol): support publish shuoshuo and comment with image

* Fix: Accidentally changed notes
This commit is contained in:
Airline233 authored and GitHub committed 2026-06-30 01:47:21 +08:00
1 parent 547640bbfd
commit 62eb8fd763
9 files changed
+658 -28

No files matched your search

@@ -0,0 +1,105 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { uploadQzoneImage, uploadQzoneImageFromSource } from '../../src/web/qzone';
import { RequestUtil } from '../../src/web/request-util';
const cookies = { p_skey: 'PSK', skey: 'SK', uin: 'o10000', p_uin: 'o10000' };
const expectedGtk = (() => {
let h = 5381;
for (const c of 'PSK') h += (h << 5) + c.charCodeAt(0);
return (h & 0x7fffffff).toString();
})();
const successBody = `<script>frameElement.callback({
"code": 0,
"subcode": 0,
"data": {
"albumid": "12345",
"lloc": "abc123",
"url": "https://example.qzone.qq.com/image.jpg",
"type": 22,
"height": 800,
"width": 600
}
});</script>`;
describe('qzone / uploadQzoneImage', () => {
afterEach(() => vi.restoreAllMocks());
it('POSTs the upload form and returns richval + direct url', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue(successBody);
const out = await uploadQzoneImage(cookies, '10000', 'AQID');
expect(out).toEqual({
richval: ',12345,abc123,abc123,22,800,600,,800,600',
url: 'https://example.qzone.qq.com/image.jpg',
albumid: '12345',
lloc: 'abc123',
type: 22,
width: 600,
height: 800,
});
const [url, method, body, headers] = spy.mock.calls[0]!;
expect(url).toBe(`https://up.qzone.qq.com/cgi-bin/upload/cgi_upload_image?g_tk=${expectedGtk}`);
expect(method).toBe('POST');
const h = headers as Record<string, string>;
expect(h['Content-Type']).toBe('application/x-www-form-urlencoded');
expect(h.Cookie).toContain('p_skey=PSK');
expect(h.Cookie).toContain('skey=SK');
const form = new URLSearchParams(body as string);
expect(form.get('uin')).toBe('10000');
expect(form.get('p_uin')).toBe('10000');
expect(form.get('p_skey')).toBe('PSK');
expect(form.get('base64')).toBe('1');
expect(form.get('jsonhtml_callback')).toBe('callback');
expect(form.get('picfile')).toBe('AQID');
expect(form.get('qzreferrer')).toBe('https://user.qzone.qq.com/10000/main');
});
it('accepts base64:// data-URI sources and strips the prefix before upload', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue(successBody);
await uploadQzoneImageFromSource(cookies, '10000', 'base64://data:image/png;base64,AQID');
const form = new URLSearchParams(spy.mock.calls[0]![2] as string);
expect(form.get('picfile')).toBe('AQID');
});
it('defaults optional image metadata to 0 when the upload succeeds without it', async () => {
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue(
'callback({"code":0,"data":{"albumid":"12345","lloc":"abc123","url":"https://example.qzone.qq.com/image.jpg"}});',
);
await expect(uploadQzoneImage(cookies, '10000', 'AQID')).resolves.toMatchObject({
richval: ',12345,abc123,abc123,0,0,0,,0,0',
type: 0,
width: 0,
height: 0,
});
});
it('rejects invalid base64 before any request', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
await expect(uploadQzoneImage(cookies, '10000', 'not base64?')).rejects.toThrow('valid base64');
expect(spy).not.toHaveBeenCalled();
});
it('throws on non-zero code or subcode from Qzone', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
spy.mockResolvedValueOnce('callback({"code":-1,"message":"upload failed"});');
await expect(uploadQzoneImage(cookies, '10000', 'AQID')).rejects.toThrow('code=-1');
spy.mockResolvedValueOnce('callback({"code":0,"subcode":-2,"message":"sub failed"});');
await expect(uploadQzoneImage(cookies, '10000', 'AQID')).rejects.toThrow('subcode=-2');
});
it('throws when the success response lacks required upload fields', async () => {
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('callback({"code":0,"data":{"albumid":"12345"}});');
await expect(uploadQzoneImage(cookies, '10000', 'AQID')).rejects.toThrow('响应缺少必要字段');
});
});
+39 -1
View File
@@ -222,6 +222,19 @@ describe('qzone / publishQzoneMsg (HTTP layer)', () => {
expect(form.get('qzreferrer')).toBe('https://user.qzone.qq.com/10000');
});
it('threads image richType/richval into the publish form', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue(
'{"code":0,"t1_tid":"NEWTID","t1_time":"1700000000"}',
);
const richval = ',12345,abc,abc,22,800,600,,800,600\t,12346,def,def,22,640,480,,640,480';
await publishQzoneMsg(cookies, '10000', 'with images', 1, richval);
const form = new URLSearchParams(spy.mock.calls[0]![2] as string);
expect(form.get('richtype')).toBe('1');
expect(form.get('richval')).toBe(richval);
});
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 });
@@ -233,6 +246,13 @@ describe('qzone / publishQzoneMsg (HTTP layer)', () => {
expect(spy).not.toHaveBeenCalled();
});
it('rejects incomplete publish rich params before any request', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
await expect(publishQzoneMsg(cookies, '10000', 'hi', 1)).rejects.toThrow('richType and richval');
await expect(publishQzoneMsg(cookies, '10000', 'hi', undefined, ',123')).rejects.toThrow('richType and richval');
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');
@@ -378,7 +398,7 @@ describe('qzone / commentQzoneMsg (HTTP layer)', () => {
expect(form.get('uin')).toBe('10000');
expect(form.get('hostUin')).toBe('20002');
expect(form.get('content')).toBe('说得好 & 顶');
expect(form.get('format')).toBe('fs');
expect(form.get('format')).toBe('json');
// qzreferrer carries the commenter's own space (matches the impls)
expect(form.get('qzreferrer')).toBe('https://user.qzone.qq.com/10000');
// the re_feeds param family (3/3 impls) the publish sibling also sends
@@ -387,6 +407,17 @@ describe('qzone / commentQzoneMsg (HTTP layer)', () => {
expect(form.get('paramstr')).toBe('1');
});
it('threads image direct-url richval into the comment form', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0,"commentid":987}');
const richval = 'https://example.qzone.qq.com/a.jpg\thttps://example.qzone.qq.com/b.jpg';
await commentQzoneMsg(cookies, '10000', '20002', 'TIDX', 'pics', 1, richval);
const form = new URLSearchParams(spy.mock.calls[0]![2] as string);
expect(form.get('richtype')).toBe('1');
expect(form.get('richval')).toBe(richval);
});
it('resolves with empty comment_id on success when the response carries no id (field name varies)', async () => {
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":0}');
await expect(commentQzoneMsg(cookies, '10000', '20002', 'TIDX', 'hi')).resolves.toEqual({ comment_id: '' });
@@ -404,6 +435,13 @@ describe('qzone / commentQzoneMsg (HTTP layer)', () => {
expect(spy).not.toHaveBeenCalled();
});
it('rejects incomplete comment rich params before any request', async () => {
const spy = vi.spyOn(RequestUtil, 'HttpGetText');
await expect(commentQzoneMsg(cookies, '10000', '20002', 'TIDX', 'hi', 1)).rejects.toThrow('richType and richval');
await expect(commentQzoneMsg(cookies, '10000', '20002', 'TIDX', 'hi', undefined, 'https://example.com/img.jpg')).rejects.toThrow('richType and richval');
expect(spy).not.toHaveBeenCalled();
});
it('throws on a non-zero code (comments disabled / no permission)', async () => {
vi.spyOn(RequestUtil, 'HttpGetText').mockResolvedValue('{"code":-3000,"message":"forbidden"}');
await expect(commentQzoneMsg(cookies, '10000', '20002', 'TIDX', 'hi')).rejects.toThrow('comment failed: code=-3000');