epicyon/bookmarks.py

664 lines
24 KiB
Python
Raw Normal View History

2020-04-01 22:22:51 +00:00
__filename__ = "bookmarks.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2022-02-03 13:58:20 +00:00
__version__ = "1.3.0"
2020-04-01 22:22:51 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-01 22:22:51 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Timeline"
2019-11-17 14:01:49 +00:00
import os
from pprint import pprint
2021-12-29 21:55:09 +00:00
from webfinger import webfinger_handle
2021-12-28 21:36:27 +00:00
from auth import create_basic_auth_header
2021-12-26 18:17:37 +00:00
from utils import remove_domain_port
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 11:05:24 +00:00
from utils import remove_post_from_cache
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-26 23:41:34 +00:00
from utils import get_cached_post_filename
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-26 10:57:03 +00:00
from utils import has_object_dict
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2021-12-26 17:15:04 +00:00
from utils import has_actor
2021-12-26 17:12:07 +00:00
from utils import has_object_stringType
2021-12-29 21:55:09 +00:00
from posts import get_person_box
from session import post_json
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-29 21:55:09 +00:00
def undo_bookmarks_collection_entry(recent_posts_cache: {},
base_dir: str, post_filename: str,
2021-12-30 11:59:25 +00:00
object_url: str,
2021-12-29 21:55:09 +00:00
actor: str, domain: str,
debug: bool) -> None:
2019-11-17 14:01:49 +00:00
"""Undoes a bookmark for a particular actor
"""
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:
2019-11-18 15:21:35 +00:00
return
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
# remove any cached version of this post so that the
# bookmark icon is changed
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2021-12-30 11:59:25 +00:00
cached_post_filename = \
2021-12-26 23:41:34 +00:00
get_cached_post_filename(base_dir, nickname,
domain, post_json_object)
2021-12-30 11:59:25 +00:00
if cached_post_filename:
if os.path.isfile(cached_post_filename):
try:
2021-12-30 11:59:25 +00:00
os.remove(cached_post_filename)
2021-11-25 18:42:38 +00:00
except OSError:
2021-10-29 16:31:20 +00:00
if debug:
2021-12-29 21:55:09 +00:00
print('EX: undo_bookmarks_collection_entry ' +
2021-10-29 16:31:20 +00:00
'unable to delete cached post file ' +
2021-12-30 11:59:25 +00:00
str(cached_post_filename))
2021-12-27 11:05:24 +00:00
remove_post_from_cache(post_json_object, recent_posts_cache)
2019-11-18 15:21:35 +00:00
2020-05-21 22:12:31 +00:00
# remove from the index
2021-12-30 11:59:25 +00:00
bookmarks_index_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
2021-12-30 11:59:25 +00:00
if not os.path.isfile(bookmarks_index_filename):
2020-05-21 22:12:31 +00:00
return
2021-12-26 23:41:34 +00:00
if '/' in post_filename:
2021-12-30 11:59:25 +00:00
bookmark_index = post_filename.split('/')[-1].strip()
2020-05-21 22:12:31 +00:00
else:
2021-12-30 11:59:25 +00:00
bookmark_index = post_filename.strip()
bookmark_index = bookmark_index.replace('\n', '').replace('\r', '')
if bookmark_index not in open(bookmarks_index_filename).read():
2020-05-21 22:12:31 +00:00
return
2021-12-30 11:59:25 +00:00
index_str = ''
2021-11-26 12:28:20 +00:00
try:
2021-12-30 11:59:25 +00:00
with open(bookmarks_index_filename, 'r') as index_file:
index_str = index_file.read().replace(bookmark_index + '\n', '')
2021-11-26 12:28:20 +00:00
except OSError:
2021-12-30 11:59:25 +00:00
print('EX: unable to read ' + bookmarks_index_filename)
if index_str:
2021-11-25 18:42:38 +00:00
try:
2021-12-30 11:59:25 +00:00
with open(bookmarks_index_filename, 'w+') as bmi_file:
bmi_file.write(index_str)
2021-11-25 18:42:38 +00:00
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to write bookmarks index ' +
2021-12-30 11:59:25 +00:00
bookmarks_index_filename)
2021-12-25 22:09:19 +00:00
if not post_json_object.get('type'):
2019-11-18 15:21:35 +00:00
return
2021-12-25 22:09:19 +00:00
if post_json_object['type'] != 'Create':
2019-11-18 15:21:35 +00:00
return
2021-12-26 10:57:03 +00:00
if not has_object_dict(post_json_object):
2019-11-18 15:21:35 +00:00
if debug:
2021-03-20 10:13:59 +00:00
print('DEBUG: bookmarked post has no object ' +
2021-12-25 22:09:19 +00:00
str(post_json_object))
2019-11-18 15:21:35 +00:00
return
2021-12-25 22:09:19 +00:00
if not post_json_object['object'].get('bookmarks'):
2019-11-18 15:21:35 +00:00
return
2021-12-25 22:09:19 +00:00
if not isinstance(post_json_object['object']['bookmarks'], dict):
2019-11-18 15:21:35 +00:00
return
2021-12-25 22:09:19 +00:00
if not post_json_object['object']['bookmarks'].get('items'):
2019-11-18 15:21:35 +00:00
return
2021-12-30 11:59:25 +00:00
total_items = 0
2021-12-25 22:09:19 +00:00
if post_json_object['object']['bookmarks'].get('totalItems'):
2021-12-30 11:59:25 +00:00
total_items = post_json_object['object']['bookmarks']['totalItems']
item_found = False
for bookmark_item in post_json_object['object']['bookmarks']['items']:
if bookmark_item.get('actor'):
if bookmark_item['actor'] == actor:
2019-11-17 14:01:49 +00:00
if debug:
2020-04-01 22:22:51 +00:00
print('DEBUG: bookmark was removed for ' + actor)
2021-12-30 11:59:25 +00:00
bm_it = bookmark_item
post_json_object['object']['bookmarks']['items'].remove(bm_it)
item_found = True
2019-11-18 15:21:35 +00:00
break
2021-12-30 11:59:25 +00:00
if not item_found:
2019-11-18 15:21:35 +00:00
return
2021-12-30 11:59:25 +00:00
if total_items == 1:
2019-11-18 15:21:35 +00:00
if debug:
print('DEBUG: bookmarks was removed from post')
2021-12-25 22:09:19 +00:00
del post_json_object['object']['bookmarks']
2019-11-18 15:21:35 +00:00
else:
2021-12-30 11:59:25 +00:00
bm_it_len = len(post_json_object['object']['bookmarks']['items'])
post_json_object['object']['bookmarks']['totalItems'] = bm_it_len
2021-12-26 23:41:34 +00:00
save_json(post_json_object, post_filename)
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-29 21:55:09 +00:00
def bookmarked_by_person(post_json_object: {},
nickname: str, domain: str) -> bool:
2019-11-17 14:01:49 +00:00
"""Returns True if the given post is bookmarked by the given person
"""
2021-12-29 21:55:09 +00:00
if _no_of_bookmarks(post_json_object) == 0:
2019-11-17 14:01:49 +00:00
return False
2021-12-30 11:59:25 +00:00
actor_match = domain + '/users/' + nickname
2021-12-25 22:09:19 +00:00
for item in post_json_object['object']['bookmarks']['items']:
2021-12-30 11:59:25 +00:00
if item['actor'].endswith(actor_match):
2019-11-17 14:01:49 +00:00
return True
return False
2020-04-01 22:22:51 +00:00
2021-12-29 21:55:09 +00:00
def _no_of_bookmarks(post_json_object: {}) -> int:
2019-11-17 14:01:49 +00:00
"""Returns the number of bookmarks ona given post
"""
2021-12-26 10:57:03 +00:00
if not has_object_dict(post_json_object):
2019-11-17 14:01:49 +00:00
return 0
2021-12-25 22:09:19 +00:00
if not post_json_object['object'].get('bookmarks'):
2019-11-17 14:01:49 +00:00
return 0
2021-12-25 22:09:19 +00:00
if not isinstance(post_json_object['object']['bookmarks'], dict):
2019-11-17 14:01:49 +00:00
return 0
2021-12-25 22:09:19 +00:00
if not post_json_object['object']['bookmarks'].get('items'):
post_json_object['object']['bookmarks']['items'] = []
post_json_object['object']['bookmarks']['totalItems'] = 0
return len(post_json_object['object']['bookmarks']['items'])
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-29 21:55:09 +00:00
def update_bookmarks_collection(recent_posts_cache: {},
base_dir: str, post_filename: str,
2021-12-30 11:59:25 +00:00
object_url: str,
2021-12-29 21:55:09 +00:00
actor: str, domain: str, debug: bool) -> None:
2019-11-17 14:01:49 +00:00
"""Updates the bookmarks collection within a post
"""
2021-12-26 23:41:34 +00:00
post_json_object = load_json(post_filename)
2021-12-25 22:09:19 +00:00
if post_json_object:
2020-04-01 22:22:51 +00:00
# remove any cached version of this post so that the
# bookmark icon is changed
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2021-12-30 11:59:25 +00:00
cached_post_filename = \
2021-12-26 23:41:34 +00:00
get_cached_post_filename(base_dir, nickname,
domain, post_json_object)
2021-12-30 11:59:25 +00:00
if cached_post_filename:
if os.path.isfile(cached_post_filename):
try:
2021-12-30 11:59:25 +00:00
os.remove(cached_post_filename)
2021-11-25 18:42:38 +00:00
except OSError:
2021-10-29 16:31:20 +00:00
if debug:
2021-12-29 21:55:09 +00:00
print('EX: update_bookmarks_collection ' +
2021-10-29 16:31:20 +00:00
'unable to delete cached post ' +
2021-12-30 11:59:25 +00:00
str(cached_post_filename))
2021-12-27 11:05:24 +00:00
remove_post_from_cache(post_json_object, recent_posts_cache)
2019-11-17 14:01:49 +00:00
2021-12-25 22:09:19 +00:00
if not post_json_object.get('object'):
2019-11-17 14:01:49 +00:00
if debug:
2021-03-20 10:13:59 +00:00
print('DEBUG: no object in bookmarked post ' +
2021-12-25 22:09:19 +00:00
str(post_json_object))
2019-11-17 14:01:49 +00:00
return
2021-12-30 11:59:25 +00:00
if not object_url.endswith('/bookmarks'):
object_url = object_url + '/bookmarks'
2021-03-20 10:13:59 +00:00
# does this post have bookmarks on it from differenent actors?
2021-12-25 22:09:19 +00:00
if not post_json_object['object'].get('bookmarks'):
2019-11-17 14:01:49 +00:00
if debug:
2021-12-30 11:59:25 +00:00
print('DEBUG: Adding initial bookmarks to ' + object_url)
bookmarks_json = {
2019-11-18 16:03:54 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2021-12-30 11:59:25 +00:00
'id': object_url,
2019-11-18 16:03:54 +00:00
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'Bookmark',
'actor': actor
2020-03-22 21:16:02 +00:00
}]
2019-11-18 16:03:54 +00:00
}
2021-12-30 11:59:25 +00:00
post_json_object['object']['bookmarks'] = bookmarks_json
2019-11-17 14:01:49 +00:00
else:
2021-12-25 22:09:19 +00:00
if not post_json_object['object']['bookmarks'].get('items'):
post_json_object['object']['bookmarks']['items'] = []
bm_items = post_json_object['object']['bookmarks']['items']
2021-12-30 11:59:25 +00:00
for bookmark_item in bm_items:
if bookmark_item.get('actor'):
if bookmark_item['actor'] == actor:
2019-11-17 14:01:49 +00:00
return
2021-12-30 11:59:25 +00:00
new_bookmark = {
'type': 'Bookmark',
'actor': actor
}
2021-12-30 11:59:25 +00:00
nbook = new_bookmark
bm_it = len(post_json_object['object']['bookmarks']['items'])
post_json_object['object']['bookmarks']['items'].append(nbook)
post_json_object['object']['bookmarks']['totalItems'] = bm_it
2019-11-17 14:01:49 +00:00
if debug:
print('DEBUG: saving post with bookmarks added')
2021-12-25 22:09:19 +00:00
pprint(post_json_object)
2019-11-17 14:01:49 +00:00
2021-12-26 23:41:34 +00:00
save_json(post_json_object, post_filename)
2019-11-17 14:01:49 +00:00
# prepend to the index
2021-12-30 11:59:25 +00:00
bookmarks_index_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
2021-12-30 11:59:25 +00:00
bookmark_index = post_filename.split('/')[-1]
if os.path.isfile(bookmarks_index_filename):
if bookmark_index not in open(bookmarks_index_filename).read():
2019-11-17 14:01:49 +00:00
try:
2021-12-30 11:59:25 +00:00
with open(bookmarks_index_filename, 'r+') as bmi_file:
content = bmi_file.read()
if bookmark_index + '\n' not in content:
bmi_file.seek(0, 0)
bmi_file.write(bookmark_index + '\n' + content)
if debug:
print('DEBUG: bookmark added to index')
2021-12-30 11:59:25 +00:00
except OSError as ex:
2020-04-01 22:22:51 +00:00
print('WARN: Failed to write entry to bookmarks index ' +
2021-12-30 11:59:25 +00:00
bookmarks_index_filename + ' ' + str(ex))
2019-11-17 14:01:49 +00:00
else:
2021-11-25 18:42:38 +00:00
try:
2021-12-30 11:59:25 +00:00
with open(bookmarks_index_filename, 'w+') as bm_file:
bm_file.write(bookmark_index + '\n')
2021-11-25 18:42:38 +00:00
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to write bookmarks index ' +
2021-12-30 11:59:25 +00:00
bookmarks_index_filename)
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-29 21:55:09 +00:00
def bookmark_post(recent_posts_cache: {},
session, base_dir: str, federation_list: [],
nickname: str, domain: str, port: int,
ccList: [], http_prefix: str,
2021-12-30 11:59:25 +00:00
object_url: str, actorBookmarked: str,
2021-12-29 21:55:09 +00:00
client_to_server: bool,
2021-12-30 11:59:25 +00:00
send_threads: [], post_log: [],
2021-12-29 21:55:09 +00:00
person_cache: {}, cached_webfingers: {},
debug: bool, project_version: str) -> {}:
2019-11-17 14:01:49 +00:00
"""Creates a bookmark
actor is the person doing the bookmarking
'to' might be a specific person (actor) whose post was bookmarked
object is typically the url of the message which was bookmarked
"""
2021-12-30 11:59:25 +00:00
if not url_permitted(object_url, federation_list):
2019-11-17 14:01:49 +00:00
return None
2021-12-30 11:59:25 +00:00
full_domain = get_full_domain(domain, port)
2019-11-17 14:01:49 +00:00
2021-12-30 11:59:25 +00:00
new_bookmark_json = {
2019-11-17 14:01:49 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'Bookmark',
2021-12-30 11:59:25 +00:00
'actor': local_actor_url(http_prefix, nickname, full_domain),
'object': object_url
2019-11-17 14:01:49 +00:00
}
if ccList:
2020-04-01 22:22:51 +00:00
if len(ccList) > 0:
2021-12-30 11:59:25 +00:00
new_bookmark_json['cc'] = ccList
2019-11-17 14:01:49 +00:00
# Extract the domain and nickname from a statuses link
2021-12-30 11:59:25 +00:00
bookmarked_post_nickname = None
2019-11-17 14:01:49 +00:00
if actorBookmarked:
2021-12-30 11:59:25 +00:00
ac_bm = actorBookmarked
bookmarked_post_nickname = get_nickname_from_actor(ac_bm)
_, _ = get_domain_from_actor(ac_bm)
2019-11-17 14:01:49 +00:00
else:
2021-12-30 11:59:25 +00:00
if has_users_path(object_url):
ourl = object_url
bookmarked_post_nickname = get_nickname_from_actor(ourl)
_, _ = get_domain_from_actor(ourl)
if bookmarked_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: bookmark base_dir: ' + base_dir)
2020-04-01 22:22:51 +00:00
print('DEBUG: bookmark nickname: ' + nickname)
print('DEBUG: bookmark domain: ' + domain)
2021-12-30 11:59:25 +00:00
print('DEBUG: bookmark object_url: ' + object_url)
2019-11-17 14:01:49 +00:00
return None
2020-03-22 21:16:02 +00:00
2021-12-29 21:55:09 +00:00
update_bookmarks_collection(recent_posts_cache,
2021-12-30 11:59:25 +00:00
base_dir, post_filename, object_url,
new_bookmark_json['actor'], domain, debug)
2019-11-17 14:01:49 +00:00
2021-12-30 11:59:25 +00:00
return new_bookmark_json
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-29 21:55:09 +00:00
def undo_bookmark_post(recent_posts_cache: {},
session, base_dir: str, federation_list: [],
nickname: str, domain: str, port: int,
ccList: [], http_prefix: str,
2021-12-30 11:59:25 +00:00
object_url: str, actorBookmarked: str,
2021-12-29 21:55:09 +00:00
client_to_server: bool,
2021-12-30 11:59:25 +00:00
send_threads: [], post_log: [],
2021-12-29 21:55:09 +00:00
person_cache: {}, cached_webfingers: {},
debug: bool, project_version: str) -> {}:
2019-11-17 14:01:49 +00:00
"""Removes a bookmark
actor is the person doing the bookmarking
'to' might be a specific person (actor) whose post was bookmarked
object is typically the url of the message which was bookmarked
"""
2021-12-30 11:59:25 +00:00
if not url_permitted(object_url, federation_list):
2019-11-17 14:01:49 +00:00
return None
2021-12-30 11:59:25 +00:00
full_domain = get_full_domain(domain, port)
2019-11-17 14:01:49 +00:00
2021-12-30 11:59:25 +00:00
new_undo_bookmark_json = {
2019-11-17 14:01:49 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'Undo',
2021-12-30 11:59:25 +00:00
'actor': local_actor_url(http_prefix, nickname, full_domain),
2019-11-17 14:01:49 +00:00
'object': {
'type': 'Bookmark',
2021-12-30 11:59:25 +00:00
'actor': local_actor_url(http_prefix, nickname, full_domain),
'object': object_url
2019-11-17 14:01:49 +00:00
}
}
if ccList:
2020-04-01 22:22:51 +00:00
if len(ccList) > 0:
2021-12-30 11:59:25 +00:00
new_undo_bookmark_json['cc'] = ccList
new_undo_bookmark_json['object']['cc'] = ccList
2019-11-17 14:01:49 +00:00
# Extract the domain and nickname from a statuses link
2021-12-30 11:59:25 +00:00
bookmarked_post_nickname = None
2019-11-17 14:01:49 +00:00
if actorBookmarked:
2021-12-30 11:59:25 +00:00
ac_bm = actorBookmarked
bookmarked_post_nickname = get_nickname_from_actor(ac_bm)
_, _ = get_domain_from_actor(ac_bm)
2019-11-17 14:01:49 +00:00
else:
2021-12-30 11:59:25 +00:00
if has_users_path(object_url):
ourl = object_url
bookmarked_post_nickname = get_nickname_from_actor(ourl)
_, _ = get_domain_from_actor(ourl)
if bookmarked_post_nickname:
post_filename = locate_post(base_dir, nickname, domain, object_url)
2021-12-26 23:41:34 +00:00
if not post_filename:
2019-11-17 14:01:49 +00:00
return None
2021-12-29 21:55:09 +00:00
undo_bookmarks_collection_entry(recent_posts_cache,
2021-12-30 11:59:25 +00:00
base_dir, post_filename, object_url,
new_undo_bookmark_json['actor'],
2021-12-29 21:55:09 +00:00
domain, debug)
2019-11-17 14:01:49 +00:00
else:
return None
2021-12-30 11:59:25 +00:00
return new_undo_bookmark_json
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-29 21:55:09 +00:00
def send_bookmark_via_server(base_dir: str, session,
nickname: str, password: str,
2021-12-30 12:00:56 +00:00
domain: str, from_port: int,
http_prefix: str, bookmark_url: str,
2021-12-29 21:55:09 +00:00
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2021-03-19 22:04:57 +00:00
"""Creates a bookmark via c2s
"""
if not session:
2021-12-29 21:55:09 +00:00
print('WARN: No session for send_bookmark_via_server')
2021-03-19 22:04:57 +00:00
return 6
2021-12-30 12:00:56 +00:00
domain_full = get_full_domain(domain, from_port)
2021-03-19 22:04:57 +00:00
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, nickname, domain_full)
2021-03-19 22:04:57 +00:00
2021-12-30 11:59:25 +00:00
new_bookmark_json = {
2021-03-19 22:04:57 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Add",
"actor": actor,
2021-03-20 12:20:17 +00:00
"to": [actor],
2021-03-19 22:04:57 +00:00
"object": {
"type": "Document",
2021-12-30 12:00:56 +00:00
"url": bookmark_url,
2021-03-20 12:20:17 +00:00
"to": [actor]
2021-03-19 22:04:57 +00:00
},
"target": actor + "/tlbookmarks"
}
2021-12-26 10:00:46 +00:00
handle = http_prefix + '://' + domain_full + '/@' + nickname
2021-03-19 22:04:57 +00:00
# lookup the inbox for the To handle
2021-12-30 11:59:25 +00:00
wf_request = \
webfinger_handle(session, handle, http_prefix,
cached_webfingers,
domain, project_version, debug, False,
signing_priv_key_pem)
if not wf_request:
2021-03-19 22:04:57 +00:00
if debug:
print('DEBUG: bookmark webfinger failed for ' + handle)
return 1
2021-12-30 11:59:25 +00:00
if not isinstance(wf_request, dict):
2021-03-19 22:04:57 +00:00
print('WARN: bookmark webfinger for ' + handle +
2021-12-30 11:59:25 +00:00
' did not return a dict. ' + str(wf_request))
2021-03-19 22:04:57 +00:00
return 1
2021-12-30 11:59:25 +00:00
post_to_box = 'outbox'
2021-03-19 22:04:57 +00:00
# get the actor inbox for the To handle
2021-12-30 11:59:25 +00:00
origin_domain = 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,
nickname, domain,
post_to_box, 58391)
if not inbox_url:
2021-03-19 22:04:57 +00:00
if debug:
2021-12-30 11:59:25 +00:00
print('DEBUG: bookmark no ' + post_to_box +
2021-03-19 22:04:57 +00:00
' was found for ' + handle)
return 3
2021-12-30 11:59:25 +00:00
if not from_person_id:
2021-03-19 22:04:57 +00:00
if debug:
print('DEBUG: bookmark no actor was found for ' + handle)
return 4
2021-12-30 11:59:25 +00:00
auth_header = create_basic_auth_header(nickname, password)
2021-03-19 22:04:57 +00:00
headers = {
'host': domain,
'Content-type': 'application/json',
2021-12-30 11:59:25 +00:00
'Authorization': auth_header
2021-03-19 22:04:57 +00:00
}
2021-12-30 11:59:25 +00:00
post_result = post_json(http_prefix, domain_full,
session, new_bookmark_json, [], inbox_url,
headers, 3, True)
if not post_result:
2021-03-19 22:04:57 +00:00
if debug:
2021-12-30 11:59:25 +00:00
print('WARN: POST bookmark failed for c2s to ' + inbox_url)
2021-03-19 22:04:57 +00:00
return 5
if debug:
print('DEBUG: c2s POST bookmark success')
2021-12-30 11:59:25 +00:00
return new_bookmark_json
2021-03-19 22:11:45 +00:00
2021-12-29 21:55:09 +00:00
def send_undo_bookmark_via_server(base_dir: str, session,
nickname: str, password: str,
2021-12-30 12:00:56 +00:00
domain: str, from_port: int,
http_prefix: str, bookmark_url: str,
2021-12-29 21:55:09 +00:00
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2021-03-19 22:11:45 +00:00
"""Removes a bookmark via c2s
"""
if not session:
2021-12-29 21:55:09 +00:00
print('WARN: No session for send_undo_bookmark_via_server')
2021-03-19 22:11:45 +00:00
return 6
2021-12-30 12:00:56 +00:00
domain_full = get_full_domain(domain, from_port)
2021-03-19 22:11:45 +00:00
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, nickname, domain_full)
2021-03-19 22:11:45 +00:00
2021-12-30 11:59:25 +00:00
new_bookmark_json = {
2021-03-19 22:11:45 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Remove",
"actor": actor,
2021-03-20 14:32:25 +00:00
"to": [actor],
2021-03-19 22:11:45 +00:00
"object": {
"type": "Document",
2021-12-30 12:00:56 +00:00
"url": bookmark_url,
2021-03-20 14:32:25 +00:00
"to": [actor]
2021-03-19 22:11:45 +00:00
},
"target": actor + "/tlbookmarks"
}
2021-12-26 10:00:46 +00:00
handle = http_prefix + '://' + domain_full + '/@' + nickname
2021-03-19 22:11:45 +00:00
# lookup the inbox for the To handle
2021-12-30 11:59:25 +00:00
wf_request = \
webfinger_handle(session, handle, http_prefix,
cached_webfingers,
domain, project_version, debug, False,
signing_priv_key_pem)
if not wf_request:
2021-03-19 22:11:45 +00:00
if debug:
print('DEBUG: unbookmark webfinger failed for ' + handle)
return 1
2021-12-30 11:59:25 +00:00
if not isinstance(wf_request, dict):
2021-03-19 22:11:45 +00:00
print('WARN: unbookmark webfinger for ' + handle +
2021-12-30 11:59:25 +00:00
' did not return a dict. ' + str(wf_request))
2021-03-19 22:11:45 +00:00
return 1
2021-12-30 11:59:25 +00:00
post_to_box = 'outbox'
2021-03-19 22:11:45 +00:00
# get the actor inbox for the To handle
2021-12-30 11:59:25 +00:00
origin_domain = 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,
nickname, domain,
post_to_box, 52594)
if not inbox_url:
2021-03-19 22:11:45 +00:00
if debug:
2021-12-30 11:59:25 +00:00
print('DEBUG: unbookmark no ' + post_to_box +
2021-03-19 22:11:45 +00:00
' was found for ' + handle)
return 3
2021-12-30 11:59:25 +00:00
if not from_person_id:
2021-03-19 22:11:45 +00:00
if debug:
print('DEBUG: unbookmark no actor was found for ' + handle)
return 4
2021-12-30 11:59:25 +00:00
auth_header = create_basic_auth_header(nickname, password)
2021-03-19 22:11:45 +00:00
headers = {
'host': domain,
'Content-type': 'application/json',
2021-12-30 11:59:25 +00:00
'Authorization': auth_header
2021-03-19 22:11:45 +00:00
}
2021-12-30 11:59:25 +00:00
post_result = post_json(http_prefix, domain_full,
session, new_bookmark_json, [], inbox_url,
headers, 3, True)
if not post_result:
2021-03-19 22:11:45 +00:00
if debug:
2021-12-30 11:59:25 +00:00
print('WARN: POST unbookmark failed for c2s to ' + inbox_url)
2021-03-19 22:11:45 +00:00
return 5
if debug:
print('DEBUG: c2s POST unbookmark success')
2021-12-30 11:59:25 +00:00
return new_bookmark_json
2021-03-20 09:49:43 +00:00
2021-12-29 21:55:09 +00:00
def outbox_bookmark(recent_posts_cache: {},
base_dir: str, http_prefix: str,
nickname: str, domain: str, port: int,
message_json: {}, debug: bool) -> None:
2021-03-20 09:49:43 +00:00
""" When a bookmark request is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if message_json['type'] != 'Add':
2021-03-20 09:49:43 +00:00
return
2021-12-26 17:15:04 +00:00
if not has_actor(message_json, debug):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json.get('target'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: no target in bookmark Add')
return
2021-12-26 17:12:07 +00:00
if not has_object_stringType(message_json, debug):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if not isinstance(message_json['target'], str):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: bookmark Add target is not string')
return
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2021-12-26 10:00:46 +00:00
if not message_json['target'].endswith('://' + domain_full +
2021-12-25 23:51:19 +00:00
'/users/' + nickname +
'/tlbookmarks'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: bookmark Add target invalid ' +
2021-12-25 23:51:19 +00:00
message_json['target'])
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if message_json['object']['type'] != 'Document':
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: bookmark Add type is not Document')
return
2021-12-25 23:51:19 +00:00
if not message_json['object'].get('url'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: bookmark Add missing url')
return
if debug:
print('DEBUG: c2s bookmark Add request arrived in outbox')
2021-12-30 11:59:25 +00:00
message_url = remove_id_ending(message_json['object']['url'])
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2021-12-30 11:59:25 +00:00
post_filename = locate_post(base_dir, nickname, domain, message_url)
2021-12-26 23:41:34 +00:00
if not post_filename:
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: c2s like post not found in inbox or outbox')
2021-12-30 11:59:25 +00:00
print(message_url)
2021-03-20 09:49:43 +00:00
return True
2021-12-29 21:55:09 +00:00
update_bookmarks_collection(recent_posts_cache,
2021-12-30 11:59:25 +00:00
base_dir, post_filename, message_url,
2021-12-29 21:55:09 +00:00
message_json['actor'], domain, debug)
2021-03-20 09:49:43 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post bookmarked via c2s - ' + post_filename)
2021-03-20 09:49:43 +00:00
2021-12-29 21:55:09 +00:00
def outbox_undo_bookmark(recent_posts_cache: {},
base_dir: str, http_prefix: str,
nickname: str, domain: str, port: int,
message_json: {}, debug: bool) -> None:
2021-03-20 09:49:43 +00:00
""" When an undo bookmark request is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if message_json['type'] != 'Remove':
2021-03-20 09:49:43 +00:00
return
2021-12-26 17:15:04 +00:00
if not has_actor(message_json, debug):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json.get('target'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: no target in unbookmark Remove')
return
2021-12-26 17:12:07 +00:00
if not has_object_stringType(message_json, debug):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if not isinstance(message_json['target'], str):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: unbookmark Remove target is not string')
return
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2021-12-26 10:00:46 +00:00
if not message_json['target'].endswith('://' + domain_full +
2021-12-25 23:51:19 +00:00
'/users/' + nickname +
'/tlbookmarks'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: unbookmark Remove target invalid ' +
2021-12-25 23:51:19 +00:00
message_json['target'])
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if message_json['object']['type'] != 'Document':
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: unbookmark Remove type is not Document')
return
2021-12-25 23:51:19 +00:00
if not message_json['object'].get('url'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: unbookmark Remove missing url')
return
if debug:
print('DEBUG: c2s unbookmark Remove request arrived in outbox')
2021-12-30 11:59:25 +00:00
message_url = remove_id_ending(message_json['object']['url'])
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2021-12-30 11:59:25 +00:00
post_filename = locate_post(base_dir, nickname, domain, message_url)
2021-12-26 23:41:34 +00:00
if not post_filename:
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: c2s unbookmark post not found in inbox or outbox')
2021-12-30 11:59:25 +00:00
print(message_url)
2021-03-20 09:49:43 +00:00
return True
2021-12-29 21:55:09 +00:00
update_bookmarks_collection(recent_posts_cache,
2021-12-30 11:59:25 +00:00
base_dir, post_filename, message_url,
2021-12-29 21:55:09 +00:00
message_json['actor'], domain, debug)
2021-03-20 09:49:43 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post unbookmarked via c2s - ' + post_filename)