From 388ecf2b2f2d8e3a0ffffe4af1d9e3d53206f5c8 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 28 May 2025 17:25:11 +0100 Subject: [PATCH] Move reaction function --- daemon_get_buttons_reaction.py | 2 +- inbox_receive_undo.py | 2 +- reaction.py | 70 +++++++++++++++++++++++++++++++++- utils.py | 69 --------------------------------- 4 files changed, 71 insertions(+), 72 deletions(-) diff --git a/daemon_get_buttons_reaction.py b/daemon_get_buttons_reaction.py index 87eb6259b..80f04e5ed 100644 --- a/daemon_get_buttons_reaction.py +++ b/daemon_get_buttons_reaction.py @@ -11,7 +11,6 @@ __module_group__ = "Daemon GET" import os import urllib.parse -from utils import undo_reaction_collection_entry from utils import get_cached_post_filename from utils import load_json from utils import locate_post @@ -26,6 +25,7 @@ from httpcodes import http_404 from posts import get_original_post_from_announce_url from daemon_utils import post_to_outbox from fitnessFunctions import fitness_performance +from reaction import undo_reaction_collection_entry from reaction import update_reaction_collection from follow import follower_approval_active from webapp_post import individual_post_as_html diff --git a/inbox_receive_undo.py b/inbox_receive_undo.py index 95c9626e9..8a91adc52 100644 --- a/inbox_receive_undo.py +++ b/inbox_receive_undo.py @@ -15,7 +15,6 @@ from utils import has_object_dict from utils import remove_domain_port from utils import remove_id_ending from utils import get_url_from_post -from utils import undo_reaction_collection_entry from utils import remove_html from utils import is_dm from utils import get_cached_post_filename @@ -35,6 +34,7 @@ from follow import unfollower_of_account from follow import follower_approval_active from bookmarks import undo_bookmarks_collection_entry from webapp_post import individual_post_as_html +from reaction import undo_reaction_collection_entry def _receive_undo_follow(base_dir: str, message_json: {}, diff --git a/reaction.py b/reaction.py index c31b7d7dd..d509efb02 100644 --- a/reaction.py +++ b/reaction.py @@ -25,7 +25,6 @@ from utils import remove_id_ending from utils import get_nickname_from_actor from utils import get_domain_from_actor from utils import locate_post -from utils import undo_reaction_collection_entry from utils import local_actor_url from utils import load_json from utils import save_json @@ -695,3 +694,72 @@ def html_emoji_reactions(post_json_object: {}, interactive: bool, html_str += ' \n' html_str += '\n' return html_str + + +def undo_reaction_collection_entry(recent_posts_cache: {}, + base_dir: str, post_filename: str, + actor: str, domain: str, debug: bool, + post_json_object: {}, + emoji_content: str) -> None: + """Undoes an emoji reaction for a particular actor + """ + if not post_json_object: + post_json_object = load_json(post_filename) + if not post_json_object: + return + # remove any cached version of this post so that the + # like icon is changed + nickname = get_nickname_from_actor(actor) + if not nickname: + return + 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): + try: + os.remove(cached_post_filename) + except OSError: + print('EX: undo_reaction_collection_entry ' + + 'unable to delete cached post ' + + str(cached_post_filename)) + remove_post_from_cache(post_json_object, recent_posts_cache) + + if not post_json_object.get('type'): + return + if post_json_object['type'] != 'Create': + return + obj = post_json_object + if has_object_dict(post_json_object): + obj = post_json_object['object'] + if not obj.get('reactions'): + return + if not isinstance(obj['reactions'], dict): + return + if not obj['reactions'].get('items'): + return + total_items = 0 + if obj['reactions'].get('totalItems'): + total_items = obj['reactions']['totalItems'] + item_found = False + for like_item in obj['reactions']['items']: + if not like_item.get('actor'): + continue + if like_item['actor'] == actor and \ + like_item['content'] == emoji_content: + if debug: + print('DEBUG: emoji reaction was removed for ' + actor) + obj['reactions']['items'].remove(like_item) + item_found = True + break + if not item_found: + return + if total_items == 1: + if debug: + print('DEBUG: emoji reaction was removed from post') + del obj['reactions'] + else: + itlen = len(obj['reactions']['items']) + obj['reactions']['totalItems'] = itlen + + save_json(post_json_object, post_filename) diff --git a/utils.py b/utils.py index b3bf59bbd..f5c4f51ec 100644 --- a/utils.py +++ b/utils.py @@ -2607,75 +2607,6 @@ def get_file_case_insensitive(path: str) -> str: return None -def undo_reaction_collection_entry(recent_posts_cache: {}, - base_dir: str, post_filename: str, - actor: str, domain: str, debug: bool, - post_json_object: {}, - emoji_content: str) -> None: - """Undoes an emoji reaction for a particular actor - """ - if not post_json_object: - post_json_object = load_json(post_filename) - if not post_json_object: - return - # remove any cached version of this post so that the - # like icon is changed - nickname = get_nickname_from_actor(actor) - if not nickname: - return - 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): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: undo_reaction_collection_entry ' + - 'unable to delete cached post ' + - str(cached_post_filename)) - remove_post_from_cache(post_json_object, recent_posts_cache) - - if not post_json_object.get('type'): - return - if post_json_object['type'] != 'Create': - return - obj = post_json_object - if has_object_dict(post_json_object): - obj = post_json_object['object'] - if not obj.get('reactions'): - return - if not isinstance(obj['reactions'], dict): - return - if not obj['reactions'].get('items'): - return - total_items = 0 - if obj['reactions'].get('totalItems'): - total_items = obj['reactions']['totalItems'] - item_found = False - for like_item in obj['reactions']['items']: - if not like_item.get('actor'): - continue - if like_item['actor'] == actor and \ - like_item['content'] == emoji_content: - if debug: - print('DEBUG: emoji reaction was removed for ' + actor) - obj['reactions']['items'].remove(like_item) - item_found = True - break - if not item_found: - return - if total_items == 1: - if debug: - print('DEBUG: emoji reaction was removed from post') - del obj['reactions'] - else: - itlen = len(obj['reactions']['items']) - obj['reactions']['totalItems'] = itlen - - save_json(post_json_object, post_filename) - - def undo_announce_collection_entry(recent_posts_cache: {}, base_dir: str, post_filename: str, actor: str, domain: str,