From b86102823bc1a7dcae5d9c4533c53b9137356f20 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 14 Apr 2025 01:18:14 -0600 Subject: [PATCH] Update CustomFilter model and CustomFilterController --- app/Http/Controllers/Api/ApiV1Controller.php | 77 +++++++++++++++++-- .../Controllers/CustomFilterController.php | 2 +- app/Models/CustomFilter.php | 2 + 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 2d25f236c..a82863841 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -2559,21 +2559,23 @@ class ApiV1Controller extends Controller ->filter(function ($s) use ($includeReblogs) { return $includeReblogs ? true : $s['reblog'] == null; }) - ->filter(function ($s) use ($homeFilters) { - $filterResults = CustomFilter::applyCachedFilters($homeFilters, $s); + ->map(function($status) use ($homeFilters) { + $filterResults = CustomFilter::applyCachedFilters($homeFilters, $status); - if (! empty($filterResults)) { - $shouldHide = collect($filterResults)->contains(function ($result) { - return $result['filter']->action === CustomFilter::ACTION_HIDE; + if (!empty($filterResults)) { + $status['filtered'] = $filterResults; + $shouldHide = collect($filterResults)->contains(function($result) { + return $result['filter']['filter_action'] === "hide"; }); if ($shouldHide) { - return false; + return null; } } - return true; + return $status; }) + ->filter() ->take($limit) ->map(function ($status) use ($pid) { if ($pid) { @@ -2682,6 +2684,23 @@ class ApiV1Controller extends Controller return $status; }) + ->map(function($status) use ($homeFilters) { + $filterResults = CustomFilter::applyCachedFilters($homeFilters, $status); + + if (!empty($filterResults)) { + $status['filtered'] = $filterResults; + $shouldHide = collect($filterResults)->contains(function($result) { + return $result['filter']['filter_action'] === "hide"; + }); + + if ($shouldHide) { + return null; + } + } + + return $status; + }) + ->filter() ->take($limit) ->values(); } else { @@ -2736,6 +2755,23 @@ class ApiV1Controller extends Controller return $status; }) + ->map(function($status) use ($homeFilters) { + $filterResults = CustomFilter::applyCachedFilters($homeFilters, $status); + + if (!empty($filterResults)) { + $status['filtered'] = $filterResults; + $shouldHide = collect($filterResults)->contains(function($result) { + return $result['filter']['filter_action'] === "hide"; + }); + + if ($shouldHide) { + return null; + } + } + + return $status; + }) + ->filter() ->take($limit) ->values(); } @@ -2797,7 +2833,7 @@ class ApiV1Controller extends Controller $limit = 40; } $user = $request->user(); - + $pid = $user->profile_id; $remote = $request->has('remote') && $request->boolean('remote'); $local = $request->boolean('local'); $userRoleKey = $remote ? 'can-view-network-feed' : 'can-view-public-feed'; @@ -2810,6 +2846,14 @@ class ApiV1Controller extends Controller $hideNsfw = config('instance.hide_nsfw_on_public_feeds'); $amin = SnowflakeService::byDate(now()->subDays(config('federation.network_timeline_days_falloff'))); $asf = AdminShadowFilterService::getHideFromPublicFeedsList(); + + $cachedFilters = CustomFilter::getCachedFiltersForAccount($pid); + + $homeFilters = array_filter($cachedFilters, function ($item) { + [$filter, $rules] = $item; + + return in_array('public', $filter->context); + }); if ($local && $remote) { $feed = Status::select( 'id', @@ -3000,6 +3044,23 @@ class ApiV1Controller extends Controller return true; }) + ->map(function($status) use ($homeFilters) { + $filterResults = CustomFilter::applyCachedFilters($homeFilters, $status); + + if (!empty($filterResults)) { + $status['filtered'] = $filterResults; + $shouldHide = collect($filterResults)->contains(function($result) { + return $result['filter']['filter_action'] === "hide"; + }); + + if ($shouldHide) { + return null; + } + } + + return $status; + }) + ->filter() ->take($limit) ->values(); diff --git a/app/Http/Controllers/CustomFilterController.php b/app/Http/Controllers/CustomFilterController.php index a76960ae0..581d35bb7 100644 --- a/app/Http/Controllers/CustomFilterController.php +++ b/app/Http/Controllers/CustomFilterController.php @@ -281,7 +281,7 @@ class CustomFilterController extends Controller ]); $rateKey = 'filters_updated:'.$request->user()->id; - $maxUpdatesPerHour = 30; + $maxUpdatesPerHour = CustomFilter::MAX_UPDATES_PER_HOUR; $currentCount = Cache::get($rateKey, 0); if ($currentCount >= $maxUpdatesPerHour) { diff --git a/app/Models/CustomFilter.php b/app/Models/CustomFilter.php index 013dc7ef8..1f0463e4a 100644 --- a/app/Models/CustomFilter.php +++ b/app/Models/CustomFilter.php @@ -43,6 +43,8 @@ class CustomFilter extends Model const MAX_PER_HOUR = 40; + const MAX_UPDATES_PER_HOUR = 40; + const EXPIRATION_DURATIONS = [ 1800, // 30 minutes 3600, // 1 hour