Merge remote-tracking branch 'remotes/origin/develop' into 1560-non-federating-instan...
[akkoma] / lib / pleroma / web / mastodon_api / controllers / media_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MediaController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Object
9 alias Pleroma.Plugs.OAuthScopesPlug
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12
13 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
14 plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
15
16 plug(OAuthScopesPlug, %{scopes: ["write:media"]})
17
18 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
19
20 @doc "POST /api/v1/media"
21 def create(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
22 with {:ok, object} <-
23 ActivityPub.upload(
24 file,
25 actor: User.ap_id(user),
26 description: Map.get(data, "description")
27 ) do
28 attachment_data = Map.put(object.data, "id", object.id)
29
30 render(conn, "attachment.json", %{attachment: attachment_data})
31 end
32 end
33
34 @doc "PUT /api/v1/media/:id"
35 def update(%{assigns: %{user: user}} = conn, %{"id" => id, "description" => description})
36 when is_binary(description) do
37 with %Object{} = object <- Object.get_by_id(id),
38 true <- Object.authorize_mutation(object, user),
39 {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
40 attachment_data = Map.put(data, "id", object.id)
41
42 render(conn, "attachment.json", %{attachment: attachment_data})
43 end
44 end
45
46 def update(_conn, _data), do: {:error, :bad_request}
47 end