Merge branch 'features/mastoapi_v2_media' into 'develop'
MastodonAPI: /api/v2/media endpoints See merge request pleroma/pleroma!2439
This commit is contained in:
commit
d96f8f17e8
132
lib/pleroma/web/api_spec/operations/media_operation.ex
Normal file
132
lib/pleroma/web/api_spec/operations/media_operation.ex
Normal file
@ -0,0 +1,132 @@
|
||||
# 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
|
||||
alias Pleroma.Web.ApiSpec.Schemas.Attachment
|
||||
|
||||
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", 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,
|
||||
required: [:file],
|
||||
properties: %{
|
||||
file: %Schema{
|
||||
type: :string,
|
||||
format: :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 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"]}],
|
||||
parameters: [id_param()],
|
||||
requestBody: Helpers.request_body("Parameters", update_request()),
|
||||
responses: %{
|
||||
200 => Operation.response("Media", "application/json", Attachment),
|
||||
400 => Operation.response("Media", "application/json", ApiError),
|
||||
401 => Operation.response("Media", "application/json", ApiError),
|
||||
422 => Operation.response("Media", "application/json", ApiError)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
defp update_request do
|
||||
%Schema{
|
||||
title: "MediaUpdateRequest",
|
||||
description: "POST body for updating an attachment",
|
||||
type: :object,
|
||||
properties: %{
|
||||
file: %Schema{
|
||||
type: :string,
|
||||
format: :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",
|
||||
parameters: [id_param()],
|
||||
security: [%{"oAuth" => ["read:media"]}],
|
||||
responses: %{
|
||||
200 => Operation.response("Media", "application/json", 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", Attachment),
|
||||
422 => Operation.response("Media", "application/json", ApiError),
|
||||
500 => Operation.response("Media", "application/json", ApiError)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
defp id_param do
|
||||
Operation.parameter(:id, :path, :string, "The ID of the Attachment entity")
|
||||
end
|
||||
end
|
@ -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,
|
||||
|
@ -11,17 +11,20 @@ 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
|
||||
def create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
|
||||
with {:ok, object} <-
|
||||
ActivityPub.upload(
|
||||
file,
|
||||
actor: User.ap_id(user),
|
||||
description: Map.get(data, "description")
|
||||
description: Map.get(data, :description)
|
||||
) do
|
||||
attachment_data = Map.put(object.data, "id", object.id)
|
||||
|
||||
@ -29,9 +32,28 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
|
||||
end
|
||||
end
|
||||
|
||||
def create(_conn, _data), do: {:error, :bad_request}
|
||||
|
||||
@doc "POST /api/v2/media"
|
||||
def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
|
||||
with {:ok, object} <-
|
||||
ActivityPub.upload(
|
||||
file,
|
||||
actor: User.ap_id(user),
|
||||
description: Map.get(data, :description)
|
||||
) do
|
||||
attachment_data = Map.put(object.data, "id", object.id)
|
||||
|
||||
conn
|
||||
|> put_status(202)
|
||||
|> render("attachment.json", %{attachment: attachment_data})
|
||||
end
|
||||
end
|
||||
|
||||
def create2(_conn, _data), do: {:error, :bad_request}
|
||||
|
||||
@doc "PUT /api/v1/media/:id"
|
||||
def update(%{assigns: %{user: user}} = conn, %{"id" => id, "description" => description})
|
||||
when is_binary(description) do
|
||||
def update(%{assigns: %{user: user}, body_params: %{description: description}} = conn, %{id: id}) do
|
||||
with %Object{} = object <- Object.get_by_id(id),
|
||||
true <- Object.authorize_mutation(object, user),
|
||||
{:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
|
||||
@ -41,5 +63,16 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
|
||||
end
|
||||
end
|
||||
|
||||
def update(_conn, _data), do: {:error, :bad_request}
|
||||
def update(conn, data), do: show(conn, data)
|
||||
|
||||
@doc "GET /api/v1/media/:id"
|
||||
def show(conn, %{id: id}) do
|
||||
with %Object{data: data, id: object_id} <- Object.get_by_id(id) do
|
||||
attachment_data = Map.put(data, "id", object_id)
|
||||
|
||||
render(conn, "attachment.json", %{attachment: attachment_data})
|
||||
end
|
||||
end
|
||||
|
||||
def get_media(_conn, _data), do: {:error, :bad_request}
|
||||
end
|
||||
|
@ -403,6 +403,7 @@ defmodule Pleroma.Web.Router do
|
||||
post("/markers", MarkerController, :upsert)
|
||||
|
||||
post("/media", MediaController, :create)
|
||||
get("/media/:id", MediaController, :show)
|
||||
put("/media/:id", MediaController, :update)
|
||||
|
||||
get("/notifications", NotificationController, :index)
|
||||
@ -497,6 +498,8 @@ defmodule Pleroma.Web.Router do
|
||||
scope "/api/v2", Pleroma.Web.MastodonAPI do
|
||||
pipe_through(:api)
|
||||
get("/search", SearchController, :search2)
|
||||
|
||||
post("/media", MediaController, :create2)
|
||||
end
|
||||
|
||||
scope "/api", Pleroma.Web do
|
||||
|
@ -11,7 +11,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
|
||||
|
||||
setup do: oauth_access(["write:media"])
|
||||
|
||||
describe "media upload" do
|
||||
describe "Upload media" do
|
||||
setup do
|
||||
image = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
@ -25,13 +25,14 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
|
||||
setup do: clear_config([:media_proxy])
|
||||
setup do: clear_config([Pleroma.Upload])
|
||||
|
||||
test "returns uploaded image", %{conn: conn, image: image} do
|
||||
test "/api/v1/media", %{conn: conn, image: image} do
|
||||
desc = "Description of the image"
|
||||
|
||||
media =
|
||||
conn
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> post("/api/v1/media", %{"file" => image, "description" => desc})
|
||||
|> json_response(:ok)
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|
||||
assert media["type"] == "image"
|
||||
assert media["description"] == desc
|
||||
@ -40,9 +41,32 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
|
||||
object = Object.get_by_id(media["id"])
|
||||
assert object.data["actor"] == User.ap_id(conn.assigns[:user])
|
||||
end
|
||||
|
||||
test "/api/v2/media", %{conn: conn, image: image} do
|
||||
desc = "Description of the image"
|
||||
|
||||
response =
|
||||
conn
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> post("/api/v2/media", %{"file" => image, "description" => desc})
|
||||
|> json_response_and_validate_schema(202)
|
||||
|
||||
assert media_id = response["id"]
|
||||
|
||||
media =
|
||||
conn
|
||||
|> get("/api/v1/media/#{media_id}")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
assert media["type"] == "image"
|
||||
assert media["description"] == desc
|
||||
assert media["id"]
|
||||
object = Object.get_by_id(media["id"])
|
||||
assert object.data["actor"] == User.ap_id(conn.assigns[:user])
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT /api/v1/media/:id" do
|
||||
describe "Update media description" do
|
||||
setup %{user: actor} do
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
@ -60,23 +84,45 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
|
||||
[object: object]
|
||||
end
|
||||
|
||||
test "updates name of media", %{conn: conn, object: object} do
|
||||
test "/api/v1/media/:id good request", %{conn: conn, object: object} do
|
||||
media =
|
||||
conn
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
|
||||
|> json_response(:ok)
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|
||||
assert media["description"] == "test-media"
|
||||
assert refresh_record(object).data["name"] == "test-media"
|
||||
end
|
||||
end
|
||||
|
||||
test "returns error when request is bad", %{conn: conn, object: object} do
|
||||
describe "Get media by id" do
|
||||
setup %{user: actor} do
|
||||
file = %Plug.Upload{
|
||||
content_type: "image/jpg",
|
||||
path: Path.absname("test/fixtures/image.jpg"),
|
||||
filename: "an_image.jpg"
|
||||
}
|
||||
|
||||
{:ok, %Object{} = object} =
|
||||
ActivityPub.upload(
|
||||
file,
|
||||
actor: User.ap_id(actor),
|
||||
description: "test-media"
|
||||
)
|
||||
|
||||
[object: object]
|
||||
end
|
||||
|
||||
test "/api/v1/media/:id", %{conn: conn, object: object} do
|
||||
media =
|
||||
conn
|
||||
|> put("/api/v1/media/#{object.id}", %{})
|
||||
|> json_response(400)
|
||||
|> get("/api/v1/media/#{object.id}")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|
||||
assert media == %{"error" => "bad_request"}
|
||||
assert media["description"] == "test-media"
|
||||
assert media["type"] == "image"
|
||||
assert media["id"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user