4f319b3607003da149ccddd96e82fd817890cfeb
[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(request_url, params) do
8 Enum.concat([
9 if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: []),
10 if(meta_enabled?(:oembed), do: oembed_links(request_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 # opengraph for single status
23 defp opengraph_tags(%{activity: activity, user: user}) do
24 with truncated_content = Formatter.truncate(activity.data["object"]["content"]) do
25 [
26 {:meta,
27 [
28 property: "og:title",
29 content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) post ##{activity.id}"
30 ], []},
31 {:meta, [property: "og:url", content: activity.data["id"]], []},
32 {:meta, [property: "og:description", content: truncated_content],
33 []},
34 {:meta, [property: "og:image", content: user_avatar_url(user)], []},
35 {:meta, [property: "og:image:width", content: 120], []},
36 {:meta, [property: "og:image:height", content: 120], []},
37 {:meta, [property: "twitter:card", content: "summary"], []}
38 ]
39 end
40 end
41
42 # opengraph for user card
43 defp opengraph_tags(%{user: user}) do
44 with truncated_bio = Formatter.truncate(user.bio) do
45 [
46 {:meta,
47 [
48 property: "og:title",
49 content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
50 ], []},
51 {:meta, [property: "og:url", content: User.profile_url(user)], []},
52 {:meta, [property: "og:description", content: truncated_bio], []},
53 {:meta, [property: "og:image", content: user_avatar_url(user)], []},
54 {:meta, [property: "og:image:width", content: 120], []},
55 {:meta, [property: "og:image:height", content: 120], []},
56 {:meta, [property: "twitter:card", content: "summary"], []}
57 ]
58 end
59 end
60
61 defp oembed_links(url) do
62 Enum.map(["xml", "json"], fn format ->
63 href = HTML.raw(oembed_path(url, format))
64 { :link, [ type: ["application/#{format}+oembed"], href: href, rel: 'alternate'], [] }
65 end)
66 end
67
68 def to_tag(data) do
69 with {name, attrs, _content = []} <- data do
70 HTML.Tag.tag(name, attrs)
71 else
72 {name, attrs, content} ->
73 HTML.Tag.content_tag(name, content, attrs)
74
75 _ ->
76 raise ArgumentError, message: "make_tag invalid args"
77 end
78 end
79
80 defp oembed_path(url, format) do
81 query = URI.encode_query(%{url: url, format: format})
82 "#{Web.base_url()}/oembed?#{query}"
83 end
84
85 defp user_avatar_url(user) do
86 User.avatar_url(user) |> MediaProxy.url()
87 end
88
89 def pleroma_domain do
90 Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN")
91 end
92 end