123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace App\Http\Controllers;
- use App\{
- DiscoverCategory,
- Follower,
- Hashtag,
- HashtagFollow,
- Profile,
- Status,
- StatusHashtag,
- UserFilter
- };
- use Auth, DB, Cache;
- use Illuminate\Http\Request;
- use App\Transformer\Api\AccountTransformer;
- use App\Transformer\Api\AccountWithStatusesTransformer;
- use App\Transformer\Api\StatusTransformer;
- use App\Transformer\Api\StatusStatelessTransformer;
- use League\Fractal;
- use League\Fractal\Serializer\ArraySerializer;
- use League\Fractal\Pagination\IlluminatePaginatorAdapter;
- use App\Services\StatusHashtagService;
- use App\Services\SnowflakeService;
- use App\Services\StatusService;
- class DiscoverController extends Controller
- {
- protected $fractal;
- public function __construct()
- {
- $this->fractal = new Fractal\Manager();
- $this->fractal->setSerializer(new ArraySerializer());
- }
- public function home(Request $request)
- {
- abort_if(!Auth::check(), 403);
- return view('discover.home');
- }
- public function showTags(Request $request, $hashtag)
- {
- abort_if(!config('instance.discover.tags.is_public') && !Auth::check(), 403);
- $tag = Hashtag::whereName($hashtag)
- ->orWhere('slug', $hashtag)
- ->firstOrFail();
- $tagCount = StatusHashtagService::count($tag->id);
- return view('discover.tags.show', compact('tag', 'tagCount'));
- }
- public function showCategory(Request $request, $slug)
- {
- abort_if(!Auth::check(), 403);
- $tag = DiscoverCategory::whereActive(true)
- ->whereSlug($slug)
- ->firstOrFail();
- $posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
- $tagids = $tag->hashtags->pluck('id')->toArray();
- $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
- $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();
- return $posts;
- });
- $tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
- return $tag->posts()->whereScope('public')->count();
- });
- return view('discover.tags.category', compact('tag', 'posts'));
- }
- public function showLoops(Request $request)
- {
- if(config('exp.loops') != true) {
- return redirect('/');
- }
- return view('discover.loops.home');
- }
- public function loopsApi(Request $request)
- {
- abort_if(!config('exp.loops'), 403);
-
- // todo proper pagination, maybe LoopService
- $res = Cache::remember('discover:loops:recent', now()->addHours(6), function() {
- $loops = Status::whereType('video')
- ->whereNull('uri')
- ->whereScope('public')
- ->latest()
- ->take(18)
- ->get();
- $resource = new Fractal\Resource\Collection($loops, new StatusStatelessTransformer());
- return $this->fractal->createData($resource)->toArray();
- });
- return $res;
- }
- public function loopWatch(Request $request)
- {
- abort_if(!Auth::check(), 403);
- abort_if(!config('exp.loops'), 403);
- $this->validate($request, [
- 'id' => 'integer|min:1'
- ]);
- $id = $request->input('id');
- // todo log loops
- return response()->json(200);
- }
- public function getHashtags(Request $request)
- {
- $auth = Auth::check();
- abort_if(!config('instance.discover.tags.is_public') && !$auth, 403);
- $this->validate($request, [
- 'hashtag' => 'required|string|min:1|max:124',
- 'page' => 'nullable|integer|min:1|max:' . ($auth ? 29 : 10)
- ]);
- $page = $request->input('page') ?? '1';
- $end = $page > 1 ? $page * 9 : 0;
- $tag = $request->input('hashtag');
- $hashtag = Hashtag::whereName($tag)->firstOrFail();
- $res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end);
- if($page == 1) {
- $res['follows'] = HashtagFollow::whereUserId(Auth::id())->whereHashtagId($hashtag->id)->exists();
- }
- return $res;
- }
- public function profilesDirectory(Request $request)
- {
- return redirect('/')->with('statusRedirect', 'The Profile Directory is unavailable at this time.');
- return view('discover.profiles.home');
- }
- public function profilesDirectoryApi(Request $request)
- {
- return ['error' => 'Temporarily unavailable.'];
- $this->validate($request, [
- 'page' => 'integer|max:10'
- ]);
- $page = $request->input('page') ?? 1;
- $key = 'discover:profiles:page:' . $page;
- $ttl = now()->addHours(12);
- $res = Cache::remember($key, $ttl, function() {
- $profiles = Profile::whereNull('domain')
- ->whereNull('status')
- ->whereIsPrivate(false)
- ->has('statuses')
- ->whereIsSuggestable(true)
- // ->inRandomOrder()
- ->simplePaginate(8);
- $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
- return $this->fractal->createData($resource)->toArray();
- });
- return $res;
- }
- public function trendingApi(Request $request)
- {
- $this->validate($request, [
- 'range' => 'nullable|string|in:daily,monthly'
- ]);
- $range = $request->input('range') == 'monthly' ? 31 : 1;
- $key = ':api:discover:trending:v2.8:range:' . $range;
- $ttl = now()->addMinutes(15);
- $ids = Cache::remember($key, $ttl, function() use($range) {
- $days = $range == 1 ? 2 : 31;
- $min_id = SnowflakeService::byDate(now()->subDays($days));
- return Status::select(
- 'id',
- 'scope',
- 'type',
- 'is_nsfw',
- 'likes_count',
- 'created_at'
- )
- ->where('id', '>', $min_id)
- ->whereNull('uri')
- ->whereScope('public')
- ->whereIn('type', [
- 'photo',
- 'photo:album',
- 'video'
- ])
- ->whereIsNsfw(false)
- ->orderBy('likes_count','desc')
- ->take(15)
- ->pluck('id');
- });
- $res = $ids->map(function($s) {
- return StatusService::get($s);
- });
- return response()->json($res);
- }
- public function trendingHashtags(Request $request)
- {
- return [];
-
- $res = StatusHashtag::select('hashtag_id', \DB::raw('count(*) as total'))
- ->groupBy('hashtag_id')
- ->orderBy('total','desc')
- ->where('created_at', '>', now()->subDays(4))
- ->take(9)
- ->get()
- ->map(function($h) {
- $hashtag = $h->hashtag;
- return [
- 'id' => $hashtag->id,
- 'total' => $h->total,
- 'name' => '#'.$hashtag->name,
- 'url' => $hashtag->url('?src=dsh1')
- ];
- });
- return $res;
- }
- public function trendingPlaces(Request $request)
- {
- return [];
- $res = Status::select('place_id',DB::raw('count(place_id) as total'))
- ->whereNotNull('place_id')
- ->where('created_at','>',now()->subDays(14))
- ->groupBy('place_id')
- ->orderBy('total')
- ->limit(4)
- ->get()
- ->map(function($s){
- $p = $s->place;
- return [
- 'name' => $p->name,
- 'country' => $p->country,
- 'url' => $p->url()
- ];
- });
- return [];
- }
- }
|