2019-03-04 16:20:18 +00:00
|
|
|
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
|
|
|
alias Phoenix.HTML
|
|
|
|
alias Phoenix.HTML.Tag
|
2021-10-21 15:55:16 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-06-22 11:15:21 +00:00
|
|
|
alias Mobilizon.Addresses.Address
|
2021-10-10 14:25:50 +00:00
|
|
|
alias Mobilizon.Events.{Event, EventOptions}
|
2021-10-21 15:55:16 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2020-01-26 20:36:50 +00:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2021-10-21 15:55:16 +00:00
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2021-06-22 11:15:21 +00:00
|
|
|
|
|
|
|
import Mobilizon.Service.Metadata.Utils,
|
|
|
|
only: [process_description: 2, strip_tags: 1, datetime_to_string: 2, render_address: 1]
|
2019-03-04 16:20:18 +00:00
|
|
|
|
2020-06-15 14:20:58 +00:00
|
|
|
def build_tags(%Event{} = event, locale \\ "en") do
|
2021-06-22 11:15:21 +00:00
|
|
|
formatted_description = description(event, locale)
|
2019-10-10 10:25:32 +00:00
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
tags = [
|
2019-10-10 10:25:32 +00:00
|
|
|
Tag.content_tag(:title, event.title <> " - Mobilizon"),
|
2021-06-22 11:15:21 +00:00
|
|
|
Tag.tag(:meta, name: "description", content: process_description(event.description, locale)),
|
2019-03-04 16:20:18 +00:00
|
|
|
Tag.tag(:meta, property: "og:title", content: event.title),
|
|
|
|
Tag.tag(:meta, property: "og:url", content: event.url),
|
2021-06-22 11:15:21 +00:00
|
|
|
Tag.tag(:meta, property: "og:description", content: formatted_description),
|
2020-02-07 15:25:30 +00:00
|
|
|
Tag.tag(:meta, property: "og:type", content: "website"),
|
|
|
|
# Tell Search Engines what's the origin
|
|
|
|
Tag.tag(:link, rel: "canonical", href: event.url)
|
2019-03-04 16:20:18 +00:00
|
|
|
]
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
tags =
|
|
|
|
if is_nil(event.picture) do
|
|
|
|
tags
|
|
|
|
else
|
|
|
|
tags ++
|
|
|
|
[
|
2019-05-28 08:51:02 +00:00
|
|
|
Tag.tag(:meta,
|
|
|
|
property: "og:image",
|
2020-12-15 16:17:42 +00:00
|
|
|
content: event.picture.file.url
|
2019-05-28 08:51:02 +00:00
|
|
|
)
|
2019-05-22 12:12:11 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-10-21 15:55:16 +00:00
|
|
|
breadcrumbs =
|
|
|
|
if event.attributed_to do
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"@context" => "https://schema.org",
|
|
|
|
"@type" => "BreadcrumbList",
|
|
|
|
"itemListElement" => [
|
|
|
|
%{
|
|
|
|
"@type" => "ListItem",
|
|
|
|
"position" => 1,
|
|
|
|
"name" => Actor.display_name(event.attributed_to),
|
|
|
|
"item" =>
|
|
|
|
Endpoint
|
|
|
|
|> Routes.page_url(
|
|
|
|
:actor,
|
|
|
|
Actor.preferred_username_and_domain(event.attributed_to)
|
|
|
|
)
|
|
|
|
|> URI.decode()
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"@type" => "ListItem",
|
|
|
|
"position" => 2,
|
|
|
|
"name" => event.title
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
breadcrumbs =
|
|
|
|
breadcrumbs ++
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"@context" => "https://schema.org",
|
|
|
|
"@type" => "BreadcrumbList",
|
|
|
|
"itemListElement" => [
|
|
|
|
%{
|
|
|
|
"@type" => "ListItem",
|
|
|
|
"position" => 1,
|
|
|
|
"name" => "Events",
|
|
|
|
"item" => "#{Endpoint.url()}/search"
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"@type" => "ListItem",
|
|
|
|
"position" => 2,
|
|
|
|
"name" => event.title
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
tags ++
|
|
|
|
[
|
|
|
|
Tag.tag(:meta, property: "twitter:card", content: "summary_large_image"),
|
2021-10-21 15:55:16 +00:00
|
|
|
~s{<script type="application/ld+json">#{Jason.encode!(breadcrumbs)}</script>}
|
|
|
|
|> HTML.raw(),
|
2019-05-22 12:12:11 +00:00
|
|
|
~s{<script type="application/ld+json">#{json(event)}</script>} |> HTML.raw()
|
|
|
|
]
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|
2019-03-04 17:38:30 +00:00
|
|
|
|
|
|
|
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
2020-06-24 14:33:59 +00:00
|
|
|
defp json(%Event{title: title} = event) do
|
2019-04-17 15:13:20 +00:00
|
|
|
"event.json"
|
2020-07-09 15:24:28 +00:00
|
|
|
|> ObjectView.render(%{event: %{event | title: strip_tags(title)}})
|
2019-04-17 15:13:20 +00:00
|
|
|
|> Jason.encode!()
|
2019-03-04 17:38:30 +00:00
|
|
|
end
|
2021-06-22 11:15:21 +00:00
|
|
|
|
|
|
|
defp description(
|
|
|
|
%Event{
|
|
|
|
description: description,
|
|
|
|
begins_on: begins_on,
|
2021-10-10 14:25:50 +00:00
|
|
|
physical_address: address,
|
|
|
|
options: %EventOptions{timezone: timezone},
|
|
|
|
language: language
|
2021-06-22 11:15:21 +00:00
|
|
|
},
|
|
|
|
locale
|
|
|
|
) do
|
2021-10-10 14:25:50 +00:00
|
|
|
language = build_language(language, locale)
|
2021-10-11 15:13:57 +00:00
|
|
|
begins_on = build_begins_on(begins_on, timezone)
|
2021-10-10 14:25:50 +00:00
|
|
|
|
|
|
|
begins_on
|
|
|
|
|> datetime_to_string(language)
|
|
|
|
|> (&[&1]).()
|
|
|
|
|> add_timezone(begins_on)
|
|
|
|
|> maybe_build_address(address)
|
|
|
|
|> build_description(description, language)
|
|
|
|
|> Enum.join(" - ")
|
2021-06-22 11:15:21 +00:00
|
|
|
end
|
|
|
|
|
2021-10-10 14:25:50 +00:00
|
|
|
@spec build_language(String.t() | nil, String.t()) :: String.t()
|
|
|
|
defp build_language(language, locale), do: language || locale
|
|
|
|
|
2021-10-11 15:13:57 +00:00
|
|
|
@spec build_begins_on(DateTime.t(), String.t() | nil) :: DateTime.t()
|
|
|
|
defp build_begins_on(begins_on, timezone) do
|
2021-10-10 14:25:50 +00:00
|
|
|
if timezone do
|
|
|
|
case DateTime.shift_zone(begins_on, timezone) do
|
|
|
|
{:ok, begins_on} -> begins_on
|
|
|
|
{:error, _err} -> begins_on
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begins_on
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp add_timezone(elements, %DateTime{} = begins_on) do
|
|
|
|
elements ++ [Cldr.DateTime.Formatter.zone_gmt(begins_on)]
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec maybe_build_address(list(String.t()), Address.t() | nil) :: list(String.t())
|
|
|
|
defp maybe_build_address(elements, %Address{} = address) do
|
|
|
|
elements ++ [render_address(address)]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_build_address(elements, _address), do: elements
|
|
|
|
|
|
|
|
@spec build_description(list(String.t()), String.t(), String.t()) :: list(String.t())
|
|
|
|
defp build_description(elements, description, language) do
|
|
|
|
elements ++ [process_description(description, language)]
|
2021-06-22 11:15:21 +00:00
|
|
|
end
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|