DiscoverController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. DiscoverCategory,
  5. Follower,
  6. Hashtag,
  7. HashtagFollow,
  8. Profile,
  9. Status,
  10. StatusHashtag,
  11. UserFilter
  12. };
  13. use Auth, DB, Cache;
  14. use Illuminate\Http\Request;
  15. use App\Transformer\Api\StatusStatelessTransformer;
  16. use League\Fractal;
  17. use League\Fractal\Serializer\ArraySerializer;
  18. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  19. use App\Services\StatusHashtagService;
  20. class DiscoverController extends Controller
  21. {
  22. protected $fractal;
  23. public function __construct()
  24. {
  25. $this->fractal = new Fractal\Manager();
  26. $this->fractal->setSerializer(new ArraySerializer());
  27. }
  28. public function home(Request $request)
  29. {
  30. abort_if(!Auth::check(), 403);
  31. return view('discover.home');
  32. }
  33. public function showTags(Request $request, $hashtag)
  34. {
  35. abort_if(!config('instance.discover.tags.is_public') && !Auth::check(), 403);
  36. $tag = Hashtag::whereSlug($hashtag)->firstOrFail();
  37. $tagCount = StatusHashtagService::count($tag->id);
  38. return view('discover.tags.show', compact('tag', 'tagCount'));
  39. }
  40. public function showCategory(Request $request, $slug)
  41. {
  42. abort_if(!Auth::check(), 403);
  43. $tag = DiscoverCategory::whereActive(true)
  44. ->whereSlug($slug)
  45. ->firstOrFail();
  46. $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
  47. $tagids = $tag->hashtags->pluck('id')->toArray();
  48. $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
  49. $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();
  50. return $posts;
  51. });
  52. $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
  53. return $tag->posts()->whereScope('public')->count();
  54. });
  55. return view('discover.tags.category', compact('tag', 'posts'));
  56. }
  57. public function showPersonal(Request $request)
  58. {
  59. abort_if(!Auth::check(), 403);
  60. $profile = Auth::user()->profile;
  61. $tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){
  62. return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get();
  63. });
  64. $following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) {
  65. $res = Follower::whereProfileId($profile->id)->pluck('following_id');
  66. return $res->push($profile->id)->toArray();
  67. });
  68. $posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) {
  69. $posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get();
  70. $posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
  71. return $posts;
  72. });
  73. return view('discover.personal', compact('posts', 'tags'));
  74. }
  75. public function showLoops(Request $request)
  76. {
  77. if(config('exp.loops') != true) {
  78. return redirect('/');
  79. }
  80. return view('discover.loops.home');
  81. }
  82. public function loopsApi(Request $request)
  83. {
  84. abort_if(!config('exp.loops'), 403);
  85. // todo proper pagination, maybe LoopService
  86. $res = Cache::remember('discover:loops:recent', now()->addHours(1), function() {
  87. $loops = Status::whereType('video')
  88. ->whereScope('public')
  89. ->latest()
  90. ->take(18)
  91. ->get();
  92. $resource = new Fractal\Resource\Collection($loops, new StatusStatelessTransformer());
  93. return $this->fractal->createData($resource)->toArray();
  94. });
  95. return $res;
  96. }
  97. public function loopWatch(Request $request)
  98. {
  99. abort_if(!Auth::check(), 403);
  100. abort_if(!config('exp.loops'), 403);
  101. $this->validate($request, [
  102. 'id' => 'integer|min:1'
  103. ]);
  104. $id = $request->input('id');
  105. // todo log loops
  106. return response()->json(200);
  107. }
  108. public function getHashtags(Request $request)
  109. {
  110. $auth = Auth::check();
  111. abort_if(!config('instance.discover.tags.is_public') && !$auth, 403);
  112. $this->validate($request, [
  113. 'hashtag' => 'required|alphanum|min:2|max:124',
  114. 'page' => 'nullable|integer|min:1|max:' . ($auth ? 19 : 3)
  115. ]);
  116. $page = $request->input('page') ?? '1';
  117. $end = $page > 1 ? $page * 9 : 0;
  118. $tag = $request->input('hashtag');
  119. $hashtag = Hashtag::whereName($tag)->firstOrFail();
  120. $res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end);
  121. if($page == 1) {
  122. $res['follows'] = HashtagFollow::whereUserId(Auth::id())->whereHashtagId($hashtag->id)->exists();
  123. }
  124. return $res;
  125. }
  126. }