From e0c3dae32447721fc025709aedde44e78feb3a13 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 31 Dec 2022 06:25:43 -0700 Subject: [PATCH 1/3] Update profile audience to filter blocked instances --- app/Services/FollowerService.php | 32 ++++++++++++++++++++++------ app/Services/InstanceService.php | 6 +++--- app/Services/RelationshipService.php | 4 ++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/Services/FollowerService.php b/app/Services/FollowerService.php index aacf71218..b9cd36723 100644 --- a/app/Services/FollowerService.php +++ b/app/Services/FollowerService.php @@ -34,8 +34,8 @@ class FollowerService { Redis::zrem(self::FOLLOWING_KEY . $actor, $target); Redis::zrem(self::FOLLOWERS_KEY . $target, $actor); - Cache::forget('pf:services:follow:audience:' . $actor); - Cache::forget('pf:services:follow:audience:' . $target); + Cache::forget('pf:services:follower:audience:' . $actor); + Cache::forget('pf:services:follower:audience:' . $target); AccountService::del($actor); AccountService::del($target); RelationshipService::refresh($actor, $target); @@ -151,9 +151,9 @@ class FollowerService protected function getAudienceInboxes($pid, $scope = null) { - $key = 'pf:services:follow:audience:' . $pid; - return Cache::remember($key, 86400, function() use($pid) { - $profile = Profile::find($pid); + $key = 'pf:services:follower:audience:' . $pid; + $domains = Cache::remember($key, 432000, function() use($pid) { + $profile = Profile::whereNull(['status', 'domain'])->find($pid); if(!$profile) { return []; } @@ -165,9 +165,27 @@ class FollowerService }) ->filter() ->unique() - ->values() - ->toArray(); + ->values(); }); + + if(!$domains || !$domains->count()) { + return []; + } + + $banned = InstanceService::getBannedDomains(); + + if(!$banned || count($banned) === 0) { + return $domains->toArray(); + } + + $res = $domains->filter(function($domain) use($banned) { + $parsed = parse_url($domain, PHP_URL_HOST); + return !in_array($parsed, $banned); + }) + ->values() + ->toArray(); + + return $res; } public static function mutualCount($pid, $mid) diff --git a/app/Services/InstanceService.php b/app/Services/InstanceService.php index 2b253e233..3f051d66d 100644 --- a/app/Services/InstanceService.php +++ b/app/Services/InstanceService.php @@ -20,21 +20,21 @@ class InstanceService public static function getBannedDomains() { - return Cache::remember(self::CACHE_KEY_BANNED_DOMAINS, now()->addHours(12), function() { + return Cache::remember(self::CACHE_KEY_BANNED_DOMAINS, 1209600, function() { return Instance::whereBanned(true)->pluck('domain')->toArray(); }); } public static function getUnlistedDomains() { - return Cache::remember(self::CACHE_KEY_UNLISTED_DOMAINS, now()->addHours(12), function() { + return Cache::remember(self::CACHE_KEY_UNLISTED_DOMAINS, 1209600, function() { return Instance::whereUnlisted(true)->pluck('domain')->toArray(); }); } public static function getNsfwDomains() { - return Cache::remember(self::CACHE_KEY_NSFW_DOMAINS, now()->addHours(12), function() { + return Cache::remember(self::CACHE_KEY_NSFW_DOMAINS, 1209600, function() { return Instance::whereAutoCw(true)->pluck('domain')->toArray(); }); } diff --git a/app/Services/RelationshipService.php b/app/Services/RelationshipService.php index a5642e79e..a00e6e085 100644 --- a/app/Services/RelationshipService.php +++ b/app/Services/RelationshipService.php @@ -57,8 +57,8 @@ class RelationshipService public static function refresh($aid, $tid) { - Cache::forget('pf:services:follow:audience:' . $aid); - Cache::forget('pf:services:follow:audience:' . $tid); + Cache::forget('pf:services:follower:audience:' . $aid); + Cache::forget('pf:services:follower:audience:' . $tid); self::delete($tid, $aid); self::delete($aid, $tid); self::get($tid, $aid); From 4d1f281144baeb5d7387ec89e65e792dcb49e4a3 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 31 Dec 2022 06:28:17 -0700 Subject: [PATCH 2/3] Update SearchApiV2Service, improve query performance --- app/Services/SearchApiV2Service.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Services/SearchApiV2Service.php b/app/Services/SearchApiV2Service.php index 397af6b18..0471224b8 100644 --- a/app/Services/SearchApiV2Service.php +++ b/app/Services/SearchApiV2Service.php @@ -87,7 +87,7 @@ class SearchApiV2Service $limit = $this->query->input('limit') ?? 20; $offset = $this->query->input('offset') ?? 0; $rawQuery = $initalQuery ? $initalQuery : $this->query->input('q'); - $query = '%' . $rawQuery . '%'; + $query = $rawQuery . '%'; if(Str::substrCount($rawQuery, '@') >= 1 && Str::contains($rawQuery, config('pixelfed.domain.app'))) { $deliminatorCount = Str::substrCount($rawQuery, '@'); $query = explode('@', $rawQuery)[$deliminatorCount == 1 ? 0 : 1]; @@ -123,7 +123,7 @@ class SearchApiV2Service $mastodonMode = self::$mastodonMode; $limit = $this->query->input('limit') ?? 20; $offset = $this->query->input('offset') ?? 0; - $query = '%' . $this->query->input('q') . '%'; + $query = $this->query->input('q') . '%'; return Hashtag::where('can_search', true) ->where('name', 'like', $query) ->offset($offset) From 4ce43f49281bb1f446d9cf7a4c7a9d3a9b670867 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 31 Dec 2022 06:28:52 -0700 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f175cce1..ec7abf287 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,8 @@ - Update AdminInvite component, fix email regex ([2aea77d3](https://github.com/pixelfed/pixelfed/commit/2aea77d3)) - Update database config, use single transaction and skip lock tables for mysql dump ([936f1e7a](https://github.com/pixelfed/pixelfed/commit/936f1e7a)) - Update database config, add sticky flag https://laravel.com/docs/9.x/database#the-sticky-option ([10b65980](https://github.com/pixelfed/pixelfed/commit/10b65980)) +- Update profile audience to filter blocked instances ([e0c3dae3](https://github.com/pixelfed/pixelfed/commit/e0c3dae3)) +- Update SearchApiV2Service, improve query performance ([4d1f2811](https://github.com/pixelfed/pixelfed/commit/4d1f2811)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.11.4 (2022-10-04)](https://github.com/pixelfed/pixelfed/compare/v0.11.3...v0.11.4)