From 44a60d745beef4cce58365fca14f4e5ba71f2a5f Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 17 Apr 2019 20:36:14 -0600 Subject: [PATCH] Update search --- app/Http/Controllers/SearchController.php | 19 +++++++++---- .../assets/js/components/SearchResults.vue | 27 +++++++++++-------- routes/web.php | 4 +-- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index e9b98438e..cc3ed6501 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -9,6 +9,7 @@ use App\Status; use Illuminate\Http\Request; use App\Util\ActivityPub\Helpers; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Str; use App\Transformer\Api\{ AccountTransformer, HashtagTransformer, @@ -22,11 +23,14 @@ class SearchController extends Controller $this->middleware('auth'); } - public function searchAPI(Request $request, $tag) + public function searchAPI(Request $request) { - if(mb_strlen($tag) < 3) { - return; - } + $this->validate($request, [ + 'q' => 'required|string|min:3|max:120', + 'src' => 'required|string|in:metro', + 'v' => 'required|integer|in:1' + ]); + $tag = $request->input('q'); $tag = e(urldecode($tag)); $hash = hash('sha256', $tag); @@ -65,7 +69,12 @@ class SearchController extends Controller } } } - $hashtags = Hashtag::select('id', 'name', 'slug')->where('slug', 'like', '%'.$tag.'%')->whereHas('posts')->limit(20)->get(); + $htag = Str::startsWith($tag, '#') == true ? mb_substr($tag, 1) : $tag; + $hashtags = Hashtag::select('id', 'name', 'slug') + ->where('slug', 'like', '%'.$htag.'%') + ->whereHas('posts') + ->limit(20) + ->get(); if($hashtags->count() > 0) { $tags = $hashtags->map(function ($item, $key) { return [ diff --git a/resources/assets/js/components/SearchResults.vue b/resources/assets/js/components/SearchResults.vue index ff53bdfd4..f0f3a1281 100644 --- a/resources/assets/js/components/SearchResults.vue +++ b/resources/assets/js/components/SearchResults.vue @@ -124,17 +124,22 @@ export default { }, methods: { fetchSearchResults() { - axios.get('/api/search/' + encodeURI(this.query)) - .then(res => { - let results = res.data; - this.results.hashtags = results.hashtags; - this.results.profiles = results.profiles; - this.results.statuses = results.posts; - this.loading = false; - }).catch(err => { - this.loading = false; - // this.networkError = true; - }) + axios.get('/api/search', { + params: { + 'q': this.query, + 'src': 'metro', + 'v': 1 + } + }).then(res => { + let results = res.data; + this.results.hashtags = results.hashtags; + this.results.profiles = results.profiles; + this.results.statuses = results.posts; + this.loading = false; + }).catch(err => { + this.loading = false; + // this.networkError = true; + }) }, followProfile(id) { diff --git a/routes/web.php b/routes/web.php index d23a00b00..8e9252176 100644 --- a/routes/web.php +++ b/routes/web.php @@ -64,9 +64,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::get('discover', 'DiscoverController@home')->name('discover'); Route::group(['prefix' => 'api'], function () { - Route::get('search/{tag}', 'SearchController@searchAPI') - //->where('tag', '.*'); - ->where('tag', '[A-Za-z0-9]+'); + Route::get('search', 'SearchController@searchAPI'); Route::get('nodeinfo/2.0.json', 'FederationController@nodeinfo'); Route::group(['prefix' => 'v1'], function () {