1
0

DiscoverController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 showLoops(Request $request)
  58. {
  59. if(config('exp.loops') != true) {
  60. return redirect('/');
  61. }
  62. return view('discover.loops.home');
  63. }
  64. public function loopsApi(Request $request)
  65. {
  66. abort_if(!config('exp.loops'), 403);
  67. // todo proper pagination, maybe LoopService
  68. $res = Cache::remember('discover:loops:recent', now()->addHours(6), function() {
  69. $loops = Status::whereType('video')
  70. ->whereNull('uri')
  71. ->whereScope('public')
  72. ->latest()
  73. ->take(18)
  74. ->get();
  75. $resource = new Fractal\Resource\Collection($loops, new StatusStatelessTransformer());
  76. return $this->fractal->createData($resource)->toArray();
  77. });
  78. return $res;
  79. }
  80. public function loopWatch(Request $request)
  81. {
  82. abort_if(!Auth::check(), 403);
  83. abort_if(!config('exp.loops'), 403);
  84. $this->validate($request, [
  85. 'id' => 'integer|min:1'
  86. ]);
  87. $id = $request->input('id');
  88. // todo log loops
  89. return response()->json(200);
  90. }
  91. public function getHashtags(Request $request)
  92. {
  93. $auth = Auth::check();
  94. abort_if(!config('instance.discover.tags.is_public') && !$auth, 403);
  95. $this->validate($request, [
  96. 'hashtag' => 'required|alphanum|min:2|max:124',
  97. 'page' => 'nullable|integer|min:1|max:' . ($auth ? 19 : 3)
  98. ]);
  99. $page = $request->input('page') ?? '1';
  100. $end = $page > 1 ? $page * 9 : 0;
  101. $tag = $request->input('hashtag');
  102. $hashtag = Hashtag::whereName($tag)->firstOrFail();
  103. $res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end);
  104. if($page == 1) {
  105. $res['follows'] = HashtagFollow::whereUserId(Auth::id())->whereHashtagId($hashtag->id)->exists();
  106. }
  107. return $res;
  108. }
  109. public function profilesDirectory(Request $request)
  110. {
  111. $profiles = Profile::whereNull('domain')->simplePaginate(48);
  112. return view('discover.profiles.home', compact('profiles'));
  113. }
  114. }