Browse Source

Put comments that have http: or https: into the mod queue

axkuhta 1 year ago
parent
commit
28dd5a5ec3
2 changed files with 19 additions and 4 deletions
  1. 10 1
      app/Listeners/PremoderateComment.php
  2. 9 3
      app/Models/Comment.php

+ 10 - 1
app/Listeners/PremoderateComment.php

@@ -7,6 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Queue\InteractsWithQueue;
 
 use App\Models\Comment;
+use Illuminate\Support\Str;
 
 class PremoderateComment implements ShouldQueue
 {
@@ -23,6 +24,14 @@ class PremoderateComment implements ShouldQueue
      */
 	public function handle(CommentAdded $event): void
     {
-        dump($event);
+        $comment = $event->comment;
+
+        $result = Str::of($comment->content)->match('/(http:|https:)/');
+
+        if ($result == "") {
+			$comment->allow();
+        } else {
+			$comment->makePending();
+        }
     }
 }

+ 9 - 3
app/Models/Comment.php

@@ -10,9 +10,10 @@ class Comment extends Model
 {
 	use SoftDeletes;
 
-	const STATUS_PENDING	= 0;
-	const STATUS_PUBLISHED	= 1;
-	const STATUS_REJECTED	= 2;
+	const STATUS_NEW		= 0; // Ждут автоматической проверки
+	const STATUS_PENDING	= 1; // Ждут проверки модератором
+	const STATUS_PUBLISHED	= 2; // Опубликованы
+	const STATUS_REJECTED	= 3; // Не прошли проверку
 
 	// Наличие fillable требуется только при использовании mass assignment, т.е. создание записи из ассоциативного массива
 	protected $fillable = [
@@ -42,6 +43,11 @@ class Comment extends Model
 		$query->orderBy("created_at", "desc");
 	}
 
+	function makePending() {
+		$this->status = static::STATUS_PENDING;
+		$this->save();
+	}
+
 	function allow() {
 		$this->status = static::STATUS_PUBLISHED;
 		$this->save();