CommentController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Jobs\StatusPipeline\NewStatusPipeline;
  5. use Auth, Hashids;
  6. use App\{Comment, Profile, Status};
  7. class CommentController extends Controller
  8. {
  9. public function show(Request $request, $username, int $id, int $cid)
  10. {
  11. $user = Profile::whereUsername($username)->firstOrFail();
  12. $status = Status::whereProfileId($user->id)->whereInReplyToId($id)->findOrFail($cid);
  13. return view('status.reply', compact('user', 'status'));
  14. }
  15. public function store(Request $request)
  16. {
  17. if(Auth::check() === false) { abort(403); }
  18. $this->validate($request, [
  19. 'item' => 'required|integer',
  20. 'comment' => 'required|string|max:500'
  21. ]);
  22. $comment = $request->input('comment');
  23. $statusId = $request->item;
  24. $user = Auth::user();
  25. $profile = $user->profile;
  26. $status = Status::findOrFail($statusId);
  27. $reply = new Status();
  28. $reply->profile_id = $profile->id;
  29. $reply->caption = $comment;
  30. $reply->rendered = e($comment);
  31. $reply->in_reply_to_id = $status->id;
  32. $reply->in_reply_to_profile_id = $status->profile_id;
  33. $reply->save();
  34. NewStatusPipeline::dispatch($reply, false);
  35. if($request->ajax()) {
  36. $response = ['code' => 200, 'msg' => 'Comment saved', 'username' => $profile->username, 'url' => $reply->url(), 'profile' => $profile->url()];
  37. } else {
  38. $response = redirect($status->url());
  39. }
  40. return $response;
  41. }
  42. }