From 876d9c2695cd3a0d4d1a345e56edeaae8429becc Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 16 May 2022 09:24:01 -0700 Subject: [PATCH 1/5] Fixes how backdated statuses are prioritized --- bookwyrm/activitystreams.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bookwyrm/activitystreams.py b/bookwyrm/activitystreams.py index f2dd43fb2..a90d7943b 100644 --- a/bookwyrm/activitystreams.py +++ b/bookwyrm/activitystreams.py @@ -298,8 +298,9 @@ def add_status_on_create_command(sender, instance, created): priority = HIGH # check if this is an old status, de-prioritize if so # (this will happen if federation is very slow, or, more expectedly, on csv import) - one_day = 60 * 60 * 24 - if (instance.created_date - instance.published_date).seconds > one_day: + if instance.published_date < timezone.now() - timedelta( + days=1 + ) or instance.created_date < instance.published_date - timedelta(days=1): priority = LOW add_status_task.apply_async( From fdd4691e007c33867fbd4a4ddb27a1e0b69ad754 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 16 May 2022 09:41:34 -0700 Subject: [PATCH 2/5] Adds unit test --- .../tests/activitystreams/test_signals.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/bookwyrm/tests/activitystreams/test_signals.py b/bookwyrm/tests/activitystreams/test_signals.py index 4db1875f9..f7c6c20bb 100644 --- a/bookwyrm/tests/activitystreams/test_signals.py +++ b/bookwyrm/tests/activitystreams/test_signals.py @@ -1,6 +1,10 @@ """ testing activitystreams """ +from datetime import datetime, timedelta from unittest.mock import patch + from django.test import TestCase +from django.utils import timezone + from bookwyrm import activitystreams, models @@ -62,6 +66,39 @@ class ActivitystreamsSignals(TestCase): self.assertEqual(args["args"][0], status.id) self.assertEqual(args["queue"], "high_priority") + def test_add_status_on_create_created_low_priority(self, *_): + """a new statuses has entered""" + # created later than publication + status = models.Status.objects.create( + user=self.remote_user, + content="hi", + privacy="public", + created_date=datetime(2022, 5, 16, tzinfo=timezone.utc), + published_date=datetime(2022, 5, 14, tzinfo=timezone.utc), + ) + with patch("bookwyrm.activitystreams.add_status_task.apply_async") as mock: + activitystreams.add_status_on_create_command(models.Status, status, False) + + self.assertEqual(mock.call_count, 1) + args = mock.call_args[1] + self.assertEqual(args["args"][0], status.id) + self.assertEqual(args["queue"], "low_priority") + + # published later than yesterday + status = models.Status.objects.create( + user=self.remote_user, + content="hi", + privacy="public", + published_date=timezone.now() - timedelta(days=1), + ) + with patch("bookwyrm.activitystreams.add_status_task.apply_async") as mock: + activitystreams.add_status_on_create_command(models.Status, status, False) + + self.assertEqual(mock.call_count, 1) + args = mock.call_args[1] + self.assertEqual(args["args"][0], status.id) + self.assertEqual(args["queue"], "low_priority") + def test_populate_streams_on_account_create_command(self, *_): """create streams for a user""" with patch("bookwyrm.activitystreams.populate_stream_task.delay") as mock: From fd43b56d31ebce09f11e42de8fb1109b7eda1e71 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 16 May 2022 10:11:45 -0700 Subject: [PATCH 3/5] Fixes celery error encountering Article type activities --- bookwyrm/activitypub/base_activity.py | 2 +- .../tests/views/inbox/test_inbox_create.py | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 6bee25f62..1aacfd1f9 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -43,7 +43,7 @@ def naive_parse(activity_objects, activity_json, serializer=None): serializer = activity_objects[activity_type] except KeyError as err: # we know this exists and that we can't handle it - if activity_type in ["Question"]: + if activity_type in ["Question", "Article"]: return None raise ActivitySerializerError(err) diff --git a/bookwyrm/tests/views/inbox/test_inbox_create.py b/bookwyrm/tests/views/inbox/test_inbox_create.py index 4ee366cfe..d08840b5b 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_create.py +++ b/bookwyrm/tests/views/inbox/test_inbox_create.py @@ -208,16 +208,44 @@ class InboxCreate(TestCase): self.assertEqual(book_list.description, "summary text") self.assertEqual(book_list.remote_id, "https://example.com/list/22") - def test_create_unsupported_type(self, *_): + def test_create_unsupported_type_question(self, *_): """ignore activities we know we can't handle""" activity = self.create_json activity["object"] = { "id": "https://example.com/status/887", "type": "Question", } - # just observer how it doesn't throw an error + # just observe how it doesn't throw an error views.inbox.activity_task(activity) + def test_create_unsupported_type_article(self, *_): + """ special case in unsupported type because we do know what it is""" + activity = self.create_json + activity["object"] = { + "id": "https://example.com/status/887", + "type": "Article", + "name": "hello", + "published": "2021-04-29T21:27:30.014235+00:00", + "attributedTo": "https://example.com/user/mouse", + "to": ["https://www.w3.org/ns/activitystreams#Public"], + "cc": ["https://example.com/user/mouse/followers"], + "sensitive": False, + "@context": "https://www.w3.org/ns/activitystreams", + } + # just observe how it doesn't throw an error + views.inbox.activity_task(activity) + + def test_create_unsupported_type_unknown(self, *_): + """Something truly unexpected should throw an error""" + activity = self.create_json + activity["object"] = { + "id": "https://example.com/status/887", + "type": "Blaaaah", + } + # error this time + with self.assertRaises(ActivitySerializerError): + views.inbox.activity_task(activity) + def test_create_unknown_type(self, *_): """ignore activities we know we've never heard of""" activity = self.create_json From b2775c51606b9f7dba2983f4d8d7f57a9b28b5d8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 16 May 2022 10:20:13 -0700 Subject: [PATCH 4/5] Check unsupported types before attempting to serialize --- bookwyrm/activitypub/base_activity.py | 4 ++-- bookwyrm/tests/views/inbox/test_inbox_create.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 1aacfd1f9..448d55637 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -39,12 +39,12 @@ def naive_parse(activity_objects, activity_json, serializer=None): activity_json["type"] = "PublicKey" activity_type = activity_json.get("type") + if activity_type in ["Question", "Article"]: + return None try: serializer = activity_objects[activity_type] except KeyError as err: # we know this exists and that we can't handle it - if activity_type in ["Question", "Article"]: - return None raise ActivitySerializerError(err) return serializer(activity_objects=activity_objects, **activity_json) diff --git a/bookwyrm/tests/views/inbox/test_inbox_create.py b/bookwyrm/tests/views/inbox/test_inbox_create.py index d08840b5b..cc1b8d508 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_create.py +++ b/bookwyrm/tests/views/inbox/test_inbox_create.py @@ -219,7 +219,7 @@ class InboxCreate(TestCase): views.inbox.activity_task(activity) def test_create_unsupported_type_article(self, *_): - """ special case in unsupported type because we do know what it is""" + """special case in unsupported type because we do know what it is""" activity = self.create_json activity["object"] = { "id": "https://example.com/status/887", From 8d2da587d990c03c131b3bf8ab7e2d65e370c42b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 16 May 2022 11:06:11 -0700 Subject: [PATCH 5/5] Prevent error when a book language has a null value --- bookwyrm/models/book.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 3ea8e1a8e..190046019 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -176,8 +176,8 @@ class Book(BookDataModel): """properties of this edition, as a string""" items = [ self.physical_format if hasattr(self, "physical_format") else None, - self.languages[0] + " language" - if self.languages and self.languages[0] != "English" + f"{self.languages[0]} language" + if self.languages and self.languages[0] and self.languages[0] != "English" else None, str(self.published_date.year) if self.published_date else None, ", ".join(self.publishers) if hasattr(self, "publishers") else None,