From 7ca534740f5b402630f84ed2c6b8acd95a76d2ad Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Tue, 21 Jun 2022 17:23:30 +0100 Subject: [PATCH] keep masto routes, add placeholder index --- config/config.exs | 8 ++- lib/pleroma/web/endpoint.ex | 35 ++++++---- .../controllers/auth_controller.ex | 68 ++++++++++++++++++- .../mastodon_api/views/notification_view.ex | 3 +- lib/pleroma/web/o_auth/o_auth_controller.ex | 1 + lib/pleroma/web/router.ex | 43 +++++------- priv/static/index.html | 6 ++ 7 files changed, 120 insertions(+), 44 deletions(-) create mode 100644 priv/static/index.html diff --git a/config/config.exs b/config/config.exs index 8c742c0f5..b26eaf153 100644 --- a/config/config.exs +++ b/config/config.exs @@ -709,7 +709,7 @@ "192.168.0.0/16" ] -config :pleroma, :static_fe, enabled: false +config :pleroma, :static_fe, enabled: true # Example of frontend configuration # This example will make us serve the primary frontend from the @@ -726,6 +726,10 @@ # available: %{...} config :pleroma, :frontends, + mastodon: %{ + "name" => "mastodon-fe", + "ref" => "develop" + }, available: %{ "pleroma-fe" => %{ "name" => "pleroma-fe", @@ -734,7 +738,7 @@ "ref" => "develop", "build_dir" => "dist" }, - # mastodon-Fe cannot be set as a primary - it can only be enabled via the mastodon configuration above + # mastodon-Fe cannot be set as a primary - this is only here so we can update this seperately "mastodon-fe" => %{ "name" => "mastodon-fe", "git" => "https://akkoma.dev/AkkomaGang/masto-fe", diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index 4d2cd474d..6cf9ee2fb 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -8,7 +8,6 @@ defmodule Pleroma.Web.Endpoint do require Pleroma.Constants alias Pleroma.Config - @mastoFEEnabled Config.get([:frontends, :mastodon, "enabled"]) socket("/socket", Pleroma.Web.UserSocket) socket("/live", Phoenix.LiveView.Socket) @@ -69,6 +68,28 @@ defmodule Pleroma.Web.Endpoint do } ) + plug(Plug.Static.IndexHtml, at: "/pleroma/fedife/") + + plug(Pleroma.Web.Plugs.FrontendStatic, + at: "/pleroma/fedife", + frontend_type: :fedife, + gzip: true, + cache_control_for_etags: @static_cache_control, + headers: %{ + "cache-control" => @static_cache_control + } + ) + + plug(Pleroma.Web.Plugs.FrontendStatic, + at: "/", + frontend_type: :mastodon, + gzip: true, + cache_control_for_etags: @static_cache_control, + headers: %{ + "cache-control" => @static_cache_control + } + ) + # Serve at "/" the static files from "priv/static" directory. # # You should set gzip to true if you are running phoenix.digest @@ -91,18 +112,6 @@ defmodule Pleroma.Web.Endpoint do from: {:pleroma, "priv/static/adminfe/"} ) - if @mastoFEEnabled do - plug(Pleroma.Web.Plugs.FrontendStatic, - at: "/", - frontend_type: :mastodon, - gzip: true, - cache_control_for_etags: @static_cache_control, - headers: %{ - "cache-control" => @static_cache_control - } - ) - end - # Code reloading can be explicitly enabled under the # :code_reloader configuration of your endpoint. if code_reloading? do diff --git a/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex b/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex index aa8014846..4920d65da 100644 --- a/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex @@ -8,16 +8,61 @@ defmodule Pleroma.Web.MastodonAPI.AuthController do import Pleroma.Web.ControllerHelper, only: [json_response: 3] alias Pleroma.Helpers.AuthHelper + alias Pleroma.Helpers.UriHelper + alias Pleroma.User + alias Pleroma.Web.OAuth.App + alias Pleroma.Web.OAuth.Authorization alias Pleroma.Web.OAuth.Token alias Pleroma.Web.OAuth.Token.Strategy.Revoke, as: RevokeToken alias Pleroma.Web.TwitterAPI.TwitterAPI - alias Pleroma.Web.MastodonFE.Controller, as: MastodonFEController action_fallback(Pleroma.Web.MastodonAPI.FallbackController) plug(Pleroma.Web.Plugs.RateLimiter, [name: :password_reset] when action == :password_reset) - use MastodonFEController, :auth_controller_login + @local_mastodon_name "Mastodon-Local" + + @doc "GET /web/login" + # Local Mastodon FE login callback action + def login(conn, %{"code" => auth_token} = params) do + with {:ok, app} <- local_mastofe_app(), + {:ok, auth} <- Authorization.get_by_token(app, auth_token), + {:ok, oauth_token} <- Token.exchange_token(app, auth) do + redirect_to = + conn + |> local_mastodon_post_login_path() + |> UriHelper.modify_uri_params(%{"access_token" => oauth_token.token}) + + conn + |> AuthHelper.put_session_token(oauth_token.token) + |> redirect(to: redirect_to) + else + _ -> redirect_to_oauth_form(conn, params) + end + end + + def login(conn, params) do + with %{assigns: %{user: %User{}, token: %Token{app_id: app_id}}} <- conn, + {:ok, %{id: ^app_id}} <- local_mastofe_app() do + redirect(conn, to: local_mastodon_post_login_path(conn)) + else + _ -> redirect_to_oauth_form(conn, params) + end + end + + defp redirect_to_oauth_form(conn, _params) do + with {:ok, app} <- local_mastofe_app() do + path = + Routes.o_auth_path(conn, :authorize, + response_type: "code", + client_id: app.client_id, + redirect_uri: ".", + scope: Enum.join(app.scopes, " ") + ) + + redirect(conn, to: path) + end + end @doc "DELETE /auth/sign_out" def logout(conn, _) do @@ -42,5 +87,22 @@ def password_reset(conn, params) do json_response(conn, :no_content, "") end - use MastodonFEController, :auth_controller_local_functions + defp local_mastodon_post_login_path(conn) do + case get_session(conn, :return_to) do + nil -> + Routes.masto_fe_path(conn, :index, ["getting-started"]) + + return_to -> + delete_session(conn, :return_to) + return_to + end + end + + @spec local_mastofe_app() :: {:ok, App.t()} | {:error, Ecto.Changeset.t()} + def local_mastofe_app do + App.get_or_make( + %{client_name: @local_mastodon_name, redirect_uris: "."}, + ["read", "write", "follow", "push", "admin"] + ) + end end diff --git a/lib/pleroma/web/mastodon_api/views/notification_view.ex b/lib/pleroma/web/mastodon_api/views/notification_view.ex index ff7adeb2b..bb156799e 100644 --- a/lib/pleroma/web/mastodon_api/views/notification_view.ex +++ b/lib/pleroma/web/mastodon_api/views/notification_view.ex @@ -14,6 +14,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do alias Pleroma.Web.AdminAPI.Report alias Pleroma.Web.AdminAPI.ReportView alias Pleroma.Web.CommonAPI + alias Pleroma.Web.MediaProxy alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.NotificationView alias Pleroma.Web.MastodonAPI.StatusView @@ -140,7 +141,7 @@ defp put_report(response, activity) do defp put_emoji(response, activity) do response |> Map.put(:emoji, activity.data["content"]) - |> Map.put(:emoji_url, Pleroma.Emoji.emoji_url(activity.data)) + |> Map.put(:emoji_url, MediaProxy.url(Pleroma.Emoji.emoji_url(activity.data))) end defp put_chat_message(response, activity, reading_user, opts) do diff --git a/lib/pleroma/web/o_auth/o_auth_controller.ex b/lib/pleroma/web/o_auth/o_auth_controller.ex index f8da3fd4d..247d8399c 100644 --- a/lib/pleroma/web/o_auth/o_auth_controller.ex +++ b/lib/pleroma/web/o_auth/o_auth_controller.ex @@ -597,6 +597,7 @@ def login(%User{} = user, %App{} = app, requested_scopes) when is_list(requested end end + # Special case: Local MastodonFE defp redirect_uri(%Plug.Conn{} = conn, "."), do: Routes.auth_url(conn, :login) defp redirect_uri(%Plug.Conn{}, redirect_uri), do: redirect_uri diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 81eb486ee..7977fa619 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -4,11 +4,8 @@ defmodule Pleroma.Web.Router do use Pleroma.Web, :router - alias Pleroma.Config import Phoenix.LiveDashboard.Router - @mastoFEEnabled Config.get([:frontends, :mastodon, "enabled"]) - pipeline :accepts_html do plug(:accepts, ["html"]) end @@ -581,13 +578,11 @@ defmodule Pleroma.Web.Router do get("/timelines/list/:list_id", TimelineController, :list) end - if @mastoFEEnabled do - scope "/api/web", Pleroma.Web do - pipe_through(:authenticated_api) + scope "/api/web", Pleroma.Web do + pipe_through(:authenticated_api) - # Backend-obscure settings blob for MastoFE, don't parse/reuse elsewhere - put("/settings", MastoFEController, :put_settings) - end + # Backend-obscure settings blob for MastoFE, don't parse/reuse elsewhere + put("/settings", MastoFEController, :put_settings) end scope "/api/v1", Pleroma.Web.MastodonAPI do @@ -796,26 +791,24 @@ defmodule Pleroma.Web.Router do get("/:version", Nodeinfo.NodeinfoController, :nodeinfo) end - if @mastoFEEnabled do - scope "/", Pleroma.Web do - pipe_through(:api) + scope "/", Pleroma.Web do + pipe_through(:api) - get("/manifest.json", ManifestController, :show) - get("/web/manifest.json", MastoFEController, :manifest) - end - - scope "/", Pleroma.Web do - pipe_through(:mastodon_html) - get("/web/login", MastodonAPI.AuthController, :login) - delete("/auth/sign_out", MastodonAPI.AuthController, :logout) - get("/web/*path", MastoFEController, :index) - get("/embed/:id", EmbedController, :show) - end + get("/manifest.json", ManifestController, :show) + get("/web/manifest.json", MastoFEController, :manifest) end scope "/", Pleroma.Web do - pipe_through(:pleroma_html) - post("/auth/password", TwitterAPI.PasswordController, :request) + pipe_through(:mastodon_html) + + get("/web/login", MastodonAPI.AuthController, :login) + delete("/auth/sign_out", MastodonAPI.AuthController, :logout) + + post("/auth/password", MastodonAPI.AuthController, :password_reset) + + get("/web/*path", MastoFEController, :index) + + get("/embed/:id", EmbedController, :show) end scope "/proxy/", Pleroma.Web do diff --git a/priv/static/index.html b/priv/static/index.html new file mode 100644 index 000000000..2376b50f5 --- /dev/null +++ b/priv/static/index.html @@ -0,0 +1,6 @@ + + +

Welcome to Akkoma!

+

If you're seeing this page, your server works!

+

In order to get a frontend to show here, you'll need to set up

:pleroma, :frontends, :primary
and install your frontend of choice

+