2020-07-09 17:24:28 +02:00
|
|
|
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do
|
|
|
|
alias Phoenix.HTML
|
|
|
|
alias Phoenix.HTML.Tag
|
2021-10-21 17:55:16 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-05-19 11:06:30 +02:00
|
|
|
alias Mobilizon.Medias.{File, Media}
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Posts.Post
|
2021-10-21 17:55:16 +02:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2021-10-21 17:55:16 +02:00
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2022-10-31 13:00:45 +01:00
|
|
|
|
|
|
|
import Mobilizon.Service.Metadata.Utils,
|
|
|
|
only: [process_description: 2, strip_tags: 1, escape_text: 1]
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
def build_tags(%Post{} = post, locale \\ "en") do
|
|
|
|
post = Map.put(post, :body, process_description(post.body, locale))
|
|
|
|
|
2021-05-19 11:06:30 +02:00
|
|
|
tags =
|
|
|
|
[
|
2022-10-31 13:00:45 +01:00
|
|
|
Tag.tag(:meta, property: "og:title", content: escape_text(post.title)),
|
2021-05-19 11:06:30 +02:00
|
|
|
Tag.tag(:meta, property: "og:url", content: post.url),
|
|
|
|
Tag.tag(:meta, property: "og:description", content: post.body),
|
|
|
|
Tag.tag(:meta, property: "og:type", content: "article"),
|
|
|
|
Tag.tag(:meta, property: "twitter:card", content: "summary"),
|
|
|
|
# Tell Search Engines what's the origin
|
|
|
|
Tag.tag(:link, rel: "canonical", href: post.url)
|
|
|
|
]
|
|
|
|
|> maybe_add_post_picture(post)
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2021-10-21 17:55:16 +02:00
|
|
|
breadcrumbs = %{
|
|
|
|
"@context" => "https://schema.org",
|
|
|
|
"@type" => "BreadcrumbList",
|
|
|
|
"itemListElement" => [
|
|
|
|
%{
|
|
|
|
"@type" => "ListItem",
|
|
|
|
"position" => 1,
|
2022-10-31 13:00:45 +01:00
|
|
|
"name" => post.attributed_to |> Actor.display_name() |> escape_text,
|
2021-10-21 17:55:16 +02:00
|
|
|
"item" =>
|
|
|
|
Endpoint
|
|
|
|
|> Routes.page_url(
|
|
|
|
:actor,
|
|
|
|
Actor.preferred_username_and_domain(post.attributed_to)
|
|
|
|
)
|
|
|
|
|> URI.decode()
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"@type" => "ListItem",
|
|
|
|
"position" => 2,
|
|
|
|
"name" => post.title
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
tags ++
|
|
|
|
[
|
|
|
|
Tag.tag(:meta, property: "twitter:card", content: "summary_large_image"),
|
2021-10-21 17:55:16 +02:00
|
|
|
~s{<script type="application/ld+json">#{Jason.encode!(breadcrumbs)}</script>}
|
|
|
|
|> HTML.raw(),
|
2020-07-09 17:24:28 +02:00
|
|
|
~s{<script type="application/ld+json">#{json(post)}</script>} |> HTML.raw()
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
|
|
|
defp json(%Post{title: title} = post) do
|
|
|
|
"post.json"
|
|
|
|
|> ObjectView.render(%{post: %{post | title: strip_tags(title)}})
|
|
|
|
|> Jason.encode!()
|
|
|
|
end
|
2021-05-19 11:06:30 +02:00
|
|
|
|
|
|
|
@spec maybe_add_post_picture(list(), Post.t()) :: list()
|
|
|
|
defp maybe_add_post_picture(tags, %Post{picture: %Media{file: %File{url: url}}}),
|
|
|
|
do:
|
|
|
|
tags ++
|
|
|
|
[
|
|
|
|
Tag.tag(:meta,
|
|
|
|
property: "og:image",
|
|
|
|
content: url
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
defp maybe_add_post_picture(tags, _), do: tags
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|