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.
This commit is contained in:
St John Karp 2019-02-17 16:41:35 +00:00
parent e13542d4fd
commit daa9343cb0
6 changed files with 119 additions and 103 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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