DiscoverController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. DiscoverCategory,
  5. Follower,
  6. Hashtag,
  7. Profile,
  8. Status,
  9. StatusHashtag,
  10. UserFilter
  11. };
  12. use Auth, DB, Cache;
  13. use Illuminate\Http\Request;
  14. class DiscoverController extends Controller
  15. {
  16. public function __construct()
  17. {
  18. $this->middleware('auth');
  19. }
  20. public function home(Request $request)
  21. {
  22. return view('discover.home');
  23. }
  24. public function showTags(Request $request, $hashtag)
  25. {
  26. $this->validate($request, [
  27. 'page' => 'nullable|integer|min:1|max:10',
  28. ]);
  29. $tag = Hashtag::with('posts')
  30. ->withCount('posts')
  31. ->whereSlug($hashtag)
  32. ->firstOrFail();
  33. $posts = $tag->posts()
  34. ->whereNull('url')
  35. ->whereNull('uri')
  36. ->whereHas('media')
  37. ->withCount(['likes', 'comments'])
  38. ->whereIsNsfw(false)
  39. ->whereVisibility('public')
  40. ->orderBy('id', 'desc')
  41. ->simplePaginate(12);
  42. if($posts->count() == 0) {
  43. abort(404);
  44. }
  45. return view('discover.tags.show', compact('tag', 'posts'));
  46. }
  47. public function showCategory(Request $request, $slug)
  48. {
  49. $tag = DiscoverCategory::whereActive(true)
  50. ->whereSlug($slug)
  51. ->firstOrFail();
  52. $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
  53. $tagids = $tag->hashtags->pluck('id')->toArray();
  54. $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
  55. $posts = Status::whereScope('public')->whereIn('id', $sids)->whereNull('uri')->whereType('photo')->whereNull('in_reply_to_id')->whereNull('reblog_of_id')->orderByDesc('created_at')->take(39)->get();
  56. return $posts;
  57. });
  58. $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
  59. return $tag->posts()->whereScope('public')->count();
  60. });
  61. return view('discover.tags.category', compact('tag', 'posts'));
  62. }
  63. public function showPersonal(Request $request)
  64. {
  65. $profile = Auth::user()->profile;
  66. $tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){
  67. return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get();
  68. });
  69. $following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) {
  70. $res = Follower::whereProfileId($profile->id)->pluck('following_id');
  71. return $res->push($profile->id)->toArray();
  72. });
  73. $posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) {
  74. $posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get();
  75. $posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
  76. return $posts;
  77. });
  78. return view('discover.personal', compact('posts', 'tags'));
  79. }
  80. }