From 9aa69e12b87a892d33d1bf4f0d556752391b465a Mon Sep 17 00:00:00 2001 From: rinpatch Date: Tue, 15 Jan 2019 23:25:28 +0300 Subject: [PATCH] Add behaviours to TwitterCard, remove some dumb stuff in Formatter.truncate --- lib/pleroma/formatter.ex | 21 ++++++--------------- lib/pleroma/web/metadata/opengraph.ex | 3 ++- lib/pleroma/web/metadata/twitter_card.ex | 6 ++++++ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 49f7075e6..63e0acb21 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -184,21 +184,12 @@ def finalize({subs, text}) do end) end - def truncate(text, opts \\ []) do - max_length = opts[:max_length] || 200 - omission = opts[:omission] || "..." - - cond do - not String.valid?(text) -> - text - - String.length(text) < max_length -> - text - - true -> - length_with_omission = max_length - String.length(omission) - - "#{String.slice(text, 0, length_with_omission)}#{omission}" + def truncate(text, max_length \\ 200, omission \\ "...") do + if String.length(text) < max_length do + text + else + length_with_omission = max_length - String.length(omission) + String.slice(text, 0, length_with_omission) <> omission end end end diff --git a/lib/pleroma/web/metadata/opengraph.ex b/lib/pleroma/web/metadata/opengraph.ex index fcc960311..1e3af947d 100644 --- a/lib/pleroma/web/metadata/opengraph.ex +++ b/lib/pleroma/web/metadata/opengraph.ex @@ -19,7 +19,8 @@ def build_tags(%{activity: activity, user: user}) do {:meta, [property: "og:url", content: activity.data["id"]], []}, {: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:width", content: 120], []}, diff --git a/lib/pleroma/web/metadata/twitter_card.ex b/lib/pleroma/web/metadata/twitter_card.ex index 6424f84b3..3094e61fd 100644 --- a/lib/pleroma/web/metadata/twitter_card.ex +++ b/lib/pleroma/web/metadata/twitter_card.ex @@ -1,4 +1,9 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do + alias Pleroma.Web.Metadata.Providers.Provider + + @behaviour Provider + + @impl Provider def build_tags(%{activity: activity}) do if Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) or activity.data["object"]["attachment"] == [] do @@ -20,6 +25,7 @@ def build_tags(%{activity: activity}) do end end + @impl Provider def build_tags(_) do [{:meta, [property: "twitter:card", content: "summary"], []}] end