mirror of
				https://github.com/KevinMidboe/immich.git
				synced 2025-10-29 17:40:28 +00:00 
			
		
		
		
	Show all albums an asset appears in on the asset viewer page (#575)
* Add route to query albums for a specific asset * Update API and add to detail-panel * Fix tests * Refactor API endpoint * Added alt attribute to img tag Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
		@@ -22,6 +22,7 @@ export interface IAlbumRepository {
 | 
			
		||||
  removeAssets(album: AlbumEntity, removeAssets: RemoveAssetsDto): Promise<AlbumEntity>;
 | 
			
		||||
  addAssets(album: AlbumEntity, addAssetsDto: AddAssetsDto): Promise<AlbumEntity>;
 | 
			
		||||
  updateAlbum(album: AlbumEntity, updateAlbumDto: UpdateAlbumDto): Promise<AlbumEntity>;
 | 
			
		||||
  getListByAssetId(userId: string, assetId: string): Promise<AlbumEntity[]>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const ALBUM_REPOSITORY = 'ALBUM_REPOSITORY';
 | 
			
		||||
@@ -149,6 +150,31 @@ export class AlbumRepository implements IAlbumRepository {
 | 
			
		||||
    return albums;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  async getListByAssetId(userId: string, assetId: string): Promise<AlbumEntity[]> {
 | 
			
		||||
    let query = this.albumRepository.createQueryBuilder('album');
 | 
			
		||||
 | 
			
		||||
    const albums = await query
 | 
			
		||||
        .where('album.ownerId = :ownerId', { ownerId: userId })
 | 
			
		||||
        .andWhere((qb) => {
 | 
			
		||||
          // shared with userId
 | 
			
		||||
          const subQuery = qb
 | 
			
		||||
              .subQuery()
 | 
			
		||||
              .select('assetAlbum.albumId')
 | 
			
		||||
              .from(AssetAlbumEntity, 'assetAlbum')
 | 
			
		||||
              .where('assetAlbum.assetId = :assetId', {assetId: assetId})
 | 
			
		||||
              .getQuery();
 | 
			
		||||
          return `album.id IN ${subQuery}`;
 | 
			
		||||
        })
 | 
			
		||||
        .leftJoinAndSelect('album.assets', 'assets')
 | 
			
		||||
        .leftJoinAndSelect('assets.assetInfo', 'assetInfo')
 | 
			
		||||
        .leftJoinAndSelect('album.sharedUsers', 'sharedUser')
 | 
			
		||||
        .leftJoinAndSelect('sharedUser.userInfo', 'userInfo')
 | 
			
		||||
        .orderBy('"assetInfo"."createdAt"::timestamptz', 'ASC')
 | 
			
		||||
        .getMany();
 | 
			
		||||
 | 
			
		||||
    return albums;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  async get(albumId: string): Promise<AlbumEntity | undefined> {
 | 
			
		||||
    let query = this.albumRepository.createQueryBuilder('album');
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -116,6 +116,7 @@ describe('Album service', () => {
 | 
			
		||||
      removeAssets: jest.fn(),
 | 
			
		||||
      removeUser: jest.fn(),
 | 
			
		||||
      updateAlbum: jest.fn(),
 | 
			
		||||
      getListByAssetId: jest.fn()
 | 
			
		||||
    };
 | 
			
		||||
    sut = new AlbumService(albumRepositoryMock);
 | 
			
		||||
  });
 | 
			
		||||
 
 | 
			
		||||
@@ -48,8 +48,11 @@ export class AlbumService {
 | 
			
		||||
   * @returns All Shared Album And Its Members
 | 
			
		||||
   */
 | 
			
		||||
  async getAllAlbums(authUser: AuthUserDto, getAlbumsDto: GetAlbumsDto): Promise<AlbumResponseDto[]> {
 | 
			
		||||
    if (typeof getAlbumsDto.assetId === 'string') {
 | 
			
		||||
      const albums = await this._albumRepository.getListByAssetId(authUser.id, getAlbumsDto.assetId);
 | 
			
		||||
      return albums.map(mapAlbumExcludeAssetInfo);
 | 
			
		||||
    }
 | 
			
		||||
    const albums = await this._albumRepository.getList(authUser.id, getAlbumsDto);
 | 
			
		||||
 | 
			
		||||
    return albums.map((album) => mapAlbumExcludeAssetInfo(album));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -18,4 +18,11 @@ export class GetAlbumsDto {
 | 
			
		||||
   * undefined: shared and owned albums
 | 
			
		||||
   */
 | 
			
		||||
  shared?: boolean;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Only returns albums that contain the asset
 | 
			
		||||
   * Ignores the shared parameter
 | 
			
		||||
   * undefined: get all albums
 | 
			
		||||
   */
 | 
			
		||||
  assetId?: string;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user