diff --git a/lib/service/metadata/post.ex b/lib/service/metadata/post.ex index 6553b78f..db3486eb 100644 --- a/lib/service/metadata/post.ex +++ b/lib/service/metadata/post.ex @@ -1,6 +1,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do alias Phoenix.HTML alias Phoenix.HTML.Tag + alias Mobilizon.Medias.{File, Media} alias Mobilizon.Posts.Post alias Mobilizon.Web.JsonLD.ObjectView import Mobilizon.Service.Metadata.Utils, only: [process_description: 2, strip_tags: 1] @@ -8,15 +9,17 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do def build_tags(%Post{} = post, locale \\ "en") do post = Map.put(post, :body, process_description(post.body, locale)) - tags = [ - Tag.tag(:meta, property: "og:title", content: post.title), - 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) - ] + tags = + [ + Tag.tag(:meta, property: "og:title", content: post.title), + 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) tags ++ [ @@ -31,4 +34,17 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do |> ObjectView.render(%{post: %{post | title: strip_tags(title)}}) |> Jason.encode!() 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 diff --git a/test/service/metadata/metadata_test.exs b/test/service/metadata/metadata_test.exs index a2148258..0a747eab 100644 --- a/test/service/metadata/metadata_test.exs +++ b/test/service/metadata/metadata_test.exs @@ -120,7 +120,7 @@ defmodule Mobilizon.Service.MetadataTest do Metadata.Utils.process_description(post.body) }\" property=\"og:description\">