From d43c9cbcf47367f2bdd299b3f45edfe12b8fec14 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Tue, 3 Dec 2019 20:56:39 -0500 Subject: [PATCH] add opengraph meta --- .../controllers/api/json/oembed_controller.ex | 2 +- .../templates/layout/_opengraph.html.slime | 41 +++++++++++++++++++ .../templates/layout/app.html.slime | 1 + lib/philomena_web/views/image_view.ex | 9 ++-- lib/philomena_web/views/layout_view.ex | 5 +++ 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 lib/philomena_web/templates/layout/_opengraph.html.slime diff --git a/lib/philomena_web/controllers/api/json/oembed_controller.ex b/lib/philomena_web/controllers/api/json/oembed_controller.ex index a10e7244..e694e927 100644 --- a/lib/philomena_web/controllers/api/json/oembed_controller.ex +++ b/lib/philomena_web/controllers/api/json/oembed_controller.ex @@ -67,6 +67,6 @@ defmodule PhilomenaWeb.Api.Json.OembedController do defp artist_tags(tags) do tags |> Enum.filter(& &1.namespace == "artist") - |> Enum.map_join(", ", & &1.name) + |> Enum.map_join(", ", & &1.name_in_namespace) end end \ No newline at end of file diff --git a/lib/philomena_web/templates/layout/_opengraph.html.slime b/lib/philomena_web/templates/layout/_opengraph.html.slime new file mode 100644 index 00000000..11a869c1 --- /dev/null +++ b/lib/philomena_web/templates/layout/_opengraph.html.slime @@ -0,0 +1,41 @@ +meta name="generator" content="philomena" +meta name="theme-color" +meta name="format-detection" content="telephone=no" +meta name="robots" content="noindex, nofollow" + += if !is_nil(@conn.assigns.image) and @conn.assigns.image.__meta__.state == :loaded do + - image = @conn.assigns.image + - filtered = ImageView.filter_or_spoiler_hits?(@conn, image) + + meta name="description" content="##{image.id} - Derpibooru" + meta name="keywords" content=image.tag_list_cache + meta name="og:title" content="##{image.id} - Derpibooru" + meta name="og:url" content=Routes.image_url(@conn, :show, image) + + = for tag <- artist_tags(image.tags) do + meta property="dc:creator" content=tag.name_in_namespace + + = if image.source_url not in [nil, ""] do + meta propery="foaf:primaryTopic" content=image.source_url + + link rel="alternate" type="application/json+oembed" href=Routes.api_json_oembed_url(@conn, :index, url: Routes.image_path(@conn, :show, image)) title="oEmbed JSON Profile" + + = cond do + - image.image_mime_type == "video/webm" and !filtered -> + meta name="og:type" content="video.other" + meta name="og:image" content=ImageView.thumb_url(image, false, :rendered) + meta name="og:video" content=Imageview.thumb_url(image, false, :full) + + - image.image_mime_type == "image/svg+xml" and !filtered -> + meta name="og:type" content="video.other" + meta name="og:image" content=ImageView.thumb_url(image, false, :rendered) + + - !filtered -> + meta name="og:type" content="website" + meta name="og:image" content=ImageView.thumb_url(image, false, :full) + + - true -> + meta name="og:type" content="website" + +- else + meta name="description" content="Derpibooru is a linear imagebooru which lets you share, find and discover new art and media surrounding the show My Little Pony: Friendship is Magic" \ No newline at end of file diff --git a/lib/philomena_web/templates/layout/app.html.slime b/lib/philomena_web/templates/layout/app.html.slime index 9f60cc95..042600b3 100644 --- a/lib/philomena_web/templates/layout/app.html.slime +++ b/lib/philomena_web/templates/layout/app.html.slime @@ -19,6 +19,7 @@ html lang="en" meta name="robots" content="noindex, nofollow" = csrf_meta_tag() script type="text/javascript" src=Routes.static_path(@conn, "/js/app.js") async="async" + = render PhilomenaWeb.LayoutView, "_opengraph.html", assigns body data-theme="default" = render PhilomenaWeb.LayoutView, "_burger.html", assigns #container class=container_class(@current_user) diff --git a/lib/philomena_web/views/image_view.ex b/lib/philomena_web/views/image_view.ex index 0e435496..0a9c136d 100644 --- a/lib/philomena_web/views/image_view.ex +++ b/lib/philomena_web/views/image_view.ex @@ -72,7 +72,7 @@ defmodule PhilomenaWeb.ImageView do format = image.image_format |> String.downcase() - |> thumb_format() + |> thumb_format(name) id_fragment = if deleted and show_hidden do @@ -93,7 +93,7 @@ defmodule PhilomenaWeb.ImageView do format = image.image_format |> String.downcase() - |> thumb_format() + |> thumb_format(nil) "#{root}/#{view}/#{year}/#{month}/#{day}/#{filename}.#{format}" end @@ -136,8 +136,9 @@ defmodule PhilomenaWeb.ImageView do conn.assigns.current_user.anonymous_by_default end - defp thumb_format("svg"), do: "png" - defp thumb_format(format), do: format + defp thumb_format("svg", _name), do: "png" + defp thumb_format(_, :rendered), do: "png" + defp thumb_format(format, _name), do: format defp image_filter_data(image) do %{ diff --git a/lib/philomena_web/views/layout_view.ex b/lib/philomena_web/views/layout_view.ex index ca244656..05e37ea0 100644 --- a/lib/philomena_web/views/layout_view.ex +++ b/lib/philomena_web/views/layout_view.ex @@ -1,6 +1,8 @@ defmodule PhilomenaWeb.LayoutView do use PhilomenaWeb, :view + alias PhilomenaWeb.ImageView + def layout_class(conn) do conn.assigns[:layout_class] || "layout--narrow" end @@ -71,4 +73,7 @@ defmodule PhilomenaWeb.LayoutView do def stylesheet_path(conn, _user), do: Routes.static_path(conn, "/css/default.css") + + def artist_tags(tags), + do: Enum.filter(tags, & &1.namespace == "artist") end