Fixes how inboxes are initialized and updates user model tests

This commit is contained in:
Mouse Reeve 2021-10-27 10:25:51 -07:00
parent b01a64d560
commit 94ce426617
3 changed files with 30 additions and 19 deletions

View file

@ -28,12 +28,6 @@ class ActorModel(BookWyrmModel):
blank=True,
)
inbox = fields.RemoteIdField(unique=True)
shared_inbox = fields.RemoteIdField(
activitypub_field="sharedInbox",
activitypub_wrapper="endpoints",
deduplication_field=False,
null=True,
)
local = models.BooleanField(default=False)
discoverable = fields.BooleanField(default=False)
default_post_privacy = models.CharField(
@ -53,7 +47,9 @@ class ActorModel(BookWyrmModel):
def save(self, *args, **kwargs):
"""set fields"""
print('at the parent')
created = not bool(self.id)
print('parent created', created)
if not created:
super().save(*args, **kwargs)
return
@ -62,9 +58,12 @@ class ActorModel(BookWyrmModel):
# this is a new remote obj, we need to set their remote server field
if not self.local:
super().save(*args, **kwargs)
# TODO transaction.on_commit(lambda: set_remote_server.delay(self.id))
transaction.on_commit(lambda: set_remote_server.delay(self.id))
return
self.followers_url = f"{self.remote_id}/followers"
self.inbox = f"{self.remote_id}/inbox"
self.outbox = f"{self.remote_id}/outbox"
super().save(*args, **kwargs)
# create keys and shelves for new local users

View file

@ -43,6 +43,12 @@ class User(OrderedCollectionPageMixin, AbstractUser, ActorModel):
unique=True,
validators=[fields.validate_localname],
)
shared_inbox = fields.RemoteIdField(
activitypub_field="sharedInbox",
activitypub_wrapper="endpoints",
deduplication_field=False,
null=True,
)
# name is your display name, which you can change at will
name = fields.CharField(max_length=100, null=True, blank=True)
avatar = fields.ImageField(
@ -231,14 +237,17 @@ class User(OrderedCollectionPageMixin, AbstractUser, ActorModel):
def save(self, *args, **kwargs):
"""populate fields for new local users"""
print('hi')
created = not bool(self.id)
print('created', created)
if not self.local and not re.match(regex.FULL_USERNAME, self.username):
# generate a username that uses the domain (webfinger format)
# parse out the username that uses the domain (webfinger format)
actor_parts = urlparse(self.remote_id)
self.username = f"{self.username}@{actor_parts.netloc}"
# this user already exists, no need to populate fields
if not created:
# make sure the deactivation state is correct in case it was updated
if self.is_active:
self.deactivation_date = None
elif not self.deactivation_date:
@ -250,8 +259,11 @@ class User(OrderedCollectionPageMixin, AbstractUser, ActorModel):
with transaction.atomic():
# populate fields for local users
link = site_link()
print('link', link)
self.remote_id = f"{link}/user/{self.localname}"
self.shared_inbox = f"{link}/inbox"
# an id needs to be set before we can proceed with related models
print('heading to the parent')
super().save(*args, **kwargs)
# make users editors by default

View file

@ -17,7 +17,7 @@ class User(TestCase):
"bookwyrm.activitystreams.populate_stream_task.delay"
):
self.user = models.User.objects.create_user(
"mouse@%s" % DOMAIN,
f"mouse@{DOMAIN}",
"mouse@mouse.mouse",
"mouseword",
local=True,
@ -40,7 +40,7 @@ class User(TestCase):
self.assertIsNotNone(self.user.key_pair.public_key)
def test_remote_user(self):
with patch("bookwyrm.models.user.set_remote_server.delay"):
with patch("bookwyrm.models.actor.set_remote_server.delay"):
user = models.User.objects.create_user(
"rat",
"rat@rat.rat",
@ -98,7 +98,7 @@ class User(TestCase):
server_name=DOMAIN, application_type="test type", application_version=3
)
models.user.set_remote_server(self.user.id)
models.actor.set_remote_server(self.user.id)
self.user.refresh_from_db()
self.assertEqual(self.user.federated_server, server)
@ -107,7 +107,7 @@ class User(TestCase):
def test_get_or_create_remote_server(self):
responses.add(
responses.GET,
"https://%s/.well-known/nodeinfo" % DOMAIN,
f"https://{DOMAIN}/.well-known/nodeinfo",
json={"links": [{"href": "http://www.example.com"}, {}]},
)
responses.add(
@ -116,7 +116,7 @@ class User(TestCase):
json={"software": {"name": "hi", "version": "2"}},
)
server = models.user.get_or_create_remote_server(DOMAIN)
server = models.actor.get_or_create_remote_server(DOMAIN)
self.assertEqual(server.server_name, DOMAIN)
self.assertEqual(server.application_type, "hi")
self.assertEqual(server.application_version, "2")
@ -124,10 +124,10 @@ class User(TestCase):
@responses.activate
def test_get_or_create_remote_server_no_wellknown(self):
responses.add(
responses.GET, "https://%s/.well-known/nodeinfo" % DOMAIN, status=404
responses.GET, f"https://{DOMAIN}/.well-known/nodeinfo", status=404
)
server = models.user.get_or_create_remote_server(DOMAIN)
server = models.actor.get_or_create_remote_server(DOMAIN)
self.assertEqual(server.server_name, DOMAIN)
self.assertIsNone(server.application_type)
self.assertIsNone(server.application_version)
@ -136,12 +136,12 @@ class User(TestCase):
def test_get_or_create_remote_server_no_links(self):
responses.add(
responses.GET,
"https://%s/.well-known/nodeinfo" % DOMAIN,
f"https://{DOMAIN}/.well-known/nodeinfo",
json={"links": [{"href": "http://www.example.com"}, {}]},
)
responses.add(responses.GET, "http://www.example.com", status=404)
server = models.user.get_or_create_remote_server(DOMAIN)
server = models.actor.get_or_create_remote_server(DOMAIN)
self.assertEqual(server.server_name, DOMAIN)
self.assertIsNone(server.application_type)
self.assertIsNone(server.application_version)
@ -150,12 +150,12 @@ class User(TestCase):
def test_get_or_create_remote_server_unknown_format(self):
responses.add(
responses.GET,
"https://%s/.well-known/nodeinfo" % DOMAIN,
f"https://{DOMAIN}/.well-known/nodeinfo",
json={"links": [{"href": "http://www.example.com"}, {}]},
)
responses.add(responses.GET, "http://www.example.com", json={"fish": "salmon"})
server = models.user.get_or_create_remote_server(DOMAIN)
server = models.actor.get_or_create_remote_server(DOMAIN)
self.assertEqual(server.server_name, DOMAIN)
self.assertIsNone(server.application_type)
self.assertIsNone(server.application_version)