2020-01-26 20:11:16 +00:00
|
|
|
defmodule Mobilizon.GraphQL.API.Utils do
|
2018-12-14 16:41:55 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Utils for API.
|
2018-12-14 16:41:55 +00:00
|
|
|
"""
|
2019-09-07 22:05:54 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Config
|
2018-12-14 16:41:55 +00:00
|
|
|
alias Mobilizon.Service.Formatter
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates HTML content from text and mentions
|
|
|
|
"""
|
2019-07-26 09:30:28 +00:00
|
|
|
@spec make_content_html(String.t(), list(), String.t()) :: String.t()
|
2020-01-26 20:11:16 +00:00
|
|
|
def make_content_html(text, additional_tags, content_type) do
|
2019-07-26 09:30:28 +00:00
|
|
|
with {text, mentions, tags} <- format_input(text, content_type, []) do
|
|
|
|
{text, mentions, additional_tags ++ Enum.map(tags, fn {_, tag} -> tag end)}
|
|
|
|
end
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
def format_input(text, "text/plain", options) do
|
2018-12-14 16:41:55 +00:00
|
|
|
text
|
|
|
|
|> Formatter.html_escape("text/plain")
|
2019-07-26 09:30:28 +00:00
|
|
|
|> Formatter.linkify(options)
|
2020-01-26 20:11:16 +00:00
|
|
|
|> (fn {text, mentions, tags} -> {String.replace(text, ~r/\r?\n/, "<br>"), mentions, tags} end).()
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
def format_input(text, "text/html", options) do
|
2018-12-14 16:41:55 +00:00
|
|
|
text
|
|
|
|
|> Formatter.html_escape("text/html")
|
2019-07-26 09:30:28 +00:00
|
|
|
|> Formatter.linkify(options)
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
|
2019-09-09 07:31:08 +00:00
|
|
|
def make_report_content_text(nil), do: {:ok, nil}
|
2019-07-23 11:49:22 +00:00
|
|
|
|
2019-09-09 07:31:08 +00:00
|
|
|
def make_report_content_text(comment) do
|
2019-09-07 22:05:54 +00:00
|
|
|
max_size = Config.get([:instance, :max_report_comment_size], 1000)
|
2019-07-23 11:49:22 +00:00
|
|
|
|
|
|
|
if String.length(comment) <= max_size do
|
|
|
|
{:ok, Formatter.html_escape(comment, "text/plain")}
|
|
|
|
else
|
|
|
|
{:error, "Comment must be up to #{max_size} characters"}
|
|
|
|
end
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|