2020-02-11 23:17:21 +00:00
|
|
|
''' manage remote users '''
|
|
|
|
from urllib.parse import urlparse
|
2020-04-03 17:19:04 +00:00
|
|
|
from uuid import uuid4
|
2020-04-22 13:53:22 +00:00
|
|
|
import requests
|
2020-04-03 17:19:04 +00:00
|
|
|
|
|
|
|
from django.core.files.base import ContentFile
|
2020-05-14 19:08:57 +00:00
|
|
|
from django.db import transaction
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub, models
|
2020-02-11 23:17:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_or_create_remote_user(actor):
|
|
|
|
''' look up a remote user or add them '''
|
|
|
|
try:
|
2020-05-14 19:08:57 +00:00
|
|
|
return models.User.objects.get(remote_id=actor)
|
2020-02-11 23:17:21 +00:00
|
|
|
except models.User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2020-05-22 12:49:56 +00:00
|
|
|
data = fetch_user_data(actor)
|
2020-05-19 01:26:00 +00:00
|
|
|
|
2020-02-11 23:17:21 +00:00
|
|
|
actor_parts = urlparse(actor)
|
2020-05-14 19:08:57 +00:00
|
|
|
with transaction.atomic():
|
|
|
|
user = create_remote_user(data)
|
|
|
|
user.federated_server = get_or_create_remote_server(actor_parts.netloc)
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
avatar = get_avatar(data)
|
2020-05-21 15:32:28 +00:00
|
|
|
if avatar:
|
|
|
|
user.avatar.save(*avatar)
|
2020-05-14 19:08:57 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
if user.bookwyrm_user:
|
2020-05-14 19:08:57 +00:00
|
|
|
get_remote_reviews(user)
|
|
|
|
return user
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2020-05-22 12:49:56 +00:00
|
|
|
def fetch_user_data(actor):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' load the user's info from the actor url '''
|
2020-05-22 12:49:56 +00:00
|
|
|
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
|
|
|
|
|
2020-05-14 19:08:57 +00:00
|
|
|
|
|
|
|
def create_remote_user(data):
|
|
|
|
''' parse the activitypub actor data into a user '''
|
2020-09-17 20:02:52 +00:00
|
|
|
actor = activitypub.Person(**data)
|
|
|
|
return actor.to_model(models.User)
|
2020-05-14 19:08:57 +00:00
|
|
|
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-05-22 12:49:56 +00:00
|
|
|
def refresh_remote_user(user):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' get updated user data from its home instance '''
|
2020-05-22 12:49:56 +00:00
|
|
|
data = fetch_user_data(user.remote_id)
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
activity = activitypub.Person(**data)
|
|
|
|
activity.to_model(models.User, instance=user)
|
|
|
|
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-04-03 17:19:04 +00:00
|
|
|
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]
|
|
|
|
|
|
|
|
|
2020-02-19 20:35:57 +00:00
|
|
|
def get_remote_reviews(user):
|
2020-09-21 15:10:37 +00:00
|
|
|
''' ingest reviews by a new remote bookwyrm user '''
|
2020-02-19 20:35:57 +00:00
|
|
|
outbox_page = user.outbox + '?page=true'
|
|
|
|
response = requests.get(
|
|
|
|
outbox_page,
|
|
|
|
headers={'Accept': 'application/activity+json'}
|
|
|
|
)
|
|
|
|
data = response.json()
|
2020-04-21 15:01:54 +00:00
|
|
|
# TODO: pagination?
|
2020-02-19 20:35:57 +00:00
|
|
|
for status in data['orderedItems']:
|
2020-09-21 15:10:37 +00:00
|
|
|
if status.get('bookwyrmType') == 'Review':
|
2020-09-17 20:02:52 +00:00
|
|
|
activitypub.Review(**status).to_model(models.Review)
|
2020-02-19 20:35:57 +00:00
|
|
|
|
2020-03-29 22:51:43 +00:00
|
|
|
|
|
|
|
def get_or_create_remote_server(domain):
|
|
|
|
''' get info on a remote server '''
|
|
|
|
try:
|
|
|
|
return models.FederatedServer.objects.get(
|
|
|
|
server_name=domain
|
|
|
|
)
|
|
|
|
except models.FederatedServer.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
response = requests.get(
|
|
|
|
'https://%s/.well-known/nodeinfo' % domain,
|
|
|
|
headers={'Accept': 'application/activity+json'}
|
|
|
|
)
|
2020-05-19 20:58:30 +00:00
|
|
|
|
|
|
|
if response.status_code != 200:
|
|
|
|
return None
|
|
|
|
|
2020-03-29 22:51:43 +00:00
|
|
|
data = response.json()
|
|
|
|
try:
|
|
|
|
nodeinfo_url = data.get('links')[0].get('href')
|
|
|
|
except (TypeError, KeyError):
|
|
|
|
return None
|
|
|
|
|
|
|
|
response = requests.get(
|
|
|
|
nodeinfo_url,
|
|
|
|
headers={'Accept': 'application/activity+json'}
|
|
|
|
)
|
|
|
|
data = response.json()
|
|
|
|
|
|
|
|
server = models.FederatedServer.objects.create(
|
|
|
|
server_name=domain,
|
|
|
|
application_type=data['software']['name'],
|
|
|
|
application_version=data['software']['version'],
|
|
|
|
)
|
|
|
|
return server
|