diff --git a/files/classes/comment.py b/files/classes/comment.py index ca0b9828d..4d6e74406 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -1,16 +1,18 @@ +from os import environ import re +import time from urllib.parse import urlencode, urlparse, parse_qs + from flask import * from sqlalchemy import * from sqlalchemy.orm import relationship, deferred, lazyload -from files.classes.votes import CommentVote -from files.helpers.lazy import lazy -from files.helpers.const import SLURS + from files.__main__ import Base -from .flags import CommentFlag -from os import environ -import time +from files.classes.votes import CommentVote from files.helpers.const import AUTOPOLLER_ACCOUNT +from files.helpers.lazy import lazy +from .flags import CommentFlag +from ..helpers.word_censor import censor_slurs site = environ.get("DOMAIN").strip() @@ -298,8 +300,7 @@ class Comment(Base): if not body: return "" - if not v or v.slurreplacer: - for s, r in SLURS.items(): body = body.replace(s, r) + body = censor_slurs(body) if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com") @@ -325,8 +326,7 @@ class Comment(Base): if not body: return "" - if not v or v.slurreplacer: - for s, r in SLURS.items(): body = body.replace(s, r) + body = censor_slurs(body) if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com") diff --git a/files/classes/submission.py b/files/classes/submission.py index 214beee0e..63ace1e42 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -1,21 +1,24 @@ -from flask import render_template, g +from os import environ +import random +import re +import time +from urllib.parse import urlparse + +from flask import render_template from sqlalchemy import * from sqlalchemy.orm import relationship, deferred -import re, random -from urllib.parse import urlparse -from files.helpers.lazy import lazy -from files.helpers.const import SLURS, AUTOPOLLER_ACCOUNT + from files.__main__ import Base +from files.helpers.const import SLURS, AUTOPOLLER_ACCOUNT +from files.helpers.lazy import lazy from .flags import Flag -from os import environ -import time +from ..helpers.word_censor import censor_slurs site = environ.get("DOMAIN").strip() site_name = environ.get("SITE_NAME").strip() class Submission(Base): - __tablename__ = "submissions" id = Column(BigInteger, primary_key=True) @@ -339,9 +342,7 @@ class Submission(Base): if self.club and not (v and v.paid_dues): return "COUNTRY CLUB ONLY" body = self.body_html - if not v or v.slurreplacer: - for s,r in SLURS.items(): - body = body.replace(s, r) + body = censor_slurs(body) if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com") if v and v.nitter: body = body.replace("www.twitter.com", "nitter.net").replace("twitter.com", "nitter.net") @@ -351,9 +352,7 @@ class Submission(Base): if self.club and not (v and v.paid_dues): return "COUNTRY CLUB ONLY" body = self.body - if not v or v.slurreplacer: - for s,r in SLURS.items(): - body = body.replace(s, r) + body = censor_slurs(body) if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com") if v and v.nitter: body = body.replace("www.twitter.com", "nitter.net").replace("twitter.com", "nitter.net") diff --git a/files/helpers/word_censor.py b/files/helpers/word_censor.py index 294bde420..dabf74cdf 100644 --- a/files/helpers/word_censor.py +++ b/files/helpers/word_censor.py @@ -37,6 +37,9 @@ def sub_matcher(match: Match): def censor_slurs(v, body): + if v and not v.slurreplacer: + return body + for (slur, replace) in SLURS.items(): for variation in create_variations_slur_regex(slur): try: diff --git a/test/files/helpers/test_word_censor.py b/test/files/helpers/test_word_censor.py index 9ab8ecdb2..86b35a259 100644 --- a/test/files/helpers/test_word_censor.py +++ b/test/files/helpers/test_word_censor.py @@ -74,3 +74,18 @@ def test_censor_slurs(): assert_that(censor_slurs(None, "LLM is a manlet hehe")).is_equal_to("LLM is a little king hehe") assert_that(censor_slurs(None, "LLM is :marseycapitalistmanlet: hehe")) \ .is_equal_to("LLM is :marseycapitalistmanlet: hehe") + + +@patch("files.helpers.word_censor.SLURS", {'retard': 'r-slur', 'manlet': 'little king'}) +def test_censor_slurs_does_not_censor_on_flag_disabled(): + word_censor.REPLACE_MAP = create_replace_map() + + class V: + def __init__(self, slurreplacer): + self.slurreplacer = slurreplacer + + v = V(False) + assert_that(censor_slurs(v, "

retard

")).is_equal_to("

retard

") + + v = V(True) + assert_that(censor_slurs(v, "

retard

")).is_equal_to("

r-slur

")