ActivityPub: Partly handle incoming follows.
authorlain <lain@soykaf.club>
Sat, 17 Feb 2018 13:55:44 +0000 (14:55 +0100)
committerlain <lain@soykaf.club>
Sat, 17 Feb 2018 13:55:44 +0000 (14:55 +0100)
lib/pleroma/web/activity_pub/transmogrifier.ex
test/fixtures/mastodon-follow-activity.json [new file with mode: 0644]
test/web/activity_pub/transmogrifier_test.exs

index c4b6a79e061a15afb6c6f3361d70b82bf1ad3ec4..f4af3aed35b82570d30b39e813d47ded09892dd7 100644 (file)
@@ -38,6 +38,20 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     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 (file)
index 0000000..7a6de69
--- /dev/null
@@ -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
index 124a5703b4d10f6ebc56cbb3590db1adfd4d923c..57598d020f8e2d653155deafdb92f96e80f6fc66 100644 (file)
@@ -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 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest 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