1
0
Fork 0

Fetch updated key if old key is invalid.

This commit is contained in:
Adam Kelly 2020-05-22 13:49:56 +01:00
parent 17734940ac
commit 5cfc9aa8de
3 changed files with 96 additions and 19 deletions

View file

@ -17,17 +17,7 @@ def get_or_create_remote_user(actor):
except models.User.DoesNotExist:
pass
# load the user's info from the actor url
response = requests.get(
actor,
headers={'Accept': 'application/activity+json'}
)
if not response.ok:
response.raise_for_status()
data = response.json()
# make sure our actor is who they say they are
assert actor == data['id']
data = fetch_user_data(actor)
actor_parts = urlparse(actor)
with transaction.atomic():
@ -43,6 +33,21 @@ def get_or_create_remote_user(actor):
get_remote_reviews(user)
return user
def fetch_user_data(actor):
# load the user's info from the actor url
response = requests.get(
actor,
headers={'Accept': 'application/activity+json'}
)
if not response.ok:
response.raise_for_status()
data = response.json()
# make sure our actor is who they say they are
if actor != data['id']:
raise ValueError("Remote actor id must match url.")
return data
def create_remote_user(data):
''' parse the activitypub actor data into a user '''
@ -72,6 +77,24 @@ def create_remote_user(data):
'manuallyApprovesFollowers', False),
)
def refresh_remote_user(user):
data = fetch_user_data(user.remote_id)
shared_inbox = data.get('endpoints').get('sharedInbox') if \
data.get('endpoints') else None
# TODO - I think dataclasses change will mean less repetition here later.
user.name = data.get('name')
user.summary = data.get('summary')
user.inbox = data['inbox'] #fail if there's no inbox
user.outbox = data['outbox'] # fail if there's no outbox
user.shared_inbox = shared_inbox
user.public_key = data.get('publicKey').get('publicKeyPem')
user.local = False
user.fedireads_user = data.get('fedireadsUser', False)
user.manually_approves_followers = data.get(
'manuallyApprovesFollowers', False)
user.save()
def get_avatar(data):
''' find the icon attachment and load the image from the remote sever '''