Add behaviours to TwitterCard, remove some dumb stuff in Formatter.truncate

This commit is contained in:
rinpatch 2019-01-15 23:25:28 +03:00
parent 2e630bea0d
commit 9aa69e12b8
3 changed files with 14 additions and 16 deletions

View File

@ -184,21 +184,12 @@ def finalize({subs, text}) do
end) end)
end end
def truncate(text, opts \\ []) do def truncate(text, max_length \\ 200, omission \\ "...") do
max_length = opts[:max_length] || 200 if String.length(text) < max_length do
omission = opts[:omission] || "..."
cond do
not String.valid?(text) ->
text text
else
String.length(text) < max_length ->
text
true ->
length_with_omission = max_length - String.length(omission) length_with_omission = max_length - String.length(omission)
String.slice(text, 0, length_with_omission) <> omission
"#{String.slice(text, 0, length_with_omission)}#{omission}"
end end
end end
end end

View File

@ -19,7 +19,8 @@ def build_tags(%{activity: activity, user: user}) do
{:meta, [property: "og:url", content: activity.data["id"]], []}, {:meta, [property: "og:url", content: activity.data["id"]], []},
{:meta, [property: "og:description", content: truncated_content], []} {:meta, [property: "og:description", content: truncated_content], []}
] ++ ] ++
if attachments == [] or Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) do if attachments == [] or
Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) do
[ [
{:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []}, {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
{:meta, [property: "og:image:width", content: 120], []}, {:meta, [property: "og:image:width", content: 120], []},

View File

@ -1,4 +1,9 @@
defmodule Pleroma.Web.Metadata.Providers.TwitterCard do defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
alias Pleroma.Web.Metadata.Providers.Provider
@behaviour Provider
@impl Provider
def build_tags(%{activity: activity}) do def build_tags(%{activity: activity}) do
if Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) or if Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) or
activity.data["object"]["attachment"] == [] do activity.data["object"]["attachment"] == [] do
@ -20,6 +25,7 @@ def build_tags(%{activity: activity}) do
end end
end end
@impl Provider
def build_tags(_) do def build_tags(_) do
[{:meta, [property: "twitter:card", content: "summary"], []}] [{:meta, [property: "twitter:card", content: "summary"], []}]
end end