Add basic metadata to opengraph preview

Also refactor datetime & address utils

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-06-22 13:15:21 +02:00
parent eee2d63309
commit 8caf1e302b
9 changed files with 376 additions and 53 deletions

View File

@@ -1,19 +1,22 @@
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
alias Phoenix.HTML
alias Phoenix.HTML.Tag
alias Mobilizon.Addresses.Address
alias Mobilizon.Events.Event
alias Mobilizon.Web.JsonLD.ObjectView
import Mobilizon.Service.Metadata.Utils, only: [process_description: 2, strip_tags: 1]
import Mobilizon.Service.Metadata.Utils,
only: [process_description: 2, strip_tags: 1, datetime_to_string: 2, render_address: 1]
def build_tags(%Event{} = event, locale \\ "en") do
event = Map.put(event, :description, process_description(event.description, locale))
formatted_description = description(event, locale)
tags = [
Tag.content_tag(:title, event.title <> " - Mobilizon"),
Tag.tag(:meta, name: "description", content: event.description),
Tag.tag(:meta, name: "description", content: process_description(event.description, locale)),
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:description", content: formatted_description),
Tag.tag(:meta, property: "og:type", content: "website"),
# Tell Search Engines what's the origin
Tag.tag(:link, rel: "canonical", href: event.url)
@@ -45,4 +48,25 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|> ObjectView.render(%{event: %{event | title: strip_tags(title)}})
|> Jason.encode!()
end
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
end

View File

@@ -3,6 +3,7 @@ defmodule Mobilizon.Service.Metadata.Utils do
Tools to convert tags to string.
"""
alias Mobilizon.Service.{Address, DateTime}
alias Mobilizon.Service.Formatter.HTML, as: HTMLFormatter
alias Phoenix.HTML
import Mobilizon.Web.Gettext
@@ -52,6 +53,9 @@ defmodule Mobilizon.Service.Metadata.Utils do
gettext("The event organizer didn't add any description.")
end
defdelegate datetime_to_string(datetime, locale \\ "en", format \\ :medium), to: DateTime
defdelegate render_address(address), to: Address
defp maybe_slice(description, limit) do
if String.length(description) > limit do
description