DiscoverController.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. DiscoverCategory,
  5. Follower,
  6. Hashtag,
  7. Profile,
  8. Status,
  9. StatusHashtag,
  10. UserFilter
  11. };
  12. use Auth, DB, Cache;
  13. use Illuminate\Http\Request;
  14. class DiscoverController extends Controller
  15. {
  16. public function __construct()
  17. {
  18. $this->middleware('auth');
  19. }
  20. public function home(Request $request)
  21. {
  22. return view('discover.home');
  23. }
  24. public function showTags(Request $request, $hashtag)
  25. {
  26. $this->validate($request, [
  27. 'page' => 'nullable|integer|min:1|max:20',
  28. ]);
  29. $tag = Hashtag::whereSlug($hashtag)
  30. ->firstOrFail();
  31. $page = $request->input('page') ?? 1;
  32. $key = 'discover:tag-'.$tag->id.':page-'.$page;
  33. $keyMinutes = $page > 1 ? 5 : 2;
  34. $posts = Cache::remember($key, now()->addMinutes($keyMinutes), function() use ($tag, $request) {
  35. return $tag->posts()
  36. ->whereNull('url')
  37. ->whereNull('uri')
  38. ->whereHas('media')
  39. ->withCount(['likes', 'comments'])
  40. ->whereIsNsfw(false)
  41. ->whereVisibility('public')
  42. ->orderBy('id', 'desc')
  43. ->simplePaginate(24);
  44. });
  45. if($posts->count() == 0) {
  46. abort(404);
  47. }
  48. return view('discover.tags.show', compact('tag', 'posts'));
  49. }
  50. public function showCategory(Request $request, $slug)
  51. {
  52. $tag = DiscoverCategory::whereActive(true)
  53. ->whereSlug($slug)
  54. ->firstOrFail();
  55. $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
  56. $tagids = $tag->hashtags->pluck('id')->toArray();
  57. $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
  58. $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();
  59. return $posts;
  60. });
  61. $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
  62. return $tag->posts()->whereScope('public')->count();
  63. });
  64. return view('discover.tags.category', compact('tag', 'posts'));
  65. }
  66. public function showPersonal(Request $request)
  67. {
  68. $profile = Auth::user()->profile;
  69. $tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){
  70. return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get();
  71. });
  72. $following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) {
  73. $res = Follower::whereProfileId($profile->id)->pluck('following_id');
  74. return $res->push($profile->id)->toArray();
  75. });
  76. $posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) {
  77. $posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get();
  78. $posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
  79. return $posts;
  80. });
  81. return view('discover.personal', compact('posts', 'tags'));
  82. }
  83. }