diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index e9b98438..cc3ed650 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 ff53bdfd..f0f3a128 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 d23a00b0..8e925217 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 () {