Fixes calls to to_model, init with activitypub partially serialized

This commit is contained in:
Mouse Reeve 2021-02-16 20:17:38 -08:00
parent 3f61675a0a
commit a9ca3a4290
6 changed files with 24 additions and 17 deletions

View file

@ -74,7 +74,8 @@ class ActivityObject:
is_subclass = issubclass(field.type, ActivityObject) is_subclass = issubclass(field.type, ActivityObject)
except TypeError: except TypeError:
is_subclass = False is_subclass = False
if is_subclass: # parse a dict into the appropriate activity
if is_subclass and isinstance(value, dict):
value = naive_parse( value = naive_parse(
activity_objects, value, serializer=field.type) activity_objects, value, serializer=field.type)

View file

@ -127,7 +127,7 @@ class AbstractConnector(AbstractMinimalConnector):
# create activitypub object # create activitypub object
work_activity = activitypub.Work(**work_data) work_activity = activitypub.Work(**work_data)
# this will dedupe automatically # this will dedupe automatically
work = work_activity.to_model(models.Work) work = work_activity.to_model(model=models.Work)
for author in self.get_authors_from_data(data): for author in self.get_authors_from_data(data):
work.authors.add(author) work.authors.add(author)
@ -141,7 +141,7 @@ class AbstractConnector(AbstractMinimalConnector):
mapped_data = dict_from_mappings(edition_data, self.book_mappings) mapped_data = dict_from_mappings(edition_data, self.book_mappings)
mapped_data['work'] = work.remote_id mapped_data['work'] = work.remote_id
edition_activity = activitypub.Edition(**mapped_data) edition_activity = activitypub.Edition(**mapped_data)
edition = edition_activity.to_model(models.Edition) edition = edition_activity.to_model(model=models.Edition)
edition.connector = self.connector edition.connector = self.connector
edition.save() edition.save()
@ -168,7 +168,7 @@ class AbstractConnector(AbstractMinimalConnector):
mapped_data = dict_from_mappings(data, self.author_mappings) mapped_data = dict_from_mappings(data, self.author_mappings)
activity = activitypub.Author(**mapped_data) activity = activitypub.Author(**mapped_data)
# this will dedupe # this will dedupe
return activity.to_model(models.Author) return activity.to_model(model=models.Author)
@abstractmethod @abstractmethod

View file

@ -367,8 +367,8 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
image_slug = value image_slug = value
# when it's an inline image (User avatar/icon, Book cover), it's a json # when it's an inline image (User avatar/icon, Book cover), it's a json
# blob, but when it's an attached image, it's just a url # blob, but when it's an attached image, it's just a url
if isinstance(image_slug, dict): if hasattr(image_slug, 'url'):
url = image_slug.get('url') url = image_slug.url
elif isinstance(image_slug, str): elif isinstance(image_slug, str):
url = image_slug url = image_slug
else: else:

View file

@ -100,7 +100,7 @@ class BaseActivity(TestCase):
''' catch mismatch between activity type and model type ''' ''' catch mismatch between activity type and model type '''
instance = ActivityObject(id='a', type='b') instance = ActivityObject(id='a', type='b')
with self.assertRaises(ActivitySerializerError): with self.assertRaises(ActivitySerializerError):
instance.to_model(models.User) instance.to_model(model=models.User)
def test_to_model_simple_fields(self): def test_to_model_simple_fields(self):
''' test setting simple fields ''' ''' test setting simple fields '''
@ -118,7 +118,7 @@ class BaseActivity(TestCase):
endpoints={}, endpoints={},
) )
activity.to_model(models.User, self.user) activity.to_model(model=models.User, instance=self.user)
self.assertEqual(self.user.name, 'New Name') self.assertEqual(self.user.name, 'New Name')
@ -136,9 +136,9 @@ class BaseActivity(TestCase):
endpoints={}, endpoints={},
) )
activity.publicKey['publicKeyPem'] = 'hi im secure' activity.publicKey.publicKeyPem = 'hi im secure'
activity.to_model(models.User, self.user) activity.to_model(model=models.User, instance=self.user)
self.assertEqual(self.user.key_pair.public_key, 'hi im secure') self.assertEqual(self.user.key_pair.public_key, 'hi im secure')
@responses.activate @responses.activate
@ -152,9 +152,15 @@ class BaseActivity(TestCase):
outbox='http://www.com/', outbox='http://www.com/',
followers='', followers='',
summary='', summary='',
publicKey=None, publicKey={
'id': 'hi',
'owner': self.user.remote_id,
'publicKeyPem': 'hi'},
endpoints={}, endpoints={},
icon={'url': 'http://www.example.com/image.jpg'} icon={
'type': 'Image',
'url': 'http://www.example.com/image.jpg'
}
) )
responses.add( responses.add(
@ -169,7 +175,7 @@ class BaseActivity(TestCase):
# this would trigger a broadcast because it's a local user # this would trigger a broadcast because it's a local user
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
activity.to_model(models.User, self.user) activity.to_model(model=models.User, instance=self.user)
self.assertIsNotNone(self.user.avatar.name) self.assertIsNotNone(self.user.avatar.name)
self.assertIsNotNone(self.user.avatar.file) self.assertIsNotNone(self.user.avatar.file)
@ -202,7 +208,7 @@ class BaseActivity(TestCase):
}, },
] ]
) )
update_data.to_model(models.Status, instance=status) update_data.to_model(model=models.Status, instance=status)
self.assertEqual(status.mention_users.first(), self.user) self.assertEqual(status.mention_users.first(), self.user)
self.assertEqual(status.mention_books.first(), book) self.assertEqual(status.mention_books.first(), book)
@ -239,7 +245,7 @@ class BaseActivity(TestCase):
# sets the celery task call to the function call # sets the celery task call to the function call
with patch( with patch(
'bookwyrm.activitypub.base_activity.set_related_field.delay'): 'bookwyrm.activitypub.base_activity.set_related_field.delay'):
update_data.to_model(models.Status, instance=status) update_data.to_model(model=models.Status, instance=status)
self.assertIsNone(status.attachments.first()) self.assertIsNone(status.attachments.first())

View file

@ -25,7 +25,7 @@ class Person(TestCase):
def test_user_to_model(self): def test_user_to_model(self):
activity = activitypub.Person(**self.user_data) activity = activitypub.Person(**self.user_data)
with patch('bookwyrm.models.user.set_remote_server.delay'): with patch('bookwyrm.models.user.set_remote_server.delay'):
user = activity.to_model(models.User) user = activity.to_model(models=models.User)
self.assertEqual(user.username, 'mouse@example.com') self.assertEqual(user.username, 'mouse@example.com')
self.assertEqual(user.remote_id, 'https://example.com/user/mouse') self.assertEqual(user.remote_id, 'https://example.com/user/mouse')
self.assertFalse(user.local) self.assertFalse(user.local)

View file

@ -46,7 +46,7 @@ class Quotation(TestCase):
def test_activity_to_model(self): def test_activity_to_model(self):
''' create a model instance from an activity object ''' ''' create a model instance from an activity object '''
activity = activitypub.Quotation(**self.status_data) activity = activitypub.Quotation(**self.status_data)
quotation = activity.to_model(models.Quotation) quotation = activity.to_model(model=models.Quotation)
self.assertEqual(quotation.book, self.book) self.assertEqual(quotation.book, self.book)
self.assertEqual(quotation.user, self.user) self.assertEqual(quotation.user, self.user)