diff --git a/app/Events/SongStartedPlaying.php b/app/Events/PlaybackStarted.php similarity index 85% rename from app/Events/SongStartedPlaying.php rename to app/Events/PlaybackStarted.php index a5172c09..0d75a3be 100644 --- a/app/Events/SongStartedPlaying.php +++ b/app/Events/PlaybackStarted.php @@ -6,7 +6,7 @@ use App\Models\Song; use App\Models\User; use Illuminate\Queue\SerializesModels; -class SongStartedPlaying extends Event +class PlaybackStarted extends Event { use SerializesModels; diff --git a/app/Http/Controllers/API/Interaction/HandlePlaybackStartedController.php b/app/Http/Controllers/API/Interaction/HandlePlaybackStartedController.php new file mode 100644 index 00000000..66474d87 --- /dev/null +++ b/app/Http/Controllers/API/Interaction/HandlePlaybackStartedController.php @@ -0,0 +1,26 @@ +increasePlayCount($request->song, $user); + event(new PlaybackStarted($interaction->song, $interaction->user)); + + return InteractionResource::make($interaction); + } +} diff --git a/app/Http/Controllers/API/Interaction/LikeController.php b/app/Http/Controllers/API/Interaction/LikeController.php deleted file mode 100644 index 813fa15e..00000000 --- a/app/Http/Controllers/API/Interaction/LikeController.php +++ /dev/null @@ -1,22 +0,0 @@ -json($this->interactionService->toggleLike($request->song, $this->user)); - } -} diff --git a/app/Http/Controllers/API/Interaction/PlayCountController.php b/app/Http/Controllers/API/Interaction/PlayCountController.php deleted file mode 100644 index a46d8d16..00000000 --- a/app/Http/Controllers/API/Interaction/PlayCountController.php +++ /dev/null @@ -1,27 +0,0 @@ -interactionService->increasePlayCount($request->song, $this->user); - event(new SongStartedPlaying($interaction->song, $interaction->user)); - - return InteractionResource::make($interaction); - } -} diff --git a/app/Http/Controllers/API/Interaction/ToggleLikeSongController.php b/app/Http/Controllers/API/Interaction/ToggleLikeSongController.php new file mode 100644 index 00000000..92e3b679 --- /dev/null +++ b/app/Http/Controllers/API/Interaction/ToggleLikeSongController.php @@ -0,0 +1,21 @@ +json($interactionService->toggleLike($request->song, $user)); + } +} diff --git a/app/Http/Requests/API/Interaction/StorePlayCountRequest.php b/app/Http/Requests/API/Interaction/IncreasePlayCountRequest.php similarity index 85% rename from app/Http/Requests/API/Interaction/StorePlayCountRequest.php rename to app/Http/Requests/API/Interaction/IncreasePlayCountRequest.php index 20e4cb51..5078c816 100644 --- a/app/Http/Requests/API/Interaction/StorePlayCountRequest.php +++ b/app/Http/Requests/API/Interaction/IncreasePlayCountRequest.php @@ -7,7 +7,7 @@ use App\Http\Requests\API\Request; /** * @property string $song The song's ID */ -class StorePlayCountRequest extends Request +class IncreasePlayCountRequest extends Request { /** @return array */ public function rules(): array diff --git a/app/Http/Requests/API/SongLikeRequest.php b/app/Http/Requests/API/ToggleLikeSongRequest.php similarity index 69% rename from app/Http/Requests/API/SongLikeRequest.php rename to app/Http/Requests/API/ToggleLikeSongRequest.php index 095235fb..a1a9705a 100644 --- a/app/Http/Requests/API/SongLikeRequest.php +++ b/app/Http/Requests/API/ToggleLikeSongRequest.php @@ -7,6 +7,6 @@ use App\Models\Song; /** * @property Song $song */ -class SongLikeRequest extends Request +class ToggleLikeSongRequest extends Request { } diff --git a/app/Listeners/UpdateLastfmNowPlaying.php b/app/Listeners/UpdateLastfmNowPlaying.php index 738029b3..e7ac057d 100644 --- a/app/Listeners/UpdateLastfmNowPlaying.php +++ b/app/Listeners/UpdateLastfmNowPlaying.php @@ -2,7 +2,7 @@ namespace App\Listeners; -use App\Events\SongStartedPlaying; +use App\Events\PlaybackStarted; use App\Services\LastfmService; use Illuminate\Contracts\Queue\ShouldQueue; @@ -12,7 +12,7 @@ class UpdateLastfmNowPlaying implements ShouldQueue { } - public function handle(SongStartedPlaying $event): void + public function handle(PlaybackStarted $event): void { if (!LastfmService::enabled() || !$event->user->lastfm_session_key || $event->song->artist->is_unknown) { return; diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 066b1663..f2d792ca 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -4,10 +4,10 @@ namespace App\Providers; use App\Events\LibraryChanged; use App\Events\MediaSyncCompleted; +use App\Events\PlaybackStarted; use App\Events\SongLikeToggled; use App\Events\SongsBatchLiked; use App\Events\SongsBatchUnliked; -use App\Events\SongStartedPlaying; use App\Listeners\ClearMediaCache; use App\Listeners\DeleteNonExistingRecordsPostSync; use App\Listeners\LoveMultipleTracksOnLastfm; @@ -35,7 +35,7 @@ class EventServiceProvider extends BaseServiceProvider UnloveMultipleTracksOnLastfm::class, ], - SongStartedPlaying::class => [ + PlaybackStarted::class => [ UpdateLastfmNowPlaying::class, ], diff --git a/routes/api.base.php b/routes/api.base.php index 121e5586..16476463 100644 --- a/routes/api.base.php +++ b/routes/api.base.php @@ -22,8 +22,8 @@ use App\Http\Controllers\API\FetchSongsForQueueController; use App\Http\Controllers\API\GenreController; use App\Http\Controllers\API\GenreSongController; use App\Http\Controllers\API\Interaction\BatchLikeController; -use App\Http\Controllers\API\Interaction\LikeController; -use App\Http\Controllers\API\Interaction\PlayCountController; +use App\Http\Controllers\API\Interaction\HandlePlaybackStartedController; +use App\Http\Controllers\API\Interaction\ToggleLikeSongController; use App\Http\Controllers\API\ObjectStorage\S3\SongController as S3SongController; use App\Http\Controllers\API\PlaylistController; use App\Http\Controllers\API\PlaylistFolderController; @@ -101,8 +101,8 @@ Route::prefix('api')->middleware('api')->group(static function (): void { Route::post('upload', UploadController::class); // Interaction routes - Route::post('interaction/play', [PlayCountController::class, 'store']); - Route::post('interaction/like', [LikeController::class, 'store']); + Route::post('interaction/play', HandlePlaybackStartedController::class); + Route::post('interaction/like', ToggleLikeSongController::class); Route::post('interaction/batch/like', [BatchLikeController::class, 'store']); Route::post('interaction/batch/unlike', [BatchLikeController::class, 'destroy']); diff --git a/tests/Feature/PlayCountTest.php b/tests/Feature/PlayCountTest.php index 58070081..b2c14076 100644 --- a/tests/Feature/PlayCountTest.php +++ b/tests/Feature/PlayCountTest.php @@ -2,7 +2,7 @@ namespace Tests\Feature; -use App\Events\SongStartedPlaying; +use App\Events\PlaybackStarted; use App\Models\Interaction; use App\Models\Song; use App\Models\User; @@ -12,7 +12,7 @@ class PlayCountTest extends TestCase { public function testStoreExistingEntry(): void { - Event::fake(SongStartedPlaying::class); + Event::fake(PlaybackStarted::class); /** @var Interaction $interaction */ $interaction = Interaction::factory()->create([ @@ -29,12 +29,12 @@ class PlayCountTest extends TestCase ]); self::assertSame(11, $interaction->refresh()->play_count); - Event::assertDispatched(SongStartedPlaying::class); + Event::assertDispatched(PlaybackStarted::class); } public function testStoreNewEntry(): void { - Event::fake(SongStartedPlaying::class); + Event::fake(PlaybackStarted::class); /** @var Song $song */ $song = Song::factory()->create(); @@ -58,6 +58,6 @@ class PlayCountTest extends TestCase ->first(); self::assertSame(1, $interaction->play_count); - Event::assertDispatched(SongStartedPlaying::class); + Event::assertDispatched(PlaybackStarted::class); } } diff --git a/tests/Unit/Listeners/UpdateLastfmNowPlayingTest.php b/tests/Unit/Listeners/UpdateLastfmNowPlayingTest.php index 8aeab19e..8288de52 100644 --- a/tests/Unit/Listeners/UpdateLastfmNowPlayingTest.php +++ b/tests/Unit/Listeners/UpdateLastfmNowPlayingTest.php @@ -2,7 +2,7 @@ namespace Tests\Unit\Listeners; -use App\Events\SongStartedPlaying; +use App\Events\PlaybackStarted; use App\Listeners\UpdateLastfmNowPlaying; use App\Models\Song; use App\Models\User; @@ -26,6 +26,6 @@ class UpdateLastfmNowPlayingTest extends TestCase ->with($song, $user) ->once(); - (new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user)); + (new UpdateLastfmNowPlaying($lastfm))->handle(new PlaybackStarted($song, $user)); } }