DiscoverController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. ->take(3)
  32. ->get();
  33. $posts = Status::whereHas('media')
  34. ->where('profile_id', '!=', $pid)
  35. ->whereNotIn('profile_id', $following)
  36. ->orderBy('created_at', 'desc')
  37. ->simplePaginate(21);
  38. return view('discover.home', compact('people', 'posts'));
  39. }
  40. public function showTags(Request $request, $hashtag)
  41. {
  42. $this->validate($request, [
  43. 'page' => 'nullable|integer|min:1|max:10',
  44. ]);
  45. $tag = Hashtag::with('posts')
  46. ->withCount('posts')
  47. ->whereSlug($hashtag)
  48. ->firstOrFail();
  49. $posts = $tag->posts()
  50. ->whereIsNsfw(false)
  51. ->whereVisibility('public')
  52. ->has('media')
  53. ->orderBy('id', 'desc')
  54. ->simplePaginate(12);
  55. return view('discover.tags.show', compact('tag', 'posts'));
  56. }
  57. }