From 2652d9e4edf34531057d472c6e23812873019fd5 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 7 Sep 2017 08:58:10 +0200 Subject: Slight cleanup. --- lib/pleroma/app.ex | 29 ---------------------- .../web/mastodon_api/mastodon_api_controller.ex | 26 +++++++++++-------- lib/pleroma/web/mastodon_api/views/user_view.ex | 27 ++++++++++++++++++++ lib/pleroma/web/oauth/app.ex | 29 ++++++++++++++++++++++ lib/pleroma/web/oauth/authorization.ex | 4 +-- lib/pleroma/web/oauth/oauth_controller.ex | 6 ++--- lib/pleroma/web/oauth/token.ex | 4 +-- lib/pleroma/web/router.ex | 13 +++------- .../web/twitter_api/controllers/util_controller.ex | 12 --------- 9 files changed, 82 insertions(+), 68 deletions(-) delete mode 100644 lib/pleroma/app.ex create mode 100644 lib/pleroma/web/mastodon_api/views/user_view.ex create mode 100644 lib/pleroma/web/oauth/app.ex (limited to 'lib') diff --git a/lib/pleroma/app.ex b/lib/pleroma/app.ex deleted file mode 100644 index d467595ea..000000000 --- a/lib/pleroma/app.ex +++ /dev/null @@ -1,29 +0,0 @@ -defmodule Pleroma.App do - use Ecto.Schema - import Ecto.{Changeset} - - schema "apps" do - field :client_name, :string - field :redirect_uris, :string - field :scopes, :string - field :website, :string - field :client_id, :string - field :client_secret, :string - - timestamps() - end - - def register_changeset(struct, params \\ %{}) do - changeset = struct - |> cast(params, [:client_name, :redirect_uris, :scopes, :website]) - |> validate_required([:client_name, :redirect_uris, :scopes]) - - if changeset.valid? do - changeset - |> put_change(:client_id, :crypto.strong_rand_bytes(32) |> Base.url_encode64) - |> put_change(:client_secret, :crypto.strong_rand_bytes(32) |> Base.url_encode64) - else - changeset - end - end -end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 89e37d6ab..62522439c 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1,6 +1,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do 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 with cs <- App.register_changeset(%App{}, params) |> IO.inspect, @@ -16,17 +19,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def verify_credentials(%{assigns: %{user: user}} = conn, params) do - account = %{ - id: user.id, - username: user.nickname, - acct: user.nickname, - display_name: user.name, - locked: false, - created_at: user.inserted_at, - note: user.bio, - url: "" + account = AccountView.render("account.json", %{user: user}) + json(conn, account) + 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, account) + json(conn, response) end end diff --git a/lib/pleroma/web/mastodon_api/views/user_view.ex b/lib/pleroma/web/mastodon_api/views/user_view.ex new file mode 100644 index 000000000..88e32d6f9 --- /dev/null +++ b/lib/pleroma/web/mastodon_api/views/user_view.ex @@ -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 diff --git a/lib/pleroma/web/oauth/app.ex b/lib/pleroma/web/oauth/app.ex new file mode 100644 index 000000000..ff52ba82e --- /dev/null +++ b/lib/pleroma/web/oauth/app.ex @@ -0,0 +1,29 @@ +defmodule Pleroma.Web.OAuth.App do + use Ecto.Schema + import Ecto.{Changeset} + + schema "apps" do + field :client_name, :string + field :redirect_uris, :string + field :scopes, :string + field :website, :string + field :client_id, :string + field :client_secret, :string + + timestamps() + end + + def register_changeset(struct, params \\ %{}) do + changeset = struct + |> cast(params, [:client_name, :redirect_uris, :scopes, :website]) + |> validate_required([:client_name, :redirect_uris, :scopes]) + + if changeset.valid? do + changeset + |> put_change(:client_id, :crypto.strong_rand_bytes(32) |> Base.url_encode64) + |> put_change(:client_secret, :crypto.strong_rand_bytes(32) |> Base.url_encode64) + else + changeset + end + end +end diff --git a/lib/pleroma/web/oauth/authorization.ex b/lib/pleroma/web/oauth/authorization.ex index 9423c9632..c47289455 100644 --- a/lib/pleroma/web/oauth/authorization.ex +++ b/lib/pleroma/web/oauth/authorization.ex @@ -1,8 +1,8 @@ defmodule Pleroma.Web.OAuth.Authorization do use Ecto.Schema - alias Pleroma.{App, User, Repo} - alias Pleroma.Web.OAuth.Authorization + alias Pleroma.{User, Repo} + alias Pleroma.Web.OAuth.{Authorization, App} schema "oauth_authorizations" do field :token, :string diff --git a/lib/pleroma/web/oauth/oauth_controller.ex b/lib/pleroma/web/oauth/oauth_controller.ex index f0e091ac2..a6a411573 100644 --- a/lib/pleroma/web/oauth/oauth_controller.ex +++ b/lib/pleroma/web/oauth/oauth_controller.ex @@ -1,8 +1,8 @@ defmodule Pleroma.Web.OAuth.OAuthController do use Pleroma.Web, :controller - alias Pleroma.Web.OAuth.{Authorization, Token} - alias Pleroma.{Repo, User, App} + alias Pleroma.Web.OAuth.{Authorization, Token, App} + alias Pleroma.{Repo, User} alias Comeonin.Pbkdf2 def authorize(conn, params) do @@ -17,7 +17,7 @@ defmodule Pleroma.Web.OAuth.OAuthController 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), 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 render conn, "results.html", %{ auth: auth diff --git a/lib/pleroma/web/oauth/token.ex b/lib/pleroma/web/oauth/token.ex index 49e72428c..da723d6d6 100644 --- a/lib/pleroma/web/oauth/token.ex +++ b/lib/pleroma/web/oauth/token.ex @@ -1,8 +1,8 @@ defmodule Pleroma.Web.OAuth.Token do use Ecto.Schema - alias Pleroma.{App, User, Repo} - alias Pleroma.Web.OAuth.Token + alias Pleroma.{User, Repo} + alias Pleroma.Web.OAuth.{Token, App} schema "oauth_tokens" do field :token, :string diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 6081016d6..a8577c30b 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -28,10 +28,6 @@ defmodule Pleroma.Web.Router do plug :accepts, ["json", "xml"] end - pipeline :masto_config do - plug :accepts, ["json"] - end - pipeline :oauth do plug :accepts, ["html", "json"] end @@ -42,11 +38,10 @@ defmodule Pleroma.Web.Router do post "/token", OAuthController, :token_exchange end - scope "/api/v1", Pleroma.Web do - pipe_through :masto_config - # TODO: Move this - get "/instance", TwitterAPI.UtilController, :masto_instance - post "/apps", MastodonAPI.MastodonAPIController, :create_app + scope "/api/v1", Pleroma.Web.MastodonAPI do + pipe_through :api + get "/instance", MastodonAPO.Controller, :masto_instance + post "/apps", MastodonAPIController, :create_app end scope "/api/v1", Pleroma.Web.MastodonAPI do diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 285b4d105..41881e742 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -42,16 +42,4 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do _ -> json(conn, "Pleroma Dev") 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 -- cgit v1.2.3