When deleting a post also remove it from the recent posts cache in memory

main
Bob Mottram 2020-06-24 14:30:50 +01:00
parent f5be8eb132
commit 0e3c41db2b
8 changed files with 38 additions and 17 deletions

View File

@ -6671,7 +6671,8 @@ class PubServer(BaseHTTPRequestHandler):
self.server.httpPrefix,
nickname, self.server.domain,
postFilename,
self.server.debug)
self.server.debug,
self.server.recentPostsCache)
if callingDomain.endswith('.onion') and \
self.server.onionDomain:
actorStr = \

View File

@ -227,7 +227,8 @@ def deletePostPub(session, baseDir: str, federationList: [],
def outboxDelete(baseDir: str, httpPrefix: str,
nickname: str, domain: str,
messageJson: {}, debug: bool,
allowDeletion: bool) -> None:
allowDeletion: bool,
recentPostsCache: {}) -> None:
""" When a delete request is received by the outbox from c2s
"""
if not messageJson.get('type'):
@ -289,6 +290,6 @@ def outboxDelete(baseDir: str, httpPrefix: str,
print(messageId)
return True
deletePost(baseDir, httpPrefix, deleteNickname, deleteDomain,
postFilename, debug)
postFilename, debug, recentPostsCache)
if debug:
print('DEBUG: post deleted via c2s - ' + postFilename)

View File

@ -1313,7 +1313,7 @@ if args.archive:
else:
print('Archiving to ' + args.archive + '...')
archiveMedia(baseDir, args.archive, archiveWeeks)
archivePosts(baseDir, httpPrefix, args.archive, archiveMaxPosts)
archivePosts(baseDir, httpPrefix, args.archive, {}, archiveMaxPosts)
print('Archiving complete')
sys.exit()

View File

@ -1240,7 +1240,8 @@ def receiveDelete(session, handle: str, isGroup: bool, baseDir: str,
httpPrefix: str, domain: str, port: int,
sendThreads: [], postLog: [], cachedWebfingers: {},
personCache: {}, messageJson: {}, federationList: [],
debug: bool, allowDeletion: bool) -> bool:
debug: bool, allowDeletion: bool,
recentPostsCache: {}) -> bool:
"""Receives a Delete activity within the POST section of HTTPServer
"""
if messageJson['type'] != 'Delete':
@ -1305,7 +1306,8 @@ def receiveDelete(session, handle: str, isGroup: bool, baseDir: str,
print(messageId)
return True
deletePost(baseDir, httpPrefix, handle.split('@')[0],
handle.split('@')[1], postFilename, debug)
handle.split('@')[1], postFilename, debug,
recentPostsCache)
if debug:
print('DEBUG: post deleted - ' + postFilename)
return True
@ -2045,7 +2047,8 @@ def inboxAfterCapabilities(recentPostsCache: {}, maxRecentPosts: int,
personCache,
messageJson,
federationList,
debug, allowDeletion):
debug, allowDeletion,
recentPostsCache):
if debug:
print('DEBUG: Delete accepted from ' + actor)
return False

View File

@ -299,7 +299,8 @@ def postMessageToOutbox(messageJson: {}, postToNickname: str,
outboxDelete(baseDir, httpPrefix,
postToNickname, domain,
messageJson, debug,
allowDeletion)
allowDeletion,
recentPostsCache)
if debug:
print('DEBUG: handle block requests')

View File

@ -2718,17 +2718,21 @@ def createBoxIndexed(recentPostsCache: {},
def expireCache(baseDir: str, personCache: {},
httpPrefix: str, archiveDir: str, maxPostsInBox=32000):
httpPrefix: str, archiveDir: str,
recentPostsCache: {},
maxPostsInBox=32000):
"""Thread used to expire actors from the cache and archive old posts
"""
while True:
# once per day
time.sleep(60 * 60 * 24)
expirePersonCache(baseDir, personCache)
archivePosts(baseDir, httpPrefix, archiveDir, maxPostsInBox)
archivePosts(baseDir, httpPrefix, archiveDir, recentPostsCache,
maxPostsInBox)
def archivePosts(baseDir: str, httpPrefix: str, archiveDir: str,
recentPostsCache: {},
maxPostsInBox=32000) -> None:
"""Archives posts for all accounts
"""
@ -2760,18 +2764,19 @@ def archivePosts(baseDir: str, httpPrefix: str, archiveDir: str,
handle + '/inbox'
archivePostsForPerson(httpPrefix, nickname, domain, baseDir,
'inbox', archiveSubdir,
maxPostsInBox)
recentPostsCache, maxPostsInBox)
if archiveDir:
archiveSubdir = archiveDir + '/accounts/' + \
handle + '/outbox'
archivePostsForPerson(httpPrefix, nickname, domain, baseDir,
'outbox', archiveSubdir,
maxPostsInBox)
recentPostsCache, maxPostsInBox)
def archivePostsForPerson(httpPrefix: str, nickname: str, domain: str,
baseDir: str,
boxname: str, archiveDir: str,
recentPostsCache: {},
maxPostsInBox=32000) -> None:
"""Retain a maximum number of posts within the given box
Move any others to an archive directory
@ -2855,7 +2860,8 @@ def archivePostsForPerson(httpPrefix: str, nickname: str, domain: str,
if os.path.isfile(repliesPath):
os.rename(repliesPath, archivePath)
else:
deletePost(baseDir, httpPrefix, nickname, domain, filePath, False)
deletePost(baseDir, httpPrefix, nickname, domain,
filePath, False, recentPostsCache)
# remove cached html posts
postCacheFilename = \

View File

@ -1076,8 +1076,8 @@ def testCreatePerson():
deleteAllPosts(baseDir, nickname, domain, 'outbox')
setDisplayNickname(baseDir, nickname, domain, 'badger')
setBio(baseDir, nickname, domain, 'Randomly roaming in your backyard')
archivePostsForPerson(nickname, domain, baseDir, 'inbox', None, 4)
archivePostsForPerson(nickname, domain, baseDir, 'outbox', None, 4)
archivePostsForPerson(nickname, domain, baseDir, 'inbox', None, {}, 4)
archivePostsForPerson(nickname, domain, baseDir, 'outbox', None, {}, 4)
createPublicPost(baseDir, nickname, domain, port, httpPrefix,
"G'day world!", False, True, clientToServer,
None, None, useBlurhash, None, None,

View File

@ -444,7 +444,7 @@ def isReplyToBlogPost(baseDir: str, nickname: str, domain: str,
def deletePost(baseDir: str, httpPrefix: str,
nickname: str, domain: str, postFilename: str,
debug: bool) -> None:
debug: bool, recentPostsCache: {}) -> None:
"""Recursively deletes a post and its replies and attachments
"""
postJsonObject = loadJson(postFilename, 1)
@ -463,6 +463,14 @@ def deletePost(baseDir: str, httpPrefix: str,
postJsonObject):
return
# remove from recent posts cache in memory
postId = \
postJsonObject['id'].replace('/activity', '').replace('/', '#')
if postId in recentPostsCache['index']:
recentPostsCache['index'].remove(postId)
if recentPostsCache['json'].get(postId):
del recentPostsCache['json'][postId]
# remove any attachment
removeAttachment(baseDir, httpPrefix, domain, postJsonObject)
@ -543,7 +551,8 @@ def deletePost(baseDir: str, httpPrefix: str,
if replyFile:
if os.path.isfile(replyFile):
deletePost(baseDir, httpPrefix,
nickname, domain, replyFile, debug)
nickname, domain, replyFile, debug,
recentPostsCache)
# remove the replies file
os.remove(repliesFilename)
# finally, remove the post itself