diff --git a/files/classes/submission.py b/files/classes/submission.py index 8d977ab86..49f07369d 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -9,7 +9,7 @@ from sqlalchemy import * from sqlalchemy.orm import relationship, deferred from files.__main__ import Base -from files.helpers.const import SLURS, AUTOPOLLER_ACCOUNT +from files.helpers.const import AUTOPOLLER_ACCOUNT from files.helpers.lazy import lazy from .flags import Flag from ..helpers.word_censor import censor_slurs @@ -365,8 +365,7 @@ class Submission(Base): elif self.title_html: title = self.title_html else: title = self.title - if not v or v.slurreplacer: - for s,r in SLURS.items(): title = title.replace(s, r) + title = censor_slurs(title, v) return title @@ -375,8 +374,7 @@ class Submission(Base): if self.club and not (v and v.paid_dues) and not (v and v.admin_level == 6): return 'COUNTRY CLUB MEMBERS ONLY' else: title = self.title - if not v or v.slurreplacer: - for s,r in SLURS.items(): title = title.replace(s, r) + title = censor_slurs(title, v) return title diff --git a/files/helpers/word_censor.py b/files/helpers/word_censor.py index 369164fda..d7112a69a 100644 --- a/files/helpers/word_censor.py +++ b/files/helpers/word_censor.py @@ -43,7 +43,7 @@ def create_slur_regex() -> Pattern[str]: """Creates the regex that will find the slurs""" single_words = "|".join([slur.lower() for slur in SLURS.keys()]) - return re.compile(rf"(?i)(?<=\s|>)({single_words})([\s<,.])") + return re.compile(rf"(?i)(?<=\s|>)({single_words})(?=[\s<,.])") def create_replace_map() -> Dict[str, str]: @@ -60,9 +60,9 @@ REPLACE_MAP = create_replace_map() def sub_matcher(match: Match) -> str: """given a match returns the correct replacer string""" - found = match.group(1) + found = match.group(0) # if it does not find the correct capitalization, it tries the all lower, or return the original word - return (REPLACE_MAP.get(found) or REPLACE_MAP.get(found.lower()) or found) + match.group(2) + return REPLACE_MAP.get(found) or REPLACE_MAP.get(found.lower()) or found def censor_slurs(body: str, logged_user) -> str: diff --git a/test/files/helpers/test_word_censor.py b/test/files/helpers/test_word_censor.py index 00bf4fc2f..00d1c9548 100644 --- a/test/files/helpers/test_word_censor.py +++ b/test/files/helpers/test_word_censor.py @@ -52,7 +52,7 @@ def test_get_permutations_slur_wiht_link_replacer(): "retard": "r-slur", }) def test_create_slur_regex(): - expected = r"(?i)(?<=\s|>)(kill yourself|faggot|nig|retard)([\s<,.])" + expected = r"(?i)(?<=\s|>)(kill yourself|faggot|nig|retard)(?=[\s<,.])" assert_that(create_slur_regex()).is_equal_to(re.compile(expected)) @@ -91,19 +91,19 @@ def test_create_replace_map(): @patch("files.helpers.word_censor.REPLACE_MAP", {'retard': 'r-slur', 'Faggot': 'Cute twink', 'NIG': '🏀'}) def test_sub_matcher(): - regex = re.compile(r"(?i)(?<=\s|>)(kill yourself|retard|nig|faggot)([\s<,.])") + regex = re.compile(r"(?i)(?<=\s|>)(kill yourself|retard|nig|faggot)(?=[\s<,.])") match = regex.search("

retard

") - assert_that(sub_matcher(match)).is_equal_to("r-slur<") + assert_that(sub_matcher(match)).is_equal_to("r-slur") match = regex.search("

ReTaRd

") - assert_that(sub_matcher(match)).is_equal_to("r-slur<") + assert_that(sub_matcher(match)).is_equal_to("r-slur") match = regex.search("

NIG

") - assert_that(sub_matcher(match)).is_equal_to("🏀<") + assert_that(sub_matcher(match)).is_equal_to("🏀") match = regex.search("

Faggot

") - assert_that(sub_matcher(match)).is_equal_to("Cute twink ") + assert_that(sub_matcher(match)).is_equal_to("Cute twink") @patch("files.helpers.word_censor.SLURS", { @@ -149,11 +149,6 @@ def test_censor_slurs(): assert_that(censor_slurs('... i Hate carp ...', None)).is_equal_to('... i love Carp ...') assert_that(censor_slurs('... i hate a carp ...', None)).is_equal_to('... i hate a carp ...') - assert_that(censor_slurs("""

-I had a dream about this site last night -

""", None)).is_equal_to("""

-I had a dream about this site last night -

""") assert_that(censor_slurs("

retard Manlet NIG

", None)).is_equal_to("

r-slur Little king 🏀

")