Skip to content

Commit

Permalink
fix(server): assets in multiple albums duplicated in map view (immich…
Browse files Browse the repository at this point in the history
  • Loading branch information
mertalev authored Feb 21, 2025
1 parent 3925445 commit 6169052
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
13 changes: 11 additions & 2 deletions server/src/queries/map.repository.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ from
inner join "exif" on "assets"."id" = "exif"."assetId"
and "exif"."latitude" is not null
and "exif"."longitude" is not null
left join "albums_assets_assets" on "assets"."id" = "albums_assets_assets"."assetsId"
where
"isVisible" = $1
and "deletedAt" is null
and "ownerId" in ($2)
and (
"ownerId" in ($2)
or exists (
select
from
"albums_assets_assets"
where
"assets"."id" = "albums_assets_assets"."assetsId"
and "albums_assets_assets"."albumsId" in ($3)
)
)
order by
"fileCreatedAt" desc
21 changes: 0 additions & 21 deletions server/src/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
withTags,
} from 'src/entities/asset.entity';
import { AssetFileType, AssetOrder, AssetStatus, AssetType } from 'src/enum';
import { MapMarker, MapMarkerSearchOptions } from 'src/repositories/map.repository';
import { AssetSearchOptions, SearchExploreItem, SearchExploreItemSet } from 'src/repositories/search.repository';
import { anyUuid, asUuid, mapUpsertColumns } from 'src/utils/database';
import { Paginated, PaginationOptions, paginationHelper } from 'src/utils/pagination';
Expand Down Expand Up @@ -639,26 +638,6 @@ export class AssetRepository {
.executeTakeFirst() as Promise<AssetEntity | undefined>;
}

private getMapMarkers(ownerIds: string[], options: MapMarkerSearchOptions = {}): Promise<MapMarker[]> {
const { isArchived, isFavorite, fileCreatedAfter, fileCreatedBefore } = options;

return this.db
.selectFrom('assets')
.leftJoin('exif', 'assets.id', 'exif.assetId')
.select(['id', 'latitude as lat', 'longitude as lon', 'city', 'state', 'country'])
.where('ownerId', '=', anyUuid(ownerIds))
.where('latitude', 'is not', null)
.where('longitude', 'is not', null)
.where('isVisible', '=', true)
.where('deletedAt', 'is', null)
.$if(!!isArchived, (qb) => qb.where('isArchived', '=', isArchived!))
.$if(!!isFavorite, (qb) => qb.where('isFavorite', '=', isFavorite!))
.$if(!!fileCreatedAfter, (qb) => qb.where('fileCreatedAt', '>=', fileCreatedAfter!))
.$if(!!fileCreatedBefore, (qb) => qb.where('fileCreatedAt', '<=', fileCreatedBefore!))
.orderBy('fileCreatedAt', 'desc')
.execute() as Promise<MapMarker[]>;
}

getStatistics(ownerId: string, { isArchived, isFavorite, isTrashed }: AssetStatsOptions): Promise<AssetStats> {
return this.db
.selectFrom('assets')
Expand Down
18 changes: 12 additions & 6 deletions server/src/repositories/map.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class MapRepository {
this.logger.log('Geodata import completed');
}

@GenerateSql({ params: [[DummyValue.UUID], []] })
@GenerateSql({ params: [[DummyValue.UUID], [DummyValue.UUID]] })
getMapMarkers(ownerIds: string[], albumIds: string[], options: MapMarkerSearchOptions = {}) {
const { isArchived, isFavorite, fileCreatedAfter, fileCreatedBefore } = options;

Expand All @@ -89,25 +89,31 @@ export class MapRepository {
.on('exif.longitude', 'is not', null),
)
.select(['id', 'exif.latitude as lat', 'exif.longitude as lon', 'exif.city', 'exif.state', 'exif.country'])
.leftJoin('albums_assets_assets', (join) => join.onRef('assets.id', '=', 'albums_assets_assets.assetsId'))
.where('isVisible', '=', true)
.$if(isArchived !== undefined, (q) => q.where('isArchived', '=', isArchived!))
.$if(isFavorite !== undefined, (q) => q.where('isFavorite', '=', isFavorite!))
.$if(fileCreatedAfter !== undefined, (q) => q.where('fileCreatedAt', '>=', fileCreatedAfter!))
.$if(fileCreatedBefore !== undefined, (q) => q.where('fileCreatedAt', '<=', fileCreatedBefore!))
.where('deletedAt', 'is', null)
.where((builder) => {
.where((eb) => {
const expression: Expression<SqlBool>[] = [];

if (ownerIds.length > 0) {
expression.push(builder('ownerId', 'in', ownerIds));
expression.push(eb('ownerId', 'in', ownerIds));
}

if (albumIds.length > 0) {
expression.push(builder('albums_assets_assets.albumsId', 'in', albumIds));
expression.push(
eb.exists((eb) =>
eb
.selectFrom('albums_assets_assets')
.whereRef('assets.id', '=', 'albums_assets_assets.assetsId')
.where('albums_assets_assets.albumsId', 'in', albumIds),
),
);
}

return builder.or(expression);
return eb.or(expression);
})
.orderBy('fileCreatedAt', 'desc')
.execute() as Promise<MapMarker[]>;
Expand Down

0 comments on commit 6169052

Please sign in to comment.