epicyon/conversation.py

96 lines
3.4 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
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
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-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
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')
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')