Remove oembed for now, will submit it in another MR. Fix warnings
[akkoma] / lib / pleroma / web / ostatus / metadata.ex
1 defmodule Pleroma.Web.Metadata do
2 alias Phoenix.HTML
3 alias Pleroma.{Formatter, User}
4 alias Pleroma.Web.MediaProxy
5
6 def build_tags(params) do
7 if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: [])
8 |> Enum.map(&to_tag/1)
9 |> Enum.map(&HTML.safe_to_string/1)
10 |> Enum.join("\n")
11 end
12
13 def meta_enabled?(type) do
14 config = Pleroma.Config.get(:metadata, [])
15 Keyword.get(config, type, false)
16 end
17
18 # opengraph for single status
19 defp opengraph_tags(%{activity: activity, user: user}) do
20 with truncated_content = Formatter.truncate(activity.data["object"]["content"]) do
21 [
22 {:meta,
23 [
24 property: "og:title",
25 content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) post ##{activity.id}"
26 ], []},
27 {:meta, [property: "og:url", content: activity.data["id"]], []},
28 {:meta, [property: "og:description", content: truncated_content], []},
29 {:meta, [property: "og:image", content: user_avatar_url(user)], []},
30 {:meta, [property: "og:image:width", content: 120], []},
31 {:meta, [property: "og:image:height", content: 120], []},
32 {:meta, [property: "twitter:card", content: "summary"], []}
33 ]
34 end
35 end
36
37 # opengraph for user card
38 defp opengraph_tags(%{user: user}) do
39 with truncated_bio = Formatter.truncate(user.bio) do
40 [
41 {:meta,
42 [
43 property: "og:title",
44 content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
45 ], []},
46 {:meta, [property: "og:url", content: User.profile_url(user)], []},
47 {:meta, [property: "og:description", content: truncated_bio], []},
48 {:meta, [property: "og:image", content: user_avatar_url(user)], []},
49 {:meta, [property: "og:image:width", content: 120], []},
50 {:meta, [property: "og:image:height", content: 120], []},
51 {:meta, [property: "twitter:card", content: "summary"], []}
52 ]
53 end
54 end
55
56 def to_tag(data) do
57 with {name, attrs, _content = []} <- data do
58 HTML.Tag.tag(name, attrs)
59 else
60 {name, attrs, content} ->
61 HTML.Tag.content_tag(name, content, attrs)
62
63 _ ->
64 raise ArgumentError, message: "make_tag invalid args"
65 end
66 end
67
68 defp user_avatar_url(user) do
69 User.avatar_url(user) |> MediaProxy.url()
70 end
71
72 def pleroma_domain do
73 Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN")
74 end
75 end