mirror of
https://framagit.org/framasoft/mobilizon.git
synced 2024-12-27 10:19:59 +00:00
cc820d6b63
* Data doesn't need anymore to be converted to ActivityStream format to be saved (this was taken from Pleroma and not at all a good idea here) * Everything saved when creating an event is inserted into PostgreSQL in a single transaction
49 lines
1.3 KiB
Elixir
49 lines
1.3 KiB
Elixir
defmodule MobilizonWeb.API.Utils do
|
|
@moduledoc """
|
|
Utils for API.
|
|
"""
|
|
|
|
alias Mobilizon.Config
|
|
alias Mobilizon.Service.Formatter
|
|
|
|
@doc """
|
|
Creates HTML content from text and mentions
|
|
"""
|
|
@spec make_content_html(String.t(), list(), String.t()) :: String.t()
|
|
def make_content_html(
|
|
text,
|
|
additional_tags,
|
|
content_type
|
|
) do
|
|
with {text, mentions, tags} <- format_input(text, content_type, []) do
|
|
{text, mentions, additional_tags ++ Enum.map(tags, fn {_, tag} -> tag end)}
|
|
end
|
|
end
|
|
|
|
def format_input(text, "text/plain", options) do
|
|
text
|
|
|> Formatter.html_escape("text/plain")
|
|
|> Formatter.linkify(options)
|
|
|> (fn {text, mentions, tags} ->
|
|
{String.replace(text, ~r/\r?\n/, "<br>"), mentions, tags}
|
|
end).()
|
|
end
|
|
|
|
def format_input(text, "text/html", options) do
|
|
text
|
|
|> Formatter.html_escape("text/html")
|
|
|> Formatter.linkify(options)
|
|
end
|
|
|
|
def make_report_content_text(nil), do: {:ok, nil}
|
|
|
|
def make_report_content_text(comment) do
|
|
max_size = Config.get([:instance, :max_report_comment_size], 1000)
|
|
|
|
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
|
|
end
|