1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.MascotController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Plugs.OAuthScopesPlug
10 alias Pleroma.Web.ActivityPub.ActivityPub
12 plug(OAuthScopesPlug, %{scopes: ["read:accounts"]} when action == :show)
13 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action != :show)
15 @doc "GET /api/v1/pleroma/mascot"
16 def show(%{assigns: %{user: user}} = conn, _params) do
17 json(conn, User.get_mascot(user))
20 @doc "PUT /api/v1/pleroma/mascot"
21 def update(%{assigns: %{user: user}} = conn, %{"file" => file}) do
22 with {:ok, object} <- ActivityPub.upload(file, actor: User.ap_id(user)),
23 # Reject if not an image
24 %{type: "image"} = attachment <- render_attachment(object) do
25 {:ok, _user} = User.mascot_update(user, attachment)
27 json(conn, attachment)
29 %{type: _} -> render_error(conn, :unsupported_media_type, "mascots can only be images")
33 defp render_attachment(object) do
34 attachment_data = Map.put(object.data, "id", object.id)
35 Pleroma.Web.MastodonAPI.StatusView.render("attachment.json", %{attachment: attachment_data})