1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Http/Controllers/DiscoverController.php

166 lines
5.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-09-22 05:23:16 +00:00
use App\{
2019-02-10 20:25:37 +00:00
DiscoverCategory,
2018-09-22 05:23:16 +00:00
Follower,
Hashtag,
2019-07-08 06:04:02 +00:00
HashtagFollow,
2018-09-22 05:23:16 +00:00
Profile,
Status,
2019-02-10 20:25:37 +00:00
StatusHashtag,
2018-09-22 05:23:16 +00:00
UserFilter
};
use Auth, DB, Cache;
2018-08-28 03:07:36 +00:00
use Illuminate\Http\Request;
2019-11-24 22:48:53 +00:00
use App\Transformer\Api\AccountTransformer;
use App\Transformer\Api\AccountWithStatusesTransformer;
2019-06-03 19:13:01 +00:00
use App\Transformer\Api\StatusStatelessTransformer;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
2019-07-05 02:21:20 +00:00
use App\Services\StatusHashtagService;
class DiscoverController extends Controller
{
2019-06-03 19:13:01 +00:00
protected $fractal;
2018-05-30 03:04:26 +00:00
public function __construct()
{
2019-06-03 19:13:01 +00:00
$this->fractal = new Fractal\Manager();
$this->fractal->setSerializer(new ArraySerializer());
2018-05-30 03:04:26 +00:00
}
2018-09-22 05:31:21 +00:00
public function home(Request $request)
{
2019-06-03 19:13:01 +00:00
abort_if(!Auth::check(), 403);
2018-11-09 04:14:35 +00:00
return view('discover.home');
}
public function showTags(Request $request, $hashtag)
{
2019-07-08 06:04:02 +00:00
abort_if(!config('instance.discover.tags.is_public') && !Auth::check(), 403);
2020-02-15 05:11:42 +00:00
$tag = Hashtag::whereName($hashtag)
->orWhere('slug', $hashtag)
->firstOrFail();
2019-07-05 02:21:20 +00:00
$tagCount = StatusHashtagService::count($tag->id);
return view('discover.tags.show', compact('tag', 'tagCount'));
}
2019-02-10 20:25:37 +00:00
public function showCategory(Request $request, $slug)
{
2019-06-03 19:13:01 +00:00
abort_if(!Auth::check(), 403);
2019-02-10 20:25:37 +00:00
$tag = DiscoverCategory::whereActive(true)
->whereSlug($slug)
->firstOrFail();
2019-03-02 07:18:57 +00:00
$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();
});
2019-02-10 20:25:37 +00:00
return view('discover.tags.category', compact('tag', 'posts'));
}
2019-05-26 00:56:21 +00:00
public function showLoops(Request $request)
{
2019-06-03 18:50:37 +00:00
if(config('exp.loops') != true) {
return redirect('/');
}
2019-05-26 00:56:21 +00:00
return view('discover.loops.home');
}
2019-06-03 19:13:01 +00:00
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() {
2019-06-03 19:49:31 +00:00
$loops = Status::whereType('video')
->whereNull('uri')
2019-06-03 19:49:31 +00:00
->whereScope('public')
->latest()
->take(18)
->get();
$resource = new Fractal\Resource\Collection($loops, new StatusStatelessTransformer());
return $this->fractal->createData($resource)->toArray();
});
return $res;
2019-06-03 19:13:01 +00:00
}
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);
}
2019-07-05 02:21:20 +00:00
public function getHashtags(Request $request)
{
2019-07-08 06:04:02 +00:00
$auth = Auth::check();
2019-07-08 06:09:50 +00:00
abort_if(!config('instance.discover.tags.is_public') && !$auth, 403);
2019-07-08 06:04:02 +00:00
2019-07-05 02:21:20 +00:00
$this->validate($request, [
2019-12-05 02:54:26 +00:00
'hashtag' => 'required|alphanum|min:1|max:124',
'page' => 'nullable|integer|min:1|max:' . ($auth ? 29 : 10)
2019-07-05 02:21:20 +00:00
]);
$page = $request->input('page') ?? '1';
2019-07-11 00:58:52 +00:00
$end = $page > 1 ? $page * 9 : 0;
2019-07-05 02:21:20 +00:00
$tag = $request->input('hashtag');
$hashtag = Hashtag::whereName($tag)->firstOrFail();
2019-07-08 06:04:02 +00:00
$res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end);
if($page == 1) {
$res['follows'] = HashtagFollow::whereUserId(Auth::id())->whereHashtagId($hashtag->id)->exists();
}
2019-07-05 02:21:20 +00:00
return $res;
}
2019-09-07 05:25:55 +00:00
public function profilesDirectory(Request $request)
{
2019-11-24 22:48:53 +00:00
return view('discover.profiles.home');
}
public function profilesDirectoryApi(Request $request)
{
$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;
2019-09-07 05:25:55 +00:00
}
}