MastodonAPI: /api/v2/media endpoints

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-04-29 17:38:14 +02:00
commit cb363f0183
No known key found for this signature in database
GPG key ID: D5B7A8E43C997DEE
3 changed files with 91 additions and 5 deletions

View file

@ -29,6 +29,26 @@ 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}} = conn, %{"file" => file} = data) 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
@ -42,4 +62,15 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
end
def update(_conn, _data), do: {:error, :bad_request}
@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

View file

@ -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