DiscoverController.php 2.3 KB

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