epicyon/like.py

485 lines
17 KiB
Python
Raw Normal View History

2020-04-01 10:44:33 +00:00
__filename__ = "like.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2022-02-03 13:58:20 +00:00
__version__ = "1.3.0"
2020-04-01 10:44:33 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-01 10:44:33 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "ActivityPub"
2019-07-02 11:13:45 +00:00
2021-09-03 12:00:23 +00:00
import os
from pprint import pprint
2021-12-26 17:12:07 +00:00
from utils import has_object_string
2021-12-26 15:54:46 +00:00
from utils import has_object_string_object
2021-12-26 17:12:07 +00:00
from utils import has_object_stringType
2021-12-26 18:17:37 +00:00
from utils import remove_domain_port
2021-12-26 10:57:03 +00:00
from utils import has_object_dict
2021-12-26 12:19:00 +00:00
from utils import has_users_path
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2021-12-27 11:20:57 +00:00
from utils import remove_id_ending
2021-12-27 20:47:05 +00:00
from utils import url_permitted
2021-12-27 22:19:18 +00:00
from utils import get_nickname_from_actor
2021-12-27 19:05:25 +00:00
from utils import get_domain_from_actor
2021-12-26 20:36:08 +00:00
from utils import locate_post
2021-12-27 23:23:07 +00:00
from utils import undo_likes_collection_entry
2021-12-26 17:53:07 +00:00
from utils import has_group_type
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2021-12-26 15:13:34 +00:00
from utils import load_json
2021-12-26 14:47:21 +00:00
from utils import save_json
2021-12-27 11:05:24 +00:00
from utils import remove_post_from_cache
2021-12-26 23:41:34 +00:00
from utils import get_cached_post_filename
2021-12-29 21:55:09 +00:00
from posts import send_signed_json
from session import post_json
from webfinger import webfinger_handle
2021-12-28 21:36:27 +00:00
from auth import create_basic_auth_header
2021-12-29 21:55:09 +00:00
from posts import get_person_box
2019-07-11 12:29:31 +00:00
2020-04-01 10:44:33 +00:00
2021-12-29 21:55:09 +00:00
def no_of_likes(post_json_object: {}) -> int:
2022-03-01 09:57:42 +00:00
"""Returns the number of likes on a given post
2021-11-10 12:16:03 +00:00
"""
2021-12-25 22:09:19 +00:00
obj = post_json_object
2021-12-26 10:57:03 +00:00
if has_object_dict(post_json_object):
2021-12-25 22:09:19 +00:00
obj = post_json_object['object']
2021-11-10 12:16:03 +00:00
if not obj.get('likes'):
return 0
if not isinstance(obj['likes'], dict):
return 0
if not obj['likes'].get('items'):
obj['likes']['items'] = []
obj['likes']['totalItems'] = 0
return len(obj['likes']['items'])
2021-12-29 21:55:09 +00:00
def liked_by_person(post_json_object: {}, nickname: str, domain: str) -> bool:
"""Returns True if the given post is liked by the given person
2019-08-01 09:05:09 +00:00
"""
2021-12-29 21:55:09 +00:00
if no_of_likes(post_json_object) == 0:
2019-08-01 09:05:09 +00:00
return False
2022-01-02 21:45:26 +00:00
actor_match = domain + '/users/' + nickname
2021-10-15 09:13:04 +00:00
2021-12-25 22:09:19 +00:00
obj = post_json_object
2021-12-26 10:57:03 +00:00
if has_object_dict(post_json_object):
2021-12-25 22:09:19 +00:00
obj = post_json_object['object']
2021-10-15 09:13:04 +00:00
for item in obj['likes']['items']:
2022-01-02 21:45:26 +00:00
if item['actor'].endswith(actor_match):
2019-08-01 09:05:09 +00:00
return True
return False
2020-04-01 10:44:33 +00:00
2021-12-29 21:55:09 +00:00
def _create_like(recent_posts_cache: {},
session, base_dir: str, federation_list: [],
nickname: str, domain: str, port: int,
2022-01-02 21:45:26 +00:00
cc_list: [], http_prefix: str,
object_url: str, actor_liked: str,
2021-12-29 21:55:09 +00:00
client_to_server: bool,
send_threads: [], postLog: [],
person_cache: {}, cached_webfingers: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2019-07-02 11:13:45 +00:00
"""Creates a like
2019-07-12 09:10:09 +00:00
actor is the person doing the liking
'to' might be a specific person (actor) whose post was liked
object is typically the url of the message which was liked
2019-07-02 11:13:45 +00:00
"""
2022-01-02 21:45:26 +00:00
if not url_permitted(object_url, federation_list):
2019-07-02 11:13:45 +00:00
return None
2022-01-02 21:45:26 +00:00
full_domain = get_full_domain(domain, port)
2019-08-07 11:02:15 +00:00
2022-01-02 21:45:26 +00:00
new_like_json = {
2019-08-18 11:07:06 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2019-07-02 11:13:45 +00:00
'type': 'Like',
2022-01-02 21:45:26 +00:00
'actor': local_actor_url(http_prefix, nickname, full_domain),
'object': object_url
2019-07-02 11:13:45 +00:00
}
2022-01-02 21:45:26 +00:00
if cc_list:
if len(cc_list) > 0:
new_like_json['cc'] = cc_list
2019-07-10 09:47:07 +00:00
# Extract the domain and nickname from a statuses link
2022-01-02 21:45:26 +00:00
liked_post_nickname = None
liked_post_domain = None
liked_post_port = None
2021-12-26 00:07:44 +00:00
group_account = False
2022-01-02 21:45:26 +00:00
if actor_liked:
liked_post_nickname = get_nickname_from_actor(actor_liked)
liked_post_domain, liked_post_port = get_domain_from_actor(actor_liked)
group_account = has_group_type(base_dir, actor_liked, person_cache)
2019-11-15 09:54:19 +00:00
else:
2022-01-02 21:45:26 +00:00
if has_users_path(object_url):
liked_post_nickname = get_nickname_from_actor(object_url)
liked_post_domain, liked_post_port = \
get_domain_from_actor(object_url)
if '/' + str(liked_post_nickname) + '/' in object_url:
actor_liked = \
object_url.split('/' + liked_post_nickname + '/')[0] + \
'/' + liked_post_nickname
2021-12-26 00:07:44 +00:00
group_account = \
2022-01-02 21:45:26 +00:00
has_group_type(base_dir, actor_liked, person_cache)
2019-07-10 09:47:07 +00:00
2022-01-02 21:45:26 +00:00
if liked_post_nickname:
post_filename = locate_post(base_dir, nickname, domain, object_url)
2021-12-26 23:41:34 +00:00
if not post_filename:
2021-12-25 16:17:53 +00:00
print('DEBUG: like base_dir: ' + base_dir)
2020-04-03 16:36:21 +00:00
print('DEBUG: like nickname: ' + nickname)
print('DEBUG: like domain: ' + domain)
2022-01-02 21:45:26 +00:00
print('DEBUG: like object_url: ' + object_url)
2019-07-11 12:29:31 +00:00
return None
2020-03-22 21:16:02 +00:00
2021-12-29 21:55:09 +00:00
update_likes_collection(recent_posts_cache,
2022-01-02 21:45:26 +00:00
base_dir, post_filename, object_url,
new_like_json['actor'],
2021-12-29 21:55:09 +00:00
nickname, domain, debug, None)
2022-01-02 21:45:26 +00:00
send_signed_json(new_like_json, session, base_dir,
2021-12-29 21:55:09 +00:00
nickname, domain, port,
2022-01-02 21:45:26 +00:00
liked_post_nickname, liked_post_domain,
liked_post_port,
2021-12-29 21:55:09 +00:00
'https://www.w3.org/ns/activitystreams#Public',
http_prefix, True, client_to_server, federation_list,
send_threads, postLog, cached_webfingers,
person_cache,
debug, project_version, None, group_account,
signing_priv_key_pem, 7367374)
2019-07-10 09:47:07 +00:00
2022-01-02 21:45:26 +00:00
return new_like_json
2019-07-10 09:47:07 +00:00
2020-04-01 10:44:33 +00:00
2021-12-29 21:55:09 +00:00
def like_post(recent_posts_cache: {},
session, base_dir: str, federation_list: [],
nickname: str, domain: str, port: int, http_prefix: str,
2022-01-02 21:45:26 +00:00
like_nickname: str, like_domain: str, like_port: int,
cc_list: [],
like_status_number: int, client_to_server: bool,
2021-12-29 21:55:09 +00:00
send_threads: [], postLog: [],
person_cache: {}, cached_webfingers: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2019-11-15 09:54:19 +00:00
"""Likes a given status post. This is only used by unit tests
2019-07-02 14:02:58 +00:00
"""
2022-01-02 21:45:26 +00:00
like_domain = get_full_domain(like_domain, like_port)
2020-04-01 10:44:33 +00:00
2022-01-02 21:45:26 +00:00
actor_liked = local_actor_url(http_prefix, like_nickname, like_domain)
object_url = actor_liked + '/statuses/' + str(like_status_number)
2020-04-01 10:44:33 +00:00
2021-12-29 21:55:09 +00:00
return _create_like(recent_posts_cache,
session, base_dir, federation_list,
nickname, domain, port,
2022-01-02 21:45:26 +00:00
cc_list, http_prefix, object_url, actor_liked,
2021-12-29 21:55:09 +00:00
client_to_server,
send_threads, postLog, person_cache, cached_webfingers,
debug, project_version, signing_priv_key_pem)
def send_like_via_server(base_dir: str, session,
2022-01-02 21:45:26 +00:00
from_nickname: str, password: str,
from_domain: str, from_port: int,
http_prefix: str, like_url: str,
2021-12-29 21:55:09 +00:00
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2019-07-17 18:33:41 +00:00
"""Creates a like via c2s
"""
if not session:
2021-12-29 21:55:09 +00:00
print('WARN: No session for send_like_via_server')
2019-07-17 18:33:41 +00:00
return 6
2022-01-02 21:45:26 +00:00
from_domain_full = get_full_domain(from_domain, from_port)
2020-04-03 16:36:21 +00:00
2022-01-02 21:45:26 +00:00
actor = local_actor_url(http_prefix, from_nickname, from_domain_full)
2019-07-17 18:33:41 +00:00
2022-01-02 21:45:26 +00:00
new_like_json = {
2019-08-18 11:07:06 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2019-07-17 18:33:41 +00:00
'type': 'Like',
2020-04-03 16:36:21 +00:00
'actor': actor,
2022-01-02 21:45:26 +00:00
'object': like_url
2019-07-17 18:33:41 +00:00
}
2022-01-02 21:45:26 +00:00
handle = http_prefix + '://' + from_domain_full + '/@' + from_nickname
2019-07-17 18:33:41 +00:00
# lookup the inbox for the To handle
2022-01-02 14:51:02 +00:00
wf_request = webfinger_handle(session, handle, http_prefix,
cached_webfingers,
2022-01-02 21:45:26 +00:00
from_domain, project_version, debug, False,
2022-01-02 14:51:02 +00:00
signing_priv_key_pem)
if not wf_request:
2019-07-17 18:33:41 +00:00
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: like webfinger failed for ' + handle)
2019-07-17 18:33:41 +00:00
return 1
2022-01-02 14:51:02 +00:00
if not isinstance(wf_request, dict):
2021-03-18 10:01:01 +00:00
print('WARN: like webfinger for ' + handle +
2022-01-02 14:51:02 +00:00
' did not return a dict. ' + str(wf_request))
2020-06-23 10:41:12 +00:00
return 1
2019-07-17 18:33:41 +00:00
2022-01-02 21:45:26 +00:00
post_to_box = 'outbox'
2019-07-17 18:33:41 +00:00
# get the actor inbox for the To handle
2022-01-02 21:45:26 +00:00
origin_domain = from_domain
(inbox_url, _, _, from_person_id, _, _,
2022-01-02 14:51:02 +00:00
_, _) = get_person_box(signing_priv_key_pem,
2022-01-02 21:45:26 +00:00
origin_domain,
2022-01-02 14:51:02 +00:00
base_dir, session, wf_request,
person_cache,
project_version, http_prefix,
2022-01-02 21:45:26 +00:00
from_nickname, from_domain,
post_to_box, 72873)
2020-03-22 21:16:02 +00:00
2022-01-02 21:45:26 +00:00
if not inbox_url:
2019-07-17 18:33:41 +00:00
if debug:
2022-01-02 21:45:26 +00:00
print('DEBUG: like no ' + post_to_box + ' was found for ' + handle)
2019-07-17 18:33:41 +00:00
return 3
2022-01-02 21:45:26 +00:00
if not from_person_id:
2019-07-17 18:33:41 +00:00
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: like no actor was found for ' + handle)
2019-07-17 18:33:41 +00:00
return 4
2020-03-22 21:16:02 +00:00
2022-01-02 21:45:26 +00:00
auth_header = create_basic_auth_header(from_nickname, password)
2020-03-22 21:16:02 +00:00
2020-04-01 10:44:33 +00:00
headers = {
2022-01-02 21:45:26 +00:00
'host': from_domain,
2020-04-01 10:44:33 +00:00
'Content-type': 'application/json',
2022-01-02 21:45:26 +00:00
'Authorization': auth_header
2020-03-22 20:36:19 +00:00
}
2022-01-02 21:45:26 +00:00
post_result = post_json(http_prefix, from_domain_full,
session, new_like_json, [], inbox_url,
headers, 3, True)
if not post_result:
2021-03-11 11:40:20 +00:00
if debug:
2022-01-02 21:45:26 +00:00
print('WARN: POST like failed for c2s to ' + inbox_url)
2020-04-01 10:44:33 +00:00
return 5
2019-07-17 18:33:41 +00:00
if debug:
print('DEBUG: c2s POST like success')
2022-01-02 21:45:26 +00:00
return new_like_json
2019-07-17 19:04:00 +00:00
2020-04-01 10:44:33 +00:00
2021-12-29 21:55:09 +00:00
def send_undo_like_via_server(base_dir: str, session,
2022-01-02 21:45:26 +00:00
from_nickname: str, password: str,
from_domain: str, from_port: int,
http_prefix: str, like_url: str,
2021-12-29 21:55:09 +00:00
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2019-07-17 19:04:00 +00:00
"""Undo a like via c2s
"""
if not session:
2021-12-29 21:55:09 +00:00
print('WARN: No session for send_undo_like_via_server')
2019-07-17 19:04:00 +00:00
return 6
2022-01-02 21:45:26 +00:00
from_domain_full = get_full_domain(from_domain, from_port)
2020-04-03 16:36:21 +00:00
2022-01-02 21:45:26 +00:00
actor = local_actor_url(http_prefix, from_nickname, from_domain_full)
2019-07-17 19:04:00 +00:00
2022-01-02 21:45:26 +00:00
new_undo_like_json = {
2019-08-18 11:07:06 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2019-07-17 19:04:00 +00:00
'type': 'Undo',
2020-04-03 16:36:21 +00:00
'actor': actor,
2019-07-17 19:04:00 +00:00
'object': {
'type': 'Like',
2020-04-03 16:36:21 +00:00
'actor': actor,
2022-01-02 21:45:26 +00:00
'object': like_url
2019-07-17 19:04:00 +00:00
}
}
2022-01-02 21:45:26 +00:00
handle = http_prefix + '://' + from_domain_full + '/@' + from_nickname
2019-07-17 19:04:00 +00:00
# lookup the inbox for the To handle
2022-01-02 14:51:02 +00:00
wf_request = webfinger_handle(session, handle, http_prefix,
cached_webfingers,
2022-01-02 21:45:26 +00:00
from_domain, project_version, debug, False,
2022-01-02 14:51:02 +00:00
signing_priv_key_pem)
if not wf_request:
2019-07-17 19:04:00 +00:00
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: unlike webfinger failed for ' + handle)
2019-07-17 19:04:00 +00:00
return 1
2022-01-02 14:51:02 +00:00
if not isinstance(wf_request, dict):
2021-03-11 11:41:14 +00:00
if debug:
2021-03-18 10:01:01 +00:00
print('WARN: unlike webfinger for ' + handle +
2022-01-02 14:51:02 +00:00
' did not return a dict. ' + str(wf_request))
2020-06-23 10:41:12 +00:00
return 1
2019-07-17 19:04:00 +00:00
2022-01-02 21:45:26 +00:00
post_to_box = 'outbox'
2019-07-17 19:04:00 +00:00
# get the actor inbox for the To handle
2022-01-02 21:45:26 +00:00
origin_domain = from_domain
(inbox_url, _, _, from_person_id, _, _,
_, _) = get_person_box(signing_priv_key_pem,
origin_domain,
base_dir, session, wf_request,
person_cache, project_version,
http_prefix, from_nickname,
from_domain, post_to_box,
72625)
if not inbox_url:
2019-07-17 19:04:00 +00:00
if debug:
2022-01-02 21:45:26 +00:00
print('DEBUG: unlike no ' + post_to_box + ' was found for ' +
handle)
2019-07-17 19:04:00 +00:00
return 3
2022-01-02 21:45:26 +00:00
if not from_person_id:
2019-07-17 19:04:00 +00:00
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: unlike no actor was found for ' + handle)
2019-07-17 19:04:00 +00:00
return 4
2020-03-22 21:16:02 +00:00
2022-01-02 21:45:26 +00:00
auth_header = create_basic_auth_header(from_nickname, password)
2020-03-22 21:16:02 +00:00
2020-04-01 10:44:33 +00:00
headers = {
2022-01-02 21:45:26 +00:00
'host': from_domain,
2020-04-01 10:44:33 +00:00
'Content-type': 'application/json',
2022-01-02 21:45:26 +00:00
'Authorization': auth_header
2020-03-22 20:36:19 +00:00
}
2022-01-02 21:45:26 +00:00
post_result = post_json(http_prefix, from_domain_full,
session, new_undo_like_json, [], inbox_url,
headers, 3, True)
if not post_result:
2021-03-11 11:41:14 +00:00
if debug:
2022-01-02 21:45:26 +00:00
print('WARN: POST unlike failed for c2s to ' + inbox_url)
2020-04-01 10:44:33 +00:00
return 5
2019-07-17 19:04:00 +00:00
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: c2s POST unlike success')
2019-07-17 19:04:00 +00:00
2022-01-02 21:45:26 +00:00
return new_undo_like_json
2019-07-17 19:31:52 +00:00
2020-04-01 10:44:33 +00:00
2021-12-29 21:55:09 +00:00
def outbox_like(recent_posts_cache: {},
base_dir: str, http_prefix: str,
nickname: str, domain: str, port: int,
message_json: {}, debug: bool) -> None:
2019-07-17 19:31:52 +00:00
""" When a like request is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2019-07-17 19:31:52 +00:00
if debug:
print('DEBUG: like - no type')
return
2021-12-25 23:51:19 +00:00
if not message_json['type'] == 'Like':
2019-07-17 19:31:52 +00:00
if debug:
print('DEBUG: not a like')
return
2021-12-26 17:12:07 +00:00
if not has_object_string(message_json, debug):
2019-07-17 19:31:52 +00:00
return
if debug:
print('DEBUG: c2s like request arrived in outbox')
2022-01-02 21:45:26 +00:00
message_id = remove_id_ending(message_json['object'])
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2022-01-02 21:45:26 +00:00
post_filename = locate_post(base_dir, nickname, domain, message_id)
2021-12-26 23:41:34 +00:00
if not post_filename:
2019-07-17 19:31:52 +00:00
if debug:
print('DEBUG: c2s like post not found in inbox or outbox')
2022-01-02 21:45:26 +00:00
print(message_id)
2019-07-17 19:31:52 +00:00
return True
2021-12-29 21:55:09 +00:00
update_likes_collection(recent_posts_cache,
2022-01-02 21:45:26 +00:00
base_dir, post_filename, message_id,
2021-12-29 21:55:09 +00:00
message_json['actor'],
nickname, domain, debug, None)
2019-07-17 19:31:52 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post liked via c2s - ' + post_filename)
2019-07-17 19:55:24 +00:00
2020-04-01 10:44:33 +00:00
2021-12-29 21:55:09 +00:00
def outbox_undo_like(recent_posts_cache: {},
base_dir: str, http_prefix: str,
nickname: str, domain: str, port: int,
message_json: {}, debug: bool) -> None:
2019-07-17 19:55:24 +00:00
""" When an undo like request is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2019-07-17 19:55:24 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json['type'] == 'Undo':
2019-07-17 19:55:24 +00:00
return
2021-12-26 17:12:07 +00:00
if not has_object_stringType(message_json, debug):
2019-07-17 19:55:24 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json['object']['type'] == 'Like':
2019-07-17 19:55:24 +00:00
if debug:
print('DEBUG: not a undo like')
return
2021-12-26 15:54:46 +00:00
if not has_object_string_object(message_json, debug):
2019-07-17 19:55:24 +00:00
return
if debug:
print('DEBUG: c2s undo like request arrived in outbox')
2022-01-02 21:45:26 +00:00
message_id = remove_id_ending(message_json['object']['object'])
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2022-01-02 21:45:26 +00:00
post_filename = locate_post(base_dir, nickname, domain, message_id)
2021-12-26 23:41:34 +00:00
if not post_filename:
2019-07-17 19:55:24 +00:00
if debug:
print('DEBUG: c2s undo like post not found in inbox or outbox')
2022-01-02 21:45:26 +00:00
print(message_id)
2019-07-17 19:55:24 +00:00
return True
2021-12-27 23:23:07 +00:00
undo_likes_collection_entry(recent_posts_cache, base_dir, post_filename,
2022-01-02 21:45:26 +00:00
message_id, message_json['actor'],
2021-12-27 23:23:07 +00:00
domain, debug, None)
2019-07-17 19:55:24 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post undo liked via c2s - ' + post_filename)
2021-09-03 12:00:23 +00:00
2021-12-29 21:55:09 +00:00
def update_likes_collection(recent_posts_cache: {},
base_dir: str, post_filename: str,
2022-01-02 21:45:26 +00:00
object_url: str, actor: str,
2021-12-29 21:55:09 +00:00
nickname: str, domain: str, debug: bool,
post_json_object: {}) -> None:
2021-09-03 12:00:23 +00:00
"""Updates the likes collection within a post
"""
2021-12-25 22:09:19 +00:00
if not post_json_object:
2021-12-26 23:41:34 +00:00
post_json_object = load_json(post_filename)
2021-12-25 22:09:19 +00:00
if not post_json_object:
2021-09-03 12:00:23 +00:00
return
2021-09-03 12:00:23 +00:00
# remove any cached version of this post so that the
# like icon is changed
2021-12-27 11:05:24 +00:00
remove_post_from_cache(post_json_object, recent_posts_cache)
2022-01-02 21:45:26 +00:00
cached_post_filename = \
2021-12-26 23:41:34 +00:00
get_cached_post_filename(base_dir, nickname,
domain, post_json_object)
2022-01-02 21:45:26 +00:00
if cached_post_filename:
if os.path.isfile(cached_post_filename):
try:
2022-01-02 21:45:26 +00:00
os.remove(cached_post_filename)
2021-11-25 18:42:38 +00:00
except OSError:
2021-12-29 21:55:09 +00:00
print('EX: update_likes_collection unable to delete ' +
2022-01-02 21:45:26 +00:00
cached_post_filename)
2021-09-03 12:00:23 +00:00
2021-12-25 22:09:19 +00:00
obj = post_json_object
2021-12-26 10:57:03 +00:00
if has_object_dict(post_json_object):
2021-12-25 22:09:19 +00:00
obj = post_json_object['object']
2021-10-15 09:13:04 +00:00
2022-01-02 21:45:26 +00:00
if not object_url.endswith('/likes'):
object_url = object_url + '/likes'
2021-10-14 22:43:42 +00:00
if not obj.get('likes'):
2021-09-03 12:00:23 +00:00
if debug:
2022-01-02 21:45:26 +00:00
print('DEBUG: Adding initial like to ' + object_url)
likes_json = {
2021-09-03 12:00:23 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2022-01-02 21:45:26 +00:00
'id': object_url,
2021-09-03 12:00:23 +00:00
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'Like',
'actor': actor
}]
}
2022-01-02 21:45:26 +00:00
obj['likes'] = likes_json
2021-09-03 12:00:23 +00:00
else:
2021-10-14 22:43:42 +00:00
if not obj['likes'].get('items'):
obj['likes']['items'] = []
2022-01-02 21:45:26 +00:00
for like_item in obj['likes']['items']:
if like_item.get('actor'):
if like_item['actor'] == actor:
2021-09-03 12:00:23 +00:00
# already liked
return
2022-01-02 21:45:26 +00:00
new_like = {
2021-09-03 12:00:23 +00:00
'type': 'Like',
'actor': actor
}
2022-01-02 21:45:26 +00:00
obj['likes']['items'].append(new_like)
it_len = len(obj['likes']['items'])
obj['likes']['totalItems'] = it_len
2021-09-03 12:00:23 +00:00
if debug:
print('DEBUG: saving post with likes added')
2021-12-25 22:09:19 +00:00
pprint(post_json_object)
2021-12-26 23:41:34 +00:00
save_json(post_json_object, post_filename)