2019-03-04 16:20:18 +00:00
|
|
|
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
|
|
|
alias Phoenix.HTML
|
|
|
|
alias Phoenix.HTML.Tag
|
|
|
|
alias Mobilizon.Events.Event
|
2020-01-26 20:36:50 +00:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
|
|
|
alias Mobilizon.Web.MediaProxy
|
2020-06-15 14:20:58 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-03-04 16:20:18 +00:00
|
|
|
|
2020-06-15 14:20:58 +00:00
|
|
|
def build_tags(%Event{} = event, locale \\ "en") do
|
|
|
|
event = Map.put(event, :description, process_description(event.description, 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"),
|
|
|
|
Tag.tag(:meta, name: "description", content: event.description),
|
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),
|
|
|
|
Tag.tag(:meta, property: "og:description", content: event.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",
|
|
|
|
content: event.picture.file.url |> MediaProxy.url()
|
|
|
|
)
|
2019-05-22 12:12:11 +00: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 16:20:18 +00:00
|
|
|
end
|
2019-03-04 17:38:30 +00:00
|
|
|
|
2020-06-15 14:20:58 +00:00
|
|
|
defp process_description(nil, locale), do: process_description("", locale)
|
|
|
|
|
|
|
|
defp process_description("", locale) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
gettext("The event organizer didn't add any description.")
|
|
|
|
end
|
|
|
|
|
|
|
|
defp process_description(description, _locale) do
|
2019-10-10 10:25:32 +00:00
|
|
|
description
|
|
|
|
|> HtmlSanitizeEx.strip_tags()
|
|
|
|
|> String.slice(0..200)
|
|
|
|
|> (&"#{&1}…").()
|
|
|
|
end
|
|
|
|
|
2019-03-04 17:38:30 +00:00
|
|
|
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
2019-04-17 15:13:20 +00:00
|
|
|
defp json(%Event{} = event) do
|
|
|
|
"event.json"
|
|
|
|
|> ObjectView.render(%{event: event})
|
|
|
|
|> Jason.encode!()
|
2019-03-04 17:38:30 +00:00
|
|
|
end
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|