From a29ab19166f4b45ddccaf4c45ab91182394e84a6 Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Sat, 25 Apr 2020 03:18:21 +0300 Subject: [PATCH] Extract ActivityPub ID from Diaspora paylaods Extract ActivityPub ID from Diaspora payloads if found as the `activitypub_id` property. --- CHANGELOG.md | 1 + federation/entities/diaspora/mappers.py | 2 + .../tests/entities/diaspora/test_mappers.py | 38 +++++++++++++++- .../tests/fixtures/payloads/diaspora.py | 43 +++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dad18e..8f5d9a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ * Extract Diaspora guid from ActivityPub payloads implementing the Diaspora extension. * Add Diaspora extension and guid to outbound ActivityPub payloads, if available. For profiles, also add handle. + * Extract ActivityPub ID from Diaspora payloads if found as the `activitypub_id` property. * Add ActivityPub ID to outbound Diaspora payloads of types comment, post and profile, if an URL given as `id`. diff --git a/federation/entities/diaspora/mappers.py b/federation/entities/diaspora/mappers.py index daff354..5c49640 100644 --- a/federation/entities/diaspora/mappers.py +++ b/federation/entities/diaspora/mappers.py @@ -176,6 +176,8 @@ def transform_attributes(attrs, cls): value = "" if key == "text": transformed["raw_content"] = value + elif key == "activitypub_id": + transformed["id"] = value elif key == "author": if cls == DiasporaProfile: # Diaspora Profile XML message contains no GUID. We need the guid. Fetch it. diff --git a/federation/tests/entities/diaspora/test_mappers.py b/federation/tests/entities/diaspora/test_mappers.py index be229ad..5090b4a 100644 --- a/federation/tests/entities/diaspora/test_mappers.py +++ b/federation/tests/entities/diaspora/test_mappers.py @@ -18,7 +18,8 @@ from federation.tests.fixtures.payloads import ( DIASPORA_POST_WITH_PHOTOS, DIASPORA_CONTACT, DIASPORA_PROFILE_EMPTY_TAGS, DIASPORA_RESHARE, DIASPORA_RESHARE_WITH_EXTRA_PROPERTIES, DIASPORA_POST_SIMPLE_WITH_MENTION, - DIASPORA_PROFILE_FIRST_NAME_ONLY, DIASPORA_POST_COMMENT_NESTED) + DIASPORA_PROFILE_FIRST_NAME_ONLY, DIASPORA_POST_COMMENT_NESTED, DIASPORA_POST_ACTIVITYPUB_ID, + DIASPORA_POST_COMMENT_ACTIVITYPUB_ID, DIASPORA_PROFILE_ACTIVITYPUB_ID) from federation.types import UserType, ReceiverVariant @@ -31,6 +32,16 @@ class TestDiasporaEntityMappersReceive: post = entities[0] assert post._mentions == {'jaywink@jasonrobinson.me'} + def test_message_to_objects_post__with_activitypub_id(self): + entities = message_to_objects(DIASPORA_POST_ACTIVITYPUB_ID, "alice@alice.diaspora.example.org") + assert len(entities) == 1 + post = entities[0] + assert isinstance(post, DiasporaPost) + assert isinstance(post, Post) + assert post.guid == "((guidguidguidguidguidguidguid))" + assert post.handle == "alice@alice.diaspora.example.org" + assert post.id == "https://alice.diaspora.example.org/posts/1" + def test_message_to_objects_simple_post(self): entities = message_to_objects(DIASPORA_POST_SIMPLE, "alice@alice.diaspora.example.org") assert len(entities) == 1 @@ -80,6 +91,21 @@ class TestDiasporaEntityMappersReceive: ] mock_validate.assert_called_once_with() + @patch("federation.entities.diaspora.mappers.DiasporaComment._validate_signatures") + def test_message_to_objects_comment__activitypub_id(self, mock_validate): + entities = message_to_objects(DIASPORA_POST_COMMENT_ACTIVITYPUB_ID, "alice@alice.diaspora.example.org", + sender_key_fetcher=Mock()) + assert len(entities) == 1 + comment = entities[0] + assert isinstance(comment, DiasporaComment) + assert isinstance(comment, Comment) + assert comment.target_guid == "((parent_guidparent_guidparent_guidparent_guid))" + assert comment.root_target_guid == "" + assert comment.guid == "((guidguidguidguidguidguid))" + assert comment.handle == "alice@alice.diaspora.example.org" + assert comment.id == "https://alice.diaspora.example.org/comments/1" + mock_validate.assert_called_once_with() + @patch("federation.entities.diaspora.mappers.DiasporaComment._validate_signatures") def test_message_to_objects_nested_comment(self, mock_validate): entities = message_to_objects(DIASPORA_POST_COMMENT_NESTED, "alice@alice.diaspora.example.org", @@ -141,6 +167,16 @@ class TestDiasporaEntityMappersReceive: assert profile.nsfw == False assert profile.tag_list == ["socialfederation", "federation"] + @patch("federation.entities.diaspora.mappers.retrieve_and_parse_profile", return_value=Mock( + id="bob@example.com", + )) + def test_message_to_objects_profile__activitypub_id(self, mock_parse): + entities = message_to_objects(DIASPORA_PROFILE_ACTIVITYPUB_ID, "bob@example.com") + assert len(entities) == 1 + profile = entities[0] + assert profile.handle == "bob@example.com" + assert profile.id == "https://example.com/bob" + @patch("federation.entities.diaspora.mappers.retrieve_and_parse_profile", return_value=Mock( id="bob@example.com", )) diff --git a/federation/tests/fixtures/payloads/diaspora.py b/federation/tests/fixtures/payloads/diaspora.py index 005c863..0fa3789 100644 --- a/federation/tests/fixtures/payloads/diaspora.py +++ b/federation/tests/fixtures/payloads/diaspora.py @@ -25,6 +25,18 @@ DIASPORA_ENCRYPTED_PAYLOAD = """{ } """ +DIASPORA_POST_ACTIVITYPUB_ID = """ + + ((status message)) + ((guidguidguidguidguidguidguid)) + alice@alice.diaspora.example.org + false + 2011-07-20T01:36:07Z + Socialhome + https://alice.diaspora.example.org/posts/1 + +""" + DIASPORA_POST_SIMPLE = """ ((status message)) @@ -36,6 +48,7 @@ DIASPORA_POST_SIMPLE = """ """ + DIASPORA_POST_SIMPLE_WITH_MENTION = """ ((status message)) @{Jason Robinson 🐍🍻; jaywink@jasonrobinson.me} @@ -91,6 +104,18 @@ DIASPORA_POST_COMMENT = """ """ +DIASPORA_POST_COMMENT_ACTIVITYPUB_ID = """ + + ((guidguidguidguidguidguid)) + ((parent_guidparent_guidparent_guidparent_guid)) + ((base64-encoded data)) + ((text)) + alice@alice.diaspora.example.org + ((signature)) + https://alice.diaspora.example.org/comments/1 + +""" + DIASPORA_POST_COMMENT_NESTED = """ ((guidguidguidguidguidguid)) @@ -132,6 +157,24 @@ DIASPORA_PROFILE = """ """ +DIASPORA_PROFILE_ACTIVITYPUB_ID = """ + + bob@example.com + Bob + Bobertson + https://example.com/uploads/images/thumb_large_c833747578b5.jpg + https://example.com/uploads/images/thumb_small_c8b147578b5.jpg + https://example.com/uploads/images/thumb_medium_c8b1aab04f3.jpg + + A cool bio + Helsinki + true + false + #socialfederation #federation + https://example.com/bob + +""" + DIASPORA_PROFILE_FIRST_NAME_ONLY = """ bob@example.com