DiscoverController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if($page == 1) {
  111. $res['follows'] = HashtagFollow::whereUserId(Auth::id())
  112. ->whereHashtagId($hashtag->id)
  113. ->exists();
  114. }
  115. $res['hashtag'] = [
  116. 'name' => $hashtag->name,
  117. 'url' => $hashtag->url()
  118. ];
  119. $res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end);
  120. return $res;
  121. }
  122. public function profilesDirectory(Request $request)
  123. {
  124. return redirect('/')->with('statusRedirect', 'The Profile Directory is unavailable at this time.');
  125. return view('discover.profiles.home');
  126. }
  127. public function profilesDirectoryApi(Request $request)
  128. {
  129. return ['error' => 'Temporarily unavailable.'];
  130. $this->validate($request, [
  131. 'page' => 'integer|max:10'
  132. ]);
  133. $page = $request->input('page') ?? 1;
  134. $key = 'discover:profiles:page:' . $page;
  135. $ttl = now()->addHours(12);
  136. $res = Cache::remember($key, $ttl, function() {
  137. $profiles = Profile::whereNull('domain')
  138. ->whereNull('status')
  139. ->whereIsPrivate(false)
  140. ->has('statuses')
  141. ->whereIsSuggestable(true)
  142. // ->inRandomOrder()
  143. ->simplePaginate(8);
  144. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  145. return $this->fractal->createData($resource)->toArray();
  146. });
  147. return $res;
  148. }
  149. public function trendingApi(Request $request)
  150. {
  151. $this->validate($request, [
  152. 'range' => 'nullable|string|in:daily,monthly'
  153. ]);
  154. $range = $request->input('range') == 'monthly' ? 31 : 1;
  155. $key = ':api:discover:trending:v2.8:range:' . $range;
  156. $ttl = now()->addMinutes(15);
  157. $ids = Cache::remember($key, $ttl, function() use($range) {
  158. $days = $range == 1 ? 2 : 31;
  159. $min_id = SnowflakeService::byDate(now()->subDays($days));
  160. return Status::select(
  161. 'id',
  162. 'scope',
  163. 'type',
  164. 'is_nsfw',
  165. 'likes_count',
  166. 'created_at'
  167. )
  168. ->where('id', '>', $min_id)
  169. ->whereNull('uri')
  170. ->whereScope('public')
  171. ->whereIn('type', [
  172. 'photo',
  173. 'photo:album',
  174. 'video'
  175. ])
  176. ->whereIsNsfw(false)
  177. ->orderBy('likes_count','desc')
  178. ->take(15)
  179. ->pluck('id');
  180. });
  181. $res = $ids->map(function($s) {
  182. return StatusService::get($s);
  183. });
  184. return response()->json($res);
  185. }
  186. public function trendingHashtags(Request $request)
  187. {
  188. return [];
  189. $res = StatusHashtag::select('hashtag_id', \DB::raw('count(*) as total'))
  190. ->groupBy('hashtag_id')
  191. ->orderBy('total','desc')
  192. ->where('created_at', '>', now()->subDays(4))
  193. ->take(9)
  194. ->get()
  195. ->map(function($h) {
  196. $hashtag = $h->hashtag;
  197. return [
  198. 'id' => $hashtag->id,
  199. 'total' => $h->total,
  200. 'name' => '#'.$hashtag->name,
  201. 'url' => $hashtag->url('?src=dsh1')
  202. ];
  203. });
  204. return $res;
  205. }
  206. public function trendingPlaces(Request $request)
  207. {
  208. return [];
  209. $res = Status::select('place_id',DB::raw('count(place_id) as total'))
  210. ->whereNotNull('place_id')
  211. ->where('created_at','>',now()->subDays(14))
  212. ->groupBy('place_id')
  213. ->orderBy('total')
  214. ->limit(4)
  215. ->get()
  216. ->map(function($s){
  217. $p = $s->place;
  218. return [
  219. 'name' => $p->name,
  220. 'country' => $p->country,
  221. 'url' => $p->url()
  222. ];
  223. });
  224. return [];
  225. }
  226. }