pixelfed/app/Jobs/MovePipeline/UnfollowLegacyAccountMovePipeline.php

112 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2024-09-07 07:57:03 +00:00
<?php
namespace App\Jobs\MovePipeline;
use App\Util\ActivityPub\Helpers;
2024-09-09 06:35:08 +00:00
use DateTime;
2024-09-07 07:57:03 +00:00
use DB;
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 UnfollowLegacyAccountMovePipeline implements ShouldQueue
{
use Queueable;
2024-09-09 10:48:36 +00:00
public $target;
2024-09-09 10:14:46 +00:00
2024-09-09 10:48:36 +00:00
public $activity;
2024-09-07 07:57:03 +00:00
2024-09-09 10:48:36 +00:00
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 6;
2024-09-09 10:14:46 +00:00
2024-09-09 10:48:36 +00:00
/**
* The maximum number of unhandled exceptions to allow before failing.
*
* @var int
*/
public $maxExceptions = 3;
2024-09-07 07:57:03 +00:00
2024-09-09 10:48:36 +00:00
/**
* Create a new job instance.
*/
public function __construct($target, $activity)
2024-09-07 07:57:03 +00:00
{
$this->target = $target;
$this->activity = $activity;
}
2024-09-09 10:48:36 +00:00
/**
* Get the middleware the job should pass through.
*
* @return array<int, object>
*/
2024-09-07 07:57:03 +00:00
public function middleware(): array
{
return [
2024-09-07 09:40:33 +00:00
new WithoutOverlapping('process-move-undo-legacy-followers:'.$this->target),
2024-09-07 07:57:03 +00:00
(new ThrottlesExceptions(2, 5 * 60))->backoff(5),
];
}
2024-09-09 10:48:36 +00:00
/**
* Determine the time at which the job should timeout.
*/
2024-09-07 07:57:03 +00:00
public function retryUntil(): DateTime
{
2024-09-09 05:50:21 +00:00
return now()->addMinutes(5);
2024-09-07 07:57:03 +00:00
}
2024-09-09 10:48:36 +00:00
/**
* Execute the job.
*/
2024-09-07 07:57:03 +00:00
public function handle(): void
{
2024-09-09 10:48:36 +00:00
if (config('app.env') !== 'production' || (bool) config_cache('federation.activitypub.enabled') == false) {
throw new Exception('Activitypub not enabled');
2024-09-09 10:12:23 +00:00
}
2024-09-07 07:57:03 +00:00
2024-09-09 10:48:36 +00:00
$target = $this->target;
$actor = $this->activity;
2024-09-07 07:57:03 +00:00
2024-09-09 10:48:36 +00:00
$targetAccount = Helpers::profileFetch($target);
$actorAccount = Helpers::profileFetch($actor);
2024-09-07 07:57:03 +00:00
2024-09-09 10:48:36 +00:00
if (! $targetAccount || ! $actorAccount) {
throw new Exception('Invalid move accounts');
2024-09-07 07:57:03 +00:00
}
$version = config('pixelfed.version');
$appUrl = config('app.url');
$userAgent = "(Pixelfed/{$version}; +{$appUrl})";
$addlHeaders = [
'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'User-Agent' => $userAgent,
];
2024-09-09 10:48:36 +00:00
$targetInbox = $actorAccount['sharedInbox'] ?? $actorAccount['inbox_url'];
$targetPid = $actorAccount['id'];
2024-09-07 07:57:03 +00:00
2024-09-09 10:48:36 +00:00
DB::table('followers')
->join('profiles', 'followers.profile_id', '=', 'profiles.id')
->where('followers.following_id', $actorAccount['id'])
->whereNotNull('profiles.user_id')
->whereNull('profiles.deleted_at')
->select('profiles.id', 'profiles.user_id', 'profiles.username', 'profiles.private_key', 'profiles.status')
->chunkById(100, function ($followers) use ($actor, $targetInbox, $targetPid) {
foreach ($followers as $follower) {
if (! $follower->id || ! $follower->private_key || ! $follower->username || ! $follower->user_id || $follower->status === 'delete') {
continue;
2024-09-09 10:48:36 +00:00
}
MoveSendUndoFollowPipeline::dispatch($follower, $targetInbox, $targetPid, $actor)->onQueue('move');
}
2024-09-09 10:48:36 +00:00
}, 'id');
2024-09-07 07:57:03 +00:00
}
}