DiscoverController.php 2.7 KB

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