add selection UI
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Tue, 28 Mar 2023 11:44:52 +0000 (12:44 +0100)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Tue, 28 Mar 2023 11:44:52 +0000 (12:44 +0100)
config/description.exs
lib/pleroma/web/akkoma_api/controllers/frontend_switcher.ex [new file with mode: 0644]
lib/pleroma/web/akkoma_api/views/frontend_switcher.ex [new file with mode: 0644]
lib/pleroma/web/plugs/frontend_static.ex
lib/pleroma/web/router.ex
lib/pleroma/web/templates/akkoma_api/frontend_switcher/switch.html.eex [new file with mode: 0644]

index 2a2d70a7b4a327a26fc92ac970db8d7cd9cf2c56..75fd23566b1f3728cf8f42462ce77f1188c65fe0 100644 (file)
@@ -3148,6 +3148,12 @@ config :pleroma, :config_description, [
         description:
           "A map containing available frontends and parameters for their installation.",
         children: frontend_options
+      },
+      %{
+          key: :pickable,
+          type: {:list, :string},
+          description:
+            "A list containing all frontends users can pick as their preference, format is :name/:ref, e.g pleroma-fe/stable."
       }
     ]
   },
diff --git a/lib/pleroma/web/akkoma_api/controllers/frontend_switcher.ex b/lib/pleroma/web/akkoma_api/controllers/frontend_switcher.ex
new file mode 100644 (file)
index 0000000..2095db4
--- /dev/null
@@ -0,0 +1,20 @@
+defmodule Pleroma.Web.AkkomaAPI.FrontendSwitcherController do
+  use Pleroma.Web, :controller
+  alias Pleroma.Config
+
+  @doc "GET /akkoma/frontend"
+  def switch(conn, _params) do
+    pickable = Config.get([:frontends, :pickable], [])
+
+    conn
+    |> put_view(Pleroma.Web.AkkomaAPI.FrontendSwitcherView)
+    |> render("switch.html", choices: pickable)
+  end
+
+  @doc "POST /akkoma/frontend"
+  def do_switch(conn, params) do
+    conn
+    |> put_resp_cookie("preferred_frontend", params["frontend"])
+    |> html("<meta http-equiv=\"refresh\" content=\"0; url=/\">")
+  end
+end
diff --git a/lib/pleroma/web/akkoma_api/views/frontend_switcher.ex b/lib/pleroma/web/akkoma_api/views/frontend_switcher.ex
new file mode 100644 (file)
index 0000000..1564c9e
--- /dev/null
@@ -0,0 +1,3 @@
+defmodule Pleroma.Web.AkkomaAPI.FrontendSwitcherView do
+  use Pleroma.Web, :view
+end
index 62283353ed63311791d965b898773e319e5c3b5e..91dfc77c34f28080bee5b09383868c4ab0272d27 100644 (file)
@@ -50,6 +50,7 @@ defmodule Pleroma.Web.Plugs.FrontendStatic do
   end
 
   def call(conn, opts) do
+    IO.inspect("OPTS: #{inspect(opts)}")
     with false <- api_route?(conn.path_info),
          false <- invalid_path?(conn.path_info),
          true <- enabled?(opts[:if]),
@@ -71,16 +72,19 @@ defmodule Pleroma.Web.Plugs.FrontendStatic do
     Map.get(cookies, @frontend_cookie_name)
   end
 
-  def preferred_or_fallback(conn, fallback) do
+  # Only override primary frontend
+  def preferred_or_fallback(conn, :primary) do
     case preferred_frontend(conn) do
       nil ->
-        fallback
+        :primary
 
       frontend ->
         frontend
     end
   end
 
+  def preferred_or_fallback(conn, fallback), do: fallback
+
   defp enabled?(if_opt) when is_function(if_opt), do: if_opt.()
   defp enabled?(true), do: true
   defp enabled?(_), do: false
index 3db8ddab768b5248174e439e24c650afbb5209d1..d02ae3460f40895b77e5f91da8ab68c6eb584a16 100644 (file)
@@ -466,6 +466,13 @@ defmodule Pleroma.Web.Router do
     put("/statuses/:id/emoji_reactions/:emoji", EmojiReactionController, :create)
   end
 
+  scope "/akkoma/", Pleroma.Web.AkkomaAPI do
+    pipe_through(:browser)
+
+    get("/frontend", FrontendSwitcherController, :switch)
+    post("/frontend", FrontendSwitcherController, :do_switch)
+  end
+
   scope "/api/v1/akkoma", Pleroma.Web.AkkomaAPI do
     pipe_through(:api)
 
diff --git a/lib/pleroma/web/templates/akkoma_api/frontend_switcher/switch.html.eex b/lib/pleroma/web/templates/akkoma_api/frontend_switcher/switch.html.eex
new file mode 100644 (file)
index 0000000..0692ddf
--- /dev/null
@@ -0,0 +1,7 @@
+<h2>Switch Frontend</h2>
+
+<%= form_for @conn, Routes.frontend_switcher_path(@conn, :do_switch), fn f -> %>
+  <%= select(f, :frontend, @choices) %>
+
+  <%= submit do: "submit" %>
+<% end %>