| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  | __filename__ = "conversation.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							|  |  |  | __version__ = "1.2.0" | 
					
						
							|  |  |  | __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-09-28 09:44:30 +00:00
										 |  |  | from utils import removeIdEnding | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 16:17:53 +00:00
										 |  |  | def _getConversationFilename(base_dir: str, nickname: str, domain: str, | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |                              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-26 12:02:29 +00:00
										 |  |  |     conversationDir = acct_dir(base_dir, nickname, domain) + '/conversation' | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     if not os.path.isdir(conversationDir): | 
					
						
							|  |  |  |         os.mkdir(conversationDir) | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |     conversationId = post_json_object['object']['conversation'] | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     conversationId = conversationId.replace('/', '#') | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |     return conversationDir + '/' + conversationId | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 16:17:53 +00:00
										 |  |  | def updateConversation(base_dir: str, nickname: str, domain: str, | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |                        post_json_object: {}) -> bool: | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |     """Ads a post to a conversation index in the /conversation subdirectory
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     conversationFilename = \ | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |         _getConversationFilename(base_dir, nickname, domain, post_json_object) | 
					
						
							| 
									
										
										
										
											2021-10-14 15:12:35 +00:00
										 |  |  |     if not conversationFilename: | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |     postId = removeIdEnding(post_json_object['object']['id']) | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     if not os.path.isfile(conversationFilename): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with open(conversationFilename, 'w+') as fp: | 
					
						
							|  |  |  |                 fp.write(postId + '\n') | 
					
						
							|  |  |  |                 return True | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |         except OSError: | 
					
						
							| 
									
										
										
										
											2021-10-29 16:31:20 +00:00
										 |  |  |             print('EX: updateConversation ' + | 
					
						
							|  |  |  |                   'unable to write to ' + conversationFilename) | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     elif postId + '\n' not in open(conversationFilename).read(): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with open(conversationFilename, 'a+') as fp: | 
					
						
							|  |  |  |                 fp.write(postId + '\n') | 
					
						
							|  |  |  |                 return True | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |         except OSError: | 
					
						
							| 
									
										
										
										
											2021-10-29 16:31:20 +00:00
										 |  |  |             print('EX: updateConversation 2 ' + | 
					
						
							|  |  |  |                   'unable to write to ' + conversationFilename) | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 16:17:53 +00:00
										 |  |  | def muteConversation(base_dir: str, nickname: str, domain: str, | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |                      conversationId: str) -> None: | 
					
						
							|  |  |  |     """Mutes the given conversation
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-12-26 12:02:29 +00:00
										 |  |  |     conversationDir = acct_dir(base_dir, nickname, domain) + '/conversation' | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     conversationFilename = \ | 
					
						
							|  |  |  |         conversationDir + '/' + conversationId.replace('/', '#') | 
					
						
							|  |  |  |     if not os.path.isfile(conversationFilename): | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     if os.path.isfile(conversationFilename + '.muted'): | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         with open(conversationFilename + '.muted', 'w+') as fp: | 
					
						
							|  |  |  |             fp.write('\n') | 
					
						
							|  |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2021-11-25 22:22:54 +00:00
										 |  |  |         print('EX: unable to write mute ' + conversationFilename) | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 16:17:53 +00:00
										 |  |  | def unmuteConversation(base_dir: str, nickname: str, domain: str, | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |                        conversationId: str) -> None: | 
					
						
							|  |  |  |     """Unmutes the given conversation
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-12-26 12:02:29 +00:00
										 |  |  |     conversationDir = acct_dir(base_dir, nickname, domain) + '/conversation' | 
					
						
							| 
									
										
										
										
											2021-08-12 10:26:24 +00:00
										 |  |  |     conversationFilename = \ | 
					
						
							|  |  |  |         conversationDir + '/' + conversationId.replace('/', '#') | 
					
						
							|  |  |  |     if not os.path.isfile(conversationFilename): | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     if not os.path.isfile(conversationFilename + '.muted'): | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         os.remove(conversationFilename + '.muted') | 
					
						
							| 
									
										
										
										
											2021-11-25 18:42:38 +00:00
										 |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2021-10-29 16:31:20 +00:00
										 |  |  |         print('EX: unmuteConversation unable to delete ' + | 
					
						
							|  |  |  |               conversationFilename + '.muted') |