Add OpenAPISpex for MediaController
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Thu, 14 May 2020 08:50:12 +0000 (10:50 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Thu, 14 May 2020 10:13:25 +0000 (12:13 +0200)
lib/pleroma/web/api_spec/operations/media_operation.ex [new file with mode: 0644]
lib/pleroma/web/api_spec/schemas/attachment.ex
lib/pleroma/web/mastodon_api/controllers/media_controller.ex

diff --git a/lib/pleroma/web/api_spec/operations/media_operation.ex b/lib/pleroma/web/api_spec/operations/media_operation.ex
new file mode 100644 (file)
index 0000000..0fe686e
--- /dev/null
@@ -0,0 +1,131 @@
+# 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.MediaOperation do
+  alias OpenApiSpex.Operation
+  alias OpenApiSpex.Schema
+  alias Pleroma.Web.ApiSpec.Helpers
+  alias Pleroma.Web.ApiSpec.Schemas.ApiError
+
+  def open_api_operation(action) do
+    operation = String.to_existing_atom("#{action}_operation")
+    apply(__MODULE__, operation, [])
+  end
+
+  def create_operation do
+    %Operation{
+      tags: ["media"],
+      summary: "Upload media as attachment",
+      description: "Creates an attachment to be used with a new status.",
+      operationId: "MediaController.create",
+      security: [%{"oAuth" => ["write:media"]}],
+      requestBody: Helpers.request_body("Parameters", create_request()),
+      responses: %{
+        200 =>
+          Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
+        401 => Operation.response("Media", "application/json", ApiError),
+        422 => Operation.response("Media", "application/json", ApiError)
+      }
+    }
+  end
+
+  defp create_request() do
+    %Schema{
+      title: "MediaCreateRequest",
+      description: "POST body for creating an attachment",
+      type: :object,
+      properties: %{
+        file: %Schema{
+          type: :binary,
+          description: "The file to be attached, using multipart form data.",
+          required: true
+        },
+        description: %Schema{
+          type: :string,
+          description: "A plain-text description of the media, for accessibility purposes."
+        },
+        focus: %Schema{
+          type: :string,
+          description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
+        }
+      }
+    }
+  end
+
+  def update_operation do
+    %Operation{
+      tags: ["media"],
+      summary: "Upload media as attachment",
+      description: "Creates an attachment to be used with a new status.",
+      operationId: "MediaController.update",
+      security: [%{"oAuth" => ["write:media"]}],
+      requestBody: Helpers.request_body("Parameters", update_request()),
+      responses: %{
+        200 =>
+          Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
+        401 => Operation.response("Media", "application/json", ApiError),
+        422 => Operation.response("Media", "application/json", ApiError)
+      }
+    }
+  end
+
+  defp update_request() do
+    %Schema{
+      title: "MediaCreateRequest",
+      description: "POST body for creating an attachment",
+      type: :object,
+      properties: %{
+        id: %Schema{
+          type: :string,
+          description: "The id of the Attachment entity to be updated",
+          required: true
+        },
+        file: %Schema{
+          type: :binary,
+          description: "The file to be attached, using multipart form data."
+        },
+        description: %Schema{
+          type: :string,
+          description: "A plain-text description of the media, for accessibility purposes."
+        },
+        focus: %Schema{
+          type: :string,
+          description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
+        }
+      }
+    }
+  end
+
+  def show_operation do
+    %Operation{
+      tags: ["media"],
+      summary: "Show Uploaded media attachment",
+      operationId: "MediaController.show",
+      security: [%{"oAuth" => ["read:media"]}],
+      responses: %{
+        200 =>
+          Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
+        401 => Operation.response("Media", "application/json", ApiError),
+        422 => Operation.response("Media", "application/json", ApiError)
+      }
+    }
+  end
+
+  def create2_operation do
+    %Operation{
+      tags: ["media"],
+      summary: "Upload media as attachment",
+      description: "Creates an attachment to be used with a new status.",
+      operationId: "MediaController.create2",
+      security: [%{"oAuth" => ["write:media"]}],
+      requestBody: Helpers.request_body("Parameters", create_request()),
+      responses: %{
+        202 =>
+          Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
+        422 => Operation.response("Media", "application/json", ApiError),
+        500 => Operation.response("Media", "application/json", ApiError)
+      }
+    }
+  end
+end
index c146c416ebe3b7acba93e9d0fb9889db9fdd595b..c6edf6d362dd1b9a062cf30f9e48ae56f735d958 100644 (file)
@@ -13,7 +13,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Attachment do
     type: :object,
     requried: [:id, :url, :preview_url],
     properties: %{
-      id: %Schema{type: :string},
+      id: %Schema{type: :string, description: "The ID of the attachment in the database."},
       url: %Schema{
         type: :string,
         format: :uri,
index 1997ac1af821370ab59ee77d1a3cded634923940..52e0b22d81da5300c953f41b5df0d634be1002ab 100644 (file)
@@ -11,10 +11,13 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
   alias Pleroma.Web.ActivityPub.ActivityPub
 
   action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
+  plug(Pleroma.Web.ApiSpec.CastAndValidate)
   plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
 
   plug(OAuthScopesPlug, %{scopes: ["write:media"]})
 
+  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MediaOperation
+
   @doc "POST /api/v1/media"
   def create(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
     with {:ok, object} <-