1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.MediaController do
6 use Pleroma.Web, :controller
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.Plugs.OAuthScopesPlug
13 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
14 plug(Majic.Plug, [pool: Pleroma.MajicPool] when action in [:create, :create2])
15 plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
17 plug(OAuthScopesPlug, %{scopes: ["read:media"]} when action == :show)
18 plug(OAuthScopesPlug, %{scopes: ["write:media"]} when action != :show)
20 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MediaOperation
22 @doc "POST /api/v1/media"
24 %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{file: file} = data}}} =
31 actor: User.ap_id(user),
32 description: Map.get(data, :description)
34 attachment_data = Map.put(object.data, "id", object.id)
36 render(conn, "attachment.json", %{attachment: attachment_data})
40 def create(_conn, _data), do: {:error, :bad_request}
42 @doc "POST /api/v2/media"
44 %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{file: file} = data}}} =
51 actor: User.ap_id(user),
52 description: Map.get(data, :description)
54 attachment_data = Map.put(object.data, "id", object.id)
58 |> render("attachment.json", %{attachment: attachment_data})
62 def create2(_conn, _data), do: {:error, :bad_request}
64 @doc "PUT /api/v1/media/:id"
67 assigns: %{user: user},
69 open_api_spex: %{body_params: %{description: description}, params: %{id: id}}
74 with %Object{} = object <- Object.get_by_id(id),
75 :ok <- Object.authorize_access(object, user),
76 {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
77 attachment_data = Map.put(data, "id", object.id)
79 render(conn, "attachment.json", %{attachment: attachment_data})
83 def update(conn, data), do: show(conn, data)
85 @doc "GET /api/v1/media/:id"
86 def show(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
87 with %Object{data: data, id: object_id} = object <- Object.get_by_id(id),
88 :ok <- Object.authorize_access(object, user) do
89 attachment_data = Map.put(data, "id", object_id)
91 render(conn, "attachment.json", %{attachment: attachment_data})
95 def show(_conn, _data), do: {:error, :bad_request}