Add ability to favourite a status

Implemented the ability to favourite and unfavourite a status.
This commit is contained in:
St John Karp 2018-08-18 15:48:00 -07:00
parent 7cd1d14d8a
commit 1ca86208d5
8 changed files with 61 additions and 189 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -47,7 +47,7 @@ div.status img.avatar {
}
.favourited {
color: gold;
color: goldenrod;
}
div.actions span {

View File

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

View File

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