planiverse

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

commit daa9343cb02c07bbc158e26c2c4e105203c16da5
parent e13542d4fd5dac0dafe5c00f198f02c1e0c65119
Author: St John Karp <stjohn@fuzzjunket.com>
Date:   Sun, 17 Feb 2019 16:41:35 +0000

Implement searching for statuses and accounts

Implemented search across statuses and accounts. Replaced the old
search that was for accounts only and never quite worked right.

Don't load empty results when visiting the search page.

Diffstat:
Mapp/Http/Controllers/AccountController.php | 55-------------------------------------------------------
Aapp/Http/Controllers/SearchController.php | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mresources/views/navigation.blade.php | 2+-
Aresources/views/search.blade.php | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Dresources/views/search_accounts.blade.php | 43-------------------------------------------
Mroutes/web.php | 8++++----
6 files changed, 119 insertions(+), 103 deletions(-)

diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php @@ -112,59 +112,4 @@ class AccountController extends Controller return redirect()->route('account', ['account_id' => $account_id]) ->with('relationship', $relationship); } - - /** - * Show the page that lets users search for an Account. - * - * @return Illuminate\View\View The search page. - */ - public function show_search() - { - if (session()->has('accounts')) - { - # The user is coming here after peforming a search. - - $accounts = session('accounts'); - } - else - { - $accounts = []; - } - - $vars = [ - 'accounts' => $accounts, - 'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1] - ]; - - return view('search_accounts', $vars); - } - - /** - * Process a search request. - * - * @param Request $request The POST request with search parameters. - * - * @return Illuminate\Routing\Redirector Redirect to the search page. - */ - public function search(Request $request) - { - $user = session('user'); - - # Verify we have an actual account to search for. - if (!$request->has('account')) - { - abort(400); - } - - # Query the search end-point. - /* To-do: currently this always throws an exception from Guzzle. - I've commented out the nav link to the search page until I can figure - this out. */ - $accounts = Mastodon::domain(env('MASTODON_DOMAIN')) - ->token($user->token) - ->get('/accounts/search', ['q' => $request->account]); - - return redirect()->route('show_search_accounts') - ->with('accounts', $accounts); - } } diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php @@ -0,0 +1,65 @@ +<?php + +namespace App\Http\Controllers; + +use App\Http\Controllers\Controller; +use Mastodon; +use Illuminate\Http\Request; + +/** + * Controller for search functions. + */ +class SearchController extends Controller +{ + /** + * Show the page that lets users search across Accounts and Statuses. + * + * @return Illuminate\View\View The search page. + */ + public function show_search() + { + if (session()->has('results')) + { + # The user is coming here after peforming a search. + + $results = session('results'); + } + else + { + $results = null; + } + + $vars = [ + 'results' => $results, + 'mastodon_domain' => explode('//', env('MASTODON_DOMAIN'))[1] + ]; + + return view('search', $vars); + } + + /** + * Process a search request. + * + * @param Request $request The POST request with search parameters. + * + * @return Illuminate\Routing\Redirector Redirect to the search page. + */ + public function search(Request $request) + { + $user = session('user'); + + # Verify we have an actual search term. + if (!$request->has('search_term')) + { + abort(400); + } + + # Query the search end-point. + $results = Mastodon::domain(env('MASTODON_DOMAIN')) + ->token($user->token) + ->get('/search', ['q' => $request->search_term]); + + return redirect()->route('show_search') + ->with('results', $results); + } +} diff --git a/resources/views/navigation.blade.php b/resources/views/navigation.blade.php @@ -3,6 +3,6 @@ <li><a href="{{ route('home') }}">Timeline</a></li> <li><a href="{{ route('public') }}">Public Timeline</a></li> <li><a href="{{ route('notifications') }}">Notifications</a></li> - <!--<li><a href="{{ route('show_search_accounts') }}">Search</a></li>--> + <li><a href="{{ route('show_search') }}">Search</a></li> </ul> </nav> diff --git a/resources/views/search.blade.php b/resources/views/search.blade.php @@ -0,0 +1,49 @@ +<!doctype html> +<html lang="{{ app()->getLocale() }}"> + <head> + <meta charset="utf-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + + <title>{{ $mastodon_domain }} | Search</title> + + <link rel="stylesheet" href="{{ url('css/styles.css') }}" /> + </head> + <body> + <h1>{{ $mastodon_domain }} | Search</h1> + + @component('navigation') + @endcomponent + + <form method="post" action="{{ route('search') }}"> + <input type="text" name="search_term" placeholder="Search accounts and statuses" required autofocus /> + <input type="submit" value="Search" /> + {{ csrf_field() }} + </form> + + @if ($results !== null) + <ul> + @foreach ($results['accounts'] as $account) + <li> + <span class="account" title="{{ $account['acct'] }}"> + <a href="{{ $account['url'] }}"> + <img + src="{{ $account['avatar'] }}" + alt="{{ $account['acct'] }}" + class="avatar" + /> + {{ $account['display_name'] }} + @if ($account['bot'] ?? false) &#129302; @endif + @if ($account['locked']) &#128274; @endif + </a> + </span> + </li> + @endforeach + @foreach ($results['statuses'] as $status) + @component('status', ['status' => $status]) + @endcomponent + @endforeach + </ul> + @endif + </body> +</html> diff --git a/resources/views/search_accounts.blade.php b/resources/views/search_accounts.blade.php @@ -1,43 +0,0 @@ -<!doctype html> -<html lang="{{ app()->getLocale() }}"> - <head> - <meta charset="utf-8"> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - - <title>{{ $mastodon_domain }} | Search</title> - - <link rel="stylesheet" href="{{ url('css/styles.css') }}" /> - </head> - <body> - <h1>{{ $mastodon_domain }} | Search</h1> - - @component('navigation') - @endcomponent - - <form method="post" action="{{ route('search_accounts') }}"> - <input type="text" name="account" placeholder="Account" required autofocus /> - <input type="submit" value="Search" /> - {{ csrf_field() }} - </form> - - <ul> - @foreach ($accounts as $account) - <li> - <span class="account" title="{{ $account['acct'] }}"> - <a href="{{ $account['url'] }}"> - <img - src="{{ $account['avatar'] }}" - alt="{{ $account['acct'] }}" - class="avatar" - /> - {{ $account['display_name'] }} - @if ($account['bot'] ?? false) &#129302; @endif - @if ($account['locked']) &#128274; @endif - </a> - </span> - </li> - @endforeach - </ul> - </body> -</html> diff --git a/routes/web.php b/routes/web.php @@ -71,12 +71,12 @@ Route::get('/account/{account_id}/unfollow', 'AccountController@unfollow_account ->name('unfollow') ->middleware('authorize'); -Route::get('/accounts/search', 'AccountController@show_search') - ->name('show_search_accounts') +Route::get('/search', 'SearchController@show_search') + ->name('show_search') ->middleware('authorize'); -Route::post('/accounts/search', 'AccountController@search') - ->name('search_accounts') +Route::post('/search', 'SearchController@search') + ->name('search') ->middleware('authorize'); Route::get('/login', 'LoginController@login')