mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-18 07:33:57 +00:00
Broadcast deletions
This commit is contained in:
parent
5b7f2007e8
commit
ebdbdc8790
4 changed files with 40 additions and 20 deletions
|
@ -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)
|
||||
|
|
|
@ -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 """
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue