Skip to content

Refactor tag system #3424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Actions/Album/CreateTagAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use App\Exceptions\ModelDBException;
use App\Exceptions\UnauthenticatedException;
use App\Models\Tag;
use App\Models\TagAlbum;
use Illuminate\Support\Facades\Auth;

Expand All @@ -33,7 +34,7 @@ public function create(string $title, array $show_tags): TagAlbum

$album = new TagAlbum();
$album->title = $title;
$album->show_tags = $show_tags;
$album->show_tags = Tag::from($show_tags)->all();
$album->owner_id = $user_id;
$album->save();
$this->setStatistics($album);
Expand Down
1 change: 1 addition & 0 deletions app/Actions/Album/PositionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function get(AbstractAlbum $album, bool $include_sub_albums = false): Pos
$r->whereBetween('type', [SizeVariantType::SMALL2X, SizeVariantType::THUMB]);
},
'palette',
'tags',
])
->whereNotNull('latitude')
->whereNotNull('longitude');
Expand Down
1 change: 1 addition & 0 deletions app/Actions/Albums/PositionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function do(): PositionDataResource
$r->whereBetween('type', [SizeVariantType::SMALL2X, SizeVariantType::THUMB]);
},
'palette',
'tags',
])
->whereNotNull('latitude')
->whereNotNull('longitude'),
Expand Down
3 changes: 2 additions & 1 deletion app/Actions/Photo/Pipes/Shared/HydrateMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function handle(DuplicateDTO|StandaloneDTO $state, \Closure $next): Dupli
$state->photo->description = $state->exif_info->description;
}
if (count($state->photo->tags) === 0) {
$state->photo->tags = $state->exif_info->tags;
// TODO FIX ME.
// $state->photo->tags = $state->exif_info->tags;
}
if ($state->photo->type === null) {
$state->photo->type = $state->exif_info->type;
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Search/PhotoSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function query(array $terms): Collection
public function sqlQuery(array $terms, ?Album $album = null): Builder
{
$query = $this->photoQueryPolicy->applySearchabilityFilter(
query: Photo::query()->with(['album', 'statistics', 'size_variants', 'palette']),
query: Photo::query()->with(['album', 'statistics', 'size_variants', 'palette', 'tags']),
origin: $album,
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_search')
);
Expand All @@ -67,7 +67,7 @@ public function sqlQuery(array $terms, ?Album $album = null): Builder
fn (FixedQueryBuilder $query) => $query
->where('title', 'like', '%' . $term . '%')
->orWhere('description', 'like', '%' . $term . '%')
->orWhere('tags', 'like', '%' . $term . '%')
// ->orWhere('tags', 'like', '%' . $term . '%')
->orWhere('location', 'like', '%' . $term . '%')
->orWhere('model', 'like', '%' . $term . '%')
->orWhere('taken_at', 'like', '%' . $term . '%')
Expand Down
27 changes: 16 additions & 11 deletions app/Casts/ArrayCast.php → app/Casts/TagArrayCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,53 @@

namespace App\Casts;

use App\Models\Tag;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

/**
* @implements CastsAttributes<array,(string|null)[]>
* @implements CastsAttributes<array,(Tag|null)[]>
*/
class ArrayCast implements CastsAttributes
class TagArrayCast implements CastsAttributes
{
/**
* @param Model $model the associated model class
* @param string $key the name of the SQL column holding the stringified array
* @param mixed $value the stringified array
* @param array<string,mixed> $attributes all SQL attributes of the entity
*
* @return array<int,string> the array
* @return array<int,Tag> the array
*/
public function get(Model $model, string $key, mixed $value, array $attributes): array
{
return ($value === null || $value === '') ? [] : explode(',', strval($value));
if ($value === null || $value === '') {
return [];
}

// Split the string by ' OR ' and return an array of Tag objects
return Tag::whereIn('id', explode(' OR ', strval($value)))->get()->all();
}

/**
* @param Model $model the associated model class
* @param string $key the name of the SQL column holding the stringified array
* @param (string|null)[]|null $value the array
* @param array<string,mixed> $attributes
* @param Model $model the associated model class
* @param string $key the name of the SQL column holding the stringified array
* @param (Tag|null)[]|null $value the array
* @param array<string,mixed> $attributes
*
* @return array<string,mixed> An associative map of SQL columns and their values
*/
public function set(Model $model, string $key, mixed $value, array $attributes): array
{
// Normalize the input value
// The array must not contain empty tags and tags which contain a comma
// TODO: Either use a separate table to store the tags or another encoding (e.g. JSON) which also allows commas in tags

$arr = !is_array($value) ? [] : array_values(array_filter(
$value,
fn ($elem) => ($elem !== null && $elem !== '' && !str_contains($elem, ',')),
fn ($elem) => ($elem !== null && $elem !== '' && $elem instanceof Tag && $elem->name !== ''),
));

return [
$key => count($arr) === 0 ? null : implode(',', $arr),
$key => count($arr) === 0 ? null : implode(' OR ', array_map(fn ($t) => $t->id, $arr)),
];
}
}
2 changes: 1 addition & 1 deletion app/Factories/AlbumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function findBaseAlbumOrFail(string $album_id, bool $with_relations = tru
$tag_album_query = TagAlbum::query();

if ($with_relations) {
$album_query->with(['access_permissions', 'photos', 'children', 'children.owner', 'photos.size_variants', 'photos.statistics', 'photos.palette']);
$album_query->with(['access_permissions', 'photos', 'children', 'children.owner', 'photos.size_variants', 'photos.statistics', 'photos.palette', 'photos.tags']);
$tag_album_query->with(['photos']);
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Gallery/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use App\Http\Resources\Models\Utils\AlbumProtectionPolicy;
use App\Models\Album;
use App\Models\Extensions\BaseAlbum;
use App\Models\Tag;
use App\Models\TagAlbum;
use App\SmartAlbums\BaseSmartAlbum;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -165,7 +166,7 @@ public function updateTagAlbum(UpdateTagAlbumRequest $request): EditableBaseAlbu
}
$album->title = $request->title();
$album->description = $request->description();
$album->show_tags = $request->tags();
$album->show_tags = Tag::from($request->tags())->all();
$album->copyright = $request->copyright();
$album->photo_sorting = $request->photoSortingCriterion();
$album->photo_layout = $request->photoLayout();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Gallery/FrameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ private function loadPhoto(AbstractAlbum|null $album, int $retries = 5): ?Photo
// default query
if ($album === null) {
$query = $this->photo_query_policy->applySearchabilityFilter(
query: Photo::query()->with(['album', 'size_variants', 'palette']),
query: Photo::query()->with(['album', 'size_variants', 'palette', 'tags']),
origin: null,
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_frame')
);
} else {
$query = $album->photos()->with(['album', 'size_variants', 'palette']);
$query = $album->photos()->with(['album', 'size_variants', 'palette', 'tags']);
}

/** @var ?Photo $photo */
Expand Down
29 changes: 20 additions & 9 deletions app/Http/Controllers/Gallery/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
use App\Jobs\ProcessImageJob;
use App\Models\Configs;
use App\Models\Photo;
use App\Models\Tag;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

/**
Expand Down Expand Up @@ -123,7 +125,10 @@ public function update(EditPhotoRequest $request): PhotoResource
$photo->title = $request->title();
$photo->description = $request->description();
$photo->created_at = $request->uploadDate();
$photo->tags = $request->tags();

$existing_tags = Tag::from($request->tags());
$photo->tags()->sync($existing_tags->pluck('id'));
$photo->load('tags');
$photo->license = $request->license()->value;

// if the request takenAt is null, then we set the initial value back.
Expand Down Expand Up @@ -202,15 +207,21 @@ public function rename(RenamePhotoRequest $request): void
public function tags(SetPhotosTagsRequest $request): void
{
$tags = $request->tags();
$photos = $request->photos();

/** @var Photo $photo */
foreach ($request->photos() as $photo) {
if ($request->shall_override) {
$photo->tags = $tags;
} else {
$photo->tags = array_unique(array_merge($photo->tags, $tags));
}
$photo->save();
// Fetch existing tags
$existing_tags = Tag::from($tags);

if ($request->shall_override) {
// Delete existing associations for those photos ids if we override the tags
DB::table('photos_tags')
->whereIn('photo_id', $photos->pluck('id'))
->delete();
}

// Associate the existing tags with the photos
$existing_tags->each(function (Tag $tag) use ($photos): void {
$tag->photos()->syncWithoutDetaching($photos->pluck('id'));
});
}
}
1 change: 1 addition & 0 deletions app/Http/Requests/Album/AddTagAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasTagsTrait;
use App\Http\Requests\Traits\HasTitleTrait;
use App\Models\Tag;
use App\Policies\AlbumPolicy;
use App\Rules\TitleRule;
use Illuminate\Support\Facades\Gate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct(Album|TagAlbum $album)
}

if ($album instanceof TagAlbum) {
$this->tags = $album->show_tags;
$this->tags = array_map(fn ($t) => $t->name, $album->show_tags);
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/PhotoResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function __construct(Photo $photo)
$this->original_checksum = $photo->original_checksum;
$this->shutter = $photo->shutter;
$this->size_variants = new SizeVariantsResouce($photo);
$this->tags = $photo->tags;
$this->tags = $photo->tags->pluck('name')->all();
$this->taken_at = $photo->taken_at?->toIso8601String();
$this->taken_at_orig_tz = $photo->taken_at_orig_tz;
$this->title = (Configs::getValueAsBool('file_name_hidden') && Auth::guest()) ? '' : $photo->title;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/TagAlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(TagAlbum $tag_album)
$this->title = $tag_album->title;
$this->owner_name = Auth::check() ? $tag_album->owner->name : null;
$this->is_tag_album = true;
$this->show_tags = $tag_album->show_tags;
$this->show_tags = array_map(fn ($t) => $t->name, $tag_album->show_tags);
$this->copyright = $tag_album->copyright;

// children
Expand Down
20 changes: 20 additions & 0 deletions app/Models/Builders/TagBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Models\Builders;

use App\Eloquent\FixedQueryBuilder;

/**
* Specialized query builder for {@link \App\Models\Tag}.
*
* @extends FixedQueryBuilder<\App\Models\Tag>
*/
class TagBuilder extends FixedQueryBuilder
{
}
Loading