Refactor adding tags to an event

Also refactor extracting tags from content, now uses Pleroma's Formatter

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-07-26 11:30:28 +02:00
parent 845d6ff857
commit 6d80bf43ea
23 changed files with 1543 additions and 864 deletions

View File

@@ -6,10 +6,9 @@ defmodule MobilizonWeb.API.Comments do
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.Comment
alias Mobilizon.Service.Formatter
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
import MobilizonWeb.API.Utils
alias MobilizonWeb.API.Utils
@doc """
Create a comment
@@ -20,23 +19,14 @@ defmodule MobilizonWeb.API.Comments do
def create_comment(
from_username,
status,
visibility \\ "public",
visibility \\ :public,
in_reply_to_comment_URL \\ nil
) do
with {:local_actor, %Actor{url: url} = actor} <-
{:local_actor, Actors.get_local_actor_by_name(from_username)},
status <- String.trim(status),
mentions <- Formatter.parse_mentions(status),
in_reply_to_comment <- get_in_reply_to_comment(in_reply_to_comment_URL),
{to, cc} <- to_for_actor_and_mentions(actor, mentions, in_reply_to_comment, visibility),
tags <- Formatter.parse_tags(status),
content_html <-
make_content_html(
status,
mentions,
tags,
"text/plain"
),
{content_html, tags, to, cc} <-
Utils.prepare_content(actor, status, visibility, [], in_reply_to_comment),
comment <-
ActivityPubUtils.make_comment_data(
url,

View File

@@ -4,10 +4,9 @@ defmodule MobilizonWeb.API.Events do
"""
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Formatter
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
import MobilizonWeb.API.Utils
alias MobilizonWeb.API.Utils
@doc """
Create an event
@@ -19,24 +18,19 @@ defmodule MobilizonWeb.API.Events do
description: description,
organizer_actor_id: organizer_actor_id,
begins_on: begins_on,
category: category
category: category,
tags: tags
} = args
) do
require Logger
with %Actor{url: url} = actor <-
Actors.get_local_actor_with_everything(organizer_actor_id),
title <- String.trim(title),
mentions <- Formatter.parse_mentions(description),
visibility <- Map.get(args, :visibility, :public),
{to, cc} <- to_for_actor_and_mentions(actor, mentions, nil, Atom.to_string(visibility)),
tags <- Formatter.parse_tags(description),
picture <- Map.get(args, :picture, nil),
content_html <-
make_content_html(
description,
mentions,
tags,
"text/plain"
),
{content_html, tags, to, cc} <-
Utils.prepare_content(actor, description, visibility, tags, nil),
event <-
ActivityPubUtils.make_event_data(
url,

View File

@@ -4,10 +4,9 @@ defmodule MobilizonWeb.API.Groups do
"""
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Formatter
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
import MobilizonWeb.API.Utils
alias MobilizonWeb.API.Utils
@doc """
Create a group
@@ -24,17 +23,9 @@ defmodule MobilizonWeb.API.Groups do
{:bad_actor, Actors.get_local_actor_by_name(admin_actor_username)},
{:existing_group, nil} <- {:existing_group, Actors.get_group_by_title(title)},
title <- String.trim(title),
mentions <- Formatter.parse_mentions(description),
visibility <- Map.get(args, :visibility, "public"),
{to, cc} <- to_for_actor_and_mentions(actor, mentions, nil, visibility),
tags <- Formatter.parse_tags(description),
content_html <-
make_content_html(
description,
mentions,
tags,
"text/plain"
),
visibility <- Map.get(args, :visibility, :public),
{content_html, tags, to, cc} <-
Utils.prepare_content(actor, description, visibility, [], nil),
group <-
ActivityPubUtils.make_group_data(
url,

View File

@@ -12,11 +12,9 @@ defmodule MobilizonWeb.API.Utils do
* `to` : the mentionned actors, the eventual actor we're replying to and the public
* `cc` : the actor's followers
"""
@spec to_for_actor_and_mentions(Actor.t(), list(), map(), String.t()) :: {list(), list()}
def to_for_actor_and_mentions(%Actor{} = actor, mentions, inReplyTo, "public") do
mentioned_actors = Enum.map(mentions, fn {_, %{url: url}} -> url end)
to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_actors]
@spec get_to_and_cc(Actor.t(), list(), map(), String.t()) :: {list(), list()}
def get_to_and_cc(%Actor{} = actor, mentions, inReplyTo, :public) do
to = ["https://www.w3.org/ns/activitystreams#Public" | mentions]
cc = [actor.followers_url]
if inReplyTo do
@@ -33,11 +31,9 @@ defmodule MobilizonWeb.API.Utils do
* `to` : the mentionned actors, actor's followers and the eventual actor we're replying to
* `cc` : public
"""
@spec to_for_actor_and_mentions(Actor.t(), list(), map(), String.t()) :: {list(), list()}
def to_for_actor_and_mentions(%Actor{} = actor, mentions, inReplyTo, "unlisted") do
mentioned_actors = Enum.map(mentions, fn {_, %{url: url}} -> url end)
to = [actor.followers_url | mentioned_actors]
@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]
cc = ["https://www.w3.org/ns/activitystreams#Public"]
if inReplyTo do
@@ -54,9 +50,9 @@ defmodule MobilizonWeb.API.Utils do
* `to` : the mentionned actors, actor's followers and the eventual actor we're replying to
* `cc` : none
"""
@spec to_for_actor_and_mentions(Actor.t(), list(), map(), String.t()) :: {list(), list()}
def to_for_actor_and_mentions(%Actor{} = actor, mentions, inReplyTo, "private") do
{to, cc} = to_for_actor_and_mentions(actor, mentions, inReplyTo, "direct")
@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)
{[actor.followers_url | to], cc}
end
@@ -67,59 +63,62 @@ defmodule MobilizonWeb.API.Utils do
* `to` : the mentionned actors and the eventual actor we're replying to
* `cc` : none
"""
@spec to_for_actor_and_mentions(Actor.t(), list(), map(), String.t()) :: {list(), list()}
def to_for_actor_and_mentions(_actor, mentions, inReplyTo, "direct") do
mentioned_actors = Enum.map(mentions, fn {_, %{url: url}} -> url end)
@spec get_to_and_cc(Actor.t(), list(), map(), String.t()) :: {list(), list()}
def get_to_and_cc(_actor, mentions, inReplyTo, :direct) do
if inReplyTo do
{Enum.uniq([inReplyTo.actor | mentioned_actors]), []}
{Enum.uniq([inReplyTo.actor | mentions]), []}
else
{mentioned_actors, []}
{mentions, []}
end
end
def get_to_and_cc(_user, mentions, _inReplyTo, {:list, _}), do: {mentions, []}
# def get_addressed_users(_, to) when is_list(to) do
# Actors.get(to)
# end
def get_addressed_users(mentioned_users, _), do: mentioned_users
@doc """
Creates HTML content from text and mentions
"""
@spec make_content_html(String.t(), list(), list(), String.t()) :: String.t()
@spec make_content_html(String.t(), list(), String.t()) :: String.t()
def make_content_html(
status,
mentions,
tags,
text,
additional_tags,
content_type
),
do: format_input(status, mentions, 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, mentions, tags, "text/plain") do
def format_input(text, "text/plain", options) do
text
|> Formatter.html_escape("text/plain")
|> String.replace(~r/\r?\n/, "<br>")
|> (&{[], &1}).()
|> Formatter.add_links()
|> Formatter.add_actor_links(mentions)
|> Formatter.add_hashtag_links(tags)
|> Formatter.finalize()
|> Formatter.linkify(options)
|> (fn {text, mentions, tags} ->
{String.replace(text, ~r/\r?\n/, "<br>"), mentions, tags}
end).()
end
def format_input(text, mentions, _tags, "text/html") do
def format_input(text, "text/html", options) do
text
|> Formatter.html_escape("text/html")
|> String.replace(~r/\r?\n/, "<br>")
|> (&{[], &1}).()
|> Formatter.add_actor_links(mentions)
|> Formatter.finalize()
|> Formatter.linkify(options)
end
# def format_input(text, mentions, tags, "text/markdown") do
# text
# |> Earmark.as_html!()
# |> Formatter.html_escape("text/html")
# |> String.replace(~r/\r?\n/, "")
# |> (&{[], &1}).()
# |> Formatter.add_actor_links(mentions)
# |> Formatter.add_hashtag_links(tags)
# |> Formatter.finalize()
# end
# @doc """
# Formatting text to markdown.
# """
# def format_input(text, "text/markdown", options) do
# text
# |> Formatter.mentions_escape(options)
# |> Earmark.as_html!()
# |> Formatter.linkify(options)
# |> Formatter.html_escape("text/html")
# end
def make_report_content_html(nil), do: {:ok, {nil, [], []}}
@@ -132,4 +131,19 @@ defmodule MobilizonWeb.API.Utils do
{:error, "Comment must be up to #{max_size} characters"}
end
end
def prepare_content(actor, content, visibility, tags, in_reply_to) do
with content <- String.trim(content),
{content_html, mentions, tags} <-
make_content_html(
content,
tags,
"text/plain"
),
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
end