pixelfed/app/Http/Controllers/StatusController.php

102 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-06-01 03:12:27 +00:00
use Auth, Cache;
2018-06-01 21:04:33 +00:00
use App\Jobs\StatusPipeline\{NewStatusPipeline, StatusDelete};
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
use Illuminate\Http\Request;
2018-04-19 05:56:46 +00:00
use App\{Media, Profile, Status, User};
use Vinkla\Hashids\Facades\Hashids;
class StatusController extends Controller
{
2018-06-01 03:12:27 +00:00
public function show(Request $request, $username, int $id)
2018-04-19 05:56:46 +00:00
{
$user = Profile::whereUsername($username)->firstOrFail();
2018-06-03 07:15:38 +00:00
$status = Status::whereProfileId($user->id)
->withCount(['likes', 'comments', 'media'])
2018-06-03 07:15:38 +00:00
->findOrFail($id);
2018-06-01 03:12:27 +00:00
if(!$status->media_path && $status->in_reply_to_id) {
2018-06-04 06:07:40 +00:00
return redirect($status->url());
2018-06-01 03:12:27 +00:00
}
2018-04-19 05:56:46 +00:00
return view('status.show', compact('user', 'status'));
}
2018-04-17 01:24:42 +00:00
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'),
2018-06-09 22:29:36 +00:00
'caption' => 'string|max:' . config('pixelfed.max_caption_length'),
'cw' => 'nullable|string',
'filter_class' => 'nullable|string',
'filter_name' => 'nullable|string',
2018-04-17 01:24:42 +00:00
]);
2018-06-01 03:12:27 +00:00
2018-06-09 22:29:36 +00:00
$cw = $request->filled('cw') && $request->cw == 'on' ? true : false;
2018-06-01 03:12:27 +00:00
$monthHash = hash('sha1', date('Y') . date('m'));
2018-04-17 01:24:42 +00:00
$userHash = hash('sha1', $user->id . (string) $user->created_at);
$profile = $user->profile;
$status = new Status;
$status->profile_id = $profile->id;
2018-06-14 00:54:27 +00:00
$status->caption = strip_tags($request->caption);
2018-06-09 22:29:36 +00:00
$status->is_nsfw = $cw;
2018-04-17 01:24:42 +00:00
$status->save();
$photos = $request->file('photo');
$order = 1;
foreach ($photos as $k => $v) {
$storagePath = "public/m/{$monthHash}/{$userHash}";
$path = $v->store($storagePath);
$media = new Media;
$media->status_id = $status->id;
$media->profile_id = $profile->id;
$media->user_id = $user->id;
$media->media_path = $path;
$media->size = $v->getClientSize();
$media->mime = $v->getClientMimeType();
$media->filter_class = $request->input('filter_class');
$media->filter_name = $request->input('filter_name');
$media->order = $order;
$media->save();
ImageOptimize::dispatch($media);
$order++;
}
NewStatusPipeline::dispatch($status);
2018-06-01 03:12:27 +00:00
// TODO: Send to subscribers
2018-04-17 01:24:42 +00:00
2018-04-19 05:56:46 +00:00
return redirect($status->url());
2018-04-17 01:24:42 +00:00
}
2018-06-01 21:04:33 +00:00
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());
}
}