epicyon/conversation.py

80 lines
2.7 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
def updateConversation(baseDir: str, nickname: str, domain: str,
postJsonObject: {}) -> bool:
"""Ads a post to a conversation index in the /conversation subdirectory
"""
if not hasObjectDict(postJsonObject):
return False
if not postJsonObject['object'].get('conversation'):
return False
if not postJsonObject['object'].get('id'):
return False
conversationDir = acctDir(baseDir, nickname, domain) + '/conversation'
if not os.path.isdir(conversationDir):
os.mkdir(conversationDir)
conversationId = postJsonObject['object']['conversation']
conversationId = conversationId.replace('/', '#')
2021-09-28 09:44:30 +00:00
postId = removeIdEnding(postJsonObject['object']['id'])
2021-08-12 10:26:24 +00:00
conversationFilename = conversationDir + '/' + conversationId
if not os.path.isfile(conversationFilename):
try:
with open(conversationFilename, 'w+') as fp:
fp.write(postId + '\n')
return True
except BaseException:
pass
elif postId + '\n' not in open(conversationFilename).read():
try:
with open(conversationFilename, 'a+') as fp:
fp.write(postId + '\n')
return True
except BaseException:
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:
pass