Refactor Mobilizon.Federation.ActivityPub and add typespecs
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -11,19 +11,23 @@ defmodule Mobilizon.Web.Plugs.DetectLocalePlug do
|
||||
import Plug.Conn, only: [get_req_header: 2, assign: 3]
|
||||
alias Mobilizon.Web.Gettext, as: GettextBackend
|
||||
|
||||
@spec init(any()) :: any()
|
||||
def init(_), do: nil
|
||||
|
||||
@spec call(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
|
||||
def call(conn, _) do
|
||||
locale = get_locale_from_header(conn)
|
||||
assign(conn, :detected_locale, locale)
|
||||
end
|
||||
|
||||
@spec get_locale_from_header(Plug.Conn.t()) :: String.t()
|
||||
defp get_locale_from_header(conn) do
|
||||
conn
|
||||
|> extract_accept_language()
|
||||
|> Enum.find(&supported_locale?/1)
|
||||
end
|
||||
|
||||
@spec extract_accept_language(Plug.Conn.t()) :: list(String.t())
|
||||
defp extract_accept_language(conn) do
|
||||
case get_req_header(conn, "accept-language") do
|
||||
[value | _] ->
|
||||
@@ -40,12 +44,14 @@ defmodule Mobilizon.Web.Plugs.DetectLocalePlug do
|
||||
end
|
||||
end
|
||||
|
||||
@spec supported_locale?(String.t()) :: boolean()
|
||||
defp supported_locale?(locale) do
|
||||
GettextBackend
|
||||
|> Gettext.known_locales()
|
||||
|> Enum.member?(locale)
|
||||
end
|
||||
|
||||
@spec parse_language_option(String.t()) :: %{tag: String.t(), quality: float()}
|
||||
defp parse_language_option(string) do
|
||||
captures = Regex.named_captures(~r/^\s?(?<tag>[\w\-]+)(?:;q=(?<quality>[\d\.]+))?$/i, string)
|
||||
|
||||
@@ -58,6 +64,7 @@ defmodule Mobilizon.Web.Plugs.DetectLocalePlug do
|
||||
%{tag: captures["tag"], quality: quality}
|
||||
end
|
||||
|
||||
@spec ensure_language_fallbacks(list(String.t())) :: list(String.t())
|
||||
defp ensure_language_fallbacks(tags) do
|
||||
Enum.flat_map(tags, fn tag ->
|
||||
[language | _] = String.split(tag, "-")
|
||||
|
||||
@@ -10,10 +10,12 @@ defmodule Mobilizon.Web.Plugs.Federating do
|
||||
|
||||
import Plug.Conn
|
||||
|
||||
@spec init(any()) :: any()
|
||||
def init(options) do
|
||||
options
|
||||
end
|
||||
|
||||
@spec call(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
|
||||
def call(conn, _opts) do
|
||||
if Mobilizon.Config.get([:instance, :federating]) do
|
||||
conn
|
||||
|
||||
@@ -13,8 +13,10 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
|
||||
|
||||
require Logger
|
||||
|
||||
@spec init(any()) :: any()
|
||||
def init(opts), do: opts
|
||||
|
||||
@spec call(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
|
||||
def call(conn, options \\ []) do
|
||||
if Config.get([:http_security, :enabled]) do
|
||||
conn
|
||||
|
||||
@@ -18,6 +18,7 @@ defmodule Mobilizon.Web.Plugs.MappedSignatureToIdentity do
|
||||
|
||||
require Logger
|
||||
|
||||
@spec init(any()) :: any()
|
||||
def init(options), do: options
|
||||
|
||||
@spec key_id_from_conn(Plug.Conn.t()) :: String.t() | nil
|
||||
@@ -42,6 +43,7 @@ defmodule Mobilizon.Web.Plugs.MappedSignatureToIdentity do
|
||||
end
|
||||
end
|
||||
|
||||
@spec call(Plug.Conn.t(), any()) :: Plug.Conn.t()
|
||||
def call(%{assigns: %{actor: _}} = conn, _opts), do: conn
|
||||
|
||||
# if this has payload make sure it is signed by the same actor that made it
|
||||
|
||||
@@ -21,6 +21,7 @@ defmodule Mobilizon.Web.Plugs.UploadedMedia do
|
||||
# no slashes
|
||||
@path "media"
|
||||
|
||||
@spec init(any()) :: map()
|
||||
def init(_opts) do
|
||||
static_plug_opts =
|
||||
[]
|
||||
@@ -31,6 +32,7 @@ defmodule Mobilizon.Web.Plugs.UploadedMedia do
|
||||
%{static_plug_opts: static_plug_opts}
|
||||
end
|
||||
|
||||
@spec call(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
||||
def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
|
||||
conn =
|
||||
case fetch_query_params(conn) do
|
||||
|
||||
Reference in New Issue
Block a user