planiverse

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

commit c0e7838308764c74486c8c348cbfe7765e7c2a04
parent d6b3c8eae1576a4412ec3d360559b04ae987e448
Author: St John Karp <stjohn@fuzzjunket.com>
Date:   Sun,  7 Oct 2018 21:37:09 -0700

Implement follow/unfollow accounts

Implemented basic follow/unfollow functionality. Probably needs
some styling, definitely needs other controls.

Diffstat:
Mapp/Http/Controllers/AccountController.php | 46+++++++++++++++++++++++++++++++++++++++++++++-
Mresources/views/account.blade.php | 11+++++++++++
Mroutes/web.php | 8++++++++
3 files changed, 64 insertions(+), 1 deletion(-)

diff --git 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 @@ -29,6 +29,17 @@ </a> </span> {!! $account['note'] !!} + <div> + @if ($relationship['following']) + <a href="{{ route('unfollow', ['account_id' => $account['id']]) }}"> + Unfollow + </a> + @else + <a href="{{ route('follow', ['account_id' => $account['id']]) }}"> + Follow + </a> + @endif + </div> </div> </body> </html> diff --git 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');