planiverse

A minimalist, no-JS front-end for Mastodon.
git clone https://git.stjo.hn/planiverse
Log | Files | Refs | README | LICENSE

commit eb513faf9b3b7c2c9d3849cb38fb795358874173
parent d8f88af21bdc58dcc16e179ad4e7d2eac2afaed0
Author: St John Karp <stjohn@fuzzjunket.com>
Date:   Sun, 19 Aug 2018 09:24:28 -0700

Tidy up some of the status routing

Status routes were a bit of a mess and involved redirects,
which is only going to slow things down. I've amalgamated them
into one route and used query parameters to determine any actions
being performed.

Diffstat:
Mapp/Http/Controllers/StatusController.php | 56+++++++++++++++++++++-----------------------------------
Mpublic/css/styles.css | 2+-
Mresources/views/status.blade.php | 4++--
Mroutes/web.php | 6------
4 files changed, 24 insertions(+), 44 deletions(-)

diff --git a/app/Http/Controllers/StatusController.php b/app/Http/Controllers/StatusController.php @@ -9,19 +9,31 @@ use Illuminate\Http\Request; class StatusController extends Controller { - public function show_status(string $status_id) + public function show_status(Request $request, string $status_id) { - if (session()->has('return_status')) + // The user has a session and may be here to favourite/unfavourite + // a status. + if (session()->has('user')) { - // This route can be called as a redirect after favouriting, etc., - // in which case the status returned by the API will have been stored - // in the user's session. - $status = session('return_status'); - session()->forget('return_status'); + $user = session('user'); + if ($request->action === 'favourite') + { + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses/' . $status_id . '/favourite'); + } + elseif ($request->action === 'unfavourite') + { + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses/' . $status_id . '/unfavourite'); + } } - else + + // If the status hasn't been returned from performing an action on it, + // we need to query for it. + if (!isset($session)) { - // If the status isn't in the session, we need to query for it. $status = Mastodon::domain(env('MASTODON_DOMAIN')) ->get('/statuses/' . $status_id); } @@ -34,30 +46,4 @@ class StatusController extends Controller return view('show_status', $vars); } - - public function favourite_status(string $status_id) - { - $user = session('user'); - - $status = Mastodon::domain(env('MASTODON_DOMAIN')) - ->token($user->token) - ->post('/statuses/' . $status_id . '/favourite'); - - session(['return_status' => $status]); - - return redirect()->route('status', ['status_id' => $status_id]); - } - - public function unfavourite_status(string $status_id) - { - $user = session('user'); - - $status = Mastodon::domain(env('MASTODON_DOMAIN')) - ->token($user->token) - ->post('/statuses/' . $status_id . '/unfavourite'); - - session(['return_status' => $status]); - - return redirect()->route('status', ['status_id' => $status_id]); - } } diff --git a/public/css/styles.css b/public/css/styles.css @@ -50,7 +50,7 @@ div.status img.avatar { color: goldenrod; } -div.actions span { +div.actions > span { margin-right: 1em; } diff --git a/resources/views/status.blade.php b/resources/views/status.blade.php @@ -54,9 +54,9 @@ <!-- Favourite --> <span> @if ($status['favourited']) - <span class="favourited"><a href="/status/{{ $status['id'] }}/unfavourite">&#9733;</a></span> + <span class="favourited"><a href="/status/{{ $status['id'] }}?action=unfavourite">&#9733;</a></span> @else - <a href="/status/{{ $status['id'] }}/favourite">&#9734;</a> + <a href="/status/{{ $status['id'] }}?action=favourite">&#9734;</a> @endif {{ $status['favourites_count'] }} </span> diff --git a/routes/web.php b/routes/web.php @@ -35,12 +35,6 @@ Route::post('/timeline/home', 'TimelineController@post_status') Route::get('/status/{status_id}', 'StatusController@show_status') ->name('status'); -Route::get('/status/{status_id}/favourite', 'StatusController@favourite_status') - ->middleware('authorize'); - -Route::get('/status/{status_id}/unfavourite', 'StatusController@unfavourite_status') - ->middleware('authorize'); - Route::get('/login', 'LoginController@login') ->name('login');