pixelfed/app/Http/Controllers/CommentController.php

90 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
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;
use League\Fractal;
use App\Transformer\Api\StatusTransformer;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
class CommentController extends Controller
{
2018-06-04 06:07:40 +00:00
public function show(Request $request, $username, int $id, int $cid)
2018-06-04 02:20:12 +00:00
{
2018-08-28 03:07:36 +00:00
$user = Profile::whereUsername($username)->firstOrFail();
$status = Status::whereProfileId($user->id)->whereInReplyToId($id)->findOrFail($cid);
return view('status.reply', compact('user', 'status'));
2018-06-04 02:20:12 +00:00
}
public function showAll(Request $request, $username, int $id)
{
2018-08-28 03:07:36 +00:00
$user = Profile::whereUsername($username)->firstOrFail();
$status = Status::whereProfileId($user->id)->findOrFail($id);
$replies = Status::whereInReplyToId($id)->paginate(40);
return view('status.comments', compact('user', 'status', 'replies'));
}
public function store(Request $request)
{
2018-08-28 03:07:36 +00:00
if (Auth::check() === false) {
abort(403);
}
$this->validate($request, [
2018-12-21 19:51:52 +00:00
'item' => 'required|integer',
'comment' => 'required|string|max:500',
]);
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);
Cache::forget('transform:status:'.$status->url());
2019-04-02 03:18:39 +00:00
$autolink = Autolink::create()->autolink($comment);
2018-08-28 03:07:36 +00:00
$reply = new Status();
$reply->profile_id = $profile->id;
$reply->caption = e($comment);
2019-04-02 03:18:39 +00:00
$reply->rendered = $autolink;
2018-08-28 03:07:36 +00:00
$reply->in_reply_to_id = $status->id;
$reply->in_reply_to_profile_id = $status->profile_id;
$reply->save();
NewStatusPipeline::dispatch($reply, false);
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;
}
}