planiverse

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

StatusController.php (8263B)


      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 /**
     10  * Controller for managing Statuses.
     11  */
     12 class StatusController extends Controller
     13 {
     14     /**
     15      * Show a Status.
     16      *
     17      * Conditionally shows a reply form if the user is logged in.
     18      *
     19      * @param string $status_id The ID of the Status to display.
     20      *
     21      * @return Illuminate\View\View The Status view page.
     22      */
     23     public function show_status(string $status_id)
     24     {
     25         if (session()->has('status'))
     26         {
     27             # The user is coming here after peforming an action on a status,
     28             # in which case we don't need to re-query it.
     29 
     30             $status = session('status');
     31         }
     32         else
     33         {
     34             # If the status hasn't been returned from performing an action,
     35             # we need to query for it.
     36 
     37             if (session()->has('user'))
     38             {
     39                 # If the user is logged in, send the token to ensure they
     40                 # can see private and direct statuses. Otherwise the API
     41                 # returns a 404.
     42 
     43                 $status = Mastodon::domain(env('MASTODON_DOMAIN'))
     44                     ->token(session('user')->token)
     45                     ->get('/statuses/' . $status_id);
     46             }
     47             else
     48             {
     49                 $status = Mastodon::domain(env('MASTODON_DOMAIN'))
     50                     ->get('/statuses/' . $status_id);
     51             }
     52         }
     53 
     54         # Compile a list of accounts to include in the reply.
     55         $reply_mentions = [];
     56         if (session()->has('user'))
     57         {
     58             # Include all mentions, excluding the current user.
     59             foreach ($status['mentions'] as $mention)
     60             {
     61                 if ($mention['acct'] !== session('user')->user['acct'])
     62                 {
     63                     array_push($reply_mentions, '@' . $mention['acct']);
     64                 }
     65 	    }
     66         }
     67 
     68         $vars = [
     69             'status' => $status,
     70             'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1],
     71             'logged_in' => session()->has('user'),
     72             'reply_mentions' => implode(' ', $reply_mentions)
     73         ];
     74 
     75         return view('show_status', $vars);
     76     }
     77 
     78     /**
     79      * Reblog a Status.
     80      *
     81      * @param string $status_id The ID of the Status to reblog.
     82      *
     83      * @return Illuminate\Routing\Redirector Redirect to the Status view page.
     84      */
     85     public function reblog_status(string $status_id)
     86     {
     87         $user = session('user');
     88 
     89         # Reblog request to the API.
     90         $status = Mastodon::domain(env('MASTODON_DOMAIN'))
     91             ->token($user->token)
     92             ->post('/statuses/' . $status_id . '/reblog');
     93 
     94         return redirect()->route('status', ['status_id' => $status_id])
     95             ->with('status', $status);
     96     }
     97 
     98     /**
     99      * Unreblog a Status.
    100      *
    101      * @param string $status_id The ID of the Status to unreblog.
    102      *
    103      * @return Illuminate\Routing\Redirector Redirect to the Status view page.
    104      */
    105     public function unreblog_status(string $status_id)
    106     {
    107         $user = session('user');
    108 
    109         # Unreblog request to the API.
    110         $status = Mastodon::domain(env('MASTODON_DOMAIN'))
    111             ->token($user->token)
    112             ->post('/statuses/' . $status_id . '/unreblog');
    113 
    114         return redirect()->route('status', ['status_id' => $status_id])
    115             ->with('status', $status);
    116     }
    117 
    118     /**
    119      * Favourite a Status.
    120      *
    121      * @param string $status_id The ID of the Status to favourite.
    122      *
    123      * @return Illuminate\Routing\Redirector Redirect to the Status view page.
    124      */
    125     public function favourite_status(string $status_id)
    126     {
    127         $user = session('user');
    128 
    129         # Favourite the Status.
    130         $status = Mastodon::domain(env('MASTODON_DOMAIN'))
    131             ->token($user->token)
    132             ->post('/statuses/' . $status_id . '/favourite');
    133 
    134         return redirect()->route('status', ['status_id' => $status_id])
    135             ->with('status', $status);
    136     }
    137 
    138     /**
    139      * Unfavourite a Status.
    140      *
    141      * @param string $status_id The ID of the Status to unfavourite.
    142      *
    143      * @return Illuminate\Routing\Redirector Redirect to the Status view page.
    144      */
    145     public function unfavourite_status(string $status_id)
    146     {
    147         $user = session('user');
    148 
    149         # Unfavourite request to the API.
    150         $status = Mastodon::domain(env('MASTODON_DOMAIN'))
    151             ->token($user->token)
    152             ->post('/statuses/' . $status_id . '/unfavourite');
    153 
    154         return redirect()->route('status', ['status_id' => $status_id])
    155             ->with('status', $status);
    156     }
    157 
    158     /**
    159      * Post a new Status.
    160      *
    161      * @param Request $request Request containing the form submission.
    162      *
    163      * @return Illuminate\Routing\Redirector Redirect to the home timeline page.
    164      */
    165     public function post_status(Request $request)
    166     {
    167         $user = session('user');
    168 
    169         # Verify we have an actual status to post.
    170         if (!$request->has('status'))
    171         {
    172             abort(400);
    173         }
    174 
    175         $params = [
    176             'status' => $request->status
    177         ];
    178 
    179         $inputs = [
    180             'in_reply_to_id',
    181             'media_ids',
    182             'sensitive',
    183             'spoiler_text',
    184             'visibility',
    185             'language'
    186         ];
    187 
    188         # Grab each of the optional inputs from the form.
    189         # Not all of these are present in the HTML of the view yet.
    190         foreach ($inputs as $input)
    191         {
    192             if ($request->has($input))
    193             {
    194                 $params[$input] = $request->input($input);
    195             }
    196         }
    197 
    198         # Post the Status via the API.
    199         $new_status = Mastodon::domain(env('MASTODON_DOMAIN'))
    200             ->token($user->token)
    201             ->post('/statuses', $params);
    202 
    203         return redirect()->route('home');
    204     }
    205 
    206     /**
    207      *
    208      * Delete a Status.
    209      *
    210      * @param string $status_id The ID of the Status to be deleted.
    211      *
    212      * @return Illuminate\Routing\Redirector Redirect to the home timeline page.
    213      */
    214     public function delete_status(string $status_id)
    215     {
    216         $user = session('user');
    217 
    218 	if (session()->has('delete_status') && session('delete_status') === $status_id)
    219 	{
    220             # The user has confirmed the deletion, so go ahead and delete the Status.
    221 
    222             Mastodon::domain(env('MASTODON_DOMAIN'))
    223                 ->token($user->token)
    224                 ->call('DELETE', '/statuses/' . $status_id);
    225 
    226 	    return redirect()->route('home');
    227 	}
    228 	else
    229 	{
    230             # Render the confirmation page.
    231 
    232             session()->flash('delete_status', $status_id);
    233 
    234             try
    235             {
    236                 $status = Mastodon::domain(env('MASTODON_DOMAIN'))
    237                     ->token(session('user')->token)
    238                     ->get('/statuses/' . $status_id);
    239             }
    240             catch (\GuzzleHttp\Exception\ServerException $ex)
    241 	    {
    242                 $vars = [
    243                     'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1],
    244                     'message' => 'Status not found.'
    245                 ];
    246 
    247                 return view('error', $vars);
    248             }
    249 
    250             $vars = [
    251                 'status' => $status,
    252                 'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1]
    253             ];
    254 	}
    255 
    256         return view('delete_status', $vars);
    257     }
    258 
    259     /**
    260      * Show the context of a Status.
    261      *
    262      * Show a Status in its thread of ancestors and descendants.
    263      *
    264      * @param string $status_id The ID of the Status to display.
    265      *
    266      * @return Illuminate\View\View The context view page.
    267      */
    268     public function context(string $status_id)
    269     {
    270         # Get the Status itself.
    271         $status = Mastodon::domain(env('MASTODON_DOMAIN'))
    272             ->get('/statuses/' . $status_id);
    273 
    274         # Get the context.
    275         $context = Mastodon::domain(env('MASTODON_DOMAIN'))
    276             ->get('/statuses/' . $status_id . '/context');
    277 
    278         $vars = [
    279             'status' => $status,
    280             'context' => $context,
    281             'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1]
    282         ];
    283 
    284         return view('context', $vars);
    285     }
    286 }