2019-02-11 16:59:04 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-02-11 16:59:04 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.Metadata.Utils do
|
2021-05-22 15:44:51 -06:00
|
|
|
alias Pleroma.Activity
|
2019-08-29 13:01:37 -06:00
|
|
|
alias Pleroma.Emoji
|
2019-02-11 16:59:04 -07:00
|
|
|
alias Pleroma.Formatter
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.HTML
|
2024-04-11 22:16:47 -06: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 16:59:04 -07:00
|
|
|
|
2022-08-23 06:49:05 -06:00
|
|
|
defp scrub_html_and_truncate_object_field(field, object) do
|
|
|
|
field
|
2019-02-11 16:59:04 -07:00
|
|
|
# html content comes from DB already encoded, decode first and scrub after
|
|
|
|
|> HtmlEntities.decode()
|
|
|
|
|> String.replace(~r/<br\s?\/?>/, " ")
|
2021-05-22 15:44:51 -06:00
|
|
|
|> Activity.HTML.get_cached_stripped_html_for_activity(object, "metadata")
|
2019-08-29 13:01:37 -06:00
|
|
|
|> Emoji.Formatter.demojify()
|
2020-01-03 06:21:52 -07:00
|
|
|
|> HtmlEntities.decode()
|
2019-02-11 16:59:04 -07:00
|
|
|
|> Formatter.truncate()
|
|
|
|
end
|
|
|
|
|
2022-08-23 06:49:05 -06: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 05:38:48 -07:00
|
|
|
def scrub_html_and_truncate(%{data: _}) do
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2019-03-04 10:47:34 -07:00
|
|
|
def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do
|
2019-12-17 12:13:45 -07:00
|
|
|
content
|
|
|
|
|> scrub_html
|
|
|
|
|> Emoji.Formatter.demojify()
|
2020-01-14 06:26:56 -07:00
|
|
|
|> HtmlEntities.decode()
|
2019-12-17 12:13:45 -07:00
|
|
|
|> Formatter.truncate(max_length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def scrub_html(content) when is_binary(content) do
|
2019-02-11 16:59:04 -07: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 12:13:45 -07:00
|
|
|
def scrub_html(content), do: content
|
|
|
|
|
2019-02-11 16:59:04 -07:00
|
|
|
def user_name_string(user) do
|
|
|
|
"#{user.name} " <>
|
|
|
|
if user.local do
|
2023-01-22 10:32:58 -07:00
|
|
|
"(@#{user.nickname}@#{Pleroma.Web.WebFinger.domain()})"
|
2019-02-11 16:59:04 -07:00
|
|
|
else
|
|
|
|
"(@#{user.nickname})"
|
|
|
|
end
|
|
|
|
end
|
2019-07-12 10:42:54 -06: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 16:59:04 -07:00
|
|
|
end
|