DiscoverController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. Follower,
  5. Hashtag,
  6. Profile,
  7. Status,
  8. UserFilter
  9. };
  10. use Auth, DB, Cache;
  11. use Illuminate\Http\Request;
  12. class DiscoverController extends Controller
  13. {
  14. public function __construct()
  15. {
  16. $this->middleware('auth');
  17. }
  18. public function home(Request $request)
  19. {
  20. $this->validate($request, [
  21. 'page' => 'nullable|integer|max:50'
  22. ]);
  23. $pid = Auth::user()->profile->id;
  24. $following = Cache::remember('feature:discover:following:'.$pid, 15, function() use($pid) {
  25. $following = Follower::whereProfileId($pid)
  26. ->pluck('following_id');
  27. $filtered = UserFilter::whereUserId($pid)
  28. ->whereFilterableType('App\Profile')
  29. ->whereIn('filter_type', ['mute', 'block'])
  30. ->pluck('filterable_id');
  31. $following->push($pid);
  32. if($filtered->count() > 0) {
  33. $following->push($filtered);
  34. }
  35. return $following;
  36. });
  37. $people = Cache::remember('feature:discover:people:'.$pid, 15, function() use($following) {
  38. return Profile::inRandomOrder()
  39. ->whereHas('statuses')
  40. ->whereNull('domain')
  41. ->whereNotIn('id', $following)
  42. ->whereIsPrivate(false)
  43. ->take(3)
  44. ->get();
  45. });
  46. $posts = Status::whereHas('media')
  47. ->whereHas('profile', function($q) {
  48. $q->where('is_private', false);
  49. })
  50. ->whereIsNsfw(false)
  51. ->whereVisibility('public')
  52. ->where('profile_id', '!=', $pid)
  53. ->whereNotIn('profile_id', $following)
  54. ->withCount(['comments', 'likes'])
  55. ->orderBy('created_at', 'desc')
  56. ->simplePaginate(21);
  57. return view('discover.home', compact('people', 'posts'));
  58. }
  59. public function showTags(Request $request, $hashtag)
  60. {
  61. $this->validate($request, [
  62. 'page' => 'nullable|integer|min:1|max:10',
  63. ]);
  64. $tag = Hashtag::with('posts')
  65. ->withCount('posts')
  66. ->whereSlug($hashtag)
  67. ->firstOrFail();
  68. $posts = $tag->posts()
  69. ->whereIsNsfw(false)
  70. ->whereVisibility('public')
  71. ->has('media')
  72. ->orderBy('id', 'desc')
  73. ->simplePaginate(12);
  74. return view('discover.tags.show', compact('tag', 'posts'));
  75. }
  76. }