logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

mascot_controller.ex (1786B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.PleromaAPI.MascotController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.User
  7. alias Pleroma.Web.ActivityPub.ActivityPub
  8. alias Pleroma.Web.Plugs.OAuthScopesPlug
  9. plug(Majic.Plug, [pool: Pleroma.MajicPool] when action in [:update])
  10. plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
  11. plug(OAuthScopesPlug, %{scopes: ["read:accounts"]} when action == :show)
  12. plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action != :show)
  13. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaMascotOperation
  14. @doc "GET /api/v1/pleroma/mascot"
  15. def show(%{assigns: %{user: user}} = conn, _params) do
  16. json(conn, User.get_mascot(user))
  17. end
  18. @doc "PUT /api/v1/pleroma/mascot"
  19. def update(
  20. %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{file: file}}}} =
  21. conn,
  22. _
  23. ) do
  24. with {:content_type, "image" <> _} <- {:content_type, file.content_type},
  25. {_, {:ok, object}} <- {:upload, ActivityPub.upload(file, actor: User.ap_id(user))} do
  26. attachment = render_attachment(object)
  27. {:ok, _user} = User.mascot_update(user, attachment)
  28. json(conn, attachment)
  29. else
  30. {:content_type, _} ->
  31. render_error(conn, :unsupported_media_type, "mascots can only be images")
  32. {:upload, {:error, _}} ->
  33. render_error(conn, :error, "error uploading file")
  34. end
  35. end
  36. defp render_attachment(object) do
  37. attachment_data = Map.put(object.data, "id", object.id)
  38. Pleroma.Web.MastodonAPI.StatusView.render("attachment.json", %{attachment: attachment_data})
  39. end
  40. end