diff --git a/lib/web/auth/context.ex b/lib/web/auth/context.ex index 86ac41b3..48bbe295 100644 --- a/lib/web/auth/context.ex +++ b/lib/web/auth/context.ex @@ -26,22 +26,6 @@ defmodule Mobilizon.Web.Auth.Context do user_agent = conn |> Plug.Conn.get_req_header("user-agent") |> List.first() - if Application.get_env(:sentry, :dsn) != nil do - Sentry.Context.set_request_context(%{ - url: Plug.Conn.request_url(conn), - method: conn.method, - headers: %{ - "User-Agent": user_agent, - Referer: conn |> Plug.Conn.get_req_header("referer") |> List.first() - }, - query_string: conn.query_string, - env: %{ - REQUEST_ID: conn |> Plug.Conn.get_resp_header("x-request-id") |> List.first(), - SERVER_NAME: conn.host - } - }) - end - {conn, context} = case Guardian.Plug.current_resource(conn) do %User{id: user_id, email: user_email} = user -> diff --git a/lib/web/request_context.ex b/lib/web/request_context.ex new file mode 100644 index 00000000..b41acb46 --- /dev/null +++ b/lib/web/request_context.ex @@ -0,0 +1,26 @@ +defmodule Mobilizon.Web.RequestContext do + @moduledoc """ + Module to put some context into the request + """ + + @spec put_request_context(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t() + def put_request_context(%Plug.Conn{} = conn, _opts \\ []) do + if Application.get_env(:sentry, :dsn) != nil do + Sentry.Context.set_request_context(%{ + url: Plug.Conn.request_url(conn), + method: conn.method, + headers: %{ + "User-Agent": conn |> Plug.Conn.get_req_header("user-agent") |> List.first(), + Referer: conn |> Plug.Conn.get_req_header("referer") |> List.first() + }, + query_string: conn.query_string, + env: %{ + REQUEST_ID: conn |> Plug.Conn.get_resp_header("x-request-id") |> List.first(), + SERVER_NAME: conn.host + } + }) + end + + conn + end +end diff --git a/lib/web/router.ex b/lib/web/router.ex index 5875b889..de9a18a5 100644 --- a/lib/web/router.ex +++ b/lib/web/router.ex @@ -3,9 +3,11 @@ defmodule Mobilizon.Web.Router do Router for mobilizon app """ use Mobilizon.Web, :router + import Mobilizon.Web.RequestContext pipeline :graphql do # plug(:accepts, ["json"]) + plug(:put_request_context) plug(Mobilizon.Web.Auth.Pipeline) plug(Mobilizon.Web.Plugs.SetLocalePlug) end @@ -22,29 +24,35 @@ defmodule Mobilizon.Web.Router do end pipeline :host_meta do + plug(:put_request_context) plug(:accepts, ["xrd-xml"]) end pipeline :well_known do + plug(:put_request_context) plug(:accepts, ["json", "jrd-json"]) end pipeline :activity_pub_signature do + plug(:put_request_context) plug(Mobilizon.Web.Plugs.HTTPSignatures) plug(Mobilizon.Web.Plugs.MappedSignatureToIdentity) end pipeline :relay do + plug(:put_request_context) plug(Mobilizon.Web.Plugs.HTTPSignatures) plug(Mobilizon.Web.Plugs.MappedSignatureToIdentity) plug(:accepts, ["activity-json", "json"]) end pipeline :activity_pub do + plug(:put_request_context) plug(:accepts, ["activity-json"]) end pipeline :activity_pub_and_html do + plug(:put_request_context) plug(:accepts, ["html", "activity-json"]) plug(:put_secure_browser_headers) @@ -57,14 +65,13 @@ defmodule Mobilizon.Web.Router do end pipeline :atom_and_ical do + plug(:put_request_context) plug(:put_secure_browser_headers) plug(:accepts, ["atom", "ics", "html", "xml"]) end - pipeline :exports do - end - pipeline :browser do + plug(:put_request_context) plug(Plug.Static, at: "/", from: "priv/static") plug(Mobilizon.Web.Plugs.SetLocalePlug) @@ -78,9 +85,6 @@ defmodule Mobilizon.Web.Router do plug(:put_secure_browser_headers) end - pipeline :remote_media do - end - scope "/exports", Mobilizon.Web do pipe_through(:browser) get("/:format/:file", ExportController, :export)