From 2cae8b48de387702e744f536aed35752b47bc83c Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 30 Jun 2024 23:02:51 -0600 Subject: [PATCH 1/2] Update discover, add network trending using Beagle API --- app/Http/Controllers/DiscoverController.php | 8 +++ app/Services/Internal/BeagleService.php | 74 +++++++++++++++++++-- routes/api.php | 1 + 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/DiscoverController.php b/app/Http/Controllers/DiscoverController.php index c9e93eecf..b3047ff79 100644 --- a/app/Http/Controllers/DiscoverController.php +++ b/app/Http/Controllers/DiscoverController.php @@ -11,6 +11,7 @@ use App\Services\BookmarkService; use App\Services\ConfigCacheService; use App\Services\FollowerService; use App\Services\HashtagService; +use App\Services\Internal\BeagleService; use App\Services\LikeService; use App\Services\ReblogService; use App\Services\SnowflakeService; @@ -420,4 +421,11 @@ class DiscoverController extends Controller return response()->json($ids, 200, [], JSON_UNESCAPED_SLASHES); } + + public function discoverNetworkTrending(Request $request) + { + abort_if(! $request->user(), 404); + + return BeagleService::getDiscoverPosts(); + } } diff --git a/app/Services/Internal/BeagleService.php b/app/Services/Internal/BeagleService.php index 60a4f78e4..074f75019 100644 --- a/app/Services/Internal/BeagleService.php +++ b/app/Services/Internal/BeagleService.php @@ -2,18 +2,23 @@ namespace App\Services\Internal; -use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Http; +use App\Services\InstanceService; +use App\Services\StatusService; +use App\Util\ActivityPub\Helpers; use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\RequestException; +use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Http; class BeagleService { const DEFAULT_RULES_CACHE_KEY = 'pf:services:beagle:default_rules:v1'; + const DISCOVER_CACHE_KEY = 'pf:services:beagle:discover:v1'; + public static function getDefaultRules() { - return Cache::remember(self::DEFAULT_RULES_CACHE_KEY, now()->addDays(7), function() { + return Cache::remember(self::DEFAULT_RULES_CACHE_KEY, now()->addDays(7), function () { try { $res = Http::withOptions(['allow_redirects' => false]) ->timeout(5) @@ -28,17 +33,76 @@ class BeagleService return; } - if(!$res->ok()) { + if (! $res->ok()) { return; } $json = $res->json(); - if(!isset($json['rule_suggestions']) || !count($json['rule_suggestions'])) { + if (! isset($json['rule_suggestions']) || ! count($json['rule_suggestions'])) { return []; } + return $json['rule_suggestions']; }); } + public static function getDiscover() + { + return Cache::remember(self::DISCOVER_CACHE_KEY, now()->addHours(6), function () { + try { + $res = Http::withOptions(['allow_redirects' => false]) + ->withHeaders([ + 'X-Pixelfed-Api' => 1, + ])->timeout(5) + ->connectTimeout(5) + ->retry(2, 500) + ->get('https://beagle.pixelfed.net/api/v1/discover'); + } catch (RequestException $e) { + return; + } catch (ConnectionException $e) { + return; + } catch (Exception $e) { + return; + } + + if (! $res->ok()) { + return; + } + + $json = $res->json(); + + if (! isset($json['statuses']) || ! count($json['statuses'])) { + return []; + } + + return $json['statuses']; + }); + } + + public static function getDiscoverPosts() + { + $posts = collect(self::getDiscover()) + ->filter(function ($post) { + $bannedInstances = InstanceService::getBannedDomains(); + $domain = parse_url($post['id'], PHP_URL_HOST); + + return ! in_array($domain, $bannedInstances); + }) + ->map(function ($post) { + $domain = parse_url($post['id'], PHP_URL_HOST); + if ($domain === config_cache('pixelfed.domain.app')) { + $parts = explode('/', $post['id']); + $id = array_last($parts); + + return StatusService::get($id); + } + + return Helpers::statusFetch($post['id']); + }) + ->values() + ->toArray(); + + return $posts; + } } diff --git a/routes/api.php b/routes/api.php index 9e4ced78c..bb81af2d7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -177,6 +177,7 @@ Route::group(['prefix' => 'api'], function() use($middleware) { Route::get('accounts/popular', 'Api\ApiV1Controller@discoverAccountsPopular')->middleware($middleware); Route::get('posts/trending', 'DiscoverController@trendingApi')->middleware($middleware); Route::get('posts/hashtags', 'DiscoverController@trendingHashtags')->middleware($middleware); + Route::get('posts/network/trending', 'DiscoverController@discoverNetworkTrending')->middleware($middleware); }); Route::group(['prefix' => 'directory'], function () use($middleware) { From 18837dc15b7c2832e5fd9275246a9e83d0cd9839 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 30 Jun 2024 23:03: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 03df4d570..4c7d2b92f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - Update and refactor total local post count logic, cache value and schedule updates twice daily to eliminate the perf issue on larger instances ([4f2b8ed2](https://github.com/pixelfed/pixelfed/commit/4f2b8ed2)) - Update Media model, fix broken thumbnail/gray thumbnail bug ([e33643c2](https://github.com/pixelfed/pixelfed/commit/e33643c2)) - Update StatusController, fix unlisted post guest/ap access bug ([83098428](https://github.com/pixelfed/pixelfed/commit/83098428)) +- Update discover, add network trending using Beagle API ([2cae8b48](https://github.com/pixelfed/pixelfed/commit/2cae8b48)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.12.1 (2024-05-07)](https://github.com/pixelfed/pixelfed/compare/v0.12.0...v0.12.1)