UnfollowLegacyAccountMovePipeline.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Jobs\MovePipeline;
  3. use App\Util\ActivityPub\Helpers;
  4. use App\Util\ActivityPub\HttpSignature;
  5. use DateTime;
  6. use DB;
  7. use Exception;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Pool;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Queue\Queueable;
  12. use Illuminate\Queue\Middleware\ThrottlesExceptions;
  13. use Illuminate\Queue\Middleware\WithoutOverlapping;
  14. class UnfollowLegacyAccountMovePipeline implements ShouldQueue
  15. {
  16. use Queueable;
  17. public $target;
  18. public $activity;
  19. /**
  20. * The number of times the job may be attempted.
  21. *
  22. * @var int
  23. */
  24. public $tries = 6;
  25. /**
  26. * The maximum number of unhandled exceptions to allow before failing.
  27. *
  28. * @var int
  29. */
  30. public $maxExceptions = 3;
  31. /**
  32. * Create a new job instance.
  33. */
  34. public function __construct($target, $activity)
  35. {
  36. $this->target = $target;
  37. $this->activity = $activity;
  38. }
  39. /**
  40. * Get the middleware the job should pass through.
  41. *
  42. * @return array<int, object>
  43. */
  44. public function middleware(): array
  45. {
  46. return [
  47. new WithoutOverlapping('process-move-undo-legacy-followers:'.$this->target),
  48. (new ThrottlesExceptions(2, 5 * 60))->backoff(5),
  49. ];
  50. }
  51. /**
  52. * Determine the time at which the job should timeout.
  53. */
  54. public function retryUntil(): DateTime
  55. {
  56. return now()->addMinutes(5);
  57. }
  58. /**
  59. * Execute the job.
  60. */
  61. public function handle(): void
  62. {
  63. if (config('app.env') !== 'production' || (bool) config_cache('federation.activitypub.enabled') == false) {
  64. throw new Exception('Activitypub not enabled');
  65. }
  66. $target = $this->target;
  67. $actor = $this->activity;
  68. $targetAccount = Helpers::profileFetch($target);
  69. $actorAccount = Helpers::profileFetch($actor);
  70. if (! $targetAccount || ! $actorAccount) {
  71. throw new Exception('Invalid move accounts');
  72. }
  73. $version = config('pixelfed.version');
  74. $appUrl = config('app.url');
  75. $userAgent = "(Pixelfed/{$version}; +{$appUrl})";
  76. $addlHeaders = [
  77. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  78. 'User-Agent' => $userAgent,
  79. ];
  80. $targetInbox = $actorAccount['sharedInbox'] ?? $actorAccount['inbox_url'];
  81. $targetPid = $actorAccount['id'];
  82. DB::table('followers')
  83. ->join('profiles', 'followers.profile_id', '=', 'profiles.id')
  84. ->where('followers.following_id', $actorAccount['id'])
  85. ->whereNotNull('profiles.user_id')
  86. ->whereNull('profiles.deleted_at')
  87. ->select('profiles.id', 'profiles.user_id', 'profiles.username', 'profiles.private_key', 'profiles.status')
  88. ->chunkById(100, function ($followers) use ($actor, $addlHeaders, $targetInbox, $targetPid) {
  89. $client = new Client([
  90. 'timeout' => config('federation.activitypub.delivery.timeout'),
  91. ]);
  92. $requests = function ($followers) use ($client, $actor, $addlHeaders, $targetInbox, $targetPid) {
  93. $activity = [
  94. '@context' => 'https://www.w3.org/ns/activitystreams',
  95. 'type' => 'Undo',
  96. 'id' => null,
  97. 'actor' => null,
  98. 'object' => [
  99. 'type' => 'Follow',
  100. 'id' => null,
  101. 'object' => $actor,
  102. 'actor' => null,
  103. ],
  104. ];
  105. foreach ($followers as $follower) {
  106. if (! $follower->private_key || ! $follower->username || ! $follower->user_id || $follower->status === 'delete') {
  107. continue;
  108. }
  109. $permalink = 'https://'.config('pixelfed.domain.app').'/users/'.$follower->username;
  110. $activity['id'] = $permalink.'#follow/'.$targetPid.'/undo';
  111. $activity['actor'] = $permalink;
  112. $activity['object']['id'] = $permalink.'#follows/'.$targetPid;
  113. $activity['object']['actor'] = $permalink;
  114. $keyId = $permalink.'#main-key';
  115. $payload = json_encode($activity);
  116. $url = $targetInbox;
  117. $headers = HttpSignature::signRaw($follower->private_key, $keyId, $targetInbox, $activity, $addlHeaders);
  118. yield function () use ($client, $url, $headers, $payload) {
  119. return $client->postAsync($url, [
  120. 'curl' => [
  121. CURLOPT_HTTPHEADER => $headers,
  122. CURLOPT_POSTFIELDS => $payload,
  123. CURLOPT_HEADER => true,
  124. ],
  125. ]);
  126. };
  127. }
  128. };
  129. $pool = new Pool($client, $requests($followers), [
  130. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  131. 'fulfilled' => function ($response, $index) {},
  132. 'rejected' => function ($reason, $index) {},
  133. ]);
  134. $promise = $pool->promise();
  135. $promise->wait();
  136. }, 'id');
  137. }
  138. }