From b97d35d07e2e010264d6ba62fd531cd261668775 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 20 Apr 2024 12:07:45 +0100 Subject: [PATCH] Display quote toots if permitted --- utils.py | 41 +++++++++++++++++++++++++++++------------ webapp_post.py | 12 ++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/utils.py b/utils.py index 1a35544b5..00c41a660 100644 --- a/utils.py +++ b/utils.py @@ -4755,16 +4755,16 @@ def save_reverse_timeline(base_dir: str, reverse_sequence: []) -> []: break -def is_quote_toot(post_json_object: str, content: str) -> bool: - """Returns true if the given post is a quote toot / quote tweet +def get_quote_toot_url(post_json_object: str) -> str: """ - # Pleroma/Misskey implementations - if post_json_object['object'].get('quoteUri') or \ - post_json_object['object'].get('quoteUrl') or \ - post_json_object['object'].get('quoteReply') or \ - post_json_object['object'].get('toot:quoteReply') or \ - post_json_object['object'].get('_misskey_quote'): - return True + """ + # adhoc quote toot implementations + object_quote_url_fields = ('quoteUri', 'quoteUrl', 'quoteReply', + 'toot:quoteReply', '_misskey_quote') + for fieldname in object_quote_url_fields: + if post_json_object['object'].get(fieldname): + return post_json_object['object'][fieldname] + # More correct ActivityPub implementation - adding a Link tag if post_json_object['object'].get('tag'): if isinstance(post_json_object['object']['tag'], list): @@ -4772,15 +4772,21 @@ def is_quote_toot(post_json_object: str, content: str) -> bool: if not isinstance(item, dict): continue if item.get('rel'): + mk_quote = False if isinstance(item['rel'], list): for rel_str in item['rel']: if not isinstance(rel_str, str): continue if '_misskey_quote' in rel_str: - return True + mk_quote = True elif isinstance(item['rel'], str): if '_misskey_quote' in item['rel']: - return True + mk_quote = True + if mk_quote: + if item.get('href'): + if isinstance(item['href'], str): + if resembles_url(item['href']): + return item['href'] if not item.get('type'): continue if not item.get('mediaType'): @@ -4793,7 +4799,18 @@ def is_quote_toot(post_json_object: str, content: str) -> bool: continue if 'json' not in item['mediaType']: continue - return True + if item.get('href'): + if isinstance(item['href'], str): + if resembles_url(item['href']): + return item['href'] + return '' + + +def is_quote_toot(post_json_object: str, content: str) -> bool: + """Returns true if the given post is a quote toot / quote tweet + """ + if get_quote_toot_url(post_json_object): + return True # Twitter-style indicator if content: if 'QT: ' in content: diff --git a/webapp_post.py b/webapp_post.py index dab6082aa..60c3706ef 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -24,6 +24,7 @@ from posts import post_is_muted from posts import get_person_box from posts import download_announce from posts import populate_replies_json +from utils import get_quote_toot_url from utils import get_post_attachments from utils import get_url_from_post from utils import date_from_string_format @@ -2845,6 +2846,17 @@ def individual_post_as_html(signing_priv_key_pem: str, object_content = html_replace_quote_marks(object_content) object_content = \ format_mixed_right_to_left(object_content, system_language) + # show quote toot link + quote_url = get_quote_toot_url(post_json_object) + if quote_url: + quote_url = quote_url.replace('.json', '') + quote_url_shown = quote_url + if len(quote_url_shown) > 40: + quote_url_shown = quote_url_shown[:40] + object_content += '

💬 ' + \ + quote_url_shown + '

\n' # append any edits object_content += edits_str else: