From ee2121095c7163e08cb80ae4980e84fcfa107556 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 15 Dec 2020 12:39:09 -0800 Subject: [PATCH] Separate update editon and work functions --- bookwyrm/incoming.py | 12 ++++++--- bookwyrm/models/fields.py | 2 ++ bookwyrm/tests/test_incoming.py | 47 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index e8576022..9c8c2887 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -66,8 +66,8 @@ def shared_inbox(request): }, 'Update': { 'Person': handle_update_user, - 'Edition': handle_update_book, - 'Work': handle_update_book, + 'Edition': handle_update_edition, + 'Work': handle_update_work, }, } activity_type = activity['type'] @@ -338,6 +338,12 @@ def handle_update_user(activity): @app.task -def handle_update_book(activity): +def handle_update_edition(activity): ''' a remote instance changed a book (Document) ''' activitypub.Edition(**activity['object']).to_model(models.Edition) + + +@app.task +def handle_update_work(activity): + ''' a remote instance changed a book (Document) ''' + activitypub.Work(**activity['object']).to_model(models.Work) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 747167f0..f6142e37 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -237,6 +237,8 @@ class ManyToManyField(ActivitypubFieldMixin, models.ManyToManyField): def field_from_activity(self, value): items = [] + if value is None or value is MISSING: + return [] for remote_id in value: try: validate_remote_id(remote_id) diff --git a/bookwyrm/tests/test_incoming.py b/bookwyrm/tests/test_incoming.py index 34375321..d8e85ef2 100644 --- a/bookwyrm/tests/test_incoming.py +++ b/bookwyrm/tests/test_incoming.py @@ -432,3 +432,50 @@ class Incoming(TestCase): models.Boost.objects.create( boosted_status=self.status, user=self.remote_user) incoming.handle_unboost(activity) + + def test_handle_update_user(self): + ''' update an existing user ''' + datafile = pathlib.Path(__file__).parent.joinpath( + 'data/ap_user.json') + userdata = json.loads(datafile.read_bytes()) + del userdata['icon'] + self.assertEqual(self.local_user.name, '') + incoming.handle_update_user({'object': userdata}) + user = models.User.objects.get(id=self.local_user.id) + self.assertEqual(user.name, 'MOUSE?? MOUSE!!') + + + def test_handle_update_edition(self): + ''' update an existing edition ''' + datafile = pathlib.Path(__file__).parent.joinpath( + 'data/fr_edition.json') + bookdata = json.loads(datafile.read_bytes()) + + book = models.Edition.objects.create( + title='Test Book', remote_id='https://bookwyrm.social/book/5989') + + del bookdata['authors'] + self.assertEqual(book.title, 'Test Book') + with patch( + 'bookwyrm.activitypub.base_activity.set_related_field.delay'): + incoming.handle_update_edition({'object': bookdata}) + book = models.Edition.objects.get(id=book.id) + self.assertEqual(book.title, 'Piranesi') + + + def test_handle_update_work(self): + ''' update an existing edition ''' + datafile = pathlib.Path(__file__).parent.joinpath( + 'data/fr_work.json') + bookdata = json.loads(datafile.read_bytes()) + + book = models.Work.objects.create( + title='Test Book', remote_id='https://bookwyrm.social/book/5988') + + del bookdata['authors'] + self.assertEqual(book.title, 'Test Book') + with patch( + 'bookwyrm.activitypub.base_activity.set_related_field.delay'): + incoming.handle_update_work({'object': bookdata}) + book = models.Work.objects.get(id=book.id) + self.assertEqual(book.title, 'Piranesi')