| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  | __filename__ = "conversation.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2022-02-03 13:58:20 +00:00
										 |  |  | __version__ = "1.3.0" | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							| 
									
										
										
										
											2021-09-10 16:14:50 +00:00
										 |  |  | __email__ = "bob@libreserver.org" | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  | __status__ = "Production" | 
					
						
							|  |  |  | __module_group__ = "Timeline" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											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-27 11:20:57 +00:00
										 |  |  | from utils import remove_id_ending | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  | def _get_conversation_filename(base_dir: str, nickname: str, domain: str, | 
					
						
							|  |  |  |                                post_json_object: {}) -> str: | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |     """Returns the conversation filename
 | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-12-26 10:57:03 +00:00
										 |  |  |     if not has_object_dict(post_json_object): | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |     if not post_json_object['object'].get('conversation'): | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |     if not post_json_object['object'].get('id'): | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |     conversation_dir = acct_dir(base_dir, nickname, domain) + '/conversation' | 
					
						
							|  |  |  |     if not os.path.isdir(conversation_dir): | 
					
						
							|  |  |  |         os.mkdir(conversation_dir) | 
					
						
							|  |  |  |     conversation_id = post_json_object['object']['conversation'] | 
					
						
							|  |  |  |     conversation_id = conversation_id.replace('/', '#') | 
					
						
							|  |  |  |     return conversation_dir + '/' + conversation_id | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  | def update_conversation(base_dir: str, nickname: str, domain: str, | 
					
						
							|  |  |  |                         post_json_object: {}) -> bool: | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |     """Ads a post to a conversation index in the /conversation subdirectory
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |     conversation_filename = \ | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |         _get_conversation_filename(base_dir, nickname, domain, | 
					
						
							|  |  |  |                                    post_json_object) | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |     if not conversation_filename: | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |         return False | 
					
						
							| 
									
										
										
										
											2021-12-27 11:20:57 +00:00
										 |  |  |     post_id = remove_id_ending(post_json_object['object']['id']) | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |     if not os.path.isfile(conversation_filename): | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |             with open(conversation_filename, 'w+') as conv_file: | 
					
						
							|  |  |  |                 conv_file.write(post_id + '\n') | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |                 return True | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |         except OSError: | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |             print('EX: update_conversation ' + | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |                   'unable to write to ' + conversation_filename) | 
					
						
							|  |  |  |     elif post_id + '\n' not in open(conversation_filename).read(): | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |             with open(conversation_filename, 'a+') as conv_file: | 
					
						
							|  |  |  |                 conv_file.write(post_id + '\n') | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |                 return True | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |         except OSError: | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |             print('EX: update_conversation 2 ' + | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |                   'unable to write to ' + conversation_filename) | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  | def mute_conversation(base_dir: str, nickname: str, domain: str, | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |                       conversation_id: str) -> None: | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     """Mutes the given conversation
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |     conversation_dir = acct_dir(base_dir, nickname, domain) + '/conversation' | 
					
						
							|  |  |  |     conversation_filename = \ | 
					
						
							|  |  |  |         conversation_dir + '/' + conversation_id.replace('/', '#') | 
					
						
							|  |  |  |     if not os.path.isfile(conversation_filename): | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |         return | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |     if os.path.isfile(conversation_filename + '.muted'): | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |         return | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |         with open(conversation_filename + '.muted', 'w+') as conv_file: | 
					
						
							|  |  |  |             conv_file.write('\n') | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |         print('EX: unable to write mute ' + conversation_filename) | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  | def unmute_conversation(base_dir: str, nickname: str, domain: str, | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |                         conversation_id: str) -> None: | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     """Unmutes the given conversation
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |     conversation_dir = acct_dir(base_dir, nickname, domain) + '/conversation' | 
					
						
							|  |  |  |     conversation_filename = \ | 
					
						
							|  |  |  |         conversation_dir + '/' + conversation_id.replace('/', '#') | 
					
						
							|  |  |  |     if not os.path.isfile(conversation_filename): | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |         return | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |     if not os.path.isfile(conversation_filename + '.muted'): | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |         return | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |         os.remove(conversation_filename + '.muted') | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |         print('EX: unmute_conversation unable to delete ' + | 
					
						
							| 
									
										
										
										
											2021-12-30 20:32:19 +00:00
										 |  |  |               conversation_filename + '.muted') |