소스 검색

Update search

Daniel Supernault 6 년 전
부모
커밋
44a60d745b
3개의 변경된 파일31개의 추가작업 그리고 19개의 파일을 삭제
  1. 14 5
      app/Http/Controllers/SearchController.php
  2. 16 11
      resources/assets/js/components/SearchResults.vue
  3. 1 3
      routes/web.php

+ 14 - 5
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 [

+ 16 - 11
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) {

+ 1 - 3
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 () {