better urls

This commit is contained in:
Mouse Reeve 2020-01-28 00:44:51 -08:00
parent 591f0ccf5d
commit ae248f6fdc
4 changed files with 40 additions and 25 deletions

View file

@ -54,10 +54,15 @@ def get_actor(request, username):
'preferredUsername': user.localname,
'inbox': format_inbox(user),
'followers': '%s/followers' % user.actor,
'following': '%s/following' % user.actor,
'summary': user.summary,
'publicKey': {
'id': '%s/#main-key' % user.actor,
'owner': user.actor,
'publicKeyPem': user.public_key,
},
'endpoints': {
'sharedInbox': 'https://%s/inbox' % DOMAIN,
}
})
@ -149,7 +154,7 @@ def handle_incoming_follow(activity):
'''
# figure out who they want to follow
to_follow = re.sub(
r'https?://([\w\.]+)/api/u/(\w+)',
r'https?://([\w\.]+)/user/(\w+)',
r'\2@\1',
activity['object']
)
@ -243,6 +248,7 @@ def handle_shelve(user, book, shelf):
'id': shelf.activitypub_id
}
}
# TODO: this should be getting shared inboxes and deduplicating
recipients = [format_inbox(u) for u in user.followers.all()]
models.ShelveActivity(
@ -363,7 +369,7 @@ def broadcast(sender, action, recipients):
def sign_and_send(sender, action, destination):
''' crpyto whatever and http junk '''
inbox_fragment = '/api/u/%s/inbox' % (sender.localname)
inbox_fragment = sender.inbox.replace('https://%s' % DOMAIN, '')
now = datetime.utcnow().isoformat()
message_to_sign = '''(request-target): post %s
host: https://%s

View file

@ -1,4 +1,4 @@
# Generated by Django 3.0.2 on 2020-01-28 06:21
# Generated by Django 3.0.2 on 2020-01-28 08:28
from django.conf import settings
import django.contrib.auth.models
@ -36,6 +36,9 @@ class Migration(migrations.Migration):
('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)),
('inbox', models.CharField(max_length=255)),
('outbox', models.CharField(max_length=255)),
('summary', models.TextField(blank=True, null=True)),
('local', models.BooleanField(default=True)),
('localname', models.CharField(blank=True, max_length=255, null=True, unique=True)),
('avatar', models.ImageField(blank=True, null=True, upload_to='uploads/')),

View file

@ -14,6 +14,9 @@ class User(AbstractUser):
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)
inbox = models.CharField(max_length=255)
outbox = models.CharField(max_length=255)
summary = models.TextField(blank=True, null=True)
local = models.BooleanField(default=True)
localname = models.CharField(
max_length=255,
@ -27,25 +30,23 @@ class User(AbstractUser):
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
def save(self, *args, **kwargs):
# give a new user keys
if not self.private_key:
@receiver(models.signals.pre_save, sender=User)
def execute_before_save(sender, instance, *args, **kwargs):
''' create shelves for new users '''
# TODO: how are remote users handled? what if they aren't readers?
if not instance.local or instance.id:
return
instance.localname = instance.username
instance.username = '%s@%s' % (instance.username, DOMAIN)
instance.actor = 'https://%s/user/%s' % (DOMAIN, instance.localname)
instance.inbox = 'https://%s/user/%s/inbox' % (DOMAIN, instance.localname)
instance.outbox = 'https://%s/user/%s/outbox' % (DOMAIN, instance.localname)
if not instance.private_key:
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
self.private_key = key.export_key().decode('utf8')
self.public_key = key.publickey().export_key().decode('utf8')
if self.local and not re.match(r'\w+@\w+.\w+', self.username):
# set your local username that doesn't have the domain
self.username = '%s@%s' % (self.username, DOMAIN)
if self.local and not self.localname:
self.localname = self.username.replace('@%s' % DOMAIN, '')
if self.local and not self.actor:
self.actor = 'https://%s/api/u/%s' % (DOMAIN, self.localname)
super().save(*args, **kwargs)
instance.private_key = key.export_key().decode('utf8')
instance.public_key = key.publickey().export_key().decode('utf8')
@receiver(models.signals.post_save, sender=User)

View file

@ -20,6 +20,14 @@ from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# federation endpoints
path('user/<str:username>.json', federation.get_actor),
path('user/<str:username>/inbox', federation.inbox),
path('user/<str:username>/outbox', federation.outbox),
path('.well-known/webfinger', federation.webfinger),
# ui views
path('', views.home),
path('login/', views.user_login),
path('logout/', views.user_logout),
@ -27,6 +35,7 @@ urlpatterns = [
path('user/<str:username>/edit/', views.user_profile_edit),
path('book/<str:book_identifier>', views.book_page),
# internal action endpoints
path('review/', views.review),
path('shelve/<str:shelf_id>/<int:book_id>', views.shelve),
path('follow/', views.follow),
@ -34,8 +43,4 @@ urlpatterns = [
path('search/', views.search),
path('upload-avatar/', views.upload_avatar),
path('api/u/<str:username>', federation.get_actor),
path('api/u/<str:username>/inbox', federation.inbox),
path('api/u/<str:username>/outbox', federation.outbox),
path('.well-known/webfinger', federation.webfinger),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)