pixelfed/app/Jobs/MovePipeline/CleanupLegacyAccountMovePipeline.php

104 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2024-09-07 07:57:03 +00:00
<?php
namespace App\Jobs\MovePipeline;
use App\Follower;
use App\Profile;
use App\Services\AccountService;
use App\UserFilter;
2024-09-07 07:57:03 +00:00
use App\Util\ActivityPub\Helpers;
2024-09-09 06:35:08 +00:00
use DateTime;
2024-09-07 07:57:03 +00:00
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
2024-09-09 06:35:08 +00:00
use Illuminate\Queue\Middleware\ThrottlesExceptions;
2024-09-07 07:57:03 +00:00
use Illuminate\Queue\Middleware\WithoutOverlapping;
class CleanupLegacyAccountMovePipeline implements ShouldQueue
{
use Queueable;
public $target;
public $activity;
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 6;
/**
* The maximum number of unhandled exceptions to allow before failing.
*
* @var int
*/
public $maxExceptions = 3;
/**
* Create a new job instance.
*/
public function __construct($target, $activity)
{
$this->target = $target;
$this->activity = $activity;
}
/**
* Get the middleware the job should pass through.
*
* @return array<int, object>
*/
public function middleware(): array
{
return [
2024-09-07 09:40:33 +00:00
new WithoutOverlapping('process-move-cleanup-legacy-followers:'.$this->target),
2024-09-07 07:57:03 +00:00
(new ThrottlesExceptions(2, 5 * 60))->backoff(5),
];
}
/**
* Determine the time at which the job should timeout.
*/
public function retryUntil(): DateTime
{
2024-09-09 05:50:21 +00:00
return now()->addMinutes(5);
2024-09-07 07:57:03 +00:00
}
/**
* Execute the job.
*/
public function handle(): void
{
if (config('app.env') !== 'production' || (bool) config_cache('federation.activitypub.enabled') == false) {
throw new Exception('Activitypub not enabled');
}
$target = $this->target;
$actor = $this->activity;
$targetAccount = Helpers::profileFetch($target);
$actorAccount = Helpers::profileFetch($actor);
if (! $targetAccount || ! $actorAccount) {
throw new Exception('Invalid move accounts');
}
UserFilter::where('filterable_type', 'App\Profile')
->where('filterable_id', $actorAccount['id'])
->update(['filterable_id' => $targetAccount['id']]);
2024-09-07 07:57:03 +00:00
Follower::whereFollowingId($actorAccount['id'])->delete();
$oldProfile = Profile::find($actorAccount['id']);
if ($oldProfile) {
$oldProfile->moved_to_profile_id = $targetAccount['id'];
$oldProfile->save();
AccountService::del($oldProfile->id);
AccountService::del($targetAccount['id']);
}
2024-09-07 07:57:03 +00:00
}
}