2019-02-11 23:59:04 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 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
|
2024-04-12 04:16:47 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
|
|
|
|
|
|
|
def visible?(%Pleroma.User{} = object) do
|
|
|
|
Visibility.restrict_unauthenticated_access?(object) == :visible
|
|
|
|
end
|
|
|
|
|
|
|
|
def visible?(object) do
|
|
|
|
Visibility.visible_for_user?(object, nil)
|
|
|
|
end
|
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
|
|
|
|
|
2022-12-14 12:38:48 +00:00
|
|
|
def scrub_html_and_truncate(%{data: _}) do
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2019-03-04 17:47:34 +00:00
|
|
|
def scrub_html_and_truncate(content, max_length \\ 200) 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()
|
2019-12-17 19:13:45 +00:00
|
|
|
|> Formatter.truncate(max_length)
|
|
|
|
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
|
2023-01-22 17:32:58 +00:00
|
|
|
"(@#{user.nickname}@#{Pleroma.Web.WebFinger.domain()})"
|
2019-02-11 23:59:04 +00:00
|
|
|
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
|