From f7cb3d944478e93a12b5034aa7c49cb1c7bc203f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 3 Apr 2020 10:19:04 -0700 Subject: [PATCH] Load remote avatars --- fedireads/remote_user.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/fedireads/remote_user.py b/fedireads/remote_user.py index ac58e5a4d..67db47ea3 100644 --- a/fedireads/remote_user.py +++ b/fedireads/remote_user.py @@ -1,6 +1,9 @@ ''' manage remote users ''' import requests from urllib.parse import urlparse +from uuid import uuid4 + +from django.core.files.base import ContentFile from fedireads import models from fedireads.status import create_review_from_activity @@ -28,8 +31,8 @@ def get_or_create_remote_user(actor): shared_inbox = data.get('endpoints').get('sharedInbox') if \ data.get('endpoints') else None - server = get_or_create_remote_server(actor_parts.netloc) + avatar = get_avatar(data) # throws a key error if it can't find any of these fields user = models.User.objects.create_user( @@ -49,11 +52,27 @@ def get_or_create_remote_user(actor): 'manuallyApprovesFollowers', False), federated_server=server, ) + user.avatar.save(*avatar) if user.fedireads_user: get_remote_reviews(user) return user +def get_avatar(data): + ''' find the icon attachment and load the image from the remote sever ''' + icon_blob = data.get('icon') + if not icon_blob or not icon_blob.get('url'): + return None + + response = requests.get(icon_blob['url']) + if not response.ok: + return None + + image_name = str(uuid4()) + '.' + icon_blob['url'].split('.')[-1] + image_content = ContentFile(response.content) + return [image_name, image_content] + + def get_remote_reviews(user): ''' ingest reviews by a new remote fedireads user ''' # TODO: use the server as the data source instead of OL