diff --git a/daemon_get_profile.py b/daemon_get_profile.py index 5e9a9dd90..bf661e49a 100644 --- a/daemon_get_profile.py +++ b/daemon_get_profile.py @@ -12,8 +12,8 @@ import json from roles import get_actor_roles_list from skills import no_of_actor_skills from skills import get_skills_from_list -from utils import get_actor_status -from utils import actor_status_expired +from status import get_actor_status +from status import actor_status_expired from utils import get_nickname_from_actor from utils import load_json from utils import get_json_content_from_accept diff --git a/daemon_utils.py b/daemon_utils.py index c339fb7e0..a4e48dca0 100644 --- a/daemon_utils.py +++ b/daemon_utils.py @@ -26,8 +26,8 @@ from blocking import is_blocked_nickname from blocking import is_blocked_domain from content import valid_url_lengths from posts import add_to_field -from utils import actor_status_expired -from utils import get_actor_status +from status import actor_status_expired +from status import get_actor_status from utils import detect_mitm from utils import data_dir from utils import load_json diff --git a/inbox_receive.py b/inbox_receive.py index b89478b0c..ddca921ac 100644 --- a/inbox_receive.py +++ b/inbox_receive.py @@ -11,7 +11,7 @@ import os import time from flags import is_recent_post from flags import is_quote_toot -from utils import actor_status_expired +from status import actor_status_expired from utils import get_quote_toot_url from utils import get_actor_from_post_id from utils import contains_invalid_actor_url_chars diff --git a/status.py b/status.py new file mode 100644 index 000000000..2046d9256 --- /dev/null +++ b/status.py @@ -0,0 +1,68 @@ +__filename__ = "status.py" +__author__ = "Bob Mottram" +__license__ = "AGPL3+" +__version__ = "1.6.0" +__maintainer__ = "Bob Mottram" +__email__ = "bob@libreserver.org" +__status__ = "Production" +__module_group__ = "Core" +__accounts_data_path__ = None +__accounts_data_path_tests__ = False + +from utils import date_utcnow +from utils import date_from_string_format +from utils import remove_html +from utils import standardize_text + +MAX_STATUS_LENGTH = 100 + + +def get_actor_status(profile_json: {}) -> str: + """returns the actor status if it exists + https://codeberg.org/fediverse/fep/src/branch/main/fep/82f6/fep-82f6.md + """ + if not profile_json.get('sm:status'): + return '' + status = '' + if isinstance(profile_json['sm:status'], str): + status = profile_json['sm:status'] + elif isinstance(profile_json['sm:status'], dict): + if profile_json['sm:status'].get('content'): + possible_status = profile_json['sm:status']['content'] + if isinstance(status, str): + status = possible_status + if status: + status = remove_html(status) + if len(status) > MAX_STATUS_LENGTH: + status = status[:MAX_STATUS_LENGTH] + status = standardize_text(status) + return status + + +def actor_status_expired(actor_status_json: {}) -> bool: + """Has the given actor status expired? + """ + if not actor_status_json.get('endTime'): + return False + if not isinstance(actor_status_json['endTime'], str): + return False + if 'T' not in actor_status_json['endTime'] or \ + ':' not in actor_status_json['endTime']: + return False + date_formats = ( + "%Y-%m-%dT%H:%M:%S%z", + "%Y-%m-%dT%H:%M:%S%Z" + ) + status_end_time = None + try: + status_end_time = \ + date_from_string_format(actor_status_json['endTime'], + date_formats) + except BaseException: + return False + if not status_end_time: + return False + curr_time = date_utcnow() + if status_end_time < curr_time: + return True + return False diff --git a/tests.py b/tests.py index c1e70bc55..3d48d42a0 100644 --- a/tests.py +++ b/tests.py @@ -62,8 +62,8 @@ from flags import contains_pgp_public_key from flags import is_group_actor from flags import is_group_account from flags import is_right_to_left_text -from utils import actor_status_expired -from utils import get_actor_status +from status import actor_status_expired +from status import get_actor_status from utils import replace_strings from utils import valid_content_warning from utils import data_dir diff --git a/utils.py b/utils.py index fbc76d541..2ed30f841 100644 --- a/utils.py +++ b/utils.py @@ -23,8 +23,6 @@ from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes from followingCalendar import add_person_to_calendar -MAX_STATUS_LENGTH = 100 - VALID_HASHTAG_CHARS = \ set('_0123456789' + 'abcdefghijklmnopqrstuvwxyz' + @@ -5545,54 +5543,3 @@ def get_event_categories() -> []: 'THEATRE', 'WORKSHOPS_SKILL_SHARING' ) - - -def get_actor_status(profile_json: {}) -> str: - """returns the actor status if it exists - https://codeberg.org/fediverse/fep/src/branch/main/fep/82f6/fep-82f6.md - """ - if not profile_json.get('sm:status'): - return '' - status = '' - if isinstance(profile_json['sm:status'], str): - status = profile_json['sm:status'] - elif isinstance(profile_json['sm:status'], dict): - if profile_json['sm:status'].get('content'): - possible_status = profile_json['sm:status']['content'] - if isinstance(status, str): - status = possible_status - if status: - status = remove_html(status) - if len(status) > MAX_STATUS_LENGTH: - status = status[:MAX_STATUS_LENGTH] - status = standardize_text(status) - return status - - -def actor_status_expired(actor_status_json: {}) -> bool: - """Has the given actor status expired? - """ - if not actor_status_json.get('endTime'): - return False - if not isinstance(actor_status_json['endTime'], str): - return False - if 'T' not in actor_status_json['endTime'] or \ - ':' not in actor_status_json['endTime']: - return False - date_formats = ( - "%Y-%m-%dT%H:%M:%S%z", - "%Y-%m-%dT%H:%M:%S%Z" - ) - status_end_time = None - try: - status_end_time = \ - date_from_string_format(actor_status_json['endTime'], - date_formats) - except BaseException: - return False - if not status_end_time: - return False - curr_time = date_utcnow() - if status_end_time < curr_time: - return True - return False diff --git a/webapp_profile.py b/webapp_profile.py index 254625b05..09b1e8aab 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -16,8 +16,8 @@ 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 actor_status_expired -from utils import get_actor_status +from status import actor_status_expired +from status import get_actor_status from utils import get_person_icon from utils import text_mode_removals from utils import replace_strings