diff options
| author | lain <lain@soykaf.club> | 2019-10-13 15:37:49 +0000 | 
|---|---|---|
| committer | lain <lain@soykaf.club> | 2019-10-13 15:37:49 +0000 | 
| commit | e3b4a3e96b2ffbc6d920155cd41687414045d4d6 (patch) | |
| tree | 08f7c040f1f596e641a38279103c7aeabf4fa267 | |
| parent | 930cbd6d05653033e9f6d16960d698f5db4ae624 (diff) | |
| parent | 422aa6befee0cbcbf71d3cacd96d90c1be3263ba (diff) | |
| download | pleroma-e3b4a3e96b2ffbc6d920155cd41687414045d4d6.tar.gz pleroma-e3b4a3e96b2ffbc6d920155cd41687414045d4d6.zip  | |
Merge branch 'incoming-id-fixes' into 'develop'
Assorted transmogrifier fixes
See merge request pleroma/pleroma!1831
| -rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 45 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/transmogrifier.ex | 15 | ||||
| -rw-r--r-- | lib/pleroma/web/ostatus/handlers/delete_handler.ex | 2 | ||||
| -rw-r--r-- | test/web/activity_pub/transmogrifier_test.exs | 11 | ||||
| -rw-r--r-- | test/web/mastodon_api/controllers/search_controller_test.exs | 8 | 
5 files changed, 48 insertions, 33 deletions
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 9f29087df..364452b5d 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -269,22 +269,21 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      end    end -  def accept(%{to: to, actor: actor, object: object} = params) do -    # only accept false as false value -    local = !(params[:local] == false) +  def accept(params) do +    accept_or_reject("Accept", params) +  end -    with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object}, -         {:ok, activity} <- insert(data, local), -         :ok <- maybe_federate(activity) do -      {:ok, activity} -    end +  def reject(params) do +    accept_or_reject("Reject", params)    end -  def reject(%{to: to, actor: actor, object: object} = params) do -    # only accept false as false value -    local = !(params[:local] == false) +  def accept_or_reject(type, %{to: to, actor: actor, object: object} = params) do +    local = Map.get(params, :local, true) +    activity_id = Map.get(params, :activity_id, nil) -    with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object}, +    with data <- +           %{"to" => to, "type" => type, "actor" => actor.ap_id, "object" => object} +           |> Utils.maybe_put("id", activity_id),           {:ok, activity} <- insert(data, local),           :ok <- maybe_federate(activity) do        {:ok, activity} @@ -409,18 +408,24 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      end    end -  def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do +  def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, options \\ []) do +    local = Keyword.get(options, :local, true) +    activity_id = Keyword.get(options, :activity_id, nil) +    actor = Keyword.get(options, :actor, actor) +      user = User.get_cached_by_ap_id(actor)      to = (object.data["to"] || []) ++ (object.data["cc"] || [])      with {:ok, object, activity} <- Object.delete(object), -         data <- %{ -           "type" => "Delete", -           "actor" => actor, -           "object" => id, -           "to" => to, -           "deleted_activity_id" => activity && activity.id -         }, +         data <- +           %{ +             "type" => "Delete", +             "actor" => actor, +             "object" => id, +             "to" => to, +             "deleted_activity_id" => activity && activity.id +           } +           |> maybe_put("id", activity_id),           {:ok, activity} <- insert(data, local, false),           stream_out_participations(object, user),           _ <- decrease_replies_count_if_reply(object), diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 872ed0eb2..b56343beb 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -514,7 +514,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do    end    def handle_incoming( -        %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => _id} = data, +        %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,          _options        ) do      with actor <- Containment.get_actor(data), @@ -528,7 +528,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do          type: "Accept",          actor: followed,          object: follow_activity.data["id"], -        local: false +        local: false, +        activity_id: id        })      else        _e -> :error @@ -536,7 +537,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do    end    def handle_incoming( -        %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => _id} = data, +        %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,          _options        ) do      with actor <- Containment.get_actor(data), @@ -550,7 +551,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do               type: "Reject",               actor: followed,               object: follow_activity.data["id"], -             local: false +             local: false, +             activity_id: id             }) do        User.unfollow(follower, followed) @@ -637,7 +639,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do    # an error or a tombstone.  This would allow us to verify that a deletion actually took    # place.    def handle_incoming( -        %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => _id} = data, +        %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data,          _options        ) do      object_id = Utils.get_ap_id(object_id) @@ -646,7 +648,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do           {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),           {:ok, object} <- get_obj_helper(object_id),           :ok <- Containment.contain_origin(actor.ap_id, object.data), -         {:ok, activity} <- ActivityPub.delete(object, false) do +         {:ok, activity} <- +           ActivityPub.delete(object, local: false, activity_id: id, actor: actor.ap_id) do        {:ok, activity}      else        nil -> diff --git a/lib/pleroma/web/ostatus/handlers/delete_handler.ex b/lib/pleroma/web/ostatus/handlers/delete_handler.ex index b2f9f3946..ac2dc115c 100644 --- a/lib/pleroma/web/ostatus/handlers/delete_handler.ex +++ b/lib/pleroma/web/ostatus/handlers/delete_handler.ex @@ -11,7 +11,7 @@ defmodule Pleroma.Web.OStatus.DeleteHandler do    def handle_delete(entry, _doc \\ nil) do      with id <- XML.string_from_xpath("//id", entry),           %Object{} = object <- Object.normalize(id), -         {:ok, delete} <- ActivityPub.delete(object, false) do +         {:ok, delete} <- ActivityPub.delete(object, local: false) do        delete      end    end diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 50c0bfb84..6c35a6f4d 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -682,6 +682,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do      test "it works for incoming deletes" do        activity = insert(:note_activity) +      deleting_user = insert(:user)        data =          File.read!("test/fixtures/mastodon-delete.json") @@ -694,11 +695,14 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        data =          data          |> Map.put("object", object) -        |> Map.put("actor", activity.data["actor"]) +        |> Map.put("actor", deleting_user.ap_id) -      {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data) +      {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} = +        Transmogrifier.handle_incoming(data) +      assert id == data["id"]        refute Activity.get_by_id(activity.id) +      assert actor == deleting_user.ap_id      end      test "it fails for incoming deletes with spoofed origin" do @@ -905,6 +909,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert activity.data["object"] == follow_activity.data["id"] +      assert activity.data["id"] == accept_data["id"] +        follower = User.get_cached_by_id(follower.id)        assert User.following?(follower, followed) == true @@ -1009,6 +1015,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        {:ok, activity} = Transmogrifier.handle_incoming(reject_data)        refute activity.local +      assert activity.data["id"] == reject_data["id"]        follower = User.get_cached_by_id(follower.id) diff --git a/test/web/mastodon_api/controllers/search_controller_test.exs b/test/web/mastodon_api/controllers/search_controller_test.exs index 0ca896e01..ee413eef7 100644 --- a/test/web/mastodon_api/controllers/search_controller_test.exs +++ b/test/web/mastodon_api/controllers/search_controller_test.exs @@ -40,9 +40,9 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do      test "search", %{conn: conn} do        user = insert(:user)        user_two = insert(:user, %{nickname: "shp@shitposter.club"}) -      user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu 天子"}) +      user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"}) -      {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu private"}) +      {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu private 天子"})        {:ok, _activity} =          CommonAPI.post(user, %{ @@ -70,8 +70,8 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do          get(conn, "/api/v2/search", %{"q" => "天子"})          |> json_response(200) -      [account] == results["accounts"] -      assert account["id"] == to_string(user_three.id) +      [status] = results["statuses"] +      assert status["id"] == to_string(activity.id)      end    end  | 
