| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  | __filename__ = "webapp_likers.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2023-01-21 23:03:30 +00:00
										 |  |  | __version__ = "1.4.0" | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							|  |  |  | __email__ = "bob@libreserver.org" | 
					
						
							|  |  |  | __status__ = "Production" | 
					
						
							|  |  |  | __module_group__ = "ActivityPub" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | from utils import locate_post | 
					
						
							|  |  |  | from utils import get_config_param | 
					
						
							|  |  |  | from utils import get_account_timezone | 
					
						
							|  |  |  | from utils import get_display_name | 
					
						
							|  |  |  | from utils import get_nickname_from_actor | 
					
						
							|  |  |  | from utils import has_object_dict | 
					
						
							| 
									
										
										
										
											2022-03-01 14:11:29 +00:00
										 |  |  | from utils import load_json | 
					
						
							| 
									
										
										
										
											2022-03-02 12:01:05 +00:00
										 |  |  | from person import get_person_avatar_url | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  | from webapp_utils import html_header_with_external_style | 
					
						
							|  |  |  | from webapp_utils import html_footer | 
					
						
							|  |  |  | from webapp_utils import get_banner_file | 
					
						
							| 
									
										
										
										
											2022-03-02 11:03:56 +00:00
										 |  |  | from webapp_utils import add_emoji_to_display_name | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  | from webapp_post import individual_post_as_html | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def html_likers_of_post(base_dir: str, nickname: str, | 
					
						
							|  |  |  |                         domain: str, port: int, | 
					
						
							|  |  |  |                         post_url: str, translate: {}, | 
					
						
							|  |  |  |                         http_prefix: str, | 
					
						
							|  |  |  |                         theme: str, access_keys: {}, | 
					
						
							|  |  |  |                         recent_posts_cache: {}, max_recent_posts: int, | 
					
						
							|  |  |  |                         session, cached_webfingers: {}, | 
					
						
							|  |  |  |                         person_cache: {}, | 
					
						
							|  |  |  |                         project_version: str, | 
					
						
							|  |  |  |                         yt_replace_domain: str, | 
					
						
							|  |  |  |                         twitter_replacement_domain: str, | 
					
						
							|  |  |  |                         show_published_date_only: bool, | 
					
						
							|  |  |  |                         peertube_instances: [], | 
					
						
							|  |  |  |                         allow_local_network_access: bool, | 
					
						
							|  |  |  |                         system_language: str, | 
					
						
							|  |  |  |                         max_like_count: int, signing_priv_key_pem: str, | 
					
						
							|  |  |  |                         cw_lists: {}, lists_enabled: str, | 
					
						
							| 
									
										
										
										
											2022-05-30 18:33:51 +00:00
										 |  |  |                         box_name: str, default_timeline: str, | 
					
						
							| 
									
										
										
										
											2022-07-05 14:40:26 +00:00
										 |  |  |                         bold_reading: bool, dogwhistles: {}, | 
					
						
							| 
									
										
										
										
											2022-11-13 18:54:11 +00:00
										 |  |  |                         min_images_for_accounts: [], | 
					
						
							| 
									
										
										
										
											2023-01-13 15:04:48 +00:00
										 |  |  |                         buy_sites: {}, | 
					
						
							| 
									
										
										
										
											2022-03-02 14:27:31 +00:00
										 |  |  |                         dict_name: str = 'likes') -> str: | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |     """Returns html for a screen showing who liked a post
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     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') | 
					
						
							|  |  |  |     html_str = \ | 
					
						
							|  |  |  |         html_header_with_external_style(css_filename, instance_title, None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # get the post which was liked | 
					
						
							| 
									
										
										
										
											2022-03-01 14:11:29 +00:00
										 |  |  |     filename = locate_post(base_dir, nickname, domain, post_url) | 
					
						
							|  |  |  |     if not filename: | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  |     post_json_object = load_json(filename) | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |     if not post_json_object: | 
					
						
							| 
									
										
										
										
											2022-03-01 14:01:08 +00:00
										 |  |  |         return None | 
					
						
							|  |  |  |     if not post_json_object.get('actor') or not post_json_object.get('object'): | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # show the top banner | 
					
						
							|  |  |  |     banner_file, _ = \ | 
					
						
							|  |  |  |         get_banner_file(base_dir, nickname, domain, theme) | 
					
						
							|  |  |  |     html_str += \ | 
					
						
							|  |  |  |         '<header>\n' + \ | 
					
						
							|  |  |  |         '<a href="/users/' + nickname + '/' + default_timeline + \ | 
					
						
							|  |  |  |         '" title="' + \ | 
					
						
							|  |  |  |         translate['Switch to timeline view'] + '" alt="' + \ | 
					
						
							|  |  |  |         translate['Switch to timeline view'] + '" ' + \ | 
					
						
							|  |  |  |         'accesskey="' + access_keys['menuTimeline'] + '">\n' | 
					
						
							| 
									
										
										
										
											2022-03-28 08:47:53 +00:00
										 |  |  |     html_str += '<img loading="lazy" decoding="async" ' + \ | 
					
						
							|  |  |  |         'class="timeline-banner" src="' + \ | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |         '/users/' + nickname + '/' + banner_file + '" alt="" /></a>\n' + \ | 
					
						
							|  |  |  |         '</header>\n' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # show the post which was liked | 
					
						
							|  |  |  |     timezone = get_account_timezone(base_dir, nickname, domain) | 
					
						
							| 
									
										
										
										
											2022-03-14 19:55:36 +00:00
										 |  |  |     mitm = False | 
					
						
							|  |  |  |     if os.path.isfile(filename.replace('.json', '') + '.mitm'): | 
					
						
							|  |  |  |         mitm = True | 
					
						
							| 
									
										
										
										
											2022-11-13 18:54:11 +00:00
										 |  |  |     minimize_all_images = False | 
					
						
							|  |  |  |     if nickname in min_images_for_accounts: | 
					
						
							|  |  |  |         minimize_all_images = True | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |     html_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, False, | 
					
						
							|  |  |  |                                 http_prefix, | 
					
						
							|  |  |  |                                 project_version, | 
					
						
							| 
									
										
										
										
											2022-05-30 18:33:51 +00:00
										 |  |  |                                 box_name, | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |                                 yt_replace_domain, | 
					
						
							|  |  |  |                                 twitter_replacement_domain, | 
					
						
							|  |  |  |                                 show_published_date_only, | 
					
						
							|  |  |  |                                 peertube_instances, | 
					
						
							|  |  |  |                                 allow_local_network_access, | 
					
						
							|  |  |  |                                 theme, system_language, | 
					
						
							|  |  |  |                                 max_like_count, | 
					
						
							|  |  |  |                                 False, False, False, | 
					
						
							|  |  |  |                                 False, False, False, | 
					
						
							|  |  |  |                                 cw_lists, lists_enabled, | 
					
						
							| 
									
										
										
										
											2022-07-05 14:40:26 +00:00
										 |  |  |                                 timezone, mitm, bold_reading, | 
					
						
							| 
									
										
										
										
											2022-11-13 18:54:11 +00:00
										 |  |  |                                 dogwhistles, | 
					
						
							| 
									
										
										
										
											2023-01-13 15:04:48 +00:00
										 |  |  |                                 minimize_all_images, None, | 
					
						
							|  |  |  |                                 buy_sites) | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # show likers beneath the post | 
					
						
							|  |  |  |     obj = post_json_object | 
					
						
							|  |  |  |     if has_object_dict(post_json_object): | 
					
						
							|  |  |  |         obj = post_json_object['object'] | 
					
						
							| 
									
										
										
										
											2022-03-02 14:27:31 +00:00
										 |  |  |     if not obj.get(dict_name): | 
					
						
							| 
									
										
										
										
											2022-03-01 14:01:08 +00:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											2022-03-02 14:27:31 +00:00
										 |  |  |     if not isinstance(obj[dict_name], dict): | 
					
						
							| 
									
										
										
										
											2022-03-01 14:01:08 +00:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											2022-03-02 14:27:31 +00:00
										 |  |  |     if not obj[dict_name].get('items'): | 
					
						
							| 
									
										
										
										
											2022-03-01 14:01:08 +00:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-02 14:27:31 +00:00
										 |  |  |     if dict_name == 'likes': | 
					
						
							|  |  |  |         html_str += \ | 
					
						
							|  |  |  |             '<center><h2>' + translate['Liked by'] + '</h2></center>\n' | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         html_str += \ | 
					
						
							|  |  |  |             '<center><h2>' + translate['Repeated by'] + '</h2></center>\n' | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     likers_list = '' | 
					
						
							| 
									
										
										
										
											2022-03-02 14:27:31 +00:00
										 |  |  |     for like_item in obj[dict_name]['items']: | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |         if not like_item.get('actor'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         liker_actor = like_item['actor'] | 
					
						
							|  |  |  |         liker_display_name = \ | 
					
						
							|  |  |  |             get_display_name(base_dir, liker_actor, person_cache) | 
					
						
							|  |  |  |         if liker_display_name: | 
					
						
							|  |  |  |             liker_name = liker_display_name | 
					
						
							| 
									
										
										
										
											2022-03-02 11:03:56 +00:00
										 |  |  |             if ':' in liker_name: | 
					
						
							|  |  |  |                 liker_name = \ | 
					
						
							|  |  |  |                     add_emoji_to_display_name(session, base_dir, | 
					
						
							|  |  |  |                                               http_prefix, | 
					
						
							|  |  |  |                                               nickname, domain, | 
					
						
							| 
									
										
										
										
											2022-07-18 16:18:04 +00:00
										 |  |  |                                               liker_name, False, | 
					
						
							|  |  |  |                                               translate) | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             liker_name = get_nickname_from_actor(liker_actor) | 
					
						
							| 
									
										
										
										
											2022-03-23 23:59:29 +00:00
										 |  |  |             if not liker_name: | 
					
						
							|  |  |  |                 liker_name = 'unknown' | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |         if likers_list: | 
					
						
							| 
									
										
										
										
											2022-03-01 14:52:17 +00:00
										 |  |  |             likers_list += ' ' | 
					
						
							| 
									
										
										
										
											2022-03-02 12:01:05 +00:00
										 |  |  |         liker_avatar_url = \ | 
					
						
							| 
									
										
										
										
											2022-06-12 12:30:14 +00:00
										 |  |  |             get_person_avatar_url(base_dir, liker_actor, person_cache) | 
					
						
							| 
									
										
										
										
											2022-03-02 13:54:38 +00:00
										 |  |  |         if not liker_avatar_url: | 
					
						
							|  |  |  |             liker_avatar_url = '' | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             liker_avatar_url = ';' + liker_avatar_url | 
					
						
							| 
									
										
										
										
											2022-03-02 12:01:05 +00:00
										 |  |  |         liker_options_link = \ | 
					
						
							|  |  |  |             '/users/' + nickname + '?options=' + \ | 
					
						
							| 
									
										
										
										
											2022-03-02 13:54:38 +00:00
										 |  |  |             liker_actor + ';1' + liker_avatar_url | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |         likers_list += \ | 
					
						
							| 
									
										
										
										
											2022-03-01 14:52:17 +00:00
										 |  |  |             '<label class="likerNames">' + \ | 
					
						
							| 
									
										
										
										
											2022-03-02 12:01:05 +00:00
										 |  |  |             '<a href="' + liker_options_link + '">' + liker_name + '</a>' + \ | 
					
						
							| 
									
										
										
										
											2022-03-01 14:40:04 +00:00
										 |  |  |             '</label>' | 
					
						
							| 
									
										
										
										
											2022-03-01 13:10:10 +00:00
										 |  |  |     html_str += '<center>\n' + likers_list + '\n</center>\n' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return html_str + html_footer() |