2019-10-01 10:21:46 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-10-01 10:21:46 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.AppController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2019-10-06 16:12:17 +02:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2019-10-01 10:21:46 +02:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.Web.OAuth.App
|
|
|
|
alias Pleroma.Web.OAuth.Scopes
|
|
|
|
alias Pleroma.Web.OAuth.Token
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
2019-10-06 16:12:17 +02:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read"]} when action == :verify_credentials)
|
|
|
|
|
2019-10-01 10:21:46 +02:00
|
|
|
@local_mastodon_name "Mastodon-Local"
|
|
|
|
|
|
|
|
@doc "POST /api/v1/apps"
|
|
|
|
def create(conn, params) do
|
|
|
|
scopes = Scopes.fetch_scopes(params, ["read"])
|
|
|
|
|
|
|
|
app_attrs =
|
|
|
|
params
|
|
|
|
|> Map.drop(["scope", "scopes"])
|
|
|
|
|> Map.put("scopes", scopes)
|
|
|
|
|
|
|
|
with cs <- App.register_changeset(%App{}, app_attrs),
|
|
|
|
false <- cs.changes[:client_name] == @local_mastodon_name,
|
|
|
|
{:ok, app} <- Repo.insert(cs) do
|
|
|
|
render(conn, "show.json", app: app)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/apps/verify_credentials"
|
|
|
|
def verify_credentials(%{assigns: %{user: _user, token: token}} = conn, _) do
|
|
|
|
with %Token{app: %App{} = app} <- Repo.preload(token, :app) do
|
|
|
|
render(conn, "short.json", app: app)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|