2021-07-06 20:38:08 +00:00
|
|
|
__filename__ = "notifyOnPost.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-07-06 20:38:08 +00:00
|
|
|
__status__ = "Production"
|
|
|
|
__module_group__ = "Calendar"
|
|
|
|
|
|
|
|
import os
|
|
|
|
from utils import removeDomainPort
|
2021-07-13 21:59:53 +00:00
|
|
|
from utils import acctDir
|
2021-07-06 20:38:08 +00:00
|
|
|
|
|
|
|
|
2021-12-25 16:17:53 +00:00
|
|
|
def _notifyOnPostArrival(base_dir: str, nickname: str, domain: str,
|
2021-07-06 20:38:08 +00:00
|
|
|
followingNickname: str,
|
|
|
|
followingDomain: str,
|
|
|
|
add: bool) -> None:
|
|
|
|
"""Adds or removes a handle from the following.txt list into a list
|
|
|
|
indicating whether to notify when a new post arrives from that account
|
|
|
|
"""
|
|
|
|
# check that a following file exists
|
|
|
|
domain = removeDomainPort(domain)
|
2021-12-25 16:17:53 +00:00
|
|
|
followingFilename = acctDir(base_dir, nickname, domain) + '/following.txt'
|
2021-07-06 20:38:08 +00:00
|
|
|
if not os.path.isfile(followingFilename):
|
|
|
|
print("WARN: following.txt doesn't exist for " +
|
|
|
|
nickname + '@' + domain)
|
|
|
|
return
|
|
|
|
handle = followingNickname + '@' + followingDomain
|
|
|
|
|
|
|
|
# check that you are following this handle
|
|
|
|
if handle + '\n' not in open(followingFilename).read():
|
|
|
|
print('WARN: ' + handle + ' is not in ' + followingFilename)
|
|
|
|
return
|
|
|
|
|
2021-07-13 21:59:53 +00:00
|
|
|
notifyOnPostFilename = \
|
2021-12-25 16:17:53 +00:00
|
|
|
acctDir(base_dir, nickname, domain) + '/notifyOnPost.txt'
|
2021-07-06 20:38:08 +00:00
|
|
|
|
|
|
|
# get the contents of the notifyOnPost file, which is
|
|
|
|
# a set of handles
|
|
|
|
followingHandles = ''
|
|
|
|
if os.path.isfile(notifyOnPostFilename):
|
|
|
|
print('notify file exists')
|
|
|
|
with open(notifyOnPostFilename, 'r') as calendarFile:
|
|
|
|
followingHandles = calendarFile.read()
|
|
|
|
else:
|
|
|
|
# create a new notifyOnPost file from the following file
|
|
|
|
print('Creating notifyOnPost file ' + notifyOnPostFilename)
|
|
|
|
followingHandles = ''
|
|
|
|
with open(followingFilename, 'r') as followingFile:
|
|
|
|
followingHandles = followingFile.read()
|
|
|
|
if add:
|
|
|
|
with open(notifyOnPostFilename, 'w+') as fp:
|
|
|
|
fp.write(followingHandles + handle + '\n')
|
|
|
|
|
|
|
|
# already in the notifyOnPost file?
|
|
|
|
if handle + '\n' in followingHandles:
|
|
|
|
print(handle + ' exists in notifyOnPost.txt')
|
|
|
|
if add:
|
|
|
|
# already added
|
|
|
|
return
|
|
|
|
# remove from calendar file
|
|
|
|
followingHandles = followingHandles.replace(handle + '\n', '')
|
|
|
|
with open(notifyOnPostFilename, 'w+') as fp:
|
|
|
|
fp.write(followingHandles)
|
|
|
|
else:
|
|
|
|
print(handle + ' not in notifyOnPost.txt')
|
|
|
|
# not already in the notifyOnPost file
|
|
|
|
if add:
|
|
|
|
# append to the list of handles
|
|
|
|
followingHandles += handle + '\n'
|
|
|
|
with open(notifyOnPostFilename, 'w+') as fp:
|
|
|
|
fp.write(followingHandles)
|
|
|
|
|
|
|
|
|
2021-12-25 16:17:53 +00:00
|
|
|
def addNotifyOnPost(base_dir: str, nickname: str, domain: str,
|
2021-07-06 20:38:08 +00:00
|
|
|
followingNickname: str,
|
|
|
|
followingDomain: str) -> None:
|
2021-12-25 16:17:53 +00:00
|
|
|
_notifyOnPostArrival(base_dir, nickname, domain,
|
2021-07-06 20:38:08 +00:00
|
|
|
followingNickname, followingDomain, True)
|
|
|
|
|
|
|
|
|
2021-12-25 16:17:53 +00:00
|
|
|
def removeNotifyOnPost(base_dir: str, nickname: str, domain: str,
|
2021-07-06 20:38:08 +00:00
|
|
|
followingNickname: str,
|
|
|
|
followingDomain: str) -> None:
|
2021-12-25 16:17:53 +00:00
|
|
|
_notifyOnPostArrival(base_dir, nickname, domain,
|
2021-07-06 20:38:08 +00:00
|
|
|
followingNickname, followingDomain, False)
|
|
|
|
|
|
|
|
|
2021-12-25 16:17:53 +00:00
|
|
|
def notifyWhenPersonPosts(base_dir: str, nickname: str, domain: str,
|
2021-07-06 20:38:08 +00:00
|
|
|
followingNickname: str,
|
|
|
|
followingDomain: str) -> bool:
|
|
|
|
"""Returns true if receiving notifications when the given publishes a post
|
|
|
|
"""
|
|
|
|
if followingNickname == nickname and followingDomain == domain:
|
|
|
|
return False
|
2021-07-06 21:53:55 +00:00
|
|
|
notifyOnPostFilename = \
|
2021-12-25 16:17:53 +00:00
|
|
|
acctDir(base_dir, nickname, domain) + '/notifyOnPost.txt'
|
2021-07-06 20:38:08 +00:00
|
|
|
handle = followingNickname + '@' + followingDomain
|
|
|
|
if not os.path.isfile(notifyOnPostFilename):
|
|
|
|
# create a new notifyOnPost file
|
|
|
|
with open(notifyOnPostFilename, 'w+') as fp:
|
|
|
|
fp.write('')
|
|
|
|
return handle + '\n' in open(notifyOnPostFilename).read()
|