Special error for catching activitypub serialization issues

This commit is contained in:
Mouse Reeve 2020-11-12 11:59:34 -08:00
parent 2f0b91d843
commit 648e7a7581
2 changed files with 14 additions and 4 deletions

View file

@ -8,6 +8,10 @@ from django.db.models.fields.related_descriptors \
import ForwardManyToOneDescriptor
class ActivitySerializerError(ValueError):
''' routine problems serializing activitypub json '''
class ActivityEncoder(JSONEncoder):
''' used to convert an Activity object into json '''
def default(self, o):
@ -66,7 +70,8 @@ class ActivityObject:
value = kwargs[field.name]
except KeyError:
if field.default == MISSING:
raise TypeError('Missing required field: %s' % field.name)
raise ActivitySerializerError(\
'Missing required field: %s' % field.name)
value = field.default
setattr(self, field.name, value)
@ -74,7 +79,7 @@ class ActivityObject:
def to_model(self, model, instance=None):
''' convert from an activity to a model instance '''
if not isinstance(self, model.activity_serializer):
raise TypeError('Wrong activity type for model')
raise ActivitySerializerError('Wrong activity type for model')
# check for an existing instance, if we're not updating a known obj
if not instance:
@ -136,6 +141,7 @@ def resolve_foreign_key(model, remote_id):
).first()
if not result:
raise ValueError('Could not resolve remote_id in %s model: %s' % \
raise ActivitySerializerError(
'Could not resolve remote_id in %s model: %s' % \
(model.__name__, remote_id))
return result

View file

@ -282,7 +282,11 @@ def handle_unfavorite(activity):
@app.task
def handle_boost(activity):
''' someone gave us a boost! '''
boost = activitypub.Boost(**activity).to_model(models.Boost)
try:
boost = activitypub.Boost(**activity).to_model(models.Boost)
except activitypub.ActivitySerializerError:
# this probably just means we tried to boost an unknown status
return
if not boost.user.local:
status_builder.create_notification(