mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-20 23:26:42 +00:00
set user's remote server in a celery task
This commit is contained in:
parent
9ddd60ce16
commit
205fa0d465
3 changed files with 28 additions and 33 deletions
|
@ -7,6 +7,7 @@ from django.db import models
|
|||
from django.dispatch import receiver
|
||||
|
||||
from bookwyrm import activitypub
|
||||
from bookwyrm.connectors import get_data
|
||||
from bookwyrm.models.shelf import Shelf
|
||||
from bookwyrm.models.status import Status, Review
|
||||
from bookwyrm.settings import DOMAIN
|
||||
|
@ -195,13 +196,7 @@ def execute_after_save(sender, instance, created, *args, **kwargs):
|
|||
return
|
||||
|
||||
if not instance.local:
|
||||
actor_parts = urlparse(instance.remote_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
|
||||
set_remote_server.delay(instance.id)
|
||||
|
||||
shelves = [{
|
||||
'name': 'To Read',
|
||||
|
@ -222,6 +217,18 @@ def execute_after_save(sender, instance, created, *args, **kwargs):
|
|||
editable=False
|
||||
).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):
|
||||
''' get info on a remote server '''
|
||||
|
@ -232,25 +239,14 @@ def get_or_create_remote_server(domain):
|
|||
except FederatedServer.DoesNotExist:
|
||||
pass
|
||||
|
||||
response = requests.get(
|
||||
'https://%s/.well-known/nodeinfo' % domain,
|
||||
headers={'Accept': 'application/activity+json'}
|
||||
)
|
||||
data = get_data('https://%s/.well-known/nodeinfo' % domain)
|
||||
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
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()
|
||||
data = get_data(nodeinfo_url)
|
||||
|
||||
server = FederatedServer.objects.create(
|
||||
server_name=domain,
|
||||
|
@ -264,11 +260,8 @@ def get_or_create_remote_server(domain):
|
|||
def get_remote_reviews(outbox):
|
||||
''' ingest reviews by a new remote bookwyrm user '''
|
||||
outbox_page = outbox + '?page=true'
|
||||
response = requests.get(
|
||||
outbox_page,
|
||||
headers={'Accept': 'application/activity+json'}
|
||||
)
|
||||
data = response.json()
|
||||
data = get_data(outbox_page)
|
||||
|
||||
# TODO: pagination?
|
||||
for activity in data['orderedItems']:
|
||||
if not activity['type'] == 'Review':
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import json
|
||||
import pathlib
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
from bookwyrm import activitypub, models
|
||||
|
@ -8,13 +9,14 @@ from bookwyrm import activitypub, models
|
|||
class Quotation(TestCase):
|
||||
''' we have hecka ways to create statuses '''
|
||||
def setUp(self):
|
||||
self.user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword',
|
||||
local=False,
|
||||
inbox='https://example.com/user/mouse/inbox',
|
||||
outbox='https://example.com/user/mouse/outbox',
|
||||
remote_id='https://example.com/user/mouse',
|
||||
)
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
self.user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword',
|
||||
local=False,
|
||||
inbox='https://example.com/user/mouse/inbox',
|
||||
outbox='https://example.com/user/mouse/outbox',
|
||||
remote_id='https://example.com/user/mouse',
|
||||
)
|
||||
self.book = models.Edition.objects.create(
|
||||
title='Example Edition',
|
||||
remote_id='https://example.com/book/1',
|
||||
|
|
|
@ -38,7 +38,7 @@ class Book(TestCase):
|
|||
'joe', 'joe@mouse.mouse', 'jeoword')
|
||||
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(
|
||||
'nutria', 'nutria@mouse.mouse', 'nuword',
|
||||
remote_id='http://example.com/u/4',
|
||||
|
|
Loading…
Reference in a new issue