CommentController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth;
  5. use DB;
  6. use Cache;
  7. use App\Comment;
  8. use App\Jobs\CommentPipeline\CommentPipeline;
  9. use App\Jobs\StatusPipeline\NewStatusPipeline;
  10. use App\Util\Lexer\Autolink;
  11. use App\Profile;
  12. use App\Status;
  13. use App\UserFilter;
  14. use League\Fractal;
  15. use App\Transformer\Api\StatusTransformer;
  16. use League\Fractal\Serializer\ArraySerializer;
  17. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  18. class CommentController extends Controller
  19. {
  20. public function showAll(Request $request, $username, int $id)
  21. {
  22. abort(404);
  23. }
  24. public function store(Request $request)
  25. {
  26. if (Auth::check() === false) {
  27. abort(403);
  28. }
  29. $this->validate($request, [
  30. 'item' => 'required|integer|min:1',
  31. 'comment' => 'required|string|max:'.(int) config('pixelfed.max_caption_length'),
  32. ]);
  33. $comment = $request->input('comment');
  34. $statusId = $request->item;
  35. $user = Auth::user();
  36. $profile = $user->profile;
  37. $status = Status::findOrFail($statusId);
  38. if($status->comments_disabled == true) {
  39. return;
  40. }
  41. $filtered = UserFilter::whereUserId($status->profile_id)
  42. ->whereFilterableType('App\Profile')
  43. ->whereIn('filter_type', ['block'])
  44. ->whereFilterableId($profile->id)
  45. ->exists();
  46. if($filtered == true) {
  47. return;
  48. }
  49. $reply = DB::transaction(function() use($comment, $status, $profile) {
  50. $scope = $profile->is_private == true ? 'private' : 'public';
  51. $autolink = Autolink::create()->autolink($comment);
  52. $reply = new Status();
  53. $reply->profile_id = $profile->id;
  54. $reply->caption = e($comment);
  55. $reply->rendered = $autolink;
  56. $reply->in_reply_to_id = $status->id;
  57. $reply->in_reply_to_profile_id = $status->profile_id;
  58. $reply->scope = $scope;
  59. $reply->visibility = $scope;
  60. $reply->save();
  61. $status->reply_count++;
  62. $status->save();
  63. return $reply;
  64. });
  65. NewStatusPipeline::dispatch($reply, false);
  66. CommentPipeline::dispatch($status, $reply);
  67. if ($request->ajax()) {
  68. $fractal = new Fractal\Manager();
  69. $fractal->setSerializer(new ArraySerializer());
  70. $entity = new Fractal\Resource\Item($reply, new StatusTransformer());
  71. $entity = $fractal->createData($entity)->toArray();
  72. $response = [
  73. 'code' => 200,
  74. 'msg' => 'Comment saved',
  75. 'username' => $profile->username,
  76. 'url' => $reply->url(),
  77. 'profile' => $profile->url(),
  78. 'comment' => $reply->caption,
  79. 'entity' => $entity,
  80. ];
  81. } else {
  82. $response = redirect($status->url());
  83. }
  84. return $response;
  85. }
  86. }