mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-30 05:21:08 +00:00
Gracefully handle expect, unsupported activities
This commit is contained in:
parent
f792dd0dac
commit
caa261f4bf
3 changed files with 35 additions and 6 deletions
|
@ -52,10 +52,14 @@ def naive_parse(activity_objects, activity_json, serializer=None):
|
||||||
if activity_json.get("publicKeyPem"):
|
if activity_json.get("publicKeyPem"):
|
||||||
# ugh
|
# ugh
|
||||||
activity_json["type"] = "PublicKey"
|
activity_json["type"] = "PublicKey"
|
||||||
|
|
||||||
|
activity_type = activity_json.get("type")
|
||||||
try:
|
try:
|
||||||
activity_type = activity_json["type"]
|
|
||||||
serializer = activity_objects[activity_type]
|
serializer = activity_objects[activity_type]
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
|
# we know this exists and that we can't handle it
|
||||||
|
if activity_type in ["Question"]:
|
||||||
|
return None
|
||||||
raise ActivitySerializerError(e)
|
raise ActivitySerializerError(e)
|
||||||
|
|
||||||
return serializer(activity_objects=activity_objects, **activity_json)
|
return serializer(activity_objects=activity_objects, **activity_json)
|
||||||
|
|
|
@ -16,7 +16,11 @@ class Verb(ActivityObject):
|
||||||
|
|
||||||
def action(self):
|
def action(self):
|
||||||
""" usually we just want to update and save """
|
""" usually we just want to update and save """
|
||||||
self.object.to_model()
|
obj = self.object
|
||||||
|
# it may return None if the object is invalid in an expected way
|
||||||
|
# ie, Question type
|
||||||
|
if obj:
|
||||||
|
obj.to_model()
|
||||||
|
|
||||||
|
|
||||||
@dataclass(init=False)
|
@dataclass(init=False)
|
||||||
|
|
|
@ -6,6 +6,7 @@ from unittest.mock import patch
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from bookwyrm import models, views
|
from bookwyrm import models, views
|
||||||
|
from bookwyrm.activitypub import ActivitySerializerError
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-public-methods
|
# pylint: disable=too-many-public-methods
|
||||||
|
@ -51,7 +52,7 @@ class InboxCreate(TestCase):
|
||||||
}
|
}
|
||||||
models.SiteSettings.objects.create()
|
models.SiteSettings.objects.create()
|
||||||
|
|
||||||
def test_handle_create_status(self):
|
def test_create_status(self):
|
||||||
""" the "it justs works" mode """
|
""" the "it justs works" mode """
|
||||||
self.assertEqual(models.Status.objects.count(), 1)
|
self.assertEqual(models.Status.objects.count(), 1)
|
||||||
|
|
||||||
|
@ -82,7 +83,7 @@ class InboxCreate(TestCase):
|
||||||
views.inbox.activity_task(activity)
|
views.inbox.activity_task(activity)
|
||||||
self.assertEqual(models.Status.objects.count(), 2)
|
self.assertEqual(models.Status.objects.count(), 2)
|
||||||
|
|
||||||
def test_handle_create_status_remote_note_with_mention(self):
|
def test_create_status_remote_note_with_mention(self):
|
||||||
""" should only create it under the right circumstances """
|
""" should only create it under the right circumstances """
|
||||||
self.assertEqual(models.Status.objects.count(), 1)
|
self.assertEqual(models.Status.objects.count(), 1)
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
|
@ -105,7 +106,7 @@ class InboxCreate(TestCase):
|
||||||
)
|
)
|
||||||
self.assertEqual(models.Notification.objects.get().notification_type, "MENTION")
|
self.assertEqual(models.Notification.objects.get().notification_type, "MENTION")
|
||||||
|
|
||||||
def test_handle_create_status_remote_note_with_reply(self):
|
def test_create_status_remote_note_with_reply(self):
|
||||||
""" should only create it under the right circumstances """
|
""" should only create it under the right circumstances """
|
||||||
self.assertEqual(models.Status.objects.count(), 1)
|
self.assertEqual(models.Status.objects.count(), 1)
|
||||||
self.assertFalse(models.Notification.objects.filter(user=self.local_user))
|
self.assertFalse(models.Notification.objects.filter(user=self.local_user))
|
||||||
|
@ -126,7 +127,7 @@ class InboxCreate(TestCase):
|
||||||
self.assertTrue(models.Notification.objects.filter(user=self.local_user))
|
self.assertTrue(models.Notification.objects.filter(user=self.local_user))
|
||||||
self.assertEqual(models.Notification.objects.get().notification_type, "REPLY")
|
self.assertEqual(models.Notification.objects.get().notification_type, "REPLY")
|
||||||
|
|
||||||
def test_handle_create_list(self):
|
def test_create_list(self):
|
||||||
""" a new list """
|
""" a new list """
|
||||||
activity = self.create_json
|
activity = self.create_json
|
||||||
activity["object"] = {
|
activity["object"] = {
|
||||||
|
@ -149,3 +150,23 @@ class InboxCreate(TestCase):
|
||||||
self.assertEqual(book_list.curation, "curated")
|
self.assertEqual(book_list.curation, "curated")
|
||||||
self.assertEqual(book_list.description, "summary text")
|
self.assertEqual(book_list.description, "summary text")
|
||||||
self.assertEqual(book_list.remote_id, "https://example.com/list/22")
|
self.assertEqual(book_list.remote_id, "https://example.com/list/22")
|
||||||
|
|
||||||
|
def test_create_unsupported_type(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
|
||||||
|
views.inbox.activity_task(activity)
|
||||||
|
|
||||||
|
def test_create_unknown_type(self):
|
||||||
|
""" ignore activities we know we've never heard of """
|
||||||
|
activity = self.create_json
|
||||||
|
activity["object"] = {
|
||||||
|
"id": "https://example.com/status/887",
|
||||||
|
"type": "Threnody",
|
||||||
|
}
|
||||||
|
with self.assertRaises(ActivitySerializerError):
|
||||||
|
views.inbox.activity_task(activity)
|
||||||
|
|
Loading…
Reference in a new issue