diff --git a/inbox.py b/inbox.py index 74e0918c..bfc99738 100644 --- a/inbox.py +++ b/inbox.py @@ -20,6 +20,7 @@ from utils import getDomainFromActor from utils import getNicknameFromActor from utils import domainPermitted from utils import locatePost +from utils import deletePost from httpsig import verifyPostHeaders from session import createSession from session import getJson @@ -489,23 +490,6 @@ def receiveUndoLike(session,handle: str,baseDir: str, \ undoLikesCollectionEntry(postFilename,messageJson['object'],messageJson['actor'],debug) return True -def deletePost(baseDir: str,nickname: str,domain: str,postFilename: str,debug: bool): - """ - """ - repliesFilename=postFilename.replace('.json','.replies') - if os.path.isfile(repliesFilename): - 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 replyFile: - if os.path.isfile(replyFile): - os.remove(replyFile) - # remove the replies file itself - os.remove(repliesFilename) - os.remove(postFilename) - def receiveDelete(session,handle: str,baseDir: str, \ httpPrefix: str,domain :str,port: int, \ sendThreads: [],postLog: [],cachedWebfingers: {}, \ diff --git a/posts.py b/posts.py index 7e8b76fd..5b962a93 100644 --- a/posts.py +++ b/posts.py @@ -33,6 +33,7 @@ from utils import createPersonDir from utils import urlPermitted from utils import getNicknameFromActor from utils import getDomainFromActor +from utils import deletePost from capabilities import getOcapFilename from capabilities import capabilitiesUpdate from media import attachImage @@ -999,16 +1000,14 @@ def archivePostsForPerson(nickname: str,domain: str,baseDir: str, \ for postFilename in postsInBox: filePath = os.path.join(boxDir, postFilename) if os.path.isfile(filePath): - repliesPath=filePath.replace('.json','.replies') if archiveDir: + repliesPath=filePath.replace('.json','.replies') archivePath = os.path.join(archiveDir, postFilename) os.rename(filePath,archivePath) if os.path.isfile(repliesPath): os.rename(repliesPath,archivePath) else: - os.remove(filePath) - if os.path.isfile(repliesPath): - os.remove(repliesPath) + deletePost(baseDir,nickname,domain,filePath,False) noOfPosts -= 1 if noOfPosts <= maxPostsInBox: break diff --git a/utils.py b/utils.py index ec266cab..f5c1afdb 100644 --- a/utils.py +++ b/utils.py @@ -141,3 +141,20 @@ def locatePost(baseDir: str,nickname: str,domain: str,postUrl: str,replies=False if not os.path.isfile(postFilename): postFilename=None return postFilename + +def deletePost(baseDir: str,nickname: str,domain: str,postFilename: str,debug: bool): + """Recursively deletes a post and its replies and attachments + """ + repliesFilename=postFilename.replace('.json','.replies') + if os.path.isfile(repliesFilename): + 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 replyFile: + if os.path.isfile(replyFile): + deletePost(baseDir,nickname,domain,replyFile,debug) + # remove the replies file itself + os.remove(repliesFilename) + os.remove(postFilename)