diff --git a/packages/core/src/bridge/apis/group-album.ts b/packages/core/src/bridge/apis/group-album.ts index b4f69804..b7d30825 100644 --- a/packages/core/src/bridge/apis/group-album.ts +++ b/packages/core/src/bridge/apis/group-album.ts @@ -435,6 +435,7 @@ export class GroupAlbumApi { } async delete(groupId: number, albumId: string, lloc: string): Promise<{ success: true }> { + const target = await this.resolveDeleteTarget(groupId, albumId, lloc); const uin = this.ctx.identity.uin; const clientKey = `${uin}_${Date.now()}_${Math.floor(Math.random() * 100000)}`; @@ -445,7 +446,8 @@ export class GroupAlbumApi { body: { groupId: groupId.toString(), albumId, - lloc, + mediaIds: [target.mediaId], + ...(target.batchId ? { batchIds: [target.batchId] } : {}), }, traceId: clientKey, extMap: [{ key: 'fc-appid', value: '100' }], @@ -472,7 +474,70 @@ export class GroupAlbumApi { return { success: true }; } + + /** + * Official delete uses a photo lloc, or a video's cover lloc plus batch id. + * Callers may pass either an image lloc or a video id; resolve the latter. + */ + private async resolveDeleteTarget( + groupId: number, + albumId: string, + mediaKey: string, + ): Promise<{ mediaId: string; batchId?: string }> { + try { + const seenCursors = new Set(); + let attachInfo = ''; + while (true) { + const page = await this.getMediaList(groupId, albumId, attachInfo); + const hit = findDeleteTarget(page.mediaList, mediaKey); + if (hit) return hit; + + const next = page.nextAttachInfo ?? ''; + if (!next || seenCursors.has(next)) { + return { mediaId: mediaKey }; + } + seenCursors.add(next); + attachInfo = next; + } + } catch (err) { + log.debug( + 'resolve album delete target failed, using raw id: group=%d album=%s err=%s', + groupId, + albumId, + err instanceof Error ? err.message : String(err), + ); + return { mediaId: mediaKey }; + } + } } + +interface AlbumDeleteMediaItem { + image?: { lloc?: string } | null; + video?: { id?: string; cover?: { lloc?: string } | null } | null; + batchId?: string | number; +} + +function findDeleteTarget( + mediaList: ReadonlyArray, + mediaKey: string, +): { mediaId: string; batchId?: string } | undefined { + for (const raw of mediaList) { + if (!raw || typeof raw !== 'object') continue; + const item = raw as AlbumDeleteMediaItem; + const batchId = item.batchId === undefined || item.batchId === '' + ? undefined + : String(item.batchId); + if (item.image?.lloc === mediaKey) { + return { mediaId: mediaKey, batchId }; + } + const coverLloc = item.video?.cover?.lloc; + if (item.video?.id === mediaKey || coverLloc === mediaKey) { + return { mediaId: coverLloc || mediaKey, batchId }; + } + } + return undefined; +} + 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 b4eef030..fd3459e6 100644 --- a/packages/core/tests/actions/group-album.test.ts +++ b/packages/core/tests/actions/group-album.test.ts @@ -1,5 +1,7 @@ import { describe, expect, it } from 'vitest'; import type { + DeleteMediasRequest, + DeleteMediasResponse, GetAlbumListResponse, GetMediaListResponse, } from '@snowluma/proto-defs/oidb-actions/group-album'; @@ -437,4 +439,128 @@ describe('apis/group-album', () => { nextAttachInfo: 'next-video-page', }); }); + + function packDeleteOk() { + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ field1: 8694 })), + }; + } + + function packMediaList(body: GetMediaListResponse['data']) { + return { + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: Buffer.from(protobuf_encode({ data: body })), + }; + } + + it('deletes a photo by lloc and includes the batch id', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(async (cmd: string) => { + if (cmd.endsWith('GetMediaList')) { + return packMediaList({ + mediaList: [{ type: 1, image: { lloc: 'photo-lloc' }, batchId: 77n }], + }); + } + return packDeleteOk(); + }); + + await expect(new GroupAlbumApi(bridge as never).delete(12345, 'album-id', 'photo-lloc')) + .resolves.toEqual({ success: true }); + + const deleteCall = bridge.sendRawPacket.mock.calls.find((call) => + String(call[0]).endsWith('DeleteMedias'), + ); + expect(deleteCall?.[0]).toBe('QunAlbum.trpc.qzone.webapp_qun_media.QunMedia.DeleteMedias'); + const request = protobuf_decode(deleteCall![1] as Uint8Array); + expect(request.body).toMatchObject({ + groupId: '12345', + albumId: 'album-id', + mediaIds: ['photo-lloc'], + batchIds: ['77'], + }); + }); + + it('deletes a video by video id using the cover lloc', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(async (cmd: string) => { + if (cmd.endsWith('GetMediaList')) { + return packMediaList({ + mediaList: [{ + type: 2, + video: { id: 'video-id', cover: { lloc: 'cover-large' } }, + batchId: 88n, + }], + }); + } + return packDeleteOk(); + }); + + await expect(new GroupAlbumApi(bridge as never).delete(12345, 'album-id', 'video-id')) + .resolves.toEqual({ success: true }); + + const deleteCall = bridge.sendRawPacket.mock.calls.find((call) => + String(call[0]).endsWith('DeleteMedias'), + ); + const request = protobuf_decode(deleteCall![1] as Uint8Array); + expect(request.body?.mediaIds).toEqual(['cover-large']); + expect(request.body?.batchIds).toEqual(['88']); + }); + + it('pages the media list until the video is found', async () => { + const bridge = mockBridge(); + let pages = 0; + bridge.sendRawPacket.mockImplementation(async (cmd: string) => { + if (cmd.endsWith('GetMediaList')) { + pages += 1; + if (pages === 1) { + return packMediaList({ + mediaList: [{ type: 1, image: { lloc: 'other' }, batchId: 1n }], + nextAttachInfo: 'page-2', + }); + } + return packMediaList({ + mediaList: [{ + type: 2, + video: { id: 'video-id', cover: { lloc: 'cover-2' } }, + batchId: 2n, + }], + }); + } + return packDeleteOk(); + }); + + await new GroupAlbumApi(bridge as never).delete(12345, 'album-id', 'video-id'); + expect(pages).toBe(2); + const deleteCall = bridge.sendRawPacket.mock.calls.find((call) => + String(call[0]).endsWith('DeleteMedias'), + ); + const request = protobuf_decode(deleteCall![1] as Uint8Array); + expect(request.body?.mediaIds).toEqual(['cover-2']); + expect(request.body?.batchIds).toEqual(['2']); + }); + + it('falls back to the raw id when the media list has no match', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockImplementation(async (cmd: string) => { + if (cmd.endsWith('GetMediaList')) { + return packMediaList({ mediaList: [] }); + } + return packDeleteOk(); + }); + + await new GroupAlbumApi(bridge as never).delete(12345, 'album-id', 'unknown-id'); + const deleteCall = bridge.sendRawPacket.mock.calls.find((call) => + String(call[0]).endsWith('DeleteMedias'), + ); + const request = protobuf_decode(deleteCall![1] as Uint8Array); + expect(request.body?.mediaIds).toEqual(['unknown-id']); + expect(request.body?.batchIds ?? []).toEqual([]); + }); }); diff --git a/packages/proto-defs/src/oidb-actions/group-album.ts b/packages/proto-defs/src/oidb-actions/group-album.ts index 326ba4d9..730ef8ec 100644 --- a/packages/proto-defs/src/oidb-actions/group-album.ts +++ b/packages/proto-defs/src/oidb-actions/group-album.ts @@ -278,7 +278,10 @@ export interface DoQunLikeResponse { export interface DeleteMediasReqBody { groupId?: pb<1, string>; albumId?: pb<2, string>; - lloc?: pb<3, string>; + // deleteMedias 4th arg: photo lloc, or a video's cover lloc. + mediaIds?: pb_repeated<3, string>; + // deleteMedias 5th arg: matching batch ids. Android always sends these. + batchIds?: pb_repeated<4, string>; } export interface DeleteMediasRequest { field1?: pb<1, int_32>;