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.
|
|
|
|
*
|
2017-04-29 02:38:17 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param Song $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
|
|
|
*/
|
2017-04-29 02:38:17 +00:00
|
|
|
public function store(Request $request, Song $song, $timestamp)
|
2016-05-30 05:50:59 +00:00
|
|
|
{
|
2018-08-19 21:17:05 +00:00
|
|
|
if ($song->artist->is_unknown) {
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$request->user()->connectedToLastfm()) {
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($this->lastfmService->scrobble(
|
|
|
|
$song->artist->name,
|
|
|
|
$song->title,
|
|
|
|
$timestamp,
|
|
|
|
$song->album->name === Album::UNKNOWN_NAME ? '' : $song->album->name,
|
|
|
|
$request->user()->lastfm_session_key
|
|
|
|
));
|
2016-05-30 05:50:59 +00:00
|
|
|
}
|
|
|
|
}
|