spc-pleroma/lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex

46 lines
1.7 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.PleromaAPI.MascotController do
use Pleroma.Web, :controller
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
2020-06-24 10:07:47 +00:00
alias Pleroma.Web.Plugs.OAuthScopesPlug
plug(Majic.Plug, [pool: Pleroma.MajicPool] when action in [:update])
2020-05-18 18:00:32 +00:00
plug(Pleroma.Web.ApiSpec.CastAndValidate)
plug(OAuthScopesPlug, %{scopes: ["read:accounts"]} when action == :show)
plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action != :show)
2020-05-18 18:00:32 +00:00
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaMascotOperation
@doc "GET /api/v1/pleroma/mascot"
def show(%{assigns: %{user: user}} = conn, _params) do
json(conn, User.get_mascot(user))
end
@doc "PUT /api/v1/pleroma/mascot"
Pleroma.Web.PleromaAPI.MascotController: dialyzer errors lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex:25:no_return Function update/2 has no local return. lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex:27:call The function call will not succeed. Pleroma.Web.ActivityPub.ActivityPub.upload(_file :: atom() | %{:content_type => _, _ => _}, [{:actor, <<_::56, _::size(8)>>}, ...]) :: :ok def a() do :ok end will never return since the 2nd arguments differ from the success typing arguments: (any(), [ {:activity_type | :description | :filters | :size_limit | :type | :uploader, atom() | binary() | [atom()] | non_neg_integer()} ]) lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex:31:call The function call will not succeed. Phoenix.Controller.json( _conn :: %{ :assigns => %{:user => _, _ => _}, :body_params => %{:file => _, _ => _}, _ => _ }, _attachment :: any() ) breaks the contract (Plug.Conn.t(), term()) :: Plug.Conn.t() lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex:34:call The function call will not succeed. Plug.Conn.put_status( _conn :: %{ :assigns => %{:user => _, _ => _}, :body_params => %{:file => _, _ => _}, _ => _ }, :unsupported_media_type ) breaks the contract (t(), status()) :: t() lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex:34:call The function call will not succeed. Plug.Conn.put_status( _conn :: %{ :assigns => %{:user => _, _ => _}, :body_params => %{:file => _, _ => _}, _ => _ }, :unsupported_media_type ) breaks the contract (t(), status()) :: t() lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex:38:unused_fun Function render_attachment/1 will never be called.
2024-01-28 17:29:12 +00:00
def update(%{assigns: %{user: user}, body_params: %{"file" => file}} = conn, _) do
with {_, "image" <> _} <- {:content_type, file.content_type},
{_, {:ok, object}} <- {:upload, ActivityPub.upload(file, actor: User.ap_id(user))} do
attachment = render_attachment(object)
{:ok, _user} = User.mascot_update(user, attachment)
json(conn, attachment)
else
{:content_type, _} ->
render_error(conn, :unsupported_media_type, "mascots can only be images")
{:upload, {:error, _}} ->
render_error(conn, :error, "error uploading file")
end
end
defp render_attachment(object) do
attachment_data = Map.put(object.data, "id", object.id)
Pleroma.Web.MastodonAPI.StatusView.render("attachment.json", %{attachment: attachment_data})
end
end