Skip to content

Commit ed1bce9

Browse files
committed
feat: read what members are listening to from Last.fm
1 parent 74f91e2 commit ed1bce9

28 files changed

Lines changed: 794 additions & 5 deletions

‎.env.example‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ GITHUB_REDIRECT_URI="${APP_URL}/auth/github/callback"
8989
GITHUB_APP_ID=
9090
GITHUB_APP_PRIVATE_KEY=
9191

92+
LASTFM_API_KEY=
93+
9294
# New GitHub accounts must be at least this many days old, and have a verified primary email,
9395
# before they can sign up. Both apply to the first sign-in only.
9496
SIGNUP_MIN_ACCOUNT_AGE_DAYS=7

‎app/Actions/CreateExport.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public function handle(User $user): array
104104
'live_platform' => $user->live_platform?->value,
105105
'live_url' => $user->live_url,
106106
'live_ends_at' => $user->live_ends_at?->toIso8601String(),
107+
'lastfm_username' => $user->lastfm_username,
107108
'last_seen_at' => $user->last_seen_at?->toDateString(),
108109
'commit_rules_accepted_at' => $user->commit_rules_accepted_at?->toIso8601String(),
109110
'emails_unsubscribed_at' => $user->emails_unsubscribed_at?->toIso8601String(),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions;
6+
7+
use App\Models\User;
8+
9+
final readonly class DeleteUserNowPlaying
10+
{
11+
public function handle(User $user): void
12+
{
13+
$user->update(['lastfm_username' => null]);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions;
6+
7+
use App\Models\User;
8+
9+
final readonly class UpdateUserNowPlaying
10+
{
11+
public function handle(User $user, string $lastFmUsername): void
12+
{
13+
$user->update(['lastfm_username' => $lastFmUsername]);
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Contracts\Services;
6+
7+
use App\Dto\Track;
8+
use Illuminate\Http\Client\HttpClientException;
9+
10+
interface LastFmProvider
11+
{
12+
/**
13+
* @throws HttpClientException
14+
*/
15+
public function exists(string $username): bool;
16+
17+
/**
18+
* @throws HttpClientException
19+
*/
20+
public function nowPlaying(string $username): ?Track;
21+
}

‎app/Dto/Track.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Dto;
6+
7+
use Illuminate\Contracts\Support\Arrayable;
8+
9+
/**
10+
* @implements Arrayable<string, string|null>
11+
*/
12+
final readonly class Track implements Arrayable
13+
{
14+
public function __construct(
15+
public string $artist,
16+
public string $title,
17+
public ?string $album,
18+
public ?string $url,
19+
) {
20+
//
21+
}
22+
23+
/**
24+
* @return array{artist: string, title: string, album: string|null, url: string|null}
25+
*/
26+
public function toArray(): array
27+
{
28+
return [
29+
'artist' => $this->artist,
30+
'title' => $this->title,
31+
'album' => $this->album,
32+
'url' => $this->url,
33+
];
34+
}
35+
}

‎app/Enums/Feature.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ enum Feature: string
1111
{
1212
case Translations = 'translations';
1313
case OrganizationCommits = 'organization_commits';
14+
case NowPlaying = 'now_playing';
1415

1516
/**
1617
* Features are on until switched off, except one still being built, which stays off until switched on.
1718
*/
1819
public function isEnabledByDefault(): bool
1920
{
20-
return $this !== self::OrganizationCommits;
21+
return ! in_array($this, [self::OrganizationCommits, self::NowPlaying], true);
2122
}
2223
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use App\Actions\DeleteUserNowPlaying;
8+
use App\Actions\UpdateUserNowPlaying;
9+
use App\Http\Requests\UpdateUserNowPlayingRequest;
10+
use App\Models\User;
11+
use App\Queries\NowPlayingQuery;
12+
use Illuminate\Container\Attributes\CurrentUser;
13+
use Illuminate\Http\JsonResponse;
14+
use Illuminate\Http\RedirectResponse;
15+
16+
final readonly class UserNowPlayingController
17+
{
18+
public function show(#[CurrentUser] ?User $currentUser, User $user, NowPlayingQuery $nowPlaying): JsonResponse
19+
{
20+
return response()->json([
21+
'track' => $currentUser?->blockerIdWith($user) === null ? $nowPlaying->get($user) : null,
22+
])->header('Cache-Control', 'no-store, private');
23+
}
24+
25+
public function update(UpdateUserNowPlayingRequest $request, #[CurrentUser] User $user, UpdateUserNowPlaying $action): RedirectResponse
26+
{
27+
$action->handle($user, $request->lastFmUsername());
28+
29+
return back();
30+
}
31+
32+
public function destroy(#[CurrentUser] User $user, DeleteUserNowPlaying $action): RedirectResponse
33+
{
34+
$action->handle($user);
35+
36+
return back();
37+
}
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Requests;
6+
7+
use App\Rules\LastFmUsername;
8+
use Illuminate\Contracts\Validation\ValidationRule;
9+
use Illuminate\Foundation\Http\FormRequest;
10+
11+
final class UpdateUserNowPlayingRequest extends FormRequest
12+
{
13+
public function authorize(): bool
14+
{
15+
return true;
16+
}
17+
18+
/**
19+
* @return array<string, list<string|ValidationRule>>
20+
*/
21+
public function rules(): array
22+
{
23+
return [
24+
'lastfm_username' => ['bail', 'required', 'string', new LastFmUsername()],
25+
];
26+
}
27+
28+
public function lastFmUsername(): string
29+
{
30+
return $this->string('lastfm_username')->value();
31+
}
32+
}

‎app/Models/User.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @property-read LivePlatform|null $live_platform
5858
* @property-read string|null $live_url
5959
* @property-read CarbonInterface|null $live_ends_at
60+
* @property-read string|null $lastfm_username
6061
* @property-read Log $log
6162
* @property-read CarbonInterface|null $onboarded_at
6263
* @property-read CarbonInterface|null $github_follows_imported_at
@@ -115,6 +116,7 @@
115116
'live_platform',
116117
'live_url',
117118
'live_ends_at',
119+
'lastfm_username',
118120
'last_seen_at',
119121
'banned_at',
120122
'commit_rules_accepted_at',
@@ -164,6 +166,7 @@ final class User extends Authenticatable
164166
'live_platform' => null,
165167
'live_url' => null,
166168
'live_ends_at' => null,
169+
'lastfm_username' => null,
167170
'last_seen_at' => null,
168171
'banned_at' => null,
169172
'commit_rules_accepted_at' => null,
@@ -212,6 +215,7 @@ public function casts(): array
212215
'live_platform' => LivePlatform::class,
213216
'live_url' => 'string',
214217
'live_ends_at' => 'datetime',
218+
'lastfm_username' => 'string',
215219
'last_seen_at' => 'date',
216220
'banned_at' => 'datetime',
217221
'commit_rules_accepted_at' => 'datetime',

0 commit comments

Comments
 (0)