diff --git a/daemon_get_buttons_like.py b/daemon_get_buttons_like.py index 67586b0c5..c23adae3a 100644 --- a/daemon_get_buttons_like.py +++ b/daemon_get_buttons_like.py @@ -10,7 +10,6 @@ __status__ = "Production" __module_group__ = "Daemon GET" import os -from utils import undo_likes_collection_entry from utils import is_dm from utils import get_cached_post_filename from utils import load_json @@ -27,6 +26,7 @@ from httpcodes import http_404 from posts import get_original_post_from_announce_url from fitnessFunctions import fitness_performance from like import update_likes_collection +from like import undo_likes_collection_entry from webapp_post import individual_post_as_html diff --git a/inbox_receive_undo.py b/inbox_receive_undo.py index 34d5a8094..95c9626e9 100644 --- a/inbox_receive_undo.py +++ b/inbox_receive_undo.py @@ -20,7 +20,6 @@ from utils import remove_html from utils import is_dm from utils import get_cached_post_filename from utils import load_json -from utils import undo_likes_collection_entry from utils import locate_post from utils import acct_handle_dir from utils import has_object_string_object @@ -31,6 +30,7 @@ from utils import get_actor_from_post from utils import has_users_path from utils import get_domain_from_actor from utils import get_nickname_from_actor +from like import undo_likes_collection_entry from follow import unfollower_of_account from follow import follower_approval_active from bookmarks import undo_bookmarks_collection_entry diff --git a/like.py b/like.py index 09fdbe784..e8012f542 100644 --- a/like.py +++ b/like.py @@ -22,7 +22,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_likes_collection_entry from utils import local_actor_url from utils import load_json from utils import save_json @@ -526,3 +525,71 @@ def update_likes_collection(recent_posts_cache: {}, print('DEBUG: saving post with likes added') pprint(post_json_object) save_json(post_json_object, post_filename) + + +def undo_likes_collection_entry(recent_posts_cache: {}, + base_dir: str, post_filename: str, + actor: str, domain: str, debug: bool, + post_json_object: {}) -> None: + """Undoes a like 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_likes_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('likes'): + return + if not isinstance(obj['likes'], dict): + return + if not obj['likes'].get('items'): + return + total_items = 0 + if obj['likes'].get('totalItems'): + total_items = obj['likes']['totalItems'] + item_found = False + for like_item in obj['likes']['items']: + if not like_item.get('actor'): + continue + if like_item['actor'] != actor: + continue + if debug: + print('DEBUG: like was removed for ' + actor) + obj['likes']['items'].remove(like_item) + item_found = True + break + if not item_found: + return + if total_items == 1: + if debug: + print('DEBUG: likes was removed from post') + del obj['likes'] + else: + itlen = len(obj['likes']['items']) + obj['likes']['totalItems'] = itlen + + save_json(post_json_object, post_filename) diff --git a/utils.py b/utils.py index 23b5ec4fb..b3bf59bbd 100644 --- a/utils.py +++ b/utils.py @@ -2607,74 +2607,6 @@ def get_file_case_insensitive(path: str) -> str: return None -def undo_likes_collection_entry(recent_posts_cache: {}, - base_dir: str, post_filename: str, - actor: str, domain: str, debug: bool, - post_json_object: {}) -> None: - """Undoes a like 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_likes_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('likes'): - return - if not isinstance(obj['likes'], dict): - return - if not obj['likes'].get('items'): - return - total_items = 0 - if obj['likes'].get('totalItems'): - total_items = obj['likes']['totalItems'] - item_found = False - for like_item in obj['likes']['items']: - if not like_item.get('actor'): - continue - if like_item['actor'] != actor: - continue - if debug: - print('DEBUG: like was removed for ' + actor) - obj['likes']['items'].remove(like_item) - item_found = True - break - if not item_found: - return - if total_items == 1: - if debug: - print('DEBUG: likes was removed from post') - del obj['likes'] - else: - itlen = len(obj['likes']['items']) - obj['likes']['totalItems'] = itlen - - save_json(post_json_object, post_filename) - - def undo_reaction_collection_entry(recent_posts_cache: {}, base_dir: str, post_filename: str, actor: str, domain: str, debug: bool,