Merge branch 'main' into debug-toolbar

This commit is contained in:
Mouse Reeve 2022-05-19 10:07:16 -07:00
commit 6074af6cde
5 changed files with 74 additions and 8 deletions

View file

@ -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"]:
return None
raise ActivitySerializerError(err)
return serializer(activity_objects=activity_objects, **activity_json)

View file

@ -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(

View file

@ -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,

View file

@ -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:

View file

@ -208,14 +208,42 @@ 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, *_):