2019-03-04 16:20:18 +00:00
|
|
|
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do
|
2020-09-02 08:00:39 +00:00
|
|
|
alias Phoenix.HTML
|
2019-03-04 16:20:18 +00:00
|
|
|
alias Phoenix.HTML.Tag
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-09-02 08:00:39 +00:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2020-01-26 20:36:50 +00:00
|
|
|
alias Mobilizon.Web.MediaProxy
|
2019-03-04 16:20:18 +00:00
|
|
|
|
2020-06-15 14:20:58 +00:00
|
|
|
def build_tags(%Actor{} = actor, _locale \\ "en") do
|
2020-09-02 08:00:39 +00:00
|
|
|
[
|
2019-03-04 16:20:18 +00:00
|
|
|
Tag.tag(:meta, property: "og:title", content: Actor.display_name_and_username(actor)),
|
|
|
|
Tag.tag(:meta, property: "og:url", content: actor.url),
|
|
|
|
Tag.tag(:meta, property: "og:description", content: actor.summary),
|
|
|
|
Tag.tag(:meta, property: "og:type", content: "profile"),
|
|
|
|
Tag.tag(:meta, property: "profile:username", content: actor.preferred_username),
|
|
|
|
Tag.tag(:meta, property: "twitter:card", content: "summary")
|
|
|
|
]
|
2020-09-02 08:00:39 +00:00
|
|
|
|> maybe_add_avatar(actor)
|
|
|
|
|> maybe_add_group_schema(actor)
|
|
|
|
end
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2020-09-02 08:00:39 +00:00
|
|
|
@spec maybe_add_avatar(list(Tag.t()), Actor.t()) :: list(Tag.t())
|
|
|
|
defp maybe_add_avatar(tags, actor) do
|
2019-05-22 12:12:11 +00:00
|
|
|
if is_nil(actor.avatar) do
|
|
|
|
tags
|
|
|
|
else
|
2019-05-28 08:51:02 +00:00
|
|
|
tags ++
|
|
|
|
[Tag.tag(:meta, property: "og:image", content: actor.avatar.url |> MediaProxy.url())]
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|
2020-09-02 08:00:39 +00:00
|
|
|
|
|
|
|
defp maybe_add_group_schema(tags, %Actor{type: :Group} = group) do
|
|
|
|
tags ++ [~s{<script type="application/ld+json">#{json(group)}</script>} |> HTML.raw()]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_group_schema(tags, _), do: tags
|
|
|
|
|
|
|
|
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
|
|
|
defp json(%Actor{} = group) do
|
|
|
|
"group.json"
|
|
|
|
|> ObjectView.render(%{group: group})
|
|
|
|
|> Jason.encode!()
|
|
|
|
end
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|