Add post delete method

This commit is contained in:
Daniel Supernault 2018-06-01 15:04:33 -06:00
parent fa7d1fef2f
commit 91a6f8eba5
5 changed files with 128 additions and 5 deletions

View file

@ -3,7 +3,7 @@
namespace App\Http\Controllers;
use Auth, Cache;
use App\Jobs\StatusPipeline\NewStatusPipeline;
use App\Jobs\StatusPipeline\{NewStatusPipeline, StatusDelete};
use Illuminate\Http\Request;
use App\{Media, Profile, Status, User};
use Vinkla\Hashids\Facades\Hashids;
@ -60,4 +60,24 @@ class StatusController extends Controller
return redirect($status->url());
}
public function delete(Request $request)
{
if(!Auth::check()) {
abort(403);
}
$this->validate($request, [
'type' => 'required|string',
'item' => 'required|integer|min:1'
]);
$status = Status::findOrFail($request->input('item'));
if($status->profile_id === Auth::user()->profile->id || Auth::user()->is_admin == true) {
StatusDelete::dispatch($status);
}
return redirect(Auth::user()->url());
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace App\Jobs\StatusPipeline;
use App\{Media, StatusHashtag, Status};
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class StatusDelete implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $status;
/**
* 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;
$this->unlinkRemoveMedia($status);
}
public function unlinkRemoveMedia($status)
{
if($status->media()->count() == 0) {
return;
}
foreach($status->media as $media) {
$thumbnail = storage_path("app/{$media->thumbnail_path}");
$photo = storage_path("app/{$media->media_path}");
try {
if(is_file($thumbnail)) {
unlink($thumbnail);
}
if(is_file($photo)) {
unlink($photo);
}
$media->delete();
} catch (Exception $e) {
}
}
$status->likes()->delete();
StatusHashtag::whereStatusId($status->id)->delete();
$status->delete();
return true;
}
}

View file

@ -44,9 +44,23 @@
<input type="hidden" name="item" value="{{$status->id}}">
<button class="btn btn-link text-dark p-0" type="submit"><span class="icon-heart" style="font-size:25px;"></span></button>
</form>
<span class="icon-speech"></span>
<span class="icon-speech pr-3"></span>
@if(Auth::check())
@if(Auth::user()->profile->id === $status->profile->id || Auth::user()->is_admin == true)
<form method="post" action="/i/delete" class="d-inline-flex">
@csrf
<input type="hidden" name="type" value="post">
<input type="hidden" name="item" value="{{$status->id}}">
<button type="submit" class="btn btn-link text-dark p-0"><span class="icon-trash" style="font-size:25px;"></span></button>
</form>
@endif
@endif
<span class="float-right">
<span class="icon-notebook"></span>
<form class="bookmark-form" method="post" action="/i/bookmark" style="display: inline;" data-id="{{$status->id}}" data-action="bookmark">
@csrf
<input type="hidden" name="item" value="{{$status->id}}">
<button class="btn btn-link text-dark p-0" type="submit"><span class="icon-notebook" style="font-size:25px;"></span></button>
</form>
</span>
</div>
<div class="likes font-weight-bold mb-0">

View file

@ -13,6 +13,17 @@
<a class="dropdown-item" href="{{$item->url()}}">Go to post</a>
<a class="dropdown-item" href="{{route('report.form')}}?type=post&id={{$item->id}}">Report Inappropriate</a>
<a class="dropdown-item" href="#">Embed</a>
@if(Auth::check())
@if(Auth::user()->profile->id === $item->profile->id || Auth::user()->is_admin == true)
<form method="post" action="/i/delete">
@csrf
<input type="hidden" name="type" value="post">
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="dropdown-item btn btn-link">Delete</button>
</form>
@endif
@endif
</div>
</div>
</div>
@ -55,8 +66,17 @@
<div class="comments">
@if(isset($showSingleComment) && $showSingleComment === true)
<p class="mb-0">
<span class="font-weight-bold pr-1"><bdi><a class="text-dark" href="{{$status->profile->url()}}">{{$status->profile->username}}</a></bdi></span>
<span class="comment-text">{!!$status->rendered!!}</span><a href="{{$status->url()}}" class="text-dark small font-weight-bold float-right">{{$status->created_at->diffForHumans(null, true, true, true)}}</a>
<span class="font-weight-bold pr-1">
<bdi>
<a class="text-dark" href="{{$status->profile->url()}}">{{$status->profile->username}}</a>
</bdi>
</span>
<span class="comment-text">{!!$status->rendered!!}</span>
<span class="float-right">
<a href="{{$status->url()}}" class="text-dark small font-weight-bold">
{{$status->created_at->diffForHumans(null, true, true, true)}}
</a>
</span>
</p>
@else
@foreach($item->comments->reverse()->take(3) as $comment)

View file

@ -56,6 +56,7 @@ Route::domain(config('pixelfed.domain.app'))->group(function() {
Route::get('remote-follow', 'FederationController@remoteFollow')->name('remotefollow');
Route::post('remote-follow', 'FederationController@remoteFollowStore');
Route::post('comment', 'CommentController@store');
Route::post('delete', 'StatusController@delete');
Route::post('like', 'LikeController@store');
Route::post('follow', 'FollowerController@store');
Route::post('bookmark', 'BookmarkController@store');