2020-02-07 00:12:20 +00:00
|
|
|
<?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;
|
2020-02-07 08:27:05 +00:00
|
|
|
use Illuminate\Support\Str;
|
2021-12-05 00:32:43 +00:00
|
|
|
use App\Services\AccountService;
|
|
|
|
use App\Services\HashtagService;
|
|
|
|
use App\Services\StatusService;
|
2020-02-07 00:12:20 +00:00
|
|
|
|
|
|
|
class SearchApiV2Service
|
|
|
|
{
|
|
|
|
private $query;
|
2022-03-23 11:58:14 +00:00
|
|
|
static $mastodonMode = false;
|
2020-02-07 00:12:20 +00:00
|
|
|
|
2022-03-23 11:58:14 +00:00
|
|
|
public static function query($query, $mastodonMode = false)
|
2020-02-07 00:12:20 +00:00
|
|
|
{
|
2022-03-23 11:58:14 +00:00
|
|
|
self::$mastodonMode = $mastodonMode;
|
2020-02-07 00:12:20 +00:00
|
|
|
return (new self)->run($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function run($query)
|
|
|
|
{
|
|
|
|
$this->query = $query;
|
2022-01-10 06:15:45 +00:00
|
|
|
$q = urldecode($query->input('q'));
|
2020-02-07 00:12:20 +00:00
|
|
|
|
2020-02-07 08:27:05 +00:00
|
|
|
if($query->has('resolve') &&
|
2022-01-10 06:15:45 +00:00
|
|
|
( Str::startsWith($q, 'https://') ||
|
2022-08-28 12:28:46 +00:00
|
|
|
Str::substrCount($q, '@') >= 1)
|
2020-02-07 08:27:05 +00:00
|
|
|
) {
|
2022-01-10 06:15:45 +00:00
|
|
|
return $this->resolveQuery();
|
2020-02-07 00:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if($query->has('type')) {
|
|
|
|
switch ($query->input('type')) {
|
|
|
|
case 'accounts':
|
|
|
|
return [
|
2020-02-07 07:26:45 +00:00
|
|
|
'accounts' => $this->accounts(),
|
2020-02-07 00:12:20 +00:00
|
|
|
'hashtags' => [],
|
|
|
|
'statuses' => []
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 'hashtags':
|
|
|
|
return [
|
|
|
|
'accounts' => [],
|
2020-02-07 07:26:45 +00:00
|
|
|
'hashtags' => $this->hashtags(),
|
2020-02-07 00:12:20 +00:00
|
|
|
'statuses' => []
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 'statuses':
|
|
|
|
return [
|
|
|
|
'accounts' => [],
|
|
|
|
'hashtags' => [],
|
2020-02-07 07:26:45 +00:00
|
|
|
'statuses' => $this->statuses()
|
2020-02-07 00:12:20 +00:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($query->has('account_id')) {
|
|
|
|
return [
|
|
|
|
'accounts' => [],
|
|
|
|
'hashtags' => [],
|
|
|
|
'statuses' => $this->statusesById()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'accounts' => $this->accounts(),
|
|
|
|
'hashtags' => $this->hashtags(),
|
|
|
|
'statuses' => $this->statuses()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-28 12:28:46 +00:00
|
|
|
protected function accounts($initalQuery = false)
|
2020-02-07 00:12:20 +00:00
|
|
|
{
|
2022-03-23 11:58:14 +00:00
|
|
|
$mastodonMode = self::$mastodonMode;
|
2021-12-05 00:32:43 +00:00
|
|
|
$user = request()->user();
|
2020-05-16 03:05:41 +00:00
|
|
|
$limit = $this->query->input('limit') ?? 20;
|
|
|
|
$offset = $this->query->input('offset') ?? 0;
|
2022-08-28 12:28:46 +00:00
|
|
|
$rawQuery = $initalQuery ? $initalQuery : $this->query->input('q');
|
|
|
|
$query = '%' . $rawQuery . '%';
|
|
|
|
if(Str::substrCount($rawQuery, '@') >= 1 && Str::contains($rawQuery, config('pixelfed.domain.app'))) {
|
|
|
|
$deliminatorCount = Str::substrCount($rawQuery, '@');
|
|
|
|
$query = explode('@', $rawQuery)[$deliminatorCount == 1 ? 0 : 1];
|
|
|
|
}
|
|
|
|
if(Str::substrCount($rawQuery, '@') == 1 && substr($rawQuery, 0, 1) == '@') {
|
|
|
|
$query = substr($rawQuery, 1) . '%';
|
|
|
|
}
|
2022-03-13 06:15:47 +00:00
|
|
|
$banned = InstanceService::getBannedDomains();
|
2022-12-27 11:43:04 +00:00
|
|
|
$results = Profile::select('username', 'id', 'followers_count', 'domain')
|
2020-02-07 00:12:20 +00:00
|
|
|
->where('username', 'like', $query)
|
2021-12-05 00:32:43 +00:00
|
|
|
->orderByDesc('profiles.followers_count')
|
2020-05-16 03:05:41 +00:00
|
|
|
->offset($offset)
|
2020-02-07 00:12:20 +00:00
|
|
|
->limit($limit)
|
2021-12-05 00:32:43 +00:00
|
|
|
->get()
|
2022-03-13 06:15:47 +00:00
|
|
|
->filter(function($profile) use ($banned) {
|
|
|
|
return in_array($profile->domain, $banned) == false;
|
|
|
|
})
|
2022-03-23 11:58:14 +00:00
|
|
|
->map(function($res) use($mastodonMode) {
|
|
|
|
return $mastodonMode ?
|
|
|
|
AccountService::getMastodon($res['id']) :
|
|
|
|
AccountService::get($res['id']);
|
2022-03-13 06:15:47 +00:00
|
|
|
})
|
|
|
|
->filter(function($account) {
|
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values();
|
2020-02-07 00:12:20 +00:00
|
|
|
|
2021-12-05 00:32:43 +00:00
|
|
|
return $results;
|
2020-02-07 00:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function hashtags()
|
|
|
|
{
|
2022-03-23 11:58:14 +00:00
|
|
|
$mastodonMode = self::$mastodonMode;
|
2020-05-16 03:05:41 +00:00
|
|
|
$limit = $this->query->input('limit') ?? 20;
|
|
|
|
$offset = $this->query->input('offset') ?? 0;
|
2020-02-07 00:12:20 +00:00
|
|
|
$query = '%' . $this->query->input('q') . '%';
|
2022-12-27 11:43:04 +00:00
|
|
|
return Hashtag::where('can_search', true)
|
2020-02-07 00:12:20 +00:00
|
|
|
->where('name', 'like', $query)
|
2020-05-16 03:05:41 +00:00
|
|
|
->offset($offset)
|
2020-02-07 00:12:20 +00:00
|
|
|
->limit($limit)
|
|
|
|
->get()
|
2022-03-23 11:58:14 +00:00
|
|
|
->map(function($tag) use($mastodonMode) {
|
|
|
|
$res = [
|
2020-02-07 00:12:20 +00:00
|
|
|
'name' => $tag->name,
|
2022-03-23 11:58:14 +00:00
|
|
|
'url' => $tag->url()
|
2020-02-07 00:12:20 +00:00
|
|
|
];
|
2022-03-23 11:58:14 +00:00
|
|
|
|
|
|
|
if(!$mastodonMode) {
|
|
|
|
$res['history'] = [];
|
|
|
|
$res['count'] = HashtagService::count($tag->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
2020-02-07 00:12:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function statuses()
|
|
|
|
{
|
2020-05-16 03:05:41 +00:00
|
|
|
// Removed until we provide more relevent sorting/results
|
|
|
|
return [];
|
2020-02-07 00:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function statusesById()
|
|
|
|
{
|
2022-03-23 11:58:14 +00:00
|
|
|
$mastodonMode = self::$mastodonMode;
|
2020-02-07 00:12:20 +00:00
|
|
|
$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)
|
2021-12-05 00:32:43 +00:00
|
|
|
->get()
|
2022-03-23 11:58:14 +00:00
|
|
|
->map(function($status) use($mastodonMode) {
|
|
|
|
return $mastodonMode ?
|
|
|
|
StatusService::getMastodon($status->id) :
|
|
|
|
StatusService::get($status->id);
|
2022-03-17 04:38:42 +00:00
|
|
|
})
|
|
|
|
->filter(function($status) {
|
|
|
|
return $status && isset($status['account']);
|
2021-12-05 00:32:43 +00:00
|
|
|
});
|
|
|
|
return $results;
|
2020-02-07 00:12:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 08:27:05 +00:00
|
|
|
protected function resolveQuery()
|
|
|
|
{
|
2022-08-28 12:28:46 +00:00
|
|
|
$default = [
|
|
|
|
'accounts' => [],
|
|
|
|
'hashtags' => [],
|
|
|
|
'statuses' => [],
|
|
|
|
];
|
2022-03-23 11:58:14 +00:00
|
|
|
$mastodonMode = self::$mastodonMode;
|
2020-02-07 08:27:05 +00:00
|
|
|
$query = urldecode($this->query->input('q'));
|
2022-08-28 12:28:46 +00:00
|
|
|
if(substr($query, 0, 1) === '@' && !Str::contains($query, '.')) {
|
|
|
|
$default['accounts'] = $this->accounts(substr($query, 1));
|
|
|
|
return $default;
|
|
|
|
}
|
2020-02-07 08:28:13 +00:00
|
|
|
if(Helpers::validateLocalUrl($query)) {
|
2020-02-07 08:27:05 +00:00
|
|
|
if(Str::contains($query, '/p/')) {
|
|
|
|
return $this->resolveLocalStatus();
|
|
|
|
} else {
|
|
|
|
return $this->resolveLocalProfile();
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-10 06:15:45 +00:00
|
|
|
if(!Helpers::validateUrl($query) && strpos($query, '@') == -1) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2022-09-01 03:37:43 +00:00
|
|
|
if(!Str::startsWith($query, 'http') && Str::substrCount($query, '@') == 1 && strpos($query, '@') !== 0) {
|
2022-08-28 12:28:46 +00:00
|
|
|
try {
|
|
|
|
$res = WebfingerService::lookup('@' . $query, $mastodonMode);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
if($res && isset($res['id'])) {
|
|
|
|
$default['accounts'][] = $res;
|
|
|
|
return $default;
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 06:15:45 +00:00
|
|
|
if(Str::substrCount($query, '@') == 2) {
|
|
|
|
try {
|
2022-03-23 11:58:14 +00:00
|
|
|
$res = WebfingerService::lookup($query, $mastodonMode);
|
2022-01-10 06:15:45 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
if($res && isset($res['id'])) {
|
|
|
|
$default['accounts'][] = $res;
|
|
|
|
return $default;
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$res = ActivityPubFetchService::get($query);
|
2022-03-17 04:38:42 +00:00
|
|
|
$banned = InstanceService::getBannedDomains();
|
2022-01-10 06:15:45 +00:00
|
|
|
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);
|
2022-03-17 04:38:42 +00:00
|
|
|
if(!$obj || !isset($obj['id'])) {
|
|
|
|
return $default;
|
|
|
|
}
|
2022-03-23 11:58:14 +00:00
|
|
|
$note = $mastodonMode ?
|
|
|
|
StatusService::getMastodon($obj['id']) :
|
|
|
|
StatusService::get($obj['id']);
|
2022-03-17 04:38:42 +00:00
|
|
|
if(!$note) {
|
2022-01-10 06:15:45 +00:00
|
|
|
return $default;
|
|
|
|
}
|
2022-03-17 04:38:42 +00:00
|
|
|
$default['statuses'][] = $note;
|
2022-01-10 06:15:45 +00:00
|
|
|
return $default;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Person':
|
|
|
|
$obj = Helpers::profileFetch($query);
|
|
|
|
if(!$obj) {
|
|
|
|
return $default;
|
|
|
|
}
|
2022-03-17 04:38:42 +00:00
|
|
|
if(in_array($obj['domain'], $banned)) {
|
|
|
|
return $default;
|
|
|
|
}
|
2022-03-23 11:58:14 +00:00
|
|
|
$default['accounts'][] = $mastodonMode ?
|
|
|
|
AccountService::getMastodon($obj['id']) :
|
|
|
|
AccountService::get($obj['id']);
|
2022-01-10 06:15:45 +00:00
|
|
|
return $default;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return [
|
|
|
|
'accounts' => [],
|
|
|
|
'hashtags' => [],
|
|
|
|
'statuses' => [],
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return [
|
|
|
|
'accounts' => [],
|
|
|
|
'hashtags' => [],
|
|
|
|
'statuses' => [],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
2020-02-07 08:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function resolveLocalStatus()
|
|
|
|
{
|
|
|
|
$query = urldecode($this->query->input('q'));
|
|
|
|
$query = last(explode('/', $query));
|
2022-03-23 11:58:14 +00:00
|
|
|
$status = StatusService::getMastodon($query);
|
2020-02-07 08:27:05 +00:00
|
|
|
if(!$status) {
|
|
|
|
return [
|
|
|
|
'accounts' => [],
|
|
|
|
'hashtags' => [],
|
|
|
|
'statuses' => []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-03-23 11:58:14 +00:00
|
|
|
$res = [
|
2020-02-07 08:27:05 +00:00
|
|
|
'accounts' => [],
|
|
|
|
'hashtags' => [],
|
2022-03-23 11:58:14 +00:00
|
|
|
'statuses' => [$status]
|
2020-02-07 08:27:05 +00:00
|
|
|
];
|
2022-03-23 11:58:14 +00:00
|
|
|
|
|
|
|
return $res;
|
2020-02-07 08:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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' => []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-12-05 00:32:43 +00:00
|
|
|
}
|