Slight cleanup.
This commit is contained in:
parent
2a298d70f9
commit
2652d9e4ed
|
@ -1,6 +1,9 @@
|
||||||
defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
use Pleroma.Web, :controller
|
use Pleroma.Web, :controller
|
||||||
alias Pleroma.{Repo, App}
|
alias Pleroma.{Repo}
|
||||||
|
alias Pleroma.Web.OAuth.App
|
||||||
|
alias Pleroma.Web
|
||||||
|
alias Pleroma.Web.MastodonAPI.AccountView
|
||||||
|
|
||||||
def create_app(conn, params) do
|
def create_app(conn, params) do
|
||||||
with cs <- App.register_changeset(%App{}, params) |> IO.inspect,
|
with cs <- App.register_changeset(%App{}, params) |> IO.inspect,
|
||||||
|
@ -16,17 +19,18 @@ def create_app(conn, params) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_credentials(%{assigns: %{user: user}} = conn, params) do
|
def verify_credentials(%{assigns: %{user: user}} = conn, params) do
|
||||||
account = %{
|
account = AccountView.render("account.json", %{user: user})
|
||||||
id: user.id,
|
|
||||||
username: user.nickname,
|
|
||||||
acct: user.nickname,
|
|
||||||
display_name: user.name,
|
|
||||||
locked: false,
|
|
||||||
created_at: user.inserted_at,
|
|
||||||
note: user.bio,
|
|
||||||
url: ""
|
|
||||||
}
|
|
||||||
|
|
||||||
json(conn, account)
|
json(conn, account)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def masto_instance(conn, _params) do
|
||||||
|
response = %{
|
||||||
|
uri: Web.base_url,
|
||||||
|
title: Web.base_url,
|
||||||
|
description: "A Pleroma instance, an alternative fediverse server",
|
||||||
|
version: "Pleroma Dev"
|
||||||
|
}
|
||||||
|
|
||||||
|
json(conn, response)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
|
use Pleroma.Web, :view
|
||||||
|
alias Pleroma.User
|
||||||
|
|
||||||
|
def render("account.json", %{user: user}) do
|
||||||
|
image = User.avatar_url(user)
|
||||||
|
user_info = User.user_info(user)
|
||||||
|
|
||||||
|
%{
|
||||||
|
id: user.id,
|
||||||
|
username: user.nickname,
|
||||||
|
acct: user.nickname,
|
||||||
|
display_name: user.name,
|
||||||
|
locked: false,
|
||||||
|
created_at: user.inserted_at,
|
||||||
|
followers_count: user_info.follower_count,
|
||||||
|
following_count: user_info.following_count,
|
||||||
|
statuses_count: user_info.note_count,
|
||||||
|
note: user.bio,
|
||||||
|
url: user.ap_id,
|
||||||
|
avatar: image,
|
||||||
|
avatar_static: image,
|
||||||
|
header: "",
|
||||||
|
header_static: ""
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,4 +1,4 @@
|
||||||
defmodule Pleroma.App do
|
defmodule Pleroma.Web.OAuth.App do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.{Changeset}
|
import Ecto.{Changeset}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
defmodule Pleroma.Web.OAuth.Authorization do
|
defmodule Pleroma.Web.OAuth.Authorization do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
|
||||||
alias Pleroma.{App, User, Repo}
|
alias Pleroma.{User, Repo}
|
||||||
alias Pleroma.Web.OAuth.Authorization
|
alias Pleroma.Web.OAuth.{Authorization, App}
|
||||||
|
|
||||||
schema "oauth_authorizations" do
|
schema "oauth_authorizations" do
|
||||||
field :token, :string
|
field :token, :string
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
defmodule Pleroma.Web.OAuth.OAuthController do
|
defmodule Pleroma.Web.OAuth.OAuthController do
|
||||||
use Pleroma.Web, :controller
|
use Pleroma.Web, :controller
|
||||||
|
|
||||||
alias Pleroma.Web.OAuth.{Authorization, Token}
|
alias Pleroma.Web.OAuth.{Authorization, Token, App}
|
||||||
alias Pleroma.{Repo, User, App}
|
alias Pleroma.{Repo, User}
|
||||||
alias Comeonin.Pbkdf2
|
alias Comeonin.Pbkdf2
|
||||||
|
|
||||||
def authorize(conn, params) do
|
def authorize(conn, params) do
|
||||||
|
@ -17,7 +17,7 @@ def authorize(conn, params) do
|
||||||
def create_authorization(conn, %{"authorization" => %{"name" => name, "password" => password, "client_id" => client_id}} = params) do
|
def create_authorization(conn, %{"authorization" => %{"name" => name, "password" => password, "client_id" => client_id}} = params) do
|
||||||
with %User{} = user <- User.get_cached_by_nickname(name),
|
with %User{} = user <- User.get_cached_by_nickname(name),
|
||||||
true <- Pbkdf2.checkpw(password, user.password_hash),
|
true <- Pbkdf2.checkpw(password, user.password_hash),
|
||||||
%App{} = app <- Pleroma.Repo.get_by(Pleroma.App, client_id: client_id),
|
%App{} = app <- Repo.get_by(App, client_id: client_id),
|
||||||
{:ok, auth} <- Authorization.create_authorization(app, user) do
|
{:ok, auth} <- Authorization.create_authorization(app, user) do
|
||||||
render conn, "results.html", %{
|
render conn, "results.html", %{
|
||||||
auth: auth
|
auth: auth
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
defmodule Pleroma.Web.OAuth.Token do
|
defmodule Pleroma.Web.OAuth.Token do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
|
||||||
alias Pleroma.{App, User, Repo}
|
alias Pleroma.{User, Repo}
|
||||||
alias Pleroma.Web.OAuth.Token
|
alias Pleroma.Web.OAuth.{Token, App}
|
||||||
|
|
||||||
schema "oauth_tokens" do
|
schema "oauth_tokens" do
|
||||||
field :token, :string
|
field :token, :string
|
||||||
|
|
|
@ -28,10 +28,6 @@ def user_fetcher(username) do
|
||||||
plug :accepts, ["json", "xml"]
|
plug :accepts, ["json", "xml"]
|
||||||
end
|
end
|
||||||
|
|
||||||
pipeline :masto_config do
|
|
||||||
plug :accepts, ["json"]
|
|
||||||
end
|
|
||||||
|
|
||||||
pipeline :oauth do
|
pipeline :oauth do
|
||||||
plug :accepts, ["html", "json"]
|
plug :accepts, ["html", "json"]
|
||||||
end
|
end
|
||||||
|
@ -42,11 +38,10 @@ def user_fetcher(username) do
|
||||||
post "/token", OAuthController, :token_exchange
|
post "/token", OAuthController, :token_exchange
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api/v1", Pleroma.Web do
|
scope "/api/v1", Pleroma.Web.MastodonAPI do
|
||||||
pipe_through :masto_config
|
pipe_through :api
|
||||||
# TODO: Move this
|
get "/instance", MastodonAPO.Controller, :masto_instance
|
||||||
get "/instance", TwitterAPI.UtilController, :masto_instance
|
post "/apps", MastodonAPIController, :create_app
|
||||||
post "/apps", MastodonAPI.MastodonAPIController, :create_app
|
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api/v1", Pleroma.Web.MastodonAPI do
|
scope "/api/v1", Pleroma.Web.MastodonAPI do
|
||||||
|
|
|
@ -42,16 +42,4 @@ def version(conn, _params) do
|
||||||
_ -> json(conn, "Pleroma Dev")
|
_ -> json(conn, "Pleroma Dev")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Move this
|
|
||||||
def masto_instance(conn, _params) do
|
|
||||||
response = %{
|
|
||||||
uri: Web.base_url,
|
|
||||||
title: Web.base_url,
|
|
||||||
description: "A Pleroma instance, an alternative fediverse server",
|
|
||||||
version: "dev"
|
|
||||||
}
|
|
||||||
|
|
||||||
json(conn, response)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue