From e7b73359e352ed585613feeb61a48df3dd6d2cb3 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 17 Feb 2018 14:55:44 +0100 Subject: [PATCH] ActivityPub: Partly handle incoming follows. --- .../web/activity_pub/transmogrifier.ex | 14 +++++++++ test/fixtures/mastodon-follow-activity.json | 29 +++++++++++++++++++ test/web/activity_pub/transmogrifier_test.exs | 15 ++++++++++ 3 files changed, 58 insertions(+) create mode 100644 test/fixtures/mastodon-follow-activity.json diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index c4b6a79e0..f4af3aed3 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -38,6 +38,20 @@ def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = obje end end + def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id}) do + with %User{} = followed <- User.get_cached_by_ap_id(followed), + %User{} = follower <- User.get_or_fetch_by_ap_id(follower), + {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do + # TODO: Send an "Accept" activity. + User.follow(follower, followed) + {:ok, activity} + else + _e -> :error + end + end + + def handle_incoming(_), do: :error + @doc """ internal -> Mastodon diff --git a/test/fixtures/mastodon-follow-activity.json b/test/fixtures/mastodon-follow-activity.json new file mode 100644 index 000000000..7a6de6996 --- /dev/null +++ b/test/fixtures/mastodon-follow-activity.json @@ -0,0 +1,29 @@ +{ + "type": "Follow", + "signature": { + "type": "RsaSignature2017", + "signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", + "creator": "http://mastodon.example.org/users/admin#main-key", + "created": "2018-02-17T13:29:31Z" + }, + "object": "http://localtesting.pleroma.lol/users/lain", + "nickname": "lain", + "id": "http://mastodon.example.org/users/admin#follows/2", + "actor": "http://mastodon.example.org/users/admin", + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "toot": "http://joinmastodon.org/ns#", + "sensitive": "as:sensitive", + "ostatus": "http://ostatus.org#", + "movedTo": "as:movedTo", + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "atomUri": "ostatus:atomUri", + "Hashtag": "as:Hashtag", + "Emoji": "toot:Emoji" + } + ] +} \ No newline at end of file diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 124a5703b..57598d020 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -2,6 +2,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do use Pleroma.DataCase alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Activity + alias Pleroma.User import Pleroma.Factory alias Pleroma.Web.CommonAPI @@ -30,6 +31,20 @@ test "it works for incoming notices" do assert object["actor"] == "http://mastodon.example.org/users/admin" assert object["attributedTo"] == "http://mastodon.example.org/users/admin" end + + test "it works for incoming follow requests" do + user = insert(:user) + data = File.read!("test/fixtures/mastodon-follow-activity.json") |> Poison.decode! + |> Map.put("object", user.ap_id) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["actor"] == "http://mastodon.example.org/users/admin" + assert data["type"] == "Follow" + assert data["id"] == "http://mastodon.example.org/users/admin#follows/2" + assert User.following?(User.get_by_ap_id(data["actor"]), user) + + end end describe "prepare outgoing" do