CommentController.php 3.0 KB

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