Handling incoming list Creates and Updates

This commit is contained in:
Mouse Reeve 2021-02-03 10:16:38 -08:00
parent 627b2ae90a
commit 1b6dd666d9
2 changed files with 100 additions and 28 deletions

View file

@ -47,12 +47,20 @@ def shared_inbox(request):
return HttpResponse() return HttpResponse()
return HttpResponse(status=401) return HttpResponse(status=401)
# if this isn't a file ripe for refactor, I don't know what is.
handlers = { handlers = {
'Follow': handle_follow, 'Follow': handle_follow,
'Accept': handle_follow_accept, 'Accept': handle_follow_accept,
'Reject': handle_follow_reject, 'Reject': handle_follow_reject,
'Block': handle_block, '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, 'Delete': handle_delete_status,
'Like': handle_favorite, 'Like': handle_favorite,
'Announce': handle_boost, 'Announce': handle_boost,
@ -69,6 +77,7 @@ def shared_inbox(request):
'Person': handle_update_user, 'Person': handle_update_user,
'Edition': handle_update_edition, 'Edition': handle_update_edition,
'Work': handle_update_work, 'Work': handle_update_work,
'BookList': handle_update_list,
}, },
} }
activity_type = activity['type'] activity_type = activity['type']
@ -204,7 +213,25 @@ def handle_unblock(activity):
@app.task @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 ''' ''' someone did something, good on them '''
# deduplicate incoming activities # deduplicate incoming activities
activity = activity['object'] activity = activity['object']

View file

@ -248,7 +248,69 @@ class Incoming(TestCase):
self.assertEqual(follows.count(), 0) 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 ''' ''' the "it justs works" mode '''
self.assertEqual(models.Status.objects.count(), 1) self.assertEqual(models.Status.objects.count(), 1)
@ -259,7 +321,7 @@ class Incoming(TestCase):
title='Test Book', remote_id='https://example.com/book/1') title='Test Book', remote_id='https://example.com/book/1')
activity = {'object': status_data, 'type': 'Create'} activity = {'object': status_data, 'type': 'Create'}
incoming.handle_create(activity) incoming.handle_create_status(activity)
status = models.Quotation.objects.get() status = models.Quotation.objects.get()
self.assertEqual( self.assertEqual(
@ -270,16 +332,16 @@ class Incoming(TestCase):
self.assertEqual(models.Status.objects.count(), 2) self.assertEqual(models.Status.objects.count(), 2)
# while we're here, lets ensure we avoid dupes # 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) 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 ''' ''' folks send you all kinds of things '''
activity = {'object': {'id': 'hi'}, 'type': 'Fish'} activity = {'object': {'id': 'hi'}, 'type': 'Fish'}
result = incoming.handle_create(activity) result = incoming.handle_create_status(activity)
self.assertIsNone(result) 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 ''' ''' should only create it under the right circumstances '''
self.assertEqual(models.Status.objects.count(), 1) self.assertEqual(models.Status.objects.count(), 1)
self.assertFalse( self.assertFalse(
@ -290,7 +352,7 @@ class Incoming(TestCase):
status_data = json.loads(datafile.read_bytes()) status_data = json.loads(datafile.read_bytes())
activity = {'object': status_data, 'type': 'Create'} activity = {'object': status_data, 'type': 'Create'}
incoming.handle_create(activity) incoming.handle_create_status(activity)
status = models.Status.objects.last() status = models.Status.objects.last()
self.assertEqual(status.content, 'test content in note') self.assertEqual(status.content, 'test content in note')
self.assertEqual(status.mention_users.first(), self.local_user) self.assertEqual(status.mention_users.first(), self.local_user)
@ -299,7 +361,7 @@ class Incoming(TestCase):
self.assertEqual( self.assertEqual(
models.Notification.objects.get().notification_type, 'MENTION') 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 ''' ''' should only create it under the right circumstances '''
self.assertEqual(models.Status.objects.count(), 1) self.assertEqual(models.Status.objects.count(), 1)
self.assertFalse( self.assertFalse(
@ -312,7 +374,7 @@ class Incoming(TestCase):
status_data['inReplyTo'] = self.status.remote_id status_data['inReplyTo'] = self.status.remote_id
activity = {'object': status_data, 'type': 'Create'} activity = {'object': status_data, 'type': 'Create'}
incoming.handle_create(activity) incoming.handle_create_status(activity)
status = models.Status.objects.last() status = models.Status.objects.last()
self.assertEqual(status.content, 'test content in note') self.assertEqual(status.content, 'test content in note')
self.assertEqual(status.reply_parent, self.status) self.assertEqual(status.reply_parent, self.status)
@ -566,20 +628,3 @@ class Incoming(TestCase):
self.assertFalse(models.UserFollows.objects.exists()) self.assertFalse(models.UserFollows.objects.exists())
self.assertFalse(models.UserFollowRequest.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)