From 6f783cac66b4d3e8ef116dc643ff06f792aea75f Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 4 Jul 2019 20:21:20 -0600 Subject: [PATCH] Update DiscoverController --- app/Http/Controllers/DiscoverController.php | 72 ++++++--------------- 1 file changed, 21 insertions(+), 51 deletions(-) diff --git a/app/Http/Controllers/DiscoverController.php b/app/Http/Controllers/DiscoverController.php index bbc8d765c..0a4d67af9 100644 --- a/app/Http/Controllers/DiscoverController.php +++ b/app/Http/Controllers/DiscoverController.php @@ -17,6 +17,7 @@ use App\Transformer\Api\StatusStatelessTransformer; use League\Fractal; use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Pagination\IlluminatePaginatorAdapter; +use App\Services\StatusHashtagService; class DiscoverController extends Controller { @@ -37,56 +38,9 @@ class DiscoverController extends Controller public function showTags(Request $request, $hashtag) { abort_if(!Auth::check(), 403); - - $tag = Hashtag::whereSlug($hashtag) - ->firstOrFail(); - - $page = 1; - $key = 'discover:tag-'.$tag->id.':page-'.$page; - $keyMinutes = 15; - - $posts = Cache::remember($key, now()->addMinutes($keyMinutes), function() use ($tag, $request) { - $tags = StatusHashtag::select('status_id') - ->whereHashtagId($tag->id) - ->orderByDesc('id') - ->take(48) - ->pluck('status_id'); - - return Status::select( - 'id', - 'uri', - 'caption', - 'rendered', - 'profile_id', - 'type', - 'in_reply_to_id', - 'reblog_of_id', - 'is_nsfw', - 'scope', - 'local', - 'created_at', - 'updated_at' - )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album']) - ->with('media') - ->whereLocal(true) - ->whereNull('uri') - ->whereIn('id', $tags) - ->whereNull('in_reply_to_id') - ->whereNull('reblog_of_id') - ->whereNull('url') - ->whereNull('uri') - ->withCount(['likes', 'comments']) - ->whereIsNsfw(false) - ->whereVisibility('public') - ->orderBy('id', 'desc') - ->get(); - }); - - if($posts->count() == 0) { - abort(404); - } - - return view('discover.tags.show', compact('tag', 'posts')); + $tag = Hashtag::whereSlug($hashtag)->firstOrFail(); + $tagCount = StatusHashtagService::count($tag->id); + return view('discover.tags.show', compact('tag', 'tagCount')); } public function showCategory(Request $request, $slug) @@ -156,7 +110,6 @@ class DiscoverController extends Controller return $res; } - public function loopWatch(Request $request) { abort_if(!Auth::check(), 403); @@ -171,4 +124,21 @@ class DiscoverController extends Controller return response()->json(200); } + + public function getHashtags(Request $request) + { + abort_if(!Auth::check(), 403); + $this->validate($request, [ + 'hashtag' => 'required|alphanum|min:2|max:124', + 'page' => 'nullable|integer|min:1|max:19' + ]); + + $page = $request->input('page') ?? '1'; + $end = $page > 1 ? $page * 9 : 0; + $tag = $request->input('hashtag'); + + $hashtag = Hashtag::whereName($tag)->firstOrFail(); + $res = StatusHashtagService::get($hashtag->id, $page, $end); + return $res; + } }