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
|
2021-01-19 10:24:21 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2020-09-02 08:00:39 +00:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2021-01-19 10:24:21 +00:00
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2022-10-31 12:00:45 +00:00
|
|
|
|
|
|
|
import Mobilizon.Service.Metadata.Utils,
|
|
|
|
only: [process_description: 2, default_description: 1, escape_text: 1]
|
|
|
|
|
2021-01-19 10:24:21 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-11-17 14:45:08 +00:00
|
|
|
|
|
|
|
def build_tags(_actor, _locale \\ "en")
|
|
|
|
|
|
|
|
def build_tags(%Actor{type: :Group} = group, locale) do
|
|
|
|
default_desc = default_description(locale)
|
|
|
|
|
|
|
|
group =
|
|
|
|
Map.update(group, :summary, default_desc, fn summary ->
|
|
|
|
process_description(summary, locale)
|
|
|
|
end)
|
2019-03-04 16:20:18 +00:00
|
|
|
|
2020-09-02 08:00:39 +00:00
|
|
|
[
|
2022-10-31 12:00:45 +00:00
|
|
|
Tag.tag(:meta, property: "og:title", content: actor_display_name_escaped(group)),
|
2021-10-21 15:55:16 +00:00
|
|
|
Tag.tag(:meta,
|
|
|
|
property: "og:url",
|
|
|
|
content:
|
|
|
|
Endpoint
|
|
|
|
|> Routes.page_url(
|
|
|
|
:actor,
|
|
|
|
Actor.preferred_username_and_domain(group)
|
|
|
|
)
|
|
|
|
|> URI.decode()
|
|
|
|
),
|
2020-11-17 14:45:08 +00:00
|
|
|
Tag.tag(:meta, property: "og:description", content: group.summary),
|
2019-03-04 16:20:18 +00:00
|
|
|
Tag.tag(:meta, property: "og:type", content: "profile"),
|
2021-08-04 08:39:38 +00:00
|
|
|
Tag.tag(:meta,
|
|
|
|
property: "profile:username",
|
2022-10-31 12:00:45 +00:00
|
|
|
content: group |> Actor.preferred_username_and_domain() |> escape_text()
|
2021-08-04 08:39:38 +00:00
|
|
|
),
|
2021-10-21 15:55:16 +00:00
|
|
|
Tag.tag(:meta, property: "twitter:card", content: "summary"),
|
|
|
|
Tag.tag(:meta, property: "twitter:site", content: "@joinmobilizon")
|
2019-03-04 16:20:18 +00:00
|
|
|
]
|
2020-11-17 14:45:08 +00:00
|
|
|
|> maybe_add_avatar(group)
|
|
|
|
|> add_group_schema(group)
|
2021-08-04 08:39:38 +00:00
|
|
|
|> add_group_feeds(group)
|
|
|
|
|> add_canonical(group)
|
|
|
|
|> maybe_add_no_index(group)
|
2020-09-02 08:00:39 +00:00
|
|
|
end
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2020-11-17 14:45:08 +00:00
|
|
|
def build_tags(%Actor{} = _actor, _locale), do: []
|
|
|
|
|
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 ++
|
2020-12-15 16:17:42 +00:00
|
|
|
[Tag.tag(:meta, property: "og:image", content: actor.avatar.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
|
|
|
|
2021-08-04 08:39:38 +00:00
|
|
|
@spec add_group_schema(list(Tag.t()), Actor.t()) :: list(Tag.t())
|
2020-11-17 14:45:08 +00:00
|
|
|
defp add_group_schema(tags, %Actor{} = group) do
|
2021-10-21 15:55:16 +00:00
|
|
|
breadcrumbs = %{
|
|
|
|
"@context" => "https://schema.org",
|
|
|
|
"@type" => "BreadcrumbList",
|
|
|
|
"itemListElement" => [
|
|
|
|
%{
|
|
|
|
"@type" => "ListItem",
|
|
|
|
"position" => 1,
|
2022-10-31 12:00:45 +00:00
|
|
|
"name" => actor_display_name_escaped(group)
|
2021-10-21 15:55:16 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-01-19 10:24:21 +00:00
|
|
|
tags ++
|
|
|
|
[
|
2021-10-21 15:55:16 +00:00
|
|
|
~s{<script type="application/ld+json">#{Jason.encode!(breadcrumbs)}</script>}
|
|
|
|
|> HTML.raw(),
|
2021-08-04 08:39:38 +00:00
|
|
|
~s{<script type="application/ld+json">#{json(group)}</script>} |> HTML.raw()
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec add_group_feeds(list(Tag.t()), Actor.t()) :: list(Tag.t())
|
|
|
|
defp add_group_feeds(tags, %Actor{} = group) do
|
|
|
|
tags ++
|
|
|
|
[
|
2021-01-19 10:24:21 +00:00
|
|
|
Tag.tag(:link,
|
|
|
|
rel: "alternate",
|
|
|
|
type: "application/atom+xml",
|
2022-10-31 12:00:45 +00:00
|
|
|
title: gettext("%{name}'s feed", name: actor_display_name_escaped(group)) |> HTML.raw(),
|
2021-08-04 08:39:38 +00:00
|
|
|
href:
|
|
|
|
Routes.feed_url(Endpoint, :actor, Actor.preferred_username_and_domain(group), :atom)
|
2021-01-19 10:24:21 +00:00
|
|
|
),
|
|
|
|
Tag.tag(:link,
|
|
|
|
rel: "alternate",
|
|
|
|
type: "text/calendar",
|
2022-10-31 12:00:45 +00:00
|
|
|
title: gettext("%{name}'s feed", name: actor_display_name_escaped(group)) |> HTML.raw(),
|
2021-08-04 08:39:38 +00:00
|
|
|
href:
|
|
|
|
Routes.feed_url(
|
|
|
|
Endpoint,
|
|
|
|
:actor,
|
|
|
|
Actor.preferred_username_and_domain(group),
|
|
|
|
:ics
|
|
|
|
)
|
|
|
|
),
|
|
|
|
Tag.tag(:link,
|
|
|
|
rel: "alternate",
|
|
|
|
type: "application/activity+json",
|
|
|
|
href: group.url
|
2021-01-19 10:24:21 +00:00
|
|
|
)
|
|
|
|
]
|
2020-09-02 08:00:39 +00:00
|
|
|
end
|
|
|
|
|
2021-08-04 08:39:38 +00:00
|
|
|
@spec add_canonical(list(Tag.t()), Actor.t()) :: list(Tag.t())
|
|
|
|
defp add_canonical(tags, %Actor{url: group_url}) do
|
|
|
|
tags ++ [Tag.tag(:link, rel: "canonical", href: group_url)]
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec maybe_add_no_index(list(Tag.t()), Actor.t()) :: list(Tag.t())
|
|
|
|
defp maybe_add_no_index(tags, %Actor{domain: nil}), do: tags
|
|
|
|
|
|
|
|
defp maybe_add_no_index(tags, %Actor{}) do
|
|
|
|
tags ++ [Tag.tag(:meta, name: "robots", content: "noindex")]
|
|
|
|
end
|
|
|
|
|
2020-09-02 08:00:39 +00:00
|
|
|
# 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
|
2022-10-31 12:00:45 +00:00
|
|
|
|
|
|
|
defp actor_display_name_escaped(actor) do
|
|
|
|
actor
|
|
|
|
|> Actor.display_name()
|
|
|
|
|> escape_text()
|
|
|
|
end
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|