Add `GET /api/pleroma/admin/relay` endpoint - lists all followed relays
authorMaxim Filippov <colixer@gmail.com>
Fri, 11 Oct 2019 16:12:29 +0000 (19:12 +0300)
committerMaxim Filippov <colixer@gmail.com>
Fri, 11 Oct 2019 16:12:29 +0000 (19:12 +0300)
CHANGELOG.md
docs/API/admin_api.md
lib/mix/tasks/pleroma/relay.ex
lib/pleroma/web/activity_pub/relay.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/router.ex
test/web/admin_api/admin_api_controller_test.exs
test/web/mastodon_api/controllers/search_controller_test.exs

index bd06ec866f9bd6a73306601f17a94ea1fb2e07f4..2e7817a544d48b038e311dc5d4defa42124de291 100644 (file)
@@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Authentication: Added rate limit for password-authorized actions / login existence checks
 - Metadata Link: Atom syndication Feed
 - Mix task to re-count statuses for all users (`mix pleroma.count_statuses`)
+- Admin API: Add `GET /api/pleroma/admin/relay` endpoint - lists all followed relays
 
 ### Changed
 - **Breaking:** Elixir >=1.8 is now required (was >= 1.7)
index ee9e68cb1456c79c3231fa9a259e9dddbdfbbacb..1c5c8afa469df7598fa1dc141a71f1e638051a81 100644 (file)
@@ -222,6 +222,14 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
 - Response:
   - On success: URL of the unfollowed relay
 
+## `GET /api/pleroma/admin/relay`
+
+### List Relays
+
+- Params: none
+- Response:
+  - On success: JSON array of relays
+
 ## `/api/pleroma/admin/users/invite_token`
 
 ### Create an account registration invite token
index d7a7b599fee4f3a97a52edceb043f80b282fe5e6..7ef5f9678534a8669cb434910d5b56a78b7866f3 100644 (file)
@@ -5,7 +5,6 @@
 defmodule Mix.Tasks.Pleroma.Relay do
   use Mix.Task
   import Mix.Pleroma
-  alias Pleroma.User
   alias Pleroma.Web.ActivityPub.Relay
 
   @shortdoc "Manages remote relays"
@@ -36,13 +35,10 @@ defmodule Mix.Tasks.Pleroma.Relay do
   def run(["list"]) do
     start_pleroma()
 
-    with %User{following: following} = _user <- Relay.get_actor() do
-      following
-      |> Enum.map(fn entry -> URI.parse(entry).host end)
-      |> Enum.uniq()
-      |> Enum.each(&shell_info(&1))
+    with {:ok, list} <- Relay.list() do
+      list |> Enum.each(&shell_info(&1))
     else
-      e -> shell_error("Error while fetching relay subscription list: #{inspect(e)}")
+      {:error, e} -> shell_error("Error while fetching relay subscription list: #{inspect(e)}")
     end
   end
 end
index c2ac38907dd8cd796878363248adbae74f6b7ac9..03fc434a98119975fc01dc1b603fd1e29d1de739 100644 (file)
@@ -51,6 +51,20 @@ defmodule Pleroma.Web.ActivityPub.Relay do
 
   def publish(_), do: {:error, "Not implemented"}
 
+  @spec list() :: {:ok, [String.t()]} | {:error, any()}
+  def list do
+    with %User{following: following} = _user <- get_actor() do
+      list =
+        following
+        |> Enum.map(fn entry -> URI.parse(entry).host end)
+        |> Enum.uniq()
+
+      {:ok, list}
+    else
+      error -> format_error(error)
+    end
+  end
+
   defp format_error({:error, error}), do: format_error(error)
 
   defp format_error(error) do
index 513bae80060bc5506a1708bee93b99f357bb6ce6..24dda75a917577130034573d24ae3021d78fc541 100644 (file)
@@ -401,6 +401,16 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     end
   end
 
+  def relay_list(conn, _params) do
+    with {:ok, list} <- Relay.list() do
+      json(conn, %{relays: list})
+    else
+      _ ->
+        conn
+        |> put_status(500)
+    end
+  end
+
   def relay_follow(%{assigns: %{user: admin}} = conn, %{"relay_url" => target}) do
     with {:ok, _message} <- Relay.follow(target) do
       ModerationLog.insert_log(%{
index ae799b8ac36650112f23e7756d0cf475754557a0..8cc967af9d85bf0eedf1f6c4bb0f478818a1a7cc 100644 (file)
@@ -152,6 +152,7 @@ defmodule Pleroma.Web.Router do
 
     put("/users/:nickname/activation_status", AdminAPIController, :set_activation_status)
 
+    get("/relay", AdminAPIController, :relay_list)
     post("/relay", AdminAPIController, :relay_follow)
     delete("/relay", AdminAPIController, :relay_unfollow)
 
index b5c355e66f3eb9c36f6ad41bf676306f8f518c26..d7dce2da48b917ae9bdd3e41709697353622ee46 100644 (file)
@@ -17,6 +17,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
   alias Pleroma.Web.MediaProxy
   import Pleroma.Factory
 
+  setup_all do
+    Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
+
+    :ok
+  end
+
   describe "/api/pleroma/admin/users" do
     test "Delete" do
       admin = insert(:user, info: %{is_admin: true})
@@ -2486,6 +2492,74 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       assert User.get_by_id(user.id).info.password_reset_pending == true
     end
   end
+
+  describe "relays" do
+    setup %{conn: conn} do
+      admin = insert(:user, info: %{is_admin: true})
+
+      %{conn: assign(conn, :user, admin), admin: admin}
+    end
+
+    test "POST /relay", %{admin: admin} do
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> post("/api/pleroma/admin/relay", %{
+          relay_url: "http://mastodon.example.org/users/admin"
+        })
+
+      assert json_response(conn, 200) == "http://mastodon.example.org/users/admin"
+
+      log_entry = Repo.one(ModerationLog)
+
+      assert ModerationLog.get_log_entry_message(log_entry) ==
+               "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
+    end
+
+    test "GET /relay", %{admin: admin} do
+      Pleroma.Web.ActivityPub.Relay.get_actor()
+      |> Ecto.Changeset.change(
+        following: [
+          "http://test-app.com/user/test1",
+          "http://test-app.com/user/test1",
+          "http://test-app-42.com/user/test1"
+        ]
+      )
+      |> Pleroma.User.update_and_set_cache()
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> get("/api/pleroma/admin/relay")
+
+      assert json_response(conn, 200)["relays"] -- ["test-app.com", "test-app-42.com"] == []
+    end
+
+    test "DELETE /relay", %{admin: admin} do
+      build_conn()
+      |> assign(:user, admin)
+      |> post("/api/pleroma/admin/relay", %{
+        relay_url: "http://mastodon.example.org/users/admin"
+      })
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> delete("/api/pleroma/admin/relay", %{
+          relay_url: "http://mastodon.example.org/users/admin"
+        })
+
+      assert json_response(conn, 200) == "http://mastodon.example.org/users/admin"
+
+      [log_entry_one, log_entry_two] = Repo.all(ModerationLog)
+
+      assert ModerationLog.get_log_entry_message(log_entry_one) ==
+               "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
+
+      assert ModerationLog.get_log_entry_message(log_entry_two) ==
+               "@#{admin.nickname} unfollowed relay: http://mastodon.example.org/users/admin"
+    end
+  end
 end
 
 # Needed for testing
index 0ca896e012114403ec0d035793521eac8fca902d..62c3423f1021ea3be983d3013d271266c0444c02 100644 (file)
@@ -70,7 +70,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
         get(conn, "/api/v2/search", %{"q" => "天子"})
         |> json_response(200)
 
-      [account] == results["accounts"]
+      assert [account] == results["accounts"]
       assert account["id"] == to_string(user_three.id)
     end
   end