123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace App\Services;
- use Cache;
- use Illuminate\Support\Facades\Redis;
- use App\{Hashtag, Profile, Status};
- use App\Transformer\Api\AccountTransformer;
- use App\Transformer\Api\StatusTransformer;
- use League\Fractal;
- use League\Fractal\Serializer\ArraySerializer;
- use League\Fractal\Pagination\IlluminatePaginatorAdapter;
- use App\Util\ActivityPub\Helpers;
- use Illuminate\Support\Str;
- use App\Services\AccountService;
- use App\Services\HashtagService;
- use App\Services\StatusService;
- class SearchApiV2Service
- {
- private $query;
- public static function query($query)
- {
- return (new self)->run($query);
- }
- protected function run($query)
- {
- $this->query = $query;
- $q = urldecode($query->input('q'));
- if($query->has('resolve') &&
- $query->resolve == true &&
- ( Str::startsWith($q, 'https://') ||
- Str::substrCount($q, '@') == 2)
- ) {
- return $this->resolveQuery();
- }
- if($query->has('type')) {
- switch ($query->input('type')) {
- case 'accounts':
- return [
- 'accounts' => $this->accounts(),
- 'hashtags' => [],
- 'statuses' => []
- ];
- break;
- case 'hashtags':
- return [
- 'accounts' => [],
- 'hashtags' => $this->hashtags(),
- 'statuses' => []
- ];
- break;
- case 'statuses':
- return [
- 'accounts' => [],
- 'hashtags' => [],
- 'statuses' => $this->statuses()
- ];
- break;
- }
- }
- if($query->has('account_id')) {
- return [
- 'accounts' => [],
- 'hashtags' => [],
- 'statuses' => $this->statusesById()
- ];
- }
- return [
- 'accounts' => $this->accounts(),
- 'hashtags' => $this->hashtags(),
- 'statuses' => $this->statuses()
- ];
- }
- protected function accounts()
- {
- $user = request()->user();
- $limit = $this->query->input('limit') ?? 20;
- $offset = $this->query->input('offset') ?? 0;
- $query = '%' . $this->query->input('q') . '%';
- $banned = InstanceService::getBannedDomains();
- $results = Profile::select('profiles.*', 'followers.profile_id', 'followers.created_at')
- ->whereNull('status')
- ->leftJoin('followers', function($join) use($user) {
- return $join->on('profiles.id', '=', 'followers.following_id')
- ->where('followers.profile_id', $user->profile_id);
- })
- ->where('username', 'like', $query)
- ->orderByDesc('profiles.followers_count')
- ->orderByDesc('followers.created_at')
- ->offset($offset)
- ->limit($limit)
- ->get()
- ->filter(function($profile) use ($banned) {
- return in_array($profile->domain, $banned) == false;
- })
- ->map(function($res) {
- return AccountService::get($res['id']);
- })
- ->filter(function($account) {
- return $account && isset($account['id']);
- })
- ->values();
- return $results;
- }
- protected function hashtags()
- {
- $limit = $this->query->input('limit') ?? 20;
- $offset = $this->query->input('offset') ?? 0;
- $query = '%' . $this->query->input('q') . '%';
- return Hashtag::whereIsBanned(false)
- ->where('name', 'like', $query)
- ->offset($offset)
- ->limit($limit)
- ->get()
- ->map(function($tag) {
- return [
- 'name' => $tag->name,
- 'url' => $tag->url(),
- 'count' => HashtagService::count($tag->id),
- 'history' => []
- ];
- });
- }
- protected function statuses()
- {
- // Removed until we provide more relevent sorting/results
- return [];
- }
- protected function statusesById()
- {
- $accountId = $this->query->input('account_id');
- $limit = $this->query->input('limit', 20);
- $query = '%' . $this->query->input('q') . '%';
- $results = Status::where('caption', 'like', $query)
- ->whereProfileId($accountId)
- ->limit($limit)
- ->get()
- ->map(function($status) {
- return StatusService::get($status->id);
- })
- ->filter(function($status) {
- return $status && isset($status['account']);
- });
- return $results;
- }
- protected function resolveQuery()
- {
- $query = urldecode($this->query->input('q'));
- if(Helpers::validateLocalUrl($query)) {
- if(Str::contains($query, '/p/')) {
- return $this->resolveLocalStatus();
- } else {
- return $this->resolveLocalProfile();
- }
- } else {
- $default = [
- 'accounts' => [],
- 'hashtags' => [],
- 'statuses' => [],
- ];
- if(!Helpers::validateUrl($query) && strpos($query, '@') == -1) {
- return $default;
- }
- if(Str::substrCount($query, '@') == 2) {
- try {
- $res = WebfingerService::lookup($query);
- } catch (\Exception $e) {
- return $default;
- }
- if($res && isset($res['id'])) {
- $default['accounts'][] = $res;
- return $default;
- } else {
- return $default;
- }
- }
- try {
- $res = ActivityPubFetchService::get($query);
- $banned = InstanceService::getBannedDomains();
- if($res) {
- $json = json_decode($res, true);
- if(!$json || !isset($json['@context']) || !isset($json['type']) || !in_array($json['type'], ['Note', 'Person'])) {
- return [
- 'accounts' => [],
- 'hashtags' => [],
- 'statuses' => [],
- ];
- }
- switch($json['type']) {
- case 'Note':
- $obj = Helpers::statusFetch($query);
- if(!$obj || !isset($obj['id'])) {
- return $default;
- }
- $note = StatusService::get($obj['id']);
- if(!$note) {
- return $default;
- }
- $default['statuses'][] = $note;
- return $default;
- break;
- case 'Person':
- $obj = Helpers::profileFetch($query);
- if(!$obj) {
- return $default;
- }
- if(in_array($obj['domain'], $banned)) {
- return $default;
- }
- $default['accounts'][] = AccountService::get($obj['id']);
- return $default;
- break;
- default:
- return [
- 'accounts' => [],
- 'hashtags' => [],
- 'statuses' => [],
- ];
- break;
- }
- }
- } catch (\Exception $e) {
- return [
- 'accounts' => [],
- 'hashtags' => [],
- 'statuses' => [],
- ];
- }
- return $default;
- }
- }
- 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' => []
- ];
- }
- }
|