From ebdbdc8790ff12417548ec5dea09b92f46610ad8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 17 Apr 2021 13:19:23 -0700 Subject: [PATCH] Broadcast deletions --- bookwyrm/models/activitypub_mixin.py | 6 +++- bookwyrm/models/user.py | 6 ++++ bookwyrm/tests/models/test_user_model.py | 15 +++++++++ .../tests/views/inbox/test_inbox_delete.py | 33 ++++++++----------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/bookwyrm/models/activitypub_mixin.py b/bookwyrm/models/activitypub_mixin.py index efdf9a47..26ad7115 100644 --- a/bookwyrm/models/activitypub_mixin.py +++ b/bookwyrm/models/activitypub_mixin.py @@ -241,7 +241,11 @@ class ObjectMixin(ActivitypubMixin): return # is this a deletion? - if (hasattr(self, "deleted") and self.deleted) or hasattr(self, "is_active") and not self.is_active: + if ( + (hasattr(self, "deleted") and self.deleted) + or hasattr(self, "is_active") + and not self.is_active + ): activity = self.to_delete_activity(user) else: activity = self.to_update_activity(user) diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index d0a8a34f..b26b3b00 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -289,6 +289,12 @@ class User(OrderedCollectionPageMixin, AbstractUser): # skip the logic in this class's save() super().save(*args, **kwargs) + def to_activity(self, *args, **kwargs): + """ return a remote_id only if the user is inactive """ + if not self.is_active: + return self.remote_id + return super().to_activity_dataclass(*args, **kwargs) + @property def local_path(self): """ this model doesn't inherit bookwyrm model, so here we are """ diff --git a/bookwyrm/tests/models/test_user_model.py b/bookwyrm/tests/models/test_user_model.py index bd5255ce..883ef669 100644 --- a/bookwyrm/tests/models/test_user_model.py +++ b/bookwyrm/tests/models/test_user_model.py @@ -1,4 +1,5 @@ """ testing models """ +import json from unittest.mock import patch from django.test import TestCase import responses @@ -152,3 +153,17 @@ class User(TestCase): self.assertEqual(server.server_name, DOMAIN) self.assertIsNone(server.application_type) self.assertIsNone(server.application_version) + + def test_delete_user(self): + """ deactivate a user """ + self.assertTrue(self.user.is_active) + with patch( + "bookwyrm.models.activitypub_mixin.broadcast_task.delay" + ) as broadcast_mock: + self.user.delete() + + self.assertEqual(broadcast_mock.call_count, 1) + activity = json.loads(broadcast_mock.call_args[0][1]) + self.assertEqual(activity["type"], "Delete") + self.assertEqual(activity["object"], self.user.remote_id) + self.assertFalse(self.user.is_active) diff --git a/bookwyrm/tests/views/inbox/test_inbox_delete.py b/bookwyrm/tests/views/inbox/test_inbox_delete.py index 88ef288e..03598b88 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_delete.py +++ b/bookwyrm/tests/views/inbox/test_inbox_delete.py @@ -107,34 +107,29 @@ class InboxActivities(TestCase): def test_delete_user(self): """ delete a user """ - self.assertTrue( - models.User.objects.get(username="rat@example.com").is_active - ) + self.assertTrue(models.User.objects.get(username="rat@example.com").is_active) activity = { - '@context': 'https://www.w3.org/ns/activitystreams', - 'id': 'https://example.com/users/test-user#delete', - 'type': 'Delete', - 'actor': 'https://example.com/users/test-user', - 'to': ['https://www.w3.org/ns/activitystreams#Public'], - 'object': self.remote_user.remote_id, + "@context": "https://www.w3.org/ns/activitystreams", + "id": "https://example.com/users/test-user#delete", + "type": "Delete", + "actor": "https://example.com/users/test-user", + "to": ["https://www.w3.org/ns/activitystreams#Public"], + "object": self.remote_user.remote_id, } views.inbox.activity_task(activity) - self.assertFalse( - models.User.objects.get(username="rat@example.com").is_active - ) - + self.assertFalse(models.User.objects.get(username="rat@example.com").is_active) def test_delete_user_unknown(self): """ don't worry about it if we don't know the user """ self.assertEqual(models.User.objects.filter(is_active=True).count(), 2) activity = { - '@context': 'https://www.w3.org/ns/activitystreams', - 'id': 'https://example.com/users/test-user#delete', - 'type': 'Delete', - 'actor': 'https://example.com/users/test-user', - 'to': ['https://www.w3.org/ns/activitystreams#Public'], - 'object': 'https://example.com/users/test-user', + "@context": "https://www.w3.org/ns/activitystreams", + "id": "https://example.com/users/test-user#delete", + "type": "Delete", + "actor": "https://example.com/users/test-user", + "to": ["https://www.w3.org/ns/activitystreams#Public"], + "object": "https://example.com/users/test-user", } # nothing happens.