From 1b6dd666d95dea177f332575dc61683d1f8ab4a5 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 3 Feb 2021 10:16:38 -0800 Subject: [PATCH] Handling incoming list Creates and Updates --- bookwyrm/incoming.py | 31 ++++++++++- bookwyrm/tests/test_incoming.py | 97 ++++++++++++++++++++++++--------- 2 files changed, 100 insertions(+), 28 deletions(-) diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index 1e42d32ae..103b24fca 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -47,12 +47,20 @@ def shared_inbox(request): return HttpResponse() return HttpResponse(status=401) + # if this isn't a file ripe for refactor, I don't know what is. handlers = { 'Follow': handle_follow, 'Accept': handle_follow_accept, 'Reject': handle_follow_reject, 'Block': handle_block, - 'Create': handle_create, + 'Create': { + 'BookList': handle_create_list, + 'Note': handle_create_status, + 'Article': handle_create_status, + 'Review': handle_create_status, + 'Comment': handle_create_status, + 'Quotation': handle_create_status, + }, 'Delete': handle_delete_status, 'Like': handle_favorite, 'Announce': handle_boost, @@ -69,6 +77,7 @@ def shared_inbox(request): 'Person': handle_update_user, 'Edition': handle_update_edition, 'Work': handle_update_work, + 'BookList': handle_update_list, }, } activity_type = activity['type'] @@ -204,7 +213,25 @@ def handle_unblock(activity): @app.task -def handle_create(activity): +def handle_create_list(activity): + ''' a new list ''' + activity = activity['object'] + activitypub.BookList(**activity).to_model(models.List) + + +@app.task +def handle_update_list(activity): + ''' update a list ''' + try: + book_list = models.List.objects.get(id=activity['object']['id']) + except models.List.DoesNotExist: + return + activitypub.BookList( + **activity['object']).to_model(models.List, instance=book_list) + + +@app.task +def handle_create_status(activity): ''' someone did something, good on them ''' # deduplicate incoming activities activity = activity['object'] diff --git a/bookwyrm/tests/test_incoming.py b/bookwyrm/tests/test_incoming.py index 1ee7c59ec..8e07281cd 100644 --- a/bookwyrm/tests/test_incoming.py +++ b/bookwyrm/tests/test_incoming.py @@ -248,7 +248,69 @@ class Incoming(TestCase): self.assertEqual(follows.count(), 0) - def test_handle_create(self): + def test_handle_create_list(self): + ''' a new list ''' + activity = { + 'object': { + "id": "https://example.com/list/22", + "type": "BookList", + "totalItems": 1, + "first": "https://example.com/list/22?page=1", + "last": "https://example.com/list/22?page=1", + "name": "Test List", + "owner": "https://example.com/user/mouse", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [ + "https://example.com/user/mouse/followers" + ], + "summary": "summary text", + "curation": "curated", + "@context": "https://www.w3.org/ns/activitystreams" + } + } + incoming.handle_create_list(activity) + book_list = models.List.objects.get() + self.assertEqual(book_list.name, 'Test List') + self.assertEqual(book_list.curation, 'curated') + self.assertEqual(book_list.description, 'summary text') + self.assertEqual(book_list.remote_id, 'https://example.com/list/22') + + + def test_handle_update_list(self): + ''' a new list ''' + book_list = models.List.objects.create( + name='hi', remote_id='https://example.com/list/22', user=self.local_user) + activity = { + 'object': { + "id": "https://example.com/list/22", + "type": "BookList", + "totalItems": 1, + "first": "https://example.com/list/22?page=1", + "last": "https://example.com/list/22?page=1", + "name": "Test List", + "owner": "https://example.com/user/mouse", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [ + "https://example.com/user/mouse/followers" + ], + "summary": "summary text", + "curation": "curated", + "@context": "https://www.w3.org/ns/activitystreams" + } + } + incoming.handle_create_list(activity) + book_list.refresh_from_db() + self.assertEqual(book_list.name, 'Test List') + self.assertEqual(book_list.curation, 'curated') + self.assertEqual(book_list.description, 'summary text') + self.assertEqual(book_list.remote_id, 'https://example.com/list/22') + + + def test_handle_create_status(self): ''' the "it justs works" mode ''' self.assertEqual(models.Status.objects.count(), 1) @@ -259,7 +321,7 @@ class Incoming(TestCase): title='Test Book', remote_id='https://example.com/book/1') activity = {'object': status_data, 'type': 'Create'} - incoming.handle_create(activity) + incoming.handle_create_status(activity) status = models.Quotation.objects.get() self.assertEqual( @@ -270,16 +332,16 @@ class Incoming(TestCase): self.assertEqual(models.Status.objects.count(), 2) # while we're here, lets ensure we avoid dupes - incoming.handle_create(activity) + incoming.handle_create_status(activity) self.assertEqual(models.Status.objects.count(), 2) - def test_handle_create_unknown_type(self): + def test_handle_create_status_unknown_type(self): ''' folks send you all kinds of things ''' activity = {'object': {'id': 'hi'}, 'type': 'Fish'} - result = incoming.handle_create(activity) + result = incoming.handle_create_status(activity) self.assertIsNone(result) - def test_handle_create_remote_note_with_mention(self): + def test_handle_create_status_remote_note_with_mention(self): ''' should only create it under the right circumstances ''' self.assertEqual(models.Status.objects.count(), 1) self.assertFalse( @@ -290,7 +352,7 @@ class Incoming(TestCase): status_data = json.loads(datafile.read_bytes()) activity = {'object': status_data, 'type': 'Create'} - incoming.handle_create(activity) + incoming.handle_create_status(activity) status = models.Status.objects.last() self.assertEqual(status.content, 'test content in note') self.assertEqual(status.mention_users.first(), self.local_user) @@ -299,7 +361,7 @@ class Incoming(TestCase): self.assertEqual( models.Notification.objects.get().notification_type, 'MENTION') - def test_handle_create_remote_note_with_reply(self): + def test_handle_create_status_remote_note_with_reply(self): ''' should only create it under the right circumstances ''' self.assertEqual(models.Status.objects.count(), 1) self.assertFalse( @@ -312,7 +374,7 @@ class Incoming(TestCase): status_data['inReplyTo'] = self.status.remote_id activity = {'object': status_data, 'type': 'Create'} - incoming.handle_create(activity) + incoming.handle_create_status(activity) status = models.Status.objects.last() self.assertEqual(status.content, 'test content in note') self.assertEqual(status.reply_parent, self.status) @@ -566,20 +628,3 @@ class Incoming(TestCase): self.assertFalse(models.UserFollows.objects.exists()) self.assertFalse(models.UserFollowRequest.objects.exists()) - - def test_handle_unblock(self): - ''' undoing a block ''' - activity = { - "@context": "https://www.w3.org/ns/activitystreams", - "id": "https://friend.camp/users/tripofmice#blocks/1155/undo", - "type": "Undo", - "actor": "https://friend.camp/users/tripofmice", - "object": { - "id": "https://friend.camp/0a7d85f7-6359-4c03-8ab6-74e61a8fb678", - "type": "Block", - "actor": "https://friend.camp/users/tripofmice", - "object": "https://1b1a78582461.ngrok.io/user/mouse" - } - } - - self.remote_user.blocks.add(self.local_user)