Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

54 řádky
1.6KB

  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. defmodule Pleroma.Web.MastodonAPI.AppController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Repo
  7. alias Pleroma.Web.OAuth.App
  8. alias Pleroma.Web.OAuth.Scopes
  9. alias Pleroma.Web.OAuth.Token
  10. alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
  11. alias Pleroma.Web.Plugs.OAuthScopesPlug
  12. action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
  13. plug(
  14. :skip_plug,
  15. [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug]
  16. when action == :create
  17. )
  18. plug(OAuthScopesPlug, %{scopes: ["read"]} when action == :verify_credentials)
  19. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  20. @local_mastodon_name "Mastodon-Local"
  21. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
  22. @doc "POST /api/v1/apps"
  23. def create(%{body_params: params} = conn, _params) do
  24. scopes = Scopes.fetch_scopes(params, ["read"])
  25. app_attrs =
  26. params
  27. |> Map.take([:client_name, :redirect_uris, :website])
  28. |> Map.put(:scopes, scopes)
  29. with cs <- App.register_changeset(%App{}, app_attrs),
  30. false <- cs.changes[:client_name] == @local_mastodon_name,
  31. {:ok, app} <- Repo.insert(cs) do
  32. render(conn, "show.json", app: app)
  33. end
  34. end
  35. @doc "GET /api/v1/apps/verify_credentials"
  36. def verify_credentials(%{assigns: %{user: _user, token: token}} = conn, _) do
  37. with %Token{app: %App{} = app} <- Repo.preload(token, :app) do
  38. render(conn, "short.json", app: app)
  39. end
  40. end
  41. end