Update GroupsSearchController.php

This commit is contained in:
Daniel Supernault 2024-07-23 01:29:56 -06:00
parent 3d99972653
commit a76f3b0e2e
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -13,6 +13,8 @@ use App\Profile;
use App\Models\Group; use App\Models\Group;
use App\Models\GroupMember; use App\Models\GroupMember;
use App\Models\GroupInvitation; use App\Models\GroupInvitation;
use App\Util\ActivityPub\Helpers;
use App\Services\Groups\GroupActivityPubService;
class GroupsSearchController extends Controller class GroupsSearchController extends Controller
{ {
@ -118,32 +120,34 @@ class GroupsSearchController extends Controller
{ {
abort_if(!$request->user(), 404); abort_if(!$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'q' => 'required|min:2|max:40', 'q' => 'required|min:2|max:140',
'v' => 'required|in:0.2' 'v' => 'required|in:0.2'
]); ]);
$q = $request->input('q'); $q = $request->input('q');
$key = 'groups:search:global:by_name:' . hash('sha256', $q);
if(RateLimiter::tooManyAttempts('groups:search:global:'.$request->user()->id, 25) ) { if(str_starts_with($q, 'https://')) {
return response()->json([ $res = Helpers::getSignedFetch($q);
'error' => [ if($res && $res = json_decode($res, true)) {
'message' => 'Too many attempts, please try again later'
] }
], 422); if($res && isset($res['type']) && in_array($res['type'], ['Group', 'Note', 'Page'])) {
if($res['type'] === 'Group') {
return GroupActivityPubService::fetchGroup($q, true);
}
$resp = GroupActivityPubService::fetchGroupPost($q, true);
$resp['name'] = 'Group Post';
$resp['url'] = '/groups/' . $resp['group_id'] . '/p/' . $resp['id'];
return [$resp];
}
} }
return Group::whereNull('status')
RateLimiter::hit('groups:search:global:'.$request->user()->id); ->where('name', 'like', '%' . $q . '%')
->orderBy('id')
return Cache::remember($key, 3600, function() use($q) { ->take(10)
return Group::whereNull('status') ->pluck('id')
->where('name', 'like', '%' . $q . '%') ->map(function($group) {
->orderBy('id') return GroupService::get($group);
->take(10) });
->pluck('id')
->map(function($group) {
return GroupService::get($group);
});
});
} }
public function searchLocalAutocomplete(Request $request) public function searchLocalAutocomplete(Request $request)