ProcessMovePipeline.php 4.2 KB

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