Skip to content

Commit

Permalink
Add missing primary key in Jobs_history + option to disable feedback …
Browse files Browse the repository at this point in the history
…completely. (#2457)

* Add missing primary key in Jobs_history + option to disable feedback completely. (#2457)
* Wrap collections to avoid double queries (#2458)
  • Loading branch information
ildyria authored Jun 8, 2024
1 parent 539ce32 commit 59293f6
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 63 deletions.
2 changes: 1 addition & 1 deletion app/Livewire/Components/Modules/Jobs/Feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function mount(): void
}

$this->open = Configs::getValueAsBool('current_job_processing_visible');
$this->display = true;
$this->display = Configs::getValueAsBool('job_processing_queue_visible');
}

/**
Expand Down
61 changes: 21 additions & 40 deletions app/Livewire/Components/Pages/Gallery/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Livewire\DTO\AlbumRights;
use App\Livewire\DTO\Layouts;
use App\Livewire\DTO\PhotoFlags;
use App\Livewire\DTO\ProtectedCollection;
use App\Livewire\DTO\SessionFlags;
use App\Models\Album as ModelsAlbum;
use App\Models\Configs;
Expand All @@ -24,7 +25,6 @@
use App\Policies\AlbumPolicy;
use App\Policies\PhotoPolicy;
use Illuminate\Database\Eloquent\RelationNotFoundException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
Expand All @@ -41,6 +41,7 @@
class Album extends BaseAlbumComponent implements Reloadable
{
private AlbumFactory $albumFactory;
private AlbumFormatted $formatted;

#[Locked] public bool $is_search_accessible = false;
public ?AbstractAlbum $album = null;
Expand All @@ -54,6 +55,8 @@ public function boot(): void
{
$this->albumFactory = resolve(AlbumFactory::class);
$this->layouts = new Layouts();
$this->albumsCollection = new ProtectedCollection(type: 'album');
$this->photosCollection = new ProtectedCollection(type: 'photo');
}

public function mount(string $albumId, string $photoId = ''): void
Expand All @@ -74,18 +77,28 @@ final public function render(): View
{
$this->sessionFlags = SessionFlags::get();
$this->rights = AlbumRights::make($this->album);
$this->num_albums = 0;
$this->num_albums = 0;

if ($this->flags->is_accessible) {
$this->formatted = new AlbumFormatted($this->album, $this->fetchHeaderUrl()?->url);
$this->num_users = User::count();
$this->header_url ??= $this->fetchHeaderUrl()?->url;
$this->num_albums = $this->album instanceof ModelsAlbum ? $this->album->children->count() : 0;
$this->num_photos = $this->album->photos->count();

$this->photosCollection->set($this->album->photos);
$this->num_photos = $this->photosCollection->get()->count();

// No photos, no frame
if ($this->num_photos === 0) {
$this->flags->is_mod_frame_enabled = false;
}

if ($this->album instanceof ModelsAlbum) {
$this->albumsCollection->set($this->album->children()->getResults());
$this->num_albums = $this->albumsCollection->get()->count();
} else {
$this->albumsCollection->set(null);
}

$is_latitude_longitude_found = false;
if ($this->album instanceof ModelsAlbum) {
$is_latitude_longitude_found = $this->album->all_photos()->whereNotNull('latitude')->whereNotNull('longitude')->count() > 0;
Expand All @@ -103,6 +116,9 @@ final public function render(): View
can_rotate: Configs::getValueAsBool('editor_enabled'),
can_edit: $this->rights->can_edit,
);
} else {
$this->albumsCollection->set(null);
$this->photosCollection->set(collect([]));
}

return view('livewire.pages.gallery.album');
Expand Down Expand Up @@ -133,41 +149,6 @@ public function reloadPage(): void
}
}

/**
* Return the photoIDs.
*
* @return Collection<int,Photo>
*/
public function getPhotosProperty(): Collection
{
return $this->album->photos;
}

/**
* @return Collection<int,ModelsAlbum>|null
*/
public function getAlbumsProperty(): Collection|null
{
if ($this->album instanceof ModelsAlbum) {
/** @var Collection<int,ModelsAlbum> $res */
$res = $this->album->children()->getResults();

return $res;
}

return null;
}

/**
* Used in the JS front-end to manage the selected albums.
*
* @return string[]
*/
public function getAlbumIDsProperty(): array
{
return $this->getAlbumsProperty()?->map(fn ($v, $k) => $v->id)?->all() ?? [];
}

/**
* Fetch the header url.
*
Expand Down Expand Up @@ -282,7 +263,7 @@ public function setAsCover(string $albumID): void

public function getAlbumFormattedProperty(): AlbumFormatted
{
return new AlbumFormatted($this->album, $this->fetchHeaderUrl()?->url);
return $this->formatted;
}

public function getNoImagesAlbumsMessageProperty(): string
Expand Down
1 change: 0 additions & 1 deletion app/Livewire/Components/Pages/Gallery/Albums.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function getSmartAlbumsProperty(): Collection
{
return $this->topAlbums->smart_albums
->concat($this->topAlbums->tag_albums);
// ->reject(fn ($album) => $album === null);
}

/**
Expand Down
23 changes: 19 additions & 4 deletions app/Livewire/Components/Pages/Gallery/BaseAlbumComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Livewire\DTO\AlbumRights;
use App\Livewire\DTO\Layouts;
use App\Livewire\DTO\PhotoFlags;
use App\Livewire\DTO\ProtectedCollection;
use App\Livewire\DTO\SessionFlags;
use App\Livewire\Traits\AlbumsPhotosContextMenus;
use App\Livewire\Traits\Notify;
Expand Down Expand Up @@ -41,9 +42,14 @@ abstract class BaseAlbumComponent extends Component

protected Layouts $layouts;

/** @var ProtectedCollection<ModelsAlbum> */
protected ?ProtectedCollection $albumsCollection;

/** @var ProtectedCollection<\App\Models\Photo> */
protected ?ProtectedCollection $photosCollection;

#[Locked] public ?string $albumId = null;
#[Locked] public ?string $photoId = null;
#[Locked] public ?string $header_url = null;
#[Locked] public int $num_albums = 0;
#[Locked] public int $num_photos = 0;
#[Locked] public int $num_users = 0;
Expand Down Expand Up @@ -74,21 +80,30 @@ final public function getPhotosResourceProperty(): PhotoCollectionResource
*
* @return Collection<int,Photo>|LengthAwarePaginator<Photo>
*/
abstract public function getPhotosProperty(): Collection|LengthAwarePaginator;
public function getPhotosProperty(): Collection|LengthAwarePaginator
{
return $this->photosCollection->get();
}

/**
* Return the albums.
*
* @return Collection<int,ModelsAlbum>|null
*/
abstract public function getAlbumsProperty(): Collection|null;
final public function getAlbumsProperty(): Collection|null
{
return $this->albumsCollection->get();
}

/**
* Used in the JS front-end to manage the selected albums.
*
* @return string[]
*/
abstract public function getAlbumIDsProperty(): array;
final public function getAlbumIDsProperty(): array
{
return $this->albumsCollection->get()?->map(fn ($v, $_k) => $v->id)?->all() ?? [];
}

/**
* Back property used to retrieve the URL to step back and back arrow.
Expand Down
22 changes: 6 additions & 16 deletions app/Livewire/Components/Pages/Gallery/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Livewire\DTO\AlbumRights;
use App\Livewire\DTO\Layouts;
use App\Livewire\DTO\PhotoFlags;
use App\Livewire\DTO\ProtectedCollection;
use App\Livewire\DTO\SessionFlags;
use App\Models\Album as ModelsAlbum;
use App\Models\Configs;
Expand All @@ -35,8 +36,6 @@ class Search extends BaseAlbumComponent

/** @var LengthAwarePaginator<Photo> */
private LengthAwarePaginator $photos;
/** @var Collection<int,ModelsAlbum> */
private Collection $albums;

#[Locked]
#[Url(history: true)]
Expand All @@ -56,8 +55,9 @@ public function boot(): void
$this->layouts = new Layouts();
$this->albumSearch = resolve(AlbumSearch::class);
$this->photoSearch = resolve(PhotoSearch::class);
$this->albumsCollection = new ProtectedCollection(type: 'album', is_loaded: true, collection: collect([]));
$this->photosCollection = new ProtectedCollection(type: 'photo');
$this->photos = new LengthAwarePaginator([], 0, 200);
$this->albums = collect([]);
$this->num_albums = 0;
$this->num_photos = 0;
$this->search_minimum_length_required = Configs::getValueAsInt('search_minimum_length_required');
Expand Down Expand Up @@ -127,14 +127,14 @@ public function render(): View
->paginate(Configs::getValueAsInt('search_pagination_limit'))
->withQueryString();
$this->photos = $photoResults;
/** @var Collection<int,ModelsAlbum> $albumResults */
$albumResults = $this->albumSearch->queryAlbums($this->terms);
$this->albums = $albumResults;
$this->num_albums = $this->albums->count();
$this->albumsCollection->set($albumResults);
$this->num_albums = $this->albumsCollection->get()->count();
$this->num_photos = $this->photos->count();
$this->albumId = '';
$this->photoId = '';
}
/** @var Collection<int,ModelsAlbum> $albumResults */

return view('livewire.pages.gallery.search');
}
Expand All @@ -149,16 +149,6 @@ public function getPhotosProperty(): Collection
return collect($this->photos->items());
}

public function getAlbumsProperty(): ?Collection
{
return $this->albums;
}

public function getAlbumIDsProperty(): array
{
return $this->albums->map(fn ($v, $k) => $v->id)->all();
}

// For now, simple
public function getBackProperty(): string
{
Expand Down
52 changes: 52 additions & 0 deletions app/Livewire/DTO/ProtectedCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Livewire\DTO;

use App\Exceptions\Internal\LycheeLogicException;
use Illuminate\Support\Collection;

/**
* @template T
*/
class ProtectedCollection
{
/**
* @param bool $is_loaded
* @param Collection<int,T>|null $collection
*
* @return void
*/
public function __construct(
private string $type,
private bool $is_loaded = false,
private ?Collection $collection = null,
) {
}

/**
* @return Collection<int,T>|null collection encapsulated
*
* @throws LycheeLogicException we queried it too early
*/
public function get(): Collection|null
{
if ($this->is_loaded) {
return $this->collection;
}

throw new LycheeLogicException(sprintf('Collection %s data is not available.', $this->type));
}

/**
* Set the collection.
*
* @param Collection<int,T>|null $collection
*
* @return void
*/
public function set(Collection|null $collection)
{
$this->is_loaded = true;
$this->collection = $collection;
}
}
1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function boot()

try {
$lang = Configs::getValueAsString('lang');
/** @disregard P1013 Undefined method setLocale() (stupid intelephense) */
app()->setLocale($lang);
} catch (\Throwable $e) {
/** Ignore.
Expand Down
25 changes: 25 additions & 0 deletions database/migrations/2024_06_08_093403_primary_key_job_history.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('jobs_history', function (Blueprint $table) {
$table->index(['owner_id', 'status']);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
// We cannot remove index key because owner_id is used for FK constraint.
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use App\Models\Extensions\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const PROCESSING = 'Image Processing';
public const BOOL = '0|1';

public function getConfigs(): array
{
return [
[
'key' => 'job_processing_queue_visible',
'value' => '1',
'is_secret' => false,
'cat' => self::PROCESSING,
'type_range' => self::BOOL,
'description' => 'Enable the processing queue in the bottom left corner.',
],
];
}
};
2 changes: 1 addition & 1 deletion resources/views/livewire/forms/album/properties.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<x-forms.dropdown class="mx-2" :options="$this->albumSortingColumns" id="sorting_dialog_album_column_select" wire:model='album_sorting_column'/>
<x-forms.dropdown class="mx-2" :options="$this->sortingOrders" id="sorting_dialog_album_order_select" wire:model='album_sorting_order'/>
</div>
<livewire:forms.album.set-header :album_id="$this->albumID" />
<livewire:forms.album.set-header :album_id="$this->albumID" lazy="on-load" />
<div class="h-10">
<span class="font-bold">{{ __('lychee.ALBUM_SET_LICENSE') }}</span>
<x-forms.dropdown class="mx-2" :options="$this->licenses" id="license_dialog_select" wire:model='license'/>
Expand Down

0 comments on commit 59293f6

Please sign in to comment.