added api spec
This commit is contained in:
parent
5ae56aafb2
commit
917d325972
5 changed files with 164 additions and 52 deletions
80
lib/pleroma/web/api_spec/operations/user_import_operation.ex
Normal file
80
lib/pleroma/web/api_spec/operations/user_import_operation.ex
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.ApiSpec.UserImportOperation do
|
||||
alias OpenApiSpex.Operation
|
||||
alias OpenApiSpex.Schema
|
||||
alias Pleroma.Web.ApiSpec.Schemas.ApiError
|
||||
|
||||
import Pleroma.Web.ApiSpec.Helpers
|
||||
|
||||
@spec open_api_operation(atom) :: Operation.t()
|
||||
def open_api_operation(action) do
|
||||
operation = String.to_existing_atom("#{action}_operation")
|
||||
apply(__MODULE__, operation, [])
|
||||
end
|
||||
|
||||
def follow_operation do
|
||||
%Operation{
|
||||
tags: ["follow_import"],
|
||||
summary: "Imports your follows.",
|
||||
operationId: "UserImportController.follow",
|
||||
requestBody: request_body("Parameters", import_request(), required: true),
|
||||
responses: %{
|
||||
200 => ok_response(),
|
||||
500 => Operation.response("Error", "application/json", ApiError)
|
||||
},
|
||||
security: [%{"oAuth" => ["write:follow"]}]
|
||||
}
|
||||
end
|
||||
|
||||
def blocks_operation do
|
||||
%Operation{
|
||||
tags: ["blocks_import"],
|
||||
summary: "Imports your blocks.",
|
||||
operationId: "UserImportController.blocks",
|
||||
requestBody: request_body("Parameters", import_request(), required: true),
|
||||
responses: %{
|
||||
200 => ok_response(),
|
||||
500 => Operation.response("Error", "application/json", ApiError)
|
||||
},
|
||||
security: [%{"oAuth" => ["write:blocks"]}]
|
||||
}
|
||||
end
|
||||
|
||||
def mutes_operation do
|
||||
%Operation{
|
||||
tags: ["mutes_import"],
|
||||
summary: "Imports your mutes.",
|
||||
operationId: "UserImportController.mutes",
|
||||
requestBody: request_body("Parameters", import_request(), required: true),
|
||||
responses: %{
|
||||
200 => ok_response(),
|
||||
500 => Operation.response("Error", "application/json", ApiError)
|
||||
},
|
||||
security: [%{"oAuth" => ["write:mutes"]}]
|
||||
}
|
||||
end
|
||||
|
||||
defp import_request do
|
||||
%Schema{
|
||||
type: :object,
|
||||
required: [:list],
|
||||
properties: %{
|
||||
list: %Schema{
|
||||
description:
|
||||
"STRING or FILE containing a whitespace-separated list of accounts to import.",
|
||||
anyOf: [
|
||||
%Schema{type: :string, format: :binary},
|
||||
%Schema{type: :string}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
defp ok_response do
|
||||
Operation.response("Ok", "application/json", %Schema{type: :string, example: "ok"})
|
||||
end
|
||||
end
|
||||
|
|
@ -9,16 +9,20 @@ defmodule Pleroma.Web.PleromaAPI.UserImportController do
|
|||
|
||||
alias Pleroma.Plugs.OAuthScopesPlug
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ApiSpec
|
||||
|
||||
plug(OAuthScopesPlug, %{scopes: ["follow", "write:follows"]} when action == :follow)
|
||||
plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks)
|
||||
plug(OAuthScopesPlug, %{scopes: ["follow", "write:mutes"]} when action == :mutes)
|
||||
|
||||
def follow(conn, %{"list" => %Plug.Upload{path: path}}) do
|
||||
follow(conn, %{"list" => File.read!(path)})
|
||||
plug(OpenApiSpex.Plug.CastAndValidate)
|
||||
defdelegate open_api_operation(action), to: ApiSpec.UserImportOperation
|
||||
|
||||
def follow(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
|
||||
follow(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
|
||||
end
|
||||
|
||||
def follow(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
|
||||
def follow(%{assigns: %{user: follower}, body_params: %{list: list}} = conn, _) do
|
||||
identifiers =
|
||||
list
|
||||
|> String.split("\n")
|
||||
|
|
@ -31,20 +35,20 @@ defmodule Pleroma.Web.PleromaAPI.UserImportController do
|
|||
json(conn, "job started")
|
||||
end
|
||||
|
||||
def blocks(conn, %{"list" => %Plug.Upload{path: path}}) do
|
||||
blocks(conn, %{"list" => File.read!(path)})
|
||||
def blocks(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
|
||||
blocks(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
|
||||
end
|
||||
|
||||
def blocks(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
|
||||
def blocks(%{assigns: %{user: blocker}, body_params: %{list: list}} = conn, _) do
|
||||
User.Import.blocks_import(blocker, prepare_user_identifiers(list))
|
||||
json(conn, "job started")
|
||||
end
|
||||
|
||||
def mutes(conn, %{"list" => %Plug.Upload{path: path}}) do
|
||||
mutes(conn, %{"list" => File.read!(path)})
|
||||
def mutes(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
|
||||
mutes(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
|
||||
end
|
||||
|
||||
def mutes(%{assigns: %{user: user}} = conn, %{"list" => list}) do
|
||||
def mutes(%{assigns: %{user: user}, body_params: %{list: list}} = conn, _) do
|
||||
User.Import.mutes_import(user, prepare_user_identifiers(list))
|
||||
json(conn, "job started")
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue