Update SearchController

This commit is contained in:
Daniel Supernault 2018-09-26 23:00:45 -06:00
parent 55362f3b59
commit 958b5b402f
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -2,43 +2,86 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Auth;
use App\Hashtag; use App\Hashtag;
use App\Profile; use App\Profile;
use App\Status;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
class SearchController extends Controller class SearchController extends Controller
{ {
public function __construct()
{
$this->middleware('auth');
}
public function searchAPI(Request $request, $tag) public function searchAPI(Request $request, $tag)
{ {
$res = Cache::remember('api:search:tag:'.$tag, 1440, function () use ($tag) { if(mb_strlen($tag) < 3) {
$res = Hashtag::where('slug', 'like', '%'.$tag.'%')->get(); return;
$tags = $res->map(function ($item, $key) { }
return [ $hash = hash('sha256', $tag);
'count' => $item->posts()->count(), $tokens = Cache::remember('api:search:tag:'.$hash, 60, function () use ($tag) {
'url' => $item->url(), $tokens = collect([]);
'type' => 'hashtag', $hashtags = Hashtag::select('id', 'name', 'slug')->where('slug', 'like', '%'.$tag.'%')->limit(20)->get();
'value' => $item->name, if($hashtags->count() > 0) {
'tokens' => explode('-', $item->name), $tags = $hashtags->map(function ($item, $key) {
'name' => null, return [
]; 'count' => $item->posts()->count(),
}); 'url' => $item->url(),
$res = Profile::where('username', 'like', '%'.$tag.'%')->get(); 'type' => 'hashtag',
$profiles = $res->map(function ($item, $key) { 'value' => $item->name,
return [ 'tokens' => explode('-', $item->name),
'count' => 0, 'name' => null,
'url' => $item->url(), ];
'type' => 'profile', });
'value' => $item->username, $tokens->push($tags);
'tokens' => [$item->username], }
'name' => $item->name, $users = Profile::select('username', 'name', 'id')->where('username', 'like', '%'.$tag.'%')->limit(20)->get();
];
});
$tags = $tags->push($profiles[0]);
return $tags; 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,
];
});
$tokens->push($profiles);
}
return $tokens;
}); });
$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.'%')
->orderBy('created_at', 'desc')
->get();
return response()->json($res); if($posts->count() > 0) {
$posts = $posts->map(function($item, $key) {
return [
'count' => 0,
'url' => $item->url(),
'type' => 'status',
'value' => 'Posted '.$item->created_at->diffForHumans(),
'tokens' => [$item->caption],
'name' => $item->caption,
'thumb' => $item->thumb(),
];
});
$tokens = $tokens->push($posts);
}
if($tokens->count() > 0) {
$tokens = $tokens[0];
}
return response()->json($tokens);
} }
} }