Tidying of post deletion

main
Bob Mottram 2021-07-05 10:24:29 +01:00
parent 0335e52458
commit 021556ccee
1 changed files with 132 additions and 103 deletions

235
utils.py
View File

@ -1228,125 +1228,154 @@ def _isReplyToBlogPost(baseDir: str, nickname: str, domain: str,
return False return False
def _deletePostRemoveReplies(baseDir: str, nickname: str, domain: str,
httpPrefix: str, postFilename: str,
recentPostsCache: {}, debug: bool) -> None:
"""Removes replies when deleting a post
"""
repliesFilename = postFilename.replace('.json', '.replies')
if not os.path.isfile(repliesFilename):
return
if debug:
print('DEBUG: removing replies to ' + postFilename)
with open(repliesFilename, 'r') as f:
for replyId in f:
replyFile = locatePost(baseDir, nickname, domain, replyId)
if not replyFile:
continue
if os.path.isfile(replyFile):
deletePost(baseDir, httpPrefix,
nickname, domain, replyFile, debug,
recentPostsCache)
# remove the replies file
os.remove(repliesFilename)
def _isBookmarked(baseDir: str, nickname: str, domain: str,
postFilename: str) -> bool:
"""Returns True if the given post is bookmarked
"""
bookmarksIndexFilename = \
baseDir + '/accounts/' + nickname + '@' + domain + \
'/bookmarks.index'
if os.path.isfile(bookmarksIndexFilename):
bookmarkIndex = postFilename.split('/')[-1] + '\n'
if bookmarkIndex in open(bookmarksIndexFilename).read():
return True
return False
def deletePost(baseDir: str, httpPrefix: str, def deletePost(baseDir: str, httpPrefix: str,
nickname: str, domain: str, postFilename: str, nickname: str, domain: str, postFilename: str,
debug: bool, recentPostsCache: {}) -> None: debug: bool, recentPostsCache: {}) -> None:
"""Recursively deletes a post and its replies and attachments """Recursively deletes a post and its replies and attachments
""" """
postJsonObject = loadJson(postFilename, 1) postJsonObject = loadJson(postFilename, 1)
if postJsonObject: if not postJsonObject:
# don't allow deletion of bookmarked posts # remove any replies
bookmarksIndexFilename = \ _deletePostRemoveReplies(baseDir, nickname, domain,
baseDir + '/accounts/' + nickname + '@' + domain + \ httpPrefix, postFilename,
'/bookmarks.index' recentPostsCache, debug)
if os.path.isfile(bookmarksIndexFilename): # finally, remove the post itself
bookmarkIndex = postFilename.split('/')[-1] + '\n' os.remove(postFilename)
if bookmarkIndex in open(bookmarksIndexFilename).read(): return
return
# don't remove replies to blog posts # don't allow deletion of bookmarked posts
if _isReplyToBlogPost(baseDir, nickname, domain, if _isBookmarked(baseDir, nickname, domain, postFilename):
postJsonObject): return
return
# remove from recent posts cache in memory # don't remove replies to blog posts
if recentPostsCache: if _isReplyToBlogPost(baseDir, nickname, domain,
postId = \ postJsonObject):
removeIdEnding(postJsonObject['id']).replace('/', '#') return
if recentPostsCache.get('index'):
if postId in recentPostsCache['index']:
recentPostsCache['index'].remove(postId)
if recentPostsCache.get('json'):
if recentPostsCache['json'].get(postId):
del recentPostsCache['json'][postId]
if recentPostsCache.get('html'):
if recentPostsCache['html'].get(postId):
del recentPostsCache['html'][postId]
# remove any attachment # remove from recent posts cache in memory
_removeAttachment(baseDir, httpPrefix, domain, postJsonObject) if recentPostsCache:
postId = \
removeIdEnding(postJsonObject['id']).replace('/', '#')
if recentPostsCache.get('index'):
if postId in recentPostsCache['index']:
recentPostsCache['index'].remove(postId)
if recentPostsCache.get('json'):
if recentPostsCache['json'].get(postId):
del recentPostsCache['json'][postId]
if recentPostsCache.get('html'):
if recentPostsCache['html'].get(postId):
del recentPostsCache['html'][postId]
extensions = ('votes', 'arrived', 'muted', 'tts', 'reject') # remove any attachment
for ext in extensions: _removeAttachment(baseDir, httpPrefix, domain, postJsonObject)
extFilename = postFilename + '.' + ext
if os.path.isfile(extFilename):
os.remove(extFilename)
# remove cached html version of the post extensions = ('votes', 'arrived', 'muted', 'tts', 'reject')
cachedPostFilename = \ for ext in extensions:
getCachedPostFilename(baseDir, nickname, domain, postJsonObject) extFilename = postFilename + '.' + ext
if cachedPostFilename: if os.path.isfile(extFilename):
if os.path.isfile(cachedPostFilename): os.remove(extFilename)
os.remove(cachedPostFilename)
# removePostFromCache(postJsonObject,recentPostsCache)
hasObject = False # remove cached html version of the post
if postJsonObject.get('object'): cachedPostFilename = \
hasObject = True getCachedPostFilename(baseDir, nickname, domain, postJsonObject)
if cachedPostFilename:
if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename)
# removePostFromCache(postJsonObject,recentPostsCache)
# remove from moderation index file hasObject = False
if hasObject: if postJsonObject.get('object'):
if isinstance(postJsonObject['object'], dict): hasObject = True
if postJsonObject['object'].get('moderationStatus'):
if postJsonObject.get('id'):
postId = removeIdEnding(postJsonObject['id'])
removeModerationPostFromIndex(baseDir, postId, debug)
# remove any hashtags index entries # remove from moderation index file
removeHashtagIndex = False if hasObject:
if hasObject: if isinstance(postJsonObject['object'], dict):
if hasObject and isinstance(postJsonObject['object'], dict): if postJsonObject['object'].get('moderationStatus'):
if postJsonObject['object'].get('content'): if postJsonObject.get('id'):
if '#' in postJsonObject['object']['content']: postId = removeIdEnding(postJsonObject['id'])
removeHashtagIndex = True removeModerationPostFromIndex(baseDir, postId, debug)
if removeHashtagIndex:
if postJsonObject['object'].get('id') and \ # remove any hashtags index entries
postJsonObject['object'].get('tag'): removeHashtagIndex = False
# get the id of the post if hasObject:
postId = removeIdEnding(postJsonObject['object']['id']) if hasObject and isinstance(postJsonObject['object'], dict):
for tag in postJsonObject['object']['tag']: if postJsonObject['object'].get('content'):
if tag['type'] != 'Hashtag': if '#' in postJsonObject['object']['content']:
continue removeHashtagIndex = True
if not tag.get('name'): if removeHashtagIndex:
continue if postJsonObject['object'].get('id') and \
# find the index file for this tag postJsonObject['object'].get('tag'):
tagIndexFilename = \ # get the id of the post
baseDir + '/tags/' + tag['name'][1:] + '.txt' postId = removeIdEnding(postJsonObject['object']['id'])
if not os.path.isfile(tagIndexFilename): for tag in postJsonObject['object']['tag']:
continue if tag['type'] != 'Hashtag':
# remove postId from the tag index file continue
lines = None if not tag.get('name'):
with open(tagIndexFilename, "r") as f: continue
lines = f.readlines() # find the index file for this tag
if lines: tagIndexFilename = \
newlines = '' baseDir + '/tags/' + tag['name'][1:] + '.txt'
for fileLine in lines: if not os.path.isfile(tagIndexFilename):
if postId in fileLine: continue
continue # remove postId from the tag index file
newlines += fileLine lines = None
if not newlines.strip(): with open(tagIndexFilename, "r") as f:
# if there are no lines then remove the lines = f.readlines()
# hashtag file if lines:
os.remove(tagIndexFilename) newlines = ''
else: for fileLine in lines:
with open(tagIndexFilename, "w+") as f: if postId in fileLine:
f.write(newlines) continue
newlines += fileLine
if not newlines.strip():
# if there are no lines then remove the
# hashtag file
os.remove(tagIndexFilename)
else:
with open(tagIndexFilename, "w+") as f:
f.write(newlines)
# remove any replies # remove any replies
repliesFilename = postFilename.replace('.json', '.replies') _deletePostRemoveReplies(baseDir, nickname, domain,
if os.path.isfile(repliesFilename): httpPrefix, postFilename,
if debug: recentPostsCache, debug)
print('DEBUG: removing replies to ' + postFilename)
with open(repliesFilename, 'r') as f:
for replyId in f:
replyFile = locatePost(baseDir, nickname, domain, replyId)
if replyFile:
if os.path.isfile(replyFile):
deletePost(baseDir, httpPrefix,
nickname, domain, replyFile, debug,
recentPostsCache)
# remove the replies file
os.remove(repliesFilename)
# finally, remove the post itself # finally, remove the post itself
os.remove(postFilename) os.remove(postFilename)