From 27f436d9efe6543406002cf83e378a5fd5ba12d4 Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 19 Sep 2016 17:33:52 +0200 Subject: [PATCH] added lastfm on profile --- app/Controllers/UserController.php | 29 ++++++++ app/User.php | 74 ++++++++++++++++++- config/config.example.ini | 7 ++ .../2016_09_19_114415_last_listened_music.php | 27 +++---- resources/assets/less/yuuno/master.less | 1 + resources/assets/less/yuuno/np.less | 19 +++++ resources/views/yuuno/user/profile.twig | 35 +++++++++ routes.php | 2 + 8 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 resources/assets/less/yuuno/np.less diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 637dd83..4146078 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -7,6 +7,7 @@ namespace Sakura\Controllers; use Phroute\Phroute\Exception\HttpMethodNotAllowedException; +use Phroute\Phroute\Exception\HttpRouteNotFoundException; use Sakura\Config; use Sakura\CurrentSession; use Sakura\DB; @@ -49,6 +50,34 @@ class UserController extends Controller return view('user/profile', compact('profile')); } + /** + * Last listened to. + * @param int $id + * @throws HttpRouteNotFoundException + * @return string + */ + public function nowPlaying($id) + { + $user = User::construct($id); + + if ($user->id === 0) { + throw new HttpRouteNotFoundException; + } + + $user->updateLastTrack(); + + $artist_url = 'http://last.fm/music/' . urlencode($user->musicArtist); + $track_url = $artist_url . '/_/' . urlencode($user->musicTrack); + + return $this->json([ + 'track' => $user->musicTrack, + 'track_url' => $track_url, + 'artist' => $user->musicArtist, + 'artist_url' => $artist_url, + 'listening' => $user->musicListening, + ]); + } + /** * Display the memberlist. * @param int $rank diff --git a/app/User.php b/app/User.php index a99c9de..c6064a0 100644 --- a/app/User.php +++ b/app/User.php @@ -7,6 +7,9 @@ namespace Sakura; use Carbon\Carbon; +use LastFmApi\Api\AuthApi; +use LastFmApi\Api\UserApi; +use LastFmApi\Exception\LastFmApiExeption; use Sakura\Perms; use Sakura\Perms\Site; use stdClass; @@ -210,6 +213,30 @@ class User */ private $design = ''; + /** + * Title of the track this user last listened to. + * @var string + */ + public $musicTrack; + + /** + * Artist of the track this user last listened to. + * @var string + */ + public $musicArtist; + + /** + * Last time this was updated. + * @var int + */ + public $musicCheck; + + /** + * Whether the user is actively listening. + * @var bool + */ + public $musicListening; + /** * The user's birthday. * @var string @@ -332,11 +359,15 @@ class User $this->osu = $userRow->user_osu; $this->lastfm = $userRow->user_lastfm; $this->design = $userRow->user_design; + $this->musicTrack = $userRow->user_music_track; + $this->musicArtist = $userRow->user_music_artist; + $this->musicListening = boolval($userRow->user_music_listening); + $this->musicCheck = intval($userRow->user_music_check); // Temporary backwards compatible IP storage system try { $this->registerIp = Net::ntop($userRow->register_ip); - } catch (Exception $e) { + } catch (NetAddressTypeException $e) { $this->registerIp = $userRow->register_ip; DB::table('users') @@ -348,7 +379,7 @@ class User try { $this->lastIp = Net::ntop($userRow->last_ip); - } catch (Exception $e) { + } catch (NetAddressTypeException $e) { $this->lastIp = $userRow->last_ip; DB::table('users') @@ -1091,4 +1122,43 @@ class User ->where('user_id', $this->id) ->max('ranks.rank_hierarchy'); } + + /** + * Update last listened data. + */ + public function updateLastTrack() + { + if (strlen($this->lastfm) < 1 + || $this->musicCheck + config('user.music_update') > time()) { + return; + } + + $lfm = new UserApi( + new AuthApi('setsession', ['apiKey' => config('lastfm.api_key')]) + ); + + try { + $last = $lfm->getRecentTracks(['user' => $this->lastfm, 'limit' => '1']); + } catch (LastFmApiExeption $e) { + return; + } + + if (count($last) < 1) { + return; + } + + $this->musicCheck = time(); + $this->musicListening = isset($last[0]['nowplaying']); + $this->musicTrack = $last[0]['name'] ?? null; + $this->musicArtist = $last[0]['artist']['name'] ?? null; + + DB::table('users') + ->where('user_id', $this->id) + ->update([ + 'user_music_check' => $this->musicCheck, + 'user_music_listening' => $this->musicListening, + 'user_music_track' => $this->musicTrack, + 'user_music_artist' => $this->musicArtist, + ]); + } } diff --git a/config/config.example.ini b/config/config.example.ini index 51138c7..f88a620 100644 --- a/config/config.example.ini +++ b/config/config.example.ini @@ -162,6 +162,9 @@ signature_max = 500 ; Max length of a userpage page_max = 65535 +; After how long last listened should be updated +music_update = 10 + ; Premium settings [premium] max_months_at_once = 24 @@ -245,3 +248,7 @@ sound_packs[default] = Default ; Default soundpack to use sound_pack = default + +; LastFM settings +[lastfm] +api_key = diff --git a/database/2016_09_19_114415_last_listened_music.php b/database/2016_09_19_114415_last_listened_music.php index 0f73f72..6102487 100644 --- a/database/2016_09_19_114415_last_listened_music.php +++ b/database/2016_09_19_114415_last_listened_music.php @@ -14,23 +14,17 @@ class LastListenedMusic extends Migration $schema = DB::getSchemaBuilder(); $schema->table('users', function (Blueprint $table) { - $table->text('user_last_track') + $table->text('user_music_track') ->nullable(); - $table->string('user_last_track_url', 255) - ->nullable() - ->default(null); - - $table->text('user_last_artist') + $table->text('user_music_artist') ->nullable(); - $table->string('user_last_artist_url', 255) - ->nullable() - ->default(null); + $table->integer('user_music_check') + ->default(0); - $table->string('user_last_cover', 255) - ->nullable() - ->default(null); + $table->boolean('user_music_listening') + ->default(false); }); } @@ -44,11 +38,10 @@ class LastListenedMusic extends Migration $schema->table('users', function (Blueprint $table) { $table->dropColumn([ - 'user_last_track', - 'user_last_track_url', - 'user_last_artist', - 'user_last_artist_url', - 'user_last_cover', + 'user_music_track', + 'user_music_artist', + 'user_music_check', + 'user_music_listening', ]); }); } diff --git a/resources/assets/less/yuuno/master.less b/resources/assets/less/yuuno/master.less index 3975df4..77779d0 100644 --- a/resources/assets/less/yuuno/master.less +++ b/resources/assets/less/yuuno/master.less @@ -7,6 +7,7 @@ @import "bbcode"; @import "alert"; @import "dropdown"; +@import "np"; /* * Animation Keyframes diff --git a/resources/assets/less/yuuno/np.less b/resources/assets/less/yuuno/np.less new file mode 100644 index 0000000..9a2aa08 --- /dev/null +++ b/resources/assets/less/yuuno/np.less @@ -0,0 +1,19 @@ +.np { + text-shadow: 0 0 5px #8364A1; + color: #614390; + text-align: center; + font-size: 1.2em; + line-height: 1.5em; + border-bottom: 1px solid #9475b2; + display: flex; + justify-content: space-between; + + a { + color: inherit; + text-decoration: none; + + &:hover { + text-decoration: underline; + } + } +} diff --git a/resources/views/yuuno/user/profile.twig b/resources/views/yuuno/user/profile.twig index 0183fe7..aaf28da 100644 --- a/resources/views/yuuno/user/profile.twig +++ b/resources/views/yuuno/user/profile.twig @@ -76,6 +76,28 @@ {% if not profileHidden %}