2018-04-18 23:55:58 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2019-01-02 21:59:19 -07:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Auth;
|
2019-04-13 14:44:58 -06:00
|
|
|
use DB;
|
2019-02-15 06:30:06 +01:00
|
|
|
use Cache;
|
2019-01-02 21:59:19 -07:00
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Comment;
|
2018-06-04 02:16:33 -06:00
|
|
|
use App\Jobs\CommentPipeline\CommentPipeline;
|
2018-05-26 16:46:07 -06:00
|
|
|
use App\Jobs\StatusPipeline\NewStatusPipeline;
|
2019-04-01 21:18:39 -06:00
|
|
|
use App\Util\Lexer\Autolink;
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Profile;
|
|
|
|
use App\Status;
|
2019-07-20 20:09:46 -06:00
|
|
|
use App\UserFilter;
|
2019-01-02 21:59:19 -07:00
|
|
|
use League\Fractal;
|
|
|
|
use App\Transformer\Api\StatusTransformer;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
2018-04-18 23:55:58 -06:00
|
|
|
|
|
|
|
class CommentController extends Controller
|
|
|
|
{
|
2018-08-09 20:04:48 -06:00
|
|
|
public function showAll(Request $request, $username, int $id)
|
|
|
|
{
|
2020-02-18 00:19:18 -07:00
|
|
|
abort(404);
|
2018-08-09 20:04:48 -06:00
|
|
|
}
|
|
|
|
|
2018-04-18 23:55:58 -06:00
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
if (Auth::check() === false) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
$this->validate($request, [
|
2019-05-12 22:35:54 -06:00
|
|
|
'item' => 'required|integer|min:1',
|
|
|
|
'comment' => 'required|string|max:'.(int) config('pixelfed.max_caption_length'),
|
2018-12-21 12:51:52 -07:00
|
|
|
]);
|
2018-08-28 03:07:36 +00:00
|
|
|
$comment = $request->input('comment');
|
|
|
|
$statusId = $request->item;
|
|
|
|
|
|
|
|
$user = Auth::user();
|
|
|
|
$profile = $user->profile;
|
|
|
|
$status = Status::findOrFail($statusId);
|
|
|
|
|
2019-04-03 00:08:32 -06:00
|
|
|
if($status->comments_disabled == true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-20 20:09:46 -06:00
|
|
|
$filtered = UserFilter::whereUserId($status->profile_id)
|
|
|
|
->whereFilterableType('App\Profile')
|
2019-07-20 20:55:10 -06:00
|
|
|
->whereIn('filter_type', ['block'])
|
2019-07-20 20:09:46 -06:00
|
|
|
->whereFilterableId($profile->id)
|
|
|
|
->exists();
|
|
|
|
|
|
|
|
if($filtered == true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-13 14:44:58 -06:00
|
|
|
$reply = DB::transaction(function() use($comment, $status, $profile) {
|
2019-11-27 19:57:23 -07:00
|
|
|
$scope = $profile->is_private == true ? 'private' : 'public';
|
2019-04-13 14:44:58 -06:00
|
|
|
$autolink = Autolink::create()->autolink($comment);
|
|
|
|
$reply = new Status();
|
|
|
|
$reply->profile_id = $profile->id;
|
|
|
|
$reply->caption = e($comment);
|
|
|
|
$reply->rendered = $autolink;
|
|
|
|
$reply->in_reply_to_id = $status->id;
|
|
|
|
$reply->in_reply_to_profile_id = $status->profile_id;
|
2019-11-27 19:57:23 -07:00
|
|
|
$reply->scope = $scope;
|
|
|
|
$reply->visibility = $scope;
|
2019-04-13 14:44:58 -06:00
|
|
|
$reply->save();
|
|
|
|
|
|
|
|
$status->reply_count++;
|
|
|
|
$status->save();
|
|
|
|
|
|
|
|
return $reply;
|
|
|
|
});
|
2018-08-28 03:07:36 +00:00
|
|
|
|
|
|
|
NewStatusPipeline::dispatch($reply, false);
|
|
|
|
CommentPipeline::dispatch($status, $reply);
|
|
|
|
|
|
|
|
if ($request->ajax()) {
|
2019-01-02 21:59:19 -07:00
|
|
|
$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;
|
2018-04-18 23:55:58 -06:00
|
|
|
}
|
|
|
|
}
|