-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathArticleCommentController.php
More file actions
47 lines (37 loc) · 1.59 KB
/
ArticleCommentController.php
File metadata and controls
47 lines (37 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace App\Http\Controllers\Article;
use App\Enums\NotificationType;
use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\PreventXssService;
class ArticleCommentController extends Controller
{
public function store(string $id, string $slug, Request $request): JsonResponse
{
$data = $request->validate([
'content' => 'required|string'
]);
if (!$article = Article::fromIdAndSlug($id, $slug)->first()) {
return $this->jsonResponse(['message' => __('Article not found')], 404);
}
$user = \Auth::user();
if($user->recentlyCommentedOnArticle() && !$user->hasHighPermissions()) {
return $this->jsonResponse(['message' => __('You are commenting too fast')], 422);
}
if(strlen(preg_replace("/\[(\/?).*?\]/", '', $data['content'])) < 5) {
return $this->jsonResponse(['message' => __('Please, type a valid comment.')], 422);
}
$comment = $article->comments()->create([
'content' => PreventXssService::sanitize($data['content']),
'user_id' => $user->id
]);
$article->user->notify($user, NotificationType::ArticleCommented, route('articles.show', ['id' => $article->id, 'slug' => $article->slug]));
return $this->jsonResponse([
'comment' => $comment,
'message' => __('Comment created successfully'),
'href' => route('articles.show', ['id' => $article->id, 'slug' => $article->slug]) . '#comments'
]);
}
}