Fixes setting remote user username on update
This commit is contained in:
parent
4090b336db
commit
3fe7b95786
|
@ -1,4 +1,5 @@
|
||||||
''' database schema for user data '''
|
''' database schema for user data '''
|
||||||
|
import re
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
|
@ -13,6 +14,7 @@ from bookwyrm.models.status import Status, Review
|
||||||
from bookwyrm.settings import DOMAIN
|
from bookwyrm.settings import DOMAIN
|
||||||
from bookwyrm.signatures import create_key_pair
|
from bookwyrm.signatures import create_key_pair
|
||||||
from bookwyrm.tasks import app
|
from bookwyrm.tasks import app
|
||||||
|
from bookwyrm.utils import regex
|
||||||
from .base_model import OrderedCollectionPageMixin
|
from .base_model import OrderedCollectionPageMixin
|
||||||
from .base_model import ActivitypubMixin, BookWyrmModel
|
from .base_model import ActivitypubMixin, BookWyrmModel
|
||||||
from .federated_server import FederatedServer
|
from .federated_server import FederatedServer
|
||||||
|
@ -168,15 +170,15 @@ class User(OrderedCollectionPageMixin, AbstractUser):
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
''' populate fields for new local users '''
|
''' populate fields for new local users '''
|
||||||
# this user already exists, no need to populate fields
|
# this user already exists, no need to populate fields
|
||||||
if self.id:
|
if not self.local and not re.match(regex.full_username, self.username):
|
||||||
return super().save(*args, **kwargs)
|
|
||||||
|
|
||||||
if not self.local:
|
|
||||||
# generate a username that uses the domain (webfinger format)
|
# generate a username that uses the domain (webfinger format)
|
||||||
actor_parts = urlparse(self.remote_id)
|
actor_parts = urlparse(self.remote_id)
|
||||||
self.username = '%s@%s' % (self.username, actor_parts.netloc)
|
self.username = '%s@%s' % (self.username, actor_parts.netloc)
|
||||||
return super().save(*args, **kwargs)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
if self.id or not self.local:
|
||||||
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
# populate fields for local users
|
# populate fields for local users
|
||||||
self.remote_id = 'https://%s/user/%s' % (DOMAIN, self.localname)
|
self.remote_id = 'https://%s/user/%s' % (DOMAIN, self.localname)
|
||||||
self.inbox = '%s/inbox' % self.remote_id
|
self.inbox = '%s/inbox' % self.remote_id
|
||||||
|
|
|
@ -111,6 +111,7 @@ class ActivitypubFields(TestCase):
|
||||||
self.assertEqual(instance.max_length, 150)
|
self.assertEqual(instance.max_length, 150)
|
||||||
self.assertEqual(instance.unique, True)
|
self.assertEqual(instance.unique, True)
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
|
instance.run_validators('mouse')
|
||||||
instance.run_validators('mouseexample.com')
|
instance.run_validators('mouseexample.com')
|
||||||
instance.run_validators('mouse@example.c')
|
instance.run_validators('mouse@example.c')
|
||||||
instance.run_validators('@example.com')
|
instance.run_validators('@example.com')
|
||||||
|
|
|
@ -487,6 +487,10 @@ class Incoming(TestCase):
|
||||||
|
|
||||||
def test_handle_update_user(self):
|
def test_handle_update_user(self):
|
||||||
''' update an existing user '''
|
''' update an existing user '''
|
||||||
|
# we only do this with remote users
|
||||||
|
self.local_user.local = False
|
||||||
|
self.local_user.save()
|
||||||
|
|
||||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||||
'data/ap_user.json')
|
'data/ap_user.json')
|
||||||
userdata = json.loads(datafile.read_bytes())
|
userdata = json.loads(datafile.read_bytes())
|
||||||
|
|
Loading…
Reference in New Issue