Skip to content

Commit

Permalink
feat(lastfm): batch like/unlike are now asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Jun 4, 2021
1 parent 6c24b52 commit ef1add3
Show file tree
Hide file tree
Showing 28 changed files with 355 additions and 356 deletions.
21 changes: 21 additions & 0 deletions app/Events/SongsBatchLiked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

class SongsBatchLiked extends Event
{
use SerializesModels;

public $songs;
public $user;

public function __construct(Collection $songs, User $user)
{
$this->songs = $songs;
$this->user = $user;
}
}
21 changes: 21 additions & 0 deletions app/Events/SongsBatchUnliked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

class SongsBatchUnliked
{
use SerializesModels;

public $songs;
public $user;

public function __construct(Collection $songs, User $user)
{
$this->songs = $songs;
$this->user = $user;
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/ScrobbleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(LastfmService $lastfmService, Authenticatable $curre
public function store(ScrobbleStoreRequest $request, Song $song)
{
if (!$song->artist->is_unknown && $this->currentUser->connectedToLastfm()) {
ScrobbleJob::dispatch($this->currentUser, $song, (int) $request->timestamp);
ScrobbleJob::dispatch($this->currentUser, $song, $request->timestamp);
}

return response()->json(null, Response::HTTP_NO_CONTENT);
Expand Down
20 changes: 0 additions & 20 deletions app/Jobs/Job.php

This file was deleted.

39 changes: 0 additions & 39 deletions app/Jobs/LoveTrackOnLastfmJob.php

This file was deleted.

41 changes: 0 additions & 41 deletions app/Jobs/UpdateLastfmNowPlayingJob.php

This file was deleted.

Empty file removed app/Listeners/.gitkeep
Empty file.
29 changes: 29 additions & 0 deletions app/Listeners/LoveMultipleTracksOnLastfm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Listeners;

use App\Events\SongsBatchLiked;
use App\Models\Song;
use App\Services\LastfmService;
use App\Values\LastfmLoveTrackParameters;
use Illuminate\Contracts\Queue\ShouldQueue;

class LoveMultipleTracksOnLastfm implements ShouldQueue
{
private $lastfm;

public function __construct(LastfmService $lastfm)
{
$this->lastfm = $lastfm;
}

public function handle(SongsBatchLiked $event): void
{
$this->lastfm->batchToggleLoveTracks(
$event->songs->map(static function (Song $song): LastfmLoveTrackParameters {
return LastfmLoveTrackParameters::make($song->title, $song->artist->name);
}),
$event->user
);
}
}
14 changes: 11 additions & 3 deletions app/Listeners/LoveTrackOnLastfm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace App\Listeners;

use App\Events\SongLikeToggled;
use App\Jobs\LoveTrackOnLastfmJob;
use App\Services\LastfmService;
use App\Values\LastfmLoveTrackParameters;
use Illuminate\Contracts\Queue\ShouldQueue;

class LoveTrackOnLastfm
class LoveTrackOnLastfm implements ShouldQueue
{
private $lastfm;

Expand All @@ -25,6 +26,13 @@ public function handle(SongLikeToggled $event): void
return;
}

LoveTrackOnLastfmJob::dispatch($event->user, $event->interaction);
$this->lastfm->toggleLoveTrack(
LastfmLoveTrackParameters::make(
$event->interaction->song->title,
$event->interaction->song->artist->name,
),
$event->user->lastfm_session_key,
$event->interaction->liked
);
}
}
22 changes: 22 additions & 0 deletions app/Listeners/UnloveMultipleTracksOnLastfm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Listeners;

use App\Events\SongsBatchUnliked;
use App\Services\LastfmService;
use Illuminate\Contracts\Queue\ShouldQueue;

class UnloveMultipleTracksOnLastfm implements ShouldQueue
{
private $lastfm;

public function __construct(LastfmService $lastfm)
{
$this->lastfm = $lastfm;
}

public function handle(SongsBatchUnliked $event): void
{
$this->lastfm->batchToggleLoveTracks($event->songs, $event->user, false);
}
}
12 changes: 9 additions & 3 deletions app/Listeners/UpdateLastfmNowPlaying.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace App\Listeners;

use App\Events\SongStartedPlaying;
use App\Jobs\UpdateLastfmNowPlayingJob;
use App\Services\LastfmService;
use Illuminate\Contracts\Queue\ShouldQueue;

class UpdateLastfmNowPlaying
class UpdateLastfmNowPlaying implements ShouldQueue
{
private $lastfm;

Expand All @@ -21,6 +21,12 @@ public function handle(SongStartedPlaying $event): void
return;
}

UpdateLastfmNowPlayingJob::dispatch($event->user, $event->song);
$this->lastfm->updateNowPlaying(
$event->song->artist->name,
$event->song->title,
$event->song->album->is_unknown ? '' : $event->song->album->name,
$event->song->length,
$event->user->lastfm_session_key
);
}
}
3 changes: 2 additions & 1 deletion app/Models/Interaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
* @property User $user
* @property int $id
*
* @method self firstOrCreate(array $where, array $params = [])
* @method static self firstOrCreate(array $where, array $params = [])
* @method static self find(int $id)
* @method static Builder whereSongIdAndUserId(string $songId, string $userId)
* @method static Builder whereIn(...$params)
*/
class Interaction extends Model
{
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Song.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @method static self first()
* @method static EloquentCollection orderBy(...$args)
* @method static int count()
* @method static self|null find($id)
* @method static self|Collection|null find($id)
* @method static Builder take(int $count)
*/
class Song extends Model
Expand Down
12 changes: 12 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
use App\Events\LibraryChanged;
use App\Events\MediaCacheObsolete;
use App\Events\SongLikeToggled;
use App\Events\SongsBatchLiked;
use App\Events\SongsBatchUnliked;
use App\Events\SongStartedPlaying;
use App\Listeners\ClearMediaCache;
use App\Listeners\DownloadAlbumCover;
use App\Listeners\DownloadArtistImage;
use App\Listeners\LoveMultipleTracksOnLastfm;
use App\Listeners\LoveTrackOnLastfm;
use App\Listeners\TidyLibrary;
use App\Listeners\UnloveMultipleTracksOnLastfm;
use App\Listeners\UpdateLastfmNowPlaying;
use App\Models\Album;
use App\Models\Song;
Expand All @@ -32,6 +36,14 @@ class EventServiceProvider extends ServiceProvider
LoveTrackOnLastfm::class,
],

SongsBatchLiked::class => [
LoveMultipleTracksOnLastfm::class,
],

SongsBatchUnliked::class => [
UnloveMultipleTracksOnLastfm::class,
],

SongStartedPlaying::class => [
UpdateLastfmNowPlaying::class,
],
Expand Down
Loading

0 comments on commit ef1add3

Please sign in to comment.