DiscoverController.php 7.6 KB

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