From 2fb4ebdcb0b0ab5ae5bef5ccaa82a420983788be Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 16 Sep 2022 18:30:34 +0200 Subject: [PATCH] minor sanitize refactor --- files/helpers/offsitementions.py | 2 +- files/helpers/sanitize.py | 22 +++++++++++----------- files/routes/awards.py | 4 ++-- files/routes/chat.py | 2 +- files/routes/comments.py | 4 ++-- files/routes/posts.py | 8 ++++---- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/files/helpers/offsitementions.py b/files/helpers/offsitementions.py index 7cb745594..19dca258d 100644 --- a/files/helpers/offsitementions.py +++ b/files/helpers/offsitementions.py @@ -69,7 +69,7 @@ def notify_mentions(send_to, mentions, mention_str='site mention'): for m in mentions: author = m['author'] permalink = m['permalink'] - text = sanitize(m['text'], edit=True) + text = sanitize(m['text'], golden=False) notif_text = \ f"""

New {mention_str} by /u/{author}

https://old.reddit.com{permalink}?context=89

{text}""" diff --git a/files/helpers/sanitize.py b/files/helpers/sanitize.py index c6591fbad..8c11844a9 100644 --- a/files/helpers/sanitize.py +++ b/files/helpers/sanitize.py @@ -130,7 +130,7 @@ def callback(attrs, new=False): return attrs -def render_emoji(html, regexp, edit, marseys_used, b=False): +def render_emoji(html, regexp, golden, marseys_used, b=False): emojis = list(regexp.finditer(html)) captured = set() @@ -141,7 +141,7 @@ def render_emoji(html, regexp, edit, marseys_used, b=False): emoji = i.group(1).lower() attrs = '' if b: attrs += ' b' - if not edit and len(emojis) <= 20 and ('marsey' in emoji or emoji in marseys_const2): + if golden and len(emojis) <= 20 and ('marsey' in emoji or emoji in marseys_const2): if random() < 0.0025: attrs += ' g' elif random() < 0.00125: attrs += ' glow' @@ -193,7 +193,7 @@ def with_sigalrm_timeout(timeout: int): @with_sigalrm_timeout(2) -def sanitize(sanitized, edit=False, limit_pings=0, showmore=True, marsified=False, torture=False): +def sanitize(sanitized, golden=True, limit_pings=0, showmore=True, count_marseys=False, torture=False): sanitized = sanitized.strip() if torture: @@ -284,7 +284,7 @@ def sanitize(sanitized, edit=False, limit_pings=0, showmore=True, marsified=Fals marseys_used = set() emojis = list(emoji_regex.finditer(sanitized)) - if len(emojis) > 20: edit = True + if len(emojis) > 20: golden = False captured = [] for i in emojis: @@ -295,14 +295,14 @@ def sanitize(sanitized, edit=False, limit_pings=0, showmore=True, marsified=Fals if 'marseylong1' in old or 'marseylong2' in old or 'marseyllama1' in old or 'marseyllama2' in old: new = old.lower().replace(">", " class='mb-0'>") else: new = old.lower() - new = render_emoji(new, emoji_regex2, edit, marseys_used, True) + new = render_emoji(new, emoji_regex2, golden, marseys_used, True) sanitized = sanitized.replace(old, new) emojis = list(emoji_regex2.finditer(sanitized)) - if len(emojis) > 20: edit = True + if len(emojis) > 20: golden = False - sanitized = render_emoji(sanitized, emoji_regex2, edit, marseys_used) + sanitized = render_emoji(sanitized, emoji_regex2, golden, marseys_used) sanitized = sanitized.replace('&','&') @@ -326,7 +326,7 @@ def sanitize(sanitized, edit=False, limit_pings=0, showmore=True, marsified=Fals sanitized = video_sub_regex.sub(r'\1', sanitized) sanitized = audio_sub_regex.sub(r'\1', sanitized) - if not edit and not marsified: + if count_marseys: for marsey in g.db.query(Marsey).filter(Marsey.submitter_id==None, Marsey.name.in_(marseys_used)).all(): marsey.count += 1 g.db.add(marsey) @@ -403,7 +403,7 @@ def allowed_attributes_emojis(tag, name, value): @with_sigalrm_timeout(1) -def filter_emojis_only(title, edit=False, graceful=False, torture=False): +def filter_emojis_only(title, golden=True, count_marseys=False, graceful=False, torture=False): title = title.strip() if torture: @@ -413,9 +413,9 @@ def filter_emojis_only(title, edit=False, graceful=False, torture=False): marseys_used = set() - title = render_emoji(title, emoji_regex3, edit, marseys_used) + title = render_emoji(title, emoji_regex3, golden, marseys_used) - if not edit: + if count_marseys: for marsey in g.db.query(Marsey).filter(Marsey.submitter_id==None, Marsey.name.in_(marseys_used)).all(): marsey.count += 1 g.db.add(marsey) diff --git a/files/routes/awards.py b/files/routes/awards.py index b2e2f9848..5c3e451cc 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -358,7 +358,7 @@ def award_thing(v, thing_type, id): body = thing.body if author.owoify: body = owoify(body) body = marsify(body) - thing.body_html = sanitize(body, limit_pings=5, marsified=True) + thing.body_html = sanitize(body, limit_pings=5) g.db.add(thing) elif "Vampire" in kind and kind == v.house: if author.bite: author.bite += 86400 * 3 @@ -384,7 +384,7 @@ def award_thing(v, thing_type, id): body = thing.body body = owoify(body) if author.marsify: body = marsify(body) - thing.body_html = sanitize(body, limit_pings=5, marsified=True) + thing.body_html = sanitize(body, limit_pings=5) g.db.add(thing) elif ("Femboy" in kind and kind == v.house): if author.rainbow: author.rainbow += 86400 diff --git a/files/routes/chat.py b/files/routes/chat.py index 99dec99d2..cb96ede52 100644 --- a/files/routes/chat.py +++ b/files/routes/chat.py @@ -62,7 +62,7 @@ def speak(data, v): else: text = data[:1000].strip() if not text: return '', 403 - text_html = sanitize(text) + text_html = sanitize(text, count_marseys=True) data={ "avatar": v.profile_url, diff --git a/files/routes/comments.py b/files/routes/comments.py index fea703b20..b1e08fda4 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -252,7 +252,7 @@ def comment(v): torture = (v.agendaposter and not v.marseyawarded and parent_post.sub != 'chudrama' and parent_post.id not in ADMIGGERS) - body_html = sanitize(body_for_sanitize, limit_pings=5, marsified=v.marsify, torture=torture) + body_html = sanitize(body_for_sanitize, limit_pings=5, count_marseys=not v.marsify, torture=torture) if parent_post.id not in ADMIGGERS and '!wordle' not in body.lower() and AGENDAPOSTER_PHRASE not in body.lower(): @@ -677,7 +677,7 @@ def edit_comment(cid, v): torture = (v.agendaposter and not v.marseyawarded and c.post.sub != 'chudrama') - body_html = sanitize(body_for_sanitize, edit=True, limit_pings=5, marsified=v.marsify, torture=torture) + body_html = sanitize(body_for_sanitize, golden=False, limit_pings=5, torture=torture) if len(body_html) > 20000: abort(400) diff --git a/files/routes/posts.py b/files/routes/posts.py index d83e727a8..5e75687cd 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -407,7 +407,7 @@ def edit_post(pid, v): if title != p.title: torture = (v.agendaposter and not v.marseyawarded and p.sub != 'chudrama' and v.id == p.author_id) - title_html = filter_emojis_only(title, edit=True, torture=torture) + title_html = filter_emojis_only(title, golden=False, torture=torture) if v.id == p.author_id and v.marseyawarded and not marseyaward_title_regex.fullmatch(title_html): return {"error":"You can only type marseys!"}, 403 @@ -441,7 +441,7 @@ def edit_post(pid, v): torture = (v.agendaposter and not v.marseyawarded and p.sub != 'chudrama' and v.id == p.author_id) - body_html = sanitize(body, edit=True, limit_pings=100, showmore=False, torture=torture) + body_html = sanitize(body, golden=False, limit_pings=100, showmore=False, torture=torture) if v.id == p.author_id and v.marseyawarded and marseyaward_body_regex.search(body_html): return {"error":"You can only type marseys!"}, 403 @@ -721,7 +721,7 @@ def submit_post(v, sub=None): torture = (v.agendaposter and not v.marseyawarded and sub != 'chudrama') - title_html = filter_emojis_only(title, graceful=True, torture=torture) + title_html = filter_emojis_only(title, graceful=True, count_marseys=True, torture=torture) if v.marseyawarded and not marseyaward_title_regex.fullmatch(title_html): return error("You can only type marseys!") @@ -895,7 +895,7 @@ def submit_post(v, sub=None): torture = (v.agendaposter and not v.marseyawarded and sub != 'chudrama') - body_html = sanitize(body, limit_pings=100, showmore=False, torture=torture) + body_html = sanitize(body, count_marseys=True, limit_pings=100, showmore=False, torture=torture) if v.marseyawarded and marseyaward_body_regex.search(body_html): return error("You can only type marseys!")