Skip to content

Commit

Permalink
[Enhancement] Make it possible to fetch random image without needing …
Browse files Browse the repository at this point in the history
…to be public & starred (#1819)
  • Loading branch information
mingan666 authored May 4, 2023
1 parent 773269c commit 09990c3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
22 changes: 16 additions & 6 deletions app/Http/Controllers/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use App\ModelFunctions\SymLinkFunctions;
use App\Models\Configs;
use App\Models\Photo;
use App\SmartAlbums\StarredAlbum;
use App\Policies\PhotoQueryPolicy;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -64,9 +64,12 @@ public function get(GetPhotoRequest $request): PhotoResource
}

/**
* Returns a random public photo (starred)
* Returns a random photo (from the configured album).
* Only photos with enough access rights are included.
* This is used in the Frame Controller.
*
* @param PhotoQueryPolicy $photoQueryPolicy
*
* @return PhotoResource
*
* @throws ModelNotFoundException
Expand All @@ -75,13 +78,20 @@ public function get(GetPhotoRequest $request): PhotoResource
*
* @noinspection PhpIncompatibleReturnTypeInspection
*/
public function getRandom(): PhotoResource
public function getRandom(PhotoQueryPolicy $photoQueryPolicy): PhotoResource
{
$randomAlbumId = Configs::getValueAsString('random_album_id');

if ($randomAlbumId === '') {
$query = $photoQueryPolicy->applySearchabilityFilter(Photo::query()->with(['album', 'size_variants', 'size_variants.sym_links']));
} else {
$query = $this->albumFactory->findAbstractAlbumOrFail($randomAlbumId)
->photos()
->with(['album', 'size_variants', 'size_variants.sym_links']);
}
// PHPStan does not understand that `firstOrFail` returns `Photo`, but assumes that it returns `Model`
// @phpstan-ignore-next-line
return PhotoResource::make(StarredAlbum::getInstance()
->photos()
->inRandomOrder()
return PhotoResource::make($query->inRandomOrder()
->firstOrFail());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
DB::table('configs')->insert([
'key' => 'random_album_id',
'value' => 'starred',
'cat' => 'Mod Frame',
'type_range' => 'string',
'confidentiality' => '0',
'description' => 'Album id to be used by for random function.',
]);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::table('configs')->where('key', '=', 'random_album_id')->delete();
}
};

0 comments on commit 09990c3

Please sign in to comment.