1
0
Fork 0
pixelfed/app/Http/Controllers/DiscoverController.php

95 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-09-22 05:23:16 +00:00
use App\{
Follower,
Hashtag,
Profile,
Status,
UserFilter
};
use Auth, DB, Cache;
2018-08-28 03:07:36 +00:00
use Illuminate\Http\Request;
class DiscoverController extends Controller
{
2018-05-30 03:04:26 +00:00
public function __construct()
{
2018-08-28 03:07:36 +00:00
$this->middleware('auth');
2018-05-30 03:04:26 +00:00
}
2018-09-22 05:31:21 +00:00
public function home(Request $request)
{
2018-09-22 05:31:21 +00:00
$this->validate($request, [
'page' => 'nullable|integer|max:50'
]);
2018-08-28 03:07:36 +00:00
$pid = Auth::user()->profile->id;
2018-08-10 02:58:52 +00:00
2018-09-23 02:41:26 +00:00
$following = Cache::remember('feature:discover:following:'.$pid, 720, function() use($pid) {
$following = Follower::select('following_id')
->whereProfileId($pid)
->pluck('following_id');
$filtered = UserFilter::select('filterable_id')
->whereUserId($pid)
2018-09-22 05:23:16 +00:00
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id');
$following->push($pid);
if($filtered->count() > 0) {
$following->push($filtered);
}
return $following;
});
2018-08-31 04:18:43 +00:00
2018-09-22 05:23:16 +00:00
$people = Cache::remember('feature:discover:people:'.$pid, 15, function() use($following) {
2018-09-23 02:41:26 +00:00
return Profile::select('id', 'name', 'username')->inRandomOrder()
2018-09-22 05:23:16 +00:00
->whereHas('statuses')
->whereNull('domain')
->whereNotIn('id', $following)
->whereIsPrivate(false)
->take(3)
->get();
});
2018-08-10 02:58:52 +00:00
2018-09-23 02:41:26 +00:00
$posts = Status::select('id', 'caption', 'profile_id')
->whereHas('media')
2018-09-22 05:23:16 +00:00
->whereHas('profile', function($q) {
$q->where('is_private', false);
})
2018-09-22 05:31:21 +00:00
->whereIsNsfw(false)
2018-09-09 02:42:35 +00:00
->whereVisibility('public')
2018-09-23 02:41:26 +00:00
->where('profile_id', '<>', $pid)
2018-08-10 02:58:52 +00:00
->whereNotIn('profile_id', $following)
2018-09-22 05:44:33 +00:00
->withCount(['comments', 'likes'])
2018-08-10 02:58:52 +00:00
->orderBy('created_at', 'desc')
->simplePaginate(21);
2018-08-28 03:07:36 +00:00
return view('discover.home', compact('people', 'posts'));
}
public function showTags(Request $request, $hashtag)
{
2018-08-28 03:07:36 +00:00
$this->validate($request, [
'page' => 'nullable|integer|min:1|max:10',
2018-08-10 02:58:52 +00:00
]);
2018-08-28 03:07:36 +00:00
$tag = Hashtag::with('posts')
2018-08-10 02:58:52 +00:00
->withCount('posts')
->whereSlug($hashtag)
->firstOrFail();
2018-08-28 03:07:36 +00:00
$posts = $tag->posts()
2018-10-20 18:29:37 +00:00
->withCount(['likes', 'comments'])
2018-08-10 02:58:52 +00:00
->whereIsNsfw(false)
->whereVisibility('public')
->has('media')
2018-08-28 03:07:36 +00:00
->orderBy('id', 'desc')
2018-08-10 02:58:52 +00:00
->simplePaginate(12);
2018-08-28 03:07:36 +00:00
return view('discover.tags.show', compact('tag', 'posts'));
}
}