| 
									
										
										
										
											2020-04-01 22:22:51 +00:00
										 |  |  | __filename__ = "bookmarks.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2023-01-21 23:03:30 +00:00
										 |  |  | __version__ = "1.4.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 | 
					
						
							| 
									
										
										
										
											2022-04-09 15:11:22 +00:00
										 |  |  | from utils import has_object_string_type | 
					
						
							| 
									
										
										
										
											2022-06-10 11:43:33 +00:00
										 |  |  | from utils import text_in_file | 
					
						
							| 
									
										
										
										
											2022-06-21 11:58:50 +00:00
										 |  |  | from utils import remove_eol | 
					
						
							| 
									
										
										
										
											2023-07-12 11:08:02 +00:00
										 |  |  | from utils import remove_html | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  |                                     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) | 
					
						
							| 
									
										
										
										
											2022-03-23 23:59:29 +00:00
										 |  |  |     if not nickname: | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											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): | 
					
						
							| 
									
										
										
										
											2021-09-05 10:17:43 +00:00
										 |  |  |             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() | 
					
						
							| 
									
										
										
										
											2022-06-21 11:58:50 +00:00
										 |  |  |     bookmark_index = remove_eol(bookmark_index) | 
					
						
							| 
									
										
										
										
											2022-06-10 11:43:33 +00:00
										 |  |  |     if not text_in_file(bookmark_index, bookmarks_index_filename): | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2022-06-09 14:46:30 +00:00
										 |  |  |         with open(bookmarks_index_filename, 'r', | 
					
						
							|  |  |  |                   encoding='utf-8') as index_file: | 
					
						
							| 
									
										
										
										
											2021-12-30 11:59:25 +00:00
										 |  |  |             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: | 
					
						
							| 
									
										
										
										
											2022-06-09 14:46:30 +00:00
										 |  |  |             with open(bookmarks_index_filename, 'w+', | 
					
						
							|  |  |  |                       encoding='utf-8') as bmi_file: | 
					
						
							| 
									
										
										
										
											2021-12-30 11:59:25 +00:00
										 |  |  |                 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) | 
					
						
							| 
									
										
										
										
											2022-06-09 09:44:21 +00:00
										 |  |  |     if not post_json_object: | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2019-11-17 14:01:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 09:44:21 +00:00
										 |  |  |     # remove any cached version of this post so that the | 
					
						
							|  |  |  |     # bookmark 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: | 
					
						
							|  |  |  |                 if debug: | 
					
						
							|  |  |  |                     print('EX: update_bookmarks_collection ' + | 
					
						
							|  |  |  |                           '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('object'): | 
					
						
							|  |  |  |         if debug: | 
					
						
							|  |  |  |             print('DEBUG: no object in bookmarked post ' + | 
					
						
							|  |  |  |                   str(post_json_object)) | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     if not object_url.endswith('/bookmarks'): | 
					
						
							|  |  |  |         object_url = object_url + '/bookmarks' | 
					
						
							|  |  |  |     # does this post have bookmarks on it from differenent actors? | 
					
						
							|  |  |  |     if not post_json_object['object'].get('bookmarks'): | 
					
						
							|  |  |  |         if debug: | 
					
						
							|  |  |  |             print('DEBUG: Adding initial bookmarks to ' + object_url) | 
					
						
							|  |  |  |         bookmarks_json = { | 
					
						
							|  |  |  |             "@context": "https://www.w3.org/ns/activitystreams", | 
					
						
							|  |  |  |             'id': object_url, | 
					
						
							|  |  |  |             'type': 'Collection', | 
					
						
							|  |  |  |             "totalItems": 1, | 
					
						
							|  |  |  |             'items': [{ | 
					
						
							| 
									
										
										
										
											2021-03-21 10:45:24 +00:00
										 |  |  |                 'type': 'Bookmark', | 
					
						
							|  |  |  |                 'actor': actor | 
					
						
							| 
									
										
										
										
											2022-06-09 09:44:21 +00:00
										 |  |  |             }] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         post_json_object['object']['bookmarks'] = bookmarks_json | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if not post_json_object['object']['bookmarks'].get('items'): | 
					
						
							|  |  |  |             post_json_object['object']['bookmarks']['items'] = [] | 
					
						
							|  |  |  |         bm_items = post_json_object['object']['bookmarks']['items'] | 
					
						
							|  |  |  |         for bookmark_item in bm_items: | 
					
						
							|  |  |  |             if bookmark_item.get('actor'): | 
					
						
							|  |  |  |                 if bookmark_item['actor'] == actor: | 
					
						
							|  |  |  |                     return | 
					
						
							|  |  |  |         new_bookmark = { | 
					
						
							|  |  |  |             'type': 'Bookmark', | 
					
						
							|  |  |  |             'actor': actor | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 09:44:21 +00:00
										 |  |  |     if debug: | 
					
						
							|  |  |  |         print('DEBUG: saving post with bookmarks added') | 
					
						
							|  |  |  |         pprint(post_json_object) | 
					
						
							| 
									
										
										
										
											2019-11-17 14:01:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 09:44:21 +00:00
										 |  |  |     save_json(post_json_object, post_filename) | 
					
						
							| 
									
										
										
										
											2019-11-17 14:01:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 09:44:21 +00:00
										 |  |  |     # prepend to the index | 
					
						
							|  |  |  |     bookmarks_index_filename = \ | 
					
						
							|  |  |  |         acct_dir(base_dir, nickname, domain) + '/bookmarks.index' | 
					
						
							|  |  |  |     bookmark_index = post_filename.split('/')[-1] | 
					
						
							|  |  |  |     if os.path.isfile(bookmarks_index_filename): | 
					
						
							| 
									
										
										
										
											2022-06-10 11:43:33 +00:00
										 |  |  |         if not text_in_file(bookmark_index, bookmarks_index_filename): | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2022-06-09 14:46:30 +00:00
										 |  |  |                 with open(bookmarks_index_filename, 'r+', | 
					
						
							|  |  |  |                           encoding='utf-8') as bmi_file: | 
					
						
							| 
									
										
										
										
											2022-06-09 09:44:21 +00:00
										 |  |  |                     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') | 
					
						
							|  |  |  |             except OSError as ex: | 
					
						
							|  |  |  |                 print('WARN: Failed to write entry to bookmarks index ' + | 
					
						
							|  |  |  |                       bookmarks_index_filename + ' ' + str(ex)) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2022-06-09 14:46:30 +00:00
										 |  |  |             with open(bookmarks_index_filename, 'w+', | 
					
						
							|  |  |  |                       encoding='utf-8') as bm_file: | 
					
						
							| 
									
										
										
										
											2022-06-09 09:44:21 +00:00
										 |  |  |                 bm_file.write(bookmark_index + '\n') | 
					
						
							|  |  |  |         except OSError: | 
					
						
							|  |  |  |             print('EX: unable to write bookmarks index ' + | 
					
						
							|  |  |  |                   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: {}, | 
					
						
							| 
									
										
										
										
											2022-06-09 10:03:05 +00:00
										 |  |  |                   base_dir: str, federation_list: [], | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |                   nickname: str, domain: str, port: int, | 
					
						
							| 
									
										
										
										
											2022-06-09 10:03:05 +00:00
										 |  |  |                   cc_list: [], http_prefix: str, | 
					
						
							|  |  |  |                   object_url: str, actor_bookmarked: str, | 
					
						
							|  |  |  |                   debug: bool) -> {}: | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-09 10:03:05 +00:00
										 |  |  |     if cc_list: | 
					
						
							|  |  |  |         if len(cc_list) > 0: | 
					
						
							|  |  |  |             new_bookmark_json['cc'] = cc_list | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2022-06-09 10:03:05 +00:00
										 |  |  |     if actor_bookmarked: | 
					
						
							|  |  |  |         ac_bm = actor_bookmarked | 
					
						
							| 
									
										
										
										
											2021-12-30 11:59:25 +00:00
										 |  |  |         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: {}, | 
					
						
							| 
									
										
										
										
											2022-06-09 10:03:05 +00:00
										 |  |  |                        base_dir: str, federation_list: [], | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |                        nickname: str, domain: str, port: int, | 
					
						
							| 
									
										
										
										
											2022-06-09 10:03:05 +00:00
										 |  |  |                        cc_list: [], http_prefix: str, | 
					
						
							|  |  |  |                        object_url: str, actor_bookmarked: str, | 
					
						
							|  |  |  |                        debug: bool) -> {}: | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-09 10:03:05 +00:00
										 |  |  |     if cc_list: | 
					
						
							|  |  |  |         if len(cc_list) > 0: | 
					
						
							|  |  |  |             new_undo_bookmark_json['cc'] = cc_list | 
					
						
							|  |  |  |             new_undo_bookmark_json['object']['cc'] = cc_list | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2022-06-09 10:03:05 +00:00
										 |  |  |     if actor_bookmarked: | 
					
						
							|  |  |  |         ac_bm = actor_bookmarked | 
					
						
							| 
									
										
										
										
											2021-12-30 11:59:25 +00:00
										 |  |  |         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, | 
					
						
							| 
									
										
										
										
											2022-06-09 16:54:44 +00:00
										 |  |  |                                         base_dir, post_filename, | 
					
						
							| 
									
										
										
										
											2021-12-30 11:59:25 +00:00
										 |  |  |                                         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, | 
					
						
							| 
									
										
										
										
											2023-10-25 19:55:40 +00:00
										 |  |  |                              signing_priv_key_pem: str, | 
					
						
							|  |  |  |                              system_language: 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, | 
					
						
							| 
									
										
										
										
											2023-10-25 19:55:40 +00:00
										 |  |  |                             post_to_box, 58391, | 
					
						
							|  |  |  |                             system_language) | 
					
						
							| 
									
										
										
										
											2021-12-30 11:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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, | 
					
						
							| 
									
										
										
										
											2023-10-25 19:55:40 +00:00
										 |  |  |                                   signing_priv_key_pem: str, | 
					
						
							|  |  |  |                                   system_language: 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, | 
					
						
							| 
									
										
										
										
											2023-10-25 19:55:40 +00:00
										 |  |  |                             post_to_box, 52594, | 
					
						
							|  |  |  |                             system_language) | 
					
						
							| 
									
										
										
										
											2021-12-30 11:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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 | 
					
						
							| 
									
										
										
										
											2022-04-09 15:11:22 +00:00
										 |  |  |     if not has_object_string_type(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) | 
					
						
							| 
									
										
										
										
											2022-06-09 10:15:22 +00:00
										 |  |  |     expected_target = \ | 
					
						
							|  |  |  |         http_prefix + '://' + domain_full + \ | 
					
						
							|  |  |  |         '/users/' + nickname + '/tlbookmarks' | 
					
						
							|  |  |  |     if message_json['target'] != expected_target: | 
					
						
							| 
									
										
										
										
											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']) | 
					
						
							| 
									
										
										
										
											2023-07-12 11:08:02 +00:00
										 |  |  |     message_url = remove_html(message_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 | 
					
						
							| 
									
										
										
										
											2022-04-09 15:11:22 +00:00
										 |  |  |     if not has_object_string_type(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) | 
					
						
							| 
									
										
										
										
											2022-06-09 10:15:22 +00:00
										 |  |  |     expected_target = \ | 
					
						
							|  |  |  |         http_prefix + '://' + domain_full + \ | 
					
						
							|  |  |  |         '/users/' + nickname + '/tlbookmarks' | 
					
						
							|  |  |  |     if message_json['target'] != expected_target: | 
					
						
							| 
									
										
										
										
											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']) | 
					
						
							| 
									
										
										
										
											2023-07-12 11:08:02 +00:00
										 |  |  |     message_url = remove_html(message_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) |