2018-04-19 05:55:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-06-04 08:16:33 +00:00
|
|
|
use App\Jobs\CommentPipeline\CommentPipeline;
|
2018-05-26 22:46:07 +00:00
|
|
|
use App\Jobs\StatusPipeline\NewStatusPipeline;
|
2024-03-10 10:19:44 +00:00
|
|
|
use App\Services\StatusService;
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Status;
|
2024-03-10 10:19:44 +00:00
|
|
|
use App\Transformer\Api\StatusTransformer;
|
2019-07-21 02:09:46 +00:00
|
|
|
use App\UserFilter;
|
2024-03-10 10:19:44 +00:00
|
|
|
use Auth;
|
|
|
|
use DB;
|
|
|
|
use Illuminate\Http\Request;
|
2019-01-03 04:59:19 +00:00
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
2024-11-19 10:45:52 +00:00
|
|
|
use Purify;
|
2018-04-19 05:55:58 +00:00
|
|
|
|
|
|
|
class CommentController extends Controller
|
|
|
|
{
|
2018-08-10 02:04:48 +00:00
|
|
|
public function showAll(Request $request, $username, int $id)
|
|
|
|
{
|
2020-02-18 07:19:18 +00:00
|
|
|
abort(404);
|
2018-08-10 02:04:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 05:55:58 +00:00
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
if (Auth::check() === false) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
$this->validate($request, [
|
2024-03-10 10:19:44 +00:00
|
|
|
'item' => 'required|integer|min:1',
|
|
|
|
'comment' => 'required|string|max:'.config_cache('pixelfed.max_caption_length'),
|
|
|
|
'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);
|
|
|
|
|
2024-03-10 10:19:44 +00:00
|
|
|
if ($status->comments_disabled == true) {
|
2019-04-03 06:08:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-21 02:09:46 +00:00
|
|
|
$filtered = UserFilter::whereUserId($status->profile_id)
|
|
|
|
->whereFilterableType('App\Profile')
|
2019-07-21 02:55:10 +00:00
|
|
|
->whereIn('filter_type', ['block'])
|
2019-07-21 02:09:46 +00:00
|
|
|
->whereFilterableId($profile->id)
|
|
|
|
->exists();
|
|
|
|
|
2024-03-10 10:19:44 +00:00
|
|
|
if ($filtered == true) {
|
2019-07-21 02:09:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-10 10:19:44 +00:00
|
|
|
$reply = DB::transaction(function () use ($comment, $status, $profile, $nsfw) {
|
2019-11-28 02:57:23 +00:00
|
|
|
$scope = $profile->is_private == true ? 'private' : 'public';
|
2024-11-19 10:45:52 +00:00
|
|
|
$reply = new Status;
|
2019-04-13 20:44:58 +00:00
|
|
|
$reply->profile_id = $profile->id;
|
2020-05-23 07:38:23 +00:00
|
|
|
$reply->is_nsfw = $nsfw;
|
2024-11-19 10:45:52 +00:00
|
|
|
$reply->caption = Purify::clean($comment);
|
2019-04-13 20:44:58 +00:00
|
|
|
$reply->in_reply_to_id = $status->id;
|
|
|
|
$reply->in_reply_to_profile_id = $status->profile_id;
|
2019-11-28 02:57:23 +00:00
|
|
|
$reply->scope = $scope;
|
|
|
|
$reply->visibility = $scope;
|
2019-04-13 20:44:58 +00:00
|
|
|
$reply->save();
|
|
|
|
|
|
|
|
return $reply;
|
|
|
|
});
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2021-01-30 23:25:50 +00:00
|
|
|
StatusService::del($status->id);
|
2021-12-11 04:55:42 +00:00
|
|
|
NewStatusPipeline::dispatch($reply);
|
2018-08-28 03:07:36 +00:00
|
|
|
CommentPipeline::dispatch($status, $reply);
|
|
|
|
|
|
|
|
if ($request->ajax()) {
|
2024-11-19 10:45:52 +00:00
|
|
|
$fractal = new Fractal\Manager;
|
|
|
|
$fractal->setSerializer(new ArraySerializer);
|
|
|
|
$entity = new Fractal\Resource\Item($reply, new StatusTransformer);
|
2019-01-03 04:59:19 +00:00
|
|
|
$entity = $fractal->createData($entity)->toArray();
|
|
|
|
$response = [
|
2021-12-11 04:55:42 +00:00
|
|
|
'code' => 200,
|
|
|
|
'msg' => 'Comment saved',
|
|
|
|
'username' => $profile->username,
|
|
|
|
'url' => $reply->url(),
|
|
|
|
'profile' => $profile->url(),
|
2019-01-03 04:59:19 +00:00
|
|
|
'comment' => $reply->caption,
|
|
|
|
'entity' => $entity,
|
|
|
|
];
|
2018-08-28 03:07:36 +00:00
|
|
|
} else {
|
|
|
|
$response = redirect($status->url());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
2018-04-19 05:55:58 +00:00
|
|
|
}
|
|
|
|
}
|