CleanupLegacyAccountMovePipeline.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Jobs\MovePipeline;
  3. use App\Follower;
  4. use App\Profile;
  5. use App\Services\AccountService;
  6. use App\UserFilter;
  7. use App\Util\ActivityPub\Helpers;
  8. use DateTime;
  9. use Exception;
  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 CleanupLegacyAccountMovePipeline 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-cleanup-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. UserFilter::where('filterable_type', 'App\Profile')
  74. ->where('filterable_id', $actorAccount['id'])
  75. ->update(['filterable_id' => $targetAccount['id']]);
  76. Follower::whereFollowingId($actorAccount['id'])->delete();
  77. $oldProfile = Profile::find($actorAccount['id']);
  78. if ($oldProfile) {
  79. $oldProfile->moved_to_profile_id = $targetAccount['id'];
  80. $oldProfile->save();
  81. AccountService::del($oldProfile->id);
  82. AccountService::del($targetAccount['id']);
  83. }
  84. }
  85. }