diff options
Diffstat (limited to 'lib/pleroma/web/mastodon_api')
-rw-r--r-- | lib/pleroma/web/mastodon_api/controllers/media_controller.ex | 42 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/views/status_view.ex | 56 |
2 files changed, 94 insertions, 4 deletions
diff --git a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex index d6949ed80..1bd521460 100644 --- a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do use Pleroma.Web, :controller + alias Pleroma.Media alias Pleroma.Object alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub @@ -22,6 +23,20 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do @doc "POST /api/v1/media" def create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do + with {:ok, media} <- + ActivityPub.upload( + file, + user: user, + actor: User.ap_id(user), + description: Map.get(data, :description) + ) do + render(conn, "media.json", %{media: media}) + end + end + + def create(_conn, _data), do: {:error, :bad_request} + + def _create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do with {:ok, object} <- ActivityPub.upload( file, @@ -34,7 +49,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do end end - def create(_conn, _data), do: {:error, :bad_request} + 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 @@ -56,6 +71,18 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do @doc "PUT /api/v1/media/:id" def update(%{assigns: %{user: user}, body_params: %{description: description}} = conn, %{id: id}) do + with %Media{} = media <- Media.get_by_id(id), + :ok <- Media.authorize_access(media, user), + {:ok, %Media{} = media} <- Media.update(media, %{"name" => description}) do + render(conn, "media.json", %{media: media}) + end + end + + def update(conn, data), do: show(conn, data) + + def _update(%{assigns: %{user: user}, body_params: %{description: description}} = conn, %{ + id: id + }) do with %Object{} = object <- Object.get_by_id(id), :ok <- Object.authorize_access(object, user), {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do @@ -65,10 +92,19 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do end end - def update(conn, data), do: show(conn, data) + def _update(conn, data), do: show(conn, data) @doc "GET /api/v1/media/:id" def show(%{assigns: %{user: user}} = conn, %{id: id}) do + with %Pleroma.Media{} = media <- Pleroma.Media.get_by_id(id), + :ok <- Pleroma.Media.authorize_access(media, user) do + render(conn, "media.json", %{media: media}) + end + end + + def show(_conn, _data), do: {:error, :bad_request} + + def _show(%{assigns: %{user: user}} = conn, %{id: id}) do with %Object{data: data, id: object_id} = object <- Object.get_by_id(id), :ok <- Object.authorize_access(object, user) do attachment_data = Map.put(data, "id", object_id) @@ -77,5 +113,5 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do end end - def show(_conn, _data), do: {:error, :bad_request} + def _show(_conn, _data), do: {:error, :bad_request} end diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index cd1a85088..8c6d56e0f 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -243,7 +243,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do end attachment_data = object.data["attachment"] || [] - attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment) + # attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment) + attachments = render_many(attachment_data, StatusView, "object_media.json", as: :media) created_at = Utils.to_masto_date(object.data["published"]) @@ -436,6 +437,59 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do } end + def render("object_media.json", %{media: media}) do + [url] = media["url"] + media_type = media["mediaType"] || "image" + href = MediaProxy.url(url["href"]) + href_preview = MediaProxy.preview_url(url["href"]) + + type = + cond do + String.contains?(media_type, "image") -> "image" + String.contains?(media_type, "video") -> "video" + String.contains?(media_type, "audio") -> "audio" + true -> "unknown" + end + + %{ + id: to_string(media["id"]), + url: href, + remote_url: href, + preview_url: href_preview, + text_url: href, + type: type, + description: media["name"], + pleroma: %{mime_type: media_type}, + blurhash: media["blurhash"] + } + end + + def render("media.json", %{media: media}) do + media_type = media.media_type || media.mime_type || "image" + href = MediaProxy.url(media.href) + href_preview = MediaProxy.preview_url(media.href) + + type = + cond do + String.contains?(media_type, "image") -> "image" + String.contains?(media_type, "video") -> "video" + String.contains?(media_type, "audio") -> "audio" + true -> "unknown" + end + + %{ + id: to_string(media.id), + url: href, + remote_url: href, + preview_url: href_preview, + text_url: href, + type: type, + description: media.name, + pleroma: %{mime_type: media_type}, + blurhash: media.blurhash + } + end + def render("context.json", %{activity: activity, activities: activities, user: user}) do %{ancestors: ancestors, descendants: descendants} = activities |