ProcessMovePipeline.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Jobs\MovePipeline;
  3. use App\Services\ActivityPubFetchService;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Queue\Queueable;
  6. use Illuminate\Queue\Middleware\WithoutOverlapping;
  7. class ProcessMovePipeline implements ShouldQueue
  8. {
  9. use Queueable;
  10. public $target;
  11. public $activity;
  12. /**
  13. * Create a new job instance.
  14. */
  15. public function __construct($target, $activity)
  16. {
  17. $this->target = $target;
  18. $this->activity = $activity;
  19. }
  20. /**
  21. * Get the middleware the job should pass through.
  22. *
  23. * @return array<int, object>
  24. */
  25. public function middleware(): array
  26. {
  27. return [new WithoutOverlapping($this->target)];
  28. }
  29. /**
  30. * Execute the job.
  31. */
  32. public function handle(): void
  33. {
  34. if (! self::checkTarget()) {
  35. return;
  36. }
  37. if (! self::checkActor()) {
  38. return;
  39. }
  40. }
  41. protected function checkTarget()
  42. {
  43. $res = ActivityPubFetchService::fetchRequest($this->target, true);
  44. if (! $res || ! isset($res['alsoKnownAs'])) {
  45. return false;
  46. }
  47. $res = Helpers::profileFetch($this->target);
  48. if (! $res) {
  49. return false;
  50. }
  51. if (is_string($res['alsoKnownAs'])) {
  52. return self::lowerTrim($res['alsoKnownAs']) === self::lowerTrim($this->actor);
  53. }
  54. if (is_array($res['alsoKnownAs'])) {
  55. $map = array_map(self::lowerTrim(), $res['alsoKnownAs']);
  56. return in_array($this->actor, $map);
  57. }
  58. return false;
  59. }
  60. protected function checkActor()
  61. {
  62. $res = ActivityPubFetchService::fetchRequest($this->actor, true);
  63. if (! $res || ! isset($res['movedTo'])) {
  64. return false;
  65. }
  66. $res = Helpers::profileFetch($this->actor);
  67. if (! $res) {
  68. return false;
  69. }
  70. if (is_string($res['movedTo'])) {
  71. return self::lowerTrim($res['movedTo']) === self::lowerTrim($this->target);
  72. }
  73. return false;
  74. }
  75. protected function lowerTrim($str)
  76. {
  77. return trim(strtolower($str));
  78. }
  79. }