From f446e75577d456e365eedb4e047a586d86e97b7a Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 14 Aug 2018 17:05:11 +0000 Subject: [PATCH 1/4] activitypub: transmogrifier: add necessary translations for kroeg --- .../web/activity_pub/transmogrifier.ex | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 2ebc526df..e5fb6e033 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -21,6 +21,10 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do Enum.at(actor, 0) end + def get_actor(%{"actor" => actor}) when is_map(actor) do + actor["id"] + end + def get_actor(%{"actor" => actor_list}) do Enum.find(actor_list, fn %{"type" => type} -> type == "Person" end) |> Map.get("id") @@ -38,6 +42,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do |> fix_emoji |> fix_tag |> fix_content_map + |> fix_addressing + end + + def fix_addressing_list(map, field) do + if is_binary(map[field]) do + map + |> Map.put(field, [map[field]]) + else + map + end + end + + def fix_addressing(map) do + map + |> fix_addressing_list("to") + |> fix_addressing_list("cc") + |> fix_addressing_list("bto") + |> fix_addressing_list("bcc") end def fix_actor(%{"attributedTo" => actor} = object) do @@ -143,7 +165,10 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do def handle_incoming(%{"type" => "Create", "object" => %{"type" => objtype} = object} = data) when objtype in ["Article", "Note"] do actor = get_actor(data) - data = Map.put(data, "actor", actor) + + data = + Map.put(data, "actor", actor) + |> fix_addressing with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]), %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do From 7fbcd4caaf877749c13d6755eaaa956ac0ea5012 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 14 Aug 2018 17:07:01 +0000 Subject: [PATCH 2/4] test: add kroeg fixtures --- test/fixtures/kroeg-post-activity.json | 50 +++++++++++++++++++ test/support/httpoison_mock.ex | 12 +++++ test/web/activity_pub/transmogrifier_test.exs | 9 ++++ 3 files changed, 71 insertions(+) create mode 100644 test/fixtures/kroeg-post-activity.json diff --git a/test/fixtures/kroeg-post-activity.json b/test/fixtures/kroeg-post-activity.json new file mode 100644 index 000000000..32dabd947 --- /dev/null +++ b/test/fixtures/kroeg-post-activity.json @@ -0,0 +1,50 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://puckipedia.com/-/context" + ], + "actor": { + "endpoints": "https://puckipedia.com/#endpoints", + "followers": "https://puckipedia.com/followers", + "following": "https://puckipedia.com/following", + "icon": { + "mediaType": "image/png", + "type": "Image", + "url": "https://puckipedia.com/images/avatar.png" + }, + "id": "https://puckipedia.com/", + "inbox": "https://puckipedia.com/inbox", + "kroeg:blocks": { + "id": "https://puckipedia.com/blocks" + }, + "liked": "https://puckipedia.com/liked", + "manuallyApprovesFollowers": false, + "name": "HACKER TEEN PUCKIPEDIA \ud83d\udc69\u200d\ud83d\udcbb", + "outbox": "https://puckipedia.com/outbox", + "preferredUsername": "puckipedia", + "publicKey": { + "id": "https://puckipedia.com/#key", + "owner": "https://puckipedia.com/", + "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvN05xIcFE0Qgany7Rht4\n0ZI5wu++IT7K5iSqRimBYkpoeHbVcT9RFlW+aWH/QJJW/YgZ7+LMr8AMCrKrwSpS\nCndyrpx4O4lZ3FNRLu7tbklh01rGZfE6R1SFfYBpvMvImc9nYT6iezYDbv6NkHku\no3aVhjql216XlA0OhIrqQme9sAdrLbjbMrTUS8douCTkDOX+JFj1ghHCqdYEMZJI\nOY9kovtgnqyxFLm0RsPGsO1+g/OVojqG+VqHz6O2lceaTVQLlnZ4gOhLVG1tVsA2\nRfXQK+R/VgXncYE+BlQVd/tcdGAz7CDL7PP3rP65gmARnafhGR96cCOi/KzlAXSO\nMwIDAQAB\n-----END PUBLIC KEY-----", + "type": [] + }, + "summary": "

federated hacker teen
\n[she/they]

", + "type": "Person", + "updated": "2017-12-19T16:56:29.7576707+00:00" + }, + "cc": "https://puckipedia.com/followers", + "id": "https://puckipedia.com/ae4ee4e8be/activity", + "object": { + "attributedTo": "https://puckipedia.com/", + "cc": "https://puckipedia.com/followers", + "content": "

henlo from my Psion netBook

message sent from my Psion netBook

", + "id": "https://puckipedia.com/ae4ee4e8be", + "likes": "https://puckipedia.com/ae4ee4e8be/likes", + "replies": "https://puckipedia.com/ae4ee4e8be/replies", + "shares": "https://puckipedia.com/ae4ee4e8be/shares", + "to": "https://www.w3.org/ns/activitystreams#Public", + "type": "Note" + }, + "to": "https://www.w3.org/ns/activitystreams#Public", + "type": "Create" +} diff --git a/test/support/httpoison_mock.ex b/test/support/httpoison_mock.ex index a52d44ed6..dc83b115e 100644 --- a/test/support/httpoison_mock.ex +++ b/test/support/httpoison_mock.ex @@ -3,6 +3,18 @@ defmodule HTTPoisonMock do def get(url, body \\ [], headers \\ []) + def get( + "https://puckipedia.com/", + [Accept: "application/activity+json"], + [] + ) do + {:ok, + %Response{ + status_code: 200, + body: File.read!("test/fixtures/httpoison_mock/puckipedia.com.json") + }} + end + def get( "https://gerzilla.de/.well-known/webfinger?resource=acct:kaniini@gerzilla.de", [Accept: "application/xrd+xml,application/jrd+json"], diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 838ae169d..e455da39f 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -112,6 +112,15 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do "

@lain

" end + test "it works for incoming notices with to/cc not being an array (kroeg)" do + data = File.read!("test/fixtures/kroeg-post-activity.json") |> Poison.decode!() + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["object"]["content"] == + "

henlo from my Psion netBook

message sent from my Psion netBook

" + end + test "it works for incoming follow requests" do user = insert(:user) From b8560e5ed5612e37af8a759128e322025922a862 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 14 Aug 2018 17:15:33 +0000 Subject: [PATCH 3/4] testsuite: formatting --- test/support/httpoison_mock.ex | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/support/httpoison_mock.ex b/test/support/httpoison_mock.ex index dc83b115e..527c2e1f7 100644 --- a/test/support/httpoison_mock.ex +++ b/test/support/httpoison_mock.ex @@ -3,11 +3,7 @@ defmodule HTTPoisonMock do def get(url, body \\ [], headers \\ []) - def get( - "https://puckipedia.com/", - [Accept: "application/activity+json"], - [] - ) do + def get("https://puckipedia.com/", [Accept: "application/activity+json"], _) do {:ok, %Response{ status_code: 200, From 805844367475af3929b059d46f88cc31132fac1b Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 16 Aug 2018 15:10:10 +0000 Subject: [PATCH 4/4] testsuite: add puckipedia test fixture --- test/fixtures/httpoison_mock/puckipedia.com.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 test/fixtures/httpoison_mock/puckipedia.com.json diff --git a/test/fixtures/httpoison_mock/puckipedia.com.json b/test/fixtures/httpoison_mock/puckipedia.com.json new file mode 100644 index 000000000..d18dfbae7 --- /dev/null +++ b/test/fixtures/httpoison_mock/puckipedia.com.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://puckipedia.com/-/context"],"endpoints":"https://puckipedia.com/#endpoints","followers":"https://puckipedia.com/followers","following":"https://puckipedia.com/following","icon":{"mediaType":"image/png","type":"Image","url":"https://puckipedia.com/images/avatar.png"},"id":"https://puckipedia.com/","inbox":"https://puckipedia.com/inbox","kroeg:blocks":{"id":"https://puckipedia.com/blocks"},"liked":"https://puckipedia.com/liked","manuallyApprovesFollowers":false,"name":"HACKER TEEN PUCKIPEDIA 👩‍💻","outbox":"https://puckipedia.com/outbox","preferredUsername":"puckipedia","publicKey":{"id":"https://puckipedia.com/#key","owner":"https://puckipedia.com/","publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvN05xIcFE0Qgany7Rht4\n0ZI5wu++IT7K5iSqRimBYkpoeHbVcT9RFlW+aWH/QJJW/YgZ7+LMr8AMCrKrwSpS\nCndyrpx4O4lZ3FNRLu7tbklh01rGZfE6R1SFfYBpvMvImc9nYT6iezYDbv6NkHku\no3aVhjql216XlA0OhIrqQme9sAdrLbjbMrTUS8douCTkDOX+JFj1ghHCqdYEMZJI\nOY9kovtgnqyxFLm0RsPGsO1+g/OVojqG+VqHz6O2lceaTVQLlnZ4gOhLVG1tVsA2\nRfXQK+R/VgXncYE+BlQVd/tcdGAz7CDL7PP3rP65gmARnafhGR96cCOi/KzlAXSO\nMwIDAQAB\n-----END PUBLIC KEY-----","type":[]},"summary":"

federated hacker teen
\n[she/they]

","type":"Person","updated":"2017-12-19T16:56:29.7576707+00:00"} \ No newline at end of file