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/CommentController.php

104 lines
3.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use DB;
use Cache;
2018-08-28 03:07:36 +00:00
use App\Comment;
2018-06-04 08:16:33 +00:00
use App\Jobs\CommentPipeline\CommentPipeline;
use App\Jobs\StatusPipeline\NewStatusPipeline;
2019-04-02 03:18:39 +00:00
use App\Util\Lexer\Autolink;
2018-08-28 03:07:36 +00:00
use App\Profile;
use App\Status;
2019-07-21 02:09:46 +00:00
use App\UserFilter;
use League\Fractal;
use App\Transformer\Api\StatusTransformer;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Services\StatusService;
class CommentController extends Controller
{
public function showAll(Request $request, $username, int $id)
{
2020-02-18 07:19:18 +00:00
abort(404);
}
public function store(Request $request)
{
2018-08-28 03:07:36 +00:00
if (Auth::check() === false) {
abort(403);
}
$this->validate($request, [
2019-05-13 04:35:54 +00:00
'item' => 'required|integer|min:1',
'comment' => 'required|string|max:'.(int) config('pixelfed.max_caption_length'),
2020-05-23 07:38:23 +00:00
'sensitive' => 'nullable|boolean'
2018-12-21 19:51:52 +00:00
]);
2018-08-28 03:07:36 +00:00
$comment = $request->input('comment');
2020-05-23 07:38:23 +00:00
$statusId = $request->input('item');
$nsfw = $request->input('sensitive', false);
2018-08-28 03:07:36 +00:00
$user = Auth::user();
$profile = $user->profile;
$status = Status::findOrFail($statusId);
2019-04-03 06:08:32 +00:00
if($status->comments_disabled == true) {
return;
}
2019-07-21 02:09:46 +00:00
$filtered = UserFilter::whereUserId($status->profile_id)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['block'])
2019-07-21 02:09:46 +00:00
->whereFilterableId($profile->id)
->exists();
if($filtered == true) {
return;
}
2020-05-23 07:38:23 +00:00
$reply = DB::transaction(function() use($comment, $status, $profile, $nsfw) {
$scope = $profile->is_private == true ? 'private' : 'public';
$autolink = Autolink::create()->autolink($comment);
$reply = new Status();
$reply->profile_id = $profile->id;
2020-05-23 07:38:23 +00:00
$reply->is_nsfw = $nsfw;
$reply->caption = e($comment);
$reply->rendered = $autolink;
$reply->in_reply_to_id = $status->id;
$reply->in_reply_to_profile_id = $status->profile_id;
$reply->scope = $scope;
$reply->visibility = $scope;
$reply->save();
return $reply;
});
2018-08-28 03:07:36 +00:00
StatusService::del($status->id);
NewStatusPipeline::dispatch($reply);
2018-08-28 03:07:36 +00:00
CommentPipeline::dispatch($status, $reply);
if ($request->ajax()) {
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$entity = new Fractal\Resource\Item($reply, new StatusTransformer());
$entity = $fractal->createData($entity)->toArray();
$response = [
'code' => 200,
'msg' => 'Comment saved',
'username' => $profile->username,
'url' => $reply->url(),
'profile' => $profile->url(),
'comment' => $reply->caption,
'entity' => $entity,
];
2018-08-28 03:07:36 +00:00
} else {
$response = redirect($status->url());
}
return $response;
}
}