🏡 index : ~doyle/zpan.git

author Jordan Doyle <jordan@doyle.wf> 2016-06-22 18:09:56.0 +01:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-06-22 18:09:56.0 +01:00:00
commit
378a23cc1f652b37b5fe4674a8026865b9d9f866 [patch]
tree
7ab95309d4bbc8909d53d6c4ae1a4deb09494728
parent
f7e37f9df6ae425191c199d00309cbdaae859a4a
download
378a23cc1f652b37b5fe4674a8026865b9d9f866.tar.gz

Fix 'is' middleware



Diff

 storage/stats.json                       |  1 +
 app/Http/routes.php                      |  1 +
 app/Http/Controllers/StatsController.php | 34 ++++++++++++++++++++++++++--------
 app/Http/Middleware/IsGroup.php          |  6 +++---
 4 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/storage/stats.json b/storage/stats.json
new file mode 100644
index 0000000..f001048 100644
--- /dev/null
+++ a/storage/stats.json
@@ -1,0 +1,1 @@
{"dj":"Unset","listeners":2,"artist":"Sigma & Rita Ora","song":"Coming Home"}
diff --git a/app/Http/routes.php b/app/Http/routes.php
index 989397b..d81176d 100644
--- a/app/Http/routes.php
+++ a/app/Http/routes.php
@@ -119,5 +119,6 @@
    Route::get('timetable', ['as' => 'timetable', 'uses' => 'DJ\TimetableController@getJSONTimetable']);
    Route::get('event/all', ['as' => 'events', 'uses' => 'Event\TimetableController@getJSONTimetable']);
    Route::get('event/current', ['as' => 'event.current', 'uses' => 'Event\TimetableController@getCurrentEvent']);
    Route::get('stats', ['as' => 'stats', 'uses' => 'StatsController@s']);
    Route::post('request', ['as' => 'request', 'uses' => 'DJ\RequestController@request']);
});
diff --git a/app/Http/Controllers/StatsController.php b/app/Http/Controllers/StatsController.php
index ed196a0..9592503 100644
--- a/app/Http/Controllers/StatsController.php
+++ a/app/Http/Controllers/StatsController.php
@@ -1,10 +1,13 @@
<?php
namespace App\Http\Controllers;

use App\Http\Requests;
use App\Models\ConnectionInfo;
use App\Models\Timetable;
use Carbon\Carbon;
use LastFmApi\Api\AuthApi;
use LastFmApi\Api\TrackApi;
use stdClass;

class StatsController extends Controller
{
@@ -29,17 +32,28 @@
            if (empty($stats->songtitle)) {
                $ret = ['status' => false];
            } else {
                $old = json_decode(file_get_contents(storage_path('stats.json')));

                $ret['dj'] = empty($json->dj) ? $old->dj : $stats->dj;
                $ret['listeners'] = $stats->listeners;

                if (count(explode(' - ', $json->songtitle, 2)) == 2) {
                    list($artist, $song) = explode(' - ', $json->songtitle, 2);
                $old = file_exists(storage_path('stats.json')) ?
                    json_decode(file_get_contents(storage_path('stats.json'))) : false;

                $ret['dj'] = empty($stats->dj) ? ($old ? $old->dj : 'Unset') : $stats->dj;
                $ret['listeners'] = $stats->currentlisteners;

                if (!$ret['dj']) {
                    $timetable = Timetable::where('week', Carbon::now()->weekOfYear)
                        ->where('year', Carbon::now()->year)
                        ->where('day', Carbon::now()->format('N') - 1)
                        ->where('hour', Carbon::now()->hour)
                        ->first();

                    $ret['dj'] = $timetable ? $timetable->user->getDisplayName()->toHtml() : 'Offline';
                }

                if (count(explode(' - ', $stats->songtitle, 2)) == 2) {
                    list($artist, $song) = explode(' - ', $stats->songtitle, 2);
                    $ret['artist'] = $artist;
                    $ret['song'] = $song;

                    if ($stats->songtitle !== "{$old->artist} - {$old->song}") {
                    /*if ($stats->songtitle !== "{$old->artist} - {$old->song}") {
                        $lastfm = new AuthApi('getsession', [
                            'apiKey' => env('LASTFM_API_KEY'),
                            'secret' => env('LASTFM_SECRET'),
@@ -47,9 +61,9 @@
                        ]);
                        $trackApi = new TrackApi($lastfm);
                        $trackApi->scrobble(['artist' => $artist, 'track' => $song, 'timestamp' => 400]);
                    }
                    }*/
                } else {
                    $ret['song'] = $json->songtitle;
                    $ret['song'] = $stats->songtitle;
                }

                file_put_contents(storage_path('stats.json'), json_encode($ret));
diff --git a/app/Http/Middleware/IsGroup.php b/app/Http/Middleware/IsGroup.php
index 0892888..8b6457b 100644
--- a/app/Http/Middleware/IsGroup.php
+++ a/app/Http/Middleware/IsGroup.php
@@ -13,10 +13,10 @@
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param  string $group
     * @param  array $group
     * @return mixed
     */
    public function handle($request, Closure $next, $group)
    public function handle($request, Closure $next, ...$group)
    {
        if ($group === 'admin') {
            if (!Auth::user()->isAdmin()) {
@@ -42,7 +42,7 @@

                return redirect()->route('dashboard::home');
            }
        } elseif (!Auth::user()->is(...explode(',', $group))) {
        } elseif (!Auth::user()->is(...$group)) {
            Session::flash('msg', [
                'type' => 'danger',
                'msg' => sprintf('%s %s',