Snake case

merge-requests/30/head
Bob Mottram 2021-12-26 23:41:34 +00:00
parent 32cae80019
commit 0e87c9a362
21 changed files with 506 additions and 496 deletions

View File

@ -21,7 +21,7 @@ from utils import getDomainFromActor
from utils import locate_post from utils import locate_post
from utils import save_json from utils import save_json
from utils import undoAnnounceCollectionEntry from utils import undoAnnounceCollectionEntry
from utils import updateAnnounceCollection from utils import update_announce_collection
from utils import local_actor_url from utils import local_actor_url
from utils import replace_users_with_at from utils import replace_users_with_at
from utils import has_actor from utils import has_actor
@ -74,11 +74,11 @@ def outboxAnnounce(recent_posts_cache: {},
print('WARN: no nickname found in ' + message_json['actor']) print('WARN: no nickname found in ' + message_json['actor'])
return False return False
domain, _ = getDomainFromActor(message_json['actor']) domain, _ = getDomainFromActor(message_json['actor'])
postFilename = locate_post(base_dir, nickname, domain, post_filename = locate_post(base_dir, nickname, domain,
message_json['object']) message_json['object'])
if postFilename: if post_filename:
updateAnnounceCollection(recent_posts_cache, update_announce_collection(recent_posts_cache,
base_dir, postFilename, base_dir, post_filename,
message_json['actor'], message_json['actor'],
nickname, domain, debug) nickname, domain, debug)
return True return True
@ -93,11 +93,11 @@ def outboxAnnounce(recent_posts_cache: {},
print('WARN: no nickname found in ' + message_json['actor']) print('WARN: no nickname found in ' + message_json['actor'])
return False return False
domain, _ = getDomainFromActor(message_json['actor']) domain, _ = getDomainFromActor(message_json['actor'])
postFilename = locate_post(base_dir, nickname, domain, post_filename = locate_post(base_dir, nickname, domain,
message_json['object']['object']) message_json['object']['object'])
if postFilename: if post_filename:
undoAnnounceCollectionEntry(recent_posts_cache, undoAnnounceCollectionEntry(recent_posts_cache,
base_dir, postFilename, base_dir, post_filename,
message_json['actor'], message_json['actor'],
domain, debug) domain, debug)
return True return True
@ -410,13 +410,13 @@ def outboxUndoAnnounce(recent_posts_cache: {},
messageId = removeIdEnding(message_json['object']['object']) messageId = removeIdEnding(message_json['object']['object'])
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s undo announce post not found in inbox or outbox') print('DEBUG: c2s undo announce post not found in inbox or outbox')
print(messageId) print(messageId)
return True return True
undoAnnounceCollectionEntry(recent_posts_cache, base_dir, postFilename, undoAnnounceCollectionEntry(recent_posts_cache, base_dir, post_filename,
message_json['actor'], domain, debug) message_json['actor'], domain, debug)
if debug: if debug:
print('DEBUG: post undo announce via c2s - ' + postFilename) print('DEBUG: post undo announce via c2s - ' + post_filename)

View File

@ -17,7 +17,7 @@ from utils import has_object_stringType
from utils import remove_domain_port from utils import remove_domain_port
from utils import has_object_dict from utils import has_object_dict
from utils import is_account_dir from utils import is_account_dir
from utils import getCachedPostFilename from utils import get_cached_post_filename
from utils import load_json from utils import load_json
from utils import save_json from utils import save_json
from utils import fileLastModified from utils import fileLastModified
@ -442,8 +442,8 @@ def outboxBlock(base_dir: str, http_prefix: str,
print('DEBUG: c2s block object has no nickname') print('DEBUG: c2s block object has no nickname')
return False return False
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s block post not found in inbox or outbox') print('DEBUG: c2s block post not found in inbox or outbox')
print(messageId) print(messageId)
@ -459,7 +459,7 @@ def outboxBlock(base_dir: str, http_prefix: str,
nicknameBlocked, domainBlockedFull) nicknameBlocked, domainBlockedFull)
if debug: if debug:
print('DEBUG: post blocked via c2s - ' + postFilename) print('DEBUG: post blocked via c2s - ' + post_filename)
return True return True
@ -498,8 +498,8 @@ def outboxUndoBlock(base_dir: str, http_prefix: str,
print('DEBUG: c2s undo block object has no nickname') print('DEBUG: c2s undo block object has no nickname')
return return
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s undo block post not found in inbox or outbox') print('DEBUG: c2s undo block post not found in inbox or outbox')
print(messageId) print(messageId)
@ -516,7 +516,7 @@ def outboxUndoBlock(base_dir: str, http_prefix: str,
removeBlock(base_dir, nickname, domain, removeBlock(base_dir, nickname, domain,
nicknameBlocked, domainBlockedFull) nicknameBlocked, domainBlockedFull)
if debug: if debug:
print('DEBUG: post undo blocked via c2s - ' + postFilename) print('DEBUG: post undo blocked via c2s - ' + post_filename)
def mutePost(base_dir: str, nickname: str, domain: str, port: int, def mutePost(base_dir: str, nickname: str, domain: str, port: int,
@ -525,11 +525,11 @@ def mutePost(base_dir: str, nickname: str, domain: str, port: int,
""" Mutes the given post """ Mutes the given post
""" """
print('mutePost: post_id ' + post_id) print('mutePost: post_id ' + post_id)
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
print('mutePost: file not found ' + post_id) print('mutePost: file not found ' + post_id)
return return
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
print('mutePost: object not loaded ' + post_id) print('mutePost: object not loaded ' + post_id)
return return
@ -581,13 +581,13 @@ def mutePost(base_dir: str, nickname: str, domain: str, port: int,
itemsList.append(newIgnore) itemsList.append(newIgnore)
postJsonObj['ignores']['totalItems'] = igIt postJsonObj['ignores']['totalItems'] = igIt
postJsonObj['muted'] = True postJsonObj['muted'] = True
if save_json(post_json_object, postFilename): if save_json(post_json_object, post_filename):
print('mutePost: saved ' + postFilename) print('mutePost: saved ' + post_filename)
# remove cached post so that the muted version gets recreated # remove cached post so that the muted version gets recreated
# without its content text and/or image # without its content text and/or image
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, post_json_object) get_cached_post_filename(base_dir, nickname, domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
try: try:
@ -601,12 +601,12 @@ def mutePost(base_dir: str, nickname: str, domain: str, port: int,
print('MUTE: cached post not found ' + cachedPostFilename) print('MUTE: cached post not found ' + cachedPostFilename)
try: try:
with open(postFilename + '.muted', 'w+') as muteFile: with open(post_filename + '.muted', 'w+') as muteFile:
muteFile.write('\n') muteFile.write('\n')
except OSError: except OSError:
print('EX: Failed to save mute file ' + postFilename + '.muted') print('EX: Failed to save mute file ' + post_filename + '.muted')
return return
print('MUTE: ' + postFilename + '.muted file added') print('MUTE: ' + post_filename + '.muted file added')
# if the post is in the recent posts cache then mark it as muted # if the post is in the recent posts cache then mark it as muted
if recent_posts_cache.get('index'): if recent_posts_cache.get('index'):
@ -624,12 +624,12 @@ def mutePost(base_dir: str, nickname: str, domain: str, port: int,
print('MUTE: ' + post_id + ' removed cached html') print('MUTE: ' + post_id + ' removed cached html')
if alsoUpdatePostId: if alsoUpdatePostId:
postFilename = locate_post(base_dir, nickname, domain, post_filename = locate_post(base_dir, nickname, domain,
alsoUpdatePostId) alsoUpdatePostId)
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
postJsonObj = load_json(postFilename) postJsonObj = load_json(post_filename)
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, get_cached_post_filename(base_dir, nickname, domain,
postJsonObj) postJsonObj)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -658,14 +658,14 @@ def unmutePost(base_dir: str, nickname: str, domain: str, port: int,
debug: bool) -> None: debug: bool) -> None:
""" Unmutes the given post """ Unmutes the given post
""" """
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
return return
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return return
muteFilename = postFilename + '.muted' muteFilename = post_filename + '.muted'
if os.path.isfile(muteFilename): if os.path.isfile(muteFilename):
try: try:
os.remove(muteFilename) os.remove(muteFilename)
@ -709,12 +709,12 @@ def unmutePost(base_dir: str, nickname: str, domain: str, port: int,
igItLen = len(postJsonObj['ignores']['items']) igItLen = len(postJsonObj['ignores']['items'])
postJsonObj['ignores']['totalItems'] = igItLen postJsonObj['ignores']['totalItems'] = igItLen
postJsonObj['muted'] = False postJsonObj['muted'] = False
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
# remove cached post so that the muted version gets recreated # remove cached post so that the muted version gets recreated
# with its content text and/or image # with its content text and/or image
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, post_json_object) get_cached_post_filename(base_dir, nickname, domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
try: try:
@ -739,12 +739,12 @@ def unmutePost(base_dir: str, nickname: str, domain: str, port: int,
del recent_posts_cache['html'][post_id] del recent_posts_cache['html'][post_id]
print('UNMUTE: ' + post_id + ' removed cached html') print('UNMUTE: ' + post_id + ' removed cached html')
if alsoUpdatePostId: if alsoUpdatePostId:
postFilename = locate_post(base_dir, nickname, domain, post_filename = locate_post(base_dir, nickname, domain,
alsoUpdatePostId) alsoUpdatePostId)
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
postJsonObj = load_json(postFilename) postJsonObj = load_json(post_filename)
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, get_cached_post_filename(base_dir, nickname, domain,
postJsonObj) postJsonObj)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -800,8 +800,8 @@ def outboxMute(base_dir: str, http_prefix: str,
print('DEBUG: c2s mute object has no nickname') print('DEBUG: c2s mute object has no nickname')
return return
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s mute post not found in inbox or outbox') print('DEBUG: c2s mute post not found in inbox or outbox')
print(messageId) print(messageId)
@ -816,7 +816,7 @@ def outboxMute(base_dir: str, http_prefix: str,
debug) debug)
if debug: if debug:
print('DEBUG: post muted via c2s - ' + postFilename) print('DEBUG: post muted via c2s - ' + post_filename)
def outboxUndoMute(base_dir: str, http_prefix: str, def outboxUndoMute(base_dir: str, http_prefix: str,
@ -855,8 +855,8 @@ def outboxUndoMute(base_dir: str, http_prefix: str,
print('DEBUG: c2s undo mute object has no nickname') print('DEBUG: c2s undo mute object has no nickname')
return return
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s undo mute post not found in inbox or outbox') print('DEBUG: c2s undo mute post not found in inbox or outbox')
print(messageId) print(messageId)
@ -872,7 +872,7 @@ def outboxUndoMute(base_dir: str, http_prefix: str,
recent_posts_cache, debug) recent_posts_cache, debug)
if debug: if debug:
print('DEBUG: post undo mute via c2s - ' + postFilename) print('DEBUG: post undo mute via c2s - ' + post_filename)
def broch_modeIsActive(base_dir: str) -> bool: def broch_modeIsActive(base_dir: str) -> bool:

54
blog.py
View File

@ -53,19 +53,19 @@ def _noOfBlogReplies(base_dir: str, http_prefix: str, translate: {},
tryPostBox = ('tlblogs', 'inbox', 'outbox') tryPostBox = ('tlblogs', 'inbox', 'outbox')
boxFound = False boxFound = False
for postBox in tryPostBox: for postBox in tryPostBox:
postFilename = \ post_filename = \
acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \ acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \
post_id.replace('/', '#') + '.replies' post_id.replace('/', '#') + '.replies'
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
boxFound = True boxFound = True
break break
if not boxFound: if not boxFound:
# post may exist but has no replies # post may exist but has no replies
for postBox in tryPostBox: for postBox in tryPostBox:
postFilename = \ post_filename = \
acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \ acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \
post_id.replace('/', '#') post_id.replace('/', '#')
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
return 1 return 1
return 0 return 0
@ -73,10 +73,10 @@ def _noOfBlogReplies(base_dir: str, http_prefix: str, translate: {},
replies = 0 replies = 0
lines = [] lines = []
try: try:
with open(postFilename, 'r') as f: with open(post_filename, 'r') as f:
lines = f.readlines() lines = f.readlines()
except OSError: except OSError:
print('EX: failed to read blog ' + postFilename) print('EX: failed to read blog ' + post_filename)
for replyPostId in lines: for replyPostId in lines:
replyPostId = replyPostId.replace('\n', '').replace('\r', '') replyPostId = replyPostId.replace('\n', '').replace('\r', '')
@ -93,10 +93,10 @@ def _noOfBlogReplies(base_dir: str, http_prefix: str, translate: {},
# remove posts from .replies file if they don't exist # remove posts from .replies file if they don't exist
if lines and removals: if lines and removals:
print('Rewriting ' + postFilename + ' to remove ' + print('Rewriting ' + post_filename + ' to remove ' +
str(len(removals)) + ' entries') str(len(removals)) + ' entries')
try: try:
with open(postFilename, 'w+') as f: with open(post_filename, 'w+') as f:
for replyPostId in lines: for replyPostId in lines:
replyPostId = \ replyPostId = \
replyPostId.replace('\n', '').replace('\r', '') replyPostId.replace('\n', '').replace('\r', '')
@ -104,7 +104,7 @@ def _noOfBlogReplies(base_dir: str, http_prefix: str, translate: {},
f.write(replyPostId + '\n') f.write(replyPostId + '\n')
except OSError as ex: except OSError as ex:
print('EX: unable to remove replies from post ' + print('EX: unable to remove replies from post ' +
postFilename + ' ' + str(ex)) post_filename + ' ' + str(ex))
return replies return replies
@ -122,36 +122,36 @@ def _getBlogReplies(base_dir: str, http_prefix: str, translate: {},
tryPostBox = ('tlblogs', 'inbox', 'outbox') tryPostBox = ('tlblogs', 'inbox', 'outbox')
boxFound = False boxFound = False
for postBox in tryPostBox: for postBox in tryPostBox:
postFilename = \ post_filename = \
acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \ acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \
post_id.replace('/', '#') + '.replies' post_id.replace('/', '#') + '.replies'
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
boxFound = True boxFound = True
break break
if not boxFound: if not boxFound:
# post may exist but has no replies # post may exist but has no replies
for postBox in tryPostBox: for postBox in tryPostBox:
postFilename = \ post_filename = \
acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \ acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \
post_id.replace('/', '#') + '.json' post_id.replace('/', '#') + '.json'
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
postFilename = acct_dir(base_dir, nickname, domain) + \ post_filename = acct_dir(base_dir, nickname, domain) + \
'/postcache/' + \ '/postcache/' + \
post_id.replace('/', '#') + '.html' post_id.replace('/', '#') + '.html'
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
try: try:
with open(postFilename, 'r') as postFile: with open(post_filename, 'r') as postFile:
return postFile.read() + '\n' return postFile.read() + '\n'
except OSError: except OSError:
print('EX: unable to read blog 3 ' + postFilename) print('EX: unable to read blog 3 ' + post_filename)
return '' return ''
lines = [] lines = []
try: try:
with open(postFilename, 'r') as f: with open(post_filename, 'r') as f:
lines = f.readlines() lines = f.readlines()
except OSError: except OSError:
print('EX: unable to read blog 4 ' + postFilename) print('EX: unable to read blog 4 ' + post_filename)
if lines: if lines:
repliesStr = '' repliesStr = ''
@ -159,16 +159,16 @@ def _getBlogReplies(base_dir: str, http_prefix: str, translate: {},
replyPostId = replyPostId.replace('\n', '').replace('\r', '') replyPostId = replyPostId.replace('\n', '').replace('\r', '')
replyPostId = replyPostId.replace('.json', '') replyPostId = replyPostId.replace('.json', '')
replyPostId = replyPostId.replace('.replies', '') replyPostId = replyPostId.replace('.replies', '')
postFilename = acct_dir(base_dir, nickname, domain) + \ post_filename = acct_dir(base_dir, nickname, domain) + \
'/postcache/' + \ '/postcache/' + \
replyPostId.replace('/', '#') + '.html' replyPostId.replace('/', '#') + '.html'
if not os.path.isfile(postFilename): if not os.path.isfile(post_filename):
continue continue
try: try:
with open(postFilename, 'r') as postFile: with open(post_filename, 'r') as postFile:
repliesStr += postFile.read() + '\n' repliesStr += postFile.read() + '\n'
except OSError: except OSError:
print('EX: unable to read blog replies ' + postFilename) print('EX: unable to read blog replies ' + post_filename)
rply = _getBlogReplies(base_dir, http_prefix, translate, rply = _getBlogReplies(base_dir, http_prefix, translate,
nickname, domain, domain_full, nickname, domain, domain_full,
replyPostId, depth+1) replyPostId, depth+1)
@ -780,14 +780,14 @@ def htmlEditBlog(media_instance: bool, translate: {},
postUrl: str, system_language: str) -> str: postUrl: str, system_language: str) -> str:
"""Edit a blog post after it was created """Edit a blog post after it was created
""" """
postFilename = locate_post(base_dir, nickname, domain, postUrl) post_filename = locate_post(base_dir, nickname, domain, postUrl)
if not postFilename: if not post_filename:
print('Edit blog: Filename not found for ' + postUrl) print('Edit blog: Filename not found for ' + postUrl)
return None return None
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
print('Edit blog: json not loaded for ' + postFilename) print('Edit blog: json not loaded for ' + post_filename)
return None return None
editBlogText = '<h1">' + translate['Write your post text below.'] + '</h1>' editBlogText = '<h1">' + translate['Write your post text below.'] + '</h1>'

View File

@ -20,7 +20,7 @@ from utils import urlPermitted
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import locate_post from utils import locate_post
from utils import getCachedPostFilename from utils import get_cached_post_filename
from utils import load_json from utils import load_json
from utils import save_json from utils import save_json
from utils import has_object_dict from utils import has_object_dict
@ -33,19 +33,20 @@ from session import postJson
def undoBookmarksCollectionEntry(recent_posts_cache: {}, def undoBookmarksCollectionEntry(recent_posts_cache: {},
base_dir: str, postFilename: str, base_dir: str, post_filename: str,
objectUrl: str, objectUrl: str,
actor: str, domain: str, debug: bool) -> None: actor: str, domain: str, debug: bool) -> None:
"""Undoes a bookmark for a particular actor """Undoes a bookmark for a particular actor
""" """
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return return
# remove any cached version of this post so that the # remove any cached version of this post so that the
# bookmark icon is changed # bookmark icon is changed
nickname = getNicknameFromActor(actor) nickname = getNicknameFromActor(actor)
cachedPostFilename = getCachedPostFilename(base_dir, nickname, cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object) domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -63,10 +64,10 @@ def undoBookmarksCollectionEntry(recent_posts_cache: {},
acct_dir(base_dir, nickname, domain) + '/bookmarks.index' acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
if not os.path.isfile(bookmarksIndexFilename): if not os.path.isfile(bookmarksIndexFilename):
return return
if '/' in postFilename: if '/' in post_filename:
bookmarkIndex = postFilename.split('/')[-1].strip() bookmarkIndex = post_filename.split('/')[-1].strip()
else: else:
bookmarkIndex = postFilename.strip() bookmarkIndex = post_filename.strip()
bookmarkIndex = bookmarkIndex.replace('\n', '').replace('\r', '') bookmarkIndex = bookmarkIndex.replace('\n', '').replace('\r', '')
if bookmarkIndex not in open(bookmarksIndexFilename).read(): if bookmarkIndex not in open(bookmarksIndexFilename).read():
return return
@ -122,7 +123,7 @@ def undoBookmarksCollectionEntry(recent_posts_cache: {},
else: else:
bmItLen = len(post_json_object['object']['bookmarks']['items']) bmItLen = len(post_json_object['object']['bookmarks']['items'])
post_json_object['object']['bookmarks']['totalItems'] = bmItLen post_json_object['object']['bookmarks']['totalItems'] = bmItLen
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
def bookmarkedByPerson(post_json_object: {}, def bookmarkedByPerson(post_json_object: {},
@ -154,17 +155,18 @@ def _noOfBookmarks(post_json_object: {}) -> int:
def updateBookmarksCollection(recent_posts_cache: {}, def updateBookmarksCollection(recent_posts_cache: {},
base_dir: str, postFilename: str, base_dir: str, post_filename: str,
objectUrl: str, objectUrl: str,
actor: str, domain: str, debug: bool) -> None: actor: str, domain: str, debug: bool) -> None:
"""Updates the bookmarks collection within a post """Updates the bookmarks collection within a post
""" """
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if post_json_object: if post_json_object:
# remove any cached version of this post so that the # remove any cached version of this post so that the
# bookmark icon is changed # bookmark icon is changed
nickname = getNicknameFromActor(actor) nickname = getNicknameFromActor(actor)
cachedPostFilename = getCachedPostFilename(base_dir, nickname, cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object) domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -220,12 +222,12 @@ def updateBookmarksCollection(recent_posts_cache: {},
print('DEBUG: saving post with bookmarks added') print('DEBUG: saving post with bookmarks added')
pprint(post_json_object) pprint(post_json_object)
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
# prepend to the index # prepend to the index
bookmarksIndexFilename = \ bookmarksIndexFilename = \
acct_dir(base_dir, nickname, domain) + '/bookmarks.index' acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
bookmarkIndex = postFilename.split('/')[-1] bookmarkIndex = post_filename.split('/')[-1]
if os.path.isfile(bookmarksIndexFilename): if os.path.isfile(bookmarksIndexFilename):
if bookmarkIndex not in open(bookmarksIndexFilename).read(): if bookmarkIndex not in open(bookmarksIndexFilename).read():
try: try:
@ -292,8 +294,8 @@ def bookmark(recent_posts_cache: {},
bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(ou) bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(ou)
if bookmarkedPostNickname: if bookmarkedPostNickname:
postFilename = locate_post(base_dir, nickname, domain, objectUrl) post_filename = locate_post(base_dir, nickname, domain, objectUrl)
if not postFilename: if not post_filename:
print('DEBUG: bookmark base_dir: ' + base_dir) print('DEBUG: bookmark base_dir: ' + base_dir)
print('DEBUG: bookmark nickname: ' + nickname) print('DEBUG: bookmark nickname: ' + nickname)
print('DEBUG: bookmark domain: ' + domain) print('DEBUG: bookmark domain: ' + domain)
@ -301,7 +303,7 @@ def bookmark(recent_posts_cache: {},
return None return None
updateBookmarksCollection(recent_posts_cache, updateBookmarksCollection(recent_posts_cache,
base_dir, postFilename, objectUrl, base_dir, post_filename, objectUrl,
newBookmarkJson['actor'], domain, debug) newBookmarkJson['actor'], domain, debug)
return newBookmarkJson return newBookmarkJson
@ -356,12 +358,12 @@ def undoBookmark(recent_posts_cache: {},
bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(ou) bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(ou)
if bookmarkedPostNickname: if bookmarkedPostNickname:
postFilename = locate_post(base_dir, nickname, domain, objectUrl) post_filename = locate_post(base_dir, nickname, domain, objectUrl)
if not postFilename: if not post_filename:
return None return None
undoBookmarksCollectionEntry(recent_posts_cache, undoBookmarksCollectionEntry(recent_posts_cache,
base_dir, postFilename, objectUrl, base_dir, post_filename, objectUrl,
newUndoBookmarkJson['actor'], newUndoBookmarkJson['actor'],
domain, debug) domain, debug)
else: else:
@ -593,17 +595,17 @@ def outboxBookmark(recent_posts_cache: {},
messageUrl = removeIdEnding(message_json['object']['url']) messageUrl = removeIdEnding(message_json['object']['url'])
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageUrl) post_filename = locate_post(base_dir, nickname, domain, messageUrl)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s like post not found in inbox or outbox') print('DEBUG: c2s like post not found in inbox or outbox')
print(messageUrl) print(messageUrl)
return True return True
updateBookmarksCollection(recent_posts_cache, updateBookmarksCollection(recent_posts_cache,
base_dir, postFilename, messageUrl, base_dir, post_filename, messageUrl,
message_json['actor'], domain, debug) message_json['actor'], domain, debug)
if debug: if debug:
print('DEBUG: post bookmarked via c2s - ' + postFilename) print('DEBUG: post bookmarked via c2s - ' + post_filename)
def outboxUndoBookmark(recent_posts_cache: {}, def outboxUndoBookmark(recent_posts_cache: {},
@ -649,14 +651,14 @@ def outboxUndoBookmark(recent_posts_cache: {},
messageUrl = removeIdEnding(message_json['object']['url']) messageUrl = removeIdEnding(message_json['object']['url'])
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageUrl) post_filename = locate_post(base_dir, nickname, domain, messageUrl)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s unbookmark post not found in inbox or outbox') print('DEBUG: c2s unbookmark post not found in inbox or outbox')
print(messageUrl) print(messageUrl)
return True return True
updateBookmarksCollection(recent_posts_cache, updateBookmarksCollection(recent_posts_cache,
base_dir, postFilename, messageUrl, base_dir, post_filename, messageUrl,
message_json['actor'], domain, debug) message_json['actor'], domain, debug)
if debug: if debug:
print('DEBUG: post unbookmarked via c2s - ' + postFilename) print('DEBUG: post unbookmarked via c2s - ' + post_filename)

View File

@ -291,7 +291,7 @@ from utils import deletePost
from utils import isBlogPost from utils import isBlogPost
from utils import removeAvatarFromCache from utils import removeAvatarFromCache
from utils import locate_post from utils import locate_post
from utils import getCachedPostFilename from utils import get_cached_post_filename
from utils import removePostFromCache from utils import removePostFromCache
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
@ -524,11 +524,11 @@ class PubServer(BaseHTTPRequestHandler):
message_json['object']['name'] = answer message_json['object']['name'] = answer
if self._postToOutbox(message_json, if self._postToOutbox(message_json,
self.server.project_version, nickname): self.server.project_version, nickname):
postFilename = \ post_filename = \
locate_post(self.server.base_dir, nickname, locate_post(self.server.base_dir, nickname,
self.server.domain, messageId) self.server.domain, messageId)
if postFilename: if post_filename:
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if post_json_object: if post_json_object:
populateReplies(self.server.base_dir, populateReplies(self.server.base_dir,
self.server.http_prefix, self.server.http_prefix,
@ -547,7 +547,7 @@ class PubServer(BaseHTTPRequestHandler):
# ensure that the cached post is removed if it exists, # ensure that the cached post is removed if it exists,
# so that it then will be recreated # so that it then will be recreated
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(self.server.base_dir, get_cached_post_filename(self.server.base_dir,
nickname, nickname,
self.server.domain, self.server.domain,
post_json_object) post_json_object)
@ -2005,33 +2005,33 @@ class PubServer(BaseHTTPRequestHandler):
removeAccount(base_dir, nickname, domain, port) removeAccount(base_dir, nickname, domain, port)
else: else:
# remove a post or thread # remove a post or thread
postFilename = \ post_filename = \
locate_post(base_dir, nickname, domain, locate_post(base_dir, nickname, domain,
moderationText) moderationText)
if postFilename: if post_filename:
if canRemovePost(base_dir, if canRemovePost(base_dir,
nickname, domain, port, nickname, domain, port,
moderationText): moderationText):
deletePost(base_dir, deletePost(base_dir,
http_prefix, http_prefix,
nickname, domain, nickname, domain,
postFilename, post_filename,
debug, debug,
self.server.recent_posts_cache) self.server.recent_posts_cache)
if nickname != 'news': if nickname != 'news':
# if this is a local blog post then also remove it # if this is a local blog post then also remove it
# from the news actor # from the news actor
postFilename = \ post_filename = \
locate_post(base_dir, 'news', domain, locate_post(base_dir, 'news', domain,
moderationText) moderationText)
if postFilename: if post_filename:
if canRemovePost(base_dir, if canRemovePost(base_dir,
'news', domain, port, 'news', domain, port,
moderationText): moderationText):
deletePost(base_dir, deletePost(base_dir,
http_prefix, http_prefix,
'news', domain, 'news', domain,
postFilename, post_filename,
debug, debug,
self.server.recent_posts_cache) self.server.recent_posts_cache)
@ -4627,11 +4627,11 @@ class PubServer(BaseHTTPRequestHandler):
if newsPostUrl and newsPostContent and newsPostTitle: if newsPostUrl and newsPostContent and newsPostTitle:
# load the post # load the post
postFilename = \ post_filename = \
locate_post(base_dir, nickname, domain, locate_post(base_dir, nickname, domain,
newsPostUrl) newsPostUrl)
if postFilename: if post_filename:
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
# update the content and title # update the content and title
post_json_object['object']['summary'] = \ post_json_object['object']['summary'] = \
newsPostTitle newsPostTitle
@ -4666,7 +4666,7 @@ class PubServer(BaseHTTPRequestHandler):
newsId) newsId)
# save the news post # save the news post
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
# redirect back to the default timeline # redirect back to the default timeline
if self.server.news_instance: if self.server.news_instance:
@ -7739,7 +7739,7 @@ class PubServer(BaseHTTPRequestHandler):
if debug: if debug:
print('Generating html post for announce') print('Generating html post for announce')
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, self.postToNickname, get_cached_post_filename(base_dir, self.postToNickname,
domain, announceJson) domain, announceJson)
if debug: if debug:
print('Announced post json: ' + str(announceJson)) print('Announced post json: ' + str(announceJson))
@ -7867,15 +7867,15 @@ class PubServer(BaseHTTPRequestHandler):
announceUrl = path.split('?unannounce=')[1] announceUrl = path.split('?unannounce=')[1]
if '?' in announceUrl: if '?' in announceUrl:
announceUrl = announceUrl.split('?')[0] announceUrl = announceUrl.split('?')[0]
postFilename = None post_filename = None
nickname = getNicknameFromActor(announceUrl) nickname = getNicknameFromActor(announceUrl)
if nickname: if nickname:
if domain_full + '/users/' + nickname + '/' in announceUrl: if domain_full + '/users/' + nickname + '/' in announceUrl:
postFilename = \ post_filename = \
locate_post(base_dir, nickname, domain, announceUrl) locate_post(base_dir, nickname, domain, announceUrl)
if postFilename: if post_filename:
deletePost(base_dir, http_prefix, deletePost(base_dir, http_prefix,
nickname, domain, postFilename, nickname, domain, post_filename,
debug, recent_posts_cache) debug, recent_posts_cache)
self._postToOutbox(newUndoAnnounce, self._postToOutbox(newUndoAnnounce,
@ -8200,7 +8200,7 @@ class PubServer(BaseHTTPRequestHandler):
# clear the icon from the cache so that it gets updated # clear the icon from the cache so that it gets updated
if likedPostJson: if likedPostJson:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, self.postToNickname, get_cached_post_filename(base_dir, self.postToNickname,
domain, likedPostJson) domain, likedPostJson)
if debug: if debug:
print('Liked post json: ' + str(likedPostJson)) print('Liked post json: ' + str(likedPostJson))
@ -8538,7 +8538,7 @@ class PubServer(BaseHTTPRequestHandler):
# clear the icon from the cache so that it gets updated # clear the icon from the cache so that it gets updated
if reactionPostJson: if reactionPostJson:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, self.postToNickname, get_cached_post_filename(base_dir, self.postToNickname,
domain, reactionPostJson) domain, reactionPostJson)
if debug: if debug:
print('Reaction post json: ' + str(reactionPostJson)) print('Reaction post json: ' + str(reactionPostJson))
@ -8932,7 +8932,7 @@ class PubServer(BaseHTTPRequestHandler):
bookmarkPostJson = load_json(bookmarkFilename, 0, 1) bookmarkPostJson = load_json(bookmarkFilename, 0, 1)
if bookmarkPostJson: if bookmarkPostJson:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, self.postToNickname, get_cached_post_filename(base_dir, self.postToNickname,
domain, bookmarkPostJson) domain, bookmarkPostJson)
print('Bookmarked post json: ' + str(bookmarkPostJson)) print('Bookmarked post json: ' + str(bookmarkPostJson))
print('Bookmarked post nickname: ' + print('Bookmarked post nickname: ' +
@ -9060,7 +9060,7 @@ class PubServer(BaseHTTPRequestHandler):
bookmarkPostJson = load_json(bookmarkFilename, 0, 1) bookmarkPostJson = load_json(bookmarkFilename, 0, 1)
if bookmarkPostJson: if bookmarkPostJson:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, self.postToNickname, get_cached_post_filename(base_dir, self.postToNickname,
domain, bookmarkPostJson) domain, bookmarkPostJson)
print('Unbookmarked post json: ' + str(bookmarkPostJson)) print('Unbookmarked post json: ' + str(bookmarkPostJson))
print('Unbookmarked post nickname: ' + print('Unbookmarked post nickname: ' +
@ -9255,7 +9255,7 @@ class PubServer(BaseHTTPRequestHandler):
mutePostJson = load_json(muteFilename, 0, 1) mutePostJson = load_json(muteFilename, 0, 1)
if mutePostJson: if mutePostJson:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, get_cached_post_filename(base_dir, nickname,
domain, mutePostJson) domain, mutePostJson)
print('mutePost: Muted post json: ' + str(mutePostJson)) print('mutePost: Muted post json: ' + str(mutePostJson))
print('mutePost: Muted post nickname: ' + print('mutePost: Muted post nickname: ' +
@ -9365,7 +9365,7 @@ class PubServer(BaseHTTPRequestHandler):
mutePostJson = load_json(muteFilename, 0, 1) mutePostJson = load_json(muteFilename, 0, 1)
if mutePostJson: if mutePostJson:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, get_cached_post_filename(base_dir, nickname,
domain, mutePostJson) domain, mutePostJson)
print('unmutePost: Unmuted post json: ' + str(mutePostJson)) print('unmutePost: Unmuted post json: ' + str(mutePostJson))
print('unmutePost: Unmuted post nickname: ' + print('unmutePost: Unmuted post nickname: ' +
@ -9930,7 +9930,7 @@ class PubServer(BaseHTTPRequestHandler):
if len(statusNumber) <= 10 or not statusNumber.isdigit(): if len(statusNumber) <= 10 or not statusNumber.isdigit():
return False return False
postFilename = \ post_filename = \
acct_dir(base_dir, nickname, domain) + '/outbox/' + \ acct_dir(base_dir, nickname, domain) + '/outbox/' + \
http_prefix + ':##' + domain_full + '#users#' + nickname + \ http_prefix + ':##' + domain_full + '#users#' + nickname + \
'#statuses#' + statusNumber + '.json' '#statuses#' + statusNumber + '.json'
@ -9939,7 +9939,7 @@ class PubServer(BaseHTTPRequestHandler):
if postSections[-1] == 'activity': if postSections[-1] == 'activity':
includeCreateWrapper = True includeCreateWrapper = True
result = self._showPostFromFile(postFilename, likedBy, result = self._showPostFromFile(post_filename, likedBy,
reactBy, reactEmoji, reactBy, reactEmoji,
authorized, calling_domain, path, authorized, calling_domain, path,
base_dir, http_prefix, nickname, base_dir, http_prefix, nickname,
@ -9953,7 +9953,7 @@ class PubServer(BaseHTTPRequestHandler):
self.server.debug) self.server.debug)
return result return result
def _showPostFromFile(self, postFilename: str, likedBy: str, def _showPostFromFile(self, post_filename: str, likedBy: str,
reactBy: str, reactEmoji: str, reactBy: str, reactEmoji: str,
authorized: bool, authorized: bool,
calling_domain: str, path: str, calling_domain: str, path: str,
@ -9965,12 +9965,12 @@ class PubServer(BaseHTTPRequestHandler):
debug: str, includeCreateWrapper: bool) -> bool: debug: str, includeCreateWrapper: bool) -> bool:
"""Shows an individual post from its filename """Shows an individual post from its filename
""" """
if not os.path.isfile(postFilename): if not os.path.isfile(post_filename):
self._404() self._404()
self.server.GETbusy = False self.server.GETbusy = False
return True return True
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
self.send_response(429) self.send_response(429)
self.end_headers() self.end_headers()
@ -10088,7 +10088,7 @@ class PubServer(BaseHTTPRequestHandler):
if len(statusNumber) <= 10 or (not statusNumber.isdigit()): if len(statusNumber) <= 10 or (not statusNumber.isdigit()):
return False return False
postFilename = \ post_filename = \
acct_dir(base_dir, nickname, domain) + '/outbox/' + \ acct_dir(base_dir, nickname, domain) + '/outbox/' + \
http_prefix + ':##' + domain_full + '#users#' + nickname + \ http_prefix + ':##' + domain_full + '#users#' + nickname + \
'#statuses#' + statusNumber + '.json' '#statuses#' + statusNumber + '.json'
@ -10097,7 +10097,7 @@ class PubServer(BaseHTTPRequestHandler):
if postSections[-1] == 'activity': if postSections[-1] == 'activity':
includeCreateWrapper = True includeCreateWrapper = True
result = self._showPostFromFile(postFilename, likedBy, result = self._showPostFromFile(post_filename, likedBy,
reactBy, reactEmoji, reactBy, reactEmoji,
authorized, calling_domain, path, authorized, calling_domain, path,
base_dir, http_prefix, nickname, base_dir, http_prefix, nickname,
@ -10133,16 +10133,16 @@ class PubServer(BaseHTTPRequestHandler):
return False return False
replies = False replies = False
postFilename = locate_post(base_dir, nickname, domain, post_filename = locate_post(base_dir, nickname, domain,
post_id, replies) post_id, replies)
if not postFilename: if not post_filename:
return False return False
includeCreateWrapper = False includeCreateWrapper = False
if path.endswith('/activity'): if path.endswith('/activity'):
includeCreateWrapper = True includeCreateWrapper = True
result = self._showPostFromFile(postFilename, likedBy, result = self._showPostFromFile(post_filename, likedBy,
reactBy, reactEmoji, reactBy, reactEmoji,
authorized, calling_domain, path, authorized, calling_domain, path,
base_dir, http_prefix, nickname, base_dir, http_prefix, nickname,
@ -16749,12 +16749,12 @@ class PubServer(BaseHTTPRequestHandler):
return -1 return -1
elif postType == 'editblogpost': elif postType == 'editblogpost':
print('Edited blog post received') print('Edited blog post received')
postFilename = \ post_filename = \
locate_post(self.server.base_dir, locate_post(self.server.base_dir,
nickname, self.server.domain, nickname, self.server.domain,
fields['postUrl']) fields['postUrl'])
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if post_json_object: if post_json_object:
cachedFilename = \ cachedFilename = \
acct_dir(self.server.base_dir, acct_dir(self.server.base_dir,
@ -16832,19 +16832,19 @@ class PubServer(BaseHTTPRequestHandler):
replaceTwitter(post_json_object, replaceTwitter(post_json_object,
self.server.twitter_replacement_domain, self.server.twitter_replacement_domain,
self.server.system_language) self.server.system_language)
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
# also save to the news actor # also save to the news actor
if nickname != 'news': if nickname != 'news':
postFilename = \ post_filename = \
postFilename.replace('#users#' + post_filename.replace('#users#' +
nickname + '#', nickname + '#',
'#users#news#') '#users#news#')
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
print('Edited blog post, resaved ' + postFilename) print('Edited blog post, resaved ' + post_filename)
return 1 return 1
else: else:
print('Edited blog post, unable to load json for ' + print('Edited blog post, unable to load json for ' +
postFilename) post_filename)
else: else:
print('Edited blog post not found ' + print('Edited blog post not found ' +
str(fields['postUrl'])) str(fields['postUrl']))

View File

@ -161,17 +161,17 @@ def outboxDelete(base_dir: str, http_prefix: str,
"wasn't created by you (domain does not match)") "wasn't created by you (domain does not match)")
return return
removeModerationPostFromIndex(base_dir, messageId, debug) removeModerationPostFromIndex(base_dir, messageId, debug)
postFilename = locate_post(base_dir, deleteNickname, deleteDomain, post_filename = locate_post(base_dir, deleteNickname, deleteDomain,
messageId) messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s delete post not found in inbox or outbox') print('DEBUG: c2s delete post not found in inbox or outbox')
print(messageId) print(messageId)
return True return True
deletePost(base_dir, http_prefix, deleteNickname, deleteDomain, deletePost(base_dir, http_prefix, deleteNickname, deleteDomain,
postFilename, debug, recent_posts_cache) post_filename, debug, recent_posts_cache)
if debug: if debug:
print('DEBUG: post deleted via c2s - ' + postFilename) print('DEBUG: post deleted via c2s - ' + post_filename)
def removeOldHashtags(base_dir: str, maxMonths: int) -> str: def removeOldHashtags(base_dir: str, maxMonths: int) -> str:

View File

@ -205,12 +205,12 @@ def getTodaysEvents(base_dir: str, nickname: str, domain: str,
with open(calendarFilename, 'r') as eventsFile: with open(calendarFilename, 'r') as eventsFile:
for post_id in eventsFile: for post_id in eventsFile:
post_id = post_id.replace('\n', '').replace('\r', '') post_id = post_id.replace('\n', '').replace('\r', '')
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
recreateEventsFile = True recreateEventsFile = True
continue continue
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not _isHappeningPost(post_json_object): if not _isHappeningPost(post_json_object):
continue continue
@ -280,11 +280,11 @@ def dayEventsCheck(base_dir: str, nickname: str, domain: str,
with open(calendarFilename, 'r') as eventsFile: with open(calendarFilename, 'r') as eventsFile:
for post_id in eventsFile: for post_id in eventsFile:
post_id = post_id.replace('\n', '').replace('\r', '') post_id = post_id.replace('\n', '').replace('\r', '')
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
continue continue
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not _isHappeningPost(post_json_object): if not _isHappeningPost(post_json_object):
continue continue
@ -336,12 +336,12 @@ def getThisWeeksEvents(base_dir: str, nickname: str, domain: str) -> {}:
with open(calendarFilename, 'r') as eventsFile: with open(calendarFilename, 'r') as eventsFile:
for post_id in eventsFile: for post_id in eventsFile:
post_id = post_id.replace('\n', '').replace('\r', '') post_id = post_id.replace('\n', '').replace('\r', '')
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
recreateEventsFile = True recreateEventsFile = True
continue continue
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not _isHappeningPost(post_json_object): if not _isHappeningPost(post_json_object):
continue continue
@ -401,12 +401,12 @@ def getCalendarEvents(base_dir: str, nickname: str, domain: str,
with open(calendarFilename, 'r') as eventsFile: with open(calendarFilename, 'r') as eventsFile:
for post_id in eventsFile: for post_id in eventsFile:
post_id = post_id.replace('\n', '').replace('\r', '') post_id = post_id.replace('\n', '').replace('\r', '')
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
recreateEventsFile = True recreateEventsFile = True
continue continue
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not _isHappeningPost(post_json_object): if not _isHappeningPost(post_json_object):
continue continue

165
inbox.py
View File

@ -44,7 +44,7 @@ from utils import getProtocolPrefixes
from utils import isBlogPost from utils import isBlogPost
from utils import removeAvatarFromCache from utils import removeAvatarFromCache
from utils import isPublicPost from utils import isPublicPost
from utils import getCachedPostFilename from utils import get_cached_post_filename
from utils import removePostFromCache from utils import removePostFromCache
from utils import urlPermitted from utils import urlPermitted
from utils import createInboxQueueDir from utils import createInboxQueueDir
@ -86,7 +86,7 @@ from blocking import isBlocked
from blocking import isBlockedDomain from blocking import isBlockedDomain
from blocking import broch_modeLapses from blocking import broch_modeLapses
from filters import isFiltered from filters import isFiltered
from utils import updateAnnounceCollection from utils import update_announce_collection
from utils import undoAnnounceCollectionEntry from utils import undoAnnounceCollectionEntry
from utils import dangerousMarkup from utils import dangerousMarkup
from utils import is_dm from utils import is_dm
@ -913,11 +913,11 @@ def _receiveUpdateToQuestion(recent_posts_cache: {}, message_json: {},
if '#' in messageId: if '#' in messageId:
messageId = messageId.split('#', 1)[0] messageId = messageId.split('#', 1)[0]
# find the question post # find the question post
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
return return
# load the json for the question # load the json for the question
post_json_object = load_json(postFilename, 1) post_json_object = load_json(post_filename, 1)
if not post_json_object: if not post_json_object:
return return
if not post_json_object.get('actor'): if not post_json_object.get('actor'):
@ -925,11 +925,11 @@ def _receiveUpdateToQuestion(recent_posts_cache: {}, message_json: {},
# does the actor match? # does the actor match?
if post_json_object['actor'] != message_json['actor']: if post_json_object['actor'] != message_json['actor']:
return return
save_json(message_json, postFilename) save_json(message_json, post_filename)
# ensure that the cached post is removed if it exists, so # ensure that the cached post is removed if it exists, so
# that it then will be recreated # that it then will be recreated
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, message_json) get_cached_post_filename(base_dir, nickname, domain, message_json)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
try: try:
@ -1038,8 +1038,8 @@ def _receiveLike(recent_posts_cache: {},
handleName = handle.split('@')[0] handleName = handle.split('@')[0]
handleDom = handle.split('@')[1] handleDom = handle.split('@')[1]
postLikedId = message_json['object'] postLikedId = message_json['object']
postFilename = locate_post(base_dir, handleName, handleDom, postLikedId) post_filename = locate_post(base_dir, handleName, handleDom, postLikedId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: post not found in inbox or outbox') print('DEBUG: post not found in inbox or outbox')
print(postLikedId) print(postLikedId)
@ -1056,11 +1056,11 @@ def _receiveLike(recent_posts_cache: {},
likeActor): likeActor):
_likeNotify(base_dir, domain, onion_domain, handle, _likeNotify(base_dir, domain, onion_domain, handle,
likeActor, postLikedId) likeActor, postLikedId)
updateLikesCollection(recent_posts_cache, base_dir, postFilename, updateLikesCollection(recent_posts_cache, base_dir, post_filename,
postLikedId, likeActor, postLikedId, likeActor,
handleName, domain, debug, None) handleName, domain, debug, None)
# regenerate the html # regenerate the html
likedPostJson = load_json(postFilename, 0, 1) likedPostJson = load_json(post_filename, 0, 1)
if likedPostJson: if likedPostJson:
if likedPostJson.get('type'): if likedPostJson.get('type'):
if likedPostJson['type'] == 'Announce' and \ if likedPostJson['type'] == 'Announce' and \
@ -1072,10 +1072,10 @@ def _receiveLike(recent_posts_cache: {},
domain, announceLikeUrl) domain, announceLikeUrl)
if announceLikedFilename: if announceLikedFilename:
postLikedId = announceLikeUrl postLikedId = announceLikeUrl
postFilename = announceLikedFilename post_filename = announceLikedFilename
updateLikesCollection(recent_posts_cache, updateLikesCollection(recent_posts_cache,
base_dir, base_dir,
postFilename, post_filename,
postLikedId, postLikedId,
likeActor, likeActor,
handleName, handleName,
@ -1083,7 +1083,7 @@ def _receiveLike(recent_posts_cache: {},
if likedPostJson: if likedPostJson:
if debug: if debug:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, handleName, domain, get_cached_post_filename(base_dir, handleName, domain,
likedPostJson) likedPostJson)
print('Liked post json: ' + str(likedPostJson)) print('Liked post json: ' + str(likedPostJson))
print('Liked post nickname: ' + handleName + ' ' + domain) print('Liked post nickname: ' + handleName + ' ' + domain)
@ -1159,10 +1159,10 @@ def _receiveUndoLike(recent_posts_cache: {},
# if this post in the outbox of the person? # if this post in the outbox of the person?
handleName = handle.split('@')[0] handleName = handle.split('@')[0]
handleDom = handle.split('@')[1] handleDom = handle.split('@')[1]
postFilename = \ post_filename = \
locate_post(base_dir, handleName, handleDom, locate_post(base_dir, handleName, handleDom,
message_json['object']['object']) message_json['object']['object'])
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: unliked post not found in inbox or outbox') print('DEBUG: unliked post not found in inbox or outbox')
print(message_json['object']['object']) print(message_json['object']['object'])
@ -1171,10 +1171,10 @@ def _receiveUndoLike(recent_posts_cache: {},
print('DEBUG: liked post found in inbox. Now undoing.') print('DEBUG: liked post found in inbox. Now undoing.')
likeActor = message_json['actor'] likeActor = message_json['actor']
postLikedId = message_json['object'] postLikedId = message_json['object']
undoLikesCollectionEntry(recent_posts_cache, base_dir, postFilename, undoLikesCollectionEntry(recent_posts_cache, base_dir, post_filename,
postLikedId, likeActor, domain, debug, None) postLikedId, likeActor, domain, debug, None)
# regenerate the html # regenerate the html
likedPostJson = load_json(postFilename, 0, 1) likedPostJson = load_json(post_filename, 0, 1)
if likedPostJson: if likedPostJson:
if likedPostJson.get('type'): if likedPostJson.get('type'):
if likedPostJson['type'] == 'Announce' and \ if likedPostJson['type'] == 'Announce' and \
@ -1186,15 +1186,15 @@ def _receiveUndoLike(recent_posts_cache: {},
domain, announceLikeUrl) domain, announceLikeUrl)
if announceLikedFilename: if announceLikedFilename:
postLikedId = announceLikeUrl postLikedId = announceLikeUrl
postFilename = announceLikedFilename post_filename = announceLikedFilename
undoLikesCollectionEntry(recent_posts_cache, base_dir, undoLikesCollectionEntry(recent_posts_cache, base_dir,
postFilename, postLikedId, post_filename, postLikedId,
likeActor, domain, debug, likeActor, domain, debug,
None) None)
if likedPostJson: if likedPostJson:
if debug: if debug:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, handleName, domain, get_cached_post_filename(base_dir, handleName, domain,
likedPostJson) likedPostJson)
print('Unliked post json: ' + str(likedPostJson)) print('Unliked post json: ' + str(likedPostJson))
print('Unliked post nickname: ' + handleName + ' ' + domain) print('Unliked post nickname: ' + handleName + ' ' + domain)
@ -1295,8 +1295,9 @@ def _receiveReaction(recent_posts_cache: {},
if debug: if debug:
print('DEBUG: emoji reaction has no content') print('DEBUG: emoji reaction has no content')
return True return True
postFilename = locate_post(base_dir, handleName, handleDom, postReactionId) post_filename = locate_post(base_dir, handleName, handleDom,
if not postFilename: postReactionId)
if not post_filename:
if debug: if debug:
print('DEBUG: emoji reaction post not found in inbox or outbox') print('DEBUG: emoji reaction post not found in inbox or outbox')
print(postReactionId) print(postReactionId)
@ -1314,11 +1315,11 @@ def _receiveReaction(recent_posts_cache: {},
emojiContent): emojiContent):
_reactionNotify(base_dir, domain, onion_domain, handle, _reactionNotify(base_dir, domain, onion_domain, handle,
reactionActor, postReactionId, emojiContent) reactionActor, postReactionId, emojiContent)
updateReactionCollection(recent_posts_cache, base_dir, postFilename, updateReactionCollection(recent_posts_cache, base_dir, post_filename,
postReactionId, reactionActor, postReactionId, reactionActor,
handleName, domain, debug, None, emojiContent) handleName, domain, debug, None, emojiContent)
# regenerate the html # regenerate the html
reactionPostJson = load_json(postFilename, 0, 1) reactionPostJson = load_json(post_filename, 0, 1)
if reactionPostJson: if reactionPostJson:
if reactionPostJson.get('type'): if reactionPostJson.get('type'):
if reactionPostJson['type'] == 'Announce' and \ if reactionPostJson['type'] == 'Announce' and \
@ -1330,10 +1331,10 @@ def _receiveReaction(recent_posts_cache: {},
domain, announceReactionUrl) domain, announceReactionUrl)
if announceReactionFilename: if announceReactionFilename:
postReactionId = announceReactionUrl postReactionId = announceReactionUrl
postFilename = announceReactionFilename post_filename = announceReactionFilename
updateReactionCollection(recent_posts_cache, updateReactionCollection(recent_posts_cache,
base_dir, base_dir,
postFilename, post_filename,
postReactionId, postReactionId,
reactionActor, reactionActor,
handleName, handleName,
@ -1342,7 +1343,7 @@ def _receiveReaction(recent_posts_cache: {},
if reactionPostJson: if reactionPostJson:
if debug: if debug:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, handleName, domain, get_cached_post_filename(base_dir, handleName, domain,
reactionPostJson) reactionPostJson)
print('Reaction post json: ' + str(reactionPostJson)) print('Reaction post json: ' + str(reactionPostJson))
print('Reaction post nickname: ' + handleName + ' ' + domain) print('Reaction post nickname: ' + handleName + ' ' + domain)
@ -1428,10 +1429,10 @@ def _receiveUndoReaction(recent_posts_cache: {},
# if this post in the outbox of the person? # if this post in the outbox of the person?
handleName = handle.split('@')[0] handleName = handle.split('@')[0]
handleDom = handle.split('@')[1] handleDom = handle.split('@')[1]
postFilename = \ post_filename = \
locate_post(base_dir, handleName, handleDom, locate_post(base_dir, handleName, handleDom,
message_json['object']['object']) message_json['object']['object'])
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: unreaction post not found in inbox or outbox') print('DEBUG: unreaction post not found in inbox or outbox')
print(message_json['object']['object']) print(message_json['object']['object'])
@ -1445,11 +1446,11 @@ def _receiveUndoReaction(recent_posts_cache: {},
if debug: if debug:
print('DEBUG: unreaction has no content') print('DEBUG: unreaction has no content')
return True return True
undoReactionCollectionEntry(recent_posts_cache, base_dir, postFilename, undoReactionCollectionEntry(recent_posts_cache, base_dir, post_filename,
postReactionId, reactionActor, domain, postReactionId, reactionActor, domain,
debug, None, emojiContent) debug, None, emojiContent)
# regenerate the html # regenerate the html
reactionPostJson = load_json(postFilename, 0, 1) reactionPostJson = load_json(post_filename, 0, 1)
if reactionPostJson: if reactionPostJson:
if reactionPostJson.get('type'): if reactionPostJson.get('type'):
if reactionPostJson['type'] == 'Announce' and \ if reactionPostJson['type'] == 'Announce' and \
@ -1461,10 +1462,10 @@ def _receiveUndoReaction(recent_posts_cache: {},
domain, announceReactionUrl) domain, announceReactionUrl)
if announceReactionFilename: if announceReactionFilename:
postReactionId = announceReactionUrl postReactionId = announceReactionUrl
postFilename = announceReactionFilename post_filename = announceReactionFilename
undoReactionCollectionEntry(recent_posts_cache, undoReactionCollectionEntry(recent_posts_cache,
base_dir, base_dir,
postFilename, post_filename,
postReactionId, postReactionId,
reactionActor, domain, reactionActor, domain,
debug, None, debug, None,
@ -1472,7 +1473,7 @@ def _receiveUndoReaction(recent_posts_cache: {},
if reactionPostJson: if reactionPostJson:
if debug: if debug:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, handleName, domain, get_cached_post_filename(base_dir, handleName, domain,
reactionPostJson) reactionPostJson)
print('Unreaction post json: ' + str(reactionPostJson)) print('Unreaction post json: ' + str(reactionPostJson))
print('Unreaction post nickname: ' + handleName + ' ' + domain) print('Unreaction post nickname: ' + handleName + ' ' + domain)
@ -1567,22 +1568,22 @@ def _receiveBookmark(recent_posts_cache: {},
messageUrl = removeIdEnding(message_json['object']['url']) messageUrl = removeIdEnding(message_json['object']['url'])
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageUrl) post_filename = locate_post(base_dir, nickname, domain, messageUrl)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s inbox like post not found in inbox or outbox') print('DEBUG: c2s inbox like post not found in inbox or outbox')
print(messageUrl) print(messageUrl)
return True return True
updateBookmarksCollection(recent_posts_cache, base_dir, postFilename, updateBookmarksCollection(recent_posts_cache, base_dir, post_filename,
message_json['object']['url'], message_json['object']['url'],
message_json['actor'], domain, debug) message_json['actor'], domain, debug)
# regenerate the html # regenerate the html
bookmarkedPostJson = load_json(postFilename, 0, 1) bookmarkedPostJson = load_json(post_filename, 0, 1)
if bookmarkedPostJson: if bookmarkedPostJson:
if debug: if debug:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, get_cached_post_filename(base_dir, nickname, domain,
bookmarkedPostJson) bookmarkedPostJson)
print('Bookmarked post json: ' + str(bookmarkedPostJson)) print('Bookmarked post json: ' + str(bookmarkedPostJson))
print('Bookmarked post nickname: ' + nickname + ' ' + domain) print('Bookmarked post nickname: ' + nickname + ' ' + domain)
@ -1680,22 +1681,22 @@ def _receiveUndoBookmark(recent_posts_cache: {},
messageUrl = removeIdEnding(message_json['object']['url']) messageUrl = removeIdEnding(message_json['object']['url'])
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageUrl) post_filename = locate_post(base_dir, nickname, domain, messageUrl)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s inbox like post not found in inbox or outbox') print('DEBUG: c2s inbox like post not found in inbox or outbox')
print(messageUrl) print(messageUrl)
return True return True
undoBookmarksCollectionEntry(recent_posts_cache, base_dir, postFilename, undoBookmarksCollectionEntry(recent_posts_cache, base_dir, post_filename,
message_json['object']['url'], message_json['object']['url'],
message_json['actor'], domain, debug) message_json['actor'], domain, debug)
# regenerate the html # regenerate the html
bookmarkedPostJson = load_json(postFilename, 0, 1) bookmarkedPostJson = load_json(post_filename, 0, 1)
if bookmarkedPostJson: if bookmarkedPostJson:
if debug: if debug:
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, get_cached_post_filename(base_dir, nickname, domain,
bookmarkedPostJson) bookmarkedPostJson)
print('Unbookmarked post json: ' + str(bookmarkedPostJson)) print('Unbookmarked post json: ' + str(bookmarkedPostJson))
print('Unbookmarked post nickname: ' + nickname + ' ' + domain) print('Unbookmarked post nickname: ' + nickname + ' ' + domain)
@ -1776,29 +1777,29 @@ def _receiveDelete(session, handle: str, isGroup: bool, base_dir: str,
removeModerationPostFromIndex(base_dir, messageId, debug) removeModerationPostFromIndex(base_dir, messageId, debug)
handleNickname = handle.split('@')[0] handleNickname = handle.split('@')[0]
handleDomain = handle.split('@')[1] handleDomain = handle.split('@')[1]
postFilename = locate_post(base_dir, handleNickname, post_filename = locate_post(base_dir, handleNickname,
handleDomain, messageId) handleDomain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: delete post not found in inbox or outbox') print('DEBUG: delete post not found in inbox or outbox')
print(messageId) print(messageId)
return True return True
deletePost(base_dir, http_prefix, handleNickname, deletePost(base_dir, http_prefix, handleNickname,
handleDomain, postFilename, debug, handleDomain, post_filename, debug,
recent_posts_cache) recent_posts_cache)
if debug: if debug:
print('DEBUG: post deleted - ' + postFilename) print('DEBUG: post deleted - ' + post_filename)
# also delete any local blogs saved to the news actor # also delete any local blogs saved to the news actor
if handleNickname != 'news' and handleDomain == domain_full: if handleNickname != 'news' and handleDomain == domain_full:
postFilename = locate_post(base_dir, 'news', post_filename = locate_post(base_dir, 'news',
handleDomain, messageId) handleDomain, messageId)
if postFilename: if post_filename:
deletePost(base_dir, http_prefix, 'news', deletePost(base_dir, http_prefix, 'news',
handleDomain, postFilename, debug, handleDomain, post_filename, debug,
recent_posts_cache) recent_posts_cache)
if debug: if debug:
print('DEBUG: blog post deleted - ' + postFilename) print('DEBUG: blog post deleted - ' + post_filename)
return True return True
@ -1889,14 +1890,14 @@ def _receiveAnnounce(recent_posts_cache: {},
return False return False
# is this post in the outbox of the person? # is this post in the outbox of the person?
postFilename = locate_post(base_dir, nickname, domain, post_filename = locate_post(base_dir, nickname, domain,
message_json['object']) message_json['object'])
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: announce post not found in inbox or outbox') print('DEBUG: announce post not found in inbox or outbox')
print(message_json['object']) print(message_json['object'])
return True return True
updateAnnounceCollection(recent_posts_cache, base_dir, postFilename, update_announce_collection(recent_posts_cache, base_dir, post_filename,
message_json['actor'], nickname, domain, debug) message_json['actor'], nickname, domain, debug)
if debug: if debug:
print('DEBUG: Downloading announce post ' + message_json['actor'] + print('DEBUG: Downloading announce post ' + message_json['actor'] +
@ -1959,13 +1960,13 @@ def _receiveAnnounce(recent_posts_cache: {},
if onion_domain in message_json['object']: if onion_domain in message_json['object']:
notInOnion = False notInOnion = False
if domain not in message_json['object'] and notInOnion: if domain not in message_json['object'] and notInOnion:
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
# if the announce can't be downloaded then remove it # if the announce can't be downloaded then remove it
try: try:
os.remove(postFilename) os.remove(post_filename)
except OSError: except OSError:
print('EX: _receiveAnnounce unable to delete ' + print('EX: _receiveAnnounce unable to delete ' +
str(postFilename)) str(post_filename))
else: else:
if debug: if debug:
print('DEBUG: Announce post downloaded for ' + print('DEBUG: Announce post downloaded for ' +
@ -1991,7 +1992,7 @@ def _receiveAnnounce(recent_posts_cache: {},
lookupActor = lookupActor.split('/statuses/')[0] lookupActor = lookupActor.split('/statuses/')[0]
if is_recent_post(post_json_object, 3): if is_recent_post(post_json_object, 3):
if not os.path.isfile(postFilename + '.tts'): if not os.path.isfile(post_filename + '.tts'):
domain_full = get_full_domain(domain, port) domain_full = get_full_domain(domain, port)
updateSpeaker(base_dir, http_prefix, updateSpeaker(base_dir, http_prefix,
nickname, domain, domain_full, nickname, domain, domain_full,
@ -1999,11 +2000,11 @@ def _receiveAnnounce(recent_posts_cache: {},
translate, lookupActor, translate, lookupActor,
theme_name) theme_name)
try: try:
with open(postFilename + '.tts', 'w+') as ttsFile: with open(post_filename + '.tts', 'w+') as ttsFile:
ttsFile.write('\n') ttsFile.write('\n')
except OSError: except OSError:
print('EX: unable to write recent post ' + print('EX: unable to write recent post ' +
postFilename) post_filename)
if debug: if debug:
print('DEBUG: Obtaining actor for announce post ' + print('DEBUG: Obtaining actor for announce post ' +
@ -2060,9 +2061,9 @@ def _receiveUndoAnnounce(recent_posts_cache: {},
# if this post in the outbox of the person? # if this post in the outbox of the person?
handleName = handle.split('@')[0] handleName = handle.split('@')[0]
handleDom = handle.split('@')[1] handleDom = handle.split('@')[1]
postFilename = locate_post(base_dir, handleName, handleDom, post_filename = locate_post(base_dir, handleName, handleDom,
message_json['object']['object']) message_json['object']['object'])
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: undo announce post not found in inbox or outbox') print('DEBUG: undo announce post not found in inbox or outbox')
print(message_json['object']['object']) print(message_json['object']['object'])
@ -2070,7 +2071,7 @@ def _receiveUndoAnnounce(recent_posts_cache: {},
if debug: if debug:
print('DEBUG: announced/repeated post to be undone found in inbox') print('DEBUG: announced/repeated post to be undone found in inbox')
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if post_json_object: if post_json_object:
if not post_json_object.get('type'): if not post_json_object.get('type'):
if post_json_object['type'] != 'Announce': if post_json_object['type'] != 'Announce':
@ -2078,14 +2079,14 @@ def _receiveUndoAnnounce(recent_posts_cache: {},
print("DEBUG: Attempt to undo something " + print("DEBUG: Attempt to undo something " +
"which isn't an announcement") "which isn't an announcement")
return False return False
undoAnnounceCollectionEntry(recent_posts_cache, base_dir, postFilename, undoAnnounceCollectionEntry(recent_posts_cache, base_dir, post_filename,
message_json['actor'], domain, debug) message_json['actor'], domain, debug)
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
try: try:
os.remove(postFilename) os.remove(post_filename)
except OSError: except OSError:
print('EX: _receiveUndoAnnounce unable to delete ' + print('EX: _receiveUndoAnnounce unable to delete ' +
str(postFilename)) str(post_filename))
return True return True
@ -2106,10 +2107,10 @@ def jsonPostAllowsComments(post_json_object: {}) -> bool:
return True return True
def _postAllowsComments(postFilename: str) -> bool: def _postAllowsComments(post_filename: str) -> bool:
"""Returns true if the given post allows comments/replies """Returns true if the given post allows comments/replies
""" """
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return False return False
return jsonPostAllowsComments(post_json_object) return jsonPostAllowsComments(post_json_object)
@ -2150,19 +2151,19 @@ def populateReplies(base_dir: str, http_prefix: str, domain: str,
print('DEBUG: no domain found for ' + replyTo) print('DEBUG: no domain found for ' + replyTo)
return False return False
postFilename = locate_post(base_dir, replyToNickname, post_filename = locate_post(base_dir, replyToNickname,
replyToDomain, replyTo) replyToDomain, replyTo)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: post may have expired - ' + replyTo) print('DEBUG: post may have expired - ' + replyTo)
return False return False
if not _postAllowsComments(postFilename): if not _postAllowsComments(post_filename):
if debug: if debug:
print('DEBUG: post does not allow comments - ' + replyTo) print('DEBUG: post does not allow comments - ' + replyTo)
return False return False
# populate a text file containing the ids of replies # populate a text file containing the ids of replies
postRepliesFilename = postFilename.replace('.json', '.replies') postRepliesFilename = post_filename.replace('.json', '.replies')
messageId = removeIdEnding(message_json['id']) messageId = removeIdEnding(message_json['id'])
if os.path.isfile(postRepliesFilename): if os.path.isfile(postRepliesFilename):
numLines = sum(1 for line in open(postRepliesFilename)) numLines = sum(1 for line in open(postRepliesFilename))
@ -2365,11 +2366,11 @@ def _alreadyLiked(base_dir: str, nickname: str, domain: str,
postUrl: str, likerActor: str) -> bool: postUrl: str, likerActor: str) -> bool:
"""Is the given post already liked by the given handle? """Is the given post already liked by the given handle?
""" """
postFilename = \ post_filename = \
locate_post(base_dir, nickname, domain, postUrl) locate_post(base_dir, nickname, domain, postUrl)
if not postFilename: if not post_filename:
return False return False
post_json_object = load_json(postFilename, 1) post_json_object = load_json(post_filename, 1)
if not post_json_object: if not post_json_object:
return False return False
if not has_object_dict(post_json_object): if not has_object_dict(post_json_object):
@ -2395,11 +2396,11 @@ def _alreadyReacted(base_dir: str, nickname: str, domain: str,
emojiContent: str) -> bool: emojiContent: str) -> bool:
"""Is the given post already emoji reacted by the given handle? """Is the given post already emoji reacted by the given handle?
""" """
postFilename = \ post_filename = \
locate_post(base_dir, nickname, domain, postUrl) locate_post(base_dir, nickname, domain, postUrl)
if not postFilename: if not post_filename:
return False return False
post_json_object = load_json(postFilename, 1) post_json_object = load_json(post_filename, 1)
if not post_json_object: if not post_json_object:
return False return False
if not has_object_dict(post_json_object): if not has_object_dict(post_json_object):
@ -3013,7 +3014,7 @@ def _receiveQuestionVote(base_dir: str, nickname: str, domain: str,
# ensure that the cached post is removed if it exists, so # ensure that the cached post is removed if it exists, so
# that it then will be recreated # that it then will be recreated
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, questionJson) get_cached_post_filename(base_dir, nickname, domain, questionJson)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
try: try:

33
like.py
View File

@ -27,7 +27,7 @@ from utils import local_actor_url
from utils import load_json from utils import load_json
from utils import save_json from utils import save_json
from utils import removePostFromCache from utils import removePostFromCache
from utils import getCachedPostFilename from utils import get_cached_post_filename
from posts import sendSignedJson from posts import sendSignedJson
from session import postJson from session import postJson
from webfinger import webfingerHandle from webfinger import webfingerHandle
@ -119,8 +119,8 @@ def _like(recent_posts_cache: {},
has_group_type(base_dir, actorLiked, person_cache) has_group_type(base_dir, actorLiked, person_cache)
if likedPostNickname: if likedPostNickname:
postFilename = locate_post(base_dir, nickname, domain, objectUrl) post_filename = locate_post(base_dir, nickname, domain, objectUrl)
if not postFilename: if not post_filename:
print('DEBUG: like base_dir: ' + base_dir) print('DEBUG: like base_dir: ' + base_dir)
print('DEBUG: like nickname: ' + nickname) print('DEBUG: like nickname: ' + nickname)
print('DEBUG: like domain: ' + domain) print('DEBUG: like domain: ' + domain)
@ -128,7 +128,7 @@ def _like(recent_posts_cache: {},
return None return None
updateLikesCollection(recent_posts_cache, updateLikesCollection(recent_posts_cache,
base_dir, postFilename, objectUrl, base_dir, post_filename, objectUrl,
newLikeJson['actor'], newLikeJson['actor'],
nickname, domain, debug, None) nickname, domain, debug, None)
@ -360,18 +360,18 @@ def outboxLike(recent_posts_cache: {},
messageId = removeIdEnding(message_json['object']) messageId = removeIdEnding(message_json['object'])
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s like post not found in inbox or outbox') print('DEBUG: c2s like post not found in inbox or outbox')
print(messageId) print(messageId)
return True return True
updateLikesCollection(recent_posts_cache, updateLikesCollection(recent_posts_cache,
base_dir, postFilename, messageId, base_dir, post_filename, messageId,
message_json['actor'], message_json['actor'],
nickname, domain, debug, None) nickname, domain, debug, None)
if debug: if debug:
print('DEBUG: post liked via c2s - ' + postFilename) print('DEBUG: post liked via c2s - ' + post_filename)
def outboxUndoLike(recent_posts_cache: {}, def outboxUndoLike(recent_posts_cache: {},
@ -397,35 +397,36 @@ def outboxUndoLike(recent_posts_cache: {},
messageId = removeIdEnding(message_json['object']['object']) messageId = removeIdEnding(message_json['object']['object'])
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s undo like post not found in inbox or outbox') print('DEBUG: c2s undo like post not found in inbox or outbox')
print(messageId) print(messageId)
return True return True
undoLikesCollectionEntry(recent_posts_cache, base_dir, postFilename, undoLikesCollectionEntry(recent_posts_cache, base_dir, post_filename,
messageId, message_json['actor'], messageId, message_json['actor'],
domain, debug, None) domain, debug, None)
if debug: if debug:
print('DEBUG: post undo liked via c2s - ' + postFilename) print('DEBUG: post undo liked via c2s - ' + post_filename)
def updateLikesCollection(recent_posts_cache: {}, def updateLikesCollection(recent_posts_cache: {},
base_dir: str, postFilename: str, base_dir: str, post_filename: str,
objectUrl: str, actor: str, objectUrl: str, actor: str,
nickname: str, domain: str, debug: bool, nickname: str, domain: str, debug: bool,
post_json_object: {}) -> None: post_json_object: {}) -> None:
"""Updates the likes collection within a post """Updates the likes collection within a post
""" """
if not post_json_object: if not post_json_object:
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return return
# remove any cached version of this post so that the # remove any cached version of this post so that the
# like icon is changed # like icon is changed
removePostFromCache(post_json_object, recent_posts_cache) removePostFromCache(post_json_object, recent_posts_cache)
cachedPostFilename = getCachedPostFilename(base_dir, nickname, cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object) domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -474,4 +475,4 @@ def updateLikesCollection(recent_posts_cache: {},
if debug: if debug:
print('DEBUG: saving post with likes added') print('DEBUG: saving post with likes added')
pprint(post_json_object) pprint(post_json_object)
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)

View File

@ -64,14 +64,14 @@ def _updateFeedsOutboxIndex(base_dir: str, domain: str, post_id: str) -> None:
print('EX: unable to write ' + indexFilename) print('EX: unable to write ' + indexFilename)
def _saveArrivedTime(base_dir: str, postFilename: str, arrived: str) -> None: def _saveArrivedTime(base_dir: str, post_filename: str, arrived: str) -> None:
"""Saves the time when an rss post arrived to a file """Saves the time when an rss post arrived to a file
""" """
try: try:
with open(postFilename + '.arrived', 'w+') as arrivedFile: with open(post_filename + '.arrived', 'w+') as arrivedFile:
arrivedFile.write(arrived) arrivedFile.write(arrived)
except OSError: except OSError:
print('EX: unable to write ' + postFilename + '.arrived') print('EX: unable to write ' + post_filename + '.arrived')
def _removeControlCharacters(content: str) -> str: def _removeControlCharacters(content: str) -> str:

View File

@ -200,7 +200,7 @@ def _downloadNewswireFeedFavicon(session, base_dir: str,
def _addNewswireDictEntry(base_dir: str, domain: str, def _addNewswireDictEntry(base_dir: str, domain: str,
newswire: {}, dateStr: str, newswire: {}, dateStr: str,
title: str, link: str, title: str, link: str,
votesStatus: str, postFilename: str, votesStatus: str, post_filename: str,
description: str, moderated: bool, description: str, moderated: bool,
mirrored: bool, mirrored: bool,
tags: [], tags: [],
@ -243,7 +243,7 @@ def _addNewswireDictEntry(base_dir: str, domain: str,
title, title,
link, link,
votesStatus, votesStatus,
postFilename, post_filename,
description, description,
moderated, moderated,
postTags, postTags,
@ -445,12 +445,12 @@ def _xml2StrToDict(base_dir: str, domain: str, xmlStr: str,
pubDateStr = parseFeedDate(pubDate) pubDateStr = parseFeedDate(pubDate)
if pubDateStr: if pubDateStr:
if _validFeedDate(pubDateStr): if _validFeedDate(pubDateStr):
postFilename = '' post_filename = ''
votesStatus = [] votesStatus = []
_addNewswireDictEntry(base_dir, domain, _addNewswireDictEntry(base_dir, domain,
result, pubDateStr, result, pubDateStr,
title, link, title, link,
votesStatus, postFilename, votesStatus, post_filename,
description, moderated, description, moderated,
mirrored, [], 32, session, debug) mirrored, [], 32, session, debug)
postCtr += 1 postCtr += 1
@ -533,12 +533,12 @@ def _xml1StrToDict(base_dir: str, domain: str, xmlStr: str,
pubDateStr = parseFeedDate(pubDate) pubDateStr = parseFeedDate(pubDate)
if pubDateStr: if pubDateStr:
if _validFeedDate(pubDateStr): if _validFeedDate(pubDateStr):
postFilename = '' post_filename = ''
votesStatus = [] votesStatus = []
_addNewswireDictEntry(base_dir, domain, _addNewswireDictEntry(base_dir, domain,
result, pubDateStr, result, pubDateStr,
title, link, title, link,
votesStatus, postFilename, votesStatus, post_filename,
description, moderated, description, moderated,
mirrored, [], 32, session, debug) mirrored, [], 32, session, debug)
postCtr += 1 postCtr += 1
@ -609,12 +609,12 @@ def _atomFeedToDict(base_dir: str, domain: str, xmlStr: str,
pubDateStr = parseFeedDate(pubDate) pubDateStr = parseFeedDate(pubDate)
if pubDateStr: if pubDateStr:
if _validFeedDate(pubDateStr): if _validFeedDate(pubDateStr):
postFilename = '' post_filename = ''
votesStatus = [] votesStatus = []
_addNewswireDictEntry(base_dir, domain, _addNewswireDictEntry(base_dir, domain,
result, pubDateStr, result, pubDateStr,
title, link, title, link,
votesStatus, postFilename, votesStatus, post_filename,
description, moderated, description, moderated,
mirrored, [], 32, session, debug) mirrored, [], 32, session, debug)
postCtr += 1 postCtr += 1
@ -721,12 +721,12 @@ def _jsonFeedV1ToDict(base_dir: str, domain: str, xmlStr: str,
pubDateStr = parseFeedDate(pubDate) pubDateStr = parseFeedDate(pubDate)
if pubDateStr: if pubDateStr:
if _validFeedDate(pubDateStr): if _validFeedDate(pubDateStr):
postFilename = '' post_filename = ''
votesStatus = [] votesStatus = []
_addNewswireDictEntry(base_dir, domain, _addNewswireDictEntry(base_dir, domain,
result, pubDateStr, result, pubDateStr,
title, link, title, link,
votesStatus, postFilename, votesStatus, post_filename,
description, moderated, description, moderated,
mirrored, [], 32, session, debug) mirrored, [], 32, session, debug)
postCtr += 1 postCtr += 1
@ -794,12 +794,12 @@ def _atomFeedYTToDict(base_dir: str, domain: str, xmlStr: str,
pubDateStr = parseFeedDate(pubDate) pubDateStr = parseFeedDate(pubDate)
if pubDateStr: if pubDateStr:
if _validFeedDate(pubDateStr): if _validFeedDate(pubDateStr):
postFilename = '' post_filename = ''
votesStatus = [] votesStatus = []
_addNewswireDictEntry(base_dir, domain, _addNewswireDictEntry(base_dir, domain,
result, pubDateStr, result, pubDateStr,
title, link, title, link,
votesStatus, postFilename, votesStatus, post_filename,
description, moderated, mirrored, description, moderated, mirrored,
[], 32, session, debug) [], 32, session, debug)
postCtr += 1 postCtr += 1
@ -1024,20 +1024,20 @@ def _addAccountBlogsToNewswire(base_dir: str, nickname: str, domain: str,
moderated = True moderated = True
with open(indexFilename, 'r') as indexFile: with open(indexFilename, 'r') as indexFile:
postFilename = 'start' post_filename = 'start'
ctr = 0 ctr = 0
while postFilename: while post_filename:
postFilename = indexFile.readline() post_filename = indexFile.readline()
if postFilename: if post_filename:
# if this is a full path then remove the directories # if this is a full path then remove the directories
if '/' in postFilename: if '/' in post_filename:
postFilename = postFilename.split('/')[-1] post_filename = post_filename.split('/')[-1]
# filename of the post without any extension or path # filename of the post without any extension or path
# This should also correspond to any index entry in # This should also correspond to any index entry in
# the posts cache # the posts cache
postUrl = \ postUrl = \
postFilename.replace('\n', '').replace('\r', '') post_filename.replace('\n', '').replace('\r', '')
postUrl = postUrl.replace('.json', '').strip() postUrl = postUrl.replace('.json', '').strip()
# read the post from file # read the post from file

View File

@ -3445,10 +3445,10 @@ def createModeration(base_dir: str, nickname: str, domain: str, port: int,
lineNumber -= 1 lineNumber -= 1
for postUrl in pageLines: for postUrl in pageLines:
postFilename = \ post_filename = \
boxDir + '/' + postUrl.replace('/', '#') + '.json' boxDir + '/' + postUrl.replace('/', '#') + '.json'
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if post_json_object: if post_json_object:
boxItems['orderedItems'].append(post_json_object) boxItems['orderedItems'].append(post_json_object)
@ -3607,7 +3607,7 @@ def removePostInteractions(post_json_object: {}, force: bool) -> bool:
def _passedNewswireVoting(newswire_votes_threshold: int, def _passedNewswireVoting(newswire_votes_threshold: int,
base_dir: str, domain: str, base_dir: str, domain: str,
postFilename: str, post_filename: str,
positive_voting: bool, positive_voting: bool,
voting_time_mins: int) -> bool: voting_time_mins: int) -> bool:
"""Returns true if the post has passed through newswire voting """Returns true if the post has passed through newswire voting
@ -3618,7 +3618,7 @@ def _passedNewswireVoting(newswire_votes_threshold: int,
# note that the presence of an arrival file also indicates # note that the presence of an arrival file also indicates
# that this post is moderated # that this post is moderated
arrivalDate = \ arrivalDate = \
locateNewsArrival(base_dir, domain, postFilename) locateNewsArrival(base_dir, domain, post_filename)
if not arrivalDate: if not arrivalDate:
return True return True
# how long has elapsed since this post arrived? # how long has elapsed since this post arrived?
@ -3632,7 +3632,7 @@ def _passedNewswireVoting(newswire_votes_threshold: int,
return False return False
# if there a votes file for this post? # if there a votes file for this post?
votesFilename = \ votesFilename = \
locateNewsVotes(base_dir, domain, postFilename) locateNewsVotes(base_dir, domain, post_filename)
if not votesFilename: if not votesFilename:
return True return True
# load the votes file and count the votes # load the votes file and count the votes
@ -3733,15 +3733,15 @@ def _createBoxIndexed(recent_posts_cache: {},
with open(indexFilename, 'r') as indexFile: with open(indexFilename, 'r') as indexFile:
postsAddedToTimeline = 0 postsAddedToTimeline = 0
while postsAddedToTimeline < itemsPerPage: while postsAddedToTimeline < itemsPerPage:
postFilename = indexFile.readline() post_filename = indexFile.readline()
if not postFilename: if not post_filename:
break break
# Has this post passed through the newswire voting stage? # Has this post passed through the newswire voting stage?
if not _passedNewswireVoting(newswire_votes_threshold, if not _passedNewswireVoting(newswire_votes_threshold,
base_dir, domain, base_dir, domain,
postFilename, post_filename,
positive_voting, positive_voting,
voting_time_mins): voting_time_mins):
continue continue
@ -3752,14 +3752,14 @@ def _createBoxIndexed(recent_posts_cache: {},
continue continue
# if this is a full path then remove the directories # if this is a full path then remove the directories
if '/' in postFilename: if '/' in post_filename:
postFilename = postFilename.split('/')[-1] post_filename = post_filename.split('/')[-1]
# filename of the post without any extension or path # filename of the post without any extension or path
# This should also correspond to any index entry in # This should also correspond to any index entry in
# the posts cache # the posts cache
postUrl = \ postUrl = \
postFilename.replace('\n', '').replace('\r', '') post_filename.replace('\n', '').replace('\r', '')
postUrl = postUrl.replace('.json', '').strip() postUrl = postUrl.replace('.json', '').strip()
if postUrl in postUrlsInBox: if postUrl in postUrlsInBox:
@ -3983,12 +3983,12 @@ def archivePostsForPerson(http_prefix: str, nickname: str, domain: str,
postsInBoxDict = {} postsInBoxDict = {}
postsCtr = 0 postsCtr = 0
postsInBox = os.scandir(boxDir) postsInBox = os.scandir(boxDir)
for postFilename in postsInBox: for post_filename in postsInBox:
postFilename = postFilename.name post_filename = post_filename.name
if not postFilename.endswith('.json'): if not post_filename.endswith('.json'):
continue continue
# Time of file creation # Time of file creation
fullFilename = os.path.join(boxDir, postFilename) fullFilename = os.path.join(boxDir, post_filename)
if os.path.isfile(fullFilename): if os.path.isfile(fullFilename):
content = open(fullFilename).read() content = open(fullFilename).read()
if '"published":' in content: if '"published":' in content:
@ -3996,7 +3996,7 @@ def archivePostsForPerson(http_prefix: str, nickname: str, domain: str,
if '"' in publishedStr: if '"' in publishedStr:
publishedStr = publishedStr.split('"')[1] publishedStr = publishedStr.split('"')[1]
if publishedStr.endswith('Z'): if publishedStr.endswith('Z'):
postsInBoxDict[publishedStr] = postFilename postsInBoxDict[publishedStr] = post_filename
postsCtr += 1 postsCtr += 1
noOfPosts = postsCtr noOfPosts = postsCtr
@ -4013,12 +4013,12 @@ def archivePostsForPerson(http_prefix: str, nickname: str, domain: str,
postCacheDir = boxDir.replace('/' + boxname, '/postcache') postCacheDir = boxDir.replace('/' + boxname, '/postcache')
removeCtr = 0 removeCtr = 0
for publishedStr, postFilename in postsInBoxSorted.items(): for publishedStr, post_filename in postsInBoxSorted.items():
filePath = os.path.join(boxDir, postFilename) filePath = os.path.join(boxDir, post_filename)
if not os.path.isfile(filePath): if not os.path.isfile(filePath):
continue continue
if archive_dir: if archive_dir:
archivePath = os.path.join(archive_dir, postFilename) archivePath = os.path.join(archive_dir, post_filename)
os.rename(filePath, archivePath) os.rename(filePath, archivePath)
extensions = ('replies', 'votes', 'arrived', 'muted') extensions = ('replies', 'votes', 'arrived', 'muted')
@ -4039,7 +4039,7 @@ def archivePostsForPerson(http_prefix: str, nickname: str, domain: str,
# remove cached html posts # remove cached html posts
postCacheFilename = \ postCacheFilename = \
os.path.join(postCacheDir, postFilename).replace('.json', '.html') os.path.join(postCacheDir, post_filename).replace('.json', '.html')
if os.path.isfile(postCacheFilename): if os.path.isfile(postCacheFilename):
try: try:
os.remove(postCacheFilename) os.remove(postCacheFilename)
@ -4754,10 +4754,10 @@ def isMuted(base_dir: str, nickname: str, domain: str, post_id: str,
conversationId.replace('/', '#') + '.muted' conversationId.replace('/', '#') + '.muted'
if os.path.isfile(convMutedFilename): if os.path.isfile(convMutedFilename):
return True return True
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
return False return False
if os.path.isfile(postFilename + '.muted'): if os.path.isfile(post_filename + '.muted'):
return True return True
return False return False
@ -5225,11 +5225,11 @@ def editedPostFilename(base_dir: str, nickname: str, domain: str,
return '' return ''
if lastpost_id == post_id: if lastpost_id == post_id:
return '' return ''
lastpostFilename = \ lastpost_filename = \
locate_post(base_dir, nickname, domain, lastpost_id, False) locate_post(base_dir, nickname, domain, lastpost_id, False)
if not lastpostFilename: if not lastpost_filename:
return '' return ''
lastpostJson = load_json(lastpostFilename, 0) lastpostJson = load_json(lastpost_filename, 0)
if not lastpostJson: if not lastpostJson:
return '' return ''
if not lastpostJson.get('type'): if not lastpostJson.get('type'):
@ -5261,7 +5261,7 @@ def editedPostFilename(base_dir: str, nickname: str, domain: str,
post_json_object['object']['content'], 10) < 70: post_json_object['object']['content'], 10) < 70:
return '' return ''
print(post_id + ' is an edit of ' + lastpost_id) print(post_id + ' is an edit of ' + lastpost_id)
return lastpostFilename return lastpost_filename
def getOriginalPostFromAnnounceUrl(announceUrl: str, base_dir: str, def getOriginalPostFromAnnounceUrl(announceUrl: str, base_dir: str,
@ -5270,20 +5270,20 @@ def getOriginalPostFromAnnounceUrl(announceUrl: str, base_dir: str,
"""From the url of an announce this returns the actor, url and """From the url of an announce this returns the actor, url and
filename (if available) of the original post being announced filename (if available) of the original post being announced
""" """
postFilename = locate_post(base_dir, nickname, domain, announceUrl) post_filename = locate_post(base_dir, nickname, domain, announceUrl)
if not postFilename: if not post_filename:
return None, None, None return None, None, None
announcePostJson = load_json(postFilename, 0, 1) announcePostJson = load_json(post_filename, 0, 1)
if not announcePostJson: if not announcePostJson:
return None, None, postFilename return None, None, post_filename
if not announcePostJson.get('type'): if not announcePostJson.get('type'):
return None, None, postFilename return None, None, post_filename
if announcePostJson['type'] != 'Announce': if announcePostJson['type'] != 'Announce':
return None, None, postFilename return None, None, post_filename
if not announcePostJson.get('object'): if not announcePostJson.get('object'):
return None, None, postFilename return None, None, post_filename
if not isinstance(announcePostJson['object'], str): if not isinstance(announcePostJson['object'], str):
return None, None, postFilename return None, None, post_filename
actor = url = None actor = url = None
# do we have the original post? # do we have the original post?
origPostId = announcePostJson['object'] origPostId = announcePostJson['object']

View File

@ -29,7 +29,7 @@ from utils import local_actor_url
from utils import load_json from utils import load_json
from utils import save_json from utils import save_json
from utils import removePostFromCache from utils import removePostFromCache
from utils import getCachedPostFilename from utils import get_cached_post_filename
from utils import containsInvalidChars from utils import containsInvalidChars
from posts import sendSignedJson from posts import sendSignedJson
from session import postJson from session import postJson
@ -118,8 +118,8 @@ def _reaction(recent_posts_cache: {},
has_group_type(base_dir, actorReaction, person_cache) has_group_type(base_dir, actorReaction, person_cache)
if reactionPostNickname: if reactionPostNickname:
postFilename = locate_post(base_dir, nickname, domain, objectUrl) post_filename = locate_post(base_dir, nickname, domain, objectUrl)
if not postFilename: if not post_filename:
print('DEBUG: reaction base_dir: ' + base_dir) print('DEBUG: reaction base_dir: ' + base_dir)
print('DEBUG: reaction nickname: ' + nickname) print('DEBUG: reaction nickname: ' + nickname)
print('DEBUG: reaction domain: ' + domain) print('DEBUG: reaction domain: ' + domain)
@ -127,7 +127,7 @@ def _reaction(recent_posts_cache: {},
return None return None
updateReactionCollection(recent_posts_cache, updateReactionCollection(recent_posts_cache,
base_dir, postFilename, objectUrl, base_dir, post_filename, objectUrl,
newReactionJson['actor'], newReactionJson['actor'],
nickname, domain, debug, None, nickname, domain, debug, None,
emojiContent) emojiContent)
@ -384,18 +384,18 @@ def outboxReaction(recent_posts_cache: {},
messageId = removeIdEnding(message_json['object']) messageId = removeIdEnding(message_json['object'])
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
emojiContent = message_json['content'] emojiContent = message_json['content']
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s reaction post not found in inbox or outbox') print('DEBUG: c2s reaction post not found in inbox or outbox')
print(messageId) print(messageId)
return True return True
updateReactionCollection(recent_posts_cache, updateReactionCollection(recent_posts_cache,
base_dir, postFilename, messageId, base_dir, post_filename, messageId,
message_json['actor'], message_json['actor'],
nickname, domain, debug, None, emojiContent) nickname, domain, debug, None, emojiContent)
if debug: if debug:
print('DEBUG: post reaction via c2s - ' + postFilename) print('DEBUG: post reaction via c2s - ' + post_filename)
def outboxUndoReaction(recent_posts_cache: {}, def outboxUndoReaction(recent_posts_cache: {},
@ -426,21 +426,21 @@ def outboxUndoReaction(recent_posts_cache: {},
messageId = removeIdEnding(message_json['object']['object']) messageId = removeIdEnding(message_json['object']['object'])
emojiContent = message_json['object']['content'] emojiContent = message_json['object']['content']
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
if debug: if debug:
print('DEBUG: c2s undo reaction post not found in inbox or outbox') print('DEBUG: c2s undo reaction post not found in inbox or outbox')
print(messageId) print(messageId)
return True return True
undoReactionCollectionEntry(recent_posts_cache, base_dir, postFilename, undoReactionCollectionEntry(recent_posts_cache, base_dir, post_filename,
messageId, message_json['actor'], messageId, message_json['actor'],
domain, debug, None, emojiContent) domain, debug, None, emojiContent)
if debug: if debug:
print('DEBUG: post undo reaction via c2s - ' + postFilename) print('DEBUG: post undo reaction via c2s - ' + post_filename)
def updateReactionCollection(recent_posts_cache: {}, def updateReactionCollection(recent_posts_cache: {},
base_dir: str, postFilename: str, base_dir: str, post_filename: str,
objectUrl: str, actor: str, objectUrl: str, actor: str,
nickname: str, domain: str, debug: bool, nickname: str, domain: str, debug: bool,
post_json_object: {}, post_json_object: {},
@ -448,14 +448,15 @@ def updateReactionCollection(recent_posts_cache: {},
"""Updates the reactions collection within a post """Updates the reactions collection within a post
""" """
if not post_json_object: if not post_json_object:
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return return
# remove any cached version of this post so that the # remove any cached version of this post so that the
# reaction icon is changed # reaction icon is changed
removePostFromCache(post_json_object, recent_posts_cache) removePostFromCache(post_json_object, recent_posts_cache)
cachedPostFilename = getCachedPostFilename(base_dir, nickname, cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object) domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -510,7 +511,7 @@ def updateReactionCollection(recent_posts_cache: {},
if debug: if debug:
print('DEBUG: saving post with emoji reaction added') print('DEBUG: saving post with emoji reaction added')
pprint(post_json_object) pprint(post_json_object)
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
def htmlEmojiReactions(post_json_object: {}, interactive: bool, def htmlEmojiReactions(post_json_object: {}, interactive: bool,

View File

@ -43,15 +43,15 @@ def _updatePostSchedule(base_dir: str, handle: str, httpd,
if 'T' not in dateStr: if 'T' not in dateStr:
continue continue
post_id = line.split(' ', 1)[1].replace('\n', '').replace('\r', '') post_id = line.split(' ', 1)[1].replace('\n', '').replace('\r', '')
postFilename = scheduleDir + post_id + '.json' post_filename = scheduleDir + post_id + '.json'
if deleteSchedulePost: if deleteSchedulePost:
# delete extraneous scheduled posts # delete extraneous scheduled posts
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
try: try:
os.remove(postFilename) os.remove(post_filename)
except OSError: except OSError:
print('EX: _updatePostSchedule unable to delete ' + print('EX: _updatePostSchedule unable to delete ' +
str(postFilename)) str(post_filename))
continue continue
# create the new index file # create the new index file
indexLines.append(line) indexLines.append(line)
@ -68,12 +68,12 @@ def _updatePostSchedule(base_dir: str, handle: str, httpd,
continue continue
if curr_time.time().minute < postTime.time().minute: if curr_time.time().minute < postTime.time().minute:
continue continue
if not os.path.isfile(postFilename): if not os.path.isfile(post_filename):
print('WARN: schedule missing postFilename=' + postFilename) print('WARN: schedule missing post_filename=' + post_filename)
indexLines.remove(line) indexLines.remove(line)
continue continue
# load post # load post
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
print('WARN: schedule json not loaded') print('WARN: schedule json not loaded')
indexLines.remove(line) indexLines.remove(line)
@ -132,16 +132,16 @@ def _updatePostSchedule(base_dir: str, handle: str, httpd,
httpd.content_license_url): httpd.content_license_url):
indexLines.remove(line) indexLines.remove(line)
try: try:
os.remove(postFilename) os.remove(post_filename)
except OSError: except OSError:
print('EX: _updatePostSchedule unable to delete ' + print('EX: _updatePostSchedule unable to delete ' +
str(postFilename)) str(post_filename))
continue continue
# move to the outbox # move to the outbox
outboxPostFilename = postFilename.replace('/scheduled/', outboxPostFilename = \
'/outbox/') post_filename.replace('/scheduled/', '/outbox/')
os.rename(postFilename, outboxPostFilename) os.rename(post_filename, outboxPostFilename)
print('Scheduled post sent ' + post_id) print('Scheduled post sent ' + post_id)

152
utils.py
View File

@ -1256,9 +1256,9 @@ def locateNewsVotes(base_dir: str, domain: str,
postUrl = postUrl + '.json.votes' postUrl = postUrl + '.json.votes'
accountDir = base_dir + '/accounts/news@' + domain + '/' accountDir = base_dir + '/accounts/news@' + domain + '/'
postFilename = accountDir + 'outbox/' + postUrl post_filename = accountDir + 'outbox/' + postUrl
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
return postFilename return post_filename
return None return None
@ -1280,9 +1280,9 @@ def locateNewsArrival(base_dir: str, domain: str,
postUrl = postUrl + '.json.arrived' postUrl = postUrl + '.json.arrived'
accountDir = base_dir + '/accounts/news@' + domain + '/' accountDir = base_dir + '/accounts/news@' + domain + '/'
postFilename = accountDir + 'outbox/' + postUrl post_filename = accountDir + 'outbox/' + postUrl
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
with open(postFilename, 'r') as arrivalFile: with open(post_filename, 'r') as arrivalFile:
arrival = arrivalFile.read() arrival = arrivalFile.read()
if arrival: if arrival:
arrivalDate = \ arrivalDate = \
@ -1306,13 +1306,13 @@ def clearFromPostCaches(base_dir: str, recent_posts_cache: {},
if acct.startswith('inbox@'): if acct.startswith('inbox@'):
continue continue
cacheDir = os.path.join(base_dir + '/accounts', acct) cacheDir = os.path.join(base_dir + '/accounts', acct)
postFilename = cacheDir + filename post_filename = cacheDir + filename
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
try: try:
os.remove(postFilename) os.remove(post_filename)
except OSError: except OSError:
print('EX: clearFromPostCaches file not removed ' + print('EX: clearFromPostCaches file not removed ' +
str(postFilename)) str(post_filename))
# if the post is in the recent posts cache then remove it # if the post is in the recent posts cache then remove it
if recent_posts_cache.get('index'): if recent_posts_cache.get('index'):
if post_id in recent_posts_cache['index']: if post_id in recent_posts_cache['index']:
@ -1345,20 +1345,20 @@ def locate_post(base_dir: str, nickname: str, domain: str,
boxes = ('inbox', 'outbox', 'tlblogs') boxes = ('inbox', 'outbox', 'tlblogs')
accountDir = acct_dir(base_dir, nickname, domain) + '/' accountDir = acct_dir(base_dir, nickname, domain) + '/'
for boxName in boxes: for boxName in boxes:
postFilename = accountDir + boxName + '/' + postUrl post_filename = accountDir + boxName + '/' + postUrl
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
return postFilename return post_filename
# check news posts # check news posts
accountDir = base_dir + '/accounts/news' + '@' + domain + '/' accountDir = base_dir + '/accounts/news' + '@' + domain + '/'
postFilename = accountDir + 'outbox/' + postUrl post_filename = accountDir + 'outbox/' + postUrl
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
return postFilename return post_filename
# is it in the announce cache? # is it in the announce cache?
postFilename = base_dir + '/cache/announce/' + nickname + '/' + postUrl post_filename = base_dir + '/cache/announce/' + nickname + '/' + postUrl
if os.path.isfile(postFilename): if os.path.isfile(post_filename):
return postFilename return post_filename
# print('WARN: unable to locate ' + nickname + ' ' + postUrl) # print('WARN: unable to locate ' + nickname + ' ' + postUrl)
return None return None
@ -1427,10 +1427,10 @@ def canReplyTo(base_dir: str, nickname: str, domain: str,
if '/statuses/' not in postUrl: if '/statuses/' not in postUrl:
return True return True
if not post_json_object: if not post_json_object:
postFilename = locate_post(base_dir, nickname, domain, postUrl) post_filename = locate_post(base_dir, nickname, domain, postUrl)
if not postFilename: if not post_filename:
return False return False
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return False return False
published = _getPublishedDate(post_json_object) published = _getPublishedDate(post_json_object)
@ -1528,15 +1528,15 @@ def _is_reply_to_blog_post(base_dir: str, nickname: str, domain: str,
def _deletePostRemoveReplies(base_dir: str, nickname: str, domain: str, def _deletePostRemoveReplies(base_dir: str, nickname: str, domain: str,
http_prefix: str, postFilename: str, http_prefix: str, post_filename: str,
recent_posts_cache: {}, debug: bool) -> None: recent_posts_cache: {}, debug: bool) -> None:
"""Removes replies when deleting a post """Removes replies when deleting a post
""" """
repliesFilename = postFilename.replace('.json', '.replies') repliesFilename = post_filename.replace('.json', '.replies')
if not os.path.isfile(repliesFilename): if not os.path.isfile(repliesFilename):
return return
if debug: if debug:
print('DEBUG: removing replies to ' + postFilename) print('DEBUG: removing replies to ' + post_filename)
with open(repliesFilename, 'r') as f: with open(repliesFilename, 'r') as f:
for replyId in f: for replyId in f:
replyFile = locate_post(base_dir, nickname, domain, replyId) replyFile = locate_post(base_dir, nickname, domain, replyId)
@ -1555,13 +1555,13 @@ def _deletePostRemoveReplies(base_dir: str, nickname: str, domain: str,
def _isBookmarked(base_dir: str, nickname: str, domain: str, def _isBookmarked(base_dir: str, nickname: str, domain: str,
postFilename: str) -> bool: post_filename: str) -> bool:
"""Returns True if the given post is bookmarked """Returns True if the given post is bookmarked
""" """
bookmarksIndexFilename = \ bookmarksIndexFilename = \
acct_dir(base_dir, nickname, domain) + '/bookmarks.index' acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
if os.path.isfile(bookmarksIndexFilename): if os.path.isfile(bookmarksIndexFilename):
bookmarkIndex = postFilename.split('/')[-1] + '\n' bookmarkIndex = post_filename.split('/')[-1] + '\n'
if bookmarkIndex in open(bookmarksIndexFilename).read(): if bookmarkIndex in open(bookmarksIndexFilename).read():
return True return True
return False return False
@ -1604,7 +1604,7 @@ def _deleteCachedHtml(base_dir: str, nickname: str, domain: str,
"""Removes cached html file for the given post """Removes cached html file for the given post
""" """
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, post_json_object) get_cached_post_filename(base_dir, nickname, domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
try: try:
@ -1712,27 +1712,27 @@ def _deleteConversationPost(base_dir: str, nickname: str, domain: str,
def deletePost(base_dir: str, http_prefix: str, def deletePost(base_dir: str, http_prefix: str,
nickname: str, domain: str, postFilename: str, nickname: str, domain: str, post_filename: str,
debug: bool, recent_posts_cache: {}) -> None: debug: bool, recent_posts_cache: {}) -> None:
"""Recursively deletes a post and its replies and attachments """Recursively deletes a post and its replies and attachments
""" """
post_json_object = load_json(postFilename, 1) post_json_object = load_json(post_filename, 1)
if not post_json_object: if not post_json_object:
# remove any replies # remove any replies
_deletePostRemoveReplies(base_dir, nickname, domain, _deletePostRemoveReplies(base_dir, nickname, domain,
http_prefix, postFilename, http_prefix, post_filename,
recent_posts_cache, debug) recent_posts_cache, debug)
# finally, remove the post itself # finally, remove the post itself
try: try:
os.remove(postFilename) os.remove(post_filename)
except OSError: except OSError:
if debug: if debug:
print('EX: deletePost unable to delete post ' + print('EX: deletePost unable to delete post ' +
str(postFilename)) str(post_filename))
return return
# don't allow deletion of bookmarked posts # don't allow deletion of bookmarked posts
if _isBookmarked(base_dir, nickname, domain, postFilename): if _isBookmarked(base_dir, nickname, domain, post_filename):
return return
# don't remove replies to blog posts # don't remove replies to blog posts
@ -1751,7 +1751,7 @@ def deletePost(base_dir: str, http_prefix: str,
extensions = ('votes', 'arrived', 'muted', 'tts', 'reject') extensions = ('votes', 'arrived', 'muted', 'tts', 'reject')
for ext in extensions: for ext in extensions:
extFilename = postFilename + '.' + ext extFilename = post_filename + '.' + ext
if os.path.isfile(extFilename): if os.path.isfile(extFilename):
try: try:
os.remove(extFilename) os.remove(extFilename)
@ -1780,14 +1780,14 @@ def deletePost(base_dir: str, http_prefix: str,
# remove any replies # remove any replies
_deletePostRemoveReplies(base_dir, nickname, domain, _deletePostRemoveReplies(base_dir, nickname, domain,
http_prefix, postFilename, http_prefix, post_filename,
recent_posts_cache, debug) recent_posts_cache, debug)
# finally, remove the post itself # finally, remove the post itself
try: try:
os.remove(postFilename) os.remove(post_filename)
except OSError: except OSError:
if debug: if debug:
print('EX: deletePost unable to delete post ' + str(postFilename)) print('EX: deletePost unable to delete post ' + str(post_filename))
def isValidLanguage(text: str) -> bool: def isValidLanguage(text: str) -> bool:
@ -1946,10 +1946,10 @@ def isPublicPostFromUrl(base_dir: str, nickname: str, domain: str,
postUrl: str) -> bool: postUrl: str) -> bool:
"""Returns whether the given url is a public post """Returns whether the given url is a public post
""" """
postFilename = locate_post(base_dir, nickname, domain, postUrl) post_filename = locate_post(base_dir, nickname, domain, postUrl)
if not postFilename: if not post_filename:
return False return False
post_json_object = load_json(postFilename, 1) post_json_object = load_json(post_filename, 1)
if not post_json_object: if not post_json_object:
return False return False
return isPublicPost(post_json_object) return isPublicPost(post_json_object)
@ -1991,7 +1991,7 @@ def getCachedPostDirectory(base_dir: str, nickname: str, domain: str) -> str:
return htmlPostCacheDir return htmlPostCacheDir
def getCachedPostFilename(base_dir: str, nickname: str, domain: str, def get_cached_post_filename(base_dir: str, nickname: str, domain: str,
post_json_object: {}) -> str: post_json_object: {}) -> str:
"""Returns the html cache filename for the given post """Returns the html cache filename for the given post
""" """
@ -2124,17 +2124,17 @@ def _searchVirtualBoxPosts(base_dir: str, nickname: str, domain: str,
res = [] res = []
with open(indexFilename, 'r') as indexFile: with open(indexFilename, 'r') as indexFile:
postFilename = 'start' post_filename = 'start'
while postFilename: while post_filename:
postFilename = indexFile.readline() post_filename = indexFile.readline()
if not postFilename: if not post_filename:
break break
if '.json' not in postFilename: if '.json' not in post_filename:
break break
postFilename = path + '/' + postFilename.strip() post_filename = path + '/' + post_filename.strip()
if not os.path.isfile(postFilename): if not os.path.isfile(post_filename):
continue continue
with open(postFilename, 'r') as postFile: with open(post_filename, 'r') as postFile:
data = postFile.read().lower() data = postFile.read().lower()
notFound = False notFound = False
@ -2145,7 +2145,7 @@ def _searchVirtualBoxPosts(base_dir: str, nickname: str, domain: str,
if notFound: if notFound:
continue continue
res.append(postFilename) res.append(post_filename)
if len(res) >= maxResults: if len(res) >= maxResults:
return res return res
return res return res
@ -2208,19 +2208,20 @@ def getFileCaseInsensitive(path: str) -> str:
def undoLikesCollectionEntry(recent_posts_cache: {}, def undoLikesCollectionEntry(recent_posts_cache: {},
base_dir: str, postFilename: str, objectUrl: str, base_dir: str, post_filename: str, objectUrl: str,
actor: str, domain: str, debug: bool, actor: str, domain: str, debug: bool,
post_json_object: {}) -> None: post_json_object: {}) -> None:
"""Undoes a like for a particular actor """Undoes a like for a particular actor
""" """
if not post_json_object: if not post_json_object:
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return return
# remove any cached version of this post so that the # remove any cached version of this post so that the
# like icon is changed # like icon is changed
nickname = getNicknameFromActor(actor) nickname = getNicknameFromActor(actor)
cachedPostFilename = getCachedPostFilename(base_dir, nickname, cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object) domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -2267,11 +2268,11 @@ def undoLikesCollectionEntry(recent_posts_cache: {},
itlen = len(obj['likes']['items']) itlen = len(obj['likes']['items'])
obj['likes']['totalItems'] = itlen obj['likes']['totalItems'] = itlen
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
def undoReactionCollectionEntry(recent_posts_cache: {}, def undoReactionCollectionEntry(recent_posts_cache: {},
base_dir: str, postFilename: str, base_dir: str, post_filename: str,
objectUrl: str, objectUrl: str,
actor: str, domain: str, debug: bool, actor: str, domain: str, debug: bool,
post_json_object: {}, post_json_object: {},
@ -2279,13 +2280,14 @@ def undoReactionCollectionEntry(recent_posts_cache: {},
"""Undoes an emoji reaction for a particular actor """Undoes an emoji reaction for a particular actor
""" """
if not post_json_object: if not post_json_object:
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return return
# remove any cached version of this post so that the # remove any cached version of this post so that the
# like icon is changed # like icon is changed
nickname = getNicknameFromActor(actor) nickname = getNicknameFromActor(actor)
cachedPostFilename = getCachedPostFilename(base_dir, nickname, cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object) domain, post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -2333,24 +2335,25 @@ def undoReactionCollectionEntry(recent_posts_cache: {},
itlen = len(obj['reactions']['items']) itlen = len(obj['reactions']['items'])
obj['reactions']['totalItems'] = itlen obj['reactions']['totalItems'] = itlen
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
def undoAnnounceCollectionEntry(recent_posts_cache: {}, def undoAnnounceCollectionEntry(recent_posts_cache: {},
base_dir: str, postFilename: str, base_dir: str, post_filename: str,
actor: str, domain: str, debug: bool) -> None: actor: str, domain: str, debug: bool) -> None:
"""Undoes an announce for a particular actor by removing it from """Undoes an announce for a particular actor by removing it from
the "shares" collection within a post. Note that the "shares" the "shares" collection within a post. Note that the "shares"
collection has no relation to shared items in shares.py. It's collection has no relation to shared items in shares.py. It's
shares of posts, not shares of physical objects. shares of posts, not shares of physical objects.
""" """
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return return
# remove any cached version of this announce so that the announce # remove any cached version of this announce so that the announce
# icon is changed # icon is changed
nickname = getNicknameFromActor(actor) nickname = getNicknameFromActor(actor)
cachedPostFilename = getCachedPostFilename(base_dir, nickname, domain, cachedPostFilename = \
get_cached_post_filename(base_dir, nickname, domain,
post_json_object) post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -2400,24 +2403,25 @@ def undoAnnounceCollectionEntry(recent_posts_cache: {},
itlen = len(post_json_object['object']['shares']['items']) itlen = len(post_json_object['object']['shares']['items'])
post_json_object['object']['shares']['totalItems'] = itlen post_json_object['object']['shares']['totalItems'] = itlen
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
def updateAnnounceCollection(recent_posts_cache: {}, def update_announce_collection(recent_posts_cache: {},
base_dir: str, postFilename: str, base_dir: str, post_filename: str,
actor: str, actor: str, nickname: str, domain: str,
nickname: str, domain: str, debug: bool) -> None: debug: bool) -> None:
"""Updates the announcements collection within a post """Updates the announcements collection within a post
Confusingly this is known as "shares", but isn't the Confusingly this is known as "shares", but isn't the
same as shared items within shares.py same as shared items within shares.py
It's shares of posts, not shares of physical objects. It's shares of posts, not shares of physical objects.
""" """
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return return
# remove any cached version of this announce so that the announce # remove any cached version of this announce so that the announce
# icon is changed # icon is changed
cachedPostFilename = getCachedPostFilename(base_dir, nickname, domain, cachedPostFilename = \
get_cached_post_filename(base_dir, nickname, domain,
post_json_object) post_json_object)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
@ -2425,7 +2429,7 @@ def updateAnnounceCollection(recent_posts_cache: {},
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
except OSError: except OSError:
if debug: if debug:
print('EX: updateAnnounceCollection ' + print('EX: update_announce_collection ' +
'unable to delete cached post ' + 'unable to delete cached post ' +
str(cachedPostFilename)) str(cachedPostFilename))
removePostFromCache(post_json_object, recent_posts_cache) removePostFromCache(post_json_object, recent_posts_cache)
@ -2433,7 +2437,7 @@ def updateAnnounceCollection(recent_posts_cache: {},
if not has_object_dict(post_json_object): if not has_object_dict(post_json_object):
if debug: if debug:
pprint(post_json_object) pprint(post_json_object)
print('DEBUG: post ' + postFilename + ' has no object') print('DEBUG: post ' + post_filename + ' has no object')
return return
postUrl = removeIdEnding(post_json_object['id']) + '/shares' postUrl = removeIdEnding(post_json_object['id']) + '/shares'
if not post_json_object['object'].get('shares'): if not post_json_object['object'].get('shares'):
@ -2473,7 +2477,7 @@ def updateAnnounceCollection(recent_posts_cache: {},
if debug: if debug:
print('DEBUG: saving post with shares (announcements) added') print('DEBUG: saving post with shares (announcements) added')
pprint(post_json_object) pprint(post_json_object)
save_json(post_json_object, postFilename) save_json(post_json_object, post_filename)
def week_day_of_month_start(month_number: int, year: int) -> int: def week_day_of_month_start(month_number: int, year: int) -> int:
@ -2534,9 +2538,9 @@ def is_recent_post(post_json_object: {}, max_days: int) -> bool:
str(published_date_str)) str(published_date_str))
return False return False
publishedDaysSinceEpoch = \ published_days_since_epoch = \
(published_date - datetime.datetime(1970, 1, 1)).days (published_date - datetime.datetime(1970, 1, 1)).days
if publishedDaysSinceEpoch < recently: if published_days_since_epoch < recently:
return False return False
return True return True

View File

@ -43,11 +43,11 @@ def htmlCalendarDeleteConfirm(cssCache: {}, translate: {}, base_dir: str,
domain, port = getDomainFromActor(actor) domain, port = getDomainFromActor(actor)
messageId = actor + '/statuses/' + post_id messageId = actor + '/statuses/' + post_id
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
return None return None
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return None return None

View File

@ -660,10 +660,10 @@ def htmlEditNewsPost(cssCache: {}, translate: {}, base_dir: str, path: str,
return '' return ''
postUrl = postUrl.replace('/', '#') postUrl = postUrl.replace('/', '#')
postFilename = locate_post(base_dir, nickname, domain, postUrl) post_filename = locate_post(base_dir, nickname, domain, postUrl)
if not postFilename: if not post_filename:
return '' return ''
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return '' return ''

View File

@ -47,11 +47,11 @@ def htmlConfirmDelete(cssCache: {},
domain, port = getDomainFromActor(actor) domain, port = getDomainFromActor(actor)
domain_full = get_full_domain(domain, port) domain_full = get_full_domain(domain, port)
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if not postFilename: if not post_filename:
return None return None
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
return None return None

View File

@ -29,7 +29,7 @@ from utils import get_actor_languages_list
from utils import get_base_content_from_post from utils import get_base_content_from_post
from utils import get_content_from_post from utils import get_content_from_post
from utils import has_object_dict from utils import has_object_dict
from utils import updateAnnounceCollection from utils import update_announce_collection
from utils import is_pgp_encrypted from utils import is_pgp_encrypted
from utils import is_dm from utils import is_dm
from utils import reject_post_id from utils import reject_post_id
@ -40,7 +40,7 @@ from utils import is_editor
from utils import locate_post from utils import locate_post
from utils import load_json from utils import load_json
from utils import getCachedPostDirectory from utils import getCachedPostDirectory
from utils import getCachedPostFilename from utils import get_cached_post_filename
from utils import getProtocolPrefixes from utils import getProtocolPrefixes
from utils import isNewsPost from utils import isNewsPost
from utils import isBlogPost from utils import isBlogPost
@ -252,7 +252,7 @@ def _saveIndividualPostAsHtmlToCache(base_dir: str,
htmlPostCacheDir = \ htmlPostCacheDir = \
getCachedPostDirectory(base_dir, nickname, domain) getCachedPostDirectory(base_dir, nickname, domain)
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, post_json_object) get_cached_post_filename(base_dir, nickname, domain, post_json_object)
# create the cache directory if needed # create the cache directory if needed
if not os.path.isdir(htmlPostCacheDir): if not os.path.isdir(htmlPostCacheDir):
@ -1539,9 +1539,10 @@ def individualPostAsHtml(signing_priv_key_pem: str,
announceFilename = \ announceFilename = \
locate_post(base_dir, nickname, domain, post_json_object['id']) locate_post(base_dir, nickname, domain, post_json_object['id'])
if announceFilename: if announceFilename:
updateAnnounceCollection(recent_posts_cache, update_announce_collection(recent_posts_cache,
base_dir, announceFilename, base_dir, announceFilename,
postActor, nickname, domain_full, False) postActor, nickname,
domain_full, False)
# create a file for use by text-to-speech # create a file for use by text-to-speech
if is_recent_post(post_json_object, 3): if is_recent_post(post_json_object, 3):
@ -2078,12 +2079,12 @@ def htmlIndividualPost(cssCache: {},
# show the previous posts # show the previous posts
if has_object_dict(post_json_object): if has_object_dict(post_json_object):
while post_json_object['object'].get('inReplyTo'): while post_json_object['object'].get('inReplyTo'):
postFilename = \ post_filename = \
locate_post(base_dir, nickname, domain, locate_post(base_dir, nickname, domain,
post_json_object['object']['inReplyTo']) post_json_object['object']['inReplyTo'])
if not postFilename: if not post_filename:
break break
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if post_json_object: if post_json_object:
postStr = \ postStr = \
individualPostAsHtml(signing_priv_key_pem, individualPostAsHtml(signing_priv_key_pem,
@ -2108,10 +2109,10 @@ def htmlIndividualPost(cssCache: {},
cw_lists, lists_enabled) + postStr cw_lists, lists_enabled) + postStr
# show the following posts # show the following posts
postFilename = locate_post(base_dir, nickname, domain, messageId) post_filename = locate_post(base_dir, nickname, domain, messageId)
if postFilename: if post_filename:
# is there a replies file for this post? # is there a replies file for this post?
repliesFilename = postFilename.replace('.json', '.replies') repliesFilename = post_filename.replace('.json', '.replies')
if os.path.isfile(repliesFilename): if os.path.isfile(repliesFilename):
# get items from the replies file # get items from the replies file
repliesJson = { repliesJson = {

View File

@ -662,11 +662,11 @@ def htmlHistorySearch(cssCache: {}, translate: {}, base_dir: str,
index = startIndex index = startIndex
while index <= endIndex: while index <= endIndex:
postFilename = boxFilenames[index] post_filename = boxFilenames[index]
if not postFilename: if not post_filename:
index += 1 index += 1
continue continue
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
index += 1 index += 1
continue continue
@ -831,11 +831,11 @@ def htmlHashtagSearch(cssCache: {},
continue continue
nickname = postFields[1] nickname = postFields[1]
post_id = postFields[2] post_id = postFields[2]
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
index += 1 index += 1
continue continue
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if not post_json_object: if not post_json_object:
index += 1 index += 1
continue continue
@ -956,13 +956,13 @@ def rssHashtagSearch(nickname: str, domain: str, port: int,
continue continue
nickname = postFields[1] nickname = postFields[1]
post_id = postFields[2] post_id = postFields[2]
postFilename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
if not postFilename: if not post_filename:
index += 1 index += 1
if index >= maxFeedLength: if index >= maxFeedLength:
break break
continue continue
post_json_object = load_json(postFilename) post_json_object = load_json(post_filename)
if post_json_object: if post_json_object:
if not isPublicPost(post_json_object): if not isPublicPost(post_json_object):
index += 1 index += 1

View File

@ -15,7 +15,7 @@ from utils import is_account_dir
from utils import removeHtml from utils import removeHtml
from utils import getProtocolPrefixes from utils import getProtocolPrefixes
from utils import load_json from utils import load_json
from utils import getCachedPostFilename from utils import get_cached_post_filename
from utils import get_config_param from utils import get_config_param
from utils import acct_dir from utils import acct_dir
from utils import getNicknameFromActor from utils import getNicknameFromActor
@ -864,7 +864,7 @@ def loadIndividualPostAsHtmlFromCache(base_dir: str,
This is much quicker than generating the html from the json object This is much quicker than generating the html from the json object
""" """
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(base_dir, nickname, domain, post_json_object) get_cached_post_filename(base_dir, nickname, domain, post_json_object)
postHtml = '' postHtml = ''
if not cachedPostFilename: if not cachedPostFilename: