Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into features/poll-valid...
authorlain <lain@soykaf.club>
Wed, 5 Aug 2020 09:12:14 +0000 (11:12 +0200)
committerlain <lain@soykaf.club>
Wed, 5 Aug 2020 09:12:14 +0000 (11:12 +0200)
1  2 
lib/pleroma/web/activity_pub/object_validators/common_validations.ex
test/web/activity_pub/transmogrifier_test.exs

index e981dacaa8e166585f1b095dce992d3eae3e7e95,bd46f8034ddf4d2e26399c655413fad794e78730..57d4456aa202659b31490c5604c05b029f98876f
@@@ -9,7 -9,7 +9,7 @@@ defmodule Pleroma.Web.ActivityPub.Objec
    alias Pleroma.Object
    alias Pleroma.User
  
 -  def validate_recipients_presence(cng, fields \\ [:to, :cc]) do
 +  def validate_any_presence(cng, fields) do
      non_empty =
        fields
        |> Enum.map(fn field -> get_field(cng, field) end)
@@@ -24,7 -24,7 +24,7 @@@
        fields
        |> Enum.reduce(cng, fn field, cng ->
          cng
 -        |> add_error(field, "no recipients in any field")
 +        |> add_error(field, "none of #{inspect(fields)} present")
        end)
      end
    end
  
      cng
      |> validate_change(field_name, fn field_name, actor ->
-       if User.get_cached_by_ap_id(actor) do
-         []
-       else
-         [{field_name, "can't find user"}]
+       case User.get_cached_by_ap_id(actor) do
+         %User{deactivated: true} ->
+           [{field_name, "user is deactivated"}]
+         %User{} ->
+           []
+         _ ->
+           [{field_name, "can't find user"}]
        end
      end)
    end
  
 +  def validate_actor_is_active(cng, options \\ []) do
 +    field_name = Keyword.get(options, :field_name, :actor)
 +
 +    cng
 +    |> validate_change(field_name, fn field_name, actor ->
 +      if %User{deactivated: false} = User.get_cached_by_ap_id(actor) do
 +        []
 +      else
 +        [{field_name, "can't find user (or deactivated)"}]
 +      end
 +    end)
 +  end
 +
    def validate_object_presence(cng, options \\ []) do
      field_name = Keyword.get(options, :field_name, :object)
      allowed_types = Keyword.get(options, :allowed_types, false)
  
      if actor_cng.valid?, do: actor_cng, else: object_cng
    end
 +
 +  def validate_host_match(cng, fields \\ [:id, :actor]) do
 +    unique_hosts =
 +      fields
 +      |> Enum.map(fn field ->
 +        %URI{host: host} =
 +          cng
 +          |> get_field(field)
 +          |> URI.parse()
 +
 +        host
 +      end)
 +      |> Enum.uniq()
 +      |> Enum.count()
 +
 +    if unique_hosts == 1 do
 +      cng
 +    else
 +      fields
 +      |> Enum.reduce(cng, fn field, cng ->
 +        cng
 +        |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
 +      end)
 +    end
 +  end
 +
 +  def validate_fields_match(cng, fields) do
 +    unique_fields =
 +      fields
 +      |> Enum.map(fn field -> get_field(cng, field) end)
 +      |> Enum.uniq()
 +      |> Enum.count()
 +
 +    if unique_fields == 1 do
 +      cng
 +    else
 +      fields
 +      |> Enum.reduce(cng, fn field, cng ->
 +        cng
 +        |> add_error(field, "Fields #{inspect(fields)} aren't matching")
 +      end)
 +    end
 +  end
  end
index 92ab0f28f0247d2c9605bc6c75940f2da1577e45,828964a360616c09f681a1a26bb6cd114be729e6..6dd9a3fec13cf5f77faabe261e0c853dbdee885e
@@@ -163,6 -163,14 +163,14 @@@ defmodule Pleroma.Web.ActivityPub.Trans
               end) =~ "[warn] Couldn't fetch \"https://404.site/whatever\", error: nil"
      end
  
+     test "it does not work for deactivated users" do
+       data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
+       insert(:user, ap_id: data["actor"], deactivated: true)
+       assert {:error, _} = Transmogrifier.handle_incoming(data)
+     end
      test "it works for incoming notices" do
        data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
  
        assert Enum.at(object.data["tag"], 2) == "moo"
      end
  
 -    test "it works for incoming questions" do
 -      data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
 -
 -      {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
 -
 -      object = Object.normalize(activity)
 -
 -      assert Enum.all?(object.data["oneOf"], fn choice ->
 -               choice["name"] in [
 -                 "Dunno",
 -                 "Everyone knows that!",
 -                 "25 char limit is dumb",
 -                 "I can't even fit a funny"
 -               ]
 -             end)
 -    end
 -
      test "it works for incoming listens" do
        data = %{
          "@context" => "https://www.w3.org/ns/activitystreams",
        assert object.data["length"] == 180_000
      end
  
 -    test "it rewrites Note votes to Answers and increments vote counters on question activities" do
 -      user = insert(:user)
 -
 -      {:ok, activity} =
 -        CommonAPI.post(user, %{
 -          status: "suya...",
 -          poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
 -        })
 -
 -      object = Object.normalize(activity)
 -
 -      data =
 -        File.read!("test/fixtures/mastodon-vote.json")
 -        |> Poison.decode!()
 -        |> Kernel.put_in(["to"], user.ap_id)
 -        |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
 -        |> Kernel.put_in(["object", "to"], user.ap_id)
 -
 -      {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
 -      answer_object = Object.normalize(activity)
 -      assert answer_object.data["type"] == "Answer"
 -      object = Object.get_by_ap_id(object.data["id"])
 -
 -      assert Enum.any?(
 -               object.data["oneOf"],
 -               fn
 -                 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
 -                 _ -> false
 -               end
 -             )
 -    end
 -
      test "it works for incoming notices with contentMap" do
        data =
          File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
                     %{
                       "href" =>
                         "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
 -                     "mediaType" => "video/mp4"
 +                     "mediaType" => "video/mp4",
 +                     "type" => "Link"
                     }
                   ]
                 }
                     %{
                       "href" =>
                         "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4",
 -                     "mediaType" => "video/mp4"
 +                     "mediaType" => "video/mp4",
 +                     "type" => "Link"
                     }
                   ]
                 }
      end
    end
  
 -  test "Rewrites Answers to Notes" do
 -    user = insert(:user)
 -
 -    {:ok, poll_activity} =
 -      CommonAPI.post(user, %{
 -        status: "suya...",
 -        poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
 -      })
 -
 -    poll_object = Object.normalize(poll_activity)
 -    # TODO: Replace with CommonAPI vote creation when implemented
 -    data =
 -      File.read!("test/fixtures/mastodon-vote.json")
 -      |> Poison.decode!()
 -      |> Kernel.put_in(["to"], user.ap_id)
 -      |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
 -      |> Kernel.put_in(["object", "to"], user.ap_id)
 -
 -    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
 -    {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
 -
 -    assert data["object"]["type"] == "Note"
 -  end
 -
    describe "fix_explicit_addressing" do
      setup do
        user = insert(:user)
                 "attachment" => [
                   %{
                     "mediaType" => "video/mp4",
 +                   "type" => "Document",
                     "url" => [
 -                     %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
 +                     %{
 +                       "href" => "https://peertube.moe/stat-480.mp4",
 +                       "mediaType" => "video/mp4",
 +                       "type" => "Link"
 +                     }
                     ]
                   }
                 ]
                 "attachment" => [
                   %{
                     "mediaType" => "video/mp4",
 +                   "type" => "Document",
                     "url" => [
 -                     %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
 +                     %{
 +                       "href" => "https://pe.er/stat-480.mp4",
 +                       "mediaType" => "video/mp4",
 +                       "type" => "Link"
 +                     }
                     ]
                   },
                   %{
                     "mediaType" => "video/mp4",
 +                   "type" => "Document",
                     "url" => [
 -                     %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
 +                     %{
 +                       "href" => "https://pe.er/stat-480.mp4",
 +                       "mediaType" => "video/mp4",
 +                       "type" => "Link"
 +                     }
                     ]
                   }
                 ]