From 135474ae114231030687af3f104f863d7f7adfd0 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 15 Jul 2021 20:48:39 -0600 Subject: [PATCH 1/2] Update DiscoverController, use UserFilterService on trendingApi --- app/Http/Controllers/DiscoverController.php | 266 +++++++++++--------- app/Services/UserFilterService.php | 4 +- 2 files changed, 145 insertions(+), 125 deletions(-) diff --git a/app/Http/Controllers/DiscoverController.php b/app/Http/Controllers/DiscoverController.php index 87db73ba3..7b0a6d731 100644 --- a/app/Http/Controllers/DiscoverController.php +++ b/app/Http/Controllers/DiscoverController.php @@ -3,14 +3,14 @@ namespace App\Http\Controllers; use App\{ - DiscoverCategory, - Follower, - Hashtag, - HashtagFollow, - Profile, - Status, - StatusHashtag, - UserFilter + DiscoverCategory, + Follower, + Hashtag, + HashtagFollow, + Profile, + Status, + StatusHashtag, + UserFilter }; use Auth, DB, Cache; use Illuminate\Http\Request; @@ -24,145 +24,165 @@ use League\Fractal\Pagination\IlluminatePaginatorAdapter; use App\Services\StatusHashtagService; use App\Services\SnowflakeService; use App\Services\StatusService; +use App\Services\UserFilterService; class DiscoverController extends Controller { - protected $fractal; + protected $fractal; - public function __construct() - { - $this->fractal = new Fractal\Manager(); - $this->fractal->setSerializer(new ArraySerializer()); - } + public function __construct() + { + $this->fractal = new Fractal\Manager(); + $this->fractal->setSerializer(new ArraySerializer()); + } - public function home(Request $request) - { - abort_if(!Auth::check() && config('instance.discover.public') == false, 403); - return view('discover.home'); - } + public function home(Request $request) + { + abort_if(!Auth::check() && config('instance.discover.public') == false, 403); + return view('discover.home'); + } - public function showTags(Request $request, $hashtag) - { - abort_if(!config('instance.discover.tags.is_public') && !Auth::check(), 403); + 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')); - } + $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(404); - } + public function showCategory(Request $request, $slug) + { + abort(404); + } - public function showLoops(Request $request) - { - abort(404); - } + public function showLoops(Request $request) + { + abort(404); + } - public function loopsApi(Request $request) - { - abort(404); - } + public function loopsApi(Request $request) + { + abort(404); + } - public function loopWatch(Request $request) - { - return response()->json(200); - } + public function loopWatch(Request $request) + { + return response()->json(200); + } - public function getHashtags(Request $request) - { - $auth = Auth::check(); - abort_if(!config('instance.discover.tags.is_public') && !$auth, 403); + 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) - ]); + $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'); + $page = $request->input('page') ?? '1'; + $end = $page > 1 ? $page * 9 : 0; + $tag = $request->input('hashtag'); - $hashtag = Hashtag::whereName($tag)->firstOrFail(); - if($page == 1) { - $res['follows'] = HashtagFollow::whereUserId(Auth::id()) - ->whereHashtagId($hashtag->id) - ->exists(); - } - $res['hashtag'] = [ - 'name' => $hashtag->name, - 'url' => $hashtag->url() - ]; - $res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end); - return $res; - } + $hashtag = Hashtag::whereName($tag)->firstOrFail(); + if($page == 1) { + $res['follows'] = HashtagFollow::whereUserId(Auth::id()) + ->whereHashtagId($hashtag->id) + ->exists(); + } + $res['hashtag'] = [ + 'name' => $hashtag->name, + 'url' => $hashtag->url() + ]; + $res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end); + return $res; + } - public function profilesDirectory(Request $request) - { - return redirect('/') - ->with('statusRedirect', 'The Profile Directory is unavailable at this time.'); - } + public function profilesDirectory(Request $request) + { + return redirect('/') + ->with('statusRedirect', 'The Profile Directory is unavailable at this time.'); + } - public function profilesDirectoryApi(Request $request) - { - return ['error' => 'Temporarily unavailable.']; - } + public function profilesDirectoryApi(Request $request) + { + return ['error' => 'Temporarily unavailable.']; + } - public function trendingApi(Request $request) - { - abort_if(config('instance.discover.public') == false && !Auth::check(), 403); + public function trendingApi(Request $request) + { + abort_if(config('instance.discover.public') == false && !Auth::check(), 403); - $this->validate($request, [ - 'range' => 'nullable|string|in:daily,monthly' - ]); + $this->validate($request, [ + 'range' => 'nullable|string|in:daily,monthly' + ]); - $range = $request->input('range') == 'monthly' ? 31 : 1; + $range = $request->input('range') == 'monthly' ? 31 : 1; - $key = ':api:discover:trending:v2.8:range:' . $range; - $ttl = now()->addMinutes(15); + $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'); - }); + $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); - }); + $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : []; - return response()->json($res); - } + $res = $ids->map(function($s) { + return StatusService::get($s); + })->filter(function($s) use($filtered) { + return $s && !in_array($s['account']['id'], $filtered); + })->values(); - public function trendingHashtags(Request $request) - { - return []; - } + return response()->json($res); + } - public function trendingPlaces(Request $request) - { - return []; - } + public function trendingHashtags(Request $request) + { + $res = StatusHashtag::select('hashtag_id', \DB::raw('count(*) as total')) + ->groupBy('hashtag_id') + ->orderBy('total','desc') + ->where('created_at', '>', now()->subDays(90)) + ->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 []; + } } diff --git a/app/Services/UserFilterService.php b/app/Services/UserFilterService.php index d449435bf..4d155fa69 100644 --- a/app/Services/UserFilterService.php +++ b/app/Services/UserFilterService.php @@ -54,7 +54,7 @@ class UserFilterService { public static function filters(int $profile_id) : array { - return array_merge(self::mutes($profile_id), self::blocks($profile_id)); + return array_unique(array_merge(self::mutes($profile_id), self::blocks($profile_id))); } public static function mute(int $profile_id, int $muted_id) @@ -98,4 +98,4 @@ class UserFilterService { } return $exists; } -} \ No newline at end of file +} From 2fe676aa55b517e0cf85ec7cd76c9e31e4022104 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 15 Jul 2021 20:50:27 -0600 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49b3abfaa..dd7f04c5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ - Updated reply blade view, fix missing avatar and media images. ([5fb33772](https://github.com/pixelfed/pixelfed/commit/5fb33772)) - Updated components, add fallback default avatar. ([726553f5](https://github.com/pixelfed/pixelfed/commit/726553f5)) - Updated job queue, separate deletes into their own queue. ([7f421392](https://github.com/pixelfed/pixelfed/commit/7f421392)) +- Updated DiscoverController, use UserFilterService on trendingApi. ([135474ae](https://github.com/pixelfed/pixelfed/commit/135474ae)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.11.0 (2021-06-01)](https://github.com/pixelfed/pixelfed/compare/v0.10.10...v0.11.0)