planiverse

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

commit 1ca86208d588f946b2630015d220ada5373b2db6
parent 7cd1d14d8a346209af217452239981e7f523bd8e
Author: St John Karp <stjohn@fuzzjunket.com>
Date:   Sat, 18 Aug 2018 15:48:00 -0700

Add ability to favourite a status

Implemented the ability to favourite and unfavourite a status.

Diffstat:
Dapp/Http/Controllers/Auth/ForgotPasswordController.php | 32--------------------------------
Dapp/Http/Controllers/Auth/LoginController.php | 39---------------------------------------
Dapp/Http/Controllers/Auth/RegisterController.php | 71-----------------------------------------------------------------------
Dapp/Http/Controllers/Auth/ResetPasswordController.php | 39---------------------------------------
Mapp/Http/Controllers/StatusController.php | 54+++++++++++++++++++++++++++++++++++++++++++++++++++---
Mpublic/css/styles.css | 2+-
Mresources/views/status.blade.php | 6+++---
Mroutes/web.php | 7++++++-
8 files changed, 61 insertions(+), 189 deletions(-)

diff --git a/app/Http/Controllers/Auth/ForgotPasswordController.php b/app/Http/Controllers/Auth/ForgotPasswordController.php @@ -1,32 +0,0 @@ -<?php - -namespace App\Http\Controllers\Auth; - -use App\Http\Controllers\Controller; -use Illuminate\Foundation\Auth\SendsPasswordResetEmails; - -class ForgotPasswordController extends Controller -{ - /* - |-------------------------------------------------------------------------- - | Password Reset Controller - |-------------------------------------------------------------------------- - | - | This controller is responsible for handling password reset emails and - | includes a trait which assists in sending these notifications from - | your application to your users. Feel free to explore this trait. - | - */ - - use SendsPasswordResetEmails; - - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('guest'); - } -} diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php @@ -1,39 +0,0 @@ -<?php - -namespace App\Http\Controllers\Auth; - -use App\Http\Controllers\Controller; -use Illuminate\Foundation\Auth\AuthenticatesUsers; - -class LoginController extends Controller -{ - /* - |-------------------------------------------------------------------------- - | Login Controller - |-------------------------------------------------------------------------- - | - | This controller handles authenticating users for the application and - | redirecting them to your home screen. The controller uses a trait - | to conveniently provide its functionality to your applications. - | - */ - - use AuthenticatesUsers; - - /** - * Where to redirect users after login. - * - * @var string - */ - protected $redirectTo = '/home'; - - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('guest')->except('logout'); - } -} diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php @@ -1,71 +0,0 @@ -<?php - -namespace App\Http\Controllers\Auth; - -use App\User; -use App\Http\Controllers\Controller; -use Illuminate\Support\Facades\Validator; -use Illuminate\Foundation\Auth\RegistersUsers; - -class RegisterController extends Controller -{ - /* - |-------------------------------------------------------------------------- - | Register Controller - |-------------------------------------------------------------------------- - | - | This controller handles the registration of new users as well as their - | validation and creation. By default this controller uses a trait to - | provide this functionality without requiring any additional code. - | - */ - - use RegistersUsers; - - /** - * Where to redirect users after registration. - * - * @var string - */ - protected $redirectTo = '/home'; - - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('guest'); - } - - /** - * Get a validator for an incoming registration request. - * - * @param array $data - * @return \Illuminate\Contracts\Validation\Validator - */ - protected function validator(array $data) - { - return Validator::make($data, [ - 'name' => 'required|string|max:255', - 'email' => 'required|string|email|max:255|unique:users', - 'password' => 'required|string|min:6|confirmed', - ]); - } - - /** - * Create a new user instance after a valid registration. - * - * @param array $data - * @return \App\User - */ - protected function create(array $data) - { - return User::create([ - 'name' => $data['name'], - 'email' => $data['email'], - 'password' => bcrypt($data['password']), - ]); - } -} diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php @@ -1,39 +0,0 @@ -<?php - -namespace App\Http\Controllers\Auth; - -use App\Http\Controllers\Controller; -use Illuminate\Foundation\Auth\ResetsPasswords; - -class ResetPasswordController extends Controller -{ - /* - |-------------------------------------------------------------------------- - | Password Reset Controller - |-------------------------------------------------------------------------- - | - | This controller is responsible for handling password reset requests - | and uses a simple trait to include this behavior. You're free to - | explore this trait and override any methods you wish to tweak. - | - */ - - use ResetsPasswords; - - /** - * Where to redirect users after resetting their password. - * - * @var string - */ - protected $redirectTo = '/home'; - - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('guest'); - } -} diff --git a/app/Http/Controllers/StatusController.php b/app/Http/Controllers/StatusController.php @@ -9,10 +9,22 @@ use Illuminate\Http\Request; class StatusController extends Controller { - public function show_status(Request $request, string $status_id) + public function show_status(string $status_id) { - $status = Mastodon::domain(env('MASTODON_DOMAIN')) - ->get('/statuses/' . $status_id); + if (session()->has('return_status')) + { + // This route can be called as a redirect after favouriting, etc., + // in which case the status returned by the API will have been stored + // in the user's session. + $status = session('return_status'); + session()->forget('return_status'); + } + else + { + // If the status isn't in the session, we need to query for it. + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->get('/statuses/' . $status_id); + } $vars = [ 'status' => $status, @@ -22,4 +34,40 @@ class StatusController extends Controller return view('show_status', $vars); } + + public function favourite_status(string $status_id) + { + # Check the user is logged in. + if (!session()->has('user')) + { + return redirect()->route('login'); + } + $user = session('user'); + + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses/' . $status_id . '/favourite'); + + session(['return_status' => $status]); + + return redirect()->route('status', ['status_id' => $status_id]); + } + + public function unfavourite_status(string $status_id) + { + # Check the user is logged in. + if (!session()->has('user')) + { + return redirect()->route('login'); + } + $user = session('user'); + + $status = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->post('/statuses/' . $status_id . '/unfavourite'); + + session(['return_status' => $status]); + + return redirect()->route('status', ['status_id' => $status_id]); + } } diff --git a/public/css/styles.css b/public/css/styles.css @@ -47,7 +47,7 @@ div.status img.avatar { } .favourited { - color: gold; + color: goldenrod; } div.actions span { diff --git a/resources/views/status.blade.php b/resources/views/status.blade.php @@ -53,10 +53,10 @@ <!-- Favourite --> <span> - @if ($status['favourited'] === true) - <span class="favourited">&#9733;</span> + @if ($status['favourited']) + <span class="favourited"><a href="/status/{{ $status['id'] }}/unfavourite">&#9733;</a></span> @else - &#9734; + <a href="/status/{{ $status['id'] }}/favourite">&#9734;</a> @endif {{ $status['favourites_count'] }} </span> diff --git a/routes/web.php b/routes/web.php @@ -30,7 +30,12 @@ Route::get('/timeline/home', 'TimelineController@home_timeline') Route::post('/timeline/home', 'TimelineController@post_status'); -Route::get('/status/{status_id}', 'StatusController@show_status'); +Route::get('/status/{status_id}', 'StatusController@show_status') + ->name('status'); + +Route::get('/status/{status_id}/favourite', 'StatusController@favourite_status'); + +Route::get('/status/{status_id}/unfavourite', 'StatusController@unfavourite_status'); Route::get('/login', 'LoginController@login') ->name('login');