Skip to content

Commit ab1aace

Browse files
committed
small progress
1 parent 06c4b69 commit ab1aace

8 files changed

Lines changed: 66 additions & 124 deletions

File tree

app/Actions/Photo/Create.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ 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\SetOwnership::class;
145143
$pipes[] = Shared\Save::class;
146144
$pipes[] = Shared\SetParent::class;
147145
$pipes[] = Shared\SaveStatistics::class;

app/Actions/Photo/Pipes/Duplicate/ReplicateAsPhoto.php

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

app/Actions/RSS/Generate.php

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,58 @@
88

99
namespace App\Actions\RSS;
1010

11+
use App\Constants\PhotoAlbum as PA;
1112
use App\Contracts\Exceptions\InternalLycheeException;
13+
use App\Enum\SizeVariantType;
1214
use App\Exceptions\Internal\FrameworkException;
1315
use App\Models\Configs;
16+
use App\Models\Extensions\HasUrlGenerator;
17+
use App\Models\Extensions\UTCBasedTimes;
1418
use App\Models\Photo;
1519
use App\Policies\PhotoQueryPolicy;
1620
use Carbon\Exceptions\InvalidFormatException;
1721
use Carbon\Exceptions\UnitException;
22+
use Illuminate\Contracts\Container\BindingResolutionException;
1823
use Illuminate\Support\Carbon;
1924
use Illuminate\Support\Collection;
25+
use Illuminate\Support\Facades\DB;
2026
use Spatie\Feed\FeedItem;
2127

28+
/**
29+
* @template T of object{id:string,title:string,description:?string,type:string,created_at:string,updated_at:string,album_id:string,album_title:string,short_path:string,filesize:int,storage_disk:string,size_variant_type:string,username:string}
30+
*
31+
* @package App\Actions\RSS
32+
*/
2233
class Generate
2334
{
35+
use HasUrlGenerator;
36+
use UTCBasedTimes;
37+
2438
public function __construct(
2539
protected PhotoQueryPolicy $photo_query_policy)
2640
{
2741
}
2842

29-
private function create_link_to_page(Photo $photo_model): string
30-
{
31-
if ($photo_model->album_id !== null) {
32-
return url('/gallery/' . $photo_model->album_id . '/' . $photo_model->id);
33-
}
34-
35-
return url('/view?p=' . $photo_model->id);
36-
}
37-
38-
private function toFeedItem(Photo $photo_model): FeedItem
43+
/**
44+
*
45+
* @param T $data
46+
* @return FeedItem
47+
* @throws BindingResolutionException
48+
*/
49+
private function toFeedItem(object $data): FeedItem
3950
{
40-
$page_link = $this->create_link_to_page($photo_model);
41-
$size_variant = $photo_model->size_variants->getOriginal();
42-
$cat_arr = [];
43-
if ($photo_model->album_id !== null) {
44-
$cat_arr[] = $photo_model->album->title;
45-
}
51+
$page_link = route('gallery', ['albumId' => $data->album_id, 'photoId' => $data->id]);
4652
$feed_item = [
4753
'id' => $page_link,
48-
'title' => $photo_model->title,
49-
'summary' => $photo_model->description ?? '',
50-
'updated' => $photo_model->updated_at,
54+
'title' => $data->title,
55+
'summary' => $data->description ?? '',
56+
'updated' => $this->asDateTime($data->updated_at),
5157
'link' => $page_link,
52-
'enclosure' => $size_variant->url,
53-
'enclosureType' => $photo_model->type,
54-
'enclosureLength' => $size_variant->filesize,
55-
'authorName' => $photo_model->owner->username,
56-
'category' => $cat_arr,
58+
'enclosure' => self::pathToUrl($data->short_path, $data->storage_disk, SizeVariantType::ORIGINAL),
59+
'enclosureType' => $data->type,
60+
'enclosureLength' => $data->filesize,
61+
'authorName' => $data->username,
62+
'category' => [$data->album_title],
5763
];
5864

5965
return FeedItem::create($feed_item);
@@ -74,17 +80,36 @@ public function do(): Collection
7480
throw new FrameworkException('Date/Time component (Carbon)', $e);
7581
}
7682

77-
/** @var Collection<int,Photo> $photos */
83+
/** @var Collection<int,T> $photos */
7884
$photos = $this->photo_query_policy
7985
->applySearchabilityFilter(
80-
query: Photo::query()->with(['albums', 'owner', 'size_variants']),
86+
query: Photo::query(),
8187
origin: null,
8288
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_rss')
8389
)
90+
->joinSub(DB::table(PA::PHOTO_ALBUM), 'outer_'.PA::PHOTO_ALBUM, 'photos.id', '=', 'outer_'.PA::PHOTO_ID)
91+
->join('base_albums', 'base_albums.id', '=', 'outer_'.PA::ALBUM_ID)
92+
->join('size_variants', 'size_variants.photo_id', '=', 'photos.id')
93+
->join('users', 'users.id', '=', 'photos.owner_id')
94+
->where('size_variants.type', '=', SizeVariantType::ORIGINAL->value)
95+
->select([
96+
'photos.id',
97+
'photos.title',
98+
'photos.description',
99+
'photos.type',
100+
'photos.updated_at',
101+
'outer_'.PA::ALBUM_ID,
102+
'base_albums.title as album_title',
103+
'size_variants.short_path',
104+
'size_variants.filesize',
105+
'size_variants.storage_disk',
106+
'users.username']
107+
)
84108
->where('photos.created_at', '>=', $now_minus)
85109
->limit($rss_max)
110+
->toBase() // We use toBase() to avoid the use of the Eloquent casts etc.
86111
->get();
87112

88-
return $photos->map(fn (Photo $p) => $this->toFeedItem($p));
113+
return $photos->map(fn (object $p) => $this->toFeedItem($p));
89114
}
90115
}

app/DTO/PhotoCreate/DuplicateDTO.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,4 @@ public function setHasBeenResync(bool $val): void
6363
{
6464
$this->has_been_resynced = $val;
6565
}
66-
67-
public function replicatePhoto(): void
68-
{
69-
$dup = $this->photo;
70-
$this->photo = $dup->replicate();
71-
}
7266
}

app/Http/Requests/Album/UpdateAlbumRequest.php

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

99
namespace App\Http\Requests\Album;
1010

11+
use App\Constants\PhotoAlbum as PA;
1112
use App\Contracts\Http\Requests\HasAlbum;
1213
use App\Contracts\Http\Requests\HasAlbumSortingCriterion;
1314
use App\Contracts\Http\Requests\HasCompactBoolean;
@@ -54,6 +55,7 @@
5455
use App\Rules\EnumRequireSupportRule;
5556
use App\Rules\RandomIDRule;
5657
use App\Rules\TitleRule;
58+
use Illuminate\Support\Facades\DB;
5759
use Illuminate\Support\Facades\Gate;
5860
use Illuminate\Validation\Rules\Enum;
5961
use Illuminate\Validation\ValidationException;
@@ -77,9 +79,13 @@ class UpdateAlbumRequest extends BaseApiRequest implements HasAlbum, HasTitle, H
7779
public function authorize(): bool
7880
{
7981
return Gate::check(AlbumPolicy::CAN_EDIT, [AbstractAlbum::class, $this->album]) &&
80-
($this->is_compact ||
81-
$this->photo === null ||
82-
$this->photo->album_id === $this->album->id);
82+
(
83+
$this->is_compact ||
84+
$this->photo === null ||
85+
(DB::table(PA::PHOTO_ALBUM)->where('album_id', $this->album->id)
86+
->where('photo_id', $this->photo->id)
87+
->count() > 1)
88+
);
8389
}
8490

8591
/**
@@ -95,12 +101,14 @@ public function rules(): array
95101
RequestAttribute::PHOTO_SORTING_COLUMN_ATTRIBUTE => ['present', 'nullable', new Enum(ColumnSortingPhotoType::class)],
96102
RequestAttribute::PHOTO_SORTING_ORDER_ATTRIBUTE => [
97103
'required_with:' . RequestAttribute::PHOTO_SORTING_COLUMN_ATTRIBUTE,
98-
'nullable', new Enum(OrderSortingType::class),
104+
'nullable',
105+
new Enum(OrderSortingType::class),
99106
],
100107
RequestAttribute::ALBUM_SORTING_COLUMN_ATTRIBUTE => ['present', 'nullable', new Enum(ColumnSortingAlbumType::class)],
101108
RequestAttribute::ALBUM_SORTING_ORDER_ATTRIBUTE => [
102109
'required_with:' . RequestAttribute::ALBUM_SORTING_COLUMN_ATTRIBUTE,
103-
'nullable', new Enum(OrderSortingType::class),
110+
'nullable',
111+
new Enum(OrderSortingType::class),
104112
],
105113
RequestAttribute::ALBUM_ASPECT_RATIO_ATTRIBUTE => ['present', 'nullable', new Enum(AspectRatioType::class)],
106114
RequestAttribute::ALBUM_PHOTO_LAYOUT => ['present', 'nullable', new Enum(PhotoLayoutType::class)],
@@ -161,4 +169,4 @@ protected function processValidatedValues(array $values, array $files): void
161169
$photo_id = $values[RequestAttribute::HEADER_ID_ATTRIBUTE];
162170
$this->photo = $photo_id !== null ? Photo::query()->findOrFail($photo_id) : null;
163171
}
164-
}
172+
}

app/Models/Extensions/SizeVariants.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -263,41 +263,6 @@ public function deleteAll(): void
263263
(new Delete())->do(array_diff($ids, [null]))->do();
264264
}
265265

266-
/**
267-
* @throws ModelDBException
268-
* @throws IllegalOrderOfOperationException
269-
*/
270-
public function replicate(Photo $duplicate_photo): SizeVariants
271-
{
272-
$duplicate = new SizeVariants($duplicate_photo);
273-
self::replicateSizeVariant($duplicate, $this->original);
274-
self::replicateSizeVariant($duplicate, $this->medium2x);
275-
self::replicateSizeVariant($duplicate, $this->medium);
276-
self::replicateSizeVariant($duplicate, $this->small2x);
277-
self::replicateSizeVariant($duplicate, $this->small);
278-
self::replicateSizeVariant($duplicate, $this->thumb2x);
279-
self::replicateSizeVariant($duplicate, $this->thumb);
280-
self::replicateSizeVariant($duplicate, $this->placeholder);
281-
282-
return $duplicate;
283-
}
284-
285-
/**
286-
* @throws ModelDBException
287-
* @throws IllegalOrderOfOperationException
288-
*/
289-
private static function replicateSizeVariant(SizeVariants $duplicate, ?SizeVariant $size_variant): void
290-
{
291-
if ($size_variant !== null) {
292-
$duplicate->create(
293-
$size_variant->type,
294-
$size_variant->short_path,
295-
new ImageDimension($size_variant->width, $size_variant->height),
296-
$size_variant->filesize
297-
);
298-
}
299-
}
300-
301266
/**
302267
* Returns true if at least one version of medium is not null.
303268
*/

app/Models/Photo.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -423,32 +423,6 @@ public function isRaw(): bool
423423
return !$this->isPhoto() && !$this->isVideo();
424424
}
425425

426-
/**
427-
* @param string[] $except
428-
* //method.childParameterType (contravariance)
429-
*/
430-
public function replicate(?array $except = null): Photo
431-
{
432-
$duplicate = parent::replicate($except);
433-
// A photo has the following relations: (parent) album, owner and
434-
// size_variants.
435-
// While the duplicate may keep the relation to the same album and
436-
// each photo requires an individual set of size variants.
437-
// Se we unset the relation and explicitly duplicate the size variants.
438-
$duplicate->unsetRelation('size_variants');
439-
// save duplicate so that the photo gets an ID
440-
$duplicate->save();
441-
442-
$are_size_variants_originally_loaded = $this->relationLoaded('size_variants');
443-
// Duplicate the size variants of this instance for the duplicate
444-
$duplicated_size_variants = $this->size_variants->replicate($duplicate);
445-
if ($are_size_variants_originally_loaded) {
446-
$duplicate->setRelation('size_variants', $duplicated_size_variants);
447-
}
448-
449-
return $duplicate;
450-
}
451-
452426
/**
453427
* {@inheritDoc}
454428
*

database/migrations/2025_05_01_121707_create_photo_album_pivot.php renamed to database/migrations/2025_05_12_121707_create_photo_album_pivot.php

File renamed without changes.

0 commit comments

Comments
 (0)