From 03e306785b2013fe6fd47b59d4e578c6ed586b08 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 27 Oct 2020 19:20:04 +0400 Subject: [PATCH 1/9] Add an API endpoint to install a new frontend --- config/config.exs | 3 +- lib/mix/tasks/pleroma/frontend.ex | 107 +---------------- lib/pleroma/frontend.ex | 108 ++++++++++++++++++ .../controllers/frontend_controller.ex | 41 +++++++ .../web/admin_api/views/frontend_view.ex | 21 ++++ .../operations/admin/frontend_operation.ex | 77 +++++++++++++ lib/pleroma/web/router.ex | 3 + .../workers/frontend_installer_worker.ex | 21 ++++ test/pleroma/frontend_test.exs | 80 +++++++++++++ .../controllers/frontend_controller_test.exs | 94 +++++++++++++++ 10 files changed, 448 insertions(+), 107 deletions(-) create mode 100644 lib/pleroma/frontend.ex create mode 100644 lib/pleroma/web/admin_api/controllers/frontend_controller.ex create mode 100644 lib/pleroma/web/admin_api/views/frontend_view.ex create mode 100644 lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex create mode 100644 lib/pleroma/workers/frontend_installer_worker.ex create mode 100644 test/pleroma/frontend_test.exs create mode 100644 test/pleroma/web/admin_api/controllers/frontend_controller_test.exs diff --git a/config/config.exs b/config/config.exs index 124f30a77..2e22a046b 100644 --- a/config/config.exs +++ b/config/config.exs @@ -560,7 +560,8 @@ background: 5, remote_fetcher: 2, attachments_cleanup: 5, - new_users_digest: 1 + new_users_digest: 1, + frontend_installer: 1 ], plugins: [Oban.Plugins.Pruner], crontab: [ diff --git a/lib/mix/tasks/pleroma/frontend.ex b/lib/mix/tasks/pleroma/frontend.ex index cbce81ab9..f15dbc38b 100644 --- a/lib/mix/tasks/pleroma/frontend.ex +++ b/lib/mix/tasks/pleroma/frontend.ex @@ -17,8 +17,6 @@ def run(["install", "none" | _args]) do end def run(["install", frontend | args]) do - log_level = Logger.level() - Logger.configure(level: :warn) start_pleroma() {options, [], []} = @@ -33,109 +31,6 @@ def run(["install", frontend | args]) do ] ) - instance_static_dir = - with nil <- options[:static_dir] do - Pleroma.Config.get!([:instance, :static_dir]) - end - - cmd_frontend_info = %{ - "name" => frontend, - "ref" => options[:ref], - "build_url" => options[:build_url], - "build_dir" => options[:build_dir] - } - - config_frontend_info = Pleroma.Config.get([:frontends, :available, frontend], %{}) - - frontend_info = - Map.merge(config_frontend_info, cmd_frontend_info, fn _key, config, cmd -> - # This only overrides things that are actually set - cmd || config - end) - - ref = frontend_info["ref"] - - unless ref do - raise "No ref given or configured" - end - - dest = - Path.join([ - instance_static_dir, - "frontends", - frontend, - ref - ]) - - fe_label = "#{frontend} (#{ref})" - - tmp_dir = Path.join([instance_static_dir, "frontends", "tmp"]) - - with {_, :ok} <- - {:download_or_unzip, download_or_unzip(frontend_info, tmp_dir, options[:file])}, - shell_info("Installing #{fe_label} to #{dest}"), - :ok <- install_frontend(frontend_info, tmp_dir, dest) do - File.rm_rf!(tmp_dir) - shell_info("Frontend #{fe_label} installed to #{dest}") - - Logger.configure(level: log_level) - else - {:download_or_unzip, _} -> - shell_info("Could not download or unzip the frontend") - - _e -> - shell_info("Could not install the frontend") - end - end - - defp download_or_unzip(frontend_info, temp_dir, file) do - if file do - with {:ok, zip} <- File.read(Path.expand(file)) do - unzip(zip, temp_dir) - end - else - download_build(frontend_info, temp_dir) - end - end - - def unzip(zip, dest) do - with {:ok, unzipped} <- :zip.unzip(zip, [:memory]) do - File.rm_rf!(dest) - File.mkdir_p!(dest) - - Enum.each(unzipped, fn {filename, data} -> - path = filename - - new_file_path = Path.join(dest, path) - - new_file_path - |> Path.dirname() - |> File.mkdir_p!() - - File.write!(new_file_path, data) - end) - - :ok - end - end - - defp download_build(frontend_info, dest) do - shell_info("Downloading pre-built bundle for #{frontend_info["name"]}") - url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"]) - - with {:ok, %{status: 200, body: zip_body}} <- - Pleroma.HTTP.get(url, [], pool: :media, recv_timeout: 120_000) do - unzip(zip_body, dest) - else - e -> {:error, e} - end - end - - defp install_frontend(frontend_info, source, dest) do - from = frontend_info["build_dir"] || "dist" - File.rm_rf!(dest) - File.mkdir_p!(dest) - File.cp_r!(Path.join([source, from]), dest) - :ok + Pleroma.Frontend.install(frontend, options) end end diff --git a/lib/pleroma/frontend.ex b/lib/pleroma/frontend.ex new file mode 100644 index 000000000..3413d2fba --- /dev/null +++ b/lib/pleroma/frontend.ex @@ -0,0 +1,108 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Frontend do + alias Pleroma.Config + + require Logger + + def install(name, opts \\ []) do + cmd_frontend_info = %{ + "ref" => opts[:ref], + "build_url" => opts[:build_url], + "build_dir" => opts[:build_dir] + } + + config_frontend_info = Config.get([:frontends, :available, name], %{}) + + frontend_info = + Map.merge(config_frontend_info, cmd_frontend_info, fn _key, config, cmd -> + # This only overrides things that are actually set + cmd || config + end) + + ref = frontend_info["ref"] + + unless ref do + raise "No ref given or configured" + end + + dest = Path.join([dir(), name, ref]) + + label = "#{name} (#{ref})" + tmp_dir = Path.join(dir(), "tmp") + + with {_, :ok} <- + {:download_or_unzip, download_or_unzip(frontend_info, tmp_dir, opts[:file])}, + Logger.info("Installing #{label} to #{dest}"), + :ok <- install_frontend(frontend_info, tmp_dir, dest) do + File.rm_rf!(tmp_dir) + Logger.info("Frontend #{label} installed to #{dest}") + else + {:download_or_unzip, _} -> + Logger.info("Could not download or unzip the frontend") + + _e -> + Logger.info("Could not install the frontend") + end + end + + def dir(opts \\ []) do + if is_nil(opts[:static_dir]) do + Pleroma.Config.get!([:instance, :static_dir]) + else + opts[:static_dir] + end + |> Path.join("frontends") + end + + defp download_or_unzip(frontend_info, temp_dir, nil), + do: download_build(frontend_info, temp_dir) + + defp download_or_unzip(_frontend_info, temp_dir, file) do + with {:ok, zip} <- File.read(Path.expand(file)) do + unzip(zip, temp_dir) + end + end + + def unzip(zip, dest) do + with {:ok, unzipped} <- :zip.unzip(zip, [:memory]) do + File.rm_rf!(dest) + File.mkdir_p!(dest) + + Enum.each(unzipped, fn {filename, data} -> + path = filename + + new_file_path = Path.join(dest, path) + + new_file_path + |> Path.dirname() + |> File.mkdir_p!() + + File.write!(new_file_path, data) + end) + end + end + + defp download_build(frontend_info, dest) do + Logger.info("Downloading pre-built bundle for #{frontend_info["name"]}") + url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"]) + + with {:ok, %{status: 200, body: zip_body}} <- + Pleroma.HTTP.get(url, [], pool: :media, recv_timeout: 120_000) do + unzip(zip_body, dest) + else + {:error, e} -> {:error, e} + e -> {:error, e} + end + end + + defp install_frontend(frontend_info, source, dest) do + from = frontend_info["build_dir"] || "dist" + File.rm_rf!(dest) + File.mkdir_p!(dest) + File.cp_r!(Path.join([source, from]), dest) + :ok + end +end diff --git a/lib/pleroma/web/admin_api/controllers/frontend_controller.ex b/lib/pleroma/web/admin_api/controllers/frontend_controller.ex new file mode 100644 index 000000000..59c69aba1 --- /dev/null +++ b/lib/pleroma/web/admin_api/controllers/frontend_controller.ex @@ -0,0 +1,41 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.AdminAPI.FrontendController do + use Pleroma.Web, :controller + + alias Pleroma.Config + alias Pleroma.Web.Plugs.OAuthScopesPlug + alias Pleroma.Workers.FrontendInstallerWorker + + plug(Pleroma.Web.ApiSpec.CastAndValidate) + plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action == :install) + plug(OAuthScopesPlug, %{scopes: ["read"], admin: true} when action == :index) + action_fallback(Pleroma.Web.AdminAPI.FallbackController) + + defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.FrontendOperation + + def index(conn, _params) do + installed = installed() + + frontends = + [:frontends, :available] + |> Config.get([]) + |> Enum.map(fn {name, desc} -> + Map.put(desc, "installed", name in installed) + end) + + render(conn, "index.json", frontends: frontends) + end + + def install(%{body_params: params} = conn, _params) do + FrontendInstallerWorker.install(params.name, Map.delete(params, :name)) + + index(conn, %{}) + end + + defp installed do + File.ls!(Pleroma.Frontend.dir()) + end +end diff --git a/lib/pleroma/web/admin_api/views/frontend_view.ex b/lib/pleroma/web/admin_api/views/frontend_view.ex new file mode 100644 index 000000000..374841d0b --- /dev/null +++ b/lib/pleroma/web/admin_api/views/frontend_view.ex @@ -0,0 +1,21 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.AdminAPI.FrontendView do + use Pleroma.Web, :view + + def render("index.json", %{frontends: frontends}) do + render_many(frontends, __MODULE__, "show.json") + end + + def render("show.json", %{frontend: frontend}) do + %{ + name: frontend["name"], + git: frontend["git"], + build_url: frontend["build_url"], + ref: frontend["ref"], + installed: frontend["installed"] + } + end +end diff --git a/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex b/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex new file mode 100644 index 000000000..24d23a4e0 --- /dev/null +++ b/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex @@ -0,0 +1,77 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ApiSpec.Admin.FrontendOperation do + alias OpenApiSpex.Operation + alias OpenApiSpex.Schema + alias Pleroma.Web.ApiSpec.Schemas.ApiError + + import Pleroma.Web.ApiSpec.Helpers + + def open_api_operation(action) do + operation = String.to_existing_atom("#{action}_operation") + apply(__MODULE__, operation, []) + end + + def index_operation do + %Operation{ + tags: ["Admin", "Reports"], + summary: "Get a list of available frontends", + operationId: "AdminAPI.FrontendController.index", + security: [%{"oAuth" => ["read"]}], + responses: %{ + 200 => Operation.response("Response", "application/json", list_of_frontends()), + 403 => Operation.response("Forbidden", "application/json", ApiError) + } + } + end + + def install_operation do + %Operation{ + tags: ["Admin", "Reports"], + summary: "Install a frontend", + operationId: "AdminAPI.FrontendController.install", + security: [%{"oAuth" => ["read"]}], + requestBody: request_body("Parameters", install_request(), required: true), + responses: %{ + 200 => Operation.response("Response", "application/json", list_of_frontends()), + 403 => Operation.response("Forbidden", "application/json", ApiError) + } + } + end + + defp list_of_frontends do + %Schema{ + type: :array, + items: %Schema{ + type: :object, + properties: %{ + name: %Schema{type: :string}, + git: %Schema{type: :string, format: :uri, nullable: true}, + build_url: %Schema{type: :string, format: :uri}, + ref: %Schema{type: :string}, + installed: %Schema{type: :boolean} + } + } + } + end + + defp install_request do + %Schema{ + title: "FrontendInstallRequest", + type: :object, + required: [:name], + properties: %{ + name: %Schema{ + type: :string, + nullable: false + }, + ref: %Schema{ + type: :string, + nullable: false + } + } + } + end +end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index d2d939989..aba505a66 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -223,6 +223,9 @@ defmodule Pleroma.Web.Router do get("/chats/:id", ChatController, :show) get("/chats/:id/messages", ChatController, :messages) delete("/chats/:id/messages/:message_id", ChatController, :delete_message) + + get("/frontends", FrontendController, :index) + post("/frontends", FrontendController, :install) end scope "/api/pleroma/emoji", Pleroma.Web.PleromaAPI do diff --git a/lib/pleroma/workers/frontend_installer_worker.ex b/lib/pleroma/workers/frontend_installer_worker.ex new file mode 100644 index 000000000..38688c63b --- /dev/null +++ b/lib/pleroma/workers/frontend_installer_worker.ex @@ -0,0 +1,21 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Workers.FrontendInstallerWorker do + use Oban.Worker, queue: :frontend_installer, max_attempts: 1 + + alias Oban.Job + alias Pleroma.Frontend + + def install(name, opts \\ []) do + %{"name" => name, "opts" => Map.new(opts)} + |> new() + |> Oban.insert() + end + + def perform(%Job{args: %{"name" => name, "opts" => opts}}) do + opts = Keyword.new(opts, fn {key, value} -> {String.to_existing_atom(key), value} end) + Frontend.install(name, opts) + end +end diff --git a/test/pleroma/frontend_test.exs b/test/pleroma/frontend_test.exs new file mode 100644 index 000000000..77913b223 --- /dev/null +++ b/test/pleroma/frontend_test.exs @@ -0,0 +1,80 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.FrontendTest do + use Pleroma.DataCase + alias Pleroma.Frontend + + import ExUnit.CaptureIO, only: [capture_io: 1] + + @dir "test/frontend_static_test" + + setup do + File.mkdir_p!(@dir) + clear_config([:instance, :static_dir], @dir) + + on_exit(fn -> + File.rm_rf(@dir) + end) + end + + test "it downloads and unzips a known frontend" do + clear_config([:frontends, :available], %{ + "pleroma" => %{ + "ref" => "fantasy", + "name" => "pleroma", + "build_url" => "http://gensokyo.2hu/builds/${ref}" + } + }) + + Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} -> + %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")} + end) + + capture_io(fn -> + Frontend.install("pleroma") + end) + + assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) + end + + test "it also works given a file" do + clear_config([:frontends, :available], %{ + "pleroma" => %{ + "ref" => "fantasy", + "name" => "pleroma", + "build_dir" => "" + } + }) + + folder = Path.join([@dir, "frontends", "pleroma", "fantasy"]) + previously_existing = Path.join([folder, "temp"]) + File.mkdir_p!(folder) + File.write!(previously_existing, "yey") + assert File.exists?(previously_existing) + + capture_io(fn -> + Frontend.install("pleroma", file: "test/fixtures/tesla_mock/frontend.zip") + end) + + assert File.exists?(Path.join([folder, "test.txt"])) + refute File.exists?(previously_existing) + end + + test "it downloads and unzips unknown frontends" do + Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} -> + %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")} + end) + + capture_io(fn -> + Frontend.install("unknown", + ref: "baka", + build_url: "http://gensokyo.2hu/madeup.zip", + build_dir: "" + ) + end) + + assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"])) + end +end diff --git a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs new file mode 100644 index 000000000..461d6e5c9 --- /dev/null +++ b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs @@ -0,0 +1,94 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do + use Pleroma.Web.ConnCase + use Oban.Testing, repo: Pleroma.Repo + + import Pleroma.Factory + + alias Pleroma.Config + alias Pleroma.Tests.ObanHelpers + alias Pleroma.Workers.FrontendInstallerWorker + + @dir "test/frontend_static_test" + + setup do + clear_config([:instance, :static_dir], @dir) + File.mkdir_p!(Pleroma.Frontend.dir()) + + on_exit(fn -> + File.rm_rf(@dir) + end) + + admin = insert(:user, is_admin: true) + token = insert(:oauth_admin_token, user: admin) + + conn = + build_conn() + |> assign(:user, admin) + |> assign(:token, token) + + {:ok, %{admin: admin, token: token, conn: conn}} + end + + describe "GET /api/pleroma/admin/frontends" do + test "it lists available frontends", %{conn: conn} do + response = + conn + |> get("/api/pleroma/admin/frontends") + |> json_response_and_validate_schema(:ok) + + assert Enum.map(response, & &1["name"]) == + Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end) + + refute Enum.any?(response, fn frontend -> frontend["installed"] == true end) + end + end + + describe "POST /api/pleroma/admin/frontends" do + test "it installs a frontend", %{conn: conn} do + clear_config([:frontends, :available], %{ + "pleroma" => %{ + "ref" => "fantasy", + "name" => "pleroma", + "build_url" => "http://gensokyo.2hu/builds/${ref}" + } + }) + + Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} -> + %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")} + end) + + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/frontends", %{name: "pleroma"}) + |> json_response_and_validate_schema(:ok) + + assert_enqueued( + worker: FrontendInstallerWorker, + args: %{"name" => "pleroma", "opts" => %{}} + ) + + ObanHelpers.perform(all_enqueued(worker: FrontendInstallerWorker)) + + assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) + + response = + conn + |> get("/api/pleroma/admin/frontends") + |> json_response_and_validate_schema(:ok) + + assert response == [ + %{ + "build_url" => "http://gensokyo.2hu/builds/${ref}", + "git" => nil, + "installed" => true, + "name" => "pleroma", + "ref" => "fantasy" + } + ] + end + end +end From d83c2bd330d1ed01b84634b70dfe024020ebfd6c Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Thu, 29 Oct 2020 16:37:50 +0400 Subject: [PATCH 2/9] Add support for install via `file` and `build_url` params --- lib/pleroma/frontend.ex | 8 +-- .../operations/admin/frontend_operation.ex | 17 ++++-- test/pleroma/frontend_test.exs | 22 +++----- .../controllers/frontend_controller_test.exs | 52 ++++++++++++++++++- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/lib/pleroma/frontend.ex b/lib/pleroma/frontend.ex index 3413d2fba..b3d4c3325 100644 --- a/lib/pleroma/frontend.ex +++ b/lib/pleroma/frontend.ex @@ -8,16 +8,16 @@ defmodule Pleroma.Frontend do require Logger def install(name, opts \\ []) do - cmd_frontend_info = %{ + frontend_info = %{ "ref" => opts[:ref], "build_url" => opts[:build_url], "build_dir" => opts[:build_dir] } - config_frontend_info = Config.get([:frontends, :available, name], %{}) - frontend_info = - Map.merge(config_frontend_info, cmd_frontend_info, fn _key, config, cmd -> + [:frontends, :available, name] + |> Config.get(%{}) + |> Map.merge(frontend_info, fn _key, config, cmd -> # This only overrides things that are actually set cmd || config end) diff --git a/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex b/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex index 24d23a4e0..9d7d017a2 100644 --- a/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex @@ -49,7 +49,7 @@ defp list_of_frontends do properties: %{ name: %Schema{type: :string}, git: %Schema{type: :string, format: :uri, nullable: true}, - build_url: %Schema{type: :string, format: :uri}, + build_url: %Schema{type: :string, format: :uri, nullable: true}, ref: %Schema{type: :string}, installed: %Schema{type: :boolean} } @@ -64,12 +64,19 @@ defp install_request do required: [:name], properties: %{ name: %Schema{ - type: :string, - nullable: false + type: :string }, ref: %Schema{ - type: :string, - nullable: false + type: :string + }, + file: %Schema{ + type: :string + }, + build_url: %Schema{ + type: :string + }, + build_dir: %Schema{ + type: :string } } } diff --git a/test/pleroma/frontend_test.exs b/test/pleroma/frontend_test.exs index 77913b223..223625857 100644 --- a/test/pleroma/frontend_test.exs +++ b/test/pleroma/frontend_test.exs @@ -6,8 +6,6 @@ defmodule Pleroma.FrontendTest do use Pleroma.DataCase alias Pleroma.Frontend - import ExUnit.CaptureIO, only: [capture_io: 1] - @dir "test/frontend_static_test" setup do @@ -32,9 +30,7 @@ test "it downloads and unzips a known frontend" do %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")} end) - capture_io(fn -> - Frontend.install("pleroma") - end) + Frontend.install("pleroma") assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) end @@ -54,9 +50,7 @@ test "it also works given a file" do File.write!(previously_existing, "yey") assert File.exists?(previously_existing) - capture_io(fn -> - Frontend.install("pleroma", file: "test/fixtures/tesla_mock/frontend.zip") - end) + Frontend.install("pleroma", file: "test/fixtures/tesla_mock/frontend.zip") assert File.exists?(Path.join([folder, "test.txt"])) refute File.exists?(previously_existing) @@ -67,13 +61,11 @@ test "it downloads and unzips unknown frontends" do %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")} end) - capture_io(fn -> - Frontend.install("unknown", - ref: "baka", - build_url: "http://gensokyo.2hu/madeup.zip", - build_dir: "" - ) - end) + Frontend.install("unknown", + ref: "baka", + build_url: "http://gensokyo.2hu/madeup.zip", + build_dir: "" + ) assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"])) end diff --git a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs index 461d6e5c9..afe82ddf5 100644 --- a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs @@ -48,7 +48,7 @@ test "it lists available frontends", %{conn: conn} do end describe "POST /api/pleroma/admin/frontends" do - test "it installs a frontend", %{conn: conn} do + test "from available frontends", %{conn: conn} do clear_config([:frontends, :available], %{ "pleroma" => %{ "ref" => "fantasy", @@ -90,5 +90,55 @@ test "it installs a frontend", %{conn: conn} do } ] end + + test "from a file", %{conn: conn} do + clear_config([:frontends, :available], %{ + "pleroma" => %{ + "ref" => "fantasy", + "name" => "pleroma", + "build_dir" => "" + } + }) + + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/frontends", %{ + name: "pleroma", + file: "test/fixtures/tesla_mock/frontend.zip" + }) + |> json_response_and_validate_schema(:ok) + + assert_enqueued( + worker: FrontendInstallerWorker, + args: %{ + "name" => "pleroma", + "opts" => %{"file" => "test/fixtures/tesla_mock/frontend.zip"} + } + ) + + ObanHelpers.perform(all_enqueued(worker: FrontendInstallerWorker)) + + assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) + end + + test "from an URL", %{conn: conn} do + Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} -> + %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")} + end) + + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/frontends", %{ + name: "unknown", + ref: "baka", + build_url: "http://gensokyo.2hu/madeup.zip", + build_dir: "" + }) + |> json_response_and_validate_schema(:ok) + + ObanHelpers.perform(all_enqueued(worker: FrontendInstallerWorker)) + + assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"])) + end end end From 75d131ba1860dc75486819ca93310292244ef92e Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Thu, 29 Oct 2020 15:55:30 +0400 Subject: [PATCH 3/9] Add documentation and update CHANGELOG --- CHANGELOG.md | 1 + docs/API/admin_api.md | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index afeaa930b..5b52acfb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Mix task option for force-unfollowing relays - Media preview proxy (requires `ffmpeg` and `ImageMagick` to be installed and media proxy to be enabled; see `:media_preview_proxy` config for more details). - Pleroma API: Importing the mutes users from CSV files. +- Pleroma API: An endpoint to manage frontends - Experimental websocket-based federation between Pleroma instances. ### Changed diff --git a/docs/API/admin_api.md b/docs/API/admin_api.md index 7bf13daef..ca8c98728 100644 --- a/docs/API/admin_api.md +++ b/docs/API/admin_api.md @@ -1497,3 +1497,61 @@ Returns the content of the document "url": "https://example.com/instance/panel.html" } ``` + +## `GET /api/pleroma/admin/frontends + +### List available frontends + +- Response: + +```json +[ + { + "build_url": "https://git.pleroma.social/pleroma/fedi-fe/-/jobs/artifacts/${ref}/download?job=build", + "git": "https://git.pleroma.social/pleroma/fedi-fe", + "installed": true, + "name": "fedi-fe", + "ref": "master" + }, + { + "build_url": "https://git.pleroma.social/lambadalambda/kenoma/-/jobs/artifacts/${ref}/download?job=build", + "git": "https://git.pleroma.social/lambadalambda/kenoma", + "installed": false, + "name": "kenoma", + "ref": "master" + } +] +``` + + +## `POST /api/pleroma/admin/frontends + +### Install a frontend + +- Params: + - `name`: frontend name, required + - `ref`: frontend ref + - `file`: path to a frontend zip file + - `build_url`: build URL + - `build_dir`: build directory + +- Response: + +```json +[ + { + "build_url": "https://git.pleroma.social/pleroma/fedi-fe/-/jobs/artifacts/${ref}/download?job=build", + "git": "https://git.pleroma.social/pleroma/fedi-fe", + "installed": true, + "name": "fedi-fe", + "ref": "master" + }, + { + "build_url": "https://git.pleroma.social/lambadalambda/kenoma/-/jobs/artifacts/${ref}/download?job=build", + "git": "https://git.pleroma.social/lambadalambda/kenoma", + "installed": false, + "name": "kenoma", + "ref": "master" + } +] +``` From 435bf1f9450954eab5f753a983dee202aa11bac1 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 11 Nov 2020 20:12:35 +0400 Subject: [PATCH 4/9] Remove FrontendInstallerWorker --- config/config.exs | 2 -- .../controllers/frontend_controller.ex | 3 +-- .../workers/frontend_installer_worker.ex | 21 ------------------ .../controllers/frontend_controller_test.exs | 22 ------------------- 4 files changed, 1 insertion(+), 47 deletions(-) delete mode 100644 lib/pleroma/workers/frontend_installer_worker.ex diff --git a/config/config.exs b/config/config.exs index 1d09a0238..0b8a75aad 100644 --- a/config/config.exs +++ b/config/config.exs @@ -563,9 +563,7 @@ remote_fetcher: 2, attachments_cleanup: 5, new_users_digest: 1, - frontend_installer: 1, mute_expire: 5 - ], plugins: [Oban.Plugins.Pruner], crontab: [ diff --git a/lib/pleroma/web/admin_api/controllers/frontend_controller.ex b/lib/pleroma/web/admin_api/controllers/frontend_controller.ex index 59c69aba1..4518bed5a 100644 --- a/lib/pleroma/web/admin_api/controllers/frontend_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/frontend_controller.ex @@ -7,7 +7,6 @@ defmodule Pleroma.Web.AdminAPI.FrontendController do alias Pleroma.Config alias Pleroma.Web.Plugs.OAuthScopesPlug - alias Pleroma.Workers.FrontendInstallerWorker plug(Pleroma.Web.ApiSpec.CastAndValidate) plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action == :install) @@ -30,7 +29,7 @@ def index(conn, _params) do end def install(%{body_params: params} = conn, _params) do - FrontendInstallerWorker.install(params.name, Map.delete(params, :name)) + Pleroma.Frontend.install(params.name, Map.delete(params, :name)) index(conn, %{}) end diff --git a/lib/pleroma/workers/frontend_installer_worker.ex b/lib/pleroma/workers/frontend_installer_worker.ex deleted file mode 100644 index 38688c63b..000000000 --- a/lib/pleroma/workers/frontend_installer_worker.ex +++ /dev/null @@ -1,21 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Workers.FrontendInstallerWorker do - use Oban.Worker, queue: :frontend_installer, max_attempts: 1 - - alias Oban.Job - alias Pleroma.Frontend - - def install(name, opts \\ []) do - %{"name" => name, "opts" => Map.new(opts)} - |> new() - |> Oban.insert() - end - - def perform(%Job{args: %{"name" => name, "opts" => opts}}) do - opts = Keyword.new(opts, fn {key, value} -> {String.to_existing_atom(key), value} end) - Frontend.install(name, opts) - end -end diff --git a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs index afe82ddf5..1d4fbfa03 100644 --- a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs @@ -4,13 +4,10 @@ defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do use Pleroma.Web.ConnCase - use Oban.Testing, repo: Pleroma.Repo import Pleroma.Factory alias Pleroma.Config - alias Pleroma.Tests.ObanHelpers - alias Pleroma.Workers.FrontendInstallerWorker @dir "test/frontend_static_test" @@ -66,13 +63,6 @@ test "from available frontends", %{conn: conn} do |> post("/api/pleroma/admin/frontends", %{name: "pleroma"}) |> json_response_and_validate_schema(:ok) - assert_enqueued( - worker: FrontendInstallerWorker, - args: %{"name" => "pleroma", "opts" => %{}} - ) - - ObanHelpers.perform(all_enqueued(worker: FrontendInstallerWorker)) - assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) response = @@ -108,16 +98,6 @@ test "from a file", %{conn: conn} do }) |> json_response_and_validate_schema(:ok) - assert_enqueued( - worker: FrontendInstallerWorker, - args: %{ - "name" => "pleroma", - "opts" => %{"file" => "test/fixtures/tesla_mock/frontend.zip"} - } - ) - - ObanHelpers.perform(all_enqueued(worker: FrontendInstallerWorker)) - assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) end @@ -136,8 +116,6 @@ test "from an URL", %{conn: conn} do }) |> json_response_and_validate_schema(:ok) - ObanHelpers.perform(all_enqueued(worker: FrontendInstallerWorker)) - assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"])) end end From d26a4493960cc9d99183dfcd18464040213ac91e Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 11 Nov 2020 20:39:57 +0400 Subject: [PATCH 5/9] Change endpoint path --- docs/API/admin_api.md | 3 +-- lib/pleroma/web/router.ex | 2 +- .../admin_api/controllers/frontend_controller_test.exs | 8 ++++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/API/admin_api.md b/docs/API/admin_api.md index cbf4b9134..e18d5e513 100644 --- a/docs/API/admin_api.md +++ b/docs/API/admin_api.md @@ -1525,8 +1525,7 @@ Returns the content of the document ] ``` - -## `POST /api/pleroma/admin/frontends +## `POST /api/pleroma/admin/frontends/install ### Install a frontend diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index f497a96b7..75a885377 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -245,7 +245,7 @@ defmodule Pleroma.Web.Router do delete("/chats/:id/messages/:message_id", ChatController, :delete_message) get("/frontends", FrontendController, :index) - post("/frontends", FrontendController, :install) + post("/frontends/install", FrontendController, :install) post("/backups", AdminAPIController, :create_backup) end diff --git a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs index 1d4fbfa03..db28a27b6 100644 --- a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs @@ -44,7 +44,7 @@ test "it lists available frontends", %{conn: conn} do end end - describe "POST /api/pleroma/admin/frontends" do + describe "POST /api/pleroma/admin/frontends/install" do test "from available frontends", %{conn: conn} do clear_config([:frontends, :available], %{ "pleroma" => %{ @@ -60,7 +60,7 @@ test "from available frontends", %{conn: conn} do conn |> put_req_header("content-type", "application/json") - |> post("/api/pleroma/admin/frontends", %{name: "pleroma"}) + |> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"}) |> json_response_and_validate_schema(:ok) assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) @@ -92,7 +92,7 @@ test "from a file", %{conn: conn} do conn |> put_req_header("content-type", "application/json") - |> post("/api/pleroma/admin/frontends", %{ + |> post("/api/pleroma/admin/frontends/install", %{ name: "pleroma", file: "test/fixtures/tesla_mock/frontend.zip" }) @@ -108,7 +108,7 @@ test "from an URL", %{conn: conn} do conn |> put_req_header("content-type", "application/json") - |> post("/api/pleroma/admin/frontends", %{ + |> post("/api/pleroma/admin/frontends/install", %{ name: "unknown", ref: "baka", build_url: "http://gensokyo.2hu/madeup.zip", From 81145ecdf52c74147c842ab6c099abf5e1ad1eff Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 11 Nov 2020 20:42:05 +0400 Subject: [PATCH 6/9] Fix markdown --- docs/API/admin_api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/API/admin_api.md b/docs/API/admin_api.md index e18d5e513..4c72d3d61 100644 --- a/docs/API/admin_api.md +++ b/docs/API/admin_api.md @@ -1525,7 +1525,7 @@ Returns the content of the document ] ``` -## `POST /api/pleroma/admin/frontends/install +## `POST /api/pleroma/admin/frontends/install` ### Install a frontend From bb9650f3c26c0a6155cfb15f5e4da8257316661a Mon Sep 17 00:00:00 2001 From: lain Date: Tue, 17 Nov 2020 16:43:07 +0100 Subject: [PATCH 7/9] FrontendController: Return error on installation error. --- lib/pleroma/frontend.ex | 2 ++ .../controllers/frontend_controller.ex | 6 +++--- .../operations/admin/frontend_operation.ex | 3 ++- .../controllers/frontend_controller_test.exs | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/frontend.ex b/lib/pleroma/frontend.ex index b3d4c3325..bf935a728 100644 --- a/lib/pleroma/frontend.ex +++ b/lib/pleroma/frontend.ex @@ -42,9 +42,11 @@ def install(name, opts \\ []) do else {:download_or_unzip, _} -> Logger.info("Could not download or unzip the frontend") + {:error, "Could not download or unzip the frontend"} _e -> Logger.info("Could not install the frontend") + {:error, "Could not install the frontend"} end end diff --git a/lib/pleroma/web/admin_api/controllers/frontend_controller.ex b/lib/pleroma/web/admin_api/controllers/frontend_controller.ex index 4518bed5a..fac3522b8 100644 --- a/lib/pleroma/web/admin_api/controllers/frontend_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/frontend_controller.ex @@ -29,9 +29,9 @@ def index(conn, _params) do end def install(%{body_params: params} = conn, _params) do - Pleroma.Frontend.install(params.name, Map.delete(params, :name)) - - index(conn, %{}) + with :ok <- Pleroma.Frontend.install(params.name, Map.delete(params, :name)) do + index(conn, %{}) + end end defp installed do diff --git a/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex b/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex index 9d7d017a2..96d4cdee7 100644 --- a/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/frontend_operation.ex @@ -36,7 +36,8 @@ def install_operation do requestBody: request_body("Parameters", install_request(), required: true), responses: %{ 200 => Operation.response("Response", "application/json", list_of_frontends()), - 403 => Operation.response("Forbidden", "application/json", ApiError) + 403 => Operation.response("Forbidden", "application/json", ApiError), + 400 => Operation.response("Error", "application/json", ApiError) } } end diff --git a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs index db28a27b6..94873f6db 100644 --- a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs @@ -118,5 +118,24 @@ test "from an URL", %{conn: conn} do assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"])) end + + test "failing returns an error", %{conn: conn} do + Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} -> + %Tesla.Env{status: 404, body: ""} + end) + + result = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/frontends/install", %{ + name: "unknown", + ref: "baka", + build_url: "http://gensokyo.2hu/madeup.zip", + build_dir: "" + }) + |> json_response_and_validate_schema(400) + + assert result == %{"error" => "Could not download or unzip the frontend"} + end end end From e6d4d62f8547859d3c322621f001505ffe814503 Mon Sep 17 00:00:00 2001 From: lain Date: Tue, 17 Nov 2020 16:44:20 +0100 Subject: [PATCH 8/9] Docs: Add info about frontend install error response --- docs/API/admin_api.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/API/admin_api.md b/docs/API/admin_api.md index 4c72d3d61..19ac6a65f 100644 --- a/docs/API/admin_api.md +++ b/docs/API/admin_api.md @@ -1556,3 +1556,9 @@ Returns the content of the document } ] ``` + +```json +{ + "error": "Could not install frontend" +} +``` From d7b63272b8fb0028c9ce45a29ca9a569e5974c87 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 18 Nov 2020 18:32:13 +0100 Subject: [PATCH 9/9] Changelog: Move api info to api heading. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0e4094ea..616f9deeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Account backup. - Configuration: Add `:instance, autofollowing_nicknames` setting to provide a way to make accounts automatically follow new users that register on the local Pleroma instance. - Ability to view remote timelines, with ex. `/api/v1/timelines/public?instance=lain.com` and streams `public:remote` and `public:remote:media`. -- Pleroma API: An endpoint to manage frontends - The site title is now injected as a `title` tag like preloads or metadata.
@@ -29,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Pleroma API: Add `idempotency_key` to the chat message entity that can be used for optimistic message sending. - Pleroma API: (`GET /api/v1/pleroma/federation_status`) Add a way to get a list of unreachable instances. - Mastodon API: User and conversation mutes can now auto-expire if `expires_in` parameter was given while adding the mute. +- Admin API: An endpoint to manage frontends