1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-29 11:15:09 +00:00
pixelfed/app/Http/Controllers/StatusController.php

256 lines
7.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
2018-08-28 03:07:36 +00:00
use App\Jobs\StatusPipeline\NewStatusPipeline;
use App\Jobs\StatusPipeline\StatusDelete;
use App\Media;
use App\Profile;
use App\Status;
use App\Transformer\ActivityPub\StatusTransformer;
2018-08-28 03:07:36 +00:00
use App\User;
use Auth;
use Cache;
use Illuminate\Http\Request;
use League\Fractal;
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
{
2018-08-10 03:22:37 +00:00
$user = Profile::whereUsername($username)->firstOrFail();
2018-08-10 03:22:37 +00:00
$status = Status::whereProfileId($user->id)
->withCount(['likes', 'comments', 'media'])
->findOrFail($id);
2018-08-28 03:07:36 +00:00
if ($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
return $this->showActivityPub($request, $status);
}
2018-08-27 03:25:02 +00:00
$template = $this->detectTemplate($status);
2018-08-10 03:22:37 +00:00
$replies = Status::whereInReplyToId($status->id)->simplePaginate(30);
2018-08-27 03:25:02 +00:00
return view($template, compact('user', 'status', 'replies'));
}
protected function detectTemplate($status)
{
2018-08-28 03:07:36 +00:00
$template = Cache::rememberForever('template:status:type:'.$status->id, function () use ($status) {
$template = 'status.show.photo';
if (!$status->media_path && $status->in_reply_to_id) {
$template = 'status.reply';
}
if ($status->media->count() > 1) {
$template = 'status.show.album';
}
if ($status->viewType() == 'video') {
$template = 'status.show.video';
}
return $template;
2018-08-27 03:25:02 +00:00
});
2018-08-28 03:07:36 +00:00
2018-08-27 03:25:02 +00:00
return $template;
2018-08-10 03:22:37 +00:00
}
public function compose()
{
$this->authCheck();
2018-08-28 03:07:36 +00:00
2018-08-10 03:22:37 +00:00
return view('status.compose');
2018-04-19 05:56:46 +00:00
}
2018-04-17 01:24:42 +00:00
public function store(Request $request)
{
2018-08-27 03:25:02 +00:00
$this->authCheck();
2018-08-10 03:22:37 +00:00
$user = Auth::user();
$size = Media::whereUserId($user->id)->sum('size') / 1000;
$limit = (int) config('pixelfed.max_account_size');
2018-08-28 03:07:36 +00:00
if ($size >= $limit) {
return redirect()->back()->with('error', 'You have exceeded your storage limit. Please click <a href="#">here</a> for more info.');
2018-08-10 03:22:37 +00:00
}
$this->validate($request, [
2018-08-28 03:07:36 +00:00
'photo.*' => 'required|mimes:jpeg,png,gif|max:'.config('pixelfed.max_photo_size'),
'caption' => 'string|max:'.config('pixelfed.max_caption_length'),
'cw' => 'nullable|string',
2018-08-10 03:22:37 +00:00
'filter_class' => 'nullable|string',
2018-08-28 03:07:36 +00:00
'filter_name' => 'nullable|string',
2018-08-10 03:22:37 +00:00
]);
2018-08-28 03:07:36 +00:00
if (count($request->file('photo')) > config('pixelfed.max_album_length')) {
return redirect()->back()->with('error', 'Too many files, max limit per post: '.config('pixelfed.max_album_length'));
2018-08-10 03:22:37 +00:00
}
$cw = $request->filled('cw') && $request->cw == 'on' ? true : false;
2018-08-28 03:07:36 +00:00
$monthHash = hash('sha1', date('Y').date('m'));
$userHash = hash('sha1', $user->id.(string) $user->created_at);
2018-08-10 03:22:37 +00:00
$profile = $user->profile;
2018-08-28 03:07:36 +00:00
$status = new Status();
2018-08-10 03:22:37 +00:00
$status->profile_id = $profile->id;
$status->caption = strip_tags($request->caption);
$status->is_nsfw = $cw;
$status->save();
$photos = $request->file('photo');
$order = 1;
foreach ($photos as $k => $v) {
2018-08-28 03:07:36 +00:00
$storagePath = "public/m/{$monthHash}/{$userHash}";
$path = $v->store($storagePath);
$hash = \hash_file('sha256', $v);
$media = new Media();
$media->status_id = $status->id;
$media->profile_id = $profile->id;
$media->user_id = $user->id;
$media->media_path = $path;
$media->original_sha256 = $hash;
$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++;
2018-08-10 03:22:37 +00:00
}
NewStatusPipeline::dispatch($status);
// TODO: Send to subscribers
2018-08-28 03:07:36 +00:00
2018-08-10 03:22:37 +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)
{
2018-08-28 03:07:36 +00:00
if (!Auth::check()) {
abort(403);
2018-08-10 03:22:37 +00:00
}
$this->validate($request, [
'type' => 'required|string',
2018-08-28 03:07:36 +00:00
'item' => 'required|integer|min:1',
2018-08-10 03:22:37 +00:00
]);
2018-06-01 21:04:33 +00:00
2018-08-10 03:22:37 +00:00
$status = Status::findOrFail($request->input('item'));
2018-06-01 21:04:33 +00:00
2018-08-28 03:07:36 +00:00
if ($status->profile_id === Auth::user()->profile->id || Auth::user()->is_admin == true) {
StatusDelete::dispatch($status);
2018-08-10 03:22:37 +00:00
}
2018-06-01 21:04:33 +00:00
2018-08-10 03:22:37 +00:00
return redirect(Auth::user()->url());
}
2018-06-01 21:04:33 +00:00
2018-08-10 03:22:37 +00:00
public function storeShare(Request $request)
{
$this->validate($request, [
'item' => 'required|integer',
]);
$profile = Auth::user()->profile;
$status = Status::withCount('shares')->findOrFail($request->input('item'));
$count = $status->shares_count;
$exists = Status::whereProfileId(Auth::user()->profile->id)
->whereReblogOfId($status->id)
->count();
2018-08-28 03:07:36 +00:00
if ($exists !== 0) {
$shares = Status::whereProfileId(Auth::user()->profile->id)
2018-08-10 03:22:37 +00:00
->whereReblogOfId($status->id)
->get();
2018-08-28 03:07:36 +00:00
foreach ($shares as $share) {
$share->delete();
$count--;
}
2018-08-10 03:22:37 +00:00
} else {
2018-08-28 03:07:36 +00:00
$share = new Status();
$share->profile_id = $profile->id;
$share->reblog_of_id = $status->id;
$share->save();
$count++;
2018-08-10 03:22:37 +00:00
}
2018-08-28 03:07:36 +00:00
if ($request->ajax()) {
$response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
2018-08-10 03:22:37 +00:00
} else {
2018-08-28 03:07:36 +00:00
$response = redirect($status->url());
2018-08-10 03:22:37 +00:00
}
return $response;
2018-06-01 21:04:33 +00:00
}
public function showActivityPub(Request $request, $status)
{
2018-08-28 03:07:36 +00:00
$fractal = new Fractal\Manager();
$resource = new Fractal\Resource\Item($status, new StatusTransformer());
$res = $fractal->createData($resource)->toArray();
return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
}
2018-08-27 03:25:02 +00:00
public function edit(Request $request, $username, $id)
{
$this->authCheck();
$user = Auth::user()->profile;
$status = Status::whereProfileId($user->id)
->with(['media'])
->findOrFail($id);
2018-08-28 03:07:36 +00:00
2018-08-27 03:25:02 +00:00
return view('status.edit', compact('user', 'status'));
}
public function editStore(Request $request, $username, $id)
{
$this->authCheck();
$user = Auth::user()->profile;
$status = Status::whereProfileId($user->id)
->with(['media'])
->findOrFail($id);
$this->validate($request, [
2018-08-28 03:07:36 +00:00
'id' => 'required|integer|min:1',
2018-08-27 03:25:02 +00:00
'caption' => 'nullable',
2018-08-28 03:07:36 +00:00
'filter' => 'nullable|alpha_dash|max:30',
2018-08-27 03:25:02 +00:00
]);
$id = $request->input('id');
$caption = $request->input('caption');
$filter = $request->input('filter');
$media = Media::whereProfileId($user->id)
->whereStatusId($status->id)
->find($id);
$changed = false;
2018-08-28 03:07:36 +00:00
if ($media->caption != $caption) {
$media->caption = $caption;
$changed = true;
2018-08-27 03:25:02 +00:00
}
2018-08-28 03:07:36 +00:00
if ($media->filter_class != $filter) {
$media->filter_class = $filter;
$changed = true;
2018-08-27 03:25:02 +00:00
}
2018-08-28 03:07:36 +00:00
if ($changed === true) {
$media->save();
2018-08-27 03:25:02 +00:00
}
2018-08-28 03:07:36 +00:00
2018-08-27 03:25:02 +00:00
return response()->json([], 200);
}
protected function authCheck()
{
2018-08-28 03:07:36 +00:00
if (Auth::check() == false) {
abort(403);
}
}
}