diff --git a/cache.py b/cache.py index f711df01b..19cda4084 100644 --- a/cache.py +++ b/cache.py @@ -8,13 +8,14 @@ __status__ = "Production" import os import datetime +from session import urlExists from utils import loadJson from utils import saveJson from utils import getFileCaseInsensitive -def removePersonFromCache(baseDir: str, personUrl: str, - personCache: {}) -> bool: +def _removePersonFromCache(baseDir: str, personUrl: str, + personCache: {}) -> bool: """Removes an actor from the cache """ cacheFilename = baseDir + '/cache/actors/' + \ @@ -28,6 +29,24 @@ def removePersonFromCache(baseDir: str, personUrl: str, del personCache[personUrl] +def checkForChangedActor(session, baseDir: str, + httpPrefix: str, domainFull: str, + personUrl: str, avatarUrl: str, personCache: {}, + timeoutSec: int): + """Checks if the avatar url exists and if not then + the actor has probably changed without receiving an actor/Person Update. + So clear the actor from the cache and it will be refreshed when the next + post from them is sent + """ + if not session or not avatarUrl: + return + if domainFull in avatarUrl: + return + if urlExists(session, avatarUrl, timeoutSec, httpPrefix, domainFull): + return + _removePersonFromCache(baseDir, personUrl, personCache) + + def storePersonInCache(baseDir: str, personUrl: str, personJson: {}, personCache: {}, allowWriteToFile: bool) -> None: diff --git a/daemon.py b/daemon.py index 335b13f96..23a1abd85 100644 --- a/daemon.py +++ b/daemon.py @@ -20,7 +20,6 @@ import pyqrcode # for saving images from hashlib import sha256 from hashlib import sha1 -from siteactive import siteIsActive from session import createSession from webfinger import webfingerMeta from webfinger import webfingerNodeInfo @@ -229,7 +228,7 @@ from content import extractMediaInFormPOST from content import saveMediaInFormPOST from content import extractTextFieldsInPOST from media import removeMetaData -from cache import removePersonFromCache +from cache import checkForChangedActor from cache import storePersonInCache from cache import getPersonFromCache from httpsig import verifyPostHeaders @@ -5491,14 +5490,11 @@ class PubServer(BaseHTTPRequestHandler): if actorJson.get('alsoKnownAs'): alsoKnownAs = actorJson['alsoKnownAs'] - # check if the avatar image exists and if not then update - # the actor cache - if optionsProfileUrl: - if self.server.domainFull not in optionsProfileUrl: - if not siteIsActive(optionsProfileUrl, 3): - removePersonFromCache(self.server.baseDir, - optionsActor, - self.server.personCache) + checkForChangedActor(self.server.session, + self.server.baseDir, self.server.httpPrefix, + self.server.domainFull, + optionsActor, optionsProfileUrl, + self.server.personCache, 3) msg = htmlPersonOptions(self.server.defaultTimeline, self.server.cssCache, diff --git a/session.py b/session.py index cf235fd76..b82bee7be 100644 --- a/session.py +++ b/session.py @@ -53,6 +53,34 @@ def createSession(proxyType: str): return session +def urlExists(session, url: str, timeoutSec=3, + httpPrefix='https', domain='testdomain') -> bool: + if not isinstance(url, str): + print('url: ' + str(url)) + print('ERROR: urlExists failed, url should be a string') + return False + sessionParams = {} + sessionHeaders = {} + sessionHeaders['User-Agent'] = 'Epicyon/' + __version__ + if domain: + sessionHeaders['User-Agent'] += \ + '; +' + httpPrefix + '://' + domain + '/' + if not session: + print('WARN: urlExists failed, no session specified') + return True + try: + result = session.get(url, headers=sessionHeaders, + params=sessionParams, + timeout=timeoutSec) + if result: + if result.status_code == 200 or \ + result.status_code == 304: + return True + except BaseException: + pass + return False + + def getJson(session, url: str, headers: {}, params: {}, version='1.2.0', httpPrefix='https', domain='testdomain') -> {}: @@ -72,6 +100,7 @@ def getJson(session, url: str, headers: {}, params: {}, '; +' + httpPrefix + '://' + domain + '/' if not session: print('WARN: getJson failed, no session specified for getJson') + return None try: result = session.get(url, headers=sessionHeaders, params=sessionParams) return result.json()