fix(group-album): fetch all legacy album pages
Follow AlbumService cursors in get_group_album_list so the legacy array response contains every album instead of only the first page. Reject missing or repeated cursors when the server reports more data, preventing silent truncation and pagination loops. Add regression coverage for multi-page aggregation and malformed cursors. Fixes #272
This commit is contained in:
2 files changed
+128
-2
No files matched your search
@@ -213,8 +213,42 @@ export class GroupAlbumApi {
|
||||
|
||||
// OneBot compatibility shape used by get_group_album_list.
|
||||
async list(groupId: number): Promise<GroupAlbumList> {
|
||||
const result = await this.fetchAlbumList(groupId, '');
|
||||
return result.albumList.map(toLegacyAlbum);
|
||||
const albumList: GroupAlbumList = [];
|
||||
const seenCursors = new Set<string>();
|
||||
let attachInfo = '';
|
||||
let page = 0;
|
||||
|
||||
while (true) {
|
||||
const result = await this.fetchAlbumList(groupId, attachInfo);
|
||||
page += 1;
|
||||
albumList.push(...result.albumList.map(toLegacyAlbum));
|
||||
|
||||
if (!result.hasMore) {
|
||||
log.debug(
|
||||
'get complete album list: group=%d pages=%d albums=%d',
|
||||
groupId,
|
||||
page,
|
||||
albumList.length,
|
||||
);
|
||||
return albumList;
|
||||
}
|
||||
|
||||
const nextAttachInfo = result.attachInfo;
|
||||
if (!nextAttachInfo) {
|
||||
throw new Error(
|
||||
`get group album list pagination failed: group=${groupId} page=${page} `
|
||||
+ 'hasMore=true but cursor is empty',
|
||||
);
|
||||
}
|
||||
if (seenCursors.has(nextAttachInfo)) {
|
||||
throw new Error(
|
||||
`get group album list pagination failed: group=${groupId} repeated cursor at page ${page}`,
|
||||
);
|
||||
}
|
||||
|
||||
seenCursors.add(nextAttachInfo);
|
||||
attachInfo = nextAttachInfo;
|
||||
}
|
||||
}
|
||||
|
||||
// 上传图片到现有相册(HTTP 分片上传)。
|
||||
|
||||
@@ -108,6 +108,98 @@ describe('apis/group-album', () => {
|
||||
}]);
|
||||
});
|
||||
|
||||
it('fetches every AlbumService page for the legacy album list action', async () => {
|
||||
const bridge = mockBridge();
|
||||
bridge.sendRawPacket
|
||||
.mockResolvedValueOnce({
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<GetAlbumListResponse>({
|
||||
data: {
|
||||
albumList: [{
|
||||
albumId: 'album-1',
|
||||
name: '第一页',
|
||||
createTime: 1n,
|
||||
uploadNumber: 1n,
|
||||
}],
|
||||
attachInfo: 'next-cursor',
|
||||
hasMore: true,
|
||||
},
|
||||
})),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<GetAlbumListResponse>({
|
||||
data: {
|
||||
albumList: [{
|
||||
albumId: 'album-2',
|
||||
name: '第二页',
|
||||
createTime: 2n,
|
||||
uploadNumber: 2n,
|
||||
}],
|
||||
hasMore: false,
|
||||
},
|
||||
})),
|
||||
});
|
||||
|
||||
const result = await new GroupAlbumApi(bridge as never).list(12345);
|
||||
|
||||
expect(result.map((album) => album.id)).toEqual(['album-1', 'album-2']);
|
||||
expect(bridge.sendRawPacket).toHaveBeenCalledTimes(2);
|
||||
const secondRequest = protobuf_decode<GroupAlbumListRequestWireOracle>(
|
||||
bridge.sendRawPacket.mock.calls[1]![1],
|
||||
);
|
||||
expect(secondRequest.data?.cursor).toBe('next-cursor');
|
||||
});
|
||||
|
||||
it('rejects a legacy album page that claims more data without a cursor', async () => {
|
||||
const bridge = mockBridge();
|
||||
bridge.sendRawPacket.mockResolvedValueOnce({
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<GetAlbumListResponse>({
|
||||
data: {
|
||||
albumList: [],
|
||||
hasMore: true,
|
||||
},
|
||||
})),
|
||||
});
|
||||
|
||||
await expect(new GroupAlbumApi(bridge as never).list(12345))
|
||||
.rejects.toThrow('hasMore=true but cursor is empty');
|
||||
});
|
||||
|
||||
it('rejects a repeated AlbumService cursor instead of looping or truncating', async () => {
|
||||
const bridge = mockBridge();
|
||||
const repeatedPage = {
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<GetAlbumListResponse>({
|
||||
data: {
|
||||
albumList: [],
|
||||
attachInfo: 'same-cursor',
|
||||
hasMore: true,
|
||||
},
|
||||
})),
|
||||
};
|
||||
bridge.sendRawPacket
|
||||
.mockResolvedValueOnce(repeatedPage)
|
||||
.mockResolvedValueOnce(repeatedPage);
|
||||
|
||||
await expect(new GroupAlbumApi(bridge as never).list(12345))
|
||||
.rejects.toThrow('repeated cursor at page 2');
|
||||
expect(bridge.sendRawPacket).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('returns the native AlbumService cursor and creator fields for get_qun_album_list', async () => {
|
||||
const bridge = mockBridge();
|
||||
bridge.sendRawPacket.mockResolvedValueOnce({
|
||||
|
||||
Reference in new issue
Block a user