Refactor Mobilizon.Federation.ActivityPub and add typespecs

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-09-28 19:40:37 +02:00
parent 41f086e2c9
commit b5d9b82bdd
125 changed files with 2497 additions and 1673 deletions

View File

@@ -65,6 +65,7 @@ defmodule Mobilizon.Service.Formatter do
end
end
@spec hashtag_handler(String.t(), String.t(), any(), map()) :: {String.t(), map()}
def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
tag = String.downcase(tag)
url = "#{Endpoint.url()}/tag/#{tag}"
@@ -100,6 +101,7 @@ defmodule Mobilizon.Service.Formatter do
@doc """
Escapes a special characters in mention names.
"""
@spec mentions_escape(String.t(), Keyword.t()) :: String.t()
def mentions_escape(text, options \\ []) do
options =
Keyword.merge(options,
@@ -111,6 +113,11 @@ defmodule Mobilizon.Service.Formatter do
Linkify.link(text, options)
end
@spec html_escape(
{text :: String.t(), mentions :: list(), hashtags :: list()},
type :: String.t()
) :: {String.t(), list(), list()}
@spec html_escape(text :: String.t(), type :: String.t()) :: String.t()
def html_escape({text, mentions, hashtags}, type) do
{html_escape(text, type), mentions, hashtags}
end
@@ -131,6 +138,7 @@ defmodule Mobilizon.Service.Formatter do
|> Enum.join("")
end
@spec truncate(String.t(), non_neg_integer(), String.t()) :: String.t()
def truncate(text, max_length \\ 200, omission \\ "...") do
# Remove trailing whitespace
text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
@@ -143,6 +151,7 @@ defmodule Mobilizon.Service.Formatter do
end
end
@spec linkify_opts :: Keyword.t()
defp linkify_opts do
Mobilizon.Config.get(__MODULE__) ++
[
@@ -186,5 +195,6 @@ defmodule Mobilizon.Service.Formatter do
|> (&" #{&1}").()
end
@spec tag_text_strip(String.t()) :: String.t()
defp tag_text_strip(tag), do: tag |> String.trim("#") |> String.downcase()
end