dd099e2ad7dd5aad4aaef5f73e80d09e75314008
[akkoma] / lib / pleroma / web / ostatus / metadata.ex
1 defmodule Pleroma.Web.Metadata do
2 alias Phoenix.HTML
3 alias Pleroma.{Web, Formatter}
4 alias Pleroma.{User, Activity}
5 alias Pleroma.Web.MediaProxy
6
7 def build_tags(activity, user, url) do
8 Enum.concat([
9 if(meta_enabled?(:opengraph), do: opengraph_tags(activity, user), else: []),
10 if(meta_enabled?(:oembed), do: oembed_links(url), else: [])
11 ])
12 |> Enum.map(&to_tag/1)
13 |> Enum.map(&HTML.safe_to_string/1)
14 |> Enum.join("\n")
15 end
16
17 def meta_enabled?(type) do
18 config = Pleroma.Config.get(:metadata, [])
19 Keyword.get(config, type, false)
20 end
21
22 def to_tag(data) do
23 with {name, attrs, _content = []} <- data do
24 HTML.Tag.tag(name, attrs)
25 else
26 {name, attrs, content} ->
27 HTML.Tag.content_tag(name, content, attrs)
28
29 _ ->
30 raise ArgumentError, message: "make_tag invalid args"
31 end
32 end
33
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'], [] }
38 end)
39 end
40
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
45 [
46 {:meta,
47 [
48 property: "og:title",
49 content: "#{user.name} (@#{user.nickname}@#{domain}) post ##{activity.id}"
50 ], []},
51 {:meta, [property: "og:url", content: activity.data["id"]], []},
52 {:meta, [property: "og:description", content: truncated_content],
53 []},
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"], []}
58 ]
59 end
60 end
61
62 defp oembed_path(url, format) do
63 query = URI.encode_query(%{url: url, format: format})
64 "#{Web.base_url()}/oembed?#{query}"
65 end
66 end