| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | __filename__ = "conversation.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2024-12-22 23:37:30 +00:00
										 |  |  | __version__ = "1.6.0" | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							|  |  |  | __email__ = "bob@libreserver.org" | 
					
						
							|  |  |  | __status__ = "Production" | 
					
						
							|  |  |  | __module_group__ = "Timeline" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | from conversation import download_conversation_posts | 
					
						
							| 
									
										
										
										
											2024-09-13 13:58:14 +00:00
										 |  |  | from flags import is_public_post | 
					
						
							| 
									
										
										
										
											2024-05-29 14:48:51 +00:00
										 |  |  | from utils import remove_id_ending | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | from utils import get_config_param | 
					
						
							| 
									
										
										
										
											2023-01-30 20:56:47 +00:00
										 |  |  | from utils import get_nickname_from_actor | 
					
						
							|  |  |  | from utils import get_domain_from_actor | 
					
						
							| 
									
										
										
										
											2023-09-26 20:25:53 +00:00
										 |  |  | from utils import get_attributed_to | 
					
						
							| 
									
										
										
										
											2024-12-18 19:20:27 +00:00
										 |  |  | from utils import text_mode_removals | 
					
						
							| 
									
										
										
										
											2023-01-30 20:56:47 +00:00
										 |  |  | from blocking import is_blocked | 
					
						
							| 
									
										
										
										
											2024-04-09 16:24:08 +00:00
										 |  |  | from webapp_utils import text_mode_browser | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | from webapp_utils import html_header_with_external_style | 
					
						
							|  |  |  | from webapp_utils import html_post_separator | 
					
						
							|  |  |  | from webapp_utils import html_footer | 
					
						
							| 
									
										
										
										
											2024-06-20 11:44:13 +00:00
										 |  |  | from webapp_utils import get_banner_file | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | from webapp_post import individual_post_as_html | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 14:34:41 +00:00
										 |  |  | def html_conversation_view(authorized: bool, post_id: str, | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |                            translate: {}, base_dir: str, | 
					
						
							|  |  |  |                            http_prefix: str, | 
					
						
							|  |  |  |                            nickname: str, domain: str, | 
					
						
							|  |  |  |                            project_version: str, | 
					
						
							|  |  |  |                            recent_posts_cache: {}, | 
					
						
							|  |  |  |                            max_recent_posts: int, | 
					
						
							|  |  |  |                            session, | 
					
						
							|  |  |  |                            cached_webfingers, | 
					
						
							|  |  |  |                            person_cache: {}, | 
					
						
							|  |  |  |                            port: int, | 
					
						
							|  |  |  |                            yt_replace_domain: str, | 
					
						
							|  |  |  |                            twitter_replacement_domain: str, | 
					
						
							|  |  |  |                            show_published_date_only: bool, | 
					
						
							|  |  |  |                            peertube_instances: [], | 
					
						
							|  |  |  |                            allow_local_network_access: bool, | 
					
						
							|  |  |  |                            theme_name: str, | 
					
						
							|  |  |  |                            system_language: str, | 
					
						
							|  |  |  |                            max_like_count: int, | 
					
						
							|  |  |  |                            signing_priv_key_pem: str, | 
					
						
							|  |  |  |                            cw_lists: {}, | 
					
						
							|  |  |  |                            lists_enabled: str, | 
					
						
							|  |  |  |                            timezone: str, bold_reading: bool, | 
					
						
							|  |  |  |                            dogwhistles: {}, access_keys: {}, | 
					
						
							|  |  |  |                            min_images_for_accounts: [], | 
					
						
							| 
									
										
										
										
											2023-01-30 20:56:47 +00:00
										 |  |  |                            debug: bool, buy_sites: {}, | 
					
						
							| 
									
										
										
										
											2024-01-18 13:27:22 +00:00
										 |  |  |                            blocked_cache: [], | 
					
						
							| 
									
										
										
										
											2024-02-09 22:09:18 +00:00
										 |  |  |                            block_federated: [], | 
					
						
							| 
									
										
										
										
											2024-04-09 16:24:08 +00:00
										 |  |  |                            auto_cw_cache: {}, | 
					
						
							| 
									
										
										
										
											2024-06-20 11:44:13 +00:00
										 |  |  |                            ua_str: str, | 
					
						
							| 
									
										
										
										
											2024-12-17 13:50:48 +00:00
										 |  |  |                            default_timeline: str, | 
					
						
							| 
									
										
										
										
											2025-01-20 17:55:29 +00:00
										 |  |  |                            mitm_servers: [], | 
					
						
							|  |  |  |                            instance_software: {}) -> str: | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |     """Show a page containing a conversation thread
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     conv_posts = \ | 
					
						
							| 
									
										
										
										
											2023-03-01 14:34:41 +00:00
										 |  |  |         download_conversation_posts(authorized, | 
					
						
							|  |  |  |                                     session, http_prefix, base_dir, | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |                                     nickname, domain, | 
					
						
							| 
									
										
										
										
											2024-12-17 13:50:48 +00:00
										 |  |  |                                     post_id, debug, mitm_servers) | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if not conv_posts: | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     css_filename = base_dir + '/epicyon-profile.css' | 
					
						
							|  |  |  |     if os.path.isfile(base_dir + '/epicyon.css'): | 
					
						
							|  |  |  |         css_filename = base_dir + '/epicyon.css' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     instance_title = \ | 
					
						
							|  |  |  |         get_config_param(base_dir, 'instanceTitle') | 
					
						
							| 
									
										
										
										
											2024-12-23 18:23:47 +00:00
										 |  |  |     preload_images: list[str] = [] | 
					
						
							| 
									
										
										
										
											2024-11-16 13:18:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     metadata_str = '' | 
					
						
							|  |  |  |     if post_id: | 
					
						
							|  |  |  |         # https://swicg.github.io/activitypub-html-discovery/#html-link-element | 
					
						
							|  |  |  |         # link to the activitypub post | 
					
						
							|  |  |  |         metadata_str += \ | 
					
						
							|  |  |  |             '    <link rel="alternate" type="application/activity+json" ' + \ | 
					
						
							|  |  |  |             'href="' + post_id + '" />\n' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |     conv_str = \ | 
					
						
							| 
									
										
										
										
											2024-11-16 13:18:12 +00:00
										 |  |  |         html_header_with_external_style(css_filename, instance_title, | 
					
						
							|  |  |  |                                         metadata_str, preload_images) | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-20 11:44:13 +00:00
										 |  |  |     # banner and row of buttons | 
					
						
							|  |  |  |     users_path = '/users/' + nickname | 
					
						
							|  |  |  |     banner_file, _ = get_banner_file(base_dir, nickname, domain, theme_name) | 
					
						
							|  |  |  |     conv_str += \ | 
					
						
							|  |  |  |         '<header>\n' + \ | 
					
						
							|  |  |  |         '  <a href="/users/' + nickname + '/' + \ | 
					
						
							|  |  |  |         default_timeline + '" title="' + \ | 
					
						
							|  |  |  |         translate['Switch to timeline view'] + '" alt="' + \ | 
					
						
							|  |  |  |         translate['Switch to timeline view'] + '" ' + \ | 
					
						
							|  |  |  |         'aria-flowto="containerHeader" tabindex="1" accesskey="' + \ | 
					
						
							|  |  |  |         access_keys['menuTimeline'] + '">\n' | 
					
						
							|  |  |  |     conv_str += '<img loading="lazy" decoding="async" ' + \ | 
					
						
							|  |  |  |         'class="timeline-banner" alt="" ' + \ | 
					
						
							|  |  |  |         'src="' + users_path + '/' + banner_file + '" /></a>\n' + \ | 
					
						
							|  |  |  |         '</header>\n' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |     separator_str = html_post_separator(base_dir, None) | 
					
						
							| 
									
										
										
										
											2023-01-30 20:16:45 +00:00
										 |  |  |     text_mode_separator = '<div class="transparent"><hr></div>\n' | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     minimize_all_images = False | 
					
						
							|  |  |  |     if nickname in min_images_for_accounts: | 
					
						
							|  |  |  |         minimize_all_images = True | 
					
						
							| 
									
										
										
										
											2024-05-29 15:03:39 +00:00
										 |  |  |     current_reading_str = '' | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |     for post_json_object in conv_posts: | 
					
						
							| 
									
										
										
										
											2023-01-30 20:10:37 +00:00
										 |  |  |         show_individual_post_icons = True | 
					
						
							| 
									
										
										
										
											2023-03-01 14:34:41 +00:00
										 |  |  |         # if not authorized then only show public posts | 
					
						
							|  |  |  |         if not authorized: | 
					
						
							|  |  |  |             show_individual_post_icons = False | 
					
						
							|  |  |  |             if not is_public_post(post_json_object): | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2023-09-26 20:25:53 +00:00
										 |  |  |         from_actor = \ | 
					
						
							|  |  |  |             get_attributed_to(post_json_object['object']['attributedTo']) | 
					
						
							| 
									
										
										
										
											2023-01-30 20:56:47 +00:00
										 |  |  |         from_nickname = get_nickname_from_actor(from_actor) | 
					
						
							|  |  |  |         from_domain, _ = get_domain_from_actor(from_actor) | 
					
						
							|  |  |  |         # don't show icons on posts from blocked accounts/instances | 
					
						
							|  |  |  |         if from_nickname and from_domain: | 
					
						
							|  |  |  |             if is_blocked(base_dir, nickname, domain, | 
					
						
							|  |  |  |                           from_nickname, from_domain, | 
					
						
							| 
									
										
										
										
											2024-02-09 22:09:18 +00:00
										 |  |  |                           blocked_cache, block_federated): | 
					
						
							| 
									
										
										
										
											2023-01-30 20:56:47 +00:00
										 |  |  |                 show_individual_post_icons = False | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |         allow_deletion = False | 
					
						
							|  |  |  |         post_str = \ | 
					
						
							|  |  |  |             individual_post_as_html(signing_priv_key_pem, | 
					
						
							|  |  |  |                                     True, recent_posts_cache, | 
					
						
							|  |  |  |                                     max_recent_posts, | 
					
						
							|  |  |  |                                     translate, None, | 
					
						
							|  |  |  |                                     base_dir, session, cached_webfingers, | 
					
						
							|  |  |  |                                     person_cache, | 
					
						
							|  |  |  |                                     nickname, domain, port, | 
					
						
							|  |  |  |                                     post_json_object, | 
					
						
							|  |  |  |                                     None, True, allow_deletion, | 
					
						
							|  |  |  |                                     http_prefix, project_version, | 
					
						
							|  |  |  |                                     'search', | 
					
						
							|  |  |  |                                     yt_replace_domain, | 
					
						
							|  |  |  |                                     twitter_replacement_domain, | 
					
						
							|  |  |  |                                     show_published_date_only, | 
					
						
							|  |  |  |                                     peertube_instances, | 
					
						
							|  |  |  |                                     allow_local_network_access, | 
					
						
							|  |  |  |                                     theme_name, system_language, | 
					
						
							|  |  |  |                                     max_like_count, | 
					
						
							|  |  |  |                                     show_individual_post_icons, | 
					
						
							|  |  |  |                                     show_individual_post_icons, | 
					
						
							|  |  |  |                                     False, False, False, False, | 
					
						
							|  |  |  |                                     cw_lists, lists_enabled, | 
					
						
							|  |  |  |                                     timezone, False, bold_reading, | 
					
						
							|  |  |  |                                     dogwhistles, | 
					
						
							| 
									
										
										
										
											2023-01-13 15:04:48 +00:00
										 |  |  |                                     minimize_all_images, None, | 
					
						
							| 
									
										
										
										
											2024-12-17 13:50:48 +00:00
										 |  |  |                                     buy_sites, auto_cw_cache, | 
					
						
							| 
									
										
										
										
											2025-01-20 17:55:29 +00:00
										 |  |  |                                     mitm_servers, | 
					
						
							|  |  |  |                                     instance_software) | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |         if post_str: | 
					
						
							| 
									
										
										
										
											2024-05-29 15:03:39 +00:00
										 |  |  |             conv_str += \ | 
					
						
							|  |  |  |                 current_reading_str + text_mode_separator + \ | 
					
						
							|  |  |  |                 separator_str + post_str | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # show separator at the current reading point | 
					
						
							|  |  |  |         current_reading_str = '' | 
					
						
							|  |  |  |         if post_json_object.get('id'): | 
					
						
							|  |  |  |             if isinstance(post_json_object['id'], str): | 
					
						
							|  |  |  |                 id_str = remove_id_ending(post_json_object['id']) | 
					
						
							|  |  |  |                 if post_id in id_str: | 
					
						
							|  |  |  |                     current_reading_str = '<br><hr><br>\n' | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-09 16:24:08 +00:00
										 |  |  |     # if using a text mode browser then don't show SHOW MORE because there | 
					
						
							| 
									
										
										
										
											2024-12-18 12:13:38 +00:00
										 |  |  |     # is no way to hide/expand sections. | 
					
						
							|  |  |  |     # Also replace MITM text with an eye icon | 
					
						
							| 
									
										
										
										
											2024-04-09 16:24:08 +00:00
										 |  |  |     if text_mode_browser(ua_str): | 
					
						
							| 
									
										
										
										
											2024-12-18 19:20:27 +00:00
										 |  |  |         conv_str = text_mode_removals(conv_str, translate) | 
					
						
							| 
									
										
										
										
											2024-04-09 16:24:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-28 10:18:13 +00:00
										 |  |  |     conv_str += text_mode_separator + html_footer() | 
					
						
							|  |  |  |     return conv_str |