DiscoverController.php 1.8 KB

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