Skip to content

Commit

Permalink
Support timeline in albums and album (#2681)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Nov 25, 2024
1 parent 1a0673a commit 7b157f3
Show file tree
Hide file tree
Showing 42 changed files with 893 additions and 80 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
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
25 changes: 25 additions & 0 deletions app/Http/Resources/GalleryConfigs/AlbumConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use App\Enum\AspectRatioCSSType;
use App\Enum\AspectRatioType;
use App\Enum\PhotoLayoutType;
use App\Enum\TimelineAlbumGranularity;
use App\Enum\TimelinePhotoGranularity;
use App\Http\Resources\Traits\HasTimelineData;
use App\Models\Album;
use App\Models\Configs;
use App\Models\Extensions\BaseAlbum;
Expand All @@ -18,6 +21,8 @@
#[TypeScript()]
class AlbumConfig extends Data
{
use HasTimelineData;

public bool $is_base_album;
public bool $is_model_album;
public bool $is_accessible;
Expand All @@ -28,6 +33,8 @@ class AlbumConfig extends Data
public bool $is_nsfw_warning_visible;
public AspectRatioCSSType $album_thumb_css_aspect_ratio;
public PhotoLayoutType $photo_layout;
public bool $is_album_timeline_enabled = false;
public bool $is_photo_timeline_enabled = false;

public function __construct(AbstractAlbum $album)
{
Expand All @@ -53,6 +60,24 @@ public function __construct(AbstractAlbum $album)
}

$this->photo_layout = (($album instanceof BaseAlbum) ? $album->photo_layout : null) ?? Configs::getValueAsEnum('layout', PhotoLayoutType::class);

// Set default values.
$this->is_photo_timeline_enabled = Configs::getValueAsBool('timeline_photos_enabled');
$this->is_album_timeline_enabled = Configs::getValueAsBool('timeline_albums_enabled');

if ($album instanceof Album) {
$this->is_album_timeline_enabled = $album->album_timeline !== null || $this->is_album_timeline_enabled;
$this->is_album_timeline_enabled = $album->album_timeline !== TimelineAlbumGranularity::DISABLED && $this->is_album_timeline_enabled;
}

if ($album instanceof BaseAlbum) {
$this->is_photo_timeline_enabled = $album->photo_timeline !== null || $this->is_photo_timeline_enabled;
$this->is_photo_timeline_enabled = $album->photo_timeline !== TimelinePhotoGranularity::DISABLED && $this->is_photo_timeline_enabled;
}

// Masking to require login for timeline or allow it to be public.
$this->is_photo_timeline_enabled = $this->is_photo_timeline_enabled && (Configs::getValueAsBool('timeline_photos_public') || Auth::check());
$this->is_album_timeline_enabled = $this->is_album_timeline_enabled && (Configs::getValueAsBool('timeline_albums_public') || Auth::check());
}

public function setIsMapAccessible(): void
Expand Down
6 changes: 6 additions & 0 deletions app/Http/Resources/GalleryConfigs/InitConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class InitConfig extends Data
// Slideshow setting
public int $slideshow_timeout;

// Timeline settings
public bool $is_timeline_left_border_visible;

// Site title & dropbox key if logged in as admin.
public string $title;
public string $dropbox_api_key;
Expand Down Expand Up @@ -91,6 +94,9 @@ public function __construct()
// Slideshow settings
$this->slideshow_timeout = Configs::getValueAsInt('slideshow_timeout');

// Timeline settings
$this->is_timeline_left_border_visible = Configs::getValueAsBool('timeline_left_border_enabled');

// Site title & dropbox key if logged in as admin.
$this->title = Configs::getValueAsString('site_title');
$this->dropbox_api_key = Auth::user()?->may_administrate === true ? Configs::getValueAsString('dropbox_key') : 'disabled';
Expand Down
Loading

0 comments on commit 7b157f3

Please sign in to comment.