diff --git a/app/Http/Controllers/StatusController.php b/app/Http/Controllers/StatusController.php index cadf627..b983d52 100644 --- a/app/Http/Controllers/StatusController.php +++ b/app/Http/Controllers/StatusController.php @@ -4,48 +4,24 @@ namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Mastodon; -use GuzzleHttp\Psr7; use Illuminate\Http\Request; class StatusController extends Controller { public function show_status(Request $request, string $status_id) { - // The user has a session and may be here to favourite/unfavourite - // a status. - if (session()->has('user') && $request->has('action')) + if (session()->has('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'); - } - elseif ($request->action === 'reblog') - { - $status = Mastodon::domain(env('MASTODON_DOMAIN')) - ->token($user->token) - ->post('/statuses/' . $status_id . '/reblog'); - } - elseif ($request->action === 'unreblog') - { - $status = Mastodon::domain(env('MASTODON_DOMAIN')) - ->token($user->token) - ->post('/statuses/' . $status_id . '/unreblog'); - } + // The user is coming here after peforming an action on a status, + // in which case we don't need to re-query it. + + $status = session('status'); } - - // If the status hasn't been returned from performing an action on it, - // we need to query for it. - if (!isset($session)) + else { + // If the status hasn't been returned from performing an action, + // we need to query for it. + $status = Mastodon::domain(env('MASTODON_DOMAIN')) ->get('/statuses/' . $status_id); } @@ -59,6 +35,92 @@ class StatusController extends Controller return view('show_status', $vars); } + public function reblog_status(Request $request, string $status_id) + { + $user = session('user'); + + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses/' . $status_id . '/reblog'); + + return redirect()->route('status', ['status_id' => $status_id]) + ->with('status', $status); + } + + public function unreblog_status(Request $request, string $status_id) + { + $user = session('user'); + + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses/' . $status_id . '/unreblog'); + + return redirect()->route('status', ['status_id' => $status_id]) + ->with('status', $status); + } + + public function favourite_status(Request $request, string $status_id) + { + $user = session('user'); + + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses/' . $status_id . '/favourite'); + + return redirect()->route('status', ['status_id' => $status_id]) + ->with('status', $status); + } + + public function unfavourite_status(Request $request, string $status_id) + { + $user = session('user'); + + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses/' . $status_id . '/unfavourite'); + + return redirect()->route('status', ['status_id' => $status_id]) + ->with('status', $status); + } + + public function post_status(Request $request) + { + $user = session('user'); + + # Verify we have an actual status to post. + if (!$request->has('status')) + { + abort(400); + } + + $params = [ + 'status' => $request->status + ]; + + $inputs = [ + 'in_reply_to_id', + 'media_ids', + 'sensitive', + 'spoiler_text', + 'visibility', + 'language' + ]; + + foreach ($inputs as $input) + { + if ($request->has($input)) + { + $params[$input] = $request->input($input); + } + } + + $new_status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses', $params); + + return redirect()->route('home'); + } + public function context(Request $request, string $status_id) { $status = Mastodon::domain(env('MASTODON_DOMAIN')) diff --git a/app/Http/Controllers/TimelineController.php b/app/Http/Controllers/TimelineController.php index 961f10b..011c94f 100644 --- a/app/Http/Controllers/TimelineController.php +++ b/app/Http/Controllers/TimelineController.php @@ -44,42 +44,4 @@ class TimelineController extends Controller return view('home_timeline', $vars); } - - public function post_status(Request $request) - { - $user = session('user'); - - # Verify we have an actual status to post. - if (!$request->has('status')) - { - abort(400); - } - - $params = [ - 'status' => $request->status - ]; - - $inputs = [ - 'in_reply_to_id', - 'media_ids', - 'sensitive', - 'spoiler_text', - 'visibility', - 'language' - ]; - - foreach ($inputs as $input) - { - if ($request->has($input)) - { - $params[$input] = $request->input($input); - } - } - - $new_status = Mastodon::domain(env('MASTODON_DOMAIN')) - ->token($user->token) - ->post('/statuses', $params); - - return redirect()->route('home'); - } } diff --git a/resources/views/show_status.blade.php b/resources/views/show_status.blade.php index cd0e824..b5f2268 100644 --- a/resources/views/show_status.blade.php +++ b/resources/views/show_status.blade.php @@ -19,7 +19,7 @@ @endcomponent @if ($logged_in) -
+ @if ($status['reblogged']) - + @else - + @endif {{ $status['reblogs_count'] }} @@ -46,10 +46,10 @@ @if ($status['favourited']) - + @else - + @endif {{ $status['favourites_count'] }} diff --git a/routes/web.php b/routes/web.php index c5164fd..995e529 100644 --- a/routes/web.php +++ b/routes/web.php @@ -12,36 +12,48 @@ */ Route::get('/', function() { - if (!session()->has('user')) + if (!session()->has('user')) { return redirect()->route('public'); } - else - { - return redirect()->route('home'); - } + else + { + return redirect()->route('home'); + } }); Route::get('/timeline/public', 'TimelineController@public_timeline') - ->name('public'); + ->name('public'); Route::get('/timeline/home', 'TimelineController@home_timeline') - ->name('home') - ->middleware('authorize'); - -Route::post('/timeline/home', 'TimelineController@post_status') - ->middleware('authorize'); + ->name('home') + ->middleware('authorize'); Route::get('/status/{status_id}', 'StatusController@show_status') - ->name('status'); + ->name('status'); + +Route::get('/status/{status_id}/reblog', 'StatusController@reblog_status') + ->middleware('authorize'); + +Route::get('/status/{status_id}/unreblog', 'StatusController@unreblog_status') + ->middleware('authorize'); + +Route::get('/status/{status_id}/favourite', 'StatusController@favourite_status') + ->middleware('authorize'); + +Route::get('/status/{status_id}/unfavourite', 'StatusController@unfavourite_status') + ->middleware('authorize'); Route::get('/status/{status_id}/thread', 'StatusController@context'); +Route::post('/status', 'StatusController@post_status') + ->middleware('authorize'); + Route::get('/notifications', 'NotificationsController@get_notifications') - ->name('notifications') - ->middleware('authorize'); + ->name('notifications') + ->middleware('authorize'); Route::get('/login', 'LoginController@login') - ->name('login'); + ->name('login'); Route::get('/callback', 'LoginController@callback');