Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85710113
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/pleroma/web/metadata.ex b/lib/pleroma/web/metadata.ex
index f8906e03f..cf2b86aaa 100644
--- a/lib/pleroma/web/metadata.ex
+++ b/lib/pleroma/web/metadata.ex
@@ -1,92 +1,29 @@
defmodule Pleroma.Web.Metadata do
alias Phoenix.HTML
- alias Pleroma.{Formatter, User}
- alias Pleroma.Web.MediaProxy
+ @parsers Pleroma.Config.get([:metadata, :providers], [])
def build_tags(params) do
- if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: [])
- |> Enum.map(&to_tag/1)
- |> Enum.map(&HTML.safe_to_string/1)
- |> Enum.join()
- end
-
- def meta_enabled?(type) do
- Pleroma.Config.get([:metadata, type], false)
- end
-
- # opengraph for single status
- defp opengraph_tags(%{activity: activity, user: user}) do
- with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
- [
- {:meta,
- [
- property: "og:title",
- content:
- "#{user.name}" <>
- if user.local do
- "(@#{user.nickname}@{pleroma_domain})"
- else
- "(@#{user.nickname})"
- end
- ], []},
- {:meta, [property: "og:url", content: activity.data["id"]], []},
- {:meta, [property: "og:description", content: truncated_content], []},
- {:meta, [property: "og:image", content: user_avatar_url(user)], []},
- {:meta, [property: "og:image:width", content: 120], []},
- {:meta, [property: "og:image:height", content: 120], []},
- {:meta, [property: "twitter:card", content: "summary"], []}
- ]
- end
- end
+ Enum.reduce(@parsers, "", fn parser, acc ->
+ rendered_html =
+ params
+ |> parser.build_tags()
+ |> Enum.map(&to_tag/1)
+ |> Enum.map(&HTML.safe_to_string/1)
+ |> Enum.join()
- # opengraph for user card
- defp opengraph_tags(%{user: user}) do
- with truncated_bio = scrub_html_and_truncate(user.bio || "") do
- [
- {:meta,
- [
- property: "og:title",
- content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
- ], []},
- {:meta, [property: "og:url", content: User.profile_url(user)], []},
- {:meta, [property: "og:description", content: truncated_bio], []},
- {:meta, [property: "og:image", content: user_avatar_url(user)], []},
- {:meta, [property: "og:image:width", content: 120], []},
- {:meta, [property: "og:image:height", content: 120], []},
- {:meta, [property: "twitter:card", content: "summary"], []}
- ]
- end
- end
-
- defp opengraph_tags(_) do
- []
+ acc <> rendered_html
+ end)
end
def to_tag(data) do
with {name, attrs, _content = []} <- data do
HTML.Tag.tag(name, attrs)
else
{name, attrs, content} ->
HTML.Tag.content_tag(name, content, attrs)
_ ->
raise ArgumentError, message: "make_tag invalid args"
end
end
-
- defp scrub_html_and_truncate(content) do
- content
- # html content comes from DB already encoded, decode first and scrub after
- |> HtmlEntities.decode()
- |> Pleroma.HTML.strip_tags()
- |> Formatter.truncate()
- end
-
- defp user_avatar_url(user) do
- User.avatar_url(user) |> MediaProxy.url()
- end
-
- def pleroma_domain do
- Pleroma.Web.Endpoint.host()
- end
end
diff --git a/lib/pleroma/web/metadata.ex b/lib/pleroma/web/metadata/opengraph.ex
similarity index 55%
copy from lib/pleroma/web/metadata.ex
copy to lib/pleroma/web/metadata/opengraph.ex
index f8906e03f..5f8bed2fb 100644
--- a/lib/pleroma/web/metadata.ex
+++ b/lib/pleroma/web/metadata/opengraph.ex
@@ -1,92 +1,66 @@
-defmodule Pleroma.Web.Metadata do
- alias Phoenix.HTML
- alias Pleroma.{Formatter, User}
+defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
+ alias Pleroma.Web.Metadata.Providers.Provider
+ alias Pleroma.{HTML, Formatter, User}
alias Pleroma.Web.MediaProxy
- def build_tags(params) do
- if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: [])
- |> Enum.map(&to_tag/1)
- |> Enum.map(&HTML.safe_to_string/1)
- |> Enum.join()
- end
-
- def meta_enabled?(type) do
- Pleroma.Config.get([:metadata, type], false)
- end
+ @behaviour Provider
- # opengraph for single status
- defp opengraph_tags(%{activity: activity, user: user}) do
+ @impl Provider
+ def build_tags(%{activity: activity, user: user}) do
with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
[
{:meta,
[
property: "og:title",
- content:
- "#{user.name}" <>
- if user.local do
- "(@#{user.nickname}@{pleroma_domain})"
- else
- "(@#{user.nickname})"
- end
+ content: user_name_string(user)
], []},
{:meta, [property: "og:url", content: activity.data["id"]], []},
{:meta, [property: "og:description", content: truncated_content], []},
{:meta, [property: "og:image", content: user_avatar_url(user)], []},
{:meta, [property: "og:image:width", content: 120], []},
{:meta, [property: "og:image:height", content: 120], []},
{:meta, [property: "twitter:card", content: "summary"], []}
]
end
end
- # opengraph for user card
- defp opengraph_tags(%{user: user}) do
+ @impl Provider
+ def build_tags(%{user: user}) do
with truncated_bio = scrub_html_and_truncate(user.bio || "") do
[
{:meta,
[
property: "og:title",
- content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
+ content: user_name_string(user)
], []},
{:meta, [property: "og:url", content: User.profile_url(user)], []},
{:meta, [property: "og:description", content: truncated_bio], []},
{:meta, [property: "og:image", content: user_avatar_url(user)], []},
{:meta, [property: "og:image:width", content: 120], []},
{:meta, [property: "og:image:height", content: 120], []},
{:meta, [property: "twitter:card", content: "summary"], []}
]
end
end
- defp opengraph_tags(_) do
- []
- end
-
- def to_tag(data) do
- with {name, attrs, _content = []} <- data do
- HTML.Tag.tag(name, attrs)
- else
- {name, attrs, content} ->
- HTML.Tag.content_tag(name, content, attrs)
-
- _ ->
- raise ArgumentError, message: "make_tag invalid args"
- end
- end
-
defp scrub_html_and_truncate(content) do
content
# html content comes from DB already encoded, decode first and scrub after
|> HtmlEntities.decode()
- |> Pleroma.HTML.strip_tags()
+ |> HTML.strip_tags()
|> Formatter.truncate()
end
defp user_avatar_url(user) do
User.avatar_url(user) |> MediaProxy.url()
end
- def pleroma_domain do
- Pleroma.Web.Endpoint.host()
+ defp user_name_string(user) do
+ "#{user.name}" <>
+ if user.local do
+ "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
+ else
+ "(@#{user.nickname})"
+ end
end
end
diff --git a/lib/pleroma/web/metadata/provider.ex b/lib/pleroma/web/metadata/provider.ex
new file mode 100644
index 000000000..f64810fad
--- /dev/null
+++ b/lib/pleroma/web/metadata/provider.ex
@@ -0,0 +1,3 @@
+defmodule Pleroma.Web.Metadata.Providers.Provider do
+ @callback build_tags(map()) :: list()
+end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Sep 18, 10:21 PM (53 m, 18 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1768326
Default Alt Text
(7 KB)
Attached To
Mode
rPUBE pleroma-upstream
Attached
Detach File
Event Timeline
Log In to Comment