From 4573fd850fac80e19e86ae94ece81f9c9011d686 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Fri, 7 Feb 2020 01:27:05 -0700 Subject: [PATCH] Update SearchApiV2Service --- app/Services/SearchApiV2Service.php | 93 ++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 9 deletions(-) diff --git a/app/Services/SearchApiV2Service.php b/app/Services/SearchApiV2Service.php index f2dc9d28..86c2073f 100644 --- a/app/Services/SearchApiV2Service.php +++ b/app/Services/SearchApiV2Service.php @@ -11,6 +11,7 @@ use League\Fractal; use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Pagination\IlluminatePaginatorAdapter; use App\Util\ActivityPub\Helpers; +use Illuminate\Support\Str; class SearchApiV2Service { @@ -25,14 +26,11 @@ class SearchApiV2Service { $this->query = $query; - if($query->has('resolve') && $query->resolve == true && Helpers::validateUrl(urldecode($query))) { - return [ - 'accounts' => [ - $this->resolve() - ], - 'hashtags' => [], - 'statuses' => [] - ]; + if($query->has('resolve') && + $query->resolve == true && + Helpers::validateUrl(urldecode($query->input('q'))) + ) { + return $this->resolve(); } if($query->has('type')) { @@ -78,7 +76,12 @@ class SearchApiV2Service protected function resolve() { - return WebfingerService::lookup($this->query->input('q')); + $query = urldecode($this->query->input('q')); + if(Str::startsWith($query, '@') == true) { + return WebfingerService::lookup($this->query->input('q')); + } else if (Str::startsWith($query, 'https://') == true) { + return $this->resolveQuery(); + } } protected function accounts() @@ -124,6 +127,7 @@ class SearchApiV2Service $limit = $this->query->input('limit', 20); $query = '%' . $this->query->input('q') . '%'; $results = Status::where('caption', 'like', $query) + ->whereScope('public') ->when($this->query->input('offset') != null, function($q, $offset) { return $q->offset($offset); }) @@ -155,4 +159,75 @@ class SearchApiV2Service return $fractal->createData($resource)->toArray(); } + protected function resolveQuery() + { + $query = urldecode($this->query->input('q')); + if(Helpers::validateLocalUrl($query, false)) { + if(Str::contains($query, '/p/')) { + return $this->resolveLocalStatus(); + } else { + return $this->resolveLocalProfile(); + } + } else { + return [ + 'accounts' => [], + 'hashtags' => [], + 'statuses' => [] + ]; + } + } + + protected function resolveLocalStatus() + { + $query = urldecode($this->query->input('q')); + $query = last(explode('/', $query)); + $status = Status::whereNull('uri') + ->whereScope('public') + ->find($query); + + if(!$status) { + return [ + 'accounts' => [], + 'hashtags' => [], + 'statuses' => [] + ]; + } + + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Item($status, new StatusTransformer()); + return [ + 'accounts' => [], + 'hashtags' => [], + 'statuses' => $fractal->createData($resource)->toArray() + ]; + } + + protected function resolveLocalProfile() + { + $query = urldecode($this->query->input('q')); + $query = last(explode('/', $query)); + $profile = Profile::whereNull('status') + ->whereNull('domain') + ->whereUsername($query) + ->first(); + + if(!$profile) { + return [ + 'accounts' => [], + 'hashtags' => [], + 'statuses' => [] + ]; + } + + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); + return [ + 'accounts' => $fractal->createData($resource)->toArray(), + 'hashtags' => [], + 'statuses' => [] + ]; + } + } \ No newline at end of file