Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / pleroma_api / controllers / mascot_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.PleromaAPI.MascotController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.User
9 alias Pleroma.Web.ActivityPub.ActivityPub
10
11 @doc "GET /api/v1/pleroma/mascot"
12 def show(%{assigns: %{user: user}} = conn, _params) do
13 json(conn, User.get_mascot(user))
14 end
15
16 @doc "PUT /api/v1/pleroma/mascot"
17 def update(%{assigns: %{user: user}} = conn, %{"file" => file}) do
18 with {:ok, object} <- ActivityPub.upload(file, actor: User.ap_id(user)),
19 # Reject if not an image
20 %{type: "image"} = attachment <- render_attachment(object) do
21 # Sure!
22 # Save to the user's info
23 {:ok, _user} = User.update_info(user, &User.Info.mascot_update(&1, attachment))
24
25 json(conn, attachment)
26 else
27 %{type: _} -> render_error(conn, :unsupported_media_type, "mascots can only be images")
28 end
29 end
30
31 defp render_attachment(object) do
32 attachment_data = Map.put(object.data, "id", object.id)
33 Pleroma.Web.MastodonAPI.StatusView.render("attachment.json", %{attachment: attachment_data})
34 end
35 end