2019-09-30 12:32:43 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 12:32:43 +00:00
|
|
|
# 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
|
2019-09-30 12:32:43 +00:00
|
|
|
|
2020-05-18 18:00:32 +00:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2019-10-02 17:42:40 +00:00
|
|
|
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
|
|
|
|
|
2019-09-30 12:32:43 +00:00
|
|
|
@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"
|
2020-05-18 18:00:32 +00:00
|
|
|
def update(%{assigns: %{user: user}, body_params: %{file: file}} = conn, _) do
|
2019-09-30 12:32:43 +00:00
|
|
|
with {:ok, object} <- ActivityPub.upload(file, actor: User.ap_id(user)),
|
|
|
|
# Reject if not an image
|
|
|
|
%{type: "image"} = attachment <- render_attachment(object) do
|
2019-10-16 18:59:21 +00:00
|
|
|
{:ok, _user} = User.mascot_update(user, attachment)
|
2019-09-30 12:32:43 +00:00
|
|
|
|
|
|
|
json(conn, attachment)
|
|
|
|
else
|
|
|
|
%{type: _} -> render_error(conn, :unsupported_media_type, "mascots can only be images")
|
|
|
|
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
|