Rename and rework refresh_instance_profile as now only called for newly-discovered instances

This commit is contained in:
freamon 2024-05-23 00:14:07 +01:00
parent 01fd3fa2ea
commit 0095d7a79b

View file

@ -1041,23 +1041,22 @@ def find_instance_id(server):
db.session.commit()
# Spawn background task to fill in more details
refresh_instance_profile(new_instance.id)
new_instance_profile(new_instance.id)
return new_instance.id
def refresh_instance_profile(instance_id: int):
def new_instance_profile(instance_id: int):
if instance_id:
if current_app.debug:
refresh_instance_profile_task(instance_id)
new_instance_profile_task(instance_id)
else:
refresh_instance_profile_task.apply_async(args=(instance_id,), countdown=randint(1, 10))
new_instance_profile_task.apply_async(args=(instance_id,), countdown=randint(1, 10))
@celery.task
def refresh_instance_profile_task(instance_id: int):
def new_instance_profile_task(instance_id: int):
instance = Instance.query.get(instance_id)
if instance.inbox is None or instance.updated_at < utcnow() - timedelta(days=7):
try:
instance_data = get_request(f"https://{instance.domain}", headers={'Accept': 'application/activity+json'})
except:
@ -1069,23 +1068,9 @@ def refresh_instance_profile_task(instance_id: int):
except requests.exceptions.JSONDecodeError as ex:
instance_json = {}
if 'type' in instance_json and instance_json['type'] == 'Application':
# 'name' is unreliable as the admin can change it to anything. todo: find better way
if instance_json['name'].lower() == 'kbin':
software = 'kbin'
elif instance_json['name'].lower() == 'mbin':
software = 'mbin'
elif instance_json['name'].lower() == 'piefed':
software = 'piefed'
elif instance_json['name'].lower() == 'system account':
software = 'friendica'
else:
software = 'lemmy'
instance.inbox = instance_json['inbox']
instance.outbox = instance_json['outbox']
instance.software = software
if instance.inbox.endswith('/site_inbox'): # Lemmy provides a /site_inbox but it always returns 400 when trying to POST to it. wtf.
instance.inbox = instance.inbox.replace('/site_inbox', '/inbox')
else: # it's pretty much always /inbox so just assume that it is for whatever this instance is running (mostly likely Mastodon)
else: # it's pretty much always /inbox so just assume that it is for whatever this instance is running
instance.inbox = f"https://{instance.domain}/inbox"
instance.updated_at = utcnow()
db.session.commit()
@ -1122,12 +1107,39 @@ def refresh_instance_profile_task(instance_id: int):
InstanceRole.instance_id == instance.id,
InstanceRole.role == 'admin').delete()
db.session.commit()
elif instance_data.status_code == 406: # Mastodon does this
instance.software = 'mastodon'
elif instance_data.status_code == 406: # Mastodon and PeerTube do this
instance.inbox = f"https://{instance.domain}/inbox"
instance.updated_at = utcnow()
db.session.commit()
HEADERS = {'User-Agent': 'PieFed/1.0', 'Accept': 'application/activity+json'}
try:
nodeinfo = requests.get(f"https://{instance.domain}/.well-known/nodeinfo", headers=HEADERS,
timeout=5, allow_redirects=True)
if nodeinfo.status_code == 200:
nodeinfo_json = nodeinfo.json()
for links in nodeinfo_json['links']:
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:
time.sleep(0.1)
node = requests.get(links['href'], headers=HEADERS, timeout=5,
allow_redirects=True)
if node.status_code == 200:
node_json = node.json()
if 'software' in node_json:
instance.software = node_json['software']['name'].lower()
instance.version = node_json['software']['version']
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
# alter the effect of upvotes based on their instance. Default to 1.0
@cache.memoize(timeout=50)