minor sanitize refactor

master
Aevann1 2022-09-16 18:30:34 +02:00
parent 78bed8e66a
commit 2fb4ebdcb0
6 changed files with 21 additions and 21 deletions

View File

@ -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"""<p>New {mention_str} by <a href="https://old.reddit.com/u/{author}" rel="nofollow noopener noreferrer" target="_blank">/u/{author}</a></p><p><a href="https://old.reddit.com{permalink}?context=89" rel="nofollow noopener noreferrer" target="_blank">https://old.reddit.com{permalink}?context=89</a></p>{text}"""

View File

@ -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('&amp;','&')
@ -326,7 +326,7 @@ def sanitize(sanitized, edit=False, limit_pings=0, showmore=True, marsified=Fals
sanitized = video_sub_regex.sub(r'\1<video controls preload="metadata"><source src="\2"></video>', sanitized)
sanitized = audio_sub_regex.sub(r'\1<audio controls preload="metadata" src="\2"></audio>', 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)

View File

@ -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

View File

@ -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,

View File

@ -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)

View File

@ -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!")