Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify #2493

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/Livewire/Components/Forms/Album/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use App\Livewire\Traits\UseValidator;
use App\Models\Album as ModelsAlbum;
use App\Models\Extensions\BaseAlbum;
use App\Models\TagAlbum;
use App\Policies\AlbumPolicy;
use App\Rules\CopyrightRule;
use App\Rules\TitleRule;
Expand All @@ -38,6 +39,7 @@ class Properties extends Component

#[Locked] public string $albumID;
#[Locked] public bool $is_model_album;
#[Locked] public bool $is_tag_album;
public string $title; // ! wired
public string $description; // ! wired
public string $photo_sorting_column = ''; // ! wired
Expand All @@ -47,6 +49,7 @@ class Properties extends Component
public string $album_aspect_ratio = ''; // ! wired
public string $license = 'none'; // ! wired
public string $copyright = ''; // ! wired
public ?string $tag = ''; // ! wired

/**
* This is the equivalent of the constructor for Livewire Components.
Expand All @@ -60,6 +63,7 @@ public function mount(BaseAlbum $album): void
Gate::authorize(AlbumPolicy::CAN_EDIT, [AbstractAlbum::class, $album]);

$this->is_model_album = $album instanceof ModelsAlbum;
$this->is_tag_album = $album instanceof TagAlbum;

$this->albumID = $album->id;
$this->title = $album->title;
Expand All @@ -74,6 +78,10 @@ public function mount(BaseAlbum $album): void
$this->album_sorting_order = $album->album_sorting?->order->value ?? '';
$this->album_aspect_ratio = $album->album_thumb_aspect_ratio?->value ?? '';
}
if ($this->is_tag_album) {
/** @var TagAlbum $album */
$this->tag = implode(', ', $album->show_tags);
}
}

/**
Expand Down Expand Up @@ -132,6 +140,10 @@ public function submit(AlbumFactory $albumFactory): void
$baseAlbum->album_sorting = $albumSortingCriterion;
$baseAlbum->album_thumb_aspect_ratio = AspectRatioType::tryFrom($this->album_aspect_ratio);
}
if ($this->is_tag_album) {
/** @var TagAlbum $baseAlbum */
$baseAlbum->show_tags = collect(explode(',', $this->tag))->map(fn ($v) => trim($v))->filter(fn ($v) => $v !== '')->all();
}

$this->notify(__('lychee.CHANGE_SUCCESS'));
$baseAlbum->save();
Expand Down
3 changes: 3 additions & 0 deletions app/Livewire/DTO/AlbumRights.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Contracts\Models\AbstractAlbum;
use App\Livewire\Traits\UseWireable;
use App\Models\Album;
use App\Policies\AlbumPolicy;
use Illuminate\Support\Facades\Gate;
use Livewire\Wireable;
Expand All @@ -22,6 +23,7 @@ public function __construct(
public bool $can_share_with_users = false,
public bool $can_download = false,
public bool $can_upload = false,
public bool $can_move = false,
public bool $can_delete = false,
public bool $can_access_original = false,
) {
Expand All @@ -42,6 +44,7 @@ public static function make(?AbstractAlbum $abstractAlbum): AlbumRights
can_share_with_users: Gate::check(AlbumPolicy::CAN_SHARE_WITH_USERS, [AbstractAlbum::class, $abstractAlbum]),
can_download: Gate::check(AlbumPolicy::CAN_DOWNLOAD, [AbstractAlbum::class, $abstractAlbum]),
can_upload: Gate::check(AlbumPolicy::CAN_UPLOAD, [AbstractAlbum::class, $abstractAlbum]),
can_move: Gate::check(AlbumPolicy::CAN_DELETE, [AbstractAlbum::class, $abstractAlbum]) && $abstractAlbum instanceof Album,
can_delete: Gate::check(AlbumPolicy::CAN_DELETE, [AbstractAlbum::class, $abstractAlbum]),
can_access_original: Gate::check(AlbumPolicy::CAN_ACCESS_FULL_PHOTO, [AbstractAlbum::class, $abstractAlbum]),
);
Expand Down
8 changes: 0 additions & 8 deletions app/Models/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,6 @@ class Album extends BaseAlbum implements Node
*/
protected $with = ['cover', 'cover.size_variants', 'thumb'];

/**
* @return array<string,mixed>
*/
protected function _toArray(): array
{
return parent::toArray();
}

/**
* Return the relationship between this album and photos which are
* direct children of this album.
Expand Down
8 changes: 0 additions & 8 deletions app/Models/BaseAlbumImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,6 @@ class BaseAlbumImpl extends Model implements HasRandomID
*/
protected $with = ['owner', 'access_permissions'];

/**
* @return array<string,mixed>
*/
protected function _toArray(): array
{
return parent::toArray();
}

/**
* @param $query
*
Expand Down
5 changes: 0 additions & 5 deletions app/Models/Extensions/ToArrayThrowsNotImplemented.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
*/
trait ToArrayThrowsNotImplemented
{
/**
* @return array<string,mixed>
*/
abstract protected function _toArray(): array;

/**
* @return array<string,mixed>
*
Expand Down
8 changes: 0 additions & 8 deletions app/Models/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,6 @@ public function newEloquentBuilder($query): PhotoBuilder
return new PhotoBuilder($query);
}

/**
* @return array<string,mixed>
*/
protected function _toArray(): array
{
return parent::toArray();
}

/**
* Return the relationship between a Photo and its Album.
*
Expand Down
8 changes: 0 additions & 8 deletions app/Models/SizeVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,6 @@ public function newEloquentBuilder($query): SizeVariantBuilder
return new SizeVariantBuilder($query);
}

/**
* @return array<string,mixed>
*/
protected function _toArray(): array
{
return parent::toArray();
}

/**
* Returns the association to the photo which this size variant belongs
* to.
Expand Down
10 changes: 2 additions & 8 deletions app/Models/SymLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ class SymLink extends Model
'size_variant_id', // see above
];

/**
* @return array<string,mixed>
*/
final protected function _toArray(): array
{
return parent::toArray();
}

/**
* @param $query
*
Expand Down Expand Up @@ -136,6 +128,7 @@ public function scopeExpired(Builder $query): Builder
protected function getUrlAttribute(): string
{
try {
/** @disregard P1013 */
return Storage::disk(self::DISK_NAME)->url($this->short_path);
} catch (\RuntimeException $e) {
throw new FrameworkException('Laravel\'s storage component', $e);
Expand All @@ -161,6 +154,7 @@ protected function performInsert(Builder $query): bool
$origRealPath = $file->getRealPath();
$extension = $file->getExtension();
$symShortPath = hash('sha256', random_bytes(32) . '|' . $origRealPath) . $extension;
/** @disregard P1013 */
$symAbsolutePath = Storage::disk(SymLink::DISK_NAME)->path($symShortPath);
try {
if (is_link($symAbsolutePath)) {
Expand Down
11 changes: 0 additions & 11 deletions app/Models/TagAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ class TagAlbum extends BaseAlbum
'thumb',
];

/**
* @return array<string,mixed>
*/
protected function _toArray(): array
{
$result = parent::toArray();
$result['is_tag_album'] = true;

return $result;
}

public function photos(): HasManyPhotosByTag
{
return new HasManyPhotosByTag($this);
Expand Down
8 changes: 0 additions & 8 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ class User extends Authenticatable implements WebAuthnAuthenticatable

protected $hidden = [];

/**
* @return array<string,mixed>
*/
protected function _toArray(): array
{
return parent::toArray();
}

/**
* Create a new Eloquent query builder for the model.
*
Expand Down
34 changes: 0 additions & 34 deletions app/SmartAlbums/BaseSmartAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Contracts\Exceptions\InternalLycheeException;
use App\Contracts\Models\AbstractAlbum;
use App\DTO\AlbumProtectionPolicy;
use App\DTO\PhotoSortingCriterion;
use App\Enum\SmartAlbumType;
use App\Exceptions\ConfigurationKeyMissingException;
Expand Down Expand Up @@ -68,39 +67,6 @@ protected function __construct(SmartAlbumType $id, \Closure $smartCondition)
}
}

/**
* @return array{id:string,title:string,thumb:?Thumb,policy:AlbumProtectionPolicy,photos:?array<int,Photo>}
*/
protected function _toArray(): array
{
// The properties `thumb` and `photos` are intentionally treated
// differently.
//
// 1. The result always includes `thumb`, hence we call the
// getter method to ensure that the property is initialized, if it
// has not already been accessed before.
// 2. The result only includes the collection `photos`, if it has
// already explicitly been accessed earlier and thus is initialized.
//
// Rationale:
//
// 1. This resembles the behaviour of a real Eloquent model, if the
// attribute `thumb` was part of the `append`-property of model.
// 2. This resembles the behaviour of a real Eloquent model for
// one-to-many relations.
// A relation is only included in the array representation, if the
// relation has been loaded.
// This avoids unnecessary hydration of photos if the album is
// only used within a listing of sub-albums.
return [
'id' => $this->id,
'title' => $this->title,
'thumb' => $this->getThumbAttribute(),
'policy' => AlbumProtectionPolicy::ofSmartAlbum($this),
'photos' => $this->photos?->toArray(),
];
}

/**
* @return \App\Eloquent\FixedQueryBuilder<Photo>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<livewire:forms.album.share-with :album="$this->album" />
</div>
@endif
@if($this->rights->can_delete === true)
@if($this->rights->can_move === true)
<div class="w-full xl:w-5/6 flex justify-center flex-wrap mb-4 sm:mt-7 pl-7" x-cloak x-show="albumFlags.activeTab === 2">
<livewire:forms.album.move-panel :album="$this->album" />
</div>
Expand Down
6 changes: 6 additions & 0 deletions resources/views/livewire/forms/album/properties.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<x-forms.dropdown class="mx-2" :options="$this->aspectRatios" id="aspect_ratio_dialog_select" wire:model='album_aspect_ratio'/>
</div>
@endif
@if($is_tag_album)
<div class="mb-4 h-10">
<span class="font-bold">{{ __('lychee.ALBUM_SET_SHOWTAGS') }}</span>
<x-forms.inputs.text wire:model='tag' id="albumTags" />
</div>
@endif
<x-forms.buttons.action class="rounded w-full" wire:click='submit' >
{{ __('lychee.SAVE') }}
</x-forms.buttons.action>
Expand Down
Loading