From 443750d6dbf381264b74654ddc64252d5f04d791 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 4 Oct 2021 09:14:04 -0700 Subject: [PATCH 1/3] f strings for activistreams tasks tests --- bookwyrm/tests/activitystreams/test_tasks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bookwyrm/tests/activitystreams/test_tasks.py b/bookwyrm/tests/activitystreams/test_tasks.py index f4c85e1b..1002e4a8 100644 --- a/bookwyrm/tests/activitystreams/test_tasks.py +++ b/bookwyrm/tests/activitystreams/test_tasks.py @@ -141,7 +141,7 @@ class Activitystreams(TestCase): call_args = mock.call_args self.assertEqual(call_args[0][0], status) self.assertEqual( - call_args[1]["stores"], ["{:d}-home".format(self.another_user.id)] + call_args[1]["stores"], [f"{self.another_user.id}-home"] ) @patch("bookwyrm.activitystreams.LocalStream.remove_object_from_related_stores") @@ -164,10 +164,10 @@ class Activitystreams(TestCase): call_args = mock.call_args self.assertEqual(call_args[0][0], status) self.assertTrue( - "{:d}-home".format(self.another_user.id) in call_args[1]["stores"] + f"{self.another_user.id}-home" in call_args[1]["stores"] ) self.assertTrue( - "{:d}-home".format(self.local_user.id) in call_args[1]["stores"] + f"{self.local_user.id}-home" in call_args[1]["stores"] ) @patch("bookwyrm.activitystreams.LocalStream.remove_object_from_related_stores") @@ -189,5 +189,5 @@ class Activitystreams(TestCase): call_args = mock.call_args self.assertEqual(call_args[0][0], status) self.assertEqual( - call_args[1]["stores"], ["{:d}-home".format(self.local_user.id)] + call_args[1]["stores"], [f"{self.local_user.id}-home"] ) From 321949f2fac201ebf66b0cd480e151f63f377769 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 4 Oct 2021 09:47:33 -0700 Subject: [PATCH 2/3] Lightly updates tests --- bookwyrm/activitystreams.py | 2 ++ bookwyrm/redis_store.py | 2 +- bookwyrm/tests/activitystreams/test_tasks.py | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bookwyrm/activitystreams.py b/bookwyrm/activitystreams.py index 1feb495b..f843641f 100644 --- a/bookwyrm/activitystreams.py +++ b/bookwyrm/activitystreams.py @@ -480,12 +480,14 @@ def handle_boost_task(boost_id): instance = models.Status.objects.get(id=boost_id) boosted = instance.boost.boosted_status + # previous boosts of this status old_versions = models.Boost.objects.filter( boosted_status__id=boosted.id, created_date__lt=instance.created_date, ) for stream in streams.values(): + # people who should see the boost (not people who see the original status) audience = stream.get_stores_for_object(instance) stream.remove_object_from_related_stores(boosted, stores=audience) for status in old_versions: diff --git a/bookwyrm/redis_store.py b/bookwyrm/redis_store.py index 521e73b2..78f373a2 100644 --- a/bookwyrm/redis_store.py +++ b/bookwyrm/redis_store.py @@ -35,7 +35,7 @@ class RedisStore(ABC): def remove_object_from_related_stores(self, obj, stores=None): """remove an object from all stores""" - stores = stores or self.get_stores_for_object(obj) + stores = self.get_stores_for_object(obj) if stores is None else stores pipeline = r.pipeline() for store in stores: pipeline.zrem(store, -1, obj.id) diff --git a/bookwyrm/tests/activitystreams/test_tasks.py b/bookwyrm/tests/activitystreams/test_tasks.py index 1002e4a8..88704783 100644 --- a/bookwyrm/tests/activitystreams/test_tasks.py +++ b/bookwyrm/tests/activitystreams/test_tasks.py @@ -125,7 +125,7 @@ class Activitystreams(TestCase): @patch("bookwyrm.activitystreams.BooksStream.remove_object_from_related_stores") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") def test_boost_to_another_timeline(self, *_): - """add a boost and deduplicate the boosted status on the timeline""" + """boost from a non-follower doesn't remove original status from feed""" status = models.Status.objects.create(user=self.local_user, content="hi") with patch("bookwyrm.activitystreams.handle_boost_task.delay"): boost = models.Boost.objects.create( @@ -138,6 +138,7 @@ class Activitystreams(TestCase): activitystreams.handle_boost_task(boost.id) self.assertTrue(mock.called) + self.assertEqual(mock.call_count, 1) call_args = mock.call_args self.assertEqual(call_args[0][0], status) self.assertEqual( From ac70a810af9cf5136baf63525d34b8a607e8b447 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 4 Oct 2021 09:58:05 -0700 Subject: [PATCH 3/3] Boosts by remote users --- bookwyrm/tests/activitystreams/test_tasks.py | 48 +++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/bookwyrm/tests/activitystreams/test_tasks.py b/bookwyrm/tests/activitystreams/test_tasks.py index 88704783..80b0b771 100644 --- a/bookwyrm/tests/activitystreams/test_tasks.py +++ b/bookwyrm/tests/activitystreams/test_tasks.py @@ -22,6 +22,16 @@ class Activitystreams(TestCase): local=True, localname="nutria", ) + with patch("bookwyrm.models.user.set_remote_server.delay"): + self.remote_user = models.User.objects.create_user( + "rat", + "rat@rat.com", + "ratword", + local=False, + remote_id="https://example.com/users/rat", + inbox="https://example.com/users/rat/inbox", + outbox="https://example.com/users/rat/outbox", + ) work = models.Work.objects.create(title="test work") self.book = models.Edition.objects.create(title="test book", parent_work=work) with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): @@ -141,9 +151,29 @@ class Activitystreams(TestCase): self.assertEqual(mock.call_count, 1) call_args = mock.call_args self.assertEqual(call_args[0][0], status) - self.assertEqual( - call_args[1]["stores"], [f"{self.another_user.id}-home"] - ) + self.assertEqual(call_args[1]["stores"], [f"{self.another_user.id}-home"]) + + @patch("bookwyrm.activitystreams.LocalStream.remove_object_from_related_stores") + @patch("bookwyrm.activitystreams.BooksStream.remove_object_from_related_stores") + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") + def test_boost_to_another_timeline_remote(self, *_): + """boost from a remote non-follower doesn't remove original status from feed""" + status = models.Status.objects.create(user=self.local_user, content="hi") + with patch("bookwyrm.activitystreams.handle_boost_task.delay"): + boost = models.Boost.objects.create( + boosted_status=status, + user=self.remote_user, + ) + with patch( + "bookwyrm.activitystreams.HomeStream.remove_object_from_related_stores" + ) as mock: + activitystreams.handle_boost_task(boost.id) + + self.assertTrue(mock.called) + self.assertEqual(mock.call_count, 1) + call_args = mock.call_args + self.assertEqual(call_args[0][0], status) + self.assertEqual(call_args[1]["stores"], []) @patch("bookwyrm.activitystreams.LocalStream.remove_object_from_related_stores") @patch("bookwyrm.activitystreams.BooksStream.remove_object_from_related_stores") @@ -164,12 +194,8 @@ class Activitystreams(TestCase): self.assertTrue(mock.called) call_args = mock.call_args self.assertEqual(call_args[0][0], status) - self.assertTrue( - f"{self.another_user.id}-home" in call_args[1]["stores"] - ) - self.assertTrue( - f"{self.local_user.id}-home" in call_args[1]["stores"] - ) + self.assertTrue(f"{self.another_user.id}-home" in call_args[1]["stores"]) + self.assertTrue(f"{self.local_user.id}-home" in call_args[1]["stores"]) @patch("bookwyrm.activitystreams.LocalStream.remove_object_from_related_stores") @patch("bookwyrm.activitystreams.BooksStream.remove_object_from_related_stores") @@ -189,6 +215,4 @@ class Activitystreams(TestCase): self.assertTrue(mock.called) call_args = mock.call_args self.assertEqual(call_args[0][0], status) - self.assertEqual( - call_args[1]["stores"], [f"{self.local_user.id}-home"] - ) + self.assertEqual(call_args[1]["stores"], [f"{self.local_user.id}-home"])