simplify conditions #183

This commit is contained in:
rimu 2024-05-14 22:04:52 +12:00
parent ac1d49a41d
commit ec7f38f0cb
2 changed files with 12 additions and 18 deletions

View file

@ -1322,23 +1322,6 @@ def create_post_reply(activity_log: ActivityPubLog, community: Community, in_rep
return None
post_id, parent_comment_id, root_id = find_reply_parent(in_reply_to)
# special case: add comment from auto-tldr bot to post body if body is empty
if post_id and not parent_comment_id and not root_id and user.ap_id == 'autotldr@lemmings.world':
post = Post.query.get(post_id)
if post and post.body_html == '':
if 'source' in request_json['object'] and isinstance(request_json['object']['source'], dict) and \
'mediaType' in request_json['object']['source'] and \
request_json['object']['source']['mediaType'] == 'text/markdown':
body = request_json['object']['source']['content']
# sometimes the bot includes its content inside spoiler tags, sometimes not. This is an attempt to make things look consistent.
if not '::: spoiler' in body:
post.body = "🤖 I'm a bot that provides automatic summaries for articles:\n::: spoiler Click here to see the summary\n" + body + '\n:::'
else:
post.body = body
post.body_html = markdown_to_html(post.body) + '<small><span class="render_username">Generated using AI by: <a href="/u/autotldr@lemmings.world" title="AutoTL;DR">AutoTL;DR</a></span></small>'
db.session.commit()
return None
if post_id or parent_comment_id or root_id:
# set depth to +1 of the parent depth
if parent_comment_id:
@ -1380,6 +1363,17 @@ def create_post_reply(activity_log: ActivityPubLog, community: Community, in_rep
if blocked_phrase in post_reply.body:
return None
post = Post.query.get(post_id)
# special case: add comment from auto-tldr bot to post body if body is empty
if user.ap_id == 'autotldr@lemmings.world' and post.body.strip() == '':
if not '::: spoiler' in post_reply.body:
post.body = "🤖 I'm a bot that provides automatic summaries for articles:\n::: spoiler Click here to see the summary\n" + post_reply.body + '\n:::'
else:
post.body = post_reply.body
post.body_html = allowlist_html(markdown_to_html(post.body) + '\n\n<small><span class="render_username">Generated using AI by: <a href="/u/autotldr@lemmings.world" title="AutoTL;DR">AutoTL;DR</a></span></small>')
db.session.commit()
return None
if post.comments_enabled:
anchor = None
if not parent_comment_id:

View file

@ -180,7 +180,7 @@ def allowlist_html(html: str) -> str:
if html is None or html == '':
return ''
allowed_tags = ['p', 'strong', 'a', 'ul', 'ol', 'li', 'em', 'blockquote', 'cite', 'br', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre',
'code', 'img', 'details', 'summary', 'table', 'tr', 'td', 'th', 'tbody', 'thead', 'hr', 'span']
'code', 'img', 'details', 'summary', 'table', 'tr', 'td', 'th', 'tbody', 'thead', 'hr', 'span', 'small']
# Parse the HTML using BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')