diff --git a/packages/core/src/bridge/apis/group-album.ts b/packages/core/src/bridge/apis/group-album.ts index b7d30825..505205b3 100644 --- a/packages/core/src/bridge/apis/group-album.ts +++ b/packages/core/src/bridge/apis/group-album.ts @@ -2,6 +2,7 @@ import type { JsonObject, JsonValue } from '@snowluma/common/json'; import { createLogger } from '@snowluma/common/logger'; import type { AlbumCreator, + CommentRespData, DeleteMediasRequest, DeleteMediasResponse, DoQunCommentRequest, @@ -23,6 +24,8 @@ import type { BridgeContext } from '../bridge-context'; const log = createLogger('Bridge.GroupAlbum'); const GET_ALBUM_LIST_CMD = 'QunAlbum.trpc.qzone.webapp_qun_media.QunMedia.GetAlbumList'; const GET_ALBUM_LIST_SEQ = 3331; +const DO_QUN_COMMENT_CMD = 'QunAlbum.trpc.qzone.webapp_qun_operation.FeedsWriter.DoQunComment'; +const DO_QUN_COMMENT_SEQ = 8527; function uint64ToString(value: bigint | undefined): string { return (value ?? 0n).toString(); @@ -305,38 +308,30 @@ export class GroupAlbumApi { const traceId = `_${Date.now()}_${Math.floor(Math.random() * 100000)}`; const clientKey = Date.now().toString(); const uin = this.ctx.identity.uin; + const resolved = await this.resolveCommentMedia(groupId, albumId, lloc); + const media = mediaInfoForComment(resolved, lloc); + const batchId = media.batchId ?? optionalBatchId(resolved?.batchId); const body = protobuf_encode({ - field1: 8527, + field1: DO_QUN_COMMENT_SEQ, field2: new Uint8Array(0), field3: new Uint8Array(0), body: { groupId: groupId.toString(), field3: 2, reqBody: { - field1: { field3: 0, field4: '' }, field2: { field1: { uin }, }, field5: { - field1: { - field2: { - field1: 0, field2: '', - lloc, - field4: '', field6: '', field7: 0, field8: 0, field9: 0, field14: 0, field15: 0, field17: 0, - }, - }, + medias: [media], albumId, - field5: 0, + ...(batchId !== undefined ? { batchId } : {}), }, }, field5: { - field2: { uin }, - field3: { - field1: 0, - field2: content, - field3: '', field4: '', field5: 0, field6: '', - }, + user: { uin }, + contents: [{ type: 0, content }], clientKey, }, }, @@ -344,31 +339,35 @@ export class GroupAlbumApi { extMap: [{ key: 'fc-appid', value: '100' }], }); - const result = await this.ctx.sendRawPacket( - 'QunAlbum.trpc.qzone.webapp_qun_operation.FeedsWriter.DoQunComment', - body, - 15000, - ); + const result = await this.ctx.sendRawPacket(DO_QUN_COMMENT_CMD, body, 15000); if (!result.success || !result.gotResponse || !result.responseData) { throw new Error(result.errorMessage || 'failed to comment on album media'); } const resp = protobuf_decode(result.responseData); - - const resCode = resp.field1; - if (resCode !== 0 && resCode !== 8527 && !resp.comment) { - throw new Error(`comment album media error: retCode ${resCode ?? 'unknown'}`); + const resultCode = resp.result ?? 0; + if (resultCode !== 0) { + throw new Error( + `comment album media error: retCode ${resultCode}` + + (resp.errorText ? `, ${resp.errorText}` : ''), + ); } - const commentData = resp.comment?.data ?? {}; + const commentData = commentFromResponse(resp); + if (!commentData.id) { + throw new Error('comment album media error: empty comment'); + } return convertBigIntToString({ - id: commentData.id ?? '', - user: { uin: commentData.user?.uin ?? '' }, - content: commentData.content ?? [], + id: commentData.id, + user: { uin: commentData.user?.uin || uin }, + content: (commentData.content ?? [{ type: 0, content }]).map((item) => ({ + type: item.type ?? 0, + content: item.content ?? '', + })), time: commentData.time ?? '0', - clientKey: commentData.clientKey ?? '', + clientKey: commentData.clientKey || clientKey, }) as unknown as GroupAlbumCommentResult; } @@ -509,6 +508,39 @@ export class GroupAlbumApi { return { mediaId: mediaKey }; } } + + /** + * Official DoQunComment attaches the GetMediaList MediaInfo cell plus batch id. + * Page the album when the caller only has a lloc / video id. + */ + private async resolveCommentMedia( + groupId: number, + albumId: string, + mediaKey: string, + ): Promise { + try { + const seenCursors = new Set(); + let attachInfo = ''; + while (true) { + const page = await this.getMediaList(groupId, albumId, attachInfo); + const hit = findCommentMedia(page.mediaList, mediaKey); + if (hit) return hit; + + const next = page.nextAttachInfo ?? ''; + if (!next || seenCursors.has(next)) return undefined; + seenCursors.add(next); + attachInfo = next; + } + } catch (err) { + log.debug( + 'resolve album comment media failed, using lloc only: group=%d album=%s err=%s', + groupId, + albumId, + err instanceof Error ? err.message : String(err), + ); + return undefined; + } + } } interface AlbumDeleteMediaItem { @@ -538,6 +570,99 @@ function findDeleteTarget( return undefined; } +interface AlbumCommentMediaItem { + type?: number; + image?: { + name?: string; + sloc?: string; + lloc?: string; + isGif?: boolean; + hasRaw?: boolean; + } | null; + video?: { + id?: string; + cover?: { + name?: string; + sloc?: string; + lloc?: string; + } | null; + } | null; + uploader?: string; + batchId?: string | number | bigint; +} + +function optionalBatchId(value: unknown): bigint | undefined { + if (value === undefined || value === null || value === '') return undefined; + try { + const batchId = BigInt(value as string | number | bigint); + return batchId === 0n ? undefined : batchId; + } catch { + return undefined; + } +} + +function mediaInfoForComment(item: AlbumCommentMediaItem | undefined, lloc: string): MediaInfo { + if (!item) return { type: 1, image: { lloc } }; + + const info: MediaInfo = { + type: item.type ?? (item.video ? 2 : 1), + }; + if (item.image) { + info.image = { + name: item.image.name ?? '', + sloc: item.image.sloc ?? '', + lloc: item.image.lloc ?? '', + isGif: item.image.isGif ?? false, + hasRaw: item.image.hasRaw ?? false, + }; + } + if (item.video) { + info.video = { + id: item.video.id ?? '', + ...(item.video.cover ? { + cover: { + name: item.video.cover.name ?? '', + sloc: item.video.cover.sloc ?? '', + lloc: item.video.cover.lloc ?? '', + }, + } : {}), + }; + } + if (item.uploader) info.uploader = String(item.uploader); + const batchId = optionalBatchId(item.batchId); + if (batchId !== undefined) info.batchId = batchId; + return info; +} + +function findCommentMedia( + mediaList: ReadonlyArray, + mediaKey: string, +): AlbumCommentMediaItem | undefined { + for (const raw of mediaList) { + if (!raw || typeof raw !== 'object') continue; + const item = raw as AlbumCommentMediaItem; + if (item.image?.lloc === mediaKey) return item; + if (item.video?.id === mediaKey || item.video?.cover?.lloc === mediaKey) return item; + } + return undefined; +} + +function commentFromResponse(resp: DoQunCommentResponse): CommentRespData { + const wrapped = resp.comment; + if (!wrapped) return {}; + if (wrapped.data?.id) return wrapped.data; + if (wrapped.id) { + return { + id: wrapped.id, + content: wrapped.content, + time: wrapped.time, + clientKey: wrapped.clientKey, + user: wrapped.data?.user, + }; + } + return wrapped.data ?? {}; +} + export interface GroupAlbumMediaResult { mediaList: Array>; nextAttachInfo: string; diff --git a/packages/core/tests/actions/group-album.test.ts b/packages/core/tests/actions/group-album.test.ts index fd3459e6..7b4bfce1 100644 --- a/packages/core/tests/actions/group-album.test.ts +++ b/packages/core/tests/actions/group-album.test.ts @@ -2,6 +2,8 @@ import { describe, expect, it } from 'vitest'; import type { DeleteMediasRequest, DeleteMediasResponse, + DoQunCommentRequest, + DoQunCommentResponse, GetAlbumListResponse, GetMediaListResponse, } from '@snowluma/proto-defs/oidb-actions/group-album'; @@ -563,4 +565,162 @@ describe('apis/group-album', () => { expect(request.body?.mediaIds).toEqual(['unknown-id']); expect(request.body?.batchIds ?? []).toEqual([]); }); + + function packCommentOk(comment: DoQunCommentResponse['comment']): ReturnType { + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ + field1: 8527, + comment, + })), + }; + } + + function commentRequestOf(bridge: ReturnType): DoQunCommentRequest { + const commentCall = bridge.sendRawPacket.mock.calls.find((call) => + String(call[0]).endsWith('DoQunComment'), + ); + expect(commentCall?.[0]).toBe( + 'QunAlbum.trpc.qzone.webapp_qun_operation.FeedsWriter.DoQunComment', + ); + return protobuf_decode(commentCall![1] as Uint8Array); + } + + it('encodes a photo comment with the official MediaInfo cell and batch id', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(async (cmd: string) => { + if (cmd.endsWith('GetMediaList')) { + return packMediaList({ + mediaList: [{ + type: 1, + image: { + name: 'photo.jpg', + sloc: 'small-loc', + lloc: 'photo-lloc', + isGif: false, + hasRaw: true, + }, + uploader: '10001', + batchId: 77n, + }], + }); + } + return packCommentOk({ + data: { + id: 'cmt-1', + user: { uin: '10001' }, + content: [{ type: 0, content: 'hello' }], + time: 1700000000n, + clientKey: 'ck', + }, + }); + }); + + await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) + .resolves.toEqual({ + id: 'cmt-1', + user: { uin: '10001' }, + content: [{ type: 0, content: 'hello' }], + time: '1700000000', + clientKey: 'ck', + }); + + const request = commentRequestOf(bridge); + expect(request.field1).toBe(8527); + expect(request.body).toMatchObject({ + groupId: '12345', + field3: 2, + reqBody: { + field2: { field1: { uin: '10001' } }, + field5: { + albumId: 'album-id', + batchId: 77n, + medias: [{ + type: 1, + image: { + name: 'photo.jpg', + sloc: 'small-loc', + lloc: 'photo-lloc', + }, + uploader: '10001', + batchId: 77n, + }], + }, + }, + field5: { + user: { uin: '10001' }, + contents: [{ content: 'hello' }], + }, + }); + expect(request.body?.reqBody?.field1 ?? null).toBeNull(); + }); + + it('rejects a seq-echo 8527 packet that has no comment body', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(async (cmd: string) => { + if (cmd.endsWith('GetMediaList')) { + return packMediaList({ mediaList: [{ type: 1, image: { lloc: 'photo-lloc' } }] }); + } + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ field1: 8527 })), + }; + }); + + await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) + .rejects.toThrow('comment album media error: empty comment'); + }); + + it('surfaces the album result code instead of treating seq 8527 as success', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(async (cmd: string) => { + if (cmd.endsWith('GetMediaList')) { + return packMediaList({ mediaList: [{ type: 1, image: { lloc: 'photo-lloc' } }] }); + } + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ + field1: 8527, + result: 1001, + errorText: 'permission denied', + })), + }; + }); + + await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) + .rejects.toThrow('comment album media error: retCode 1001, permission denied'); + }); + + it('accepts a comment written directly on envelope field 4', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(async (cmd: string) => { + if (cmd.endsWith('GetMediaList')) { + return packMediaList({ mediaList: [{ type: 1, image: { lloc: 'photo-lloc' } }] }); + } + return packCommentOk({ + id: 'flat-id', + content: [{ type: 0, content: 'hello' }], + time: 123n, + clientKey: 'ck', + }); + }); + + await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) + .resolves.toMatchObject({ + id: 'flat-id', + user: { uin: '10001' }, + content: [{ type: 0, content: 'hello' }], + time: '123', + clientKey: 'ck', + }); + }); }); diff --git a/packages/proto-defs/src/oidb-actions/group-album.ts b/packages/proto-defs/src/oidb-actions/group-album.ts index 730ef8ec..ab2dadf5 100644 --- a/packages/proto-defs/src/oidb-actions/group-album.ts +++ b/packages/proto-defs/src/oidb-actions/group-album.ts @@ -147,42 +147,20 @@ export interface CommentContentItem { export interface CommentUser { uin?: pb<13, string>; } -export interface CommentReqContentMeta { - field1?: pb<1, uint_32>; - field2?: pb<2, string>; - field3?: pb<3, string>; - field4?: pb<4, string>; - field5?: pb<5, uint_32>; - field6?: pb<6, string>; -} export interface CommentReqContent { - field2?: pb<2, CommentUser>; - field3?: pb<3, CommentReqContentMeta>; + user?: pb<2, CommentUser>; + // QQ NT FeedWorker writes StComment content cells as a repeated field. + contents?: pb_repeated<3, CommentContentItem>; clientKey?: pb<7, string>; } -export interface CommentReqPhotoMeta { - field1?: pb<1, uint_32>; - field2?: pb<2, string>; - lloc?: pb<3, string>; - field4?: pb<4, string>; - field6?: pb<6, string>; - field7?: pb<7, uint_32>; - field8?: pb<8, uint_32>; - field9?: pb<9, uint_32>; - field14?: pb<14, uint_32>; - field15?: pb<15, uint_32>; - field17?: pb<17, uint_32>; -} -export interface CommentReqPhotoWrap { - field2?: pb<2, CommentReqPhotoMeta>; -} +// QQ NT FeedWorker copies StFeed media cells with the MediaInfo codec. export interface CommentReqPhotoInfo { - field1?: pb<1, CommentReqPhotoWrap>; + medias?: pb_repeated<1, MediaInfo>; albumId?: pb<3, string>; - field5?: pb<5, uint_32>; + batchId?: pb<5, uint_64>; } export interface CommentReqBodyHeader { - field3?: pb<3, uint_32>; + field3?: pb<3, uint_64>; field4?: pb<4, string>; } export interface CommentReqBodyUserWrap { @@ -222,11 +200,17 @@ export interface CommentRespData { clientKey?: pb<7, string>; } export interface DoQunCommentResponseComment { - data?: pb<2, CommentRespData>; + id?: pb<1, string>; + data?: pb<2, CommentRespData>; + content?: pb_repeated<3, CommentRespContent>; + time?: pb<4, uint_64>; + clientKey?: pb<7, string>; } export interface DoQunCommentResponse { - field1?: pb<1, int_32>; - comment?: pb<4, DoQunCommentResponseComment>; + field1?: pb<1, int_32>; + result?: pb<2, int_32>; + errorText?: pb<3, string>; + comment?: pb<4, DoQunCommentResponseComment>; } export interface DoQunLikeReqLikeInfo { id?: pb<1, string>;