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
|
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
|
2019-02-11 16:59:04 -07:00
|
|
|
alias Pleroma.Web.MediaProxy
|
|
|
|
|
|
|
|
def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
|
|
|
|
content
|
|
|
|
# html content comes from DB already encoded, decode first and scrub after
|
|
|
|
|> HtmlEntities.decode()
|
|
|
|
|> String.replace(~r/<br\s?\/?>/, " ")
|
2019-04-05 06:19:44 -06:00
|
|
|
|> 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
|
|
|
|
|
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 attachment_url(url) do
|
2020-08-22 14:12:11 -06:00
|
|
|
MediaProxy.preview_url(url)
|
2019-02-11 16:59:04 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
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 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
|