From c0f51fa6aa45720a6b8da3d50d5466ccf6ef89f6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 16 Oct 2020 15:40:23 -0700 Subject: [PATCH] Handle incomign update user activities --- bookwyrm/incoming.py | 16 ++++++++++- bookwyrm/tests/incoming/test_update_user.py | 30 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/tests/incoming/test_update_user.py diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index bbce14c1..dca5d12f 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -67,7 +67,7 @@ def shared_inbox(request): 'Like': handle_unfavorite, }, 'Update': { - 'Person': None,# TODO: handle_update_user + 'Person': handle_update_user, 'Document': handle_update_book, }, } @@ -293,6 +293,20 @@ def handle_tag(activity): status_builder.create_tag(user, book, activity['object']['name']) +@app.task +def handle_update_user(activity): + ''' receive an updated user Person activity object ''' + try: + user = models.User.objects.get(remote_id=activity['object']['id']) + except models.User.DoesNotExist: + # who is this person? who cares + return + activitypub.Person( + **activity['object'] + ).to_model(models.User, instance=user) + # model save() happens in the to_model function + + @app.task def handle_update_book(activity): ''' a remote instance changed a book (Document) ''' diff --git a/bookwyrm/tests/incoming/test_update_user.py b/bookwyrm/tests/incoming/test_update_user.py new file mode 100644 index 00000000..703078f1 --- /dev/null +++ b/bookwyrm/tests/incoming/test_update_user.py @@ -0,0 +1,30 @@ +import json +import pathlib +from django.test import TestCase + +from bookwyrm import models, incoming + + +class UpdateUser(TestCase): + def setUp(self): + self.user = models.User.objects.create_user( + 'mouse', 'mouse@mouse.com', 'mouseword', + remote_id='https://example.com/user/mouse', + local=False, + localname='mouse' + ) + + datafile = pathlib.Path(__file__).parent.joinpath( + '../data/ap_user.json' + ) + self.user_data = json.loads(datafile.read_bytes()) + + def test_handle_update_user(self): + self.assertIsNone(self.user.name) + self.assertEqual(self.user.localname, 'mouse') + + incoming.handle_update_user({'object': self.user_data}) + self.user = models.User.objects.get(id=self.user.id) + + self.assertEqual(self.user.name, 'MOUSE?? MOUSE!!') + self.assertEqual(self.user.localname, 'mouse')