Skip to content

Commit

Permalink
Use random instead of sorted for thumbs of Smart Albumbs (LycheeOrg#2429
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ildyria authored May 14, 2024
1 parent 831b5c8 commit 6a70970
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
30 changes: 30 additions & 0 deletions app/Models/Extensions/Thumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ public static function createFromQueryable(Relation|Builder $photoQueryable, Sor
}
}

/**
* Creates a thumb by using the best rated photo from the given queryable.
* In other words, same as above but this time we pick a random image instead.
*
* Note, this method assumes that the relation is already restricted
* such that it only returns photos which the current user may see.
*
* @param Relation|Builder $photoQueryable the relation to or query for {@link Photo} which is used to pick a thumb
*
* @return Thumb|null the created thumbnail; null if the relation is empty
*
* @throws InvalidPropertyException thrown, if $sortingOrder neither
* equals `desc` nor `asc`
*/
public static function createFromRandomQueryable(Relation|Builder $photoQueryable): ?Thumb
{
try {
/** @var Photo|null $cover */
$cover = $photoQueryable
->withOnly(['size_variants' => (fn (HasMany $r) => self::sizeVariantsFilter($r))])
->inRandomOrder()
->select(['photos.id', 'photos.type'])
->first();

return self::createFromPhoto($cover);
} catch (\InvalidArgumentException $e) {
throw new InvalidPropertyException('Sorting order invalid', $e);
}
}

/**
* Creates a thumbnail from the given photo.
* On Livewire it will use by default small and small2x if available, thumb and thumb2x if not.
Expand Down
17 changes: 9 additions & 8 deletions app/SmartAlbums/BaseSmartAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Exceptions\Internal\InvalidQueryModelException;
use App\Exceptions\InvalidPropertyException;
use App\Models\AccessPermission;
use App\Models\Configs;
use App\Models\Extensions\SortingDecorator;
use App\Models\Extensions\Thumb;
use App\Models\Extensions\ToArrayThrowsNotImplemented;
Expand Down Expand Up @@ -148,17 +149,17 @@ public function getPhotos(): ?Collection
*/
protected function getThumbAttribute(): ?Thumb
{
if ($this->thumb === null) {
/*
* Note, `photos()` already applies a "security filter" and
* only returns photos which are accessible by the current
* user.
*/
$this->thumb = Thumb::createFromQueryable(
/*
* Note, `photos()` already applies a "security filter" and
* only returns photos which are accessible by the current
* user.
*/
$this->thumb ??= Configs::getValueAsBool('SA_random_thumbs')
? Thumb::createFromRandomQueryable($this->photos())
: $this->thumb = Thumb::createFromQueryable(
$this->photos(),
PhotoSortingCriterion::createDefault()
);
}

return $this->thumb;
}
Expand Down
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 = 'Smart Albums';
public const BOOL = '0|1';

public function getConfigs(): array
{
return [
[
'key' => 'SA_random_thumbs',
'value' => '0',
'is_secret' => false,
'cat' => self::PROCESSING,
'type_range' => self::BOOL,
'description' => 'Use random thumbs instead of stared/sorting order.',
],
];
}
};
7 changes: 3 additions & 4 deletions tests/Feature/Base/BaseSharingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use App\SmartAlbums\StarredAlbum;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\AbstractTestCase;
use Tests\Feature\Constants\TestConstants;
use Tests\Feature\LibUnitTests\RootAlbumUnitTest;
use Tests\Feature\LibUnitTests\SharingUnitTest;
Expand Down Expand Up @@ -130,9 +129,9 @@ public function tearDown(): void
* cumbersome, expected JSON description again and again.
*
* @param string $samplePhotoID the identifier of the sample photo:
* {@link AbstractTestCase::SAMPLE_FILE_NIGHT_IMAGE},
* {@link AbstractTestCase::SAMPLE_FILE_MONGOLIA_IMAGE}, or
* {@link AbstractTestCase::SAMPLE_FILE_TRAIN_IMAGE}
* {@link Tests\AbstractTestCase::SAMPLE_FILE_NIGHT_IMAGE},
* {@link Tests\AbstractTestCase::SAMPLE_FILE_MONGOLIA_IMAGE}, or
* {@link Tests\AbstractTestCase::SAMPLE_FILE_TRAIN_IMAGE}
* @param string $photoID the photo ID
* @param string|null $albumID the album ID
* @param array $attrToMerge additional attributes which should be
Expand Down

0 comments on commit 6a70970

Please sign in to comment.