From f6f35d69d68dadf3c499797eecf5297e549d42c0 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 24 Nov 2019 13:46:28 +0000 Subject: [PATCH] Update announce posts in cache --- announce.py | 14 ++++++++++---- daemon.py | 3 ++- like.py | 10 +++------- utils.py | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/announce.py b/announce.py index 02e3f4ac..0ce17a7b 100644 --- a/announce.py +++ b/announce.py @@ -10,6 +10,7 @@ import os import time import json from pprint import pprint +from utils import removePostFromCache from utils import getStatusNumber from utils import createOutboxDir from utils import urlPermitted @@ -25,7 +26,8 @@ from session import postJson from webfinger import webfingerHandle from auth import createBasicAuthHeader -def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool: +def outboxAnnounce(recentPostsCache: {}, \ + baseDir: str,messageJson: {},debug: bool) -> bool: """ Adds or removes announce entries from the shares collection within a given post """ @@ -45,7 +47,7 @@ def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool: domain,port=getDomainFromActor(messageJson['actor']) postFilename=locatePost(baseDir,nickname,domain,messageJson['object']) if postFilename: - updateAnnounceCollection(baseDir,postFilename, \ + updateAnnounceCollection(recentPostsCache,baseDir,postFilename, \ messageJson['actor'],domain,debug) return True if messageJson['type']=='Undo': @@ -65,13 +67,15 @@ def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool: locatePost(baseDir,nickname,domain, \ messageJson['object']['object']) if postFilename: - undoAnnounceCollectionEntry(baseDir,postFilename, \ + undoAnnounceCollectionEntry(recentPostsCache, \ + baseDir,postFilename, \ messageJson['actor'], \ domain,debug) return True return False -def undoAnnounceCollectionEntry(baseDir: str,postFilename: str, \ +def undoAnnounceCollectionEntry(recentPostsCache: {}, \ + baseDir: str,postFilename: str, \ actor: str,domain: str,debug: bool) -> None: """Undoes an announce for a particular actor by removing it from the "shares" collection within a post. Note that the "shares" @@ -86,6 +90,7 @@ def undoAnnounceCollectionEntry(baseDir: str,postFilename: str, \ getCachedPostFilename(baseDir,nickname,domain,postJsonObject) if os.path.isfile(cachedPostFilename): os.remove(cachedPostFilename) + removePostFromCache(postJsonObject,recentPostsCache) if not postJsonObject.get('type'): return @@ -140,6 +145,7 @@ def updateAnnounceCollection(baseDir: str,postFilename: str, \ getCachedPostFilename(baseDir,nickname,domain,postJsonObject) if os.path.isfile(cachedPostFilename): os.remove(cachedPostFilename) + removePostFromCache(postJsonObject,recentPostsCache) if not postJsonObject.get('object'): if debug: diff --git a/daemon.py b/daemon.py index 0a35d6bf..ce6705c8 100644 --- a/daemon.py +++ b/daemon.py @@ -601,7 +601,8 @@ class PubServer(BaseHTTPRequestHandler): inboxUpdateIndex('outbox',self.server.baseDir, \ self.postToNickname+'@'+self.server.domain, \ savedFilename,self.server.debug) - if outboxAnnounce(self.server.baseDir,messageJson,self.server.debug): + if outboxAnnounce(self.server.recentPostsCache, \ + self.server.baseDir,messageJson,self.server.debug): if self.server.debug: print('DEBUG: Updated announcements (shares) collection for the post associated with the Announce activity') if not self.server.session: diff --git a/like.py b/like.py index 6674c334..16173beb 100644 --- a/like.py +++ b/like.py @@ -10,6 +10,7 @@ import os import json import time from pprint import pprint +from utils import removePostFromCache from utils import urlPermitted from utils import getNicknameFromActor from utils import getDomainFromActor @@ -35,6 +36,7 @@ def undoLikesCollectionEntry(baseDir: str,postFilename: str,objectUrl: str, \ getCachedPostFilename(baseDir,nickname,domain,postJsonObject) if os.path.isfile(cachedPostFilename): os.remove(cachedPostFilename) + removePostFromCache(postJsonObject,recentPostsCache) if not postJsonObject.get('type'): return @@ -116,13 +118,7 @@ def updateLikesCollection(recentPostsCache: {}, \ getCachedPostFilename(baseDir,nickname,domain,postJsonObject) if os.path.isfile(cachedPostFilename): os.remove(cachedPostFilename) - # if the post exists in the recent posts cache then remove it - if postJsonObject.get('id') and recentPostsCache.get('index'): - postId=postJsonObject['id'].replace('/activity','').replace('/','#') - if postId in recentPostsCache['index']: - del recentPostsCache['json'][postId] - del recentPostsCache['html'][postId] - recentPostsCache['index'].remove(postId) + removePostFromCache(postJsonObject,recentPostsCache) if not postJsonObject.get('object'): if debug: diff --git a/utils.py b/utils.py index b4b48bc6..55b102ca 100644 --- a/utils.py +++ b/utils.py @@ -451,3 +451,20 @@ def getCachedPostFilename(baseDir: str,nickname: str,domain: str, \ getCachedPostDirectory(baseDir,nickname,domain)+ \ '/'+postJsonObject['id'].replace('/activity','').replace('/','#')+'.html' return cachedPostFilename + +def removePostFromCache(postJsonObject: {},recentPostsCache: {}): + """ if the post exists in the recent posts cache then remove it + """ + if not postJsonObject.get('id'): + return + + if not recentPostsCache.get('index'): + return + + postId=postJsonObject['id'].replace('/activity','').replace('/','#') + if postId not in recentPostsCache['index']: + return + + del recentPostsCache['json'][postId] + del recentPostsCache['html'][postId] + recentPostsCache['index'].remove(postId)