Merge remote-tracking branch 'origin/develop' into global-status-expiration
[akkoma] / lib / pleroma / web / pleroma_api / controllers / mascot_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.PleromaAPI.MascotController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Plugs.OAuthScopesPlug
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11
12 plug(OAuthScopesPlug, %{scopes: ["read:accounts"]} when action == :show)
13 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action != :show)
14
15 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
16
17 @doc "GET /api/v1/pleroma/mascot"
18 def show(%{assigns: %{user: user}} = conn, _params) do
19 json(conn, User.get_mascot(user))
20 end
21
22 @doc "PUT /api/v1/pleroma/mascot"
23 def update(%{assigns: %{user: user}} = conn, %{"file" => file}) do
24 with {:ok, object} <- ActivityPub.upload(file, actor: User.ap_id(user)),
25 # Reject if not an image
26 %{type: "image"} = attachment <- render_attachment(object) do
27 {:ok, _user} = User.mascot_update(user, attachment)
28
29 json(conn, attachment)
30 else
31 %{type: _} -> render_error(conn, :unsupported_media_type, "mascots can only be images")
32 end
33 end
34
35 defp render_attachment(object) do
36 attachment_data = Map.put(object.data, "id", object.id)
37 Pleroma.Web.MastodonAPI.StatusView.render("attachment.json", %{attachment: attachment_data})
38 end
39 end