DiscoverController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. use App\Transformer\Api\StatusStatelessTransformer;
  15. use League\Fractal;
  16. use League\Fractal\Serializer\ArraySerializer;
  17. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  18. class DiscoverController extends Controller
  19. {
  20. protected $fractal;
  21. public function __construct()
  22. {
  23. $this->fractal = new Fractal\Manager();
  24. $this->fractal->setSerializer(new ArraySerializer());
  25. }
  26. public function home(Request $request)
  27. {
  28. abort_if(!Auth::check(), 403);
  29. return view('discover.home');
  30. }
  31. public function showTags(Request $request, $hashtag)
  32. {
  33. abort_if(!Auth::check(), 403);
  34. $tag = Hashtag::whereSlug($hashtag)
  35. ->firstOrFail();
  36. $page = 1;
  37. $key = 'discover:tag-'.$tag->id.':page-'.$page;
  38. $keyMinutes = 15;
  39. $posts = Cache::remember($key, now()->addMinutes($keyMinutes), function() use ($tag, $request) {
  40. $tags = StatusHashtag::select('status_id')
  41. ->whereHashtagId($tag->id)
  42. ->orderByDesc('id')
  43. ->take(48)
  44. ->pluck('status_id');
  45. return Status::select(
  46. 'id',
  47. 'uri',
  48. 'caption',
  49. 'rendered',
  50. 'profile_id',
  51. 'type',
  52. 'in_reply_to_id',
  53. 'reblog_of_id',
  54. 'is_nsfw',
  55. 'scope',
  56. 'local',
  57. 'created_at',
  58. 'updated_at'
  59. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  60. ->with('media')
  61. ->whereLocal(true)
  62. ->whereNull('uri')
  63. ->whereIn('id', $tags)
  64. ->whereNull('in_reply_to_id')
  65. ->whereNull('reblog_of_id')
  66. ->whereNull('url')
  67. ->whereNull('uri')
  68. ->withCount(['likes', 'comments'])
  69. ->whereIsNsfw(false)
  70. ->whereVisibility('public')
  71. ->orderBy('id', 'desc')
  72. ->get();
  73. });
  74. if($posts->count() == 0) {
  75. abort(404);
  76. }
  77. return view('discover.tags.show', compact('tag', 'posts'));
  78. }
  79. public function showCategory(Request $request, $slug)
  80. {
  81. abort_if(!Auth::check(), 403);
  82. $tag = DiscoverCategory::whereActive(true)
  83. ->whereSlug($slug)
  84. ->firstOrFail();
  85. $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
  86. $tagids = $tag->hashtags->pluck('id')->toArray();
  87. $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
  88. $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();
  89. return $posts;
  90. });
  91. $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
  92. return $tag->posts()->whereScope('public')->count();
  93. });
  94. return view('discover.tags.category', compact('tag', 'posts'));
  95. }
  96. public function showPersonal(Request $request)
  97. {
  98. abort_if(!Auth::check(), 403);
  99. $profile = Auth::user()->profile;
  100. $tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){
  101. return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get();
  102. });
  103. $following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) {
  104. $res = Follower::whereProfileId($profile->id)->pluck('following_id');
  105. return $res->push($profile->id)->toArray();
  106. });
  107. $posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) {
  108. $posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get();
  109. $posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
  110. return $posts;
  111. });
  112. return view('discover.personal', compact('posts', 'tags'));
  113. }
  114. public function showLoops(Request $request)
  115. {
  116. if(config('exp.loops') != true) {
  117. return redirect('/');
  118. }
  119. return view('discover.loops.home');
  120. }
  121. public function loopsApi(Request $request)
  122. {
  123. abort_if(!config('exp.loops'), 403);
  124. // todo proper pagination, maybe LoopService
  125. $loops = Status::whereType('video')
  126. ->whereScope('public')
  127. ->latest()
  128. ->take(18)
  129. ->get();
  130. $resource = new Fractal\Resource\Collection($loops, new StatusStatelessTransformer());
  131. return $this->fractal->createData($resource)->toArray();
  132. }
  133. public function loopWatch(Request $request)
  134. {
  135. abort_if(!Auth::check(), 403);
  136. abort_if(!config('exp.loops'), 403);
  137. $this->validate($request, [
  138. 'id' => 'integer|min:1'
  139. ]);
  140. $id = $request->input('id');
  141. // todo log loops
  142. return response()->json(200);
  143. }
  144. }