epicyon/conversation.py

96 lines
3.3 KiB
Python
Raw Normal View History

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
from utils import hasObjectDict
from utils import acctDir
2021-09-28 09:44:30 +00:00
from utils import removeIdEnding
2021-08-12 10:26:24 +00:00
2021-10-14 15:12:35 +00:00
def _getConversationFilename(baseDir: str, nickname: str, domain: str,
postJsonObject: {}) -> str:
"""Returns the conversation filename
2021-08-12 10:26:24 +00:00
"""
if not hasObjectDict(postJsonObject):
2021-10-14 15:12:35 +00:00
return None
2021-08-12 10:26:24 +00:00
if not postJsonObject['object'].get('conversation'):
2021-10-14 15:12:35 +00:00
return None
2021-08-12 10:26:24 +00:00
if not postJsonObject['object'].get('id'):
2021-10-14 15:12:35 +00:00
return None
2021-08-12 10:26:24 +00:00
conversationDir = acctDir(baseDir, nickname, domain) + '/conversation'
if not os.path.isdir(conversationDir):
os.mkdir(conversationDir)
conversationId = postJsonObject['object']['conversation']
conversationId = conversationId.replace('/', '#')
2021-10-14 15:12:35 +00:00
return conversationDir + '/' + conversationId
def updateConversation(baseDir: str, nickname: str, domain: str,
postJsonObject: {}) -> bool:
"""Ads a post to a conversation index in the /conversation subdirectory
"""
conversationFilename = \
_getConversationFilename(baseDir, nickname, domain, postJsonObject)
if not conversationFilename:
return False
2021-09-28 09:44:30 +00:00
postId = removeIdEnding(postJsonObject['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
except BaseException:
2021-10-29 16:31:20 +00:00
print('EX: updateConversation ' +
'unable to write to ' + conversationFilename)
2021-08-12 10:26:24 +00:00
pass
elif postId + '\n' not in open(conversationFilename).read():
try:
with open(conversationFilename, 'a+') as fp:
fp.write(postId + '\n')
return True
except BaseException:
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
pass
return False
def muteConversation(baseDir: str, nickname: str, domain: str,
conversationId: str) -> None:
"""Mutes the given conversation
"""
2021-08-12 14:16:13 +00:00
conversationDir = acctDir(baseDir, 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-08-12 10:47:03 +00:00
with open(conversationFilename + '.muted', 'w+') as fp:
2021-08-12 10:26:24 +00:00
fp.write('\n')
def unmuteConversation(baseDir: str, nickname: str, domain: str,
conversationId: str) -> None:
"""Unmutes the given conversation
"""
conversationDir = acctDir(baseDir, nickname, domain) + '/conversation'
conversationFilename = \
conversationDir + '/' + conversationId.replace('/', '#')
if not os.path.isfile(conversationFilename):
return
if not os.path.isfile(conversationFilename + '.muted'):
return
try:
os.remove(conversationFilename + '.muted')
except BaseException:
2021-10-29 16:31:20 +00:00
print('EX: unmuteConversation unable to delete ' +
conversationFilename + '.muted')
2021-08-12 10:26:24 +00:00
pass