diff --git a/person.py b/person.py index 90a43286c..5c1cea8e1 100644 --- a/person.py +++ b/person.py @@ -38,6 +38,7 @@ from roles import actor_roles_from_list from roles import get_actor_roles_list from media import process_meta_data from flags import is_image_file +from utils import get_person_icon from utils import account_is_indexable from utils import get_image_mime_type from utils import get_instance_url @@ -1995,14 +1996,9 @@ def get_person_avatar_url(base_dir: str, person_url: str, return im_path if person_json.get('icon'): - if isinstance(person_json['icon'], dict): - if person_json['icon'].get('url'): - url_str = get_url_from_post(person_json['icon']['url']) - if '.svg' not in url_str.lower(): - return remove_html(url_str) - else: - print('DEBUG: get_person_avatar_url icon is not a dict ' + - str(person_json['icon'])) + person_icon_url = get_person_icon(person_json) + if person_icon_url: + return person_icon_url return None diff --git a/posts.py b/posts.py index 3246dbfe4..9f127828d 100644 --- a/posts.py +++ b/posts.py @@ -41,6 +41,7 @@ from flags import contains_private_key from flags import has_group_type from flags import is_premium_account from flags import url_permitted +from utils import get_person_icon from utils import remove_post_from_index from utils import replace_strings from utils import valid_content_warning @@ -448,14 +449,9 @@ def get_person_box(signing_priv_key_pem: str, origin_domain: str, shared_inbox = person_json['endpoints']['sharedInbox'] avatar_url = None if person_json.get('icon'): - icon_dict = person_json['icon'] - if isinstance(icon_dict, list): - if len(icon_dict) > 0: - icon_dict = icon_dict[-1] - if isinstance(icon_dict, dict): - if icon_dict.get('url'): - url_str = get_url_from_post(icon_dict['url']) - avatar_url = remove_html(url_str) + url_str = get_person_icon(person_json) + if url_str: + avatar_url = remove_html(url_str) display_name = None possible_display_name = None if person_json.get('name'): diff --git a/utils.py b/utils.py index ab59176b0..c9841fbb2 100644 --- a/utils.py +++ b/utils.py @@ -147,6 +147,26 @@ def get_url_from_post(url_field) -> str: return '' +def get_person_icon(person_json: {}) -> str: + """Returns an icon url for an actor + """ + if not person_json.get('icon'): + return '' + person_icon = person_json['icon'] + if isinstance(person_icon, list): + # choose the first icon available + person_icon = person_json['icon'][0] + if isinstance(person_icon, dict): + if person_icon.get('url'): + url_str = get_url_from_post(person_icon['url']) + if '.svg' not in url_str.lower(): + return remove_html(url_str) + else: + print('DEBUG: get_person_icon icon is not a dict ' + + str(person_icon)) + return '' + + def get_attributed_to(field) -> str: """Returns the actor """ diff --git a/webapp_moderation.py b/webapp_moderation.py index 3f28f6f70..94a575fc0 100644 --- a/webapp_moderation.py +++ b/webapp_moderation.py @@ -10,6 +10,7 @@ __module_group__ = "Moderation" import os from flags import is_editor from flags import is_artist +from utils import get_person_icon from utils import data_dir from utils import get_url_from_post from utils import remove_html @@ -409,8 +410,8 @@ def html_moderation_info(translate: {}, base_dir: str, avatar_url = '' ext = '' if actor_json.get('icon'): - if actor_json['icon'].get('url'): - url_str = get_url_from_post(actor_json['icon']['url']) + url_str = get_person_icon(actor_json) + if url_str: avatar_url = remove_html(url_str) if '.' in avatar_url: ext = '.' + avatar_url.split('.')[-1] diff --git a/webapp_profile.py b/webapp_profile.py index 73af0e5f6..5363b544b 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -16,6 +16,7 @@ from flags import is_system_account from flags import is_group_account from flags import is_valid_date from flags import is_premium_account +from utils import get_person_icon from utils import text_mode_removals from utils import replace_strings from utils import data_dir @@ -314,14 +315,9 @@ def html_profile_after_search(authorized: bool, avatar_url = '' if profile_json.get('icon'): - icon_dict = profile_json['icon'] - if isinstance(icon_dict, list): - if len(icon_dict) > 0: - icon_dict = icon_dict[-1] - if isinstance(icon_dict, dict): - if icon_dict.get('url'): - url_str = get_url_from_post(icon_dict['url']) - avatar_url = remove_html(url_str) + url_str = get_person_icon(profile_json) + if url_str: + avatar_url = remove_html(url_str) if not avatar_url: avatar_url = get_person_avatar_url(base_dir, person_url, person_cache) display_name = search_nickname diff --git a/webapp_search.py b/webapp_search.py index 9da5af943..f3454fd65 100644 --- a/webapp_search.py +++ b/webapp_search.py @@ -12,6 +12,7 @@ from shutil import copyfile import urllib.parse from flags import is_editor from flags import is_public_post +from utils import get_person_icon from utils import data_dir from utils import get_post_attachments from utils import get_url_from_post @@ -590,7 +591,7 @@ def html_skills_search(actor: str, translate: {}, base_dir: str, skill_level_str = '0' + skill_level_str if skill_level < 10: skill_level_str = '0' + skill_level_str - url_str = get_url_from_post(actor_json['icon']['url']) + url_str = get_person_icon(actor_json) icon_url = remove_html(url_str) index_str = \ skill_level_str + ';' + actor + ';' + \ @@ -631,8 +632,7 @@ def html_skills_search(actor: str, translate: {}, base_dir: str, skill_level_str = '0' + skill_level_str if skill_level < 10: skill_level_str = '0' + skill_level_str - url_str = \ - get_url_from_post(actor_json['icon']['url']) + url_str = get_person_icon(actor_json) icon_url = remove_html(url_str) index_str = \ skill_level_str + ';' + actor + ';' + \