keep masto routes, add placeholder index

This commit is contained in:
FloatingGhost 2022-06-21 17:23:30 +01:00
parent 04d3661fcf
commit 7ca534740f
7 changed files with 120 additions and 44 deletions

View file

@ -709,7 +709,7 @@
"192.168.0.0/16" "192.168.0.0/16"
] ]
config :pleroma, :static_fe, enabled: false config :pleroma, :static_fe, enabled: true
# Example of frontend configuration # Example of frontend configuration
# This example will make us serve the primary frontend from the # This example will make us serve the primary frontend from the
@ -726,6 +726,10 @@
# available: %{...} # available: %{...}
config :pleroma, :frontends, config :pleroma, :frontends,
mastodon: %{
"name" => "mastodon-fe",
"ref" => "develop"
},
available: %{ available: %{
"pleroma-fe" => %{ "pleroma-fe" => %{
"name" => "pleroma-fe", "name" => "pleroma-fe",
@ -734,7 +738,7 @@
"ref" => "develop", "ref" => "develop",
"build_dir" => "dist" "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" => %{ "mastodon-fe" => %{
"name" => "mastodon-fe", "name" => "mastodon-fe",
"git" => "https://akkoma.dev/AkkomaGang/masto-fe", "git" => "https://akkoma.dev/AkkomaGang/masto-fe",

View file

@ -8,7 +8,6 @@ defmodule Pleroma.Web.Endpoint do
require Pleroma.Constants require Pleroma.Constants
alias Pleroma.Config alias Pleroma.Config
@mastoFEEnabled Config.get([:frontends, :mastodon, "enabled"])
socket("/socket", Pleroma.Web.UserSocket) socket("/socket", Pleroma.Web.UserSocket)
socket("/live", Phoenix.LiveView.Socket) 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. # Serve at "/" the static files from "priv/static" directory.
# #
# You should set gzip to true if you are running phoenix.digest # 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/"} 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 reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint. # :code_reloader configuration of your endpoint.
if code_reloading? do if code_reloading? do

View file

@ -8,16 +8,61 @@ defmodule Pleroma.Web.MastodonAPI.AuthController do
import Pleroma.Web.ControllerHelper, only: [json_response: 3] import Pleroma.Web.ControllerHelper, only: [json_response: 3]
alias Pleroma.Helpers.AuthHelper 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
alias Pleroma.Web.OAuth.Token.Strategy.Revoke, as: RevokeToken alias Pleroma.Web.OAuth.Token.Strategy.Revoke, as: RevokeToken
alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.MastodonFE.Controller, as: MastodonFEController
action_fallback(Pleroma.Web.MastodonAPI.FallbackController) action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
plug(Pleroma.Web.Plugs.RateLimiter, [name: :password_reset] when action == :password_reset) 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" @doc "DELETE /auth/sign_out"
def logout(conn, _) do def logout(conn, _) do
@ -42,5 +87,22 @@ def password_reset(conn, params) do
json_response(conn, :no_content, "") json_response(conn, :no_content, "")
end 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 end

View file

@ -14,6 +14,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
alias Pleroma.Web.AdminAPI.Report alias Pleroma.Web.AdminAPI.Report
alias Pleroma.Web.AdminAPI.ReportView alias Pleroma.Web.AdminAPI.ReportView
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Web.MediaProxy
alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.NotificationView alias Pleroma.Web.MastodonAPI.NotificationView
alias Pleroma.Web.MastodonAPI.StatusView alias Pleroma.Web.MastodonAPI.StatusView
@ -140,7 +141,7 @@ defp put_report(response, activity) do
defp put_emoji(response, activity) do defp put_emoji(response, activity) do
response response
|> Map.put(:emoji, activity.data["content"]) |> 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 end
defp put_chat_message(response, activity, reading_user, opts) do defp put_chat_message(response, activity, reading_user, opts) do

View file

@ -597,6 +597,7 @@ def login(%User{} = user, %App{} = app, requested_scopes) when is_list(requested
end end
end end
# Special case: Local MastodonFE
defp redirect_uri(%Plug.Conn{} = conn, "."), do: Routes.auth_url(conn, :login) defp redirect_uri(%Plug.Conn{} = conn, "."), do: Routes.auth_url(conn, :login)
defp redirect_uri(%Plug.Conn{}, redirect_uri), do: redirect_uri defp redirect_uri(%Plug.Conn{}, redirect_uri), do: redirect_uri

View file

@ -4,11 +4,8 @@
defmodule Pleroma.Web.Router do defmodule Pleroma.Web.Router do
use Pleroma.Web, :router use Pleroma.Web, :router
alias Pleroma.Config
import Phoenix.LiveDashboard.Router import Phoenix.LiveDashboard.Router
@mastoFEEnabled Config.get([:frontends, :mastodon, "enabled"])
pipeline :accepts_html do pipeline :accepts_html do
plug(:accepts, ["html"]) plug(:accepts, ["html"])
end end
@ -581,13 +578,11 @@ defmodule Pleroma.Web.Router do
get("/timelines/list/:list_id", TimelineController, :list) get("/timelines/list/:list_id", TimelineController, :list)
end end
if @mastoFEEnabled do scope "/api/web", Pleroma.Web do
scope "/api/web", Pleroma.Web do pipe_through(:authenticated_api)
pipe_through(:authenticated_api)
# Backend-obscure settings blob for MastoFE, don't parse/reuse elsewhere # Backend-obscure settings blob for MastoFE, don't parse/reuse elsewhere
put("/settings", MastoFEController, :put_settings) put("/settings", MastoFEController, :put_settings)
end
end end
scope "/api/v1", Pleroma.Web.MastodonAPI do scope "/api/v1", Pleroma.Web.MastodonAPI do
@ -796,26 +791,24 @@ defmodule Pleroma.Web.Router do
get("/:version", Nodeinfo.NodeinfoController, :nodeinfo) get("/:version", Nodeinfo.NodeinfoController, :nodeinfo)
end end
if @mastoFEEnabled do scope "/", Pleroma.Web do
scope "/", Pleroma.Web do pipe_through(:api)
pipe_through(:api)
get("/manifest.json", ManifestController, :show) get("/manifest.json", ManifestController, :show)
get("/web/manifest.json", MastoFEController, :manifest) 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
end end
scope "/", Pleroma.Web do scope "/", Pleroma.Web do
pipe_through(:pleroma_html) pipe_through(:mastodon_html)
post("/auth/password", TwitterAPI.PasswordController, :request)
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 end
scope "/proxy/", Pleroma.Web do scope "/proxy/", Pleroma.Web do

6
priv/static/index.html Normal file
View file

@ -0,0 +1,6 @@
<html>
<body>
<h3>Welcome to Akkoma!</h3>
<p>If you're seeing this page, your server works!</p>
<p>In order to get a frontend to show here, you'll need to set up <pre>:pleroma, :frontends, :primary</pre> and install your frontend of choice</p>
</html>