diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 45283d6..b0d1c1c 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -16,11 +16,55 @@ class AccountController extends Controller ->token($user->token) ->get('/accounts/' . $account_id); + if (session()->has('relationship')) + { + // The user is coming here after peforming an action on an account, + // in which case we don't need to re-query the relationship. + + $relationship = session('relationship'); + } + else + { + // If the relationship hasn't been returned from performing an action, + // we need to query for it. + + $relationships = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->get('/accounts/relationships', ['id' => $account_id]); + + $relationship = $relationships[0]; + } + $vars = [ 'account' => $account, - 'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1] + 'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1], + 'relationship' => $relationship ]; return view('account', $vars); } + + public function follow_account(Request $request, string $account_id) + { + $user = session('user'); + + $relationship = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/accounts/' . $account_id . '/follow'); + + return redirect()->route('account', ['account_id' => $account_id]) + ->with('relationship', $relationship); + } + + public function unfollow_account(Request $request, string $account_id) + { + $user = session('user'); + + $relationship = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/accounts/' . $account_id . '/unfollow'); + + return redirect()->route('account', ['account_id' => $account_id]) + ->with('relationship', $relationship); + } } diff --git a/resources/views/account.blade.php b/resources/views/account.blade.php index 7ff09c3..d538bf2 100644 --- a/resources/views/account.blade.php +++ b/resources/views/account.blade.php @@ -29,6 +29,17 @@ {!! $account['note'] !!} +
+ @if ($relationship['following']) + + Unfollow + + @else + + Follow + + @endif +
diff --git a/routes/web.php b/routes/web.php index 63136fa..ad798c7 100644 --- a/routes/web.php +++ b/routes/web.php @@ -63,6 +63,14 @@ Route::get('/account/{account_id}', 'AccountController@view_account') ->name('account') ->middleware('authorize'); +Route::get('/account/{account_id}/follow', 'AccountController@follow_account') + ->name('follow') + ->middleware('authorize'); + +Route::get('/account/{account_id}/unfollow', 'AccountController@unfollow_account') + ->name('unfollow') + ->middleware('authorize'); + Route::get('/login', 'LoginController@login') ->name('login');