diff options
| -rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 23 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/transmogrifier.ex | 33 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/utils.ex | 36 | ||||
| -rw-r--r-- | test/fixtures/mastodon-block-activity.json | 29 | ||||
| -rw-r--r-- | test/fixtures/mastodon-unblock-activity.json | 33 | ||||
| -rw-r--r-- | test/fixtures/mastodon-unfollow-activity.json | 33 | ||||
| -rw-r--r-- | test/web/activity_pub/transmogrifier_test.exs | 70 | 
7 files changed, 256 insertions, 1 deletions
| diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 24b4f045a..a711e6b76 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -221,6 +221,29 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      end    end +  def block(blocker, blocked, local \\ true) do +    follow_activity = fetch_latest_follow(blocker, blocked) + +    if follow_activity do +      unfollow(blocker, blocked, local) +    end + +    with block_data <- make_block_data(blocker, blocked), +         {:ok, activity} <- insert(block_data, local), +         :ok <- maybe_federate(activity) do +      {:ok, activity} +    end +  end + +  def unblock(blocker, blocked, local \\ true) do +    with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked), +         unblock_data <- make_unblock_data(blocker, blocked, block_activity), +         {:ok, activity} <- insert(unblock_data, local), +         :ok <- maybe_federate(activity) do +      {:ok, activity} +    end +  end +    def fetch_activities_for_context(context, opts \\ %{}) do      public = ["https://www.w3.org/ns/activitystreams#Public"] diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index a31452a63..d92ca9b65 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -244,6 +244,38 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do    def handle_incoming(          %{            "type" => "Undo", +          "object" => %{"type" => "Follow", "object" => followed}, +          "actor" => follower, +          "id" => id +        } = data +      ) do +    with %User{local: true} = followed = User.get_cached_by_ap_id(followed), +         %User{} = follower = User.get_or_fetch_by_ap_id(follower), +         {:ok, activity} <- ActivityPub.unfollow(follower, followed, false) do +      User.unfollow(follower, followed) +      {:ok, activity} +    else +      e -> :error +    end +  end + +  def handle_incoming( +        %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = data +      ) do +    with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked), +         %User{} = blocker = User.get_or_fetch_by_ap_id(blocker), +         {:ok, activity} <- ActivityPub.block(blocker, blocked, false) do +      User.unfollow(blocker, blocked) +      User.block(blocker, blocked) +      {:ok, activity} +    else +      e -> :error +    end +  end + +  def handle_incoming( +        %{ +          "type" => "Undo",            "object" => %{"type" => "Like", "object" => object_id},            "actor" => actor,            "id" => id @@ -261,7 +293,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do    # TODO    # Accept -  # Undo for non-Announce    def handle_incoming(_), do: :error diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 937f032c3..050413d51 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -355,6 +355,42 @@ defmodule Pleroma.Web.ActivityPub.Utils do      }    end +  #### Block-related helpers +  def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do +    query = +      from( +        activity in Activity, +        where: +          fragment( +            "? @> ?", +            activity.data, +            ^%{type: "Block", actor: blocker_id, object: blocked_id} +          ), +        order_by: [desc: :id], +        limit: 1 +      ) + +    Repo.one(query) +  end + +  def make_block_data(blocker, blocked) do +    %{ +      "type" => "Block", +      "actor" => blocker.ap_id, +      "to" => [blocked.ap_id], +      "object" => blocked.ap_id +    } +  end + +  def make_unblock_data(blocker, blocked, block_activity) do +    %{ +      "type" => "Undo", +      "actor" => blocker.ap_id, +      "to" => [blocked.ap_id], +      "object" => block_activity.data +    } +  end +    #### Create-related helpers    def make_create_data(params, additional) do diff --git a/test/fixtures/mastodon-block-activity.json b/test/fixtures/mastodon-block-activity.json new file mode 100644 index 000000000..186719d2c --- /dev/null +++ b/test/fixtures/mastodon-block-activity.json @@ -0,0 +1,29 @@ +{ +  "type": "Block", +  "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" +    } +  ] +} diff --git a/test/fixtures/mastodon-unblock-activity.json b/test/fixtures/mastodon-unblock-activity.json new file mode 100644 index 000000000..53b2f42a8 --- /dev/null +++ b/test/fixtures/mastodon-unblock-activity.json @@ -0,0 +1,33 @@ +{ +    "@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" +            } +          ], +    "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" +      }, +    "type": "Undo", +    "object": { +      "type": "Block",  +      "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"  +    }, +    "actor": "http://mastodon.example.org/users/admin" +} diff --git a/test/fixtures/mastodon-unfollow-activity.json b/test/fixtures/mastodon-unfollow-activity.json new file mode 100644 index 000000000..022b47acd --- /dev/null +++ b/test/fixtures/mastodon-unfollow-activity.json @@ -0,0 +1,33 @@ +{ +    "@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" +            } +          ], +    "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" +      }, +    "type": "Undo", +    "object": { +      "type": "Follow",  +      "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"  +    }, +    "actor": "http://mastodon.example.org/users/admin" +} diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index a0af75a69..87fadece2 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -315,6 +315,76 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert data["object"]["id"] ==                 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"      end + +    test "it works for incomming unfollows" do +      user = insert(:user) + +      follow_data = +        File.read!("test/fixtures/mastodon-follow-activity.json") +        |> Poison.decode!() +        |> Map.put("object", user.ap_id) + +      {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data) + +      data = +        File.read!("test/fixtures/mastodon-unfollow-activity.json") +        |> Poison.decode!() +        |> Map.put("object", follow_data) + +      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + +      assert data["type"] == "Undo" +      assert data["object"]["type"] == "Follow" +      assert data["object"]["object"] == user.ap_id +      assert data["actor"] == "http://mastodon.example.org/users/admin" + +      refute User.following?(User.get_by_ap_id(data["actor"]), user) +    end + +    test "it works for incoming blocks" do +      user = insert(:user) + +      data = +        File.read!("test/fixtures/mastodon-block-activity.json") +        |> Poison.decode!() +        |> Map.put("object", user.ap_id) + +      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + +      assert data["type"] == "Block" +      assert data["object"] == user.ap_id +      assert data["actor"] == "http://mastodon.example.org/users/admin" + +      blocker = User.get_by_ap_id(data["actor"]) + +      assert User.blocks?(blocker, user.ap_id) +    end + +    test "it works for incoming unblocks" do +      user = insert(:user) + +      block_data = +        File.read!("test/fixtures/mastodon-block-activity.json") +        |> Poison.decode!() +        |> Map.put("object", user.ap_id) + +      {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data) + +      data = +        File.read!("test/fixtures/mastodon-unblock-activity.json") +        |> Poison.decode!() +        |> Map.put("object", block_data) + +      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) +      assert data["type"] == "Undo" +      assert data["object"]["type"] == "Block" +      assert data["object"]["object"] == user.ap_id +      assert data["actor"] == "https://mastodon.example.org/users/admin" + +      blocker = User.get_by_ap_id(data["actor"]) + +      refute User.blocks?(blocker, user.ap_id) +    end    end    describe "prepare outgoing" do | 
