Move like function

main
Bob Mottram 2025-05-28 16:34:46 +01:00
parent 1994896e2f
commit 31a560f8f1
4 changed files with 70 additions and 71 deletions

View File

@ -10,7 +10,6 @@ __status__ = "Production"
__module_group__ = "Daemon GET" __module_group__ = "Daemon GET"
import os import os
from utils import undo_likes_collection_entry
from utils import is_dm from utils import is_dm
from utils import get_cached_post_filename from utils import get_cached_post_filename
from utils import load_json from utils import load_json
@ -27,6 +26,7 @@ from httpcodes import http_404
from posts import get_original_post_from_announce_url from posts import get_original_post_from_announce_url
from fitnessFunctions import fitness_performance from fitnessFunctions import fitness_performance
from like import update_likes_collection from like import update_likes_collection
from like import undo_likes_collection_entry
from webapp_post import individual_post_as_html from webapp_post import individual_post_as_html

View File

@ -20,7 +20,6 @@ from utils import remove_html
from utils import is_dm from utils import is_dm
from utils import get_cached_post_filename from utils import get_cached_post_filename
from utils import load_json from utils import load_json
from utils import undo_likes_collection_entry
from utils import locate_post from utils import locate_post
from utils import acct_handle_dir from utils import acct_handle_dir
from utils import has_object_string_object 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 has_users_path
from utils import get_domain_from_actor from utils import get_domain_from_actor
from utils import get_nickname_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 unfollower_of_account
from follow import follower_approval_active from follow import follower_approval_active
from bookmarks import undo_bookmarks_collection_entry from bookmarks import undo_bookmarks_collection_entry

69
like.py
View File

@ -22,7 +22,6 @@ from utils import remove_id_ending
from utils import get_nickname_from_actor from utils import get_nickname_from_actor
from utils import get_domain_from_actor from utils import get_domain_from_actor
from utils import locate_post from utils import locate_post
from utils import undo_likes_collection_entry
from utils import local_actor_url from utils import local_actor_url
from utils import load_json from utils import load_json
from utils import save_json from utils import save_json
@ -526,3 +525,71 @@ def update_likes_collection(recent_posts_cache: {},
print('DEBUG: saving post with likes added') print('DEBUG: saving post with likes added')
pprint(post_json_object) pprint(post_json_object)
save_json(post_json_object, post_filename) 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)

View File

@ -2607,74 +2607,6 @@ def get_file_case_insensitive(path: str) -> str:
return None 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: {}, def undo_reaction_collection_entry(recent_posts_cache: {},
base_dir: str, post_filename: str, base_dir: str, post_filename: str,
actor: str, domain: str, debug: bool, actor: str, domain: str, debug: bool,