pixelfed/app/Jobs/ProfilePipeline/ProfileMigrationMoveFollowe...

96 lines
2.4 KiB
PHP
Raw Normal View History

2024-03-02 11:21:04 +00:00
<?php
namespace App\Jobs\ProfilePipeline;
2024-03-05 06:16:32 +00:00
use App\Follower;
use App\Profile;
use App\Services\AccountService;
use Illuminate\Bus\Batchable;
2024-03-02 11:21:04 +00:00
use Illuminate\Bus\Queueable;
2024-03-05 06:16:32 +00:00
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
2024-03-02 11:21:04 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
2024-03-05 06:16:32 +00:00
use Illuminate\Queue\Middleware\WithoutOverlapping;
2024-03-02 11:21:04 +00:00
use Illuminate\Queue\SerializesModels;
2024-03-05 06:16:32 +00:00
class ProfileMigrationMoveFollowersPipeline implements ShouldBeUniqueUntilProcessing, ShouldQueue
2024-03-02 11:21:04 +00:00
{
2024-03-05 06:16:32 +00:00
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2024-03-02 11:21:04 +00:00
public $oldPid;
2024-03-05 06:16:32 +00:00
2024-03-02 11:21:04 +00:00
public $newPid;
2024-03-05 06:16:32 +00:00
public $timeout = 1400;
public $tries = 3;
public $maxExceptions = 1;
public $failOnTimeout = true;
/**
* The number of seconds after which the job's unique lock will be released.
*
* @var int
*/
public $uniqueFor = 3600;
/**
* Get the unique ID for the job.
*/
public function uniqueId(): string
{
return 'profile:migration:move-followers:oldpid-'.$this->oldPid.':newpid-'.$this->newPid;
}
/**
* Get the middleware the job should pass through.
*
* @return array<int, object>
*/
public function middleware(): array
{
return [(new WithoutOverlapping('profile:migration:move-followers:oldpid-'.$this->oldPid.':newpid-'.$this->newPid))->shared()->dontRelease()];
}
2024-03-02 11:21:04 +00:00
/**
* Create a new job instance.
*/
public function __construct($oldPid, $newPid)
{
$this->oldPid = $oldPid;
$this->newPid = $newPid;
}
/**
* Execute the job.
*/
public function handle(): void
{
2024-03-05 06:16:32 +00:00
if ($this->batch()->cancelled()) {
return;
}
2024-03-02 11:21:04 +00:00
$og = Profile::find($this->oldPid);
$ne = Profile::find($this->newPid);
2024-03-05 06:16:32 +00:00
if (! $og || ! $ne || $og == $ne) {
2024-03-02 11:21:04 +00:00
return;
}
$ne->followers_count = $og->followers_count;
$ne->save();
$og->followers_count = 0;
$og->save();
foreach (Follower::whereFollowingId($this->oldPid)->lazyById(200, 'id') as $follower) {
try {
$follower->following_id = $this->newPid;
$follower->save();
} catch (Exception $e) {
$follower->delete();
}
}
AccountService::del($this->oldPid);
AccountService::del($this->newPid);
}
}