ProcessMovePipeline.php 3.7 KB

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