Use post picture as OGP picture if existing

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-05-19 11:06:30 +02:00
parent 5d1f64ce9b
commit b0394fdb02
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 26 additions and 10 deletions

View File

@ -1,6 +1,7 @@
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do
alias Phoenix.HTML alias Phoenix.HTML
alias Phoenix.HTML.Tag alias Phoenix.HTML.Tag
alias Mobilizon.Medias.{File, Media}
alias Mobilizon.Posts.Post alias Mobilizon.Posts.Post
alias Mobilizon.Web.JsonLD.ObjectView 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]
@ -8,7 +9,8 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do
def build_tags(%Post{} = post, locale \\ "en") do def build_tags(%Post{} = post, locale \\ "en") do
post = Map.put(post, :body, process_description(post.body, locale)) post = Map.put(post, :body, process_description(post.body, locale))
tags = [ tags =
[
Tag.tag(:meta, property: "og:title", content: post.title), Tag.tag(:meta, property: "og:title", content: post.title),
Tag.tag(:meta, property: "og:url", content: post.url), Tag.tag(:meta, property: "og:url", content: post.url),
Tag.tag(:meta, property: "og:description", content: post.body), Tag.tag(:meta, property: "og:description", content: post.body),
@ -17,6 +19,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do
# Tell Search Engines what's the origin # Tell Search Engines what's the origin
Tag.tag(:link, rel: "canonical", href: post.url) Tag.tag(:link, rel: "canonical", href: post.url)
] ]
|> maybe_add_post_picture(post)
tags ++ tags ++
[ [
@ -31,4 +34,17 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do
|> ObjectView.render(%{post: %{post | title: strip_tags(title)}}) |> ObjectView.render(%{post: %{post | title: strip_tags(title)}})
|> Jason.encode!() |> Jason.encode!()
end end
@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
end end

View File

@ -120,7 +120,7 @@ defmodule Mobilizon.Service.MetadataTest do
Metadata.Utils.process_description(post.body) Metadata.Utils.process_description(post.body)
}\" property=\"og:description\"><meta content=\"article\" property=\"og:type\"><meta content=\"summary\" property=\"twitter:card\"><link href=\"#{ }\" property=\"og:description\"><meta content=\"article\" property=\"og:type\"><meta content=\"summary\" property=\"twitter:card\"><link href=\"#{
post.url post.url
}\" rel=\"canonical\"><meta content=\"summary_large_image\" property=\"twitter:card\"><script type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"Article\",\"author\":{\"@type\":\"Organization\",\"name\":\"#{ }\" rel=\"canonical\"><meta content=\"#{post.picture.file.url}\" property=\"og:image\"><meta content=\"summary_large_image\" property=\"twitter:card\"><script type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"Article\",\"author\":{\"@type\":\"Organization\",\"name\":\"#{
post.attributed_to.preferred_username post.attributed_to.preferred_username
}\"},\"dateModified\":\"#{DateTime.to_iso8601(post.updated_at)}\",\"datePublished\":\"#{ }\"},\"dateModified\":\"#{DateTime.to_iso8601(post.updated_at)}\",\"datePublished\":\"#{
DateTime.to_iso8601(post.publish_at) DateTime.to_iso8601(post.publish_at)