Skip to content

Commit

Permalink
Allow to disable smart albums individually. (#2468)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Martin Stone <[email protected]>
  • Loading branch information
ildyria and d7415 authored Jun 18, 2024
1 parent d0f793c commit 907a404
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 21 deletions.
19 changes: 6 additions & 13 deletions app/Actions/Albums/Top.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use App\Factories\AlbumFactory;
use App\Http\Resources\Collections\TopAlbumsResource;
use App\Models\Album;
use App\Models\Configs;
use App\Models\Extensions\SortingDecorator;
use App\Models\TagAlbum;
use App\Policies\AlbumPolicy;
Expand Down Expand Up @@ -59,18 +58,12 @@ public function __construct(AlbumFactory $albumFactory, AlbumQueryPolicy $albumQ
*/
public function get(): TopAlbumsResource
{
if (Configs::getValueAsBool('SA_enabled')) {
// Do not eagerly load the relation `photos` for each smart album.
// On the albums overview, we only need a thumbnail for each album.
/** @var BaseCollection<int,BaseSmartAlbum> $smartAlbums */
$smartAlbums = $this->albumFactory
->getAllBuiltInSmartAlbums(false)
->map(
fn ($smartAlbum) => Gate::check(AlbumPolicy::CAN_SEE, $smartAlbum) ? $smartAlbum : null
)->reject(fn ($smartAlbum) => $smartAlbum === null);
} else {
$smartAlbums = new BaseCollection();
}
// Do not eagerly load the relation `photos` for each smart album.
// On the albums overview, we only need a thumbnail for each album.
/** @var BaseCollection<int,BaseSmartAlbum> $smartAlbums */
$smartAlbums = $this->albumFactory
->getAllBuiltInSmartAlbums(false)
->filter(fn ($smartAlbum) => Gate::check(AlbumPolicy::CAN_SEE, $smartAlbum));

$tagAlbumQuery = $this->albumQueryPolicy
->applyVisibilityFilter(TagAlbum::query()->with(['access_permissions', 'owner']));
Expand Down
16 changes: 16 additions & 0 deletions app/Enum/SmartAlbumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Enum;

use App\Enum\Traits\DecorateBackedEnum;
use App\Models\Configs;

/**
* Enum SmartAlbumType.
Expand All @@ -15,4 +16,19 @@ enum SmartAlbumType: string
case STARRED = 'starred';
case RECENT = 'recent';
case ON_THIS_DAY = 'on_this_day';

/**
* Return whether the smart album is enabled.
*
* @return bool
*/
public function is_enabled(): bool
{
return match ($this) {
self::UNSORTED => Configs::getValueAsBool('enable_unsorted'),
self::STARRED => Configs::getValueAsBool('enable_starred'),
self::RECENT => Configs::getValueAsBool('enable_recent'),
self::ON_THIS_DAY => Configs::getValueAsBool('enable_on_this_day'),
};
}
}
7 changes: 3 additions & 4 deletions app/Factories/AlbumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,9 @@ public function findBaseAlbumsOrFail(array $albumIDs, bool $withRelations = true
public function getAllBuiltInSmartAlbums(bool $withRelations = true): Collection
{
$smartAlbums = new Collection();
/** @var SmartAlbumType $smartAlbumId */
foreach (SmartAlbumType::cases() as $smartAlbumId) {
$smartAlbums->put($smartAlbumId->value, $this->createSmartAlbum($smartAlbumId, $withRelations));
}
collect(SmartAlbumType::cases())
->filter(fn (SmartAlbumType $s) => $s->is_enabled())
->each(fn (SmartAlbumType $s) => $smartAlbums->put($s->value, $this->createSmartAlbum($s, $withRelations)));

return $smartAlbums;
}
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Resources/ConfigurationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Enum\LicenseType;
use App\Enum\MapProviders;
use App\Enum\PhotoLayoutType;
use App\Enum\SmartAlbumType;
use App\Enum\ThumbAlbumSubtitleType;
use App\Exceptions\Handler;
use App\Metadata\Versions\InstalledVersion;
Expand Down Expand Up @@ -80,7 +81,10 @@ public function toArray($request): array
'location' => base_path('public/'),

// from config
'SA_enabled' => Configs::getValueAsBool('SA_enabled'),
'SA_enabled' => SmartAlbumType::UNSORTED->is_enabled() &&
SmartAlbumType::STARRED->is_enabled() &&
SmartAlbumType::RECENT->is_enabled() &&
SmartAlbumType::ON_THIS_DAY->is_enabled(),
'SL_enable' => Configs::getValueAsBool('SL_enable'),
'SL_for_admin' => Configs::getValueAsBool('SL_for_admin'),
'SL_life_time_days' => Configs::getValueAsInt('SL_life_time_days'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use App\Models\Extensions\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const SMART_ALBUMS = 'Smart Albums';
public const BOOL = '0|1';

public function getConfigs(): array
{
return [
[
'key' => 'enable_unsorted',
'value' => '1',
'is_secret' => false,
'cat' => self::SMART_ALBUMS,
'type_range' => self::BOOL,
'description' => 'Enable Unsorted smart album. Warning! Disabling this will make pictures without an album invisible.',
],
[
'key' => 'enable_starred',
'value' => '1',
'is_secret' => false,
'cat' => self::SMART_ALBUMS,
'type_range' => self::BOOL,
'description' => 'Enable Starred smart album.',
],
[
'key' => 'enable_recent',
'value' => '1',
'is_secret' => false,
'cat' => self::SMART_ALBUMS,
'type_range' => self::BOOL,
'description' => 'Enable Recent uploads smart album.',
],
[
'key' => 'enable_on_this_day',
'value' => '1',
'is_secret' => false,
'cat' => self::SMART_ALBUMS,
'type_range' => self::BOOL,
'description' => 'Enable On this day smart album.',
],
];
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class() extends Migration {
public const SMART_ALBUMS = 'Smart Albums';
public const BOOL = '0|1';
public const TABLE = 'configs';
public const SA = 'SA_enabled';
public const UNSORTED = 'enable_unsorted';
public const STARRED = 'enable_starred';
public const RECENT = 'enable_recent';
public const ON_THIS_DAY = 'enable_on_this_day';

/**
* Run the migrations.
*/
public function up(): void
{
$val = DB::table(self::TABLE)->select('value')->where('key', '=', self::SA)->first()->value;
DB::table(self::TABLE)->whereIn('key', [
self::UNSORTED,
self::STARRED,
self::RECENT,
self::ON_THIS_DAY, ])->update(['value' => $val]);
DB::table(self::TABLE)->where('key', '=', self::SA)->delete();
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::table(self::TABLE)->insert([
[
'key' => 'SA_enabled',
'value' => '1',
'is_secret' => false,
'cat' => self::SMART_ALBUMS,
'type_range' => self::BOOL,
'description' => 'Enable Smart Albums.',
],
]);
}
};
67 changes: 64 additions & 3 deletions tests/Feature/AlbumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class AlbumTest extends AbstractTestCase
use RequiresEmptyUsers;
use RequiresEmptyPhotos;

public const ENABLE_UNSORTED = 'enable_unsorted';
public const ENABLE_STARRED = 'enable_starred';
public const ENABLE_RECENT = 'enable_recent';
public const ENABLE_ON_THIS_DAY = 'enable_on_this_day';

protected AlbumsUnitTest $albums_tests;
protected RootAlbumUnitTest $root_album_tests;
protected UsersUnitTest $users_tests;
Expand Down Expand Up @@ -880,7 +885,10 @@ public function testHiddenSmartAlbums(): void
Auth::loginUsingId(1);

$this->clearCachedSmartAlbums();
Configs::set('SA_enabled', true);
Configs::set(self::ENABLE_UNSORTED, true);
Configs::set(self::ENABLE_STARRED, true);
Configs::set(self::ENABLE_RECENT, true);
Configs::set(self::ENABLE_ON_THIS_DAY, true);
$response = $this->postJson('/api/Albums::get');
$this->assertOk($response);
$response->assertJson([
Expand All @@ -896,7 +904,10 @@ public function testHiddenSmartAlbums(): void
]);

$this->clearCachedSmartAlbums();
Configs::set('SA_enabled', false);
Configs::set(self::ENABLE_UNSORTED, false);
Configs::set(self::ENABLE_STARRED, false);
Configs::set(self::ENABLE_RECENT, false);
Configs::set(self::ENABLE_ON_THIS_DAY, false);
$response = $this->postJson('/api/Albums::get');
$this->assertOk($response);
$response->assertJson([
Expand All @@ -908,8 +919,58 @@ public function testHiddenSmartAlbums(): void
$response->assertDontSee('unsorted');
$response->assertDontSee('starred');
$response->assertDontSee('recent');
$response->assertDontSee('on_this_day');

$this->clearCachedSmartAlbums();
Configs::set(self::ENABLE_UNSORTED, true);
$response = $this->postJson('/api/Albums::get');
$this->assertOk($response);
$response->assertJson([
'smart_albums' => [
'unsorted' => [],
],
'tag_albums' => [],
'albums' => [],
'shared_albums' => [],
]);
$response->assertDontSee('starred');
$response->assertDontSee('recent');
$response->assertDontSee('on_this_day');

$this->clearCachedSmartAlbums();
Configs::set(self::ENABLE_STARRED, true);
$response = $this->postJson('/api/Albums::get');
$this->assertOk($response);
$response->assertJson([
'smart_albums' => [
'unsorted' => [],
'starred' => [],
],
'tag_albums' => [],
'albums' => [],
'shared_albums' => [],
]);
$response->assertDontSee('recent');
$response->assertDontSee('on_this_day');

$this->clearCachedSmartAlbums();
Configs::set(self::ENABLE_RECENT, true);
$response = $this->postJson('/api/Albums::get');
$this->assertOk($response);
$response->assertJson([
'smart_albums' => [
'unsorted' => [],
'starred' => [],
'recent' => [],
],
'tag_albums' => [],
'albums' => [],
'shared_albums' => [],
]);
$response->assertDontSee('on_this_day');
$this->clearCachedSmartAlbums();
Configs::set(self::ENABLE_ON_THIS_DAY, true);

Configs::set('SA_enabled', true);
Auth::logout();
Session::flush();
}
Expand Down

0 comments on commit 907a404

Please sign in to comment.