Merge branch 'develop' into 'remove-twitter-api'
[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 @doc "POST /api/v1/media"
19 def create(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
20 with {:ok, object} <-
21 ActivityPub.upload(
22 file,
23 actor: User.ap_id(user),
24 description: Map.get(data, "description")
25 ) do
26 attachment_data = Map.put(object.data, "id", object.id)
27
28 render(conn, "attachment.json", %{attachment: attachment_data})
29 end
30 end
31
32 @doc "PUT /api/v1/media/:id"
33 def update(%{assigns: %{user: user}} = conn, %{"id" => id, "description" => description})
34 when is_binary(description) do
35 with %Object{} = object <- Object.get_by_id(id),
36 true <- Object.authorize_mutation(object, user),
37 {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
38 attachment_data = Map.put(data, "id", object.id)
39
40 render(conn, "attachment.json", %{attachment: attachment_data})
41 end
42 end
43
44 def update(_conn, _data), do: {:error, :bad_request}
45 end