You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pixelfed/app/Jobs/StoryPipeline/StoryDelete.php

137 lines
3.6 KiB
PHTML

<?php
namespace App\Jobs\StoryPipeline;
9 months ago
use App\Services\FollowerService;
use App\Services\StoryService;
use App\Story;
use App\Util\ActivityPub\HttpSignature;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
4 weeks ago
use Illuminate\Http\Client\Pool;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
4 weeks ago
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
class StoryDelete implements ShouldQueue
{
9 months ago
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $story;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Story $story)
{
$this->story = $story;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$story = $this->story;
if ($story->local == false) {
return;
}
StoryService::removeRotateQueue($story->id);
StoryService::delLatest($story->profile_id);
StoryService::delById($story->id);
if (Storage::exists($story->path) == true) {
Storage::delete($story->path);
}
$story->views()->delete();
$profile = $story->profile;
$activity = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $story->url().'#delete',
'type' => 'Delete',
'actor' => $profile->permalink(),
'object' => [
'id' => $story->url(),
'type' => 'Story',
],
];
$this->fanoutExpiry($profile, $activity);
// delete notifications
// delete polls
// delete reports
$story->delete();
}
protected function fanoutExpiry($profile, $activity)
{
$audience = FollowerService::softwareAudience($profile->id, 'pixelfed');
if (empty($audience)) {
return;
}
$payload = json_encode($activity);
4 weeks ago
$version = config('pixelfed.version');
$appUrl = config('app.url');
$userAgent = "(Pixelfed/{$version}; +{$appUrl})";
$timeout = config('federation.activitypub.delivery.timeout');
9 months ago
4 weeks ago
Http::pool(function (Pool $pool) use ($audience, $activity, $profile, $payload, $userAgent, $timeout) {
9 months ago
foreach ($audience as $url) {
4 weeks ago
$curlHeaders = HttpSignature::sign($profile, $url, $activity, [
9 months ago
'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
4 weeks ago
'User-Agent' => $userAgent,
9 months ago
]);
4 weeks ago
$headers = $this->parseCurlHeaders($curlHeaders);
$pool->withHeaders($headers)
->timeout($timeout)
->withBody($payload, 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"')
->post($url);
}
});
}
9 months ago
4 weeks ago
/**
* Convert curl-format header array ("Header: value") to associative array.
*
* @param array<int, string> $curlHeaders
* @return array<string, string>
*/
private function parseCurlHeaders(array $curlHeaders): array
{
$headers = [];
foreach ($curlHeaders as $header) {
$parts = explode(': ', $header, 2);
if (count($parts) === 2) {
$headers[$parts[0]] = $parts[1];
}
}
9 months ago
4 weeks ago
return $headers;
9 months ago
}
}