CommentController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. use App\Services\StatusService;
  19. class CommentController extends Controller
  20. {
  21. public function showAll(Request $request, $username, int $id)
  22. {
  23. abort(404);
  24. }
  25. public function store(Request $request)
  26. {
  27. if (Auth::check() === false) {
  28. abort(403);
  29. }
  30. $this->validate($request, [
  31. 'item' => 'required|integer|min:1',
  32. 'comment' => 'required|string|max:'.(int) config('pixelfed.max_caption_length'),
  33. 'sensitive' => 'nullable|boolean'
  34. ]);
  35. $comment = $request->input('comment');
  36. $statusId = $request->input('item');
  37. $nsfw = $request->input('sensitive', false);
  38. $user = Auth::user();
  39. $profile = $user->profile;
  40. $status = Status::findOrFail($statusId);
  41. if($status->comments_disabled == true) {
  42. return;
  43. }
  44. $filtered = UserFilter::whereUserId($status->profile_id)
  45. ->whereFilterableType('App\Profile')
  46. ->whereIn('filter_type', ['block'])
  47. ->whereFilterableId($profile->id)
  48. ->exists();
  49. if($filtered == true) {
  50. return;
  51. }
  52. $reply = DB::transaction(function() use($comment, $status, $profile, $nsfw) {
  53. $scope = $profile->is_private == true ? 'private' : 'public';
  54. $autolink = Autolink::create()->autolink($comment);
  55. $reply = new Status();
  56. $reply->profile_id = $profile->id;
  57. $reply->is_nsfw = $nsfw;
  58. $reply->caption = e($comment);
  59. $reply->rendered = $autolink;
  60. $reply->in_reply_to_id = $status->id;
  61. $reply->in_reply_to_profile_id = $status->profile_id;
  62. $reply->scope = $scope;
  63. $reply->visibility = $scope;
  64. $reply->save();
  65. return $reply;
  66. });
  67. StatusService::del($status->id);
  68. NewStatusPipeline::dispatch($reply);
  69. CommentPipeline::dispatch($status, $reply);
  70. if ($request->ajax()) {
  71. $fractal = new Fractal\Manager();
  72. $fractal->setSerializer(new ArraySerializer());
  73. $entity = new Fractal\Resource\Item($reply, new StatusTransformer());
  74. $entity = $fractal->createData($entity)->toArray();
  75. $response = [
  76. 'code' => 200,
  77. 'msg' => 'Comment saved',
  78. 'username' => $profile->username,
  79. 'url' => $reply->url(),
  80. 'profile' => $profile->url(),
  81. 'comment' => $reply->caption,
  82. 'entity' => $entity,
  83. ];
  84. } else {
  85. $response = redirect($status->url());
  86. }
  87. return $response;
  88. }
  89. }