diff --git a/app/Http/Controllers/DiscoverController.php b/app/Http/Controllers/DiscoverController.php index 0eddeed96..ad598d5c7 100644 --- a/app/Http/Controllers/DiscoverController.php +++ b/app/Http/Controllers/DiscoverController.php @@ -60,23 +60,34 @@ class DiscoverController extends Controller ->whereSlug($slug) ->firstOrFail(); - // todo refactor this mess - $tagids = $tag->hashtags->pluck('id')->toArray(); - $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray(); - $posts = Status::whereIn('id', $sids)->whereNull('uri')->whereType('photo')->whereNull('in_reply_to_id')->whereNull('reblog_of_id')->orderByDesc('created_at')->paginate(21); - $tag->posts_count = $tag->posts()->count(); + $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) { + $tagids = $tag->hashtags->pluck('id')->toArray(); + $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray(); + $posts = Status::whereScope('public')->whereIn('id', $sids)->whereNull('uri')->whereType('photo')->whereNull('in_reply_to_id')->whereNull('reblog_of_id')->orderByDesc('created_at')->take(39)->get(); + return $posts; + }); + $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) { + return $tag->posts()->whereScope('public')->count(); + }); return view('discover.tags.category', compact('tag', 'posts')); } public function showPersonal(Request $request) { $profile = Auth::user()->profile; - // todo refactor this mess - $tags = Hashtag::whereHas('posts')->orderByRaw('rand()')->take(5)->get(); - $following = $profile->following->pluck('id'); - $following = $following->push($profile->id)->toArray(); - $posts = Status::withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->paginate(21); - $posts->post_count = Status::whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count(); + + $tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){ + return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get(); + }); + $following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) { + $res = Follower::whereProfileId($profile->id)->pluck('following_id'); + return $res->push($profile->id)->toArray(); + }); + $posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) { + $posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get(); + $posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count(); + return $posts; + }); return view('discover.personal', compact('posts', 'tags')); } } diff --git a/app/Profile.php b/app/Profile.php index 0eb8255f2..86c83bf5e 100644 --- a/app/Profile.php +++ b/app/Profile.php @@ -279,4 +279,16 @@ class Profile extends Model { return $this->hasMany(Circle::class); } + + public function hashtags() + { + return $this->hasManyThrough( + Hashtag::class, + StatusHashtag::class, + 'profile_id', + 'id', + 'id', + 'hashtag_id' + ); + } } diff --git a/resources/views/discover/personal.blade.php b/resources/views/discover/personal.blade.php index cbda555b9..2e8a63a18 100644 --- a/resources/views/discover/personal.blade.php +++ b/resources/views/discover/personal.blade.php @@ -18,10 +18,12 @@ background: linear-gradient(to right, #FFC371, #FF5F6D); /* W3C, IE 10+/ Edge, F

+ @if($tags->count() > 0) Related hashtags: @foreach($tags as $hashtag) #{{$hashtag->name}} @endforeach + @endif

{{$posts->post_count}} posts @@ -50,27 +52,6 @@ background: linear-gradient(to right, #FFC371, #FF5F6D); /* W3C, IE 10+/ Edge, F @endforeach

-
- {{$posts->links()}} -
-@endsection - -@push('scripts') - -@endpush \ No newline at end of file +@endsection \ No newline at end of file diff --git a/resources/views/discover/tags/category.blade.php b/resources/views/discover/tags/category.blade.php index d918f53a4..ea73b43c7 100644 --- a/resources/views/discover/tags/category.blade.php +++ b/resources/views/discover/tags/category.blade.php @@ -2,7 +2,7 @@ @section('content') -
+
@@ -24,10 +24,12 @@

+ @if($tag->hashtags->count() > 0) Related hashtags: @foreach($tag->hashtags as $hashtag) #{{$hashtag->name}} @endforeach + @endif

{{$tag->posts_count}} posts @@ -56,9 +58,6 @@ @endforeach

-
- {{$posts->links()}} -
@endsection @@ -84,21 +83,3 @@ } @endpush - -@push('scripts') - -@endpush diff --git a/resources/views/discover/tags/show.blade.php b/resources/views/discover/tags/show.blade.php index a710ae365..4ea1fc24b 100644 --- a/resources/views/discover/tags/show.blade.php +++ b/resources/views/discover/tags/show.blade.php @@ -44,31 +44,7 @@ @endforeach
-
- {{$posts->links()}} -
@endsection -@push('meta') - -@endpush - -@push('scripts') - -@endpush diff --git a/routes/web.php b/routes/web.php index 3dc7bce9f..9ad2d8827 100644 --- a/routes/web.php +++ b/routes/web.php @@ -59,8 +59,10 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::get('/home', 'HomeController@index')->name('home'); + Route::get('discover/c/{slug}', 'DiscoverController@showCategory'); + Route::get('discover/personal', 'DiscoverController@showPersonal'); Route::get('discover', 'DiscoverController@home')->name('discover'); - + Route::group(['prefix' => 'api'], function () { Route::get('search/{tag}', 'SearchController@searchAPI') //->where('tag', '.*'); @@ -91,6 +93,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::get('shares/profile/{username}/status/{id}', 'PublicApiController@statusShares'); Route::get('status/{id}/replies', 'InternalApiController@statusReplies'); Route::post('moderator/action', 'InternalApiController@modAction'); + Route::get('discover/categories', 'InternalApiController@discoverCategories'); }); Route::group(['prefix' => 'local'], function () { Route::get('i/follow-suggestions', 'ApiController@followSuggestions');