2019-03-04 17:20:18 +01:00
|
|
|
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
|
|
|
alias Phoenix.HTML
|
|
|
|
alias Phoenix.HTML.Tag
|
|
|
|
alias Mobilizon.Events.Event
|
2019-03-04 18:38:30 +01:00
|
|
|
alias MobilizonWeb.JsonLD.ObjectView
|
2019-03-04 17:20:18 +01:00
|
|
|
|
|
|
|
def build_tags(%Event{} = event) do
|
|
|
|
event
|
|
|
|
|> do_build_tags()
|
|
|
|
|> Enum.map(&HTML.safe_to_string/1)
|
|
|
|
|> Enum.reduce("", fn tag, acc -> acc <> tag end)
|
2019-03-04 18:38:30 +01:00
|
|
|
|> Kernel.<>(build_json_ld_schema(event))
|
2019-03-04 17:20:18 +01:00
|
|
|
end
|
|
|
|
|
2019-03-04 18:38:30 +01:00
|
|
|
# Build OpenGraph & Twitter Tags
|
2019-03-04 17:20:18 +01:00
|
|
|
defp do_build_tags(%Event{} = event) do
|
|
|
|
[
|
|
|
|
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),
|
|
|
|
Tag.tag(:meta, property: "og:type", content: "website"),
|
|
|
|
Tag.tag(:meta, property: "og:image", content: event.thumbnail),
|
|
|
|
Tag.tag(:meta, property: "og:image", content: event.large_image),
|
|
|
|
Tag.tag(:meta, property: "twitter:card", content: "summary_large_image")
|
|
|
|
]
|
|
|
|
end
|
2019-03-04 18:38:30 +01:00
|
|
|
|
|
|
|
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
|
|
|
defp build_json_ld_schema(%Event{} = event) do
|
|
|
|
"<script type=\"application\/ld+json\">" <>
|
|
|
|
(ObjectView.render("event.json", %{event: event})
|
|
|
|
|> Jason.encode!()) <> "</script>"
|
|
|
|
end
|
2019-03-04 17:20:18 +01:00
|
|
|
end
|