diff --git a/announce.py b/announce.py index 900ae20b8..7fba3be29 100644 --- a/announce.py +++ b/announce.py @@ -30,7 +30,6 @@ from utils import get_nickname_from_actor from utils import get_domain_from_actor from utils import locate_post from utils import save_json -from utils import update_announce_collection from utils import local_actor_url from utils import replace_users_with_at from utils import has_actor @@ -631,3 +630,84 @@ def undo_announce_collection_entry(recent_posts_cache: {}, post_json_object['object']['shares']['totalItems'] = itlen save_json(post_json_object, post_filename) + + +def update_announce_collection(recent_posts_cache: {}, + base_dir: str, post_filename: str, + actor: str, nickname: str, domain: str, + debug: bool) -> None: + """Updates the announcements collection within a post + Confusingly this is known as "shares", but isn't the + same as shared items within shares.py + It's shares of posts, not shares of physical objects. + """ + post_json_object = load_json(post_filename) + if not post_json_object: + return + # remove any cached version of this announce so that the announce + # icon is changed + cached_post_filename = \ + get_cached_post_filename(base_dir, nickname, domain, + post_json_object) + if cached_post_filename: + if os.path.isfile(cached_post_filename): + print('update_announce_collection: removing ' + + cached_post_filename) + try: + os.remove(cached_post_filename) + except OSError: + if debug: + print('EX: update_announce_collection ' + + 'unable to delete cached post ' + + str(cached_post_filename)) + remove_post_from_cache(post_json_object, recent_posts_cache) + + if not has_object_dict(post_json_object): + if debug: + pprint(post_json_object) + print('DEBUG: post ' + post_filename + ' has no object') + return + post_url = remove_id_ending(post_json_object['id']) + collection_id = post_url + '/shares' + if not post_json_object['object'].get('shares'): + if debug: + print('DEBUG: Adding initial shares (announcements) to ' + + post_url) + announcements_json = { + "@context": [ + 'https://www.w3.org/ns/activitystreams', + 'https://w3id.org/security/v1' + ], + 'id': collection_id, + 'sharesOf': post_url, + 'type': 'Collection', + "totalItems": 1, + 'items': [{ + 'type': 'Announce', + 'actor': actor + }] + } + post_json_object['object']['shares'] = announcements_json + else: + if post_json_object['object']['shares'].get('items'): + shares_items = post_json_object['object']['shares']['items'] + for announce_item in shares_items: + if announce_item.get('actor'): + if announce_item['actor'] == actor: + return + new_announce = { + 'type': 'Announce', + 'actor': actor + } + post_json_object['object']['shares']['items'].append(new_announce) + itlen = len(post_json_object['object']['shares']['items']) + post_json_object['object']['shares']['totalItems'] = itlen + else: + if debug: + print('DEBUG: shares (announcements) section of post ' + + 'has no items list') + + if debug: + print('DEBUG: saving post with shares (announcements) added') + pprint(post_json_object) + save_json(post_json_object, post_filename) diff --git a/inbox_receive.py b/inbox_receive.py index b00f3cb92..41c529df8 100644 --- a/inbox_receive.py +++ b/inbox_receive.py @@ -18,7 +18,6 @@ from utils import get_actor_from_post_id from utils import contains_invalid_actor_url_chars from utils import get_attributed_to from utils import remove_eol -from utils import update_announce_collection from utils import get_protocol_prefixes from utils import contains_statuses from utils import delete_post @@ -76,6 +75,7 @@ from like import update_likes_collection from reaction import valid_emoji_content from reaction import update_reaction_collection from bookmarks import update_bookmarks_collection +from announce import update_announce_collection from announce import is_self_announce from speaker import update_speaker from webapp_post import individual_post_as_html diff --git a/utils.py b/utils.py index a4e71c871..2ddabc5db 100644 --- a/utils.py +++ b/utils.py @@ -16,7 +16,6 @@ import shutil import datetime import json import locale -from pprint import pprint import idna from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes @@ -2607,87 +2606,6 @@ def get_file_case_insensitive(path: str) -> str: return None -def update_announce_collection(recent_posts_cache: {}, - base_dir: str, post_filename: str, - actor: str, nickname: str, domain: str, - debug: bool) -> None: - """Updates the announcements collection within a post - Confusingly this is known as "shares", but isn't the - same as shared items within shares.py - It's shares of posts, not shares of physical objects. - """ - post_json_object = load_json(post_filename) - if not post_json_object: - return - # remove any cached version of this announce so that the announce - # icon is changed - cached_post_filename = \ - get_cached_post_filename(base_dir, nickname, domain, - post_json_object) - if cached_post_filename: - if os.path.isfile(cached_post_filename): - print('update_announce_collection: removing ' + - cached_post_filename) - try: - os.remove(cached_post_filename) - except OSError: - if debug: - print('EX: update_announce_collection ' + - 'unable to delete cached post ' + - str(cached_post_filename)) - remove_post_from_cache(post_json_object, recent_posts_cache) - - if not has_object_dict(post_json_object): - if debug: - pprint(post_json_object) - print('DEBUG: post ' + post_filename + ' has no object') - return - post_url = remove_id_ending(post_json_object['id']) - collection_id = post_url + '/shares' - if not post_json_object['object'].get('shares'): - if debug: - print('DEBUG: Adding initial shares (announcements) to ' + - post_url) - announcements_json = { - "@context": [ - 'https://www.w3.org/ns/activitystreams', - 'https://w3id.org/security/v1' - ], - 'id': collection_id, - 'sharesOf': post_url, - 'type': 'Collection', - "totalItems": 1, - 'items': [{ - 'type': 'Announce', - 'actor': actor - }] - } - post_json_object['object']['shares'] = announcements_json - else: - if post_json_object['object']['shares'].get('items'): - shares_items = post_json_object['object']['shares']['items'] - for announce_item in shares_items: - if announce_item.get('actor'): - if announce_item['actor'] == actor: - return - new_announce = { - 'type': 'Announce', - 'actor': actor - } - post_json_object['object']['shares']['items'].append(new_announce) - itlen = len(post_json_object['object']['shares']['items']) - post_json_object['object']['shares']['totalItems'] = itlen - else: - if debug: - print('DEBUG: shares (announcements) section of post ' + - 'has no items list') - - if debug: - print('DEBUG: saving post with shares (announcements) added') - pprint(post_json_object) - save_json(post_json_object, post_filename) - - def media_file_mime_type(filename: str) -> str: """Given a media filename return its mime type """ diff --git a/webapp_post.py b/webapp_post.py index f319f9ee4..31e5c54b1 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -17,6 +17,7 @@ from cache import get_person_from_cache from cache import update_recent_posts_cache from bookmarks import bookmark_from_id from bookmarks import bookmarked_by_person +from announce import update_announce_collection from announce import announced_by_person from announce import no_of_announces from like import liked_by_person @@ -38,6 +39,8 @@ from flags import is_chat_message from flags import is_pgp_encrypted from textmode import text_mode_removals from quote import get_quote_toot_url +from timeFunctions import date_from_string_format +from timeFunctions import convert_published_to_local_timezone from utils import save_json from utils import remove_header_tags from utils import get_actor_from_post_id @@ -53,8 +56,6 @@ from utils import dont_speak_hashtags from utils import remove_eol from utils import disallow_announce from utils import disallow_reply -from timeFunctions import date_from_string_format -from timeFunctions import convert_published_to_local_timezone from utils import remove_hash_from_post_id from utils import remove_html from utils import get_actor_languages_list @@ -63,7 +64,6 @@ from utils import get_content_from_post from utils import get_language_from_post from utils import get_summary_from_post from utils import has_object_dict -from utils import update_announce_collection from utils import is_dm from utils import reject_post_id from utils import get_config_param