Use Instance.nodeinfo_href if available

This commit is contained in:
freamon 2024-05-23 17:12:04 +01:00
parent 7b7f9f4664
commit cd9da2abac
2 changed files with 39 additions and 28 deletions

View file

@ -1132,12 +1132,11 @@ def new_instance_profile_task(instance_id: int):
if 'software' in node_json:
instance.software = node_json['software']['name'].lower()
instance.version = node_json['software']['version']
instance.nodeinfo_href = links['href']
db.session.commit()
except:
# todo: update new field in Instance to indicate bad nodeinfo response
return
except:
# todo: update new field in Instance to indicate bad nodeinfo response
return

View file

@ -183,6 +183,7 @@ def register(app):
instances = Instance.query.filter(Instance.gone_forever == False, Instance.id != 1).all()
HEADERS = {'User-Agent': 'PieFed/1.0', 'Accept': 'application/activity+json'}
for instance in instances:
if not instance.nodeinfo_href:
try:
nodeinfo = requests.get(f"https://{instance.domain}/.well-known/nodeinfo", headers=HEADERS,
timeout=5, allow_redirects=True)
@ -193,9 +194,24 @@ def register(app):
if 'rel' in links and (
links['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0' or
links['rel'] == 'https://nodeinfo.diaspora.software/ns/schema/2.0'):
try:
instance.nodeinfo_href = links['href']
instance.failures = 0
instance.dormant = False
db.session.commit()
sleep(0.1)
node = requests.get(links['href'], headers=HEADERS, timeout=5,
break
elif node.status_code >= 400:
current_app.logger.info(f"{instance.domain} has no well-known/nodeinfo response")
except requests.exceptions.ReadTimeout:
instance.failures += 1
except requests.exceptions.ConnectionError:
instance.failures += 1
except requests.exceptions.RequestException:
pass
if instance.nodeinfo_href:
try:
node = requests.get(instance.nodeinfo_href, headers=HEADERS, timeout=5,
allow_redirects=True)
if node.status_code == 200:
node_json = node.json()
@ -206,11 +222,7 @@ def register(app):
instance.dormant = False
elif node.status_code >= 400:
instance.failures += 1
except:
instance.failures += 1
elif nodeinfo.status_code >= 400:
instance.failures += 1
except:
except requests.exceptions.RequestException:
instance.failures += 1
if instance.failures > 7 and instance.dormant == True:
instance.gone_forever = True