2024-04-06 04:02:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Internal;
|
|
|
|
|
2024-07-01 05:02:51 +00:00
|
|
|
use App\Services\InstanceService;
|
|
|
|
use App\Services\StatusService;
|
|
|
|
use App\Util\ActivityPub\Helpers;
|
2024-04-06 04:02:51 +00:00
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
|
|
use Illuminate\Http\Client\RequestException;
|
2024-07-01 05:02:51 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
2024-04-06 04:02:51 +00:00
|
|
|
|
|
|
|
class BeagleService
|
|
|
|
{
|
|
|
|
const DEFAULT_RULES_CACHE_KEY = 'pf:services:beagle:default_rules:v1';
|
|
|
|
|
2024-07-01 05:02:51 +00:00
|
|
|
const DISCOVER_CACHE_KEY = 'pf:services:beagle:discover:v1';
|
|
|
|
|
2024-07-01 05:29:08 +00:00
|
|
|
const DISCOVER_POSTS_CACHE_KEY = 'pf:services:beagle:discover-posts:v1';
|
|
|
|
|
2024-04-06 04:02:51 +00:00
|
|
|
public static function getDefaultRules()
|
|
|
|
{
|
2024-07-01 05:02:51 +00:00
|
|
|
return Cache::remember(self::DEFAULT_RULES_CACHE_KEY, now()->addDays(7), function () {
|
2024-04-06 04:02:51 +00:00
|
|
|
try {
|
|
|
|
$res = Http::withOptions(['allow_redirects' => false])
|
|
|
|
->timeout(5)
|
|
|
|
->connectTimeout(5)
|
|
|
|
->retry(2, 500)
|
|
|
|
->get('https://beagle.pixelfed.net/api/v1/common/suggestions/rules');
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
return;
|
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
return;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-01 05:02:51 +00:00
|
|
|
if (! $res->ok()) {
|
2024-04-06 04:02:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = $res->json();
|
|
|
|
|
2024-07-01 05:02:51 +00:00
|
|
|
if (! isset($json['rule_suggestions']) || ! count($json['rule_suggestions'])) {
|
2024-04-06 04:02:51 +00:00
|
|
|
return [];
|
|
|
|
}
|
2024-07-01 05:02:51 +00:00
|
|
|
|
2024-04-06 04:02:51 +00:00
|
|
|
return $json['rule_suggestions'];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-01 05:02:51 +00:00
|
|
|
public static function getDiscover()
|
|
|
|
{
|
2024-08-18 07:59:24 +00:00
|
|
|
if ((bool) config_cache('federation.activitypub.enabled') == false) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-10-06 05:09:33 +00:00
|
|
|
if ((bool) config('instance.discover.beagle_api') == false) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-07-01 05:02:51 +00:00
|
|
|
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()
|
|
|
|
{
|
2024-08-18 07:59:24 +00:00
|
|
|
if ((bool) config_cache('federation.activitypub.enabled') == false) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-10-06 05:09:33 +00:00
|
|
|
if ((bool) config('instance.discover.beagle_api') == false) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-07-01 05:29:08 +00:00
|
|
|
return Cache::remember(self::DISCOVER_POSTS_CACHE_KEY, now()->addHours(1), function () {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$post = Helpers::statusFetch($post['id']);
|
|
|
|
if (! $post) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$id = $post->id;
|
2024-07-01 05:02:51 +00:00
|
|
|
|
|
|
|
return StatusService::get($id);
|
2024-07-01 05:29:08 +00:00
|
|
|
})
|
|
|
|
->filter()
|
|
|
|
->values()
|
|
|
|
->toArray();
|
2024-07-01 05:02:51 +00:00
|
|
|
|
2024-07-01 05:29:08 +00:00
|
|
|
return $posts;
|
|
|
|
});
|
2024-07-01 05:02:51 +00:00
|
|
|
}
|
2024-04-06 04:02:51 +00:00
|
|
|
}
|