1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Http/Controllers/SearchController.php

271 lines
9.6 KiB
PHP
Raw Normal View History

2018-05-26 22:50:36 +00:00
<?php
namespace App\Http\Controllers;
2018-09-27 05:00:45 +00:00
use Auth;
2018-08-28 03:07:36 +00:00
use App\Hashtag;
use App\Profile;
2018-09-27 05:00:45 +00:00
use App\Status;
2018-05-26 22:50:36 +00:00
use Illuminate\Http\Request;
2019-02-07 19:01:31 +00:00
use App\Util\ActivityPub\Helpers;
2018-05-26 22:50:36 +00:00
use Illuminate\Support\Facades\Cache;
2019-04-18 02:36:14 +00:00
use Illuminate\Support\Str;
2019-04-03 04:55:56 +00:00
use App\Transformer\Api\{
AccountTransformer,
HashtagTransformer,
StatusTransformer,
};
use App\Services\WebfingerService;
2018-05-26 22:50:36 +00:00
class SearchController extends Controller
{
public $tokens = [];
public $term = '';
public $hash = '';
public $cacheKey = 'api:search:tag:';
2018-09-27 05:00:45 +00:00
public function __construct()
{
$this->middleware('auth');
}
2019-04-18 02:36:14 +00:00
public function searchAPI(Request $request)
2018-05-26 22:50:36 +00:00
{
2019-04-18 02:36:14 +00:00
$this->validate($request, [
'q' => 'required|string|min:3|max:120',
'src' => 'required|string|in:metro',
'v' => 'required|integer|in:1',
'scope' => 'required|in:all,hashtag,profile,remote,webfinger'
2019-04-18 02:36:14 +00:00
]);
2019-04-03 04:55:56 +00:00
$scope = $request->input('scope') ?? 'all';
$this->term = e(urldecode($request->input('q')));
$this->hash = hash('sha256', $this->term);
switch ($scope) {
case 'all':
$this->getHashtags();
$this->getPosts();
$this->getProfiles();
break;
case 'hashtag':
$this->getHashtags();
break;
case 'profile':
$this->getProfiles();
break;
case 'webfinger':
$this->webfingerSearch();
break;
default:
break;
}
return response()->json($this->tokens, 200, [], JSON_PRETTY_PRINT);
}
protected function getPosts()
{
$tag = $this->term;
2018-09-27 05:00:45 +00:00
$hash = hash('sha256', $tag);
if( Helpers::validateUrl($tag) != false &&
Helpers::validateLocalUrl($tag) != true &&
config('federation.activitypub.enabled') == true &&
config('federation.activitypub.remoteFollow') == true
) {
$remote = Helpers::fetchFromUrl($tag);
if( isset($remote['type']) &&
$remote['type'] == 'Note') {
$item = Helpers::statusFetch($tag);
$this->tokens['posts'] = [[
'count' => 0,
'url' => $item->url(),
'type' => 'status',
'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
'tokens' => [$item->caption],
'name' => $item->caption,
'thumb' => $item->thumb(),
]];
2019-02-07 19:01:31 +00:00
}
} else {
$posts = Status::select('id', 'profile_id', 'caption', 'created_at')
->whereHas('media')
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereProfileId(Auth::user()->profile_id)
->where('caption', 'like', '%'.$tag.'%')
->latest()
->limit(10)
->get();
if($posts->count() > 0) {
$posts = $posts->map(function($item, $key) {
return [
'count' => 0,
'url' => $item->url(),
'type' => 'status',
'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
'tokens' => [$item->caption],
'name' => $item->caption,
'thumb' => $item->thumb(),
'filter' => $item->firstMedia()->filter_class
];
});
$this->tokens['posts'] = $posts;
}
}
}
protected function getHashtags()
{
$tag = $this->term;
$key = $this->cacheKey . 'hashtags:' . $this->hash;
$ttl = now()->addMinutes(1);
$tokens = Cache::remember($key, $ttl, function() use($tag) {
2019-04-18 02:36:14 +00:00
$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();
2018-09-27 05:00:45 +00:00
if($hashtags->count() > 0) {
$tags = $hashtags->map(function ($item, $key) {
return [
'count' => $item->posts()->count(),
'url' => $item->url(),
'type' => 'hashtag',
'value' => $item->name,
2019-04-03 04:55:56 +00:00
'tokens' => '',
2018-09-27 05:00:45 +00:00
'name' => null,
];
});
return $tags;
2018-09-27 05:00:45 +00:00
}
2018-05-26 22:50:36 +00:00
});
$this->tokens['hashtags'] = $tokens;
}
protected function getProfiles()
{
$tag = $this->term;
$remoteKey = $this->cacheKey . 'profiles:remote:' . $this->hash;
$key = $this->cacheKey . 'profiles:' . $this->hash;
$remoteTtl = now()->addMinutes(15);
$ttl = now()->addHours(2);
if( Helpers::validateUrl($tag) != false &&
Helpers::validateLocalUrl($tag) != true &&
config('federation.activitypub.enabled') == true &&
config('federation.activitypub.remoteFollow') == true
) {
$remote = Helpers::fetchFromUrl($tag);
if( isset($remote['type']) &&
$remote['type'] == 'Person'
) {
$this->tokens['profiles'] = Cache::remember($remoteKey, $remoteTtl, function() use($tag) {
$item = Helpers::profileFirstOrNew($tag);
$tokens = [[
'count' => 1,
'url' => $item->url(),
'type' => 'profile',
'value' => $item->username,
'tokens' => [$item->username],
'name' => $item->name,
'entity' => [
'id' => (string) $item->id,
'following' => $item->followedBy(Auth::user()->profile),
'follow_request' => $item->hasFollowRequestById(Auth::user()->profile_id),
'thumb' => $item->avatarUrl(),
'local' => (bool) !$item->domain
]
]];
return $tokens;
});
2019-04-03 04:55:56 +00:00
}
}
// elseif( Str::containsAll($tag, ['@', '.'])
// config('federation.activitypub.enabled') == true &&
// config('federation.activitypub.remoteFollow') == true
// ) {
// if(substr_count($tag, '@') == 2) {
// $domain = last(explode('@', sub_str($u, 1)));
// } else {
// $domain = last(explode('@', $u));
// }
// }
else {
$this->tokens['profiles'] = Cache::remember($key, $ttl, function() use($tag) {
$users = Profile::select('domain', 'username', 'name', 'id')
->whereNull('status')
->where('id', '!=', Auth::user()->profile->id)
->where('username', 'like', '%'.$tag.'%')
->limit(20)
2018-09-27 05:00:45 +00:00
->get();
2018-05-26 22:50:36 +00:00
if($users->count() > 0) {
return $users->map(function ($item, $key) {
return [
'count' => 0,
'url' => $item->url(),
'type' => 'profile',
'value' => $item->username,
'tokens' => [$item->username],
'name' => $item->name,
'avatar' => $item->avatarUrl(),
'id' => (string) $item->id,
'entity' => [
'id' => (string) $item->id,
'following' => $item->followedBy(Auth::user()->profile),
'follow_request' => $item->hasFollowRequestById(Auth::user()->profile_id),
'thumb' => $item->avatarUrl(),
'local' => (bool) !$item->domain,
'post_count' => $item->statuses()->count()
]
];
});
}
2018-09-27 05:00:45 +00:00
});
}
2018-05-26 22:50:36 +00:00
}
2019-02-22 20:50:52 +00:00
public function results(Request $request)
{
$this->validate($request, [
'q' => 'required|string|min:1',
]);
return view('search.results');
}
2019-08-29 01:52:05 +00:00
protected function webfingerSearch()
{
$wfs = WebfingerService::lookup($this->term);
if(empty($wfs)) {
return;
}
$this->tokens['profiles'] = [
[
'count' => 1,
'url' => $wfs['url'],
'type' => 'profile',
'value' => $wfs['username'],
'tokens' => [$wfs['username']],
'name' => $wfs['display_name'],
'entity' => [
'id' => (string) $wfs['id'],
'following' => null,
'follow_request' => null,
'thumb' => $wfs['avatar'],
'local' => (bool) $wfs['local']
]
]
];
return;
}
}