From 9784686b1ce19c549bfeb8f7514e88c3e0e94464 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Sat, 29 Feb 2020 20:28:53 -0800 Subject: [PATCH] implement AP Delete of actors when their accounts are deleted fixes #63 --- activitypub.py | 10 ++++++++++ tests/test_activitypub.py | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/activitypub.py b/activitypub.py index 771a5f6..2b04f60 100644 --- a/activitypub.py +++ b/activitypub.py @@ -23,6 +23,7 @@ SUPPORTED_TYPES = ( 'Article', 'Audio', 'Create', + 'Delete', 'Follow', 'Image', 'Like', @@ -128,6 +129,15 @@ class InboxHandler(common.Handler): if type == 'Undo' and obj.get('type') == 'Follow': # skip actor fetch below; we don't need it to undo a follow return self.undo_follow(self.redirect_unwrap(activity)) + elif type == 'Delete': + id = obj.get('id') + if isinstance(id, str): + # assume this is an actor + # https://github.com/snarfed/bridgy-fed/issues/63 + for key in Follower.query().iter(keys_only=True): + if key.id().split(' ')[-1] == id: + key.delete() + return # fetch actor if necessary so we have name, profile photo, etc for elem in obj, activity: diff --git a/tests/test_activitypub.py b/tests/test_activitypub.py index f5e0627..74d7eaa 100644 --- a/tests/test_activitypub.py +++ b/tests/test_activitypub.py @@ -130,6 +130,15 @@ UNDO_FOLLOW_WRAPPED = { 'object': FOLLOW_WRAPPED, } +DELETE = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': 'https://mastodon.social/users/swentel#delete', + 'type': 'Delete', + 'actor': 'https://mastodon.social/users/swentel', + 'object': 'https://mastodon.social/users/swentel', +} + + @patch('requests.post') @patch('requests.get') @patch('requests.head') @@ -397,3 +406,16 @@ class ActivityPubTest(testutil.TestCase): 'object': 'http://snarfed.org/', }).encode()) self.assertEqual(501, got.status_int) + + def test_inbox_delete_actor(self, mock_head, mock_get, mock_post): + follower = Follower.get_or_create('realize.be', DELETE['actor']) + Follower.get_or_create('snarfed.org', DELETE['actor']) + # other unrelated follower + other = Follower.get_or_create('realize.be', 'https://mas.to/users/other') + self.assertEqual(3, Follower.query().count()) + + got = application.get_response('/realize.be/inbox', method='POST', + body=json_dumps(DELETE).encode()) + self.assertEqual(200, got.status_int) + + self.assertEqual([other], Follower.query().fetch())