Catch error in serializing unknown boosts

This commit is contained in:
Mouse Reeve 2021-03-13 08:13:20 -08:00
parent 1f4b3e9586
commit 919b166241
4 changed files with 25 additions and 7 deletions

View file

@ -258,10 +258,9 @@ def resolve_remote_id(remote_id, model=None, refresh=False, save=True):
# load the data and create the object
try:
data = get_data(remote_id)
except (ConnectorException, ConnectionError):
except ConnectorException:
raise ActivitySerializerError(
"Could not connect to host for remote_id in %s model: %s"
% (model.__name__, remote_id)
"Could not connect to host for remote_id in: %s" % (remote_id)
)
# determine the model implicitly, if not provided
if not model:

View file

@ -244,7 +244,7 @@ def get_data(url):
"User-Agent": settings.USER_AGENT,
},
)
except (RequestError, SSLError) as e:
except (RequestError, SSLError, ConnectionError) as e:
logger.exception(e)
raise ConnectorException()

View file

@ -115,13 +115,18 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
def ignore_activity(cls, activity):
""" keep notes if they are replies to existing statuses """
if activity.type == "Announce":
# keep it if the booster or the boosted are local
boosted = activitypub.resolve_remote_id(activity.object, save=False)
try:
boosted = activitypub.resolve_remote_id(activity.object, save=False)
except activitypub.ActivitySerializerError:
# if we can't load the status, definitely ignore it
return True
# keep the boost if we would keep the status
return cls.ignore_activity(boosted.to_activity_dataclass())
# keep if it if it's a custom type
if activity.type != "Note":
return False
# keep it if it's a reply to an existing status
if cls.objects.filter(remote_id=activity.inReplyTo).exists():
return False

View file

@ -8,8 +8,9 @@ from django.core.files.base import ContentFile
from django.db import IntegrityError
from django.test import TestCase
from django.utils import timezone
import responses
from bookwyrm import models, settings
from bookwyrm import activitypub, models, settings
# pylint: disable=too-many-public-methods
@ -385,3 +386,16 @@ class Status(TestCase):
status.mention_users.set([self.remote_user])
self.assertEqual(status.recipients, [self.remote_user])
@responses.activate
def test_ignore_activity_boost(self, _):
""" don't bother with most remote statuses """
activity = activitypub.Announce(
id="http://www.faraway.com/boost/12",
actor=self.remote_user.remote_id,
object="http://fish.com/nothing",
)
responses.add(responses.GET, "http://fish.com/nothing", status=404)
self.assertTrue(models.Status.ignore_activity(activity))