pixelfed/app/Jobs/MovePipeline/UnfollowLegacyAccountMovePipeline.php

174 lines
5.6 KiB
PHP
Raw Normal View History

2024-09-07 07:57:03 +00:00
<?php
namespace App\Jobs\MovePipeline;
use App\Util\ActivityPub\Helpers;
use App\Util\ActivityPub\HttpSignature;
2024-09-09 06:35:08 +00:00
use DateTime;
2024-09-07 07:57:03 +00:00
use DB;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
2024-09-09 10:12:23 +00:00
use GuzzleHttp\Psr7\Request;
2024-09-07 07:57:03 +00:00
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;
2024-09-09 10:12:23 +00:00
use Illuminate\Support\Facades\Log;
2024-09-07 07:57:03 +00:00
class UnfollowLegacyAccountMovePipeline implements ShouldQueue
{
use Queueable;
2024-09-09 10:12:23 +00:00
public string $target;
2024-09-09 10:14:46 +00:00
2024-09-09 10:12:23 +00:00
public string $activity;
2024-09-07 07:57:03 +00:00
2024-09-09 10:12:23 +00:00
public int $tries = 6;
2024-09-09 10:14:46 +00:00
2024-09-09 10:12:23 +00:00
public int $maxExceptions = 3;
2024-09-07 07:57:03 +00:00
2024-09-09 10:12:23 +00:00
public function __construct(string $target, string $activity)
2024-09-07 07:57:03 +00:00
{
$this->target = $target;
$this->activity = $activity;
}
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),
];
}
public function retryUntil(): DateTime
{
2024-09-09 05:50:21 +00:00
return now()->addMinutes(5);
2024-09-07 07:57:03 +00:00
}
public function handle(): void
{
2024-09-09 10:12:23 +00:00
try {
$this->validateEnvironment();
$targetAccount = $this->fetchProfile($this->target);
$actorAccount = $this->fetchProfile($this->activity);
2024-09-09 10:14:46 +00:00
if (! $targetAccount || ! $actorAccount) {
2024-09-09 10:12:23 +00:00
throw new Exception('Invalid move accounts');
}
$client = $this->createHttpClient();
$targetInbox = $actorAccount['sharedInbox'] ?? $actorAccount['inbox_url'];
$targetPid = $actorAccount['id'];
$this->processFollowers($client, $targetInbox, $targetPid);
} catch (Exception $e) {
Log::error('UnfollowLegacyAccountMovePipeline failed', [
'target' => $this->target,
'activity' => $this->activity,
2024-09-09 10:14:46 +00:00
'error' => $e->getMessage(),
2024-09-09 10:12:23 +00:00
]);
throw $e;
}
}
private function validateEnvironment(): void
{
2024-09-09 10:14:46 +00:00
if (config('app.env') !== 'production' || ! (bool) config('federation.activitypub.enabled')) {
2024-09-09 10:12:23 +00:00
throw new Exception('ActivityPub not enabled');
2024-09-07 07:57:03 +00:00
}
2024-09-09 10:12:23 +00:00
}
private function fetchProfile(string $url): ?array
{
return Helpers::profileFetch($url);
}
private function createHttpClient(): Client
{
return new Client([
'timeout' => config('federation.activitypub.delivery.timeout'),
]);
}
private function processFollowers(Client $client, string $targetInbox, int $targetPid): void
{
DB::table('followers')
->join('profiles', 'followers.profile_id', '=', 'profiles.id')
->where('followers.following_id', $targetPid)
->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 ($client, $targetInbox, $targetPid) {
$this->processFollowerChunk($followers, $client, $targetInbox, $targetPid);
}, 'id');
}
2024-09-07 07:57:03 +00:00
2024-09-09 10:12:23 +00:00
private function processFollowerChunk($followers, Client $client, string $targetInbox, int $targetPid): void
{
$requests = $this->generateRequests($followers, $targetInbox, $targetPid);
$pool = new Pool($client, $requests, [
'concurrency' => config('federation.activitypub.delivery.concurrency'),
'fulfilled' => function ($response, $index) {
// Log success if needed
},
'rejected' => function ($reason, $index) {
Log::error('Failed to process unfollow', ['reason' => $reason, 'index' => $index]);
},
]);
$pool->promise()->wait();
}
2024-09-07 07:57:03 +00:00
2024-09-09 10:12:23 +00:00
private function generateRequests($followers, string $targetInbox, int $targetPid): \Generator
{
foreach ($followers as $follower) {
2024-09-09 10:14:46 +00:00
if (! $this->isValidFollower($follower)) {
2024-09-09 10:12:23 +00:00
continue;
}
2024-09-07 07:57:03 +00:00
2024-09-09 10:12:23 +00:00
yield $this->createUnfollowRequest($follower, $targetInbox, $targetPid);
2024-09-07 07:57:03 +00:00
}
2024-09-09 10:12:23 +00:00
}
private function isValidFollower($follower): bool
{
return $follower->private_key && $follower->username && $follower->user_id && $follower->status !== 'delete';
}
private function createUnfollowRequest($follower, string $targetInbox, int $targetPid): Request
{
$permalink = 'https://'.config('pixelfed.domain.app').'/users/'.$follower->username;
$activity = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Undo',
'id' => $permalink.'#follow/'.$targetPid.'/undo',
'actor' => $permalink,
'object' => [
'type' => 'Follow',
'id' => $permalink.'#follows/'.$targetPid,
'object' => $this->activity,
'actor' => $permalink,
],
];
$keyId = $permalink.'#main-key';
$payload = json_encode($activity);
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:12:23 +00:00
$headers = HttpSignature::signRaw($follower->private_key, $keyId, $targetInbox, $activity, $addlHeaders);
return new Request('POST', $targetInbox, $headers, $payload);
2024-09-07 07:57:03 +00:00
}
}