2024-09-05 06:41:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\MovePipeline;
|
|
|
|
|
|
|
|
use App\Services\ActivityPubFetchService;
|
2024-09-09 06:35:08 +00:00
|
|
|
use App\Util\ActivityPub\Helpers;
|
2024-09-09 05:14:49 +00:00
|
|
|
use DateTime;
|
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;
|
2024-09-09 05:14:49 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2024-09-07 10:18:07 +00:00
|
|
|
use Log;
|
2024-09-05 06:41:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2024-09-09 05:50:21 +00:00
|
|
|
return now()->addMinutes(5);
|
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) {
|
2024-09-09 06:04:19 +00:00
|
|
|
Log::info('pmp: AP not enabled');
|
2024-09-07 07:08:39 +00:00
|
|
|
throw new Exception('Activitypub not enabled');
|
|
|
|
}
|
|
|
|
|
2024-09-09 06:22:24 +00:00
|
|
|
$validTarget = $this->checkTarget();
|
|
|
|
if (! $validTarget) {
|
2024-09-09 06:04:19 +00:00
|
|
|
Log::info('pmp: invalid target');
|
2024-09-07 07:08:39 +00:00
|
|
|
throw new Exception('Invalid target');
|
2024-09-05 06:41:59 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 06:22:24 +00:00
|
|
|
$validActor = $this->checkActor();
|
|
|
|
if (! $validActor) {
|
2024-09-09 06:04:19 +00:00
|
|
|
Log::info('pmp: invalid actor');
|
2024-09-07 07:08:39 +00:00
|
|
|
throw new Exception('Invalid actor');
|
2024-09-05 06:41:59 +00:00
|
|
|
}
|
2024-09-09 06:22:24 +00:00
|
|
|
|
2024-09-05 06:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkTarget()
|
|
|
|
{
|
|
|
|
$res = ActivityPubFetchService::fetchRequest($this->target, true);
|
|
|
|
|
|
|
|
if (! $res || ! isset($res['alsoKnownAs'])) {
|
2024-09-07 10:18:07 +00:00
|
|
|
Log::info('[AP][INBOX][MOVE] target_aka failure');
|
2024-09-09 05:14:49 +00:00
|
|
|
|
2024-09-05 06:41:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = Helpers::profileFetch($this->target);
|
|
|
|
if (! $res) {
|
2024-09-07 10:18:07 +00:00
|
|
|
Log::info('[AP][INBOX][MOVE] target fetch failure');
|
2024-09-09 05:14:49 +00:00
|
|
|
|
2024-09-05 06:41:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($res['alsoKnownAs'])) {
|
2024-09-09 06:22:24 +00:00
|
|
|
return $this->lowerTrim($res['alsoKnownAs']) === $this->lowerTrim($this->activity);
|
2024-09-05 06:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($res['alsoKnownAs'])) {
|
2024-09-09 05:14:49 +00:00
|
|
|
$map = Arr::map($res['alsoKnownAs'], function ($value, $key) {
|
|
|
|
return trim(strtolower($value));
|
|
|
|
});
|
2024-09-05 06:41:59 +00:00
|
|
|
|
2024-09-09 05:50:21 +00:00
|
|
|
$res = in_array($this->activity, $map);
|
2024-09-09 05:14:49 +00:00
|
|
|
$debugMessage = $res ? '[AP][INBOX][MOVE] aka target is valid' : '[AP][INBOX][MOVE] aka target is invalid';
|
|
|
|
|
|
|
|
Log::info($debugMessage);
|
|
|
|
|
|
|
|
return $res;
|
2024-09-05 06:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkActor()
|
|
|
|
{
|
2024-09-09 05:50:21 +00:00
|
|
|
$res = ActivityPubFetchService::fetchRequest($this->activity, true);
|
2024-09-05 06:41:59 +00:00
|
|
|
|
2024-09-09 06:22:24 +00:00
|
|
|
if (! $res || ! isset($res['movedTo']) || empty($res['movedTo'])) {
|
2024-09-07 10:18:07 +00:00
|
|
|
Log::info('[AP][INBOX][MOVE] actor_movedTo failure');
|
2024-09-09 05:14:49 +00:00
|
|
|
|
2024-09-05 06:41:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-09-09 05:50:21 +00:00
|
|
|
$res = Helpers::profileFetch($this->activity);
|
2024-09-05 06:41:59 +00:00
|
|
|
if (! $res) {
|
2024-09-07 10:18:07 +00:00
|
|
|
Log::info('[AP][INBOX][MOVE] actor fetch failure');
|
2024-09-09 05:14:49 +00:00
|
|
|
|
2024-09-05 06:41:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($res['movedTo'])) {
|
2024-09-09 06:22:24 +00:00
|
|
|
$match = $this->lowerTrim($res['movedTo']) === $this->lowerTrim($this->target);
|
|
|
|
if (! $match) {
|
|
|
|
$msg = json_encode([
|
|
|
|
'movedTo' => $res['movedTo'],
|
|
|
|
'target' => $this->target,
|
|
|
|
]);
|
|
|
|
Log::info('[AP][INBOX][MOVE] invalid actor match.'.$msg);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $match;
|
2024-09-05 06:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function lowerTrim($str)
|
|
|
|
{
|
|
|
|
return trim(strtolower($str));
|
|
|
|
}
|
|
|
|
}
|