pixelfed/app/Http/Controllers/StatusController.php
2018-06-09 16:29:36 -06:00

89 lines
2.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Auth, Cache;
use App\Jobs\StatusPipeline\{NewStatusPipeline, StatusDelete};
use Illuminate\Http\Request;
use App\{Media, Profile, Status, User};
use Vinkla\Hashids\Facades\Hashids;
class StatusController extends Controller
{
public function show(Request $request, $username, int $id)
{
$user = Profile::whereUsername($username)->firstOrFail();
$status = Status::whereProfileId($user->id)
->withCount(['likes', 'comments'])
->findOrFail($id);
if(!$status->media_path && $status->in_reply_to_id) {
return redirect($status->url());
}
return view('status.show', compact('user', 'status'));
}
public function store(Request $request)
{
if(Auth::check() == false)
{
abort(403);
}
$user = Auth::user();
$this->validate($request, [
'photo' => 'required|mimes:jpeg,png,bmp,gif|max:' . config('pixelfed.max_photo_size'),
'caption' => 'string|max:' . config('pixelfed.max_caption_length'),
'cw' => 'nullable|string'
]);
$cw = $request->filled('cw') && $request->cw == 'on' ? true : false;
$monthHash = hash('sha1', date('Y') . date('m'));
$userHash = hash('sha1', $user->id . (string) $user->created_at);
$storagePath = "public/m/{$monthHash}/{$userHash}";
$path = $request->photo->store($storagePath);
$profile = $user->profile;
$status = new Status;
$status->profile_id = $profile->id;
$status->caption = $request->caption;
$status->is_nsfw = $cw;
$status->save();
$media = new Media;
$media->status_id = $status->id;
$media->profile_id = $profile->id;
$media->user_id = $user->id;
$media->media_path = $path;
$media->size = $request->file('photo')->getClientSize();
$media->mime = $request->file('photo')->getClientMimeType();
$media->save();
NewStatusPipeline::dispatch($status, $media);
// TODO: Parse Caption
// TODO: Send to subscribers
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());
}
}