6d86c0ee6d5720d7b89ada56bf4b4eac9cd4954b
[akkoma] / lib / pleroma / web / metadata / opengraph.ex
1 defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
2 alias Pleroma.Web.Metadata.Providers.Provider
3 alias Pleroma.{HTML, Formatter, User}
4 alias Pleroma.Web.MediaProxy
5
6 @behaviour Provider
7
8 @impl Provider
9 def build_tags(%{activity: activity, user: user}) do
10 with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
11 [
12 {:meta,
13 [
14 property: "og:title",
15 content: user_name_string(user)
16 ], []},
17 {:meta, [property: "og:url", content: activity.data["id"]], []},
18 {:meta, [property: "og:description", content: truncated_content], []},
19 {:meta, [property: "og:image", content: user_avatar_url(user)], []},
20 {:meta, [property: "og:image:width", content: 120], []},
21 {:meta, [property: "og:image:height", content: 120], []},
22 {:meta, [property: "twitter:card", content: "summary"], []}
23 ]
24 end
25 end
26
27 @impl Provider
28 def build_tags(%{user: user}) do
29 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
30 [
31 {:meta,
32 [
33 property: "og:title",
34 content: user_name_string(user)
35 ], []},
36 {:meta, [property: "og:url", content: User.profile_url(user)], []},
37 {:meta, [property: "og:description", content: truncated_bio], []},
38 {:meta, [property: "og:image", content: user_avatar_url(user)], []},
39 {:meta, [property: "og:image:width", content: 120], []},
40 {:meta, [property: "og:image:height", content: 120], []},
41 {:meta, [property: "twitter:card", content: "summary"], []}
42 ]
43 end
44 end
45
46 defp scrub_html_and_truncate(content) do
47 content
48 # html content comes from DB already encoded, decode first and scrub after
49 |> HtmlEntities.decode()
50 |> String.replace(~r/<br\s?\/?>/, " ")
51 |> HTML.strip_tags()
52 |> Formatter.truncate()
53 end
54
55 defp user_avatar_url(user) do
56 User.avatar_url(user) |> MediaProxy.url()
57 end
58
59 defp user_name_string(user) do
60 "#{user.name}" <>
61 if user.local do
62 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
63 else
64 "(@#{user.nickname})"
65 end
66 end
67 end