Skip to content

Commit 5f4e852

Browse files
committed
squash
1 parent da45a81 commit 5f4e852

60 files changed

Lines changed: 762 additions & 514 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/Actions/Album/Merge.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
namespace App\Actions\Album;
1010

11+
use App\Constants\PhotoAlbum as PA;
1112
use App\Exceptions\Internal\QueryBuilderException;
1213
use App\Exceptions\ModelDBException;
1314
use App\Models\Album;
14-
use App\Models\Photo;
1515
use Illuminate\Database\Eloquent\ModelNotFoundException;
1616
use Illuminate\Support\Collection;
17+
use Illuminate\Support\Facades\DB;
1718

1819
class Merge
1920
{
@@ -27,10 +28,28 @@ class Merge
2728
*/
2829
public function do(Album $target_album, Collection $albums): void
2930
{
30-
// Merge photos of source albums into target
31-
Photo::query()
32-
->whereIn('album_id', $albums->pluck('id'))
33-
->update(['album_id' => $target_album->id]);
31+
$origin_ids = $albums->pluck('id')->all();
32+
33+
// Select all photos ids of the source albums
34+
$photos_ids = DB::table(PA::PHOTO_ALBUM)
35+
->whereIn(PA::ALBUM_ID, $origin_ids)
36+
->pluck(PA::PHOTO_ID)->all();
37+
38+
// Delete the existing links at destination (avoid duplicates key contraint)
39+
DB::table(PA::PHOTO_ALBUM)
40+
->whereIn(PA::PHOTO_ID, $photos_ids)
41+
->where(PA::ALBUM_ID, '=', $target_album->id)
42+
->delete();
43+
44+
// Insert the new links
45+
DB::table(PA::PHOTO_ALBUM)
46+
->insert(array_map(fn (string $id) => ['photo_id' => $id, 'album_id' => $target_album->id], $photos_ids));
47+
48+
// Delete the existing links from the origins
49+
DB::table(PA::PHOTO_ALBUM)
50+
->whereIn(PA::PHOTO_ID, $photos_ids)
51+
->whereIn(PA::ALBUM_ID, $origin_ids)
52+
->delete();
3453

3554
// Merge sub-albums of source albums into target
3655
/** @var Album $album */

app/Actions/Album/PositionData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use App\Enum\SizeVariantType;
1313
use App\Http\Resources\Collections\PositionDataResource;
1414
use App\Models\Album;
15-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
15+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1616
use Illuminate\Database\Eloquent\Relations\HasMany;
1717

1818
class PositionData
@@ -25,7 +25,7 @@ public function get(AbstractAlbum $album, bool $include_sub_albums = false): Pos
2525

2626
$photo_relation
2727
->with([
28-
'album' => function (BelongsTo $b): void {
28+
'albums' => function (BelongsToMany $b): void {
2929
// The album is required for photos to properly
3030
// determine access and visibility rights; but we
3131
// don't need to determine the cover and thumbnail for
@@ -46,6 +46,6 @@ public function get(AbstractAlbum $album, bool $include_sub_albums = false): Pos
4646
->whereNotNull('latitude')
4747
->whereNotNull('longitude');
4848

49-
return new PositionDataResource($album->get_id(), $album->get_title(), $photo_relation->get(), $album instanceof Album ? $album->track_url : null);
49+
return new PositionDataResource($album, $photo_relation->get(), $album instanceof Album ? $album->track_url : null);
5050
}
5151
}

app/Actions/Albums/PositionData.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public function do(): PositionDataResource
3838
$photo_query = $this->photo_query_policy->applySearchabilityFilter(
3939
query: Photo::query()
4040
->with([
41-
'album' => function ($b): void {
41+
// TODO: FIX ME.
42+
'albums' => function ($b): void {
4243
// The album is required for photos to properly
4344
// determine access and visibility rights; but we
4445
// don't need to determine the cover and thumbnail for
@@ -62,6 +63,6 @@ public function do(): PositionDataResource
6263
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_map')
6364
);
6465

65-
return new PositionDataResource(null, null, $photo_query->get(), null);
66+
return new PositionDataResource(null, $photo_query->get(), null);
6667
}
6768
}

app/Actions/Diagnostics/Pipes/Checks/DBIntegrityCheck.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function handle(array &$data, \Closure $next): array
3333

3434
$sub_join = DB::table('size_variants')->where('size_variants.type', '=', 0);
3535
$photos = Photo::query()
36-
->with(['album'])
36+
->with(['albums'])
3737
->select(['photos.id', 'title', 'album_id'])
3838
->joinSub($sub_join, 'size_variants', 'size_variants.photo_id', '=', 'photos.id', 'left')
3939
->whereNull('size_variants.id')
4040
->get();
4141

4242
foreach ($photos as $photo) {
43-
$data[] = DiagnosticData::error('Photo without Original found -- ' . $photo->title . ' in ' . ($photo->album?->title ?? __('gallery.smart_album.unsorted')), self::class);
43+
$data[] = DiagnosticData::error('Photo without Original found -- ' . $photo->title . ' in ' . ($photo->albums?->first()?->title ?? __('gallery.smart_album.unsorted')), self::class);
4444
}
4545

4646
return $next($data);

app/Actions/Photo/Create.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ private function handleDuplicate(InitDTO $init_dto): Photo
139139
$pipes[] = Duplicate\SaveIfDirty::class;
140140
}
141141
$pipes[] = Duplicate\ThrowSkipDuplicate::class;
142-
$pipes[] = Duplicate\ReplicateAsPhoto::class;
143142
$pipes[] = Shared\SetStarred::class;
144-
$pipes[] = Shared\SetParentAndOwnership::class;
145143
$pipes[] = Shared\Save::class;
144+
$pipes[] = Shared\SetParent::class;
146145
$pipes[] = Shared\SaveStatistics::class;
147146
$pipes[] = Shared\NotifyAlbums::class;
148147

@@ -167,14 +166,15 @@ private function handleStandalone(InitDTO $init_dto): Photo
167166
Standalone\InitNamingStrategy::class,
168167
Shared\HydrateMetadata::class,
169168
Shared\SetStarred::class,
170-
Shared\SetParentAndOwnership::class,
169+
Shared\SetOwnership::class,
171170
Standalone\SetOriginalChecksum::class,
172171
Standalone\FetchSourceImage::class,
173172
Standalone\ExtractGoogleMotionPictures::class,
174173
Standalone\PlacePhoto::class,
175174
Standalone\PlaceGoogleMotionVideo::class,
176175
Standalone\SetChecksum::class,
177176
Shared\Save::class,
177+
Shared\SetParent::class,
178178
Shared\SaveStatistics::class,
179179
Standalone\CreateOriginalSizeVariant::class,
180180
Standalone\CreateSizeVariants::class,
@@ -257,14 +257,15 @@ private function handlePhotoLivePartner(InitDTO $init_dto): Photo
257257
Standalone\InitNamingStrategy::class,
258258
Shared\HydrateMetadata::class,
259259
Shared\SetStarred::class,
260-
Shared\SetParentAndOwnership::class,
260+
Shared\SetOwnership::class,
261261
Standalone\SetOriginalChecksum::class,
262262
Standalone\FetchSourceImage::class,
263263
Standalone\ExtractGoogleMotionPictures::class,
264264
Standalone\PlacePhoto::class,
265265
Standalone\PlaceGoogleMotionVideo::class,
266266
Standalone\SetChecksum::class,
267267
Shared\Save::class,
268+
Shared\SetParent::class,
268269
Standalone\CreateOriginalSizeVariant::class,
269270
Standalone\CreateSizeVariants::class,
270271
Standalone\EncodePlaceholder::class,

app/Actions/Photo/Delete.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace App\Actions\Photo;
1010

11+
use App\Constants\PhotoAlbum as PA;
1112
use App\Enum\SizeVariantType;
1213
use App\Exceptions\Internal\LycheeAssertionError;
1314
use App\Exceptions\Internal\QueryBuilderException;
@@ -19,6 +20,7 @@
1920
use App\Models\Statistics;
2021
use Illuminate\Database\Query\Builder as BaseBuilder;
2122
use Illuminate\Database\Query\JoinClause;
23+
use Illuminate\Support\Facades\DB;
2224

2325
/**
2426
* Deletes the photos with the designated IDs **efficiently**.
@@ -159,13 +161,15 @@ private function collectSizeVariantPathsByAlbumID(array $album_ids): void
159161
$size_variants = SizeVariant::query()
160162
->from('size_variants as sv')
161163
->select(['sv.short_path', 'sv.storage_disk'])
164+
->leftJoin(PA::PHOTO_ALBUM, PA::PHOTO_ID, '=', 'sv.photo_id')
162165
->join('photos as p', 'p.id', '=', 'sv.photo_id')
163-
->leftJoin('photos as dup', function (JoinClause $join) use ($album_ids): void {
164-
$join
165-
->on('dup.checksum', '=', 'p.checksum')
166-
->whereNotIn('dup.album_id', $album_ids);
167-
})
168-
->whereIn('p.album_id', $album_ids)
166+
->joinSub(DB::table('photos')->leftJoin(PA::PHOTO_ALBUM, 'photos.id', '=', PA::PHOTO_ID)->select('id', 'checksum', 'album_id'), 'dup',
167+
function (JoinClause $join) use ($album_ids): void {
168+
$join
169+
->on('dup.checksum', '=', 'p.checksum')
170+
->whereNotIn('dup.album_id', $album_ids);
171+
}, 'left')
172+
->whereIn(PA::ALBUM_ID, $album_ids)
169173
->whereNull('dup.id')
170174
->get();
171175
$this->fileDeleter->addSizeVariants($size_variants);
@@ -253,12 +257,14 @@ private function collectLivePhotoPathsByAlbumID(array $album_ids)
253257
->on('sv.photo_id', '=', 'p.id')
254258
->where('sv.type', '=', SizeVariantType::ORIGINAL);
255259
})
256-
->leftJoin('photos as dup', function (JoinClause $join) use ($album_ids): void {
257-
$join
258-
->on('dup.live_photo_checksum', '=', 'p.live_photo_checksum')
259-
->whereNotIn('dup.album_id', $album_ids);
260-
})
261-
->whereIn('p.album_id', $album_ids)
260+
->joinSub(DB::table('photos')->leftJoin(PA::PHOTO_ALBUM, 'photos.id', '=', PA::PHOTO_ID)->select('id', 'live_photo_checksum', 'album_id'), 'dup',
261+
function (JoinClause $join) use ($album_ids): void {
262+
$join
263+
->on('dup.live_photo_checksum', '=', 'p.live_photo_checksum')
264+
->whereNotIn('dup.album_id', $album_ids);
265+
}, 'left')
266+
->leftJoin(PA::PHOTO_ALBUM, PA::PHOTO_ID, '=', 'p.id')
267+
->whereIn(PA::ALBUM_ID, $album_ids)
262268
->whereNull('dup.id')
263269
->whereNotNull('p.live_photo_short_path')
264270
->get(['p.live_photo_short_path', 'sv.storage_disk']);
@@ -301,7 +307,8 @@ private function deleteDBRecords(array $photo_ids, array $album_ids): void
301307
$query
302308
->from('photos', 'p')
303309
->whereColumn('p.id', '=', 'size_variants.photo_id')
304-
->whereIn('p.album_id', $album_ids);
310+
->leftJoin(PA::PHOTO_ALBUM, PA::PHOTO_ID, '=', 'p.id')
311+
->whereIn(PA::ALBUM_ID, $album_ids);
305312
})
306313
->delete();
307314
}
@@ -316,10 +323,17 @@ private function deleteDBRecords(array $photo_ids, array $album_ids): void
316323
$query
317324
->from('photos', 'p')
318325
->whereColumn('p.id', '=', 'statistics.photo_id')
319-
->whereIn('p.album_id', $album_ids);
326+
->leftJoin(PA::PHOTO_ALBUM, PA::PHOTO_ID, '=', 'p.id')
327+
->whereIn(PA::ALBUM_ID, $album_ids);
320328
})
321329
->delete();
322330
}
331+
if (count($photo_ids) !== 0) {
332+
DB::table(PA::PHOTO_ALBUM)->whereIn(PA::PHOTO_ID, $photo_ids)->delete();
333+
}
334+
if (count($album_ids) !== 0) {
335+
DB::table(PA::PHOTO_ALBUM)->whereIn(PA::ALBUM_ID, $album_ids)->delete();
336+
}
323337
if (count($photo_ids) !== 0) {
324338
Photo::query()->whereIn('id', $photo_ids)->delete();
325339
}

app/Actions/Photo/Duplicate.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

app/Actions/Photo/DuplicateFinder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace App\Actions\Photo;
1010

11+
use App\Constants\PhotoAlbum as PA;
1112
use App\Exceptions\Internal\LycheeLogicException;
1213
use App\Exceptions\Internal\QueryBuilderException;
1314
use App\Models\Photo;
@@ -77,7 +78,8 @@ private function query(
7778
}
7879

7980
return Photo::query()
80-
->join('base_albums', 'base_albums.id', '=', 'photos.album_id')
81+
->join(PA::PHOTO_ALBUM, PA::PHOTO_ID, '=', 'photos.id')
82+
->join('base_albums', 'base_albums.id', '=', PA::ALBUM_ID)
8183
->join(
8284
'size_variants', 'size_variants.photo_id', '=', 'photos.id', 'left'
8385
)
@@ -104,12 +106,14 @@ private function getDuplicatesIdsQuery(
104106
bool $must_have_same_title,
105107
): Builder {
106108
return DB::table('photos', 'p1')->select('p1.id')
109+
->joinSub(DB::table(PA::PHOTO_ALBUM), 'pa1', 'pa1.photo_id', '=', 'p1.id', 'left')
107110
->join(
108111
'photos as p2',
109112
fn ($join) => $join->on('p1.id', '<>', 'p2.id')
113+
->joinSub(DB::table(PA::PHOTO_ALBUM), 'pa2', 'pa2.photo_id', '=', 'p2.id', 'left')
110114
->when($must_have_same_title, fn ($q) => $q->on('p1.title', '=', 'p2.title'))
111115
->when($must_have_same_checksum, fn ($q) => $q->on('p1.checksum', '=', 'p2.checksum'))
112-
->when($must_be_within_same_album, fn ($q) => $q->on('p1.album_id', '=', 'p2.album_id'))
116+
->when($must_be_within_same_album, fn ($q) => $q->on('pa1.album_id', '=', 'pa2.album_id'))
113117
);
114118
}
115119
}

app/Actions/Photo/Move.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)