DiscoverController.php 4.9 KB

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