planiverse

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

AccountController.php (3706B)


      1 <?php
      2 
      3 namespace App\Http\Controllers;
      4 
      5 use App\Http\Controllers\Controller;
      6 use Mastodon;
      7 use Illuminate\Http\Request;
      8 
      9 use App\Helpers\Links;
     10 use App\Helpers\PaginationParameters;
     11 
     12 /**
     13  * Controller for Account functions.
     14  */
     15 class AccountController extends Controller
     16 {
     17     /**
     18      * Fetch an Account from the Mastodon API and present it to the user.
     19      *
     20      * The returned view will show the details of the Account and the actions
     21      * the user can perform (follow, mute, etc.)
     22      *
     23      * @param string $account_id The ID of the Account to query.
     24      *
     25      * @return Illuminate\View\View The Account details page.
     26      */
     27     public function view_account(string $account_id)
     28     {
     29         $user = session('user');
     30 
     31         # Get the Account from the API.
     32         $account = Mastodon::domain(env('MASTODON_DOMAIN'))
     33             ->token($user->token)
     34             ->get('/accounts/' . $account_id);
     35 
     36         if (session()->has('relationship'))
     37         {
     38             # The user is coming here after peforming an action on an account,
     39             # in which case we don't need to re-query the relationship.
     40 
     41             $relationship = session('relationship');
     42         }
     43         else
     44         {
     45             # If the relationship hasn't been returned from performing an action,
     46             # we need to query for it.
     47 
     48             $relationships = Mastodon::domain(env('MASTODON_DOMAIN'))
     49                 ->token($user->token)
     50                 ->get('/accounts/relationships', ['id' => $account_id]);
     51 
     52             $relationship = $relationships[0];
     53         }
     54 
     55         # Get the Account's Statuses from the API.
     56         $statuses = Mastodon::domain(env('MASTODON_DOMAIN'))
     57             ->token($user->token)
     58             ->get('/accounts/' . $account_id . '/statuses');
     59 
     60         $vars = [
     61             'account' => $account,
     62             'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1],
     63             'relationship' => $relationship,
     64             'statuses' => $statuses,
     65             'links' => new Links(
     66                 Mastodon::getResponse()->getHeader('link'),
     67                 'public'
     68                 )
     69         ];
     70 
     71         return view('account', $vars);
     72     }
     73 
     74     /**
     75      * Follow an Account and redirect to the "account" view.
     76      *
     77      * @param string $account_id The ID of the Account to query.
     78      *
     79      * @return Illuminate\Routing\Redirector Redirect to the account details page.
     80      */
     81     public function follow_account(string $account_id)
     82     {
     83         $user = session('user');
     84 
     85         # Query the API to follow the Account.
     86         $relationship = Mastodon::domain(env('MASTODON_DOMAIN'))
     87             ->token($user->token)
     88             ->post('/accounts/' . $account_id . '/follow');
     89 
     90         # Redirect with the resulting relationship as a temporary session variable.
     91         return redirect()->route('account', ['account_id' => $account_id])
     92             ->with('relationship', $relationship);
     93     }
     94 
     95     /**
     96      * Unfollow an Account and redirect to the "account" view.
     97      *
     98      * @param string $account_id The ID of the Account to query.
     99      *
    100      * @return Illuminate\Routing\Redirector Redirect to the account details page.
    101      */
    102     public function unfollow_account(Request $request, string $account_id)
    103     {
    104         $user = session('user');
    105 
    106         # Unfollow via the API.
    107         $relationship = Mastodon::domain(env('MASTODON_DOMAIN'))
    108             ->token($user->token)
    109             ->post('/accounts/' . $account_id . '/unfollow');
    110 
    111         # Redirect with the resulting relationship as a temporary session variable.
    112         return redirect()->route('account', ['account_id' => $account_id])
    113             ->with('relationship', $relationship);
    114     }
    115 }