From f031b46b206e8ad0a5e7d0f9e5c37d0c6f2540f1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Jan 2020 12:07:20 -0800 Subject: [PATCH] Add federated users --- fedireads/api.py | 30 +++++++++++++++++++--------- fedireads/migrations/0001_initial.py | 6 +++--- fedireads/models.py | 12 +++-------- fedireads/urls.py | 2 +- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/fedireads/api.py b/fedireads/api.py index 1c345e3dd..76a56b979 100644 --- a/fedireads/api.py +++ b/fedireads/api.py @@ -15,16 +15,28 @@ from fedireads.settings import DOMAIN def get_or_create_remote_user(actor): ''' look up a remote user or add them ''' try: - user = models.User.objects.get(actor=actor) + return models.User.objects.get(actor=actor) except models.User.DoesNotExist: - # TODO: how do you actually correctly learn this? - username = '%s@%s' % (actor.split('/')[-1], actor.split('/')[2]) - user = models.User.objects.create_user( - username, - '', '', - actor=actor, - local=False - ) + pass + + # get the user's info + response = requests.get( + actor, + headers={'Accept': 'application/activity+json'} + ) + data = response.json() + + username = '%s@%s' % (actor.split('/')[-1], actor.split('/')[2]) + user = models.User.objects.create_user( + username, '', '', + name=data.get('name'), + summary=data.get('summary'), + inbox=data['inbox'], + outbox=data['outbox'], + shared_inbox=data.get('endpoints').get('sharedInbox'), + actor=actor, + local=False + ) return user diff --git a/fedireads/migrations/0001_initial.py b/fedireads/migrations/0001_initial.py index 043a3fc47..6402ab2d1 100644 --- a/fedireads/migrations/0001_initial.py +++ b/fedireads/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.2 on 2020-01-29 18:40 +# Generated by Django 3.0.2 on 2020-01-29 19:58 from django.conf import settings import django.contrib.auth.models @@ -32,8 +32,8 @@ class Migration(migrations.Migration): ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), - ('private_key', models.TextField(blank=True, null=True, unique=True)), - ('public_key', models.TextField(blank=True, null=True, unique=True)), + ('private_key', models.TextField(blank=True, null=True)), + ('public_key', models.TextField(blank=True, null=True)), ('api_key', models.CharField(blank=True, max_length=255, null=True)), ('actor', models.CharField(max_length=255, unique=True)), ('inbox', models.CharField(max_length=255, unique=True)), diff --git a/fedireads/models.py b/fedireads/models.py index 1161b331d..3f41e436b 100644 --- a/fedireads/models.py +++ b/fedireads/models.py @@ -13,8 +13,8 @@ from fedireads.settings import DOMAIN, OL_URL class User(AbstractUser): ''' a user who wants to read books ''' - private_key = models.TextField(blank=True, null=True, unique=True) - public_key = models.TextField(blank=True, null=True, unique=True) + private_key = models.TextField(blank=True, null=True) + public_key = models.TextField(blank=True, null=True) api_key = models.CharField(max_length=255, blank=True, null=True) actor = models.CharField(max_length=255, unique=True) inbox = models.CharField(max_length=255, unique=True) @@ -44,13 +44,7 @@ class User(AbstractUser): def execute_before_save(sender, instance, *args, **kwargs): ''' create shelves for new users ''' # this user already exists, no need to poplate fields - if instance.id: - return - - # TODO: how do I know this properly??? - if not instance.local: - instance.inbox = instance.actor = 'inbox' - instance.outbox = instance.actor = 'outbox' + if instance.id or not instance.local: return # populate fields for local users diff --git a/fedireads/urls.py b/fedireads/urls.py index fd40b4341..e6641aaec 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -20,7 +20,7 @@ urlpatterns = [ # TODO: re_path(r'^.well-known/host-meta/?$', incoming.host_meta), # ui views - re_path(r'^/?$', views.home), + path(r'', views.home), re_path(r'^register/?$', views.register), re_path(r'^login/?$', views.user_login), re_path(r'^logout/?$', views.user_logout),