ProcessMovePipeline.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Jobs\MovePipeline;
  3. use App\Services\ActivityPubFetchService;
  4. use Exception;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Queue\Queueable;
  7. use Illuminate\Queue\Middleware\ThrottlesExceptions;
  8. use Illuminate\Queue\Middleware\WithoutOverlapping;
  9. class ProcessMovePipeline implements ShouldQueue
  10. {
  11. use Queueable;
  12. public $target;
  13. public $activity;
  14. /**
  15. * The number of times the job may be attempted.
  16. *
  17. * @var int
  18. */
  19. public $tries = 6;
  20. /**
  21. * The maximum number of unhandled exceptions to allow before failing.
  22. *
  23. * @var int
  24. */
  25. public $maxExceptions = 3;
  26. /**
  27. * Create a new job instance.
  28. */
  29. public function __construct($target, $activity)
  30. {
  31. $this->target = $target;
  32. $this->activity = $activity;
  33. }
  34. /**
  35. * Get the middleware the job should pass through.
  36. *
  37. * @return array<int, object>
  38. */
  39. public function middleware(): array
  40. {
  41. return [
  42. new WithoutOverlapping('process-move:'.$this->target),
  43. (new ThrottlesExceptions(2, 5 * 60))->backoff(5),
  44. ];
  45. }
  46. /**
  47. * Determine the time at which the job should timeout.
  48. */
  49. public function retryUntil(): DateTime
  50. {
  51. return now()->addMinutes(15);
  52. }
  53. /**
  54. * Execute the job.
  55. */
  56. public function handle(): void
  57. {
  58. if (config('app.env') !== 'production' || (bool) config_cache('federation.activitypub.enabled') == false) {
  59. throw new Exception('Activitypub not enabled');
  60. }
  61. if (! self::checkTarget()) {
  62. throw new Exception('Invalid target');
  63. }
  64. if (! self::checkActor()) {
  65. throw new Exception('Invalid actor');
  66. }
  67. }
  68. protected function checkTarget()
  69. {
  70. $res = ActivityPubFetchService::fetchRequest($this->target, true);
  71. if (! $res || ! isset($res['alsoKnownAs'])) {
  72. return false;
  73. }
  74. $res = Helpers::profileFetch($this->target);
  75. if (! $res) {
  76. return false;
  77. }
  78. if (is_string($res['alsoKnownAs'])) {
  79. return self::lowerTrim($res['alsoKnownAs']) === self::lowerTrim($this->actor);
  80. }
  81. if (is_array($res['alsoKnownAs'])) {
  82. $map = array_map(self::lowerTrim(), $res['alsoKnownAs']);
  83. return in_array($this->actor, $map);
  84. }
  85. return false;
  86. }
  87. protected function checkActor()
  88. {
  89. $res = ActivityPubFetchService::fetchRequest($this->actor, true);
  90. if (! $res || ! isset($res['movedTo'])) {
  91. return false;
  92. }
  93. $res = Helpers::profileFetch($this->actor);
  94. if (! $res) {
  95. return false;
  96. }
  97. if (is_string($res['movedTo'])) {
  98. return self::lowerTrim($res['movedTo']) === self::lowerTrim($this->target);
  99. }
  100. return false;
  101. }
  102. protected function lowerTrim($str)
  103. {
  104. return trim(strtolower($str));
  105. }
  106. }