## `GET /api/pleroma/admin/instance_document/:document_name`
-### Gets an instance document
+### Get an instance document
- Authentication: required
- Response:
-``` json
-{
- "url": "https://example.com/instance/panel.html"
-}
+Returns the content of the document
+
+```html
+<h1>Instance panel</h1>
```
## `PATCH /api/pleroma/admin/instance_document/:document_name`
- Params:
- `file` (the file to be uploaded, using multipart form data.)
-### Updates an instance document
+### Update an instance document
- Authentication: required
defmodule Pleroma.Web.AdminAPI.InstanceDocumentController do
use Pleroma.Web, :controller
+ alias Pleroma.Plugs.InstanceStatic
alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Web.InstanceDocument
plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action in [:update, :delete])
def show(conn, %{name: document_name}) do
- with {:ok, url} <- InstanceDocument.get(document_name) do
- json(conn, %{"url" => url})
+ with {:ok, url} <- InstanceDocument.get(document_name),
+ {:ok, content} <- File.read(InstanceStatic.file_path(url)) do
+ conn
+ |> put_resp_content_type("text/html")
+ |> send_resp(200, content)
end
end
| Helpers.admin_api_params()
],
responses: %{
- 200 => Operation.response("InstanceDocument", "application/json", instance_document()),
+ 200 => document_content(),
400 => Operation.response("Bad Request", "application/json", ApiError),
403 => Operation.response("Forbidden", "application/json", ApiError),
404 => Operation.response("Not Found", "application/json", ApiError)
}
}
end
+
+ defp document_content do
+ Operation.response("InstanceDocumentContent", "text/html", %Schema{
+ type: :string,
+ example: "<h1>Instance panel</h1>"
+ })
+ end
end
@spec get(String.t()) :: {:ok, String.t()} | {:error, atom()}
def get(document_name) do
case Map.fetch(@instance_documents, document_name) do
- {:ok, path} -> {:ok, Path.join(Endpoint.url(), path)}
+ {:ok, path} -> {:ok, path}
_ -> {:error, :not_found}
end
end
test "return the instance document url", %{conn: conn} do
conn = get(conn, "/api/pleroma/admin/instance_document/instance-panel")
- assert %{"url" => url} = json_response_and_validate_schema(conn, 200)
- index = get(build_conn(), url)
- response = html_response(index, 200)
- assert String.contains?(response, @default_instance_panel)
+ assert content = html_response(conn, 200)
+ assert String.contains?(content, @default_instance_panel)
end
test "it returns 403 if requested by a non-admin" do
conn
|> get("/api/pleroma/admin/instance_document/instance-panel")
- assert %{"url" => url} = json_response_and_validate_schema(conn_resp, 200)
- index = get(build_conn(), url)
- assert html_response(index, 200) == "Custom instance panel"
+ assert html_response(conn_resp, 200) == "Custom instance panel"
conn
|> delete("/api/pleroma/admin/instance_document/instance-panel")
conn
|> get("/api/pleroma/admin/instance_document/instance-panel")
- assert %{"url" => url} = json_response_and_validate_schema(conn_resp, 200)
- index = get(build_conn(), url)
- response = html_response(index, 200)
- assert String.contains?(response, @default_instance_panel)
+ assert content = html_response(conn_resp, 200)
+ assert String.contains?(content, @default_instance_panel)
end
end
end