1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.Plugs.OAuthScopesPlug
12 plug(Majic.Plug, [pool: Pleroma.MajicPool] when action in [:update])
13 plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
14 plug(OAuthScopesPlug, %{scopes: ["read:accounts"]} when action == :show)
15 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action != :show)
17 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaMascotOperation
19 @doc "GET /api/v1/pleroma/mascot"
20 def show(%{assigns: %{user: user}} = conn, _params) do
21 json(conn, User.get_mascot(user))
24 @doc "PUT /api/v1/pleroma/mascot"
26 %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{file: file}}}} =
30 with {:content_type, "image" <> _} <- {:content_type, file.content_type},
31 {_, {:ok, object}} <- {:upload, ActivityPub.upload(file, actor: User.ap_id(user))} do
32 attachment = render_attachment(object)
33 {:ok, _user} = User.mascot_update(user, attachment)
35 json(conn, attachment)
38 render_error(conn, :unsupported_media_type, "mascots can only be images")
40 {:upload, {:error, _}} ->
41 render_error(conn, :error, "error uploading file")
45 defp render_attachment(object) do
46 attachment_data = Map.put(object.data, "id", object.id)
47 Pleroma.Web.MastodonAPI.StatusView.render("attachment.json", %{attachment: attachment_data})