Implement follow/unfollow accounts

Implemented basic follow/unfollow functionality. Probably needs
some styling, definitely needs other controls.
This commit is contained in:
St John Karp 2018-10-07 21:37:09 -07:00
parent d6b3c8eae1
commit c0e7838308
3 changed files with 64 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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>

View File

@ -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');