Skip to content

Commit

Permalink
Support more layouts + Timeline in albums/album (#2679)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Nov 25, 2024
1 parent 8b7ffcb commit 98d012d
Show file tree
Hide file tree
Showing 53 changed files with 1,171 additions and 181 deletions.
13 changes: 13 additions & 0 deletions app/Contracts/Http/Requests/HasTimelineAlbum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Contracts\Http\Requests;

use App\Enum\TimelineAlbumGranularity;

interface HasTimelineAlbum
{
/**
* @return TimelineAlbumGranularity|null
*/
public function album_timeline(): ?TimelineAlbumGranularity;
}
13 changes: 13 additions & 0 deletions app/Contracts/Http/Requests/HasTimelinePhoto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Contracts\Http\Requests;

use App\Enum\TimelinePhotoGranularity;

interface HasTimelinePhoto
{
/**
* @return TimelinePhotoGranularity|null
*/
public function photo_timeline(): ?TimelinePhotoGranularity;
}
2 changes: 2 additions & 0 deletions app/Contracts/Http/Requests/RequestAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class RequestAttribute
public const ALBUM_SORTING_COLUMN_ATTRIBUTE = 'album_sorting_column';
public const ALBUM_SORTING_ORDER_ATTRIBUTE = 'album_sorting_order';
public const ALBUM_PHOTO_LAYOUT = 'photo_layout';
public const ALBUM_TIMELINE_ALBUM = 'album_timeline';
public const ALBUM_TIMELINE_PHOTO = 'photo_timeline';

public const PERMISSION_ID = 'perm_id';
public const IS_COMPACT_ATTRIBUTE = 'is_compact';
Expand Down
15 changes: 15 additions & 0 deletions app/Enum/TimelineAlbumGranularity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Enum;

/**
* Defines the possible granularities for album timelines.
*/
enum TimelineAlbumGranularity: string
{
case DEFAULT = 'default';
case DISABLED = 'disabled';
case YEAR = 'year';
case MONTH = 'month';
case DAY = 'day';
}
16 changes: 16 additions & 0 deletions app/Enum/TimelinePhotoGranularity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Enum;

/**
* Defines the possible granularities for photo timelines.
*/
enum TimelinePhotoGranularity: string
{
case DEFAULT = 'default';
case DISABLED = 'disabled';
case YEAR = 'year';
case MONTH = 'month';
case DAY = 'day';
case HOUR = 'hour';
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/Gallery/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public function updateAlbum(UpdateAlbumRequest $request, SetHeader $setHeader):
$album->album_sorting = $request->albumSortingCriterion();
$album->photo_layout = $request->photoLayout();

$album->album_timeline = $request->album_timeline();
$album->photo_timeline = $request->photo_timeline();

$album = $setHeader->do(
album: $album,
is_compact: $request->is_compact(),
Expand All @@ -133,6 +136,7 @@ public function updateTagAlbum(UpdateTagAlbumRequest $request): EditableBaseAlbu
$album->copyright = $request->copyright();
$album->photo_sorting = $request->photoSortingCriterion();
$album->photo_layout = $request->photoLayout();
$album->photo_timeline = $request->photo_timeline();
$album->save();

return EditableBaseAlbumResource::fromModel($album);
Expand Down
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ class Kernel extends HttpKernel
'login_required' => \App\Http\Middleware\LoginRequired::class,
'cache_control' => \App\Http\Middleware\CacheControl::class,
'support' => \LycheeVerify\Http\Middleware\VerifySupporterStatus::class,
'config_integrity' => \App\Http\Middleware\ConfigIntegrity::class,
];
}
47 changes: 47 additions & 0 deletions app/Http/Middleware/ConfigIntegrity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

// app/Http/Middleware/CacheControlMiddleware.php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
* Small update of the database to avoid sneaky people setting all the levels to 0 and thus skipping some checks...
*/
class ConfigIntegrity
{
private const SE_FIELDS = [
'default_user_quota',
'timeline_photos_granularity',
'timeline_albums_granularity',
'timeline_left_border_enabled',
'timeline_photo_date_format_year',
'timeline_photo_date_format_month',
'timeline_photo_date_format_day',
'timeline_photo_date_format_hour',
'timeline_album_date_format_year',
'timeline_album_date_format_month',
'timeline_album_date_format_day',
];

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
*
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, \Closure $next)
{
try {
DB::table('configs')->whereIn('key', self::SE_FIELDS)->update(['level' => 1]);
} catch (\Exception $e) {
// Do nothing: we are not installed yet, so we fail silently.
}

return $next($request);
}
}
16 changes: 15 additions & 1 deletion app/Http/Requests/Album/UpdateAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use App\Contracts\Http\Requests\HasPhoto;
use App\Contracts\Http\Requests\HasPhotoLayout;
use App\Contracts\Http\Requests\HasPhotoSortingCriterion;
use App\Contracts\Http\Requests\HasTimelineAlbum;
use App\Contracts\Http\Requests\HasTimelinePhoto;
use App\Contracts\Http\Requests\HasTitle;
use App\Contracts\Http\Requests\RequestAttribute;
use App\Contracts\Models\AbstractAlbum;
Expand All @@ -22,6 +24,8 @@
use App\Enum\LicenseType;
use App\Enum\OrderSortingType;
use App\Enum\PhotoLayoutType;
use App\Enum\TimelineAlbumGranularity;
use App\Enum\TimelinePhotoGranularity;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasAlbumSortingCriterionTrait;
use App\Http\Requests\Traits\HasAlbumTrait;
Expand All @@ -33,19 +37,22 @@
use App\Http\Requests\Traits\HasPhotoLayoutTrait;
use App\Http\Requests\Traits\HasPhotoSortingCriterionTrait;
use App\Http\Requests\Traits\HasPhotoTrait;
use App\Http\Requests\Traits\HasTimelineAlbumTrait;
use App\Http\Requests\Traits\HasTimelinePhotoTrait;
use App\Http\Requests\Traits\HasTitleTrait;
use App\Models\Album;
use App\Models\Photo;
use App\Policies\AlbumPolicy;
use App\Rules\CopyrightRule;
use App\Rules\DescriptionRule;
use App\Rules\EnumRequireSupportRule;
use App\Rules\RandomIDRule;
use App\Rules\TitleRule;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\ValidationException;

class UpdateAlbumRequest extends BaseApiRequest implements HasAlbum, HasTitle, HasDescription, HasLicense, HasPhotoSortingCriterion, HasAlbumSortingCriterion, HasCopyright, HasPhoto, HasCompactBoolean, HasPhotoLayout
class UpdateAlbumRequest extends BaseApiRequest implements HasAlbum, HasTitle, HasDescription, HasLicense, HasPhotoSortingCriterion, HasAlbumSortingCriterion, HasCopyright, HasPhoto, HasCompactBoolean, HasPhotoLayout, HasTimelineAlbum, HasTimelinePhoto
{
use HasAlbumTrait;
use HasLicenseTrait;
Expand All @@ -58,6 +65,8 @@ class UpdateAlbumRequest extends BaseApiRequest implements HasAlbum, HasTitle, H
use HasAlbumSortingCriterionTrait;
use HasCopyrightTrait;
use HasPhotoLayoutTrait;
use HasTimelineAlbumTrait;
use HasTimelinePhotoTrait;

public function authorize(): bool
{
Expand Down Expand Up @@ -92,6 +101,8 @@ public function rules(): array
RequestAttribute::COPYRIGHT_ATTRIBUTE => ['present', 'nullable', new CopyrightRule()],
RequestAttribute::IS_COMPACT_ATTRIBUTE => ['required', 'boolean'],
RequestAttribute::HEADER_ID_ATTRIBUTE => ['present', new RandomIDRule(true)],
RequestAttribute::ALBUM_TIMELINE_ALBUM => ['present', 'nullable', new Enum(TimelineAlbumGranularity::class), new EnumRequireSupportRule(TimelinePhotoGranularity::class, [TimelinePhotoGranularity::DEFAULT, TimelinePhotoGranularity::DISABLED], $this->verify)],
RequestAttribute::ALBUM_TIMELINE_PHOTO => ['present', 'nullable', new Enum(TimelinePhotoGranularity::class), new EnumRequireSupportRule(TimelinePhotoGranularity::class, [TimelinePhotoGranularity::DEFAULT, TimelinePhotoGranularity::DISABLED], $this->verify)],
];
}

Expand Down Expand Up @@ -129,6 +140,9 @@ protected function processValidatedValues(array $values, array $files): void

$this->aspectRatio = AspectRatioType::tryFrom($values[RequestAttribute::ALBUM_ASPECT_RATIO_ATTRIBUTE]);
$this->photoLayout = PhotoLayoutType::tryFrom($values[RequestAttribute::ALBUM_PHOTO_LAYOUT]);
$this->album_timeline = TimelineAlbumGranularity::tryFrom($values[RequestAttribute::ALBUM_TIMELINE_ALBUM]);
$this->photo_timeline = TimelinePhotoGranularity::tryFrom($values[RequestAttribute::ALBUM_TIMELINE_PHOTO]);

$this->copyright = $values[RequestAttribute::COPYRIGHT_ATTRIBUTE];

$this->is_compact = static::toBoolean($values[RequestAttribute::IS_COMPACT_ATTRIBUTE]);
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Requests/Album/UpdateTagAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use App\Contracts\Http\Requests\HasPhotoSortingCriterion;
use App\Contracts\Http\Requests\HasTagAlbum;
use App\Contracts\Http\Requests\HasTags;
use App\Contracts\Http\Requests\HasTimelinePhoto;
use App\Contracts\Http\Requests\HasTitle;
use App\Contracts\Http\Requests\RequestAttribute;
use App\DTO\PhotoSortingCriterion;
use App\Enum\ColumnSortingPhotoType;
use App\Enum\OrderSortingType;
use App\Enum\PhotoLayoutType;
use App\Enum\TimelinePhotoGranularity;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumTrait;
use App\Http\Requests\Traits\HasCopyrightTrait;
Expand All @@ -22,16 +24,18 @@
use App\Http\Requests\Traits\HasPhotoSortingCriterionTrait;
use App\Http\Requests\Traits\HasTagAlbumTrait;
use App\Http\Requests\Traits\HasTagsTrait;
use App\Http\Requests\Traits\HasTimelinePhotoTrait;
use App\Http\Requests\Traits\HasTitleTrait;
use App\Models\TagAlbum;
use App\Rules\CopyrightRule;
use App\Rules\DescriptionRule;
use App\Rules\EnumRequireSupportRule;
use App\Rules\RandomIDRule;
use App\Rules\TitleRule;
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\ValidationException;

class UpdateTagAlbumRequest extends BaseApiRequest implements HasTagAlbum, HasTitle, HasDescription, HasPhotoSortingCriterion, HasCopyright, HasTags, HasPhotoLayout
class UpdateTagAlbumRequest extends BaseApiRequest implements HasTagAlbum, HasTitle, HasDescription, HasPhotoSortingCriterion, HasCopyright, HasTags, HasPhotoLayout, HasTimelinePhoto
{
use HasTagAlbumTrait;
use HasTitleTrait;
Expand All @@ -40,6 +44,7 @@ class UpdateTagAlbumRequest extends BaseApiRequest implements HasTagAlbum, HasTi
use HasCopyrightTrait;
use HasTagsTrait;
use HasPhotoLayoutTrait;
use HasTimelinePhotoTrait;
use AuthorizeCanEditAlbumTrait;

/**
Expand All @@ -60,6 +65,7 @@ public function rules(): array
RequestAttribute::TAGS_ATTRIBUTE . '.*' => 'required|string|min:1',
RequestAttribute::COPYRIGHT_ATTRIBUTE => ['present', 'nullable', new CopyrightRule()],
RequestAttribute::ALBUM_PHOTO_LAYOUT => ['present', 'nullable', new Enum(PhotoLayoutType::class)],
RequestAttribute::ALBUM_TIMELINE_PHOTO => ['present', 'nullable', new Enum(TimelinePhotoGranularity::class), new EnumRequireSupportRule(TimelinePhotoGranularity::class, [TimelinePhotoGranularity::DEFAULT, TimelinePhotoGranularity::DISABLED], $this->verify)],
];
}

Expand Down Expand Up @@ -88,6 +94,7 @@ protected function processValidatedValues(array $values, array $files): void
new PhotoSortingCriterion($photoColumn->toColumnSortingType(), $photoOrder);

$this->photoLayout = PhotoLayoutType::tryFrom($values[RequestAttribute::ALBUM_PHOTO_LAYOUT]);
$this->photo_timeline = TimelinePhotoGranularity::tryFrom($values[RequestAttribute::ALBUM_TIMELINE_PHOTO]);
$this->copyright = $values[RequestAttribute::COPYRIGHT_ATTRIBUTE];
$this->tags = $values[RequestAttribute::TAGS_ATTRIBUTE];
}
Expand Down
18 changes: 18 additions & 0 deletions app/Http/Requests/Traits/HasTimelineAlbumTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Requests\Traits;

use App\Enum\TimelineAlbumGranularity;

trait HasTimelineAlbumTrait
{
protected ?TimelineAlbumGranularity $album_timeline = null;

/**
* @return TimelineAlbumGranularity|null
*/
public function album_timeline(): ?TimelineAlbumGranularity
{
return $this->album_timeline;
}
}
18 changes: 18 additions & 0 deletions app/Http/Requests/Traits/HasTimelinePhotoTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Requests\Traits;

use App\Enum\TimelinePhotoGranularity;

trait HasTimelinePhotoTrait
{
protected ?TimelinePhotoGranularity $photo_timeline = null;

/**
* @return TimelinePhotoGranularity|null
*/
public function photo_timeline(): ?TimelinePhotoGranularity
{
return $this->photo_timeline;
}
}
7 changes: 7 additions & 0 deletions app/Http/Resources/Collections/RootAlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace App\Http\Resources\Collections;

use App\DTO\TopAlbumDTO;
use App\Enum\ColumnSortingType;
use App\Enum\TimelineAlbumGranularity;
use App\Http\Resources\GalleryConfigs\RootConfig;
use App\Http\Resources\Models\ThumbAlbumResource;
use App\Http\Resources\Models\Utils\TimelineData;
use App\Http\Resources\Rights\RootAlbumRightsResource;
use App\Models\Configs;
use Illuminate\Support\Collection;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
Expand Down Expand Up @@ -54,6 +58,9 @@ public function __construct(
$this->smart_albums = $smart_albums;
$this->tag_albums = $tag_albums;
$this->albums = $albums;
$sorting = Configs::getValueAsEnum('sorting_albums_col', ColumnSortingType::class);
$album_granularity = Configs::getValueAsEnum('timeline_albums_granularity', TimelineAlbumGranularity::class);
$this->albums = TimelineData::setTimeLineDataForAlbums($this->albums, $sorting, $album_granularity);
$this->shared_albums = $shared_albums;
$this->config = $config;
$this->rights = $rights;
Expand Down
7 changes: 7 additions & 0 deletions app/Http/Resources/Editable/EditableBaseAlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Enum\AspectRatioType;
use App\Enum\LicenseType;
use App\Enum\PhotoLayoutType;
use App\Enum\TimelineAlbumGranularity;
use App\Enum\TimelinePhotoGranularity;
use App\Models\Album;
use App\Models\TagAlbum;
use Spatie\LaravelData\Data;
Expand All @@ -26,6 +28,8 @@ class EditableBaseAlbumResource extends Data
public ?PhotoLayoutType $photo_layout;
public ?string $header_id;
public ?string $cover_id;
public ?TimelineAlbumGranularity $album_timeline;
public ?TimelinePhotoGranularity $photo_timeline;
/** @var string[] */
public array $tags;
public bool $is_model_album;
Expand All @@ -43,6 +47,8 @@ public function __construct(Album|TagAlbum $album)
$this->header_id = null;
$this->cover_id = null;
$this->photo_layout = $album->photo_layout;
$this->album_timeline = null;
$this->photo_timeline = $album->photo_timeline;

if ($album instanceof Album) {
$this->is_model_album = true;
Expand All @@ -51,6 +57,7 @@ public function __construct(Album|TagAlbum $album)
$this->header_id = $album->header_id;
$this->cover_id = $album->cover_id;
$this->aspect_ratio = $album->album_thumb_aspect_ratio;
$this->album_timeline = $album->album_timeline;
}

if ($album instanceof TagAlbum) {
Expand Down
Loading

0 comments on commit 98d012d

Please sign in to comment.