From 6e403cb1aed2b3b91bf18614c8e80e560d32ad7f Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Wed, 7 Aug 2019 00:05:49 +0300 Subject: [PATCH] Support outgoing retraction of shares in ActivityPub --- federation/entities/activitypub/entities.py | 18 +++++++++++++++--- .../entities/activitypub/test_entities.py | 16 ++++++++++++++++ federation/tests/fixtures/entities.py | 11 +++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/federation/entities/activitypub/entities.py b/federation/entities/activitypub/entities.py index 8743dc0..7f615b3 100644 --- a/federation/entities/activitypub/entities.py +++ b/federation/entities/activitypub/entities.py @@ -209,17 +209,29 @@ class ActivitypubProfile(ActivitypubEntityMixin, Profile): class ActivitypubRetraction(ActivitypubEntityMixin, Retraction): - _type = ObjectType.TOMBSTONE.value + def resolve_object_type(self): + return { + "Comment": ObjectType.TOMBSTONE.value, + "Post": ObjectType.TOMBSTONE.value, + "Share": ActivityType.ANNOUNCE.value, + }.get(self.entity_type) + + def resolve_type(self): + return { + "Comment": ActivityType.DELETE.value, + "Post": ActivityType.DELETE.value, + "Share": ActivityType.UNDO.value, + }.get(self.entity_type) def to_as2(self) -> Dict: as2 = { "@context": CONTEXTS_DEFAULT, "id": self.activity_id, - "type": ActivityType.DELETE.value, + "type": self.resolve_type(), "actor": self.actor_id, "object": { "id": self.target_id, - "type": self._type, + "type": self.resolve_object_type(), }, "published": self.created_at.isoformat(), } diff --git a/federation/tests/entities/activitypub/test_entities.py b/federation/tests/entities/activitypub/test_entities.py index 3f9bbc6..28f89d0 100644 --- a/federation/tests/entities/activitypub/test_entities.py +++ b/federation/tests/entities/activitypub/test_entities.py @@ -166,6 +166,22 @@ class TestEntitiesConvertToAS2: 'published': '2019-04-27T00:00:00', } + def test_retraction_to_as2__announce(self, activitypubretraction_announce): + result = activitypubretraction_announce.to_as2() + assert result == { + '@context': [ + 'https://www.w3.org/ns/activitystreams', + ], + 'type': 'Undo', + 'id': 'http://127.0.0.1:8000/post/123456/#delete', + 'actor': 'http://127.0.0.1:8000/profile/123456/', + 'object': { + 'id': 'http://127.0.0.1:8000/post/123456/activity', + 'type': 'Announce', + }, + 'published': '2019-04-27T00:00:00', + } + class TestEntitiesPostReceive: @patch("federation.utils.activitypub.retrieve_and_parse_profile", autospec=True) diff --git a/federation/tests/fixtures/entities.py b/federation/tests/fixtures/entities.py index 0fd9a14..d88812c 100644 --- a/federation/tests/fixtures/entities.py +++ b/federation/tests/fixtures/entities.py @@ -93,6 +93,17 @@ def activitypubretraction(): ) +@pytest.fixture +def activitypubretraction_announce(): + with freeze_time("2019-04-27"): + return ActivitypubRetraction( + target_id="http://127.0.0.1:8000/post/123456/activity", + activity_id="http://127.0.0.1:8000/post/123456/#delete", + actor_id="http://127.0.0.1:8000/profile/123456/", + entity_type="Share", + ) + + @pytest.fixture def activitypubundofollow(): return ActivitypubFollow(