ensure we send the right files for preferred fe

Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
FloatingGhost 2023-03-12 23:59:10 +00:00 committed by nicole mikołajczyk
commit 3a5e8e5e07
3 changed files with 49 additions and 12 deletions

View file

@ -30,7 +30,7 @@ defmodule Pleroma.Web.Fallback.RedirectController do
end end
def redirector(conn, _params, code \\ 200) do def redirector(conn, _params, code \\ 200) do
{:ok, index_content} = File.read(index_file_path()) {:ok, index_content} = File.read(index_file_path(conn))
response = response =
index_content index_content
@ -51,7 +51,7 @@ defmodule Pleroma.Web.Fallback.RedirectController do
end end
def redirector_with_meta(conn, params) do def redirector_with_meta(conn, params) do
{:ok, index_content} = File.read(index_file_path()) {:ok, index_content} = File.read(index_file_path(conn))
tags = build_tags(conn, params) tags = build_tags(conn, params)
preloads = preload_data(conn, params) preloads = preload_data(conn, params)
@ -69,7 +69,7 @@ defmodule Pleroma.Web.Fallback.RedirectController do
end end
def redirector_with_preload(conn, params) do def redirector_with_preload(conn, params) do
{:ok, index_content} = File.read(index_file_path()) {:ok, index_content} = File.read(index_file_path(conn))
preloads = preload_data(conn, params) preloads = preload_data(conn, params)
response = response =
@ -91,8 +91,10 @@ defmodule Pleroma.Web.Fallback.RedirectController do
|> text("") |> text("")
end end
defp index_file_path do defp index_file_path(conn) do
Pleroma.Web.Plugs.InstanceStatic.file_path("index.html") frontend_type = Pleroma.Web.Plugs.FrontendStatic.preferred_or_fallback(conn, :primary)
Pleroma.Web.Plugs.InstanceStatic.file_path("index.html", frontend_type)
end end
defp build_tags(conn, params) do defp build_tags(conn, params) do

View file

@ -5,17 +5,23 @@
defmodule Pleroma.Web.Plugs.FrontendStatic do defmodule Pleroma.Web.Plugs.FrontendStatic do
require Pleroma.Constants require Pleroma.Constants
@frontend_cookie_name "preferred_frontend"
@moduledoc """ @moduledoc """
This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends. This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
""" """
@behaviour Plug @behaviour Plug
def file_path(path, frontend_type \\ :primary) do defp instance_static_path do
if configuration = Pleroma.Config.get([:frontends, frontend_type]) do Pleroma.Config.get([:instance, :static_dir], "instance/static")
instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static") end
def file_path(path, frontend_type \\ :primary)
def file_path(path, frontend_type) when is_atom(frontend_type) do
if configuration = Pleroma.Config.get([:frontends, frontend_type]) do
Path.join([ Path.join([
instance_static_path, instance_static_path(),
"frontends", "frontends",
configuration["name"], configuration["name"],
configuration["ref"], configuration["ref"],
@ -26,6 +32,15 @@ defmodule Pleroma.Web.Plugs.FrontendStatic do
end end
end end
def file_path(path, frontend_type) when is_binary(frontend_type) do
Path.join([
instance_static_path(),
"frontends",
frontend_type,
path
])
end
def init(opts) do def init(opts) do
opts opts
|> Keyword.put(:from, "__unconfigured_frontend_static_plug") |> Keyword.put(:from, "__unconfigured_frontend_static_plug")
@ -36,7 +51,8 @@ defmodule Pleroma.Web.Plugs.FrontendStatic do
def call(conn, opts) do def call(conn, opts) do
with false <- api_route?(conn.path_info), with false <- api_route?(conn.path_info),
false <- invalid_path?(conn.path_info), false <- invalid_path?(conn.path_info),
frontend_type <- Map.get(opts, :frontend_type, :primary), fallback_frontend_type <- Map.get(opts, :frontend_type, :primary),
frontend_type <- preferred_or_fallback(conn, fallback_frontend_type),
path when not is_nil(path) <- file_path("", frontend_type) do path when not is_nil(path) <- file_path("", frontend_type) do
call_static(conn, opts, path) call_static(conn, opts, path)
else else
@ -45,6 +61,24 @@ defmodule Pleroma.Web.Plugs.FrontendStatic do
end end
end end
def preferred_frontend(conn) do
%{req_cookies: cookies} =
conn
|> Plug.Conn.fetch_cookies()
Map.get(cookies, @frontend_cookie_name)
end
def preferred_or_fallback(conn, fallback) do
case preferred_frontend(conn) do
nil ->
fallback
frontend ->
frontend
end
end
defp invalid_path?(list) do defp invalid_path?(list) do
invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"])) invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
end end
@ -62,6 +96,7 @@ defmodule Pleroma.Web.Plugs.FrontendStatic do
defp call_static(conn, opts, from) do defp call_static(conn, opts, from) do
opts = Map.put(opts, :from, from) opts = Map.put(opts, :from, from)
IO.inspect(opts, label: "opts")
Plug.Static.call(conn, opts) Plug.Static.call(conn, opts)
end end
end end

View file

@ -13,11 +13,11 @@ defmodule Pleroma.Web.Plugs.InstanceStatic do
""" """
@behaviour Plug @behaviour Plug
def file_path(path) do def file_path(path, frontend_type \\ :primary) do
instance_path = instance_path =
Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path) Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path)
frontend_path = Pleroma.Web.Plugs.FrontendStatic.file_path(path, :primary) frontend_path = Pleroma.Web.Plugs.FrontendStatic.file_path(path, frontend_type)
(File.exists?(instance_path) && instance_path) || (File.exists?(instance_path) && instance_path) ||
(frontend_path && File.exists?(frontend_path) && frontend_path) || (frontend_path && File.exists?(frontend_path) && frontend_path) ||