frontends dir

This commit is contained in:
Alexander Strizhakov 2020-03-12 14:31:04 +03:00
parent bf95bf3492
commit 19ad944056
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
8 changed files with 125 additions and 29 deletions

View file

@ -233,6 +233,7 @@ config :pleroma, :instance,
quarantined_instances: [],
managed_config: true,
static_dir: "instance/static/",
frontends_dir: "instance/frontends/",
allowed_post_formats: [
"text/plain",
"text/html",

View file

@ -732,6 +732,14 @@ config :pleroma, :config_description, [
"instance/static/"
]
},
%{
key: :frontends_dir,
type: :string,
description: "Instance frontends directory",
suggestions: [
"instance/frontends/"
]
},
%{
key: :allowed_post_formats,
type: {:list, :string},

View file

@ -33,7 +33,8 @@ config :pleroma, :instance,
skip_thread_containment: false,
federating: false,
external_user_synchronization: false,
static_dir: "test/instance_static/"
static_dir: "test/instance_static/",
frontends_dir: "test/instance_static/frontends"
config :pleroma, :activitypub, sign_object_fetches: false

View file

@ -4,7 +4,7 @@
## Download the latest frontend
This downloads a snapshot of the latest Pleroma-FE for a given reference and writes it to the `static_dir`. In a default setup, this means that this snapshot will be served as the frontend by the backend.
This downloads a snapshot of the latest Pleroma-FE for a given reference and writes it to the `frontends_dir`. In a default setup, this means that this snapshot will be served as the frontend by the backend.
```sh tab="OTP"
./bin/pleroma_ctl pleroma.frontend download [<options>]

View file

@ -4,7 +4,7 @@
defmodule Mix.Tasks.Pleroma.Frontend do
use Mix.Task
alias __MODULE__.Fetcher
import Mix.Pleroma
@shortdoc "Manages the Pleroma frontends"
@moduledoc File.read!("docs/administration/CLI_tasks/frontend.md")
@ -15,21 +15,36 @@ defmodule Mix.Tasks.Pleroma.Frontend do
rest,
strict: [
reference: :string
]
],
aliases: [r: :reference]
)
reference = options[:reference] || "master"
IO.puts("Downloading reference #{reference}")
shell_info("Downloading reference #{reference}")
url =
"https://git.pleroma.social/pleroma/pleroma-fe/-/jobs/artifacts/#{reference}/download?job=build"
sd = Pleroma.Config.get([:instance, :static_dir]) |> Path.expand()
sd =
Pleroma.Config.get([:instance, :frontends_dir], "instance/frontends")
|> Path.join("pleroma-fe")
|> Path.expand()
with {_, {:ok, %{status: 200, body: body}}} <- {:fetch, Fetcher.get(url)},
{_, {:ok, results}} <- {:unzip, :zip.unzip(body, [:memory])} do
IO.puts("Writing to #{sd}")
adapter =
if Pleroma.Config.get(:env) == :test do
Tesla.Mock
else
Tesla.Adapter.Httpc
end
client = Tesla.client([Tesla.Middleware.FollowRedirects], adapter)
with {_, {:ok, %{status: 200, body: body}}} <- {:fetch, Tesla.get(client, url)},
{_, {:ok, results}} <- {:unzip, :zip.unzip(body, [:memory])},
shell_info("Cleaning #{sd}"),
{_, {:ok, _}} <- {:clean_up, File.rm_rf(sd)} do
shell_info("Writing to #{sd}")
results
|> Enum.each(fn {path, contents} ->
@ -39,16 +54,9 @@ defmodule Mix.Tasks.Pleroma.Frontend do
File.write!(path, contents)
end)
IO.puts("Successfully downloaded and unpacked the frontend")
shell_info("Successfully downloaded and unpacked the frontend")
else
{error, _} -> IO.puts("Step failed: #{error}")
{error, _} -> shell_error("Step failed: #{error}")
end
end
end
defmodule Mix.Tasks.Pleroma.Frontend.Fetcher do
use Tesla
plug(Tesla.Middleware.FollowRedirects)
adapter(Tesla.Adapter.Httpc)
end

View file

@ -10,19 +10,25 @@ defmodule Pleroma.Plugs.InstanceStatic do
"""
@behaviour Plug
def file_path(path) do
instance_path =
Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path)
alias Pleroma.Config
if File.exists?(instance_path) do
instance_path
else
Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
def file_path(path) do
instance_path = Path.join(Config.get([:instance, :static_dir], "instance/static/"), path)
frontends_path =
Config.get([:instance, :frontends_dir], "instance/frontends/")
|> Path.join("pleroma-fe")
|> Path.join(path)
cond do
File.exists?(instance_path) -> instance_path
File.exists?(frontends_path) -> frontends_path
true -> Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
end
end
@only ~w(index.html robots.txt static emoji packs sounds images instance favicon.png sw.js
sw-pleroma.js)
sw-pleroma.js fontello)
def init(opts) do
opts
@ -38,8 +44,7 @@ defmodule Pleroma.Plugs.InstanceStatic do
call_static(
conn,
opts,
unquote(at),
Pleroma.Config.get([:instance, :static_dir], "instance/static")
unquote(at)
)
end
end
@ -48,7 +53,20 @@ defmodule Pleroma.Plugs.InstanceStatic do
conn
end
defp call_static(conn, opts, at, from) do
defp call_static(conn, opts, at) do
static_dir = Config.get([:instance, :static_dir], "instance/static/")
frontend_dir =
Config.get([:instance, :frontends_dir], "instance/frontends/")
|> Path.join("pleroma-fe")
from =
if File.exists?(Path.join(static_dir, conn.request_path)) do
static_dir
else
frontend_dir
end
opts =
opts
|> Map.put(:from, from)

Binary file not shown.

View file

@ -0,0 +1,60 @@
defmodule Mix.Tasks.Pleroma.FrontendTest do
use ExUnit.Case
import Tesla.Mock
@path Pleroma.Config.get([:instance, :frontends_dir])
setup do
Mix.shell(Mix.Shell.Process)
mock(fn
%{
method: :get,
url:
"https://git.pleroma.social/pleroma/pleroma-fe/-/jobs/artifacts/master/download?job=build"
} ->
%Tesla.Env{status: 200, body: File.read!("test/instance_static/dist.zip")}
%{
method: :get,
url:
"https://git.pleroma.social/pleroma/pleroma-fe/-/jobs/artifacts/develop/download?job=build"
} ->
%Tesla.Env{status: 200, body: File.read!("test/instance_static/dist.zip")}
end)
on_exit(fn ->
Mix.shell(Mix.Shell.IO)
{:ok, _} = File.rm_rf(@path)
end)
:ok
end
test "downloads pleroma-fe and master by default" do
Mix.Tasks.Pleroma.Frontend.run(["download"])
@path |> Path.expand() |> Path.join("pleroma-fe") |> check_assertions("master")
end
test "download special fe with reference" do
ref = "develop"
Mix.Tasks.Pleroma.Frontend.run(["download", "-r", ref])
@path |> Path.expand() |> Path.join("pleroma-fe") |> check_assertions(ref)
end
defp check_assertions(path, ref) do
assert_receive {:mix_shell, :info, [message]}
assert message == "Downloading reference #{ref}"
assert_receive {:mix_shell, :info, [message]}
assert message == "Cleaning #{path}"
assert_receive {:mix_shell, :info, [message]}
assert message == "Writing to #{path}"
assert_receive {:mix_shell, :info, ["Successfully downloaded and unpacked the frontend"]}
assert File.exists?(path <> "/1.png")
assert File.exists?(path <> "/2.css")
assert File.exists?(path <> "/3.js")
end
end