From 9972b9d3fb4da3cf33f1cca7439f09e43eebe96e Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Sat, 25 Apr 2020 00:32:33 +0300 Subject: [PATCH] Extract Diaspora guid Diaspora entity guid will now be extracted from ActivityPub payloads implementing the Diaspora extension. --- CHANGELOG.md | 2 + federation/entities/activitypub/mappers.py | 4 +- .../entities/activitypub/test_mappers.py | 11 +++- .../tests/fixtures/payloads/activitypub.py | 52 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 842ce30..d793497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ * `str` sender id The function will be called for each generated payload. + +* Diaspora entity guid will now be extracted from ActivityPub payloads implementing the Diaspora extension. ### Changed diff --git a/federation/entities/activitypub/mappers.py b/federation/entities/activitypub/mappers.py index 1506a48..e38b0bd 100644 --- a/federation/entities/activitypub/mappers.py +++ b/federation/entities/activitypub/mappers.py @@ -227,7 +227,7 @@ def get_outbound_entity(entity: BaseEntity, private_key): def message_to_objects( - message: Dict, sender: str, sender_key_fetcher: Callable[[str], str] = None, user: UserType = None, + message: Dict, sender: str = "", sender_key_fetcher: Callable[[str], str] = None, user: UserType = None, ) -> List: """ Takes in a message extracted by a protocol and maps it to entities. @@ -267,6 +267,8 @@ def transform_attribute( transformed["_media_type"] = "text/html" transformed["raw_content"] = payload.get('content').strip() transformed["_rendered_content"] = transformed["raw_content"] + elif key == "diaspora:guid": + transformed["guid"] = value elif key == "endpoints" and isinstance(value, dict): if "inboxes" not in transformed: transformed["inboxes"] = {"private": None, "public": None} diff --git a/federation/tests/entities/activitypub/test_mappers.py b/federation/tests/entities/activitypub/test_mappers.py index f8c5628..fa981b9 100644 --- a/federation/tests/entities/activitypub/test_mappers.py +++ b/federation/tests/entities/activitypub/test_mappers.py @@ -12,7 +12,7 @@ from federation.tests.fixtures.payloads import ( ACTIVITYPUB_FOLLOW, ACTIVITYPUB_PROFILE, ACTIVITYPUB_PROFILE_INVALID, ACTIVITYPUB_UNDO_FOLLOW, ACTIVITYPUB_POST, ACTIVITYPUB_COMMENT, ACTIVITYPUB_RETRACTION, ACTIVITYPUB_SHARE, ACTIVITYPUB_RETRACTION_SHARE, ACTIVITYPUB_POST_IMAGES, ACTIVITYPUB_POST_WITH_SOURCE_MARKDOWN, ACTIVITYPUB_POST_WITH_TAGS, - ACTIVITYPUB_POST_WITH_SOURCE_BBCODE, ACTIVITYPUB_POST_WITH_MENTIONS) + ACTIVITYPUB_POST_WITH_SOURCE_BBCODE, ACTIVITYPUB_POST_WITH_MENTIONS, ACTIVITYPUB_PROFILE_WITH_DIASPORA_GUID) from federation.types import UserType, ReceiverVariant @@ -206,6 +206,15 @@ class TestActivitypubEntityMappersReceive: assert profile.nsfw is False assert profile.tag_list == [] + def test_message_to_objects_profile__diaspora_guid_extracted(self): + entities = message_to_objects( + ACTIVITYPUB_PROFILE_WITH_DIASPORA_GUID, "https://friendica.feneas.org/profile/feneas", + ) + assert len(entities) == 1 + profile = entities[0] + assert profile.id == "https://friendica.feneas.org/profile/feneas" + assert profile.guid == "76158462365bd347844d248732383358" + def test_message_to_objects_receivers_are_saved(self): # noinspection PyTypeChecker entities = message_to_objects( diff --git a/federation/tests/fixtures/payloads/activitypub.py b/federation/tests/fixtures/payloads/activitypub.py index 409dbdf..6e07f0e 100644 --- a/federation/tests/fixtures/payloads/activitypub.py +++ b/federation/tests/fixtures/payloads/activitypub.py @@ -144,6 +144,58 @@ ACTIVITYPUB_PROFILE_INVALID = { }, } +ACTIVITYPUB_PROFILE_WITH_DIASPORA_GUID = { + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "vcard": "http://www.w3.org/2006/vcard/ns#", + "dfrn": "http://purl.org/macgirvin/dfrn/1.0/", + "diaspora": "https://diasporafoundation.org/ns/", + "litepub": "http://litepub.social/ns#", + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "sensitive": "as:sensitive", + "Hashtag": "as:Hashtag", + "directMessage": "litepub:directMessage" + } + ], + "id": "https://friendica.feneas.org/profile/feneas", + "diaspora:guid": "76158462365bd347844d248732383358", + "type": "Organization", + "following": "https://friendica.feneas.org/following/feneas", + "followers": "https://friendica.feneas.org/followers/feneas", + "inbox": "https://friendica.feneas.org/inbox/feneas", + "outbox": "https://friendica.feneas.org/outbox/feneas", + "preferredUsername": "feneas", + "name": "Federated Networks Association", + "vcard:hasAddress": { + "@type": "vcard:Home", + "vcard:country-name": "", + "vcard:region": "", + "vcard:locality": "" + }, + "summary": "Federated Networks Association Ry is a non-profit volunteer organization that aims to spread knowledge about federated web projects and help people and projects involved in this area.", + "url": "https://friendica.feneas.org/profile/feneas", + "manuallyApprovesFollowers": False, + "publicKey": { + "id": "https://friendica.feneas.org/profile/feneas#main-key", + "owner": "https://friendica.feneas.org/profile/feneas", + "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtrN/RR6tSG0oH+G1oZmR\nHLx90i7ZgLAviJkfudnrgQDdSk5PKobKKyKw/GW07CuGtispNbI1nls3DdwnnPPl\naQYAvMW0MdLyPtviETecNcwRMMo4zOJxUMVooxjaK1bLstSxSbXpXXXWnn1niFkV\n+tHvtXXPJ12vXGoZjz0hKqzfKCND27rXCwFuecgvmMO8qeJB8aiJH5XfTPhAR0u1\nnA1Y2+GIdHP0Tnmt27nG0+9v9UqzIwlXOWRS2c5bG5XXZ35EnQEnfU9C+fhBzYEF\nBe5FwMR/sbK+a75ii0KxpP8x97MUnTkeuY7NgqY7GXJeKa9EZWK9xL7o5WDL5joe\ndNbDtyDL1CvUAB2JN+FabqbPeeHR1E5SFppBU7u0jyWTMejUqagBXdbf/Y6l7SRd\nJd51mSpmTkO9voybny7iXF4F2THZX0ZN77rDWYB8+NHAjdomfpz/wMwaaSALN6ZM\nSLxuobD0X3Rsv/2gDVL63UEXixQk4ZKBXfdZY2BmEKTKBezoWhR5yAbvTUPoklqL\nk98ajd9SIhE/YZD6nXD1S8CPiaV7qhw+2zdPIZDgq/S2P57eXcHam5ZasSzQGoo/\npVNvWzlRUo0GLIWEuA9FHklVpbuFlthaf5SP5qjKCtbYqLlzBPcxWaI0Y8Ei9ZL2\nTaosv23vnPSkN5wZp15lCrECAwEAAQ==\n-----END PUBLIC KEY-----\n" + }, + "endpoints": { + "sharedInbox": "https://friendica.feneas.org/inbox" + }, + "icon": { + "type": "Image", + "url": "https://friendica.feneas.org/photo/11107142045be6113fe88d7733317204-4.jpg?ts=1541804364" + }, + "generator": { + "type": "Service", + "name": "Friendica 'Dalmatian Bellflower' 2019.12-1327", + "url": "https://friendica.feneas.org" + } +} + ACTIVITYPUB_RETRACTION = { '@context': [ 'https://www.w3.org/ns/activitystreams',