diff --git a/packages/core/src/bridge/apis/group-album.ts b/packages/core/src/bridge/apis/group-album.ts index 57eb8981..8b707ff0 100644 --- a/packages/core/src/bridge/apis/group-album.ts +++ b/packages/core/src/bridge/apis/group-album.ts @@ -11,6 +11,8 @@ import type { DoQunLikeResponse, GetAlbumListRequest, GetAlbumListResponse, + GetQunFeedDetailRequest, + GetQunFeedDetailResponse, GroupAlbumInfo as GroupAlbumInfoWire, GetMediaListRequest, GetMediaListResponse, @@ -26,6 +28,8 @@ const GET_ALBUM_LIST_CMD = 'QunAlbum.trpc.qzone.webapp_qun_media.QunMedia.GetAlb 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; +const GET_QUN_FEED_DETAIL_CMD = 'QunAlbum.trpc.qzone.webapp_qun_feeds.FeedsReader.GetQunFeedDetail'; +const GET_QUN_FEED_DETAIL_COMMENT_COUNT = 20; function uint64ToString(value: bigint | undefined): string { return (value ?? 0n).toString(); @@ -316,8 +320,13 @@ export class GroupAlbumApi { 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 batchId = optionalBatchId(resolved?.batchId); + if (batchId === undefined) { + throw new Error('comment album media error: media not found'); + } + const mediaLloc = commentMediaLloc(resolved, lloc); + const feed = await this.getQunFeedDetail(groupId, albumId, batchId, mediaLloc); + const media = mediaInfoForComment(resolved, mediaLloc); const body = protobuf_encode({ field1: DO_QUN_COMMENT_SEQ, @@ -327,13 +336,17 @@ export class GroupAlbumApi { groupId: groupId.toString(), field3: 2, reqBody: { + field1: { + time: feed.time, + feedId: feed.feedId, + }, field2: { field1: { uin }, }, field5: { medias: [media], albumId, - ...(batchId !== undefined ? { batchId } : {}), + batchId, }, }, field5: { @@ -517,37 +530,76 @@ export class GroupAlbumApi { } /** - * Official DoQunComment attaches the GetMediaList MediaInfo cell plus batch id. - * Page the album when the caller only has a lloc / video id. + * Page the album when the caller only has a lloc / video id, so the + * comment can carry the official batch id and media kind. */ 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 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; + const next = page.nextAttachInfo ?? ''; + if (!next || seenCursors.has(next)) return undefined; + seenCursors.add(next); + attachInfo = next; } } + + private async getQunFeedDetail( + groupId: number, + albumId: string, + batchId: bigint, + lloc: string, + ): Promise<{ time: bigint; feedId: string }> { + const traceId = `_${Date.now()}_${Math.floor(Math.random() * 100000)}`; + const body = protobuf_encode({ + seq: 0, + field2: new Uint8Array(0), + field3: new Uint8Array(0), + data: { + groupId: groupId.toString(), + feedId: '', + commentCount: GET_QUN_FEED_DETAIL_COMMENT_COUNT, + attachInfo: '', + albumId, + batchId: batchId.toString(), + lloc, + }, + traceId, + extMap: [{ key: 'fc-appid', value: '100' }], + }); + + const result = await this.ctx.sendRawPacket(GET_QUN_FEED_DETAIL_CMD, body, 15000); + if (!result.success || !result.gotResponse || !result.responseData) { + throw new Error(result.errorMessage || 'failed to fetch album feed'); + } + + const resp = protobuf_decode(result.responseData); + const resultCode = resp.result ?? 0; + if (resultCode !== 0) { + throw new Error( + `fetch album feed error: retCode ${resultCode}` + + (resp.errorText ? `, ${resp.errorText}` : ''), + ); + } + + const cell = resp.data?.feed?.feed?.cellCommon; + const feedId = cell?.feedId ?? ''; + if (!feedId) { + throw new Error('comment album media error: empty feed'); + } + return { + time: cell?.time ?? 0n, + feedId, + }; + } } interface AlbumDeleteMediaItem { @@ -608,37 +660,23 @@ function optionalBatchId(value: unknown): bigint | undefined { } } -function mediaInfoForComment(item: AlbumCommentMediaItem | undefined, lloc: string): MediaInfo { - if (!item) return { type: 1, image: { lloc } }; +function commentMediaLloc(item: AlbumCommentMediaItem | undefined, lloc: string): string { + if (item?.video?.cover?.lloc) return item.video.cover.lloc; + if (item?.image?.lloc) return item.image.lloc; + return lloc; +} - const info: MediaInfo = { - type: item.type ?? (item.video ? 2 : 1), +function mediaInfoForComment(item: AlbumCommentMediaItem | undefined, lloc: string): MediaInfo { + if (item?.video) { + return { + type: 1, + video: { cover: { lloc: item.video.cover?.lloc || lloc } }, + }; + } + return { + type: 0, + image: { lloc: item?.image?.lloc || lloc }, }; - 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( diff --git a/packages/core/tests/actions/group-album.test.ts b/packages/core/tests/actions/group-album.test.ts index 7b4bfce1..1fbdc416 100644 --- a/packages/core/tests/actions/group-album.test.ts +++ b/packages/core/tests/actions/group-album.test.ts @@ -6,6 +6,8 @@ import type { DoQunCommentResponse, GetAlbumListResponse, GetMediaListResponse, + GetQunFeedDetailRequest, + GetQunFeedDetailResponse, } from '@snowluma/proto-defs/oidb-actions/group-album'; import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import { GroupAlbumApi } from '../../src/bridge/apis/group-album'; @@ -579,6 +581,54 @@ describe('apis/group-album', () => { }; } + function packFeedDetail( + feedId = 'official-feed-id', + time = 1700000123n, + ): ReturnType { + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ + result: 0, + data: { + feed: { + feed: { + cellCommon: { time, feedId }, + }, + }, + }, + })), + }; + } + + function packPhotoMedia(lloc = 'photo-lloc', batchId = 77n) { + return packMediaList({ + mediaList: [{ type: 1, image: { lloc }, batchId }], + }); + } + + function commentMocks( + extra: (cmd: string) => ReturnType | undefined = () => undefined, + ) { + return async (cmd: string) => { + const override = extra(cmd); + if (override) return override; + if (cmd.endsWith('GetMediaList')) return packPhotoMedia(); + if (cmd.endsWith('GetQunFeedDetail')) return packFeedDetail(); + return packCommentOk({ + data: { + id: 'cmt-1', + user: { uin: '10001' }, + content: [{ type: 0, content: 'hello' }], + time: 1700000000n, + clientKey: 'ck', + }, + }); + }; + } + function commentRequestOf(bridge: ReturnType): DoQunCommentRequest { const commentCall = bridge.sendRawPacket.mock.calls.find((call) => String(call[0]).endsWith('DoQunComment'), @@ -589,35 +639,19 @@ describe('apis/group-album', () => { return protobuf_decode(commentCall![1] as Uint8Array); } - it('encodes a photo comment with the official MediaInfo cell and batch id', async () => { + function feedDetailRequestOf(bridge: ReturnType): GetQunFeedDetailRequest { + const feedCall = bridge.sendRawPacket.mock.calls.find((call) => + String(call[0]).endsWith('GetQunFeedDetail'), + ); + expect(feedCall?.[0]).toBe( + 'QunAlbum.trpc.qzone.webapp_qun_feeds.FeedsReader.GetQunFeedDetail', + ); + return protobuf_decode(feedCall![1] as Uint8Array); + } + + it('looks up the official feed then comments with its header and slim photo cell', 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', - }, - }); - }); + bridge.sendRawPacket.mockImplementation(commentMocks()); await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) .resolves.toEqual({ @@ -628,25 +662,32 @@ describe('apis/group-album', () => { clientKey: 'ck', }); + expect(feedDetailRequestOf(bridge).data).toMatchObject({ + groupId: '12345', + commentCount: 20, + albumId: 'album-id', + batchId: '77', + lloc: 'photo-lloc', + }); + expect(feedDetailRequestOf(bridge).data?.feedId ?? '').toBe(''); + expect(feedDetailRequestOf(bridge).data?.attachInfo ?? '').toBe(''); + const request = commentRequestOf(bridge); expect(request.field1).toBe(8527); expect(request.body).toMatchObject({ groupId: '12345', field3: 2, reqBody: { + field1: { + time: 1700000123n, + feedId: 'official-feed-id', + }, 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, + image: { lloc: 'photo-lloc' }, }], }, }, @@ -655,23 +696,101 @@ describe('apis/group-album', () => { contents: [{ content: 'hello' }], }, }); - expect(request.body?.reqBody?.field1 ?? null).toBeNull(); + expect(request.body?.reqBody?.field5?.medias?.[0]?.type ?? 0).toBe(0); + }); + + it('comments a video with the cover location and video media type', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(commentMocks((cmd) => { + if (cmd.endsWith('GetMediaList')) { + return packMediaList({ + mediaList: [{ + type: 2, + video: { id: 'video-id', cover: { lloc: 'cover-lloc' } }, + batchId: 88n, + }], + }); + } + return undefined; + })); + + await new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'video-id', 'hello'); + + expect(feedDetailRequestOf(bridge).data?.lloc).toBe('cover-lloc'); + expect(commentRequestOf(bridge).body?.reqBody?.field5).toMatchObject({ + albumId: 'album-id', + batchId: 88n, + medias: [{ + type: 1, + video: { cover: { lloc: 'cover-lloc' } }, + }], + }); + }); + + it('rejects a comment when the album media cannot be resolved', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(commentMocks((cmd) => { + if (cmd.endsWith('GetMediaList')) return packMediaList({ mediaList: [] }); + return undefined; + })); + + await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'missing-lloc', 'hello')) + .rejects.toThrow('comment album media error: media not found'); + expect(bridge.sendRawPacket.mock.calls.some((call) => String(call[0]).endsWith('DoQunComment'))) + .toBe(false); + }); + + it('rejects a comment when the official feed lookup fails', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(commentMocks((cmd) => { + if (cmd.endsWith('GetQunFeedDetail')) { + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ + result: 10016, + errorText: '服务器繁忙', + })), + }; + } + return undefined; + })); + + await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) + .rejects.toThrow('fetch album feed error: retCode 10016, 服务器繁忙'); + expect(bridge.sendRawPacket.mock.calls.some((call) => String(call[0]).endsWith('DoQunComment'))) + .toBe(false); + }); + + it('rejects a comment when the official feed has no id', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(commentMocks((cmd) => { + if (cmd.endsWith('GetQunFeedDetail')) return packFeedDetail(''); + return undefined; + })); + + await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) + .rejects.toThrow('comment album media error: empty feed'); + expect(bridge.sendRawPacket.mock.calls.some((call) => String(call[0]).endsWith('DoQunComment'))) + .toBe(false); }); 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' } }] }); + bridge.sendRawPacket.mockImplementation(commentMocks((cmd) => { + if (cmd.endsWith('DoQunComment')) { + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ field1: 8527 })), + }; } - return { - success: true, - gotResponse: true, - errorCode: 0, - errorMessage: '', - responseData: Buffer.from(protobuf_encode({ field1: 8527 })), - }; - }); + return undefined; + })); await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) .rejects.toThrow('comment album media error: empty comment'); @@ -679,22 +798,22 @@ describe('apis/group-album', () => { 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' } }] }); + bridge.sendRawPacket.mockImplementation(commentMocks((cmd) => { + if (cmd.endsWith('DoQunComment')) { + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ + field1: 8527, + result: 1001, + errorText: 'permission denied', + })), + }; } - return { - success: true, - gotResponse: true, - errorCode: 0, - errorMessage: '', - responseData: Buffer.from(protobuf_encode({ - field1: 8527, - result: 1001, - errorText: 'permission denied', - })), - }; - }); + return undefined; + })); await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) .rejects.toThrow('comment album media error: retCode 1001, permission denied'); @@ -702,17 +821,17 @@ describe('apis/group-album', () => { 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' } }] }); + bridge.sendRawPacket.mockImplementation(commentMocks((cmd) => { + if (cmd.endsWith('DoQunComment')) { + return packCommentOk({ + id: 'flat-id', + content: [{ type: 0, content: 'hello' }], + time: 123n, + clientKey: 'ck', + }); } - return packCommentOk({ - id: 'flat-id', - content: [{ type: 0, content: 'hello' }], - time: 123n, - clientKey: 'ck', - }); - }); + return undefined; + })); await expect(new GroupAlbumApi(bridge as never).comment(12345, 'album-id', 'photo-lloc', 'hello')) .resolves.toMatchObject({ diff --git a/packages/proto-defs/src/oidb-actions/group-album.ts b/packages/proto-defs/src/oidb-actions/group-album.ts index ab2dadf5..df125233 100644 --- a/packages/proto-defs/src/oidb-actions/group-album.ts +++ b/packages/proto-defs/src/oidb-actions/group-album.ts @@ -160,8 +160,8 @@ export interface CommentReqPhotoInfo { batchId?: pb<5, uint_64>; } export interface CommentReqBodyHeader { - field3?: pb<3, uint_64>; - field4?: pb<4, string>; + time?: pb<3, uint_64>; + feedId?: pb<4, string>; } export interface CommentReqBodyUserWrap { field1?: pb<1, CommentUser>; @@ -259,6 +259,46 @@ export interface DoQunLikeResponse { field1?: pb<1, int_32>; body?: pb<4, DoQunLikeRespBody>; } +// QunAlbum.trpc.qzone.webapp_qun_feeds.FeedsReader.GetQunFeedDetail +// +// Official album comments look this feed up first (empty feed id + album / +// batch / lloc) and then copy the returned cell into DoQunComment. +export interface GetQunFeedDetailReqData { + groupId?: pb<2, string>; + feedId?: pb<3, string>; + commentCount?: pb<4, int_32>; + attachInfo?: pb<5, string>; + albumId?: pb<6, string>; + batchId?: pb<7, string>; + lloc?: pb<8, string>; +} +export interface GetQunFeedDetailRequest { + seq?: pb<1, int_32>; + field2?: pb_optional<2, bytes>; + field3?: pb_optional<3, bytes>; + data?: pb<4, GetQunFeedDetailReqData>; + traceId?: pb<5, string>; + extMap?: pb_repeated<10, ExtMapEntry>; +} +export interface QunFeedCellCommon { + time?: pb<3, uint_64>; + feedId?: pb<4, string>; +} +export interface QunFeed { + cellCommon?: pb<1, QunFeedCellCommon>; +} +export interface QunClientFeed { + feed?: pb<1, QunFeed>; +} +export interface GetQunFeedDetailRspData { + feed?: pb<2, QunClientFeed>; +} +export interface GetQunFeedDetailResponse { + seq?: pb<1, int_32>; + result?: pb<2, int_32>; + errorText?: pb<3, string>; + data?: pb<4, GetQunFeedDetailRspData>; +} export interface DeleteMediasReqBody { groupId?: pb<1, string>; albumId?: pb<2, string>;