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

172 lines
5.4 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,
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-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;
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-06-03 19:13:01 +00:00
abort_if(!Auth::check(), 403);
2019-03-02 19:11:24 +00:00
$tag = Hashtag::whereSlug($hashtag)
2018-08-10 02:58:52 +00:00
->firstOrFail();
2019-03-03 04:46:49 +00:00
$page = 1;
2019-03-02 08:38:44 +00:00
$key = 'discover:tag-'.$tag->id.':page-'.$page;
2019-03-03 04:46:49 +00:00
$keyMinutes = 15;
2019-03-02 08:38:44 +00:00
$posts = Cache::remember($key, now()->addMinutes($keyMinutes), function() use ($tag, $request) {
2019-03-04 03:29:51 +00:00
$tags = StatusHashtag::select('status_id')
->whereHashtagId($tag->id)
->orderByDesc('id')
->take(48)
->pluck('status_id');
return Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'created_at',
'updated_at'
)->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
->with('media')
->whereLocal(true)
->whereNull('uri')
->whereIn('id', $tags)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
2018-12-21 19:53:10 +00:00
->whereNull('url')
->whereNull('uri')
2018-10-20 18:29:37 +00:00
->withCount(['likes', 'comments'])
2018-08-10 02:58:52 +00:00
->whereIsNsfw(false)
->whereVisibility('public')
2018-08-28 03:07:36 +00:00
->orderBy('id', 'desc')
2019-03-03 04:46:49 +00:00
->get();
2019-03-02 08:38:44 +00:00
});
2018-08-10 02:58:52 +00:00
2018-11-19 04:26:52 +00:00
if($posts->count() == 0) {
abort(404);
}
2018-08-28 03:07:36 +00:00
return view('discover.tags.show', compact('tag', 'posts'));
}
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'));
}
public function showPersonal(Request $request)
{
2019-06-03 19:13:01 +00:00
abort_if(!Auth::check(), 403);
2019-02-10 20:25:37 +00:00
$profile = Auth::user()->profile;
2019-03-02 07:18:57 +00:00
$tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){
return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get();
});
$following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) {
$res = Follower::whereProfileId($profile->id)->pluck('following_id');
return $res->push($profile->id)->toArray();
});
$posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) {
$posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get();
$posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
return $posts;
});
2019-02-10 20:25:37 +00:00
return view('discover.personal', compact('posts', 'tags'));
}
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
$loops = Status::whereType('video')
->whereScope('public')
->latest()
->take(18)
->get();
$resource = new Fractal\Resource\Collection($loops, new StatusStatelessTransformer());
return $this->fractal->createData($resource)->toArray();
}
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);
}
}