Skip to content

Commit

Permalink
Fix spam detector handling of unreadable special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Nov 23, 2024
1 parent 767bb5c commit 2ec13f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/Services/SpamDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public function hasTooManySpecialCharacters()
->squish()
->wordCount();

if($countWords === 0) {
return true;
}

$diff = ($withSpecialCharacters - $withOutSpecialCharacters) / 2;

// Proportion of special characters in the message
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/SpamDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,10 @@ public function testMessageOneSpecialCharacters(): void
$spamDetector = new SpamDetector('🔥');
$this->assertTrue($spamDetector->hasTooManySpecialCharacters());
}

public function testMessageSpecialCharacters(): void
{
$spamDetector = new SpamDetector('✅ 🔤🔤🔤🔤 🔤🔤🔤🔤🔤🔤 ✅ 🔠🔠🔠🔠🔠 🔠 🔠🔠🔠🔠🔠🔠🔠 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥 🔥🔥🔥🔥🔥 ♦️🔺🔺♦️♦️ ♦️🔺🔺🔺 4️⃣2️⃣5️⃣💲 🔡 🔡🔡🔡🔡 ⚪️⚪️🔴⚪️⚪️🔴 🔴⚪️⚪️ 🔤🔤🔤🔤🔤🔤 🔤 🔤🔤');
$this->assertTrue($spamDetector->hasTooManySpecialCharacters());
}
}

0 comments on commit 2ec13f5

Please sign in to comment.