2018-12-24 12:34:45 +00:00
|
|
|
# Portions of this file are derived from Pleroma:
|
2019-07-26 09:30:28 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social>
|
2018-12-24 12:34:45 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2018-12-27 10:24:04 +00:00
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/formatter.ex
|
2018-12-24 12:34:45 +00:00
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
defmodule Mobilizon.Service.Formatter do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Formats input text to structured data, extracts mentions and hashtags.
|
2019-01-03 13:59:59 +00:00
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2022-04-20 07:01:27 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2020-11-30 15:14:13 +00:00
|
|
|
alias Phoenix.HTML.Tag
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2020-01-28 18:18:33 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
|
2021-11-26 13:30:46 +00:00
|
|
|
# https://github.com/rrrene/credo/issues/912
|
|
|
|
# credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
|
2019-07-26 09:30:28 +00:00
|
|
|
@link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui
|
|
|
|
@markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec escape_mention_handler(String.t(), String.t(), any(), any()) :: String.t()
|
|
|
|
defp escape_mention_handler("@" <> nickname = mention, buffer, _, _) do
|
2022-04-20 07:01:27 +00:00
|
|
|
case ActivityPubActor.find_or_make_actor_from_nickname(nickname) do
|
|
|
|
{:ok, %Actor{}} ->
|
2019-07-26 09:30:28 +00:00
|
|
|
# escape markdown characters with `\\`
|
|
|
|
# (we don't want something like @user__name to be parsed by markdown)
|
|
|
|
String.replace(mention, @markdown_characters_regex, "\\\\\\1")
|
|
|
|
|
2022-04-20 07:01:27 +00:00
|
|
|
{:error, _err} ->
|
2019-07-26 09:30:28 +00:00
|
|
|
buffer
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec mention_handler(String.t(), String.t(), any(), map()) :: {String.t(), map()}
|
2019-07-26 09:30:28 +00:00
|
|
|
def mention_handler("@" <> nickname, buffer, _opts, acc) do
|
2022-04-20 07:01:27 +00:00
|
|
|
case ActivityPubActor.find_or_make_actor_from_nickname(nickname) do
|
2019-12-03 10:29:51 +00:00
|
|
|
# %Actor{preferred_username: preferred_username} = actor ->
|
|
|
|
# link = "<span class='h-card mention'>@<span>#{preferred_username}</span></span>"
|
|
|
|
#
|
|
|
|
# {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, actor})}}
|
2019-11-15 17:36:47 +00:00
|
|
|
|
2022-04-20 07:01:27 +00:00
|
|
|
{:ok, %Actor{type: :Person, id: id, preferred_username: preferred_username} = actor} ->
|
2020-11-30 15:14:13 +00:00
|
|
|
# link =
|
|
|
|
# "<span class='h-card mention' data-user='#{id}'>@<span>#{preferred_username}</span></span>"
|
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
link =
|
2020-11-30 15:14:13 +00:00
|
|
|
Tag.content_tag(
|
|
|
|
:span,
|
|
|
|
[
|
|
|
|
"@",
|
|
|
|
Tag.content_tag(
|
|
|
|
:span,
|
|
|
|
preferred_username
|
|
|
|
)
|
|
|
|
],
|
|
|
|
"data-user": id,
|
|
|
|
class: "h-card mention"
|
|
|
|
)
|
|
|
|
|> Phoenix.HTML.safe_to_string()
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
{link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, actor})}}
|
|
|
|
|
2023-05-03 10:05:39 +00:00
|
|
|
# Ignore groups mentions for now
|
|
|
|
{:ok, %Actor{type: :Group}} ->
|
|
|
|
{buffer, acc}
|
|
|
|
|
2022-04-20 07:01:27 +00:00
|
|
|
{:error, _} ->
|
2019-07-26 09:30:28 +00:00
|
|
|
{buffer, acc}
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec hashtag_handler(String.t(), String.t(), any(), map()) :: {String.t(), map()}
|
2019-07-26 09:30:28 +00:00
|
|
|
def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
|
|
|
|
tag = String.downcase(tag)
|
2020-01-28 18:18:33 +00:00
|
|
|
url = "#{Endpoint.url()}/tag/#{tag}"
|
2021-03-25 16:55:21 +00:00
|
|
|
|
|
|
|
link =
|
|
|
|
Tag.content_tag(:a, tag_text,
|
|
|
|
class: "hashtag",
|
|
|
|
"data-tag": tag,
|
|
|
|
href: url,
|
|
|
|
rel: "tag ugc"
|
|
|
|
)
|
|
|
|
|> Phoenix.HTML.safe_to_string()
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
{link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}}
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
@doc """
|
|
|
|
Parses a text and replace plain text links with HTML. Returns a tuple with a result text, mentions, and hashtags.
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
"""
|
|
|
|
@spec linkify(String.t(), keyword()) ::
|
|
|
|
{String.t(), [{String.t(), Actor.t()}], [{String.t(), String.t()}]}
|
|
|
|
def linkify(text, options \\ []) do
|
2020-11-30 11:22:05 +00:00
|
|
|
options = linkify_opts() ++ options
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
acc = %{mentions: MapSet.new(), tags: MapSet.new()}
|
2022-01-18 11:46:33 +00:00
|
|
|
{text, %{mentions: mentions, tags: tags}} = Linkify.link_map(text, acc, options)
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
{text, MapSet.to_list(mentions), MapSet.to_list(tags)}
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
@doc """
|
|
|
|
Escapes a special characters in mention names.
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec mentions_escape(String.t(), Keyword.t()) :: String.t()
|
2019-07-26 09:30:28 +00:00
|
|
|
def mentions_escape(text, options \\ []) do
|
|
|
|
options =
|
|
|
|
Keyword.merge(options,
|
|
|
|
mention: true,
|
|
|
|
url: false,
|
|
|
|
mention_handler: &escape_mention_handler/4
|
|
|
|
)
|
|
|
|
|
2020-11-30 11:22:05 +00:00
|
|
|
Linkify.link(text, options)
|
2019-07-26 09:30:28 +00:00
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@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()
|
2019-07-26 09:30:28 +00:00
|
|
|
def html_escape({text, mentions, hashtags}, type) do
|
|
|
|
{html_escape(text, type), mentions, hashtags}
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-30 14:40:59 +00:00
|
|
|
def html_escape(text, "text/html") do
|
2020-02-18 07:57:00 +00:00
|
|
|
with {:ok, content} <- HTML.filter_tags(text) do
|
|
|
|
content
|
|
|
|
end
|
2019-07-26 09:30:28 +00:00
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
|
|
|
|
def html_escape(text, "text/plain") do
|
2019-09-22 11:41:24 +00:00
|
|
|
@link_regex
|
|
|
|
|> Regex.split(text, include_captures: true)
|
2018-12-14 16:41:55 +00:00
|
|
|
|> Enum.map_every(2, fn chunk ->
|
|
|
|
{:safe, part} = Phoenix.HTML.html_escape(chunk)
|
|
|
|
part
|
|
|
|
end)
|
|
|
|
|> Enum.join("")
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec truncate(String.t(), non_neg_integer(), String.t()) :: String.t()
|
2019-07-26 09:30:28 +00:00
|
|
|
def truncate(text, max_length \\ 200, omission \\ "...") do
|
|
|
|
# Remove trailing whitespace
|
|
|
|
text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
if String.length(text) < max_length do
|
|
|
|
text
|
|
|
|
else
|
|
|
|
length_with_omission = max_length - String.length(omission)
|
|
|
|
String.slice(text, 0, length_with_omission) <> omission
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
2020-11-30 11:22:05 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec linkify_opts :: Keyword.t()
|
2020-11-30 11:22:05 +00:00
|
|
|
defp linkify_opts do
|
|
|
|
Mobilizon.Config.get(__MODULE__) ++
|
|
|
|
[
|
2022-01-18 11:46:33 +00:00
|
|
|
hashtag: true,
|
|
|
|
hashtag_handler: &__MODULE__.hashtag_handler/4,
|
2020-11-30 11:22:05 +00:00
|
|
|
mention: true,
|
2020-11-30 15:14:13 +00:00
|
|
|
mention_handler: &__MODULE__.mention_handler/4
|
2020-11-30 11:22:05 +00:00
|
|
|
]
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|