Check if actor has changed

Can sometimes happen if Person Update has not been received
merge-requests/18/head
Bob Mottram 2021-02-14 13:43:05 +00:00
parent 04e80de244
commit 783711b46f
3 changed files with 56 additions and 12 deletions

View File

@ -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:

View File

@ -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,

View File

@ -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()