1
0
Fork 0
pixelfed/app/Jobs/StatusPipeline/StatusDelete.php

227 lines
5.7 KiB
PHP
Raw Normal View History

2018-06-01 21:04:33 +00:00
<?php
namespace App\Jobs\StatusPipeline;
use DB, Cache, Storage;
2018-11-04 03:24:50 +00:00
use App\{
2021-05-12 00:17:03 +00:00
AccountInterstitial,
Bookmark,
CollectionItem,
DirectMessage,
Like,
Media,
2021-05-12 00:17:03 +00:00
MediaTag,
Mention,
2021-05-12 00:17:03 +00:00
Notification,
Report,
Status,
StatusArchived,
2021-05-12 00:17:03 +00:00
StatusHashtag,
StatusView
2018-11-04 03:24:50 +00:00
};
2018-06-01 21:04:33 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2018-08-28 03:07:36 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use League\Fractal;
use Illuminate\Support\Str;
use League\Fractal\Serializer\ArraySerializer;
use App\Transformer\ActivityPub\Verb\DeleteNote;
use App\Util\ActivityPub\Helpers;
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use App\Util\ActivityPub\HttpSignature;
use App\Services\CollectionService;
use App\Services\StatusService;
use App\Services\NotificationService;
use App\Jobs\MediaPipeline\MediaDeletePipeline;
2018-06-01 21:04:33 +00:00
class StatusDelete implements ShouldQueue
{
2021-05-12 00:17:03 +00:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $status;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
public $timeout = 900;
public $tries = 2;
2021-05-12 00:17:03 +00:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Status $status)
{
$this->status = $status;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$status = $this->status;
$profile = $this->status->profile;
StatusService::del($status->id, true);
2022-12-31 04:05:34 +00:00
if($profile) {
if(in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])) {
$profile->status_count = $profile->status_count - 1;
$profile->save();
}
}
2021-05-12 00:17:03 +00:00
Cache::forget('pf:atom:user-feed:by-id:' . $status->profile_id);
2021-05-12 00:17:03 +00:00
if(config_cache('federation.activitypub.enabled') == true) {
return $this->fanoutDelete($status);
2021-05-12 00:17:03 +00:00
} else {
return $this->unlinkRemoveMedia($status);
2021-05-12 00:17:03 +00:00
}
}
public function unlinkRemoveMedia($status)
{
Media::whereStatusId($status->id)
->get()
->each(function($media) {
MediaDeletePipeline::dispatch($media);
});
2021-05-12 00:17:03 +00:00
if($status->in_reply_to_id) {
$parent = Status::findOrFail($status->in_reply_to_id);
--$parent->reply_count;
$parent->save();
StatusService::del($parent->id);
2021-05-12 00:17:03 +00:00
}
Bookmark::whereStatusId($status->id)->delete();
CollectionItem::whereObjectType('App\Status')
->whereObjectId($status->id)
->get()
->each(function($col) {
CollectionService::removeItem($col->collection_id, $col->object_id);
$col->delete();
});
$dms = DirectMessage::whereStatusId($status->id)->get();
foreach($dms as $dm) {
$not = Notification::whereItemType('App\DirectMessage')
->whereItemId($dm->id)
->first();
if($not) {
NotificationService::del($not->profile_id, $not->id);
$not->forceDeleteQuietly();
}
$dm->delete();
}
Like::whereStatusId($status->id)->delete();
$mediaTags = MediaTag::where('status_id', $status->id)->get();
foreach($mediaTags as $mtag) {
$not = Notification::whereItemType('App\MediaTag')
->whereItemId($mtag->id)
->first();
if($not) {
NotificationService::del($not->profile_id, $not->id);
$not->forceDeleteQuietly();
}
$mtag->delete();
}
Mention::whereStatusId($status->id)->forceDelete();
Notification::whereItemType('App\Status')
->whereItemId($status->id)
->forceDelete();
Report::whereObjectType('App\Status')
->whereObjectId($status->id)
->delete();
StatusArchived::whereStatusId($status->id)->delete();
2023-12-21 13:18:51 +00:00
StatusHashtag::whereStatusId($status->id)->delete();
StatusView::whereStatusId($status->id)->delete();
Status::whereInReplyToId($status->id)->update(['in_reply_to_id' => null]);
AccountInterstitial::where('item_type', 'App\Status')
->where('item_id', $status->id)
->delete();
$status->delete();
return 1;
2021-05-12 00:17:03 +00:00
}
public function fanoutDelete($status)
2021-05-12 00:17:03 +00:00
{
$profile = $status->profile;
2022-12-31 04:05:34 +00:00
if(!$profile) {
return;
}
2022-12-31 04:11:51 +00:00
$audience = $status->profile->getAudienceInbox();
2021-05-12 00:17:03 +00:00
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($status, new DeleteNote());
$activity = $fractal->createData($resource)->toArray();
$this->unlinkRemoveMedia($status);
$payload = json_encode($activity);
$client = new Client([
'timeout' => config('federation.activitypub.delivery.timeout')
]);
2023-01-31 07:15:41 +00:00
$version = config('pixelfed.version');
$appUrl = config('app.url');
$userAgent = "(Pixelfed/{$version}; +{$appUrl})";
$requests = function($audience) use ($client, $activity, $profile, $payload, $userAgent) {
2021-05-12 00:17:03 +00:00
foreach($audience as $url) {
$headers = HttpSignature::sign($profile, $url, $activity, [
'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
2023-01-31 07:15:41 +00:00
'User-Agent' => $userAgent,
]);
2021-05-12 00:17:03 +00:00
yield function() use ($client, $url, $headers, $payload) {
return $client->postAsync($url, [
'curl' => [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HEADER => true
]
]);
};
}
};
$pool = new Pool($client, $requests($audience), [
'concurrency' => config('federation.activitypub.delivery.concurrency'),
'fulfilled' => function ($response, $index) {
},
'rejected' => function ($reason, $index) {
}
]);
$promise = $pool->promise();
$promise->wait();
return 1;
2021-05-12 00:17:03 +00:00
}
2018-06-01 21:04:33 +00:00
}