DiscoverController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\AccountTransformer;
  16. use App\Transformer\Api\AccountWithStatusesTransformer;
  17. use App\Transformer\Api\StatusTransformer;
  18. use App\Transformer\Api\StatusStatelessTransformer;
  19. use League\Fractal;
  20. use League\Fractal\Serializer\ArraySerializer;
  21. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  22. use App\Services\StatusHashtagService;
  23. class DiscoverController extends Controller
  24. {
  25. protected $fractal;
  26. public function __construct()
  27. {
  28. $this->fractal = new Fractal\Manager();
  29. $this->fractal->setSerializer(new ArraySerializer());
  30. }
  31. public function home(Request $request)
  32. {
  33. abort_if(!Auth::check(), 403);
  34. return view('discover.home');
  35. }
  36. public function showTags(Request $request, $hashtag)
  37. {
  38. abort_if(!config('instance.discover.tags.is_public') && !Auth::check(), 403);
  39. $tag = Hashtag::whereName($hashtag)
  40. ->orWhere('slug', $hashtag)
  41. ->firstOrFail();
  42. $tagCount = StatusHashtagService::count($tag->id);
  43. return view('discover.tags.show', compact('tag', 'tagCount'));
  44. }
  45. public function showCategory(Request $request, $slug)
  46. {
  47. abort_if(!Auth::check(), 403);
  48. $tag = DiscoverCategory::whereActive(true)
  49. ->whereSlug($slug)
  50. ->firstOrFail();
  51. $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
  52. $tagids = $tag->hashtags->pluck('id')->toArray();
  53. $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
  54. $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();
  55. return $posts;
  56. });
  57. $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
  58. return $tag->posts()->whereScope('public')->count();
  59. });
  60. return view('discover.tags.category', compact('tag', 'posts'));
  61. }
  62. public function showLoops(Request $request)
  63. {
  64. if(config('exp.loops') != true) {
  65. return redirect('/');
  66. }
  67. return view('discover.loops.home');
  68. }
  69. public function loopsApi(Request $request)
  70. {
  71. abort_if(!config('exp.loops'), 403);
  72. // todo proper pagination, maybe LoopService
  73. $res = Cache::remember('discover:loops:recent', now()->addHours(6), function() {
  74. $loops = Status::whereType('video')
  75. ->whereNull('uri')
  76. ->whereScope('public')
  77. ->latest()
  78. ->take(18)
  79. ->get();
  80. $resource = new Fractal\Resource\Collection($loops, new StatusStatelessTransformer());
  81. return $this->fractal->createData($resource)->toArray();
  82. });
  83. return $res;
  84. }
  85. public function loopWatch(Request $request)
  86. {
  87. abort_if(!Auth::check(), 403);
  88. abort_if(!config('exp.loops'), 403);
  89. $this->validate($request, [
  90. 'id' => 'integer|min:1'
  91. ]);
  92. $id = $request->input('id');
  93. // todo log loops
  94. return response()->json(200);
  95. }
  96. public function getHashtags(Request $request)
  97. {
  98. $auth = Auth::check();
  99. abort_if(!config('instance.discover.tags.is_public') && !$auth, 403);
  100. $this->validate($request, [
  101. 'hashtag' => 'required|alphanum|min:1|max:124',
  102. 'page' => 'nullable|integer|min:1|max:' . ($auth ? 29 : 10)
  103. ]);
  104. $page = $request->input('page') ?? '1';
  105. $end = $page > 1 ? $page * 9 : 0;
  106. $tag = $request->input('hashtag');
  107. $hashtag = Hashtag::whereName($tag)->firstOrFail();
  108. $res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end);
  109. if($page == 1) {
  110. $res['follows'] = HashtagFollow::whereUserId(Auth::id())->whereHashtagId($hashtag->id)->exists();
  111. }
  112. return $res;
  113. }
  114. public function profilesDirectory(Request $request)
  115. {
  116. return redirect('/')->with('statusRedirect', 'The Profile Directory is unavailable at this time.');
  117. return view('discover.profiles.home');
  118. }
  119. public function profilesDirectoryApi(Request $request)
  120. {
  121. $this->validate($request, [
  122. 'page' => 'integer|max:10'
  123. ]);
  124. return ['error' => 'Temporarily unavailable.'];
  125. $page = $request->input('page') ?? 1;
  126. $key = 'discover:profiles:page:' . $page;
  127. $ttl = now()->addHours(12);
  128. $res = Cache::remember($key, $ttl, function() {
  129. $profiles = Profile::whereNull('domain')
  130. ->whereNull('status')
  131. ->whereIsPrivate(false)
  132. ->has('statuses')
  133. ->whereIsSuggestable(true)
  134. // ->inRandomOrder()
  135. ->simplePaginate(8);
  136. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  137. return $this->fractal->createData($resource)->toArray();
  138. });
  139. return $res;
  140. }
  141. public function trendingApi(Request $request)
  142. {
  143. $this->validate($request, [
  144. 'range' => 'nullable|string|in:daily,monthly,alltime'
  145. ]);
  146. $range = $request->filled('range') ?
  147. $request->input('range') == 'alltime' ? '-1' :
  148. ($request->input('range') == 'daily' ? 1 : 31) : 1;
  149. $key = ':api:discover:trending:v1:range:' . $range;
  150. $ttl = now()->addHours(2);
  151. $res = Cache::remember($key, $ttl, function() use($range) {
  152. if($range == '-1') {
  153. $res = Status::orderBy('likes_count','desc')
  154. ->take(12)
  155. ->get();
  156. } else {
  157. $res = Status::orderBy('likes_count','desc')
  158. ->take(12)
  159. ->where('created_at', '>', now()->subDays($range))
  160. ->get();
  161. }
  162. $resource = new Fractal\Resource\Collection($res, new StatusStatelessTransformer());
  163. return $this->fractal->createData($resource)->toArray();
  164. });
  165. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  166. }
  167. public function trendingHashtags(Request $request)
  168. {
  169. $res = StatusHashtag::select('hashtag_id', \DB::raw('count(*) as total'))
  170. ->groupBy('hashtag_id')
  171. ->orderBy('total','desc')
  172. ->where('created_at', '>', now()->subDays(4))
  173. ->take(9)
  174. ->get()
  175. ->map(function($h) {
  176. $hashtag = $h->hashtag;
  177. return [
  178. 'id' => $hashtag->id,
  179. 'total' => $h->total,
  180. 'name' => '#'.$hashtag->name,
  181. 'url' => $hashtag->url('?src=dsh1')
  182. ];
  183. });
  184. return $res;
  185. }
  186. public function trendingPlaces(Request $request)
  187. {
  188. $res = Status::select('place_id',DB::raw('count(place_id) as total'))
  189. ->whereNotNull('place_id')
  190. ->where('created_at','>',now()->subDays(14))
  191. ->groupBy('place_id')
  192. ->orderBy('total')
  193. ->limit(4)
  194. ->get()
  195. ->map(function($s){
  196. $p = $s->place;
  197. return [
  198. 'name' => $p->name,
  199. 'country' => $p->country,
  200. 'url' => $p->url()
  201. ];
  202. });
  203. return $res;
  204. }
  205. }