DiscoverController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $tag = Hashtag::whereSlug($hashtag)
  27. ->firstOrFail();
  28. $page = 1;
  29. $key = 'discover:tag-'.$tag->id.':page-'.$page;
  30. $keyMinutes = 15;
  31. $posts = Cache::remember($key, now()->addMinutes($keyMinutes), function() use ($tag, $request) {
  32. $tags = StatusHashtag::select('status_id')
  33. ->whereHashtagId($tag->id)
  34. ->orderByDesc('id')
  35. ->take(48)
  36. ->pluck('status_id');
  37. return Status::select(
  38. 'id',
  39. 'uri',
  40. 'caption',
  41. 'rendered',
  42. 'profile_id',
  43. 'type',
  44. 'in_reply_to_id',
  45. 'reblog_of_id',
  46. 'is_nsfw',
  47. 'scope',
  48. 'local',
  49. 'created_at',
  50. 'updated_at'
  51. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  52. ->with('media')
  53. ->whereLocal(true)
  54. ->whereNull('uri')
  55. ->whereIn('id', $tags)
  56. ->whereNull('in_reply_to_id')
  57. ->whereNull('reblog_of_id')
  58. ->whereNull('url')
  59. ->whereNull('uri')
  60. ->withCount(['likes', 'comments'])
  61. ->whereIsNsfw(false)
  62. ->whereVisibility('public')
  63. ->orderBy('id', 'desc')
  64. ->get();
  65. });
  66. if($posts->count() == 0) {
  67. abort(404);
  68. }
  69. return view('discover.tags.show', compact('tag', 'posts'));
  70. }
  71. public function showCategory(Request $request, $slug)
  72. {
  73. $tag = DiscoverCategory::whereActive(true)
  74. ->whereSlug($slug)
  75. ->firstOrFail();
  76. $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
  77. $tagids = $tag->hashtags->pluck('id')->toArray();
  78. $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
  79. $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();
  80. return $posts;
  81. });
  82. $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
  83. return $tag->posts()->whereScope('public')->count();
  84. });
  85. return view('discover.tags.category', compact('tag', 'posts'));
  86. }
  87. public function showPersonal(Request $request)
  88. {
  89. $profile = Auth::user()->profile;
  90. $tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){
  91. return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get();
  92. });
  93. $following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) {
  94. $res = Follower::whereProfileId($profile->id)->pluck('following_id');
  95. return $res->push($profile->id)->toArray();
  96. });
  97. $posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) {
  98. $posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get();
  99. $posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
  100. return $posts;
  101. });
  102. return view('discover.personal', compact('posts', 'tags'));
  103. }
  104. public function showLoops(Request $request)
  105. {
  106. if(config('exp.loops') != true) {
  107. return redirect('/');
  108. }
  109. return view('discover.loops.home');
  110. }
  111. }