From: Anton Khorev Date: Thu, 6 Jun 2024 14:28:57 +0000 (+0300) Subject: Set Open Graph image to first image for diary entries X-Git-Tag: live~448^2~1 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/a73e20cd5c71afbc7405923b165d3f698a9b6897?ds=inline;hp=-c Set Open Graph image to first image for diary entries --- a73e20cd5c71afbc7405923b165d3f698a9b6897 diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index 9af36709e..f3fbcd1fd 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -68,6 +68,7 @@ class DiaryEntriesController < ApplicationController @entry = entries.find_by(:id => params[:id]) if @entry @title = t ".title", :user => params[:display_name], :title => @entry.title + @og_image = @entry.body.image @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments else @title = t "diary_entries.no_such_entry.title", :id => params[:id] diff --git a/app/helpers/open_graph_helper.rb b/app/helpers/open_graph_helper.rb index 27d8d2451..89aeee6b9 100644 --- a/app/helpers/open_graph_helper.rb +++ b/app/helpers/open_graph_helper.rb @@ -1,10 +1,10 @@ module OpenGraphHelper - def opengraph_tags(title = nil) + def opengraph_tags(title = nil, og_image = nil) tags = { "og:site_name" => t("layouts.project_name.title"), "og:title" => [title, t("layouts.project_name.title")].compact.join(" | "), "og:type" => "website", - "og:image" => image_url("osm_logo_256.png"), + "og:image" => og_image || image_url("osm_logo_256.png"), "og:url" => url_for(:only_path => false), "og:description" => t("layouts.intro_text") } diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index e83dfb9ee..089c7e6c6 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -50,7 +50,7 @@ class DiaryEntry < ApplicationRecord after_save :spam_check def body - RichText.new(self[:body_format], self[:body]) + @body ||= RichText.new(self[:body_format], self[:body]) end private diff --git a/app/views/layouts/_meta.html.erb b/app/views/layouts/_meta.html.erb index 2352ad0b6..37afbb894 100644 --- a/app/views/layouts/_meta.html.erb +++ b/app/views/layouts/_meta.html.erb @@ -21,7 +21,7 @@ <% end -%> <%= tag.link :rel => "search", :type => "application/opensearchdescription+xml", :title => "OpenStreetMap Search", :href => asset_path("osm.xml") %> <%= tag.meta :name => "description", :content => "OpenStreetMap is the free wiki world map." %> -<%= opengraph_tags(@title) %> +<%= opengraph_tags(@title, @og_image) %> <% if flash[:matomo_goal] -%> <%= tag.meta :name => "matomo-goal", :content => flash[:matomo_goal] %> <% end -%> diff --git a/test/controllers/diary_entries_controller_test.rb b/test/controllers/diary_entries_controller_test.rb index d13a50163..765391d24 100644 --- a/test/controllers/diary_entries_controller_test.rb +++ b/test/controllers/diary_entries_controller_test.rb @@ -752,6 +752,17 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest end end + def test_show_og_image + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:image']" do + assert_dom "> @content", "https://example.com/picture.jpg" + end + end + def test_hide user = create(:user) diary_entry = create(:diary_entry, :user => user)