From 37dbb3de295847ca8ac50e2ea57bade7bd02dadc Mon Sep 17 00:00:00 2001 From: Daniel Supernault <danielsupernault@gmail.com> Date: Fri, 5 Feb 2021 21:00:29 -0700 Subject: [PATCH] Update MediaStorageService, dispatch deletes to MediaDeletePipeline --- .../MediaPipeline/MediaDeletePipeline.php | 67 +++++++++++++++++++ app/Services/MediaStorageService.php | 9 +++ app/Util/Sentiment/Bouncer.php | 6 ++ 3 files changed, 82 insertions(+) create mode 100644 app/Jobs/MediaPipeline/MediaDeletePipeline.php diff --git a/app/Jobs/MediaPipeline/MediaDeletePipeline.php b/app/Jobs/MediaPipeline/MediaDeletePipeline.php new file mode 100644 index 00000000..9d89f944 --- /dev/null +++ b/app/Jobs/MediaPipeline/MediaDeletePipeline.php @@ -0,0 +1,67 @@ +<?php + +namespace App\Jobs\MediaPipeline; + +use App\Media; +use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Redis; +use Illuminate\Support\Facades\Storage; + +class MediaDeletePipeline implements ShouldQueue +{ + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + + protected $media; + + public function __construct(Media $media) + { + $this->media = $media; + } + + public function handle() + { + $media = $this->media; + $path = $media->media_path; + $thumb = $media->thumbnail_path; + + if(!$path) { + return 1; + } + + $e = explode('/', $path); + array_pop($e); + $i = implode('/', $e); + + if(config('pixelfed.cloud_storage') == true) { + $disk = Storage::disk(config('filesystems.cloud')); + if($disk->exists($path)) { + $disk->delete($path); + } + if($disk->exists($thumb)) { + $disk->delete($thumb); + } + + if(count($e) > 4 && count($disk->files($i)) == 0) { + $disk->deleteDirectory($i); + } + } + + $disk = Storage::disk(config('filesystems.local')); + if($disk->exists($path)) { + $disk->delete($path); + } + if($disk->exists($thumb)) { + $disk->delete($thumb); + } + if(count($e) > 4 && count($disk->files($i)) == 0) { + $disk->deleteDirectory($i); + } + + return 1; + } + +} \ No newline at end of file diff --git a/app/Services/MediaStorageService.php b/app/Services/MediaStorageService.php index ec718d0e..5d301dd9 100644 --- a/app/Services/MediaStorageService.php +++ b/app/Services/MediaStorageService.php @@ -14,6 +14,7 @@ use App\User; use GuzzleHttp\Client; use App\Http\Controllers\AvatarController; use GuzzleHttp\Exception\RequestException; +use App\Jobs\MediaPipeline\MediaDeletePipeline; class MediaStorageService { @@ -227,4 +228,12 @@ class MediaStorageService { unlink($tmpName); } + + public static function delete(Media $media, $confirm = false) + { + if(!$confirm) { + return; + } + MediaDeletePipeline::dispatch($media); + } } \ No newline at end of file diff --git a/app/Util/Sentiment/Bouncer.php b/app/Util/Sentiment/Bouncer.php index 5841cb4d..9a1e8dde 100644 --- a/app/Util/Sentiment/Bouncer.php +++ b/app/Util/Sentiment/Bouncer.php @@ -18,6 +18,12 @@ class Bouncer { $exemptionKey = 'pf:bouncer_v0:exemption_by_pid:' . $status->profile_id; $exemptionTtl = now()->addDays(12); + if( $status->in_reply_to_id != null && + $status->in_reply_to_profile_id == $status->profile_id + ) { + return; + } + $exemption = Cache::remember($exemptionKey, $exemptionTtl, function() use($status) { $uid = $status->profile->user_id; $ids = AccountInterstitial::whereUserId($uid)