Merge remote-tracking branch 'origin/develop' into reactions
[akkoma] / lib / pleroma / web / mastodon_api / controllers / media_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11
12 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
13 plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
14
15 @doc "POST /api/v1/media"
16 def create(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
17 with {:ok, object} <-
18 ActivityPub.upload(
19 file,
20 actor: User.ap_id(user),
21 description: Map.get(data, "description")
22 ) do
23 attachment_data = Map.put(object.data, "id", object.id)
24
25 render(conn, "attachment.json", %{attachment: attachment_data})
26 end
27 end
28
29 @doc "PUT /api/v1/media/:id"
30 def update(%{assigns: %{user: user}} = conn, %{"id" => id, "description" => description})
31 when is_binary(description) do
32 with %Object{} = object <- Object.get_by_id(id),
33 true <- Object.authorize_mutation(object, user),
34 {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
35 attachment_data = Map.put(data, "id", object.id)
36
37 render(conn, "attachment.json", %{attachment: attachment_data})
38 end
39 end
40
41 def update(_conn, _data), do: {:error, :bad_request}
42 end