2019-03-04 17:20:18 +01:00
|
|
|
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
|
|
|
alias Phoenix.HTML
|
|
|
|
alias Phoenix.HTML.Tag
|
2021-06-22 13:15:21 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-03-04 17:20:18 +01:00
|
|
|
alias Mobilizon.Events.Event
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2021-06-22 13:15:21 +02:00
|
|
|
|
|
|
|
import Mobilizon.Service.Metadata.Utils,
|
|
|
|
only: [process_description: 2, strip_tags: 1, datetime_to_string: 2, render_address: 1]
|
2019-03-04 17:20:18 +01:00
|
|
|
|
2020-06-15 16:20:58 +02:00
|
|
|
def build_tags(%Event{} = event, locale \\ "en") do
|
2021-06-22 13:15:21 +02:00
|
|
|
formatted_description = description(event, locale)
|
2019-10-10 12:25:32 +02:00
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
tags = [
|
2019-10-10 12:25:32 +02:00
|
|
|
Tag.content_tag(:title, event.title <> " - Mobilizon"),
|
2021-06-22 13:15:21 +02:00
|
|
|
Tag.tag(:meta, name: "description", content: process_description(event.description, locale)),
|
2019-03-04 17:20:18 +01:00
|
|
|
Tag.tag(:meta, property: "og:title", content: event.title),
|
|
|
|
Tag.tag(:meta, property: "og:url", content: event.url),
|
2021-06-22 13:15:21 +02:00
|
|
|
Tag.tag(:meta, property: "og:description", content: formatted_description),
|
2020-02-07 16:25:30 +01: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 17:20:18 +01:00
|
|
|
]
|
2019-05-22 14:12:11 +02:00
|
|
|
|
|
|
|
tags =
|
|
|
|
if is_nil(event.picture) do
|
|
|
|
tags
|
|
|
|
else
|
|
|
|
tags ++
|
|
|
|
[
|
2019-05-28 10:51:02 +02:00
|
|
|
Tag.tag(:meta,
|
|
|
|
property: "og:image",
|
2020-12-15 17:17:42 +01:00
|
|
|
content: event.picture.file.url
|
2019-05-28 10:51:02 +02:00
|
|
|
)
|
2019-05-22 14:12:11 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
tags ++
|
|
|
|
[
|
|
|
|
Tag.tag(:meta, property: "twitter:card", content: "summary_large_image"),
|
|
|
|
~s{<script type="application/ld+json">#{json(event)}</script>} |> HTML.raw()
|
|
|
|
]
|
2019-03-04 17:20:18 +01:00
|
|
|
end
|
2019-03-04 18:38:30 +01:00
|
|
|
|
|
|
|
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
2020-06-24 16:33:59 +02:00
|
|
|
defp json(%Event{title: title} = event) do
|
2019-04-17 17:13:20 +02:00
|
|
|
"event.json"
|
2020-07-09 17:24:28 +02:00
|
|
|
|> ObjectView.render(%{event: %{event | title: strip_tags(title)}})
|
2019-04-17 17:13:20 +02:00
|
|
|
|> Jason.encode!()
|
2019-03-04 18:38:30 +01:00
|
|
|
end
|
2021-06-22 13:15:21 +02:00
|
|
|
|
|
|
|
defp description(
|
|
|
|
%Event{
|
|
|
|
description: description,
|
|
|
|
begins_on: begins_on,
|
|
|
|
physical_address: %Address{} = address
|
|
|
|
},
|
|
|
|
locale
|
|
|
|
) do
|
|
|
|
"#{datetime_to_string(begins_on, locale)} - #{render_address(address)} - #{process_description(description, locale)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
defp description(
|
|
|
|
%Event{
|
|
|
|
description: description,
|
|
|
|
begins_on: begins_on
|
|
|
|
},
|
|
|
|
locale
|
|
|
|
) do
|
|
|
|
"#{datetime_to_string(begins_on, locale)} - #{process_description(description, locale)}"
|
|
|
|
end
|
2019-03-04 17:20:18 +01:00
|
|
|
end
|