Update discover, add network trending using Beagle API

This commit is contained in:
Daniel Supernault 2024-06-30 23:02:51 -06:00
parent ecf22b54d5
commit 2cae8b48de
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
3 changed files with 78 additions and 5 deletions

View file

@ -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();
}
}

View file

@ -2,15 +2,20 @@
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 () {
@ -37,8 +42,67 @@ class BeagleService
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;
}
}

View file

@ -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) {