CI: Fix the broken tasks.
[akkoma] / lib / pleroma / web / mastodon_api / controllers / app_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AppController do
6 @moduledoc """
7 Controller for supporting app-related actions.
8 If authentication is an option, app tokens (user-unbound) must be supported.
9 """
10
11 use Pleroma.Web, :controller
12
13 alias Pleroma.Repo
14 alias Pleroma.Web.OAuth.App
15 alias Pleroma.Web.OAuth.Scopes
16 alias Pleroma.Web.OAuth.Token
17
18 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
19
20 plug(:skip_auth when action in [:create, :verify_credentials])
21
22 plug(Pleroma.Web.ApiSpec.CastAndValidate)
23
24 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
25
26 @doc "POST /api/v1/apps"
27 def create(%{body_params: params} = conn, _params) do
28 scopes = Scopes.fetch_scopes(params, ["read"])
29
30 app_attrs =
31 params
32 |> Map.take([:client_name, :redirect_uris, :website])
33 |> Map.put(:scopes, scopes)
34
35 with cs <- App.register_changeset(%App{}, app_attrs),
36 {:ok, app} <- Repo.insert(cs) do
37 render(conn, "show.json", app: app)
38 end
39 end
40
41 @doc """
42 GET /api/v1/apps/verify_credentials
43 Gets compact non-secret representation of the app. Supports app tokens and user tokens.
44 """
45 def verify_credentials(%{assigns: %{token: %Token{} = token}} = conn, _) do
46 with %{app: %App{} = app} <- Repo.preload(token, :app) do
47 render(conn, "compact_non_secret.json", app: app)
48 end
49 end
50 end