-
-
Notifications
You must be signed in to change notification settings - Fork 363
Add rating per user. #3899
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
Merged
Add rating per user. #3899
Changes from 34 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
ae74a55
specification
ildyria ef0ee27
first stab
ildyria 6032535
Fix formatting and phpstan
ildyria 8efe6d1
fix migration
ildyria f1567e5
test: add concurrency tests for photo rating feature (I10-I11)
ildyria f826563
feat: add frontend service layer for photo rating (I7)
ildyria 2d8fd7a
feat: add photo rating widget to details drawer (I8-I9)
ildyria be3fc7e
feat: add rating overlays to photo thumbs and full views (I9a-I9d)
ildyria 69dd341
docs: mark I9b-I9d as complete in tasks.md
ildyria 0af4a04
feat: add config settings for rating visibility (I12a)
ildyria b56f9f8
refactor: use VisibilityType enum for rating view modes
ildyria 30a8677
refactor: use BaseConfigMigration for rating config
ildyria c9ddae7
docs: mark I12a as complete in tasks.md
ildyria 52e5a5a
docs: update documentation for photo rating feature (I12)
ildyria 6571da9
docs: mark I12 and I13 complete - feature fully implemented
ildyria 3a4d449
docs: add I14 increment for N+1 query performance optimization
ildyria a567aad
remove stupid timestamps
ildyria 39845e9
Formatting
ildyria c5634a8
refactor settings
ildyria 6f68773
fix tests
ildyria 90302a6
minore refactoring
ildyria 96845a4
refine front-end
ildyria bbc1f93
improve UI/UX
ildyria 96e9f69
fix test
ildyria 1761fc8
fix test
ildyria 6ea0d61
fixes
ildyria 3ab5743
fix factory
ildyria 707c20d
add GD coverage back
ildyria f549ee5
add relationship + eager loading
ildyria ea250fa
Fix
ildyria d55f628
fix star/header/cover refresh issues
ildyria 01845fc
fix pgsql request
ildyria 0f05b74
translations
ildyria 823d0d0
more UI/UX fixes
ildyria b809b74
fixes
ildyria 9a23d4d
formatting
ildyria 84d51cb
add more strict requirements
ildyria 58c6367
more fix
ildyria 9b7fb5a
more fix
ildyria 74b1e5e
Fix tests
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
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,103 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2025 LycheeOrg. | ||
| */ | ||
|
|
||
| namespace App\Actions\Photo; | ||
|
|
||
| use App\Exceptions\ConflictingPropertyException; | ||
| use App\Models\Photo; | ||
| use App\Models\PhotoRating; | ||
| use App\Models\Statistics; | ||
| use App\Models\User; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| class Rating | ||
| { | ||
| /** | ||
| * Set or update the rating for the photo. | ||
| * | ||
| * Handles: | ||
| * - Creating new ratings (rating > 0, no existing rating) | ||
| * - Updating existing ratings (rating > 0, existing rating) | ||
| * - Removing ratings (rating == 0) | ||
| * - Atomic statistics updates | ||
| * | ||
| * @param Photo $photo The photo to rate | ||
| * @param User $user The user rating the photo | ||
| * @param int $rating The rating value (0-5, where 0 removes the rating) | ||
| * | ||
| * @return Photo the photo with refreshed statistics | ||
| * | ||
| * @throws ConflictingPropertyException if a database conflict occurs during the transaction | ||
| */ | ||
| public function do(Photo $photo, User $user, int $rating): Photo | ||
| { | ||
| try { | ||
| DB::transaction(function () use ($photo, $user, $rating): void { | ||
| // Ensure statistics record exists atomically (Q001-07) | ||
| $statistics = Statistics::firstOrCreate( | ||
| ['photo_id' => $photo->id], | ||
| [ | ||
| 'album_id' => null, | ||
| 'visit_count' => 0, | ||
| 'download_count' => 0, | ||
| 'favourite_count' => 0, | ||
| 'shared_count' => 0, | ||
| 'rating_sum' => 0, | ||
| 'rating_count' => 0, | ||
| ] | ||
| ); | ||
|
|
||
| if ($rating > 0) { | ||
| // Find existing rating by this user for this photo | ||
| $existing_rating = PhotoRating::where('photo_id', $photo->id) | ||
| ->where('user_id', $user->id) | ||
| ->first(); | ||
|
|
||
| if ($existing_rating !== null) { | ||
| // Update: adjust statistics delta | ||
| $delta = $rating - $existing_rating->rating; | ||
| $statistics->rating_sum += $delta; | ||
| $existing_rating->rating = $rating; | ||
| $existing_rating->save(); | ||
| } else { | ||
| // Insert: create new rating and increment statistics | ||
| PhotoRating::create([ | ||
| 'photo_id' => $photo->id, | ||
| 'user_id' => $user->id, | ||
| 'rating' => $rating, | ||
| ]); | ||
| $statistics->rating_sum += $rating; | ||
| $statistics->rating_count++; | ||
| } | ||
|
|
||
| $statistics->save(); | ||
| } else { | ||
| // Rating == 0: remove rating (idempotent, Q001-06) | ||
| $existing_rating = PhotoRating::where('photo_id', $photo->id) | ||
| ->where('user_id', $user->id) | ||
| ->first(); | ||
|
|
||
| if ($existing_rating !== null) { | ||
| $statistics->rating_sum -= $existing_rating->rating; | ||
| $statistics->rating_count--; | ||
| $statistics->save(); | ||
| $existing_rating->delete(); | ||
| } | ||
| // If no existing rating, do nothing (idempotent) | ||
| } | ||
| }); | ||
|
|
||
| // Reload photo with fresh statistics | ||
| $photo->refresh(); | ||
|
|
||
| return $photo; | ||
| } catch (\Throwable $e) { | ||
| throw new ConflictingPropertyException('Failed to update photo rating due to a conflict. Please try again.', $e); | ||
| } | ||
| } | ||
| } |
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
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,65 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2025 LycheeOrg. | ||
| */ | ||
|
|
||
| namespace App\Http\Requests\Photo; | ||
|
|
||
| use App\Contracts\Http\Requests\HasPhoto; | ||
| use App\Contracts\Http\Requests\RequestAttribute; | ||
| use App\Http\Requests\BaseApiRequest; | ||
| use App\Http\Requests\Traits\HasPhotoTrait; | ||
| use App\Models\Photo; | ||
| use App\Policies\PhotoPolicy; | ||
| use App\Rules\RandomIDRule; | ||
| use Illuminate\Support\Facades\Gate; | ||
|
|
||
| /** | ||
| * Class SetPhotoRatingRequest. | ||
| */ | ||
| class SetPhotoRatingRequest extends BaseApiRequest implements HasPhoto | ||
| { | ||
| use HasPhotoTrait; | ||
|
|
||
| protected int $rating; | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return Gate::check(PhotoPolicy::CAN_SEE, [Photo::class, $this->photo]); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| RequestAttribute::PHOTO_ID_ATTRIBUTE => ['required', new RandomIDRule(false)], | ||
| RequestAttribute::RATING_ATTRIBUTE => 'required|integer|min:0|max:5', | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| protected function processValidatedValues(array $values, array $files): void | ||
| { | ||
| /** @var ?string $photo_id */ | ||
| $photo_id = $values[RequestAttribute::PHOTO_ID_ATTRIBUTE]; | ||
| $this->photo = Photo::query() | ||
| ->with(['albums']) | ||
| ->findOrFail($photo_id); | ||
| $this->rating = intval($values[RequestAttribute::RATING_ATTRIBUTE]); | ||
| } | ||
|
|
||
| public function rating(): int | ||
| { | ||
| return $this->rating; | ||
| } | ||
| } |
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.