DiscoverController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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::with('posts')
  30. ->withCount('posts')
  31. ->whereSlug($hashtag)
  32. ->firstOrFail();
  33. $page = $request->input('page') ?? 1;
  34. $key = 'discover:tag-'.$tag->id.':page-'.$page;
  35. $keyMinutes = $page > 1 ? 5 : 2;
  36. $posts = Cache::remember($key, now()->addMinutes($keyMinutes), function() use ($tag, $request) {
  37. return $tag->posts()
  38. ->whereNull('url')
  39. ->whereNull('uri')
  40. ->whereHas('media')
  41. ->withCount(['likes', 'comments'])
  42. ->whereIsNsfw(false)
  43. ->whereVisibility('public')
  44. ->orderBy('id', 'desc')
  45. ->simplePaginate(24);
  46. });
  47. if($posts->count() == 0) {
  48. abort(404);
  49. }
  50. return view('discover.tags.show', compact('tag', 'posts'));
  51. }
  52. public function showCategory(Request $request, $slug)
  53. {
  54. $tag = DiscoverCategory::whereActive(true)
  55. ->whereSlug($slug)
  56. ->firstOrFail();
  57. $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
  58. $tagids = $tag->hashtags->pluck('id')->toArray();
  59. $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
  60. $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();
  61. return $posts;
  62. });
  63. $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
  64. return $tag->posts()->whereScope('public')->count();
  65. });
  66. return view('discover.tags.category', compact('tag', 'posts'));
  67. }
  68. public function showPersonal(Request $request)
  69. {
  70. $profile = Auth::user()->profile;
  71. $tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){
  72. return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get();
  73. });
  74. $following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) {
  75. $res = Follower::whereProfileId($profile->id)->pluck('following_id');
  76. return $res->push($profile->id)->toArray();
  77. });
  78. $posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) {
  79. $posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get();
  80. $posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
  81. return $posts;
  82. });
  83. return view('discover.personal', compact('posts', 'tags'));
  84. }
  85. }