fixes broadcasting for incoming activities

This commit is contained in:
Mouse Reeve 2021-02-07 16:23:20 -08:00
parent bf6aca5122
commit 2fcfebd4e5
5 changed files with 28 additions and 18 deletions

View file

@ -93,7 +93,7 @@ class ActivityObject:
with transaction.atomic():
# we can't set many to many and reverse fields on an unsaved object
try:
instance.save()
instance.save(broadcast=False)
except IntegrityError as e:
raise ActivitySerializerError(e)

View file

@ -321,14 +321,14 @@ class CollectionItemMixin(ActivitypubMixin):
activity_serializer = activitypub.Add
object_field = collection_field = None
def save(self, *args, **kwargs):
def save(self, *args, broadcast=True, **kwargs):
''' broadcast updated '''
created = not bool(self.id)
# first off, we want to save normally no matter what
super().save(*args, **kwargs)
# these shouldn't be edited, only created and deleted
if not created or not self.user.local:
if not broadcast or not created or not self.user.local:
return
# adding an obj to the collection
@ -368,17 +368,19 @@ class CollectionItemMixin(ActivitypubMixin):
class ActivityMixin(ActivitypubMixin):
''' add this mixin for models that are AP serializable '''
def save(self, *args, **kwargs):
def save(self, *args, broadcast=True, **kwargs):
''' broadcast activity '''
super().save(*args, **kwargs)
user = self.user if hasattr(self, 'user') else self.user_subject
self.broadcast(self.to_activity(), user)
if broadcast and user.local:
self.broadcast(self.to_activity(), user)
def delete(self, *args, **kwargs):
def delete(self, *args, broadcast=True, **kwargs):
''' nevermind, undo that activity '''
user = self.user if hasattr(self, 'user') else self.user_subject
self.broadcast(self.to_undo_activity(), user)
if broadcast and user.local:
self.broadcast(self.to_undo_activity(), user)
super().delete(*args, **kwargs)

View file

@ -71,7 +71,7 @@ class UserFollowRequest(ActivitypubMixin, UserRelationship):
status = 'follow_request'
activity_serializer = activitypub.Follow
def save(self, *args, **kwargs):
def save(self, broadcast=True, *args, **kwargs):
''' make sure the follow or block relationship doesn't already exist '''
try:
UserFollows.objects.get(
@ -85,7 +85,7 @@ class UserFollowRequest(ActivitypubMixin, UserRelationship):
return None
except (UserFollows.DoesNotExist, UserBlocks.DoesNotExist):
super().save(*args, **kwargs)
if self.user_subject.local and not self.user_object.local:
if broadcast and self.user_subject.local and not self.user_object.local:
self.broadcast(self.to_activity(), self.user_subject)

View file

@ -211,6 +211,9 @@ class KeyPair(ActivitypubMixin, BookWyrmModel):
def save(self, *args, **kwargs):
''' create a key pair '''
# no broadcasting happening here
if 'broadcast' in kwargs:
del kwargs['broadcast']
if not self.public_key:
self.private_key, self.public_key = create_key_pair()
return super().save(*args, **kwargs)

View file

@ -118,7 +118,7 @@ class Incoming(TestCase):
"object": "https://example.com/user/mouse"
}
with patch('bookwyrm.broadcast.broadcast_task.delay'):
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
incoming.handle_follow(activity)
# notification created
@ -146,9 +146,9 @@ class Incoming(TestCase):
}
self.local_user.manually_approves_followers = True
self.local_user.save()
self.local_user.save(broadcast=False)
with patch('bookwyrm.broadcast.broadcast_task.delay'):
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
incoming.handle_follow(activity)
# notification created
@ -392,11 +392,14 @@ class Incoming(TestCase):
def test_handle_delete_status(self):
''' remove a status '''
self.status.user = self.remote_user
self.status.save(broadcast=False)
self.assertFalse(self.status.deleted)
activity = {
'type': 'Delete',
'id': '%s/activity' % self.status.remote_id,
'actor': self.local_user.remote_id,
'actor': self.remote_user.remote_id,
'object': {'id': self.status.remote_id},
}
incoming.handle_delete_status(activity)
@ -408,6 +411,8 @@ class Incoming(TestCase):
def test_handle_delete_status_notifications(self):
''' remove a status with related notifications '''
self.status.user = self.remote_user
self.status.save(broadcast=False)
models.Notification.objects.create(
related_status=self.status,
user=self.local_user,
@ -423,7 +428,7 @@ class Incoming(TestCase):
activity = {
'type': 'Delete',
'id': '%s/activity' % self.status.remote_id,
'actor': self.local_user.remote_id,
'actor': self.remote_user.remote_id,
'object': {'id': self.status.remote_id},
}
incoming.handle_delete_status(activity)
@ -515,7 +520,6 @@ class Incoming(TestCase):
self.assertEqual(models.Boost.objects.count(), 0)
def test_handle_unboost(self):
''' undo a boost '''
activity = {
@ -613,9 +617,10 @@ class Incoming(TestCase):
def test_handle_blocks(self):
''' create a "block" database entry from an activity '''
self.local_user.followers.add(self.remote_user)
models.UserFollowRequest.objects.create(
user_subject=self.local_user,
user_object=self.remote_user)
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
models.UserFollowRequest.objects.create(
user_subject=self.local_user,
user_object=self.remote_user)
self.assertTrue(models.UserFollows.objects.exists())
self.assertTrue(models.UserFollowRequest.objects.exists())