Update SearchController

This commit is contained in:
Daniel Supernault 2019-04-02 22:55:56 -06:00
parent 6d0a908329
commit 1284812f6f
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -9,6 +9,11 @@ use App\Status;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Util\ActivityPub\Helpers; use App\Util\ActivityPub\Helpers;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use App\Transformer\Api\{
AccountTransformer,
HashtagTransformer,
StatusTransformer,
};
class SearchController extends Controller class SearchController extends Controller
{ {
@ -22,26 +27,33 @@ class SearchController extends Controller
if(mb_strlen($tag) < 3) { if(mb_strlen($tag) < 3) {
return; return;
} }
$tag = e(urldecode($tag));
$hash = hash('sha256', $tag); $hash = hash('sha256', $tag);
$tokens = Cache::remember('api:search:tag:'.$hash, now()->addMinutes(5), function () use ($tag) { $tokens = Cache::remember('api:search:tag:'.$hash, now()->addMinutes(5), function () use ($tag) {
$tokens = collect([]); $tokens = [];
if(Helpers::validateUrl($tag)) { if(Helpers::validateUrl($tag) != false) {
$remote = Helpers::fetchFromUrl($tag); $remote = Helpers::fetchFromUrl($tag);
if(isset($remote['type']) && in_array($remote['type'], ['Create', 'Person']) == true) { if(isset($remote['type']) && in_array($remote['type'], ['Create', 'Person']) == true) {
$type = $remote['type']; $type = $remote['type'];
if($type == 'Person') { if($type == 'Person') {
$item = Helpers::profileFirstOrNew($tag); $item = Helpers::profileFirstOrNew($tag);
$tokens->push([[ $tokens['profiles'] = [[
'count' => 1, 'count' => 1,
'url' => $item->url(), 'url' => $item->url(),
'type' => 'profile', 'type' => 'profile',
'value' => $item->username, 'value' => $item->username,
'tokens' => [$item->username], 'tokens' => [$item->username],
'name' => $item->name, 'name' => $item->name,
]]); 'entity' => [
'id' => $item->id,
'following' => $item->followedBy(Auth::user()->profile),
'thumb' => $item->avatarUrl()
]
]];
} else if ($type == 'Create') { } else if ($type == 'Create') {
$item = Helpers::statusFirstOrFetch($tag, false); $item = Helpers::statusFirstOrFetch($tag, false);
$tokens->push([[ $tokens['posts'] = [[
'count' => 0, 'count' => 0,
'url' => $item->url(), 'url' => $item->url(),
'type' => 'status', 'type' => 'status',
@ -49,10 +61,9 @@ class SearchController extends Controller
'tokens' => [$item->caption], 'tokens' => [$item->caption],
'name' => $item->caption, 'name' => $item->caption,
'thumb' => $item->thumb(), 'thumb' => $item->thumb(),
]]); ]];
} }
} }
} }
$hashtags = Hashtag::select('id', 'name', 'slug')->where('slug', 'like', '%'.$tag.'%')->whereHas('posts')->limit(20)->get(); $hashtags = Hashtag::select('id', 'name', 'slug')->where('slug', 'like', '%'.$tag.'%')->whereHas('posts')->limit(20)->get();
if($hashtags->count() > 0) { if($hashtags->count() > 0) {
@ -62,37 +73,46 @@ class SearchController extends Controller
'url' => $item->url(), 'url' => $item->url(),
'type' => 'hashtag', 'type' => 'hashtag',
'value' => $item->name, 'value' => $item->name,
'tokens' => explode('-', $item->name), 'tokens' => '',
'name' => null, 'name' => null,
]; ];
}); });
$tokens->push($tags); $tokens['hashtags'] = $tags;
} }
$users = Profile::select('username', 'name', 'id')
->whereNull('status')
->whereNull('domain')
->where('username', 'like', '%'.$tag.'%')
//->orWhere('remote_url', $tag)
->limit(20)
->get();
if($users->count() > 0) {
$profiles = $users->map(function ($item, $key) {
return [
'count' => 0,
'url' => $item->url(),
'type' => 'profile',
'value' => $item->username,
'tokens' => [$item->username],
'name' => $item->name,
'id' => $item->id
];
});
$tokens->push($profiles);
}
return $tokens; return $tokens;
}); });
$users = Profile::select('username', 'name', 'id')
->whereNull('status')
->where('id', '!=', Auth::user()->profile->id)
->where('username', 'like', '%'.$tag.'%')
->orWhere('remote_url', $tag)
->limit(20)
->get();
if($users->count() > 0) {
$profiles = $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' => $item->id,
'entity' => [
'id' => $item->id,
'following' => $item->followedBy(Auth::user()->profile),
'thumb' => $item->avatarUrl()
]
];
});
if(isset($tokens['profiles'])) {
array_push($tokens['profiles'], $profiles);
} else {
$tokens['profiles'] = $profiles;
}
}
$posts = Status::select('id', 'profile_id', 'caption', 'created_at') $posts = Status::select('id', 'profile_id', 'caption', 'created_at')
->whereHas('media') ->whereHas('media')
->whereNull('in_reply_to_id') ->whereNull('in_reply_to_id')
@ -100,7 +120,8 @@ class SearchController extends Controller
->whereProfileId(Auth::user()->profile->id) ->whereProfileId(Auth::user()->profile->id)
->where('caption', 'like', '%'.$tag.'%') ->where('caption', 'like', '%'.$tag.'%')
->orWhere('uri', $tag) ->orWhere('uri', $tag)
->orderBy('created_at', 'desc') ->latest()
->limit(10)
->get(); ->get();
if($posts->count() > 0) { if($posts->count() > 0) {
@ -115,11 +136,9 @@ class SearchController extends Controller
'thumb' => $item->thumb(), 'thumb' => $item->thumb(),
]; ];
}); });
$tokens = $tokens->push($posts); $tokens['posts'] = $posts;
}
if($tokens->count() > 0) {
$tokens = $tokens[0];
} }
return response()->json($tokens); return response()->json($tokens);
} }