This commit is contained in:
mertalev 2025-10-14 13:17:58 -04:00
parent 4ecf62d13f
commit b44b24299c
No known key found for this signature in database
GPG key ID: DF6ABC77AAD98C95
3 changed files with 25 additions and 15 deletions

View file

@ -407,3 +407,20 @@ from
where
"album_asset"."albumsId" = $1
and "album_asset"."assetsId" in ($2)
-- AlbumRepository.getContributorCounts
select
"asset"."ownerId" as "userId",
count(*) as "assetCount"
from
"album_asset"
inner join "asset" on "asset"."id" = "assetsId"
where
"asset"."deletedAt" is null
and "album_asset"."albumsId" = $1
group by
"asset"."ownerId"
having
count(*) > 0
order by
"assetCount" desc

View file

@ -385,17 +385,17 @@ export class AlbumRepository {
* Excludes deleted assets, orders by count desc.
*/
@GenerateSql({ params: [DummyValue.UUID] })
async getContributorCountsForId(id: string): Promise<{ userId: string; assetCount: number }[]> {
async getContributorCounts(id: string) {
return this.db
.selectFrom('album_asset')
.innerJoin('asset', (join) =>
join.onRef('album_asset.assetsId', '=', 'asset.id').on('asset.deletedAt', 'is', null),
)
.innerJoin('asset', 'asset.id', 'assetsId')
.where('asset.deletedAt', 'is', sql.lit(null))
.where('album_asset.albumsId', '=', id)
.select('asset.ownerId as userId')
.select((eb) => sql<number>`${eb.fn.count('asset.id')}::int`.as('assetCount'))
.select((eb) => eb.fn.countAll<number>().as('assetCount'))
.groupBy('asset.ownerId')
.orderBy(sql`count(asset.id)`, 'desc')
.having((eb) => eb(eb.fn.countAll<number>(), '>', sql.lit(0)))
.orderBy('assetCount', 'desc')
.execute();
}
}

View file

@ -84,21 +84,14 @@ export class AlbumService extends BaseService {
const hasSharedLink = (album.sharedLinks?.length ?? 0) > 0;
const isShared = hasSharedUsers || hasSharedLink;
// Fetch contributor counts only for shared albums
let contributorCounts: { userId: string; assetCount: number }[] | undefined;
if (isShared) {
const rows = await this.albumRepository.getContributorCountsForId(album.id);
// Hide zeros as per requirements
contributorCounts = rows.filter(({ assetCount }) => assetCount > 0);
}
return {
...mapAlbum(album, withAssets, auth),
startDate: albumMetadataForIds?.startDate ?? undefined,
endDate: albumMetadataForIds?.endDate ?? undefined,
assetCount: albumMetadataForIds?.assetCount ?? 0,
lastModifiedAssetTimestamp: albumMetadataForIds?.lastModifiedAssetTimestamp ?? undefined,
...(contributorCounts && contributorCounts.length > 0 ? { contributorCounts } : {}),
// Fetch contributor counts only for shared albums
contributorCounts: isShared ? await this.albumRepository.getContributorCounts(album.id) : undefined,
};
}