pixelfed/app/Http/Controllers/StatusController.php

443 lines
14 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;
use App\AccountInterstitial;
2018-08-28 03:07:36 +00:00
use App\Media;
use App\Profile;
use App\Status;
use App\StatusView;
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;
2019-04-01 02:27:22 +00:00
use Auth, Cache;
2018-08-28 03:07:36 +00:00
use Illuminate\Http\Request;
use League\Fractal;
2019-03-08 08:52:54 +00:00
use App\Util\Media\Filter;
2019-04-25 04:48:07 +00:00
use Illuminate\Support\Str;
use App\Services\HashidService;
use App\Services\StatusService;
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)
2019-06-25 04:35:21 +00:00
->whereNull('reblog_of_id')
2021-01-04 05:06:05 +00:00
->whereIn('scope', ['public','unlisted', 'private'])
2018-08-10 03:22:37 +00:00
->findOrFail($id);
2019-06-25 04:43:42 +00:00
if($status->uri || $status->url) {
$url = $status->uri ?? $status->url;
2018-12-24 05:01:15 +00:00
if(ends_with($url, '/activity')) {
$url = str_replace('/activity', '', $url);
}
return redirect($url);
}
if($status->visibility == 'private' || $user->is_private) {
if(!Auth::check()) {
2019-04-13 05:28:23 +00:00
abort(404);
}
$pid = Auth::user()->profile;
2019-04-13 05:28:23 +00:00
if($user->followedBy($pid) == false && $user->id !== $pid->id && Auth::user()->is_admin == false) {
abort(404);
}
}
2019-11-23 06:35:13 +00:00
if($status->type == 'archived') {
if(Auth::user()->profile_id !== $status->profile_id) {
abort(404);
}
}
if($request->user() && $request->user()->profile_id != $status->profile_id) {
StatusView::firstOrCreate([
'status_id' => $status->id,
'status_profile_id' => $status->profile_id,
'profile_id' => $request->user()->profile_id
]);
}
2019-06-09 23:26:24 +00:00
if ($request->wantsJson() && config('federation.activitypub.enabled')) {
2018-08-28 03:07:36 +00:00
return $this->showActivityPub($request, $status);
}
2018-11-11 03:15:26 +00:00
$template = $status->in_reply_to_id ? 'status.reply' : 'status.show';
// $template = $status->type === 'video' &&
// $request->has('video_beta') &&
// $request->video_beta == 1 &&
// $request->user() ?
// 'status.show_video' : 'status.show';
2018-11-11 03:15:26 +00:00
return view($template, compact('user', 'status'));
2018-08-10 03:22:37 +00:00
}
public function shortcodeRedirect(Request $request, $id)
{
abort_if(strlen($id) < 5, 404);
if(!Auth::check()) {
return redirect('/login?next='.urlencode('/' . $request->path()));
}
$id = HashidService::decode($id);
2020-12-14 00:30:50 +00:00
$status = Status::find($id);
if(!$status) {
return redirect('/404');
}
return redirect($status->url());
}
2019-09-07 03:21:28 +00:00
public function showId(int $id)
{
abort(404);
$status = Status::whereNull('reblog_of_id')
->whereIn('scope', ['public', 'unlisted'])
->findOrFail($id);
return redirect($status->url());
}
2019-06-27 06:44:34 +00:00
public function showEmbed(Request $request, $username, int $id)
{
2019-11-24 07:42:59 +00:00
$profile = Profile::whereNull(['domain','status'])
->whereIsPrivate(false)
->whereUsername($username)
->first();
2019-11-24 04:44:41 +00:00
if(!$profile) {
2019-11-24 04:56:16 +00:00
$content = view('status.embed-removed');
return response($content)->header('X-Frame-Options', 'ALLOWALL');
2019-11-24 04:44:41 +00:00
}
$status = Status::whereProfileId($profile->id)
->whereNull('uri')
->whereScope('public')
->whereIsNsfw(false)
->whereIn('type', ['photo', 'video','photo:album'])
2019-11-24 04:44:41 +00:00
->find($id);
if(!$status) {
2019-11-24 04:56:16 +00:00
$content = view('status.embed-removed');
return response($content)->header('X-Frame-Options', 'ALLOWALL');
2019-09-07 03:21:28 +00:00
}
2019-11-24 04:44:41 +00:00
$showLikes = $request->filled('likes') && $request->likes == true;
$showCaption = $request->filled('caption') && $request->caption !== false;
$layout = $request->filled('layout') && $request->layout == 'compact' ? 'compact' : 'full';
2019-11-24 04:56:16 +00:00
$content = view('status.embed', compact('status', 'showLikes', 'showCaption', 'layout'));
2019-11-24 05:18:51 +00:00
return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
2019-06-27 06:44:34 +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);
2019-09-07 03:21:28 +00:00
abort_if($status->uri, 404);
2019-01-07 04:18:29 +00:00
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
2019-03-26 19:13:30 +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)
{
2019-06-19 08:45:51 +00:00
return;
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
$user = Auth::user();
if($status->profile_id != $user->profile->id &&
$user->is_admin == true &&
$status->uri == null
) {
$media = $status->media;
$ai = new AccountInterstitial;
$ai->user_id = $status->profile->user_id;
$ai->type = 'post.removed';
$ai->view = 'account.moderation.post.removed';
$ai->item_type = 'App\Status';
$ai->item_id = $status->id;
$ai->has_media = (bool) $media->count();
$ai->blurhash = $media->count() ? $media->first()->blurhash : null;
$ai->meta = json_encode([
'caption' => $status->caption,
'created_at' => $status->created_at,
'type' => $status->type,
'url' => $status->url(),
'is_nsfw' => $status->is_nsfw,
'scope' => $status->scope,
'reblog' => $status->reblog_of_id,
'likes_count' => $status->likes_count,
'reblogs_count' => $status->reblogs_count,
]);
$ai->save();
$u = $status->profile->user;
$u->has_interstitial = true;
$u->save();
}
2020-12-18 06:12:27 +00:00
Cache::forget('_api:statuses:recent_9:' . $status->profile_id);
2020-12-27 21:45:31 +00:00
Cache::forget('profile:status_count:' . $status->profile_id);
Cache::forget('profile:embed:' . $status->profile_id);
StatusService::del($status->id);
if ($status->profile_id == $user->profile->id || $user->is_admin == true) {
Cache::forget('profile:status_count:'.$status->profile_id);
2018-08-28 03:07:36 +00:00
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($user->url());
2018-11-22 20:21:36 +00:00
}
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, [
2019-05-13 04:32:33 +00:00
'item' => 'required|integer|min:1',
2018-08-10 03:22:37 +00:00
]);
2019-06-18 07:16:10 +00:00
$user = Auth::user();
$profile = $user->profile;
2019-06-25 02:55:38 +00:00
$status = Status::withCount('shares')
->whereIn('scope', ['public', 'unlisted'])
->findOrFail($request->input('item'));
2018-08-10 03:22:37 +00:00
$count = $status->shares()->count();
2018-08-10 03:22:37 +00:00
$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
}
2019-06-18 06:47:38 +00:00
if($count >= 0) {
$status->reblogs_count = $count;
$status->save();
}
2019-06-18 07:16:10 +00:00
Cache::forget('status:'.$status->id.':sharedby:userid:'.$user->id);
StatusService::del($status->id);
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($res['data'], 200, ['Content-Type' => 'application/activity+json'], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
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'])
->where('created_at', '>', now()->subHours(24))
2018-08-27 03:25:02 +00:00
->findOrFail($id);
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'])
->where('created_at', '>', now()->subHours(24))
2018-08-27 03:25:02 +00:00
->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)
->findOrFail($id);
2018-08-27 03:25:02 +00:00
$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
}
if ($media->filter_class != $filter && in_array($filter, Filter::classes())) {
2018-08-28 03:07:36 +00:00
$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();
2019-06-18 05:31:53 +00:00
Cache::forget('status:transformer:media:attachments:'.$media->status_id);
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';
}
}
2019-04-01 02:27:22 +00:00
public function toggleVisibility(Request $request) {
$this->authCheck();
$this->validate($request, [
'item' => 'required|string|min:1|max:20',
'disableComments' => 'required|boolean'
]);
$user = Auth::user();
$id = $request->input('item');
$state = $request->input('disableComments');
$status = Status::findOrFail($id);
if($status->profile_id != $user->profile->id && $user->is_admin == false) {
abort(403);
}
$status->comments_disabled = $status->comments_disabled == true ? false : true;
$status->save();
return response()->json([200]);
}
public function storeView(Request $request)
{
abort_if(!$request->user(), 403);
$this->validate($request, [
'status_id' => 'required|integer|exists:statuses,id',
'profile_id' => 'required|integer|exists:profiles,id'
]);
$sid = (int) $request->input('status_id');
$pid = (int) $request->input('profile_id');
StatusView::firstOrCreate([
'status_id' => $sid,
'status_profile_id' => $pid,
'profile_id' => $request->user()->profile_id
]);
return response()->json(1);
}
}