-
-
Notifications
You must be signed in to change notification settings - Fork 362
Add 2 new smart albums: My rated pictures and My best rated pictures #4041
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
Merged
+2,063
−8
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ee630a
Add 2 new smart albums: My rated pictures and My best rated pictures
ildyria 0542694
fix phpstan & tests
ildyria 453a2b3
fix tests
ildyria bc9d8d0
more tests
ildyria 0a12a3c
complete
ildyria d66ced0
fix
ildyria 22fcc7b
do not rate but insert the value directly
ildyria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| namespace App\SmartAlbums; | ||
|
|
||
| use App\Enum\SmartAlbumType; | ||
| use App\Exceptions\ConfigurationKeyMissingException; | ||
| use App\Exceptions\Internal\FrameworkException; | ||
| use App\Exceptions\Internal\InvalidOrderDirectionException; | ||
| use App\Exceptions\Internal\InvalidQueryModelException; | ||
| use App\Models\Photo; | ||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Pagination\LengthAwarePaginator; | ||
| use Illuminate\Support\Facades\Auth; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| /** | ||
| * Smart album containing the top N highest-rated photos by the current user. | ||
| * Photos with the same rating as the Nth photo are included (ties). | ||
| * Only visible to authenticated users with Lychee SE license. | ||
| */ | ||
| class MyBestPicturesAlbum extends BaseSmartAlbum | ||
| { | ||
| public const ID = SmartAlbumType::MY_BEST_PICTURES->value; | ||
|
|
||
| /** | ||
| * @throws ConfigurationKeyMissingException | ||
| * @throws FrameworkException | ||
| */ | ||
| protected function __construct() | ||
| { | ||
| // The condition filters for photos that have been rated by current user | ||
| // The tie-inclusion logic is handled in getPhotosAttribute() | ||
| parent::__construct( | ||
| id: SmartAlbumType::MY_BEST_PICTURES, | ||
| smart_condition: fn (Builder $q) => $q->whereHas('ratings', function ($query): void { | ||
| $query->where('user_id', '=', Auth::id() ?? 0); | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| public static function getInstance(): self | ||
| { | ||
| return new self(); | ||
| } | ||
|
|
||
| /** | ||
| * Override to implement tie-inclusion logic for top N photos rated by current user. | ||
| * | ||
| * @return LengthAwarePaginator<int,Photo> | ||
| * | ||
| * @throws InvalidOrderDirectionException | ||
| * @throws InvalidQueryModelException | ||
| */ | ||
| protected function getPhotosAttribute(): LengthAwarePaginator | ||
| { | ||
| if ($this->photos !== null) { | ||
| return $this->photos; | ||
| } | ||
|
|
||
| $limit = $this->config_manager->getValueAsInt('my_best_pictures_count'); | ||
|
|
||
| // Get the Nth photo's rating to determine the cutoff | ||
| $cutoff_rating = $this->getCutoffRating($limit); | ||
|
|
||
| if ($cutoff_rating === null) { | ||
| // No photos with ratings from this user, return empty paginator | ||
| $this->photos = new LengthAwarePaginator([], 0, $limit); | ||
|
|
||
| return $this->photos; | ||
| } | ||
|
|
||
| // Include all photos with user rating >= cutoff (this handles ties) | ||
| // We need to join with photo_ratings to filter by the user's rating | ||
| $query = $this->photos() | ||
| ->join('photo_ratings as pr_filter', function ($join) use ($cutoff_rating): void { | ||
| $join->on('photos.id', '=', 'pr_filter.photo_id') | ||
| ->where('pr_filter.user_id', '=', Auth::id() ?? 0) | ||
| ->where('pr_filter.rating', '>=', $cutoff_rating); | ||
| }) | ||
| ->select('photos.*'); // Ensure we only select photo columns | ||
|
|
||
| // Always sort by user's rating DESC for My Best Pictures | ||
| // We already have the join from above, so order by that rating | ||
| $query = $query->orderByRaw('pr_filter.rating DESC') | ||
| ->orderBy('photos.created_at', 'DESC'); | ||
|
|
||
| /** @var LengthAwarePaginator<int,Photo> $photos */ | ||
| $photos = $query->paginate($this->config_manager->getValueAsInt('photos_pagination_limit')); | ||
|
|
||
| $this->photos = $photos; | ||
|
|
||
| return $this->photos; | ||
| } | ||
|
|
||
| /** | ||
| * Get the rating of the Nth photo (by current user) to use as the cutoff. | ||
| * | ||
| * @param int $limit the N for "top N photos" | ||
| * | ||
| * @return int|null the rating at position N, or null if fewer than N rated photos exist | ||
| */ | ||
| private function getCutoffRating(int $limit): ?int | ||
| { | ||
| $user_id = Auth::id() ?? 0; | ||
|
|
||
| // Get the Nth highest rating from current user | ||
| $nth_rating = DB::table('photo_ratings') | ||
| ->where('user_id', '=', $user_id) | ||
| ->join('photos', 'photo_ratings.photo_id', '=', 'photos.id') | ||
| ->orderBy('photo_ratings.rating', 'DESC') | ||
| ->skip($limit - 1) | ||
| ->take(1) | ||
| ->value('photo_ratings.rating'); | ||
|
|
||
| if ($nth_rating === null) { | ||
| // Fewer than N photos with ratings from this user exist | ||
| // Get the lowest rating among existing rated photos from this user | ||
| $lowest_rating = DB::table('photo_ratings') | ||
| ->where('user_id', '=', $user_id) | ||
| ->join('photos', 'photo_ratings.photo_id', '=', 'photos.id') | ||
| ->orderBy('photo_ratings.rating', 'ASC') | ||
| ->value('photo_ratings.rating'); | ||
|
|
||
| return $lowest_rating; | ||
| } | ||
|
|
||
| return $nth_rating; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| namespace App\SmartAlbums; | ||
|
|
||
| use App\Contracts\Exceptions\InternalLycheeException; | ||
| use App\Enum\SmartAlbumType; | ||
| use App\Exceptions\ConfigurationKeyMissingException; | ||
| use App\Exceptions\Internal\FrameworkException; | ||
| use App\Models\Photo; | ||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Support\Facades\Auth; | ||
|
|
||
| /** | ||
| * Smart album containing all photos rated by the current user. | ||
| * Shows photos ordered by user's rating (highest first), then by creation date (newest first). | ||
| * Only visible to authenticated users. | ||
| */ | ||
| class MyRatedPicturesAlbum extends BaseSmartAlbum | ||
| { | ||
| public const ID = SmartAlbumType::MY_RATED_PICTURES->value; | ||
|
|
||
| /** | ||
| * @throws ConfigurationKeyMissingException | ||
| * @throws FrameworkException | ||
| */ | ||
| protected function __construct() | ||
| { | ||
| // The condition filters for photos that have been rated by current user | ||
| parent::__construct( | ||
| id: SmartAlbumType::MY_RATED_PICTURES, | ||
| smart_condition: fn (Builder $q) => $q->whereHas('ratings', function ($query): void { | ||
| $query->where('user_id', '=', Auth::id() ?? 0); | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| public static function getInstance(): self | ||
| { | ||
| return new self(); | ||
| } | ||
|
|
||
| /** | ||
| * Override to add custom ordering: user's rating DESC, then created_at DESC. | ||
| * | ||
| * @return \App\Eloquent\FixedQueryBuilder<Photo> | ||
| * | ||
| * @throws InternalLycheeException | ||
| */ | ||
| public function photos(): Builder | ||
| { | ||
| // Get base query from parent (includes security filtering and smart condition) | ||
| $query = parent::photos(); | ||
|
|
||
| // Add join with photo_ratings to access the user's rating for ordering | ||
| // Use leftJoin since the whereHas in smart_condition already ensures ratings exist | ||
| return $query | ||
| ->leftJoin('photo_ratings', function ($join): void { | ||
| $join->on('photos.id', '=', 'photo_ratings.photo_id') | ||
| ->where('photo_ratings.user_id', '=', Auth::id() ?? 0); | ||
| }) | ||
| ->orderByRaw('photo_ratings.rating DESC') | ||
| ->orderBy('photos.created_at', 'DESC') | ||
| ->select('photos.*'); // Ensure we only select photo columns | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.