Skip to content

Commit 890b9e6

Browse files
committed
more work
1 parent 66917ea commit 890b9e6

6 files changed

Lines changed: 35 additions & 21 deletions

File tree

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, '=', 'p.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/Models/Builders/AlbumBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getModels($columns = ['*']): array
7070
$count_photos = DB::table('photos', 'p')
7171
->join(PA::PHOTO_ALBUM, 'p.id', '=', PA::PHOTO_ID)
7272
->selectRaw('COUNT(*)')
73-
->whereColumn('photo_album.album_id', '=', 'albums.id');
73+
->whereColumn(PA::ALBUM_ID, '=', 'albums.id');
7474

7575
$this->addSelect([
7676
'min_taken_at' => $this->getTakenAtSQL()->selectRaw('MIN(taken_at)'),

app/Models/Photo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
use App\Models\Extensions\ToArrayThrowsNotImplemented;
3333
use App\Models\Extensions\UTCBasedTimes;
3434
use App\Relations\HasManySizeVariants;
35+
use Illuminate\Database\Eloquent\Collection;
3536
use Illuminate\Database\Eloquent\Factories\HasFactory;
3637
use Illuminate\Database\Eloquent\Model;
37-
use Illuminate\Database\Eloquent\Collection;
3838
use Illuminate\Database\Eloquent\Relations\BelongsTo;
3939
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
4040
use Illuminate\Database\Eloquent\Relations\HasOne;

app/Policies/PhotoPolicy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ public function canDeleteById(User $user, array $photo_ids): bool
228228
// If there are any photos which are not in albums at this point, we fail.
229229
if (
230230
Photo::query()
231-
->leftJoin(PA::PHOTO_ALBUM, 'id', '=', PA::PHOTO_ID)
231+
->leftJoin(PA::PHOTO_ALBUM, 'photos.id', '=', PA::PHOTO_ID)
232232
->whereNull('album_id')
233-
->whereIn('id', $photo_ids)
233+
->whereIn('photos.id', $photo_ids)
234234
->count() > 0
235235
) {
236236
return false;

app/SmartAlbums/BaseSmartAlbum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function photos(): Builder
116116
{
117117
$query = $this->photo_query_policy
118118
->applySearchabilityFilter(
119-
query: Photo::query()->leftJoin(PA::PHOTO_ALBUM, 'id', '=', PA::PHOTO_ID)->with(['size_variants', 'statistics']),
119+
query: Photo::query()->leftJoin(PA::PHOTO_ALBUM, 'photos.id', '=', PA::PHOTO_ID)->with(['size_variants', 'statistics']),
120120
origin: null,
121121
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_smart_albums')
122122
)->where($this->smart_photo_condition);

app/SmartAlbums/UnsortedAlbum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public static function getInstance(): self
4646
public function photos(): Builder
4747
{
4848
if ($this->public_permissions !== null) {
49-
return Photo::query()->leftJoin(PA::PHOTO_ALBUM, 'id', '=', PA::PHOTO_ID)->with(['size_variants', 'statistics'])
50-
->where($this->smart_photo_condition);
49+
return Photo::query()->leftJoin(PA::PHOTO_ALBUM, 'photos.id', '=', PA::PHOTO_ID)->with(['size_variants', 'statistics'])
50+
->where($this->smart_photo_condition);
5151
}
5252

5353
return parent::photos();

0 commit comments

Comments
 (0)