2018-12-14 17:41:55 +01:00
|
|
|
defmodule MobilizonWeb.API.Utils do
|
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Utils for API.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-08 00:05:54 +02:00
|
|
|
alias Mobilizon.Config
|
2018-12-14 17:41:55 +01:00
|
|
|
alias Mobilizon.Service.Formatter
|
|
|
|
|
2019-09-20 19:43:29 +02:00
|
|
|
@ap_public "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Determines the full audience based on mentions for a public audience
|
|
|
|
|
|
|
|
Audience is:
|
2019-07-30 16:40:59 +02:00
|
|
|
* `to` : the mentioned actors, the eventual actor we're replying to and the public
|
2018-12-14 17:41:55 +01:00
|
|
|
* `cc` : the actor's followers
|
|
|
|
"""
|
2019-07-26 11:30:28 +02:00
|
|
|
@spec get_to_and_cc(Actor.t(), list(), map(), String.t()) :: {list(), list()}
|
|
|
|
def get_to_and_cc(%Actor{} = actor, mentions, inReplyTo, :public) do
|
2019-09-20 19:43:29 +02:00
|
|
|
to = [@ap_public | mentions]
|
2018-12-14 17:41:55 +01:00
|
|
|
cc = [actor.followers_url]
|
|
|
|
|
|
|
|
if inReplyTo do
|
|
|
|
{Enum.uniq([inReplyTo.actor | to]), cc}
|
|
|
|
else
|
|
|
|
{to, cc}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Determines the full audience based on mentions based on a unlisted audience
|
|
|
|
|
|
|
|
Audience is:
|
|
|
|
* `to` : the mentionned actors, actor's followers and the eventual actor we're replying to
|
|
|
|
* `cc` : public
|
|
|
|
"""
|
2019-07-26 11:30:28 +02:00
|
|
|
@spec get_to_and_cc(Actor.t(), list(), map(), String.t()) :: {list(), list()}
|
|
|
|
def get_to_and_cc(%Actor{} = actor, mentions, inReplyTo, :unlisted) do
|
|
|
|
to = [actor.followers_url | mentions]
|
2019-09-20 19:43:29 +02:00
|
|
|
cc = [@ap_public]
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
|
|
if inReplyTo do
|
|
|
|
{Enum.uniq([inReplyTo.actor | to]), cc}
|
|
|
|
else
|
|
|
|
{to, cc}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Determines the full audience based on mentions based on a private audience
|
|
|
|
|
|
|
|
Audience is:
|
2019-09-20 19:43:29 +02:00
|
|
|
* `to` : the mentioned actors, actor's followers and the eventual actor we're replying to
|
2018-12-14 17:41:55 +01:00
|
|
|
* `cc` : none
|
|
|
|
"""
|
2019-07-26 11:30:28 +02:00
|
|
|
@spec get_to_and_cc(Actor.t(), list(), map(), String.t()) :: {list(), list()}
|
|
|
|
def get_to_and_cc(%Actor{} = actor, mentions, inReplyTo, :private) do
|
|
|
|
{to, cc} = get_to_and_cc(actor, mentions, inReplyTo, :direct)
|
2018-12-14 17:41:55 +01:00
|
|
|
{[actor.followers_url | to], cc}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Determines the full audience based on mentions based on a direct audience
|
|
|
|
|
|
|
|
Audience is:
|
2019-09-20 19:43:29 +02:00
|
|
|
* `to` : the mentioned actors and the eventual actor we're replying to
|
2018-12-14 17:41:55 +01:00
|
|
|
* `cc` : none
|
|
|
|
"""
|
2019-07-26 11:30:28 +02:00
|
|
|
@spec get_to_and_cc(Actor.t(), list(), map(), String.t()) :: {list(), list()}
|
|
|
|
def get_to_and_cc(_actor, mentions, inReplyTo, :direct) do
|
2018-12-14 17:41:55 +01:00
|
|
|
if inReplyTo do
|
2019-07-26 11:30:28 +02:00
|
|
|
{Enum.uniq([inReplyTo.actor | mentions]), []}
|
2018-12-14 17:41:55 +01:00
|
|
|
else
|
2019-07-26 11:30:28 +02:00
|
|
|
{mentions, []}
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
def get_to_and_cc(_actor, mentions, _inReplyTo, {:list, _}) do
|
|
|
|
{mentions, []}
|
|
|
|
end
|
2019-07-26 11:30:28 +02:00
|
|
|
|
|
|
|
# def get_addressed_users(_, to) when is_list(to) do
|
|
|
|
# Actors.get(to)
|
|
|
|
# end
|
|
|
|
|
|
|
|
def get_addressed_users(mentioned_users, _), do: mentioned_users
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Creates HTML content from text and mentions
|
|
|
|
"""
|
2019-07-26 11:30:28 +02:00
|
|
|
@spec make_content_html(String.t(), list(), String.t()) :: String.t()
|
2018-12-14 17:41:55 +01:00
|
|
|
def make_content_html(
|
2019-07-26 11:30:28 +02:00
|
|
|
text,
|
|
|
|
additional_tags,
|
2018-12-14 17:41:55 +01:00
|
|
|
content_type
|
2019-07-26 11:30:28 +02:00
|
|
|
) do
|
|
|
|
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 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
def format_input(text, "text/plain", options) do
|
2018-12-14 17:41:55 +01:00
|
|
|
text
|
|
|
|
|> Formatter.html_escape("text/plain")
|
2019-07-26 11:30:28 +02:00
|
|
|
|> Formatter.linkify(options)
|
|
|
|
|> (fn {text, mentions, tags} ->
|
|
|
|
{String.replace(text, ~r/\r?\n/, "<br>"), mentions, tags}
|
|
|
|
end).()
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
def format_input(text, "text/html", options) do
|
2018-12-14 17:41:55 +01:00
|
|
|
text
|
|
|
|
|> Formatter.html_escape("text/html")
|
2019-07-26 11:30:28 +02:00
|
|
|
|> Formatter.linkify(options)
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
|
2019-09-09 09:31:08 +02:00
|
|
|
def make_report_content_text(nil), do: {:ok, nil}
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2019-09-09 09:31:08 +02:00
|
|
|
def make_report_content_text(comment) do
|
2019-09-08 00:05:54 +02:00
|
|
|
max_size = Config.get([:instance, :max_report_comment_size], 1000)
|
2019-07-23 13:49:22 +02: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
|
2019-07-26 11:30:28 +02:00
|
|
|
|
|
|
|
def prepare_content(actor, content, visibility, tags, in_reply_to) do
|
2019-09-09 11:21:42 +02:00
|
|
|
with content <- String.trim(content || ""),
|
2019-07-26 11:30:28 +02:00
|
|
|
{content_html, mentions, tags} <-
|
|
|
|
make_content_html(
|
|
|
|
content,
|
|
|
|
tags,
|
2019-07-30 16:40:59 +02:00
|
|
|
"text/html"
|
2019-07-26 11:30:28 +02:00
|
|
|
),
|
|
|
|
mentioned_users <- for({_, mentioned_user} <- mentions, do: mentioned_user.url),
|
|
|
|
addressed_users <- get_addressed_users(mentioned_users, nil),
|
|
|
|
{to, cc} <- get_to_and_cc(actor, addressed_users, in_reply_to, visibility) do
|
|
|
|
{content_html, tags, to, cc}
|
|
|
|
end
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|