set user's remote server in a celery task

This commit is contained in:
Mouse Reeve 2020-11-29 10:08:19 -08:00
parent 9ddd60ce16
commit 205fa0d465
3 changed files with 28 additions and 33 deletions

View file

@ -7,6 +7,7 @@ from django.db import models
from django.dispatch import receiver from django.dispatch import receiver
from bookwyrm import activitypub from bookwyrm import activitypub
from bookwyrm.connectors import get_data
from bookwyrm.models.shelf import Shelf from bookwyrm.models.shelf import Shelf
from bookwyrm.models.status import Status, Review from bookwyrm.models.status import Status, Review
from bookwyrm.settings import DOMAIN from bookwyrm.settings import DOMAIN
@ -195,13 +196,7 @@ def execute_after_save(sender, instance, created, *args, **kwargs):
return return
if not instance.local: if not instance.local:
actor_parts = urlparse(instance.remote_id) set_remote_server.delay(instance.id)
instance.federated_server = \
get_or_create_remote_server(actor_parts.netloc)
instance.save()
if instance.bookwyrm_user:
get_remote_reviews.delay(instance.outbox)
return
shelves = [{ shelves = [{
'name': 'To Read', 'name': 'To Read',
@ -222,6 +217,18 @@ def execute_after_save(sender, instance, created, *args, **kwargs):
editable=False editable=False
).save() ).save()
@app.task
def set_remote_server(user_id):
''' figure out the user's remote server in the background '''
user = User.objects.get(id=user_id)
actor_parts = urlparse(user.remote_id)
user.federated_server = \
get_or_create_remote_server(actor_parts.netloc)
user.save()
if user.bookwyrm_user:
get_remote_reviews.delay(user.outbox)
return
def get_or_create_remote_server(domain): def get_or_create_remote_server(domain):
''' get info on a remote server ''' ''' get info on a remote server '''
@ -232,25 +239,14 @@ def get_or_create_remote_server(domain):
except FederatedServer.DoesNotExist: except FederatedServer.DoesNotExist:
pass pass
response = requests.get( data = get_data('https://%s/.well-known/nodeinfo' % domain)
'https://%s/.well-known/nodeinfo' % domain,
headers={'Accept': 'application/activity+json'}
)
if response.status_code != 200:
return None
data = response.json()
try: try:
nodeinfo_url = data.get('links')[0].get('href') nodeinfo_url = data.get('links')[0].get('href')
except (TypeError, KeyError): except (TypeError, KeyError):
return None return None
response = requests.get( data = get_data(nodeinfo_url)
nodeinfo_url,
headers={'Accept': 'application/activity+json'}
)
data = response.json()
server = FederatedServer.objects.create( server = FederatedServer.objects.create(
server_name=domain, server_name=domain,
@ -264,11 +260,8 @@ def get_or_create_remote_server(domain):
def get_remote_reviews(outbox): def get_remote_reviews(outbox):
''' ingest reviews by a new remote bookwyrm user ''' ''' ingest reviews by a new remote bookwyrm user '''
outbox_page = outbox + '?page=true' outbox_page = outbox + '?page=true'
response = requests.get( data = get_data(outbox_page)
outbox_page,
headers={'Accept': 'application/activity+json'}
)
data = response.json()
# TODO: pagination? # TODO: pagination?
for activity in data['orderedItems']: for activity in data['orderedItems']:
if not activity['type'] == 'Review': if not activity['type'] == 'Review':

View file

@ -1,5 +1,6 @@
import json import json
import pathlib import pathlib
from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from bookwyrm import activitypub, models from bookwyrm import activitypub, models
@ -8,13 +9,14 @@ from bookwyrm import activitypub, models
class Quotation(TestCase): class Quotation(TestCase):
''' we have hecka ways to create statuses ''' ''' we have hecka ways to create statuses '''
def setUp(self): def setUp(self):
self.user = models.User.objects.create_user( with patch('bookwyrm.models.user.set_remote_server.delay'):
'mouse', 'mouse@mouse.mouse', 'mouseword', self.user = models.User.objects.create_user(
local=False, 'mouse', 'mouse@mouse.mouse', 'mouseword',
inbox='https://example.com/user/mouse/inbox', local=False,
outbox='https://example.com/user/mouse/outbox', inbox='https://example.com/user/mouse/inbox',
remote_id='https://example.com/user/mouse', outbox='https://example.com/user/mouse/outbox',
) remote_id='https://example.com/user/mouse',
)
self.book = models.Edition.objects.create( self.book = models.Edition.objects.create(
title='Example Edition', title='Example Edition',
remote_id='https://example.com/book/1', remote_id='https://example.com/book/1',

View file

@ -38,7 +38,7 @@ class Book(TestCase):
'joe', 'joe@mouse.mouse', 'jeoword') 'joe', 'joe@mouse.mouse', 'jeoword')
self.user.followers.add(local_follower) self.user.followers.add(local_follower)
with patch('bookwyrm.models.user.get_remote_reviews.delay'): with patch('bookwyrm.models.user.set_remote_server.delay'):
models.User.objects.create_user( models.User.objects.create_user(
'nutria', 'nutria@mouse.mouse', 'nuword', 'nutria', 'nutria@mouse.mouse', 'nuword',
remote_id='http://example.com/u/4', remote_id='http://example.com/u/4',