CommentController.php 1.6 KB

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