pixelfed/app/Http/Controllers/StatusController.php

367 lines
11 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;
2019-01-15 05:44:23 +00:00
use App\Jobs\SharePipeline\SharePipeline;
2018-08-28 03:07:36 +00:00
use App\Media;
use App\Profile;
use App\Status;
use App\Transformer\ActivityPub\StatusTransformer;
2019-01-11 05:33:44 +00:00
use App\Transformer\ActivityPub\Verb\Note;
2018-08-28 03:07:36 +00:00
use App\User;
use Auth;
use Cache;
use Illuminate\Http\Request;
use League\Fractal;
2019-03-08 08:52:54 +00:00
use App\Util\Media\Filter;
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
{
2019-01-01 06:02:23 +00:00
$user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
2018-12-24 05:01:15 +00:00
if($user->status != null) {
return ProfileController::accountCheck($user);
}
2018-08-10 03:22:37 +00:00
$status = Status::whereProfileId($user->id)
2018-11-16 01:05:56 +00:00
->whereNotIn('visibility',['draft','direct'])
2018-08-10 03:22:37 +00:00
->findOrFail($id);
2018-12-24 05:01:15 +00:00
if($status->uri) {
$url = $status->uri;
if(ends_with($url, '/activity')) {
$url = str_replace('/activity', '', $url);
}
return redirect($url);
}
if($status->visibility == 'private' || $user->is_private) {
if(!Auth::check()) {
abort(403);
}
$pid = Auth::user()->profile;
if($user->followedBy($pid) == false && $user->id !== $pid->id) {
abort(403);
}
}
2018-08-28 03:07:36 +00:00
if ($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
return $this->showActivityPub($request, $status);
}
2018-11-11 03:15:26 +00:00
$template = $status->in_reply_to_id ? 'status.reply' : 'status.show';
return view($template, compact('user', 'status'));
2018-08-10 03:22:37 +00:00
}
2019-01-07 04:18:29 +00:00
public function showObject(Request $request, $username, int $id)
{
$user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
if($user->status != null) {
return ProfileController::accountCheck($user);
}
$status = Status::whereProfileId($user->id)
->whereNotIn('visibility',['draft','direct'])
->findOrFail($id);
if($status->uri) {
$url = $status->uri;
if(ends_with($url, '/activity')) {
$url = str_replace('/activity', '', $url);
}
return redirect($url);
}
if($status->visibility == 'private' || $user->is_private) {
if(!Auth::check()) {
abort(403);
}
$pid = Auth::user()->profile;
if($user->followedBy($pid) == false && $user->id !== $pid->id) {
abort(403);
}
}
return $this->showActivityPub($request, $status);
}
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-10-16 21:36:08 +00:00
'photo.*' => 'required|mimetypes:' . config('pixelfed.media_types').'|max:' . config('pixelfed.max_photo_size'),
2019-02-27 04:32:30 +00:00
'caption' => 'nullable|string|max:'.config('pixelfed.max_caption_length'),
2018-08-28 03:07:36 +00:00
'cw' => 'nullable|string',
2019-03-08 08:18:59 +00:00
'filter_class' => 'nullable|alpha_dash|max:30',
2018-08-28 03:07:36 +00:00
'filter_name' => 'nullable|string',
'visibility' => 'required|string|min:5|max:10',
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;
$visibility = $this->validateVisibility($request->visibility);
2018-08-10 03:22:37 +00:00
2019-01-11 03:45:33 +00:00
$cw = $profile->cw == true ? true : $cw;
$visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
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;
2018-10-18 00:23:29 +00:00
// TODO: remove deprecated visibility in favor of scope
$status->visibility = $visibility;
2018-10-18 00:23:29 +00:00
$status->scope = $visibility;
2018-08-10 03:22:37 +00:00
$status->save();
$photos = $request->file('photo');
$order = 1;
2018-12-02 06:04:42 +00:00
$mimes = [];
2018-12-03 01:42:55 +00:00
$medias = 0;
2018-12-02 06:04:42 +00:00
2018-08-10 03:22:37 +00:00
foreach ($photos as $k => $v) {
2018-12-03 01:42:55 +00:00
$allowedMimes = explode(',', config('pixelfed.media_types'));
if(in_array($v->getMimeType(), $allowedMimes) == false) {
continue;
}
2019-03-08 08:52:54 +00:00
$filter_class = $request->input('filter_class');
$filter_name = $request->input('filter_name');
2018-12-03 01:42:55 +00:00
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;
2018-12-03 01:42:55 +00:00
$media->size = $v->getSize();
$media->mime = $v->getMimeType();
2019-03-08 08:52:54 +00:00
$media->filter_class = in_array($filter_class, Filter::classes()) ? $filter_class : null;
$media->filter_name = in_array($filter_name, Filter::names()) ? $filter_name : null;
2018-08-28 03:07:36 +00:00
$media->order = $order;
$media->save();
2018-12-02 06:04:42 +00:00
array_push($mimes, $media->mime);
2018-08-28 03:07:36 +00:00
ImageOptimize::dispatch($media);
$order++;
2018-12-03 01:42:55 +00:00
$medias++;
2018-08-10 03:22:37 +00:00
}
2018-12-03 01:42:55 +00:00
if($medias == 0) {
$status->delete();
return;
}
2018-12-02 06:39:29 +00:00
$status->type = (new self)::mimeTypeCheck($mimes);
2018-12-02 06:04:42 +00:00
$status->save();
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-11-19 04:49:38 +00:00
$this->authCheck();
2018-08-10 03:22:37 +00:00
$this->validate($request, [
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-11-22 20:21:36 +00:00
if($request->wantsJson()) {
return response()->json(['Status successfully deleted.']);
} else {
return redirect(Auth::user()->url());
}
2018-08-10 03:22:37 +00:00
}
2018-06-01 21:04:33 +00:00
2018-08-10 03:22:37 +00:00
public function storeShare(Request $request)
{
2018-11-19 04:49:38 +00:00
$this->authCheck();
2018-08-10 03:22:37 +00:00
$this->validate($request, [
'item' => 'required|integer',
]);
$profile = Auth::user()->profile;
$status = Status::withCount('shares')->findOrFail($request->input('item'));
Cache::forget('transform:status:'.$status->url());
2018-08-10 03:22:37 +00:00
$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;
2019-01-15 05:44:23 +00:00
$share->in_reply_to_profile_id = $status->profile_id;
2018-08-28 03:07:36 +00:00
$share->save();
$count++;
2019-01-15 05:44:23 +00:00
SharePipeline::dispatch($share);
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();
2019-01-11 05:33:44 +00:00
$resource = new Fractal\Resource\Item($status, new Note());
2018-08-28 03:07:36 +00:00
$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);
}
}
protected function validateVisibility($visibility)
{
$allowed = ['public', 'unlisted', 'private'];
return in_array($visibility, $allowed) ? $visibility : 'public';
}
2018-12-02 06:04:42 +00:00
public static function mimeTypeCheck($mimes)
{
$allowed = explode(',', config('pixelfed.media_types'));
$count = count($mimes);
$photos = 0;
$videos = 0;
foreach($mimes as $mime) {
2018-12-11 04:26:11 +00:00
if(in_array($mime, $allowed) == false && $mime !== 'video/mp4') {
2018-12-02 06:04:42 +00:00
continue;
}
if(str_contains($mime, 'image/')) {
$photos++;
}
if(str_contains($mime, 'video/')) {
$videos++;
}
}
if($photos == 1 && $videos == 0) {
return 'photo';
}
if($videos == 1 && $photos == 0) {
return 'video';
}
if($photos > 1 && $videos == 0) {
return 'photo:album';
}
if($videos > 1 && $photos == 0) {
return 'video:album';
}
if($photos >= 1 && $videos >= 1) {
return 'photo:video:album';
}
}
}