pixelfed/app/Jobs/MovePipeline/ProcessMovePipeline.php

134 lines
3.0 KiB
PHP
Raw Normal View History

2024-09-05 06:41:59 +00:00
<?php
namespace App\Jobs\MovePipeline;
use App\Services\ActivityPubFetchService;
2024-09-07 07:08:39 +00:00
use Exception;
2024-09-05 06:41:59 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
2024-09-07 07:08:39 +00:00
use Illuminate\Queue\Middleware\ThrottlesExceptions;
2024-09-05 06:41:59 +00:00
use Illuminate\Queue\Middleware\WithoutOverlapping;
class ProcessMovePipeline implements ShouldQueue
{
use Queueable;
public $target;
public $activity;
2024-09-07 07:08:39 +00:00
/**
* 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;
2024-09-05 06:41:59 +00:00
/**
* 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
{
2024-09-07 07:08:39 +00:00
return [
new WithoutOverlapping('process-move:'.$this->target),
(new ThrottlesExceptions(2, 5 * 60))->backoff(5),
];
}
/**
* Determine the time at which the job should timeout.
*/
public function retryUntil(): DateTime
{
return now()->addMinutes(15);
2024-09-05 06:41:59 +00:00
}
/**
* Execute the job.
*/
public function handle(): void
{
2024-09-07 07:08:39 +00:00
if (config('app.env') !== 'production' || (bool) config_cache('federation.activitypub.enabled') == false) {
throw new Exception('Activitypub not enabled');
}
2024-09-05 06:41:59 +00:00
if (! self::checkTarget()) {
2024-09-07 07:08:39 +00:00
throw new Exception('Invalid target');
2024-09-05 06:41:59 +00:00
}
if (! self::checkActor()) {
2024-09-07 07:08:39 +00:00
throw new Exception('Invalid actor');
2024-09-05 06:41:59 +00:00
}
2024-09-07 07:08:39 +00:00
2024-09-05 06:41:59 +00:00
}
protected function checkTarget()
{
$res = ActivityPubFetchService::fetchRequest($this->target, true);
if (! $res || ! isset($res['alsoKnownAs'])) {
return false;
}
$res = Helpers::profileFetch($this->target);
if (! $res) {
return false;
}
if (is_string($res['alsoKnownAs'])) {
return self::lowerTrim($res['alsoKnownAs']) === self::lowerTrim($this->actor);
}
if (is_array($res['alsoKnownAs'])) {
$map = array_map(self::lowerTrim(), $res['alsoKnownAs']);
return in_array($this->actor, $map);
}
return false;
}
protected function checkActor()
{
$res = ActivityPubFetchService::fetchRequest($this->actor, true);
if (! $res || ! isset($res['movedTo'])) {
return false;
}
$res = Helpers::profileFetch($this->actor);
if (! $res) {
return false;
}
if (is_string($res['movedTo'])) {
return self::lowerTrim($res['movedTo']) === self::lowerTrim($this->target);
}
return false;
}
protected function lowerTrim($str)
{
return trim(strtolower($str));
}
}