StaticFE: Prioritize json in requests.
authorlain <lain@soykaf.club>
Fri, 26 Jun 2020 14:27:39 +0000 (16:27 +0200)
committerlain <lain@soykaf.club>
Fri, 26 Jun 2020 14:27:39 +0000 (16:27 +0200)
lib/pleroma/plugs/static_fe_plug.ex
test/web/static_fe/static_fe_controller_test.exs

index 156e6788e0980fbf658ccf9fcd72a975d65ab2c6..7c69b2dac87e7b647a2e562ed9b6854022563854 100644 (file)
@@ -9,7 +9,7 @@ defmodule Pleroma.Plugs.StaticFEPlug do
   def init(options), do: options
 
   def call(conn, _) do
-    if enabled?() and accepts_html?(conn) do
+    if enabled?() and requires_html?(conn) do
       conn
       |> StaticFEController.call(:show)
       |> halt()
@@ -20,10 +20,13 @@ defmodule Pleroma.Plugs.StaticFEPlug do
 
   defp enabled?, do: Pleroma.Config.get([:static_fe, :enabled], false)
 
-  defp accepts_html?(conn) do
+  defp requires_html?(conn) do
     case get_req_header(conn, "accept") do
-      [accept | _] -> String.contains?(accept, "text/html")
-      _ -> false
+      [accept | _] ->
+        !String.contains?(accept, "json") && String.contains?(accept, "text/html")
+
+      _ ->
+        false
     end
   end
 end
index a49ab002fcb665754d1741c0430ed9f08b461648..1598bf6752ed2f19e6214e7137462b2ec80893f0 100644 (file)
@@ -87,6 +87,20 @@ defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do
       assert html =~ "testing a thing!"
     end
 
+    test "redirects to json if requested", %{conn: conn, user: user} do
+      {:ok, activity} = CommonAPI.post(user, %{status: "testing a thing!"})
+
+      conn =
+        conn
+        |> put_req_header(
+          "accept",
+          "Accept: application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", text/html"
+        )
+        |> get("/notice/#{activity.id}")
+
+      assert redirected_to(conn, 302) =~ activity.data["object"]
+    end
+
     test "filters HTML tags", %{conn: conn} do
       user = insert(:user)
       {:ok, activity} = CommonAPI.post(user, %{status: "<script>alert('xss')</script>"})