2019-02-11 23:59:04 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-02-11 23:59:04 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.Metadata.Utils do
|
2021-05-22 21:44:51 +00:00
|
|
|
alias Pleroma.Activity
|
2019-08-29 19:01:37 +00:00
|
|
|
alias Pleroma.Emoji
|
2019-02-11 23:59:04 +00:00
|
|
|
alias Pleroma.Formatter
|
2019-03-05 02:52:23 +00:00
|
|
|
alias Pleroma.HTML
|
2019-02-11 23:59:04 +00:00
|
|
|
|
2022-08-23 12:49:05 +00:00
|
|
|
defp scrub_html_and_truncate_object_field(field, object) do
|
|
|
|
field
|
2019-02-11 23:59:04 +00:00
|
|
|
# html content comes from DB already encoded, decode first and scrub after
|
|
|
|
|> HtmlEntities.decode()
|
|
|
|
|> String.replace(~r/<br\s?\/?>/, " ")
|
2021-05-22 21:44:51 +00:00
|
|
|
|> Activity.HTML.get_cached_stripped_html_for_activity(object, "metadata")
|
2019-08-29 19:01:37 +00:00
|
|
|
|> Emoji.Formatter.demojify()
|
2020-01-03 13:21:52 +00:00
|
|
|
|> HtmlEntities.decode()
|
2019-02-11 23:59:04 +00:00
|
|
|
|> Formatter.truncate()
|
|
|
|
end
|
|
|
|
|
2022-08-23 12:49:05 +00:00
|
|
|
def scrub_html_and_truncate(%{data: %{"summary" => summary}} = object)
|
|
|
|
when is_binary(summary) and summary != "" do
|
|
|
|
summary
|
|
|
|
|> scrub_html_and_truncate_object_field(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
|
|
|
|
content
|
|
|
|
|> scrub_html_and_truncate_object_field(object)
|
|
|
|
end
|
|
|
|
|
2023-03-05 21:30:52 +00:00
|
|
|
def scrub_html_and_truncate(content, max_length \\ 200, omission \\ "...")
|
|
|
|
when is_binary(content) do
|
2019-12-17 19:13:45 +00:00
|
|
|
content
|
|
|
|
|> scrub_html
|
|
|
|
|> Emoji.Formatter.demojify()
|
2020-01-14 13:26:56 +00:00
|
|
|
|> HtmlEntities.decode()
|
2023-03-05 21:30:52 +00:00
|
|
|
|> Formatter.truncate(max_length, omission)
|
2019-12-17 19:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def scrub_html(content) when is_binary(content) do
|
2019-02-11 23:59:04 +00:00
|
|
|
content
|
|
|
|
# html content comes from DB already encoded, decode first and scrub after
|
|
|
|
|> HtmlEntities.decode()
|
|
|
|
|> String.replace(~r/<br\s?\/?>/, " ")
|
|
|
|
|> HTML.strip_tags()
|
|
|
|
end
|
|
|
|
|
2019-12-17 19:13:45 +00:00
|
|
|
def scrub_html(content), do: content
|
|
|
|
|
2019-02-11 23:59:04 +00:00
|
|
|
def user_name_string(user) do
|
|
|
|
"#{user.name} " <>
|
|
|
|
if user.local do
|
|
|
|
"(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
|
|
|
|
else
|
|
|
|
"(@#{user.nickname})"
|
|
|
|
end
|
|
|
|
end
|
2019-07-12 16:42:54 +00:00
|
|
|
|
|
|
|
@spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
|
|
|
|
def fetch_media_type(supported_types, media_type) do
|
|
|
|
Enum.find(supported_types, fn support_type ->
|
|
|
|
String.starts_with?(media_type, support_type)
|
|
|
|
end)
|
|
|
|
end
|
2019-02-11 23:59:04 +00:00
|
|
|
end
|