| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  | __filename__ = "pronouns.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2024-12-22 23:37:30 +00:00
										 |  |  | __version__ = "1.6.0" | 
					
						
							| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							|  |  |  | __email__ = "bob@libreserver.org" | 
					
						
							|  |  |  | __status__ = "Production" | 
					
						
							|  |  |  | __module_group__ = "Profile Metadata" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from utils import get_attachment_property_value | 
					
						
							|  |  |  | from utils import remove_html | 
					
						
							| 
									
										
										
										
											2024-08-12 16:52:45 +00:00
										 |  |  | from utils import string_contains | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pronoun_fieldnames = ('pronoun', 'they/them', 'he/him', 'she/her') | 
					
						
							| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_pronouns(actor_json: {}) -> str: | 
					
						
							|  |  |  |     """Returns pronouns for the given actor
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if not actor_json.get('attachment'): | 
					
						
							|  |  |  |         return '' | 
					
						
							|  |  |  |     if not isinstance(actor_json['attachment'], list): | 
					
						
							|  |  |  |         return '' | 
					
						
							|  |  |  |     for property_value in actor_json['attachment']: | 
					
						
							|  |  |  |         name_value = None | 
					
						
							|  |  |  |         if property_value.get('name'): | 
					
						
							|  |  |  |             name_value = property_value['name'].lower() | 
					
						
							|  |  |  |         elif property_value.get('schema:name'): | 
					
						
							|  |  |  |             name_value = property_value['schema:name'].lower() | 
					
						
							|  |  |  |         if not name_value: | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2024-08-12 16:52:45 +00:00
										 |  |  |         if not string_contains(name_value, pronoun_fieldnames): | 
					
						
							| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  |             continue | 
					
						
							|  |  |  |         if not property_value.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         prop_value_name, _ = \ | 
					
						
							|  |  |  |             get_attachment_property_value(property_value) | 
					
						
							|  |  |  |         if not prop_value_name: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not property_value['type'].endswith('PropertyValue'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         pronouns_text = property_value[prop_value_name] | 
					
						
							|  |  |  |         return remove_html(pronouns_text) | 
					
						
							|  |  |  |     return '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def set_pronouns(actor_json: {}, pronouns: str) -> None: | 
					
						
							|  |  |  |     """Sets pronouns for the given actor
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if not actor_json.get('attachment'): | 
					
						
							| 
									
										
										
										
											2024-12-23 17:45:20 +00:00
										 |  |  |         actor_json['attachment']: list[dict] = [] | 
					
						
							| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # remove any existing value | 
					
						
							|  |  |  |     property_found = None | 
					
						
							|  |  |  |     for property_value in actor_json['attachment']: | 
					
						
							|  |  |  |         name_value = None | 
					
						
							|  |  |  |         if property_value.get('name'): | 
					
						
							|  |  |  |             name_value = property_value['name'].lower() | 
					
						
							|  |  |  |         elif property_value.get('schema:name'): | 
					
						
							|  |  |  |             name_value = property_value['schema:name'].lower() | 
					
						
							|  |  |  |         if not name_value: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not property_value.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2024-08-12 16:52:45 +00:00
										 |  |  |         if not string_contains(name_value, pronoun_fieldnames): | 
					
						
							| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  |             continue | 
					
						
							|  |  |  |         property_found = property_value | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if property_found: | 
					
						
							|  |  |  |         actor_json['attachment'].remove(property_found) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for property_value in actor_json['attachment']: | 
					
						
							|  |  |  |         name_value = None | 
					
						
							|  |  |  |         if property_value.get('name'): | 
					
						
							|  |  |  |             name_value = property_value['name'] | 
					
						
							|  |  |  |         elif property_value.get('schema:name'): | 
					
						
							|  |  |  |             name_value = property_value['schema:name'] | 
					
						
							|  |  |  |         if not name_value: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not property_value.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         name_value = name_value.lower() | 
					
						
							| 
									
										
										
										
											2024-08-12 16:52:45 +00:00
										 |  |  |         if not string_contains(name_value, pronoun_fieldnames): | 
					
						
							| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  |             continue | 
					
						
							|  |  |  |         if not property_value['type'].endswith('PropertyValue'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         prop_value_name, _ = \ | 
					
						
							|  |  |  |             get_attachment_property_value(property_value) | 
					
						
							|  |  |  |         if not prop_value_name: | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2024-08-12 13:15:21 +00:00
										 |  |  |         property_value[prop_value_name] = remove_html(pronouns) | 
					
						
							| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     new_pronouns = { | 
					
						
							|  |  |  |         "type": "PropertyValue", | 
					
						
							|  |  |  |         "name": "Pronouns", | 
					
						
							| 
									
										
										
										
											2024-08-12 13:15:21 +00:00
										 |  |  |         "value": remove_html(pronouns) | 
					
						
							| 
									
										
										
										
											2024-08-12 10:34:50 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     actor_json['attachment'].append(new_pronouns) |