BeagleService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Services\Internal;
  3. use App\Services\InstanceService;
  4. use App\Services\StatusService;
  5. use App\Util\ActivityPub\Helpers;
  6. use Illuminate\Http\Client\ConnectionException;
  7. use Illuminate\Http\Client\RequestException;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\Http;
  10. class BeagleService
  11. {
  12. const DEFAULT_RULES_CACHE_KEY = 'pf:services:beagle:default_rules:v1';
  13. const DISCOVER_CACHE_KEY = 'pf:services:beagle:discover:v1';
  14. const DISCOVER_POSTS_CACHE_KEY = 'pf:services:beagle:discover-posts:v1';
  15. public static function getDefaultRules()
  16. {
  17. return Cache::remember(self::DEFAULT_RULES_CACHE_KEY, now()->addDays(7), function () {
  18. try {
  19. $res = Http::withOptions(['allow_redirects' => false])
  20. ->timeout(5)
  21. ->connectTimeout(5)
  22. ->retry(2, 500)
  23. ->get('https://beagle.pixelfed.net/api/v1/common/suggestions/rules');
  24. } catch (RequestException $e) {
  25. return;
  26. } catch (ConnectionException $e) {
  27. return;
  28. } catch (Exception $e) {
  29. return;
  30. }
  31. if (! $res->ok()) {
  32. return;
  33. }
  34. $json = $res->json();
  35. if (! isset($json['rule_suggestions']) || ! count($json['rule_suggestions'])) {
  36. return [];
  37. }
  38. return $json['rule_suggestions'];
  39. });
  40. }
  41. public static function getDiscover()
  42. {
  43. if ((bool) config_cache('federation.activitypub.enabled') == false) {
  44. return [];
  45. }
  46. if ((bool) config('instance.discover.beagle_api') == false) {
  47. return [];
  48. }
  49. return Cache::remember(self::DISCOVER_CACHE_KEY, now()->addHours(6), function () {
  50. try {
  51. $res = Http::withOptions(['allow_redirects' => false])
  52. ->withHeaders([
  53. 'X-Pixelfed-Api' => 1,
  54. ])->timeout(5)
  55. ->connectTimeout(5)
  56. ->retry(2, 500)
  57. ->get('https://beagle.pixelfed.net/api/v1/discover');
  58. } catch (RequestException $e) {
  59. return;
  60. } catch (ConnectionException $e) {
  61. return;
  62. } catch (Exception $e) {
  63. return;
  64. }
  65. if (! $res->ok()) {
  66. return;
  67. }
  68. $json = $res->json();
  69. if (! isset($json['statuses']) || ! count($json['statuses'])) {
  70. return [];
  71. }
  72. return $json['statuses'];
  73. });
  74. }
  75. public static function getDiscoverPosts()
  76. {
  77. if ((bool) config_cache('federation.activitypub.enabled') == false) {
  78. return [];
  79. }
  80. if ((bool) config('instance.discover.beagle_api') == false) {
  81. return [];
  82. }
  83. return Cache::remember(self::DISCOVER_POSTS_CACHE_KEY, now()->addHours(1), function () {
  84. $posts = collect(self::getDiscover())
  85. ->filter(function ($post) {
  86. $bannedInstances = InstanceService::getBannedDomains();
  87. $domain = parse_url($post['id'], PHP_URL_HOST);
  88. return ! in_array($domain, $bannedInstances);
  89. })
  90. ->map(function ($post) {
  91. $domain = parse_url($post['id'], PHP_URL_HOST);
  92. if ($domain === config_cache('pixelfed.domain.app')) {
  93. $parts = explode('/', $post['id']);
  94. $id = array_last($parts);
  95. return StatusService::get($id);
  96. }
  97. $post = Helpers::statusFetch($post['id']);
  98. if (! $post) {
  99. return;
  100. }
  101. $id = $post->id;
  102. return StatusService::get($id);
  103. })
  104. ->filter()
  105. ->values()
  106. ->toArray();
  107. return $posts;
  108. });
  109. }
  110. }