koel/app/Http/Controllers/API/ScrobbleController.php

42 lines
1.1 KiB
PHP
Raw Normal View History

2016-05-30 05:50:59 +00:00
<?php
namespace App\Http\Controllers\API;
2018-08-19 21:17:05 +00:00
use App\Models\Album;
2016-05-30 05:50:59 +00:00
use App\Models\Song;
2018-08-19 21:17:05 +00:00
use App\Services\LastfmService;
2017-06-04 01:30:45 +00:00
use Illuminate\Http\JsonResponse;
2017-05-01 17:49:44 +00:00
use Illuminate\Http\Request;
2016-05-30 05:50:59 +00:00
class ScrobbleController extends Controller
{
2018-08-19 21:17:05 +00:00
private $lastfmService;
public function __construct(LastfmService $lastfmService)
{
$this->lastfmService = $lastfmService;
}
2016-05-30 05:50:59 +00:00
/**
* Create a Last.fm scrobble entry for a song.
*
* @param string $timestamp The UNIX timestamp when the song started playing.
2016-05-30 05:50:59 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2016-05-30 05:50:59 +00:00
*/
2018-08-24 15:27:19 +00:00
public function store(Request $request, Song $song, string $timestamp)
2016-05-30 05:50:59 +00:00
{
2018-08-24 15:27:19 +00:00
if (!$song->artist->is_unknown && $request->user()->connectedToLastfm()) {
$this->lastfmService->scrobble(
$song->artist->name,
$song->title,
(int) $timestamp,
$song->album->name === Album::UNKNOWN_NAME ? '' : $song->album->name,
$request->user()->lastfm_session_key
);
2018-08-19 21:17:05 +00:00
}
2018-08-24 15:27:19 +00:00
return response()->json();
2016-05-30 05:50:59 +00:00
}
}