MoveMigrateFollowersPipeline.php 5.6 KB

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