1 defmodule Pleroma.Web.Metadata do
3 alias Pleroma.{Web, Formatter}
4 alias Pleroma.{User, Activity}
5 alias Pleroma.Web.MediaProxy
7 def build_tags(activity, user, url) do
9 if(meta_enabled?(:opengraph), do: opengraph_tags(activity, user), else: []),
10 if(meta_enabled?(:oembed), do: oembed_links(url), else: [])
12 |> Enum.map(&to_tag/1)
13 |> Enum.map(&HTML.safe_to_string/1)
17 def meta_enabled?(type) do
18 config = Pleroma.Config.get(:metadata, [])
19 Keyword.get(config, type, false)
23 with {name, attrs, _content = []} <- data do
24 HTML.Tag.tag(name, attrs)
26 {name, attrs, content} ->
27 HTML.Tag.content_tag(name, content, attrs)
30 raise ArgumentError, message: "make_tag invalid args"
34 defp oembed_links(url) do
35 Enum.map(["xml", "json"], fn format ->
36 href = HTML.raw(oembed_path(url, format))
37 { :link, [ type: ["application/#{format}+oembed"], href: href, rel: 'alternate'], [] }
41 defp opengraph_tags(activity, user) do
42 with image = User.avatar_url(user) |> MediaProxy.url(),
43 truncated_content = Formatter.truncate(activity.data["object"]["content"]),
44 domain = Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN") do
49 content: "#{user.name} (@#{user.nickname}@#{domain}) post ##{activity.id}"
51 {:meta, [property: "og:url", content: activity.data["id"]], []},
52 {:meta, [property: "og:description", content: truncated_content],
54 {:meta, [property: "og:image", content: image], []},
55 {:meta, [property: "og:image:width", content: 120], []},
56 {:meta, [property: "og:image:height", content: 120], []},
57 {:meta, [property: "twitter:card", content: "summary"], []}
62 defp oembed_path(url, format) do
63 query = URI.encode_query(%{url: url, format: format})
64 "#{Web.base_url()}/oembed?#{query}"