From 19ad94405695af366fc08bafc14ce306a44806d4 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 12 Mar 2020 14:31:04 +0300 Subject: frontends dir --- config/config.exs | 1 + config/description.exs | 8 ++++ config/test.exs | 3 +- docs/administration/CLI_tasks/frontend.md | 2 +- lib/mix/tasks/pleroma/frontend.ex | 40 ++++++++++++-------- lib/pleroma/plugs/instance_static.ex | 38 ++++++++++++++----- test/instance_static/dist.zip | Bin 0 -> 790 bytes test/tasks/frontend_test.exs | 60 ++++++++++++++++++++++++++++++ 8 files changed, 124 insertions(+), 28 deletions(-) create mode 100644 test/instance_static/dist.zip create mode 100644 test/tasks/frontend_test.exs diff --git a/config/config.exs b/config/config.exs index 2cd741213..a047be90f 100644 --- a/config/config.exs +++ b/config/config.exs @@ -233,6 +233,7 @@ quarantined_instances: [], managed_config: true, static_dir: "instance/static/", + frontends_dir: "instance/frontends/", allowed_post_formats: [ "text/plain", "text/html", diff --git a/config/description.exs b/config/description.exs index 9fdcfcd96..1a272ec84 100644 --- a/config/description.exs +++ b/config/description.exs @@ -732,6 +732,14 @@ "instance/static/" ] }, + %{ + key: :frontends_dir, + type: :string, + description: "Instance frontends directory", + suggestions: [ + "instance/frontends/" + ] + }, %{ key: :allowed_post_formats, type: {:list, :string}, diff --git a/config/test.exs b/config/test.exs index a17886265..33f4322f8 100644 --- a/config/test.exs +++ b/config/test.exs @@ -33,7 +33,8 @@ 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 diff --git a/docs/administration/CLI_tasks/frontend.md b/docs/administration/CLI_tasks/frontend.md index d2c183d5b..4094ac3b5 100644 --- a/docs/administration/CLI_tasks/frontend.md +++ b/docs/administration/CLI_tasks/frontend.md @@ -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 [] diff --git a/lib/mix/tasks/pleroma/frontend.ex b/lib/mix/tasks/pleroma/frontend.ex index 7a691b95d..0d52ae69e 100644 --- a/lib/mix/tasks/pleroma/frontend.ex +++ b/lib/mix/tasks/pleroma/frontend.ex @@ -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 @@ def run(["download" | rest]) 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 @@ def run(["download" | rest]) 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 diff --git a/lib/pleroma/plugs/instance_static.ex b/lib/pleroma/plugs/instance_static.ex index 927fa2663..f7c9df5f9 100644 --- a/lib/pleroma/plugs/instance_static.ex +++ b/lib/pleroma/plugs/instance_static.ex @@ -10,19 +10,25 @@ defmodule Pleroma.Plugs.InstanceStatic do """ @behaviour Plug + alias Pleroma.Config + def file_path(path) do - instance_path = - Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path) + 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) - if File.exists?(instance_path) do - instance_path - else - Path.join(Application.app_dir(:pleroma, "priv/static/"), 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 @@ def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do call_static( conn, opts, - unquote(at), - Pleroma.Config.get([:instance, :static_dir], "instance/static") + unquote(at) ) end end @@ -48,7 +53,20 @@ def call(conn, _) 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) diff --git a/test/instance_static/dist.zip b/test/instance_static/dist.zip new file mode 100644 index 000000000..bec271c43 Binary files /dev/null and b/test/instance_static/dist.zip differ diff --git a/test/tasks/frontend_test.exs b/test/tasks/frontend_test.exs new file mode 100644 index 000000000..d72b03358 --- /dev/null +++ b/test/tasks/frontend_test.exs @@ -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 -- cgit v1.2.3