2021-07-06 20:38:08 +00:00
|
|
|
__filename__ = "notifyOnPost.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2024-01-21 19:01:20 +00:00
|
|
|
__version__ = "1.5.0"
|
2021-07-06 20:38:08 +00:00
|
|
|
__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
|
2021-12-26 18:17:37 +00:00
|
|
|
from utils import remove_domain_port
|
2021-12-26 12:02:29 +00:00
|
|
|
from utils import acct_dir
|
2022-06-10 11:43:33 +00:00
|
|
|
from utils import text_in_file
|
2021-07-06 20:38:08 +00:00
|
|
|
|
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def _notify_on_post_arrival(base_dir: str, nickname: str, domain: str,
|
2022-01-03 12:41:06 +00:00
|
|
|
following_nickname: str,
|
|
|
|
following_domain: str,
|
2021-12-29 21:55:09 +00:00
|
|
|
add: bool) -> None:
|
2021-07-06 20:38:08 +00:00
|
|
|
"""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
|
2021-12-26 18:17:37 +00:00
|
|
|
domain = remove_domain_port(domain)
|
2022-01-03 12:41:06 +00:00
|
|
|
following_filename = \
|
|
|
|
acct_dir(base_dir, nickname, domain) + '/following.txt'
|
|
|
|
if not os.path.isfile(following_filename):
|
2021-07-06 20:38:08 +00:00
|
|
|
print("WARN: following.txt doesn't exist for " +
|
|
|
|
nickname + '@' + domain)
|
|
|
|
return
|
2022-01-03 12:41:06 +00:00
|
|
|
handle = following_nickname + '@' + following_domain
|
2021-07-06 20:38:08 +00:00
|
|
|
|
|
|
|
# check that you are following this handle
|
2022-12-30 21:39:25 +00:00
|
|
|
if not text_in_file(handle + '\n', following_filename, False):
|
2022-01-03 12:41:06 +00:00
|
|
|
print('WARN: ' + handle + ' is not in ' + following_filename)
|
2021-07-06 20:38:08 +00:00
|
|
|
return
|
|
|
|
|
2022-01-03 12:41:06 +00:00
|
|
|
notify_on_post_filename = \
|
2021-12-26 12:02:29 +00:00
|
|
|
acct_dir(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
|
2022-01-03 12:41:06 +00:00
|
|
|
following_handles = ''
|
|
|
|
if os.path.isfile(notify_on_post_filename):
|
2021-07-06 20:38:08 +00:00
|
|
|
print('notify file exists')
|
2024-02-01 13:30:59 +00:00
|
|
|
try:
|
|
|
|
with open(notify_on_post_filename, 'r',
|
2024-07-14 13:01:46 +00:00
|
|
|
encoding='utf-8') as fp_calendar:
|
|
|
|
following_handles = fp_calendar.read()
|
2024-02-01 13:30:59 +00:00
|
|
|
except OSError:
|
|
|
|
print('EX: _notify_on_post_arrival unable to read 1 ' +
|
|
|
|
notify_on_post_filename)
|
2021-07-06 20:38:08 +00:00
|
|
|
else:
|
|
|
|
# create a new notifyOnPost file from the following file
|
2022-01-03 12:41:06 +00:00
|
|
|
print('Creating notifyOnPost file ' + notify_on_post_filename)
|
|
|
|
following_handles = ''
|
2024-02-01 13:30:59 +00:00
|
|
|
try:
|
|
|
|
with open(following_filename, 'r',
|
2024-07-14 13:01:46 +00:00
|
|
|
encoding='utf-8') as fp_following:
|
|
|
|
following_handles = fp_following.read()
|
2024-02-01 13:30:59 +00:00
|
|
|
except OSError:
|
|
|
|
print('EX: _notify_on_post_arrival unable to read 2 ' +
|
|
|
|
following_filename)
|
2021-07-06 20:38:08 +00:00
|
|
|
if add:
|
2024-02-01 13:30:59 +00:00
|
|
|
try:
|
|
|
|
with open(notify_on_post_filename, 'w+',
|
|
|
|
encoding='utf-8') as fp_notify:
|
|
|
|
fp_notify.write(following_handles + handle + '\n')
|
|
|
|
except OSError:
|
|
|
|
print('EX: _notify_on_post_arrival unable to write 1' +
|
|
|
|
notify_on_post_filename)
|
2021-07-06 20:38:08 +00:00
|
|
|
|
|
|
|
# already in the notifyOnPost file?
|
2022-12-30 21:39:25 +00:00
|
|
|
if handle + '\n' in following_handles or \
|
|
|
|
handle + '\n' in following_handles.lower():
|
2021-07-06 20:38:08 +00:00
|
|
|
print(handle + ' exists in notifyOnPost.txt')
|
|
|
|
if add:
|
|
|
|
# already added
|
|
|
|
return
|
|
|
|
# remove from calendar file
|
2022-12-30 21:39:25 +00:00
|
|
|
new_following_handles = ''
|
|
|
|
following_handles_list = following_handles.split('\n')
|
|
|
|
handle_lower = handle.lower()
|
|
|
|
for followed in following_handles_list:
|
|
|
|
if followed.lower() != handle_lower:
|
|
|
|
new_following_handles += followed + '\n'
|
|
|
|
following_handles = new_following_handles
|
|
|
|
|
2024-02-01 13:30:59 +00:00
|
|
|
try:
|
|
|
|
with open(notify_on_post_filename, 'w+',
|
|
|
|
encoding='utf-8') as fp_notify:
|
|
|
|
fp_notify.write(following_handles)
|
|
|
|
except OSError:
|
|
|
|
print('EX: _notify_on_post_arrival unable to write 2' +
|
|
|
|
notify_on_post_filename)
|
2021-07-06 20:38:08 +00:00
|
|
|
else:
|
|
|
|
print(handle + ' not in notifyOnPost.txt')
|
|
|
|
# not already in the notifyOnPost file
|
|
|
|
if add:
|
|
|
|
# append to the list of handles
|
2022-01-03 12:41:06 +00:00
|
|
|
following_handles += handle + '\n'
|
2024-02-01 13:30:59 +00:00
|
|
|
try:
|
|
|
|
with open(notify_on_post_filename, 'w+',
|
|
|
|
encoding='utf-8') as fp_notify:
|
|
|
|
fp_notify.write(following_handles)
|
|
|
|
except OSError:
|
|
|
|
print('EX: _notify_on_post_arrival unable to write 3' +
|
|
|
|
notify_on_post_filename)
|
2021-07-06 20:38:08 +00:00
|
|
|
|
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def add_notify_on_post(base_dir: str, nickname: str, domain: str,
|
2022-01-03 12:41:06 +00:00
|
|
|
following_nickname: str,
|
|
|
|
following_domain: str) -> None:
|
|
|
|
"""Add a notification
|
|
|
|
"""
|
2021-12-29 21:55:09 +00:00
|
|
|
_notify_on_post_arrival(base_dir, nickname, domain,
|
2022-01-03 12:41:06 +00:00
|
|
|
following_nickname, following_domain, True)
|
2021-07-06 20:38:08 +00:00
|
|
|
|
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def remove_notify_on_post(base_dir: str, nickname: str, domain: str,
|
2022-01-03 12:41:06 +00:00
|
|
|
following_nickname: str,
|
|
|
|
following_domain: str) -> None:
|
|
|
|
"""Remove a notification
|
|
|
|
"""
|
2021-12-29 21:55:09 +00:00
|
|
|
_notify_on_post_arrival(base_dir, nickname, domain,
|
2022-01-03 12:41:06 +00:00
|
|
|
following_nickname, following_domain, False)
|
2021-12-29 21:55:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def notify_when_person_posts(base_dir: str, nickname: str, domain: str,
|
2022-01-03 12:41:06 +00:00
|
|
|
following_nickname: str,
|
|
|
|
following_domain: str) -> bool:
|
2021-07-06 20:38:08 +00:00
|
|
|
"""Returns true if receiving notifications when the given publishes a post
|
|
|
|
"""
|
2022-01-03 12:41:06 +00:00
|
|
|
if following_nickname == nickname and following_domain == domain:
|
2021-07-06 20:38:08 +00:00
|
|
|
return False
|
2022-01-03 12:41:06 +00:00
|
|
|
notify_on_post_filename = \
|
2021-12-26 12:02:29 +00:00
|
|
|
acct_dir(base_dir, nickname, domain) + '/notifyOnPost.txt'
|
2022-01-03 12:41:06 +00:00
|
|
|
handle = following_nickname + '@' + following_domain
|
|
|
|
if not os.path.isfile(notify_on_post_filename):
|
2021-07-06 20:38:08 +00:00
|
|
|
# create a new notifyOnPost file
|
2024-02-01 13:30:59 +00:00
|
|
|
try:
|
|
|
|
with open(notify_on_post_filename, 'w+',
|
|
|
|
encoding='utf-8') as fp_notify:
|
|
|
|
fp_notify.write('')
|
|
|
|
except OSError:
|
|
|
|
print('EX: notify_when_person_posts unable to write ' +
|
|
|
|
notify_on_post_filename)
|
2022-12-30 21:07:19 +00:00
|
|
|
return text_in_file(handle + '\n', notify_on_post_filename, False)
|