Merge branch 'develop' into global-status-expiration
[akkoma] / lib / pleroma / web / mastodon_api / controllers / app_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AppController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Plugs.OAuthScopesPlug
9 alias Pleroma.Repo
10 alias Pleroma.Web.OAuth.App
11 alias Pleroma.Web.OAuth.Scopes
12 alias Pleroma.Web.OAuth.Token
13
14 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
15
16 plug(OAuthScopesPlug, %{scopes: ["read"]} when action == :verify_credentials)
17 plug(OpenApiSpex.Plug.CastAndValidate)
18
19 @local_mastodon_name "Mastodon-Local"
20
21 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
22
23 @doc "POST /api/v1/apps"
24 def create(%{body_params: params} = conn, _params) do
25 scopes = Scopes.fetch_scopes(params, ["read"])
26
27 app_attrs =
28 params
29 |> Map.take([:client_name, :redirect_uris, :website])
30 |> Map.put(:scopes, scopes)
31
32 with cs <- App.register_changeset(%App{}, app_attrs),
33 false <- cs.changes[:client_name] == @local_mastodon_name,
34 {:ok, app} <- Repo.insert(cs) do
35 render(conn, "show.json", app: app)
36 end
37 end
38
39 @doc "GET /api/v1/apps/verify_credentials"
40 def verify_credentials(%{assigns: %{user: _user, token: token}} = conn, _) do
41 with %Token{app: %App{} = app} <- Repo.preload(token, :app) do
42 render(conn, "short.json", app: app)
43 end
44 end
45 end