mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-06 15:28:53 +00:00
Special error for catching activitypub serialization issues
This commit is contained in:
parent
2f0b91d843
commit
648e7a7581
2 changed files with 14 additions and 4 deletions
|
@ -8,6 +8,10 @@ from django.db.models.fields.related_descriptors \
|
||||||
import ForwardManyToOneDescriptor
|
import ForwardManyToOneDescriptor
|
||||||
|
|
||||||
|
|
||||||
|
class ActivitySerializerError(ValueError):
|
||||||
|
''' routine problems serializing activitypub json '''
|
||||||
|
|
||||||
|
|
||||||
class ActivityEncoder(JSONEncoder):
|
class ActivityEncoder(JSONEncoder):
|
||||||
''' used to convert an Activity object into json '''
|
''' used to convert an Activity object into json '''
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
|
@ -66,7 +70,8 @@ class ActivityObject:
|
||||||
value = kwargs[field.name]
|
value = kwargs[field.name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if field.default == MISSING:
|
if field.default == MISSING:
|
||||||
raise TypeError('Missing required field: %s' % field.name)
|
raise ActivitySerializerError(\
|
||||||
|
'Missing required field: %s' % field.name)
|
||||||
value = field.default
|
value = field.default
|
||||||
setattr(self, field.name, value)
|
setattr(self, field.name, value)
|
||||||
|
|
||||||
|
@ -74,7 +79,7 @@ class ActivityObject:
|
||||||
def to_model(self, model, instance=None):
|
def to_model(self, model, instance=None):
|
||||||
''' convert from an activity to a model instance '''
|
''' convert from an activity to a model instance '''
|
||||||
if not isinstance(self, model.activity_serializer):
|
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
|
# check for an existing instance, if we're not updating a known obj
|
||||||
if not instance:
|
if not instance:
|
||||||
|
@ -136,6 +141,7 @@ def resolve_foreign_key(model, remote_id):
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
if not result:
|
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))
|
(model.__name__, remote_id))
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -282,7 +282,11 @@ def handle_unfavorite(activity):
|
||||||
@app.task
|
@app.task
|
||||||
def handle_boost(activity):
|
def handle_boost(activity):
|
||||||
''' someone gave us a boost! '''
|
''' 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:
|
if not boost.user.local:
|
||||||
status_builder.create_notification(
|
status_builder.create_notification(
|
||||||
|
|
Loading…
Reference in a new issue