]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/5072'
authorTom Hughes <tom@compton.nu>
Wed, 14 Aug 2024 18:12:59 +0000 (19:12 +0100)
committerTom Hughes <tom@compton.nu>
Wed, 14 Aug 2024 18:12:59 +0000 (19:12 +0100)
14 files changed:
.github/workflows/tests.yml
Gemfile
Gemfile.lock
app/assets/javascripts/richtext.js
app/controllers/diary_entries_controller.rb
app/helpers/open_graph_helper.rb
app/views/layouts/_meta.html.erb
config/initializers/gd2.rb [new file with mode: 0644]
lib/rich_text.rb
test/controllers/api/nodes_controller_test.rb
test/controllers/diary_entries_controller_test.rb
test/lib/rich_text_test.rb
test/system/rich_text_test.rb [new file with mode: 0644]
test/system/user_blocks_test.rb

index 34405f778d0672bf5a350c16e135211d3df67e0d..46ab75482815eb90b5d9aa23f7a8ac8e4418f6f5 100644 (file)
@@ -11,7 +11,7 @@ jobs:
     strategy:
       matrix:
         ubuntu: [20.04, 22.04]
-        ruby: ['3.0', '3.1', '3.2']
+        ruby: ['3.0', '3.1', '3.2', '3.3']
     runs-on: ubuntu-${{ matrix.ubuntu }}
     env:
       RAILS_ENV: test
diff --git a/Gemfile b/Gemfile
index fcf2ceb4d1df6f381344c20479b8aaf45e10b19c..dcdfbe27edf1238e53de61895b6090aba6c19d93 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -180,4 +180,7 @@ end
 
 group :development, :test do
   gem "annotate"
+
+  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
+  gem "debug", :require => "debug/prelude"
 end
index 4cb7d0729b63bfd0d3633927f202617f799bd5be..e7ec71569b3e5297ad49db2b2dd3e715f04baaaa 100644 (file)
@@ -170,6 +170,9 @@ GEM
       sprockets-rails
       tilt
     date (3.3.4)
+    debug (1.9.2)
+      irb (~> 1.10)
+      reline (>= 0.3.8)
     debug_inspector (1.2.0)
     deep_merge (1.2.2)
     delayed_job (4.1.11)
@@ -627,6 +630,7 @@ DEPENDENCIES
   connection_pool
   dalli
   dartsass-sprockets
+  debug
   debug_inspector
   delayed_job_active_record
   doorkeeper
index 1576656eece0a21f91457a548a5c2b4279dc52f7..e069f6f88007cbfc4d732cac5ac08116b18bab49 100644 (file)
@@ -8,6 +8,10 @@ $(document).ready(function () {
     var container = $(this).closest(".richtext_container");
 
     container.find(".tab-pane[id$='_preview']").empty();
+  }).on("invalid", function () {
+    var container = $(this).closest(".richtext_container");
+
+    container.find("button[data-bs-target$='_edit']").tab("show");
   });
 
   /*
index 9962c7efd5f019f0f333bc6174931a7f8791276e..dd7efc7cacf3c359e21a28d32aed1b19357b6674 100644 (file)
@@ -68,8 +68,12 @@ 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
-      @og_image_alt = @entry.body.image_alt
+      @opengraph_properties = {
+        "og:image" => @entry.body.image,
+        "og:image:alt" => @entry.body.image_alt,
+        "og:description" => @entry.body.description,
+        "article:published_time" => @entry.created_at.xmlschema
+      }
       @comments = can?(:unhide, DiaryComment) ? @entry.comments : @entry.visible_comments
     else
       @title = t "diary_entries.no_such_entry.title", :id => params[:id]
index ad24c73b2fb3c2115d3b0a19b4c3809f18cd0638..cde848e5d13762ac718431632aaf44c597c3bf54 100644 (file)
@@ -1,15 +1,17 @@
 module OpenGraphHelper
   require "addressable/uri"
 
-  def opengraph_tags(title = nil, og_image = nil, og_image_alt = nil)
+  def opengraph_tags(title, properties)
     tags = {
       "og:site_name" => t("layouts.project_name.title"),
       "og:title" => title || t("layouts.project_name.title"),
       "og:type" => "website",
       "og:url" => url_for(:only_path => false),
-      "og:description" => t("layouts.intro_text")
+      "og:description" => properties["og:description"] || t("layouts.intro_text")
     }.merge(
-      opengraph_image_properties(og_image, og_image_alt)
+      opengraph_image_properties(properties)
+    ).merge(
+      properties.slice("article:published_time")
     )
 
     safe_join(tags.map do |property, content|
@@ -19,13 +21,13 @@ module OpenGraphHelper
 
   private
 
-  def opengraph_image_properties(og_image, og_image_alt)
+  def opengraph_image_properties(properties)
     begin
-      if og_image
-        properties = {}
-        properties["og:image"] = Addressable::URI.join(root_url, og_image).normalize
-        properties["og:image:alt"] = og_image_alt if og_image_alt
-        return properties
+      if properties["og:image"]
+        image_properties = {}
+        image_properties["og:image"] = Addressable::URI.join(root_url, properties["og:image"]).normalize
+        image_properties["og:image:alt"] = properties["og:image:alt"] if properties["og:image:alt"]
+        return image_properties
       end
     rescue Addressable::URI::InvalidURIError
       # return default image
index f09449761a4990c9226fb17c4b9cf64e4fc0d533..790d13fc380b03161f8ee59c0ed679559a68b31d 100644 (file)
@@ -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, @og_image, @og_image_alt) %>
+<%= opengraph_tags(@title, @opengraph_properties || {}) %>
 <% if flash[:matomo_goal] -%>
 <%= tag.meta :name => "matomo-goal", :content => flash[:matomo_goal] %>
 <% end -%>
diff --git a/config/initializers/gd2.rb b/config/initializers/gd2.rb
new file mode 100644 (file)
index 0000000..0005bd8
--- /dev/null
@@ -0,0 +1,15 @@
+module OpenStreetMap
+  module GD2
+    module AnimatedGif
+      def frames_finalizer
+        proc do
+          @frames.each do |frame|
+            ::GD2::GD2FFI.send(:gdFree, frame.ptr)
+          end
+        end
+      end
+    end
+  end
+end
+
+GD2::AnimatedGif.prepend(OpenStreetMap::GD2::AnimatedGif)
index a439342f7743f9b1fb8682147f6562a9ff0750ab..bdf9c37ca7359eec1f2cea57c2243178d8ce7791 100644 (file)
@@ -3,6 +3,8 @@ module RichText
     "Business Description:", "Additional Keywords:"
   ].freeze
 
+  MAX_DESCRIPTION_LENGTH = 500
+
   def self.new(format, text)
     case format
     when "html" then HTML.new(text || "")
@@ -57,6 +59,10 @@ module RichText
       nil
     end
 
+    def description
+      nil
+    end
+
     protected
 
     def simple_format(text)
@@ -105,6 +111,12 @@ module RichText
       @image_element.attr["alt"] if @image_element
     end
 
+    def description
+      return @description if defined? @description
+
+      @description = first_truncated_text_content(document.root)
+    end
+
     private
 
     def document
@@ -120,9 +132,44 @@ module RichText
       end
     end
 
+    def first_truncated_text_content(element)
+      if paragraph?(element)
+        truncated_text_content(element)
+      else
+        element.children.find do |child|
+          text = first_truncated_text_content(child)
+          break text unless text.nil?
+        end
+      end
+    end
+
+    def truncated_text_content(element)
+      text = ""
+
+      append_text = lambda do |child|
+        if child.type == :text
+          text << child.value
+        else
+          child.children.each do |c|
+            append_text.call(c)
+            break if text.length > MAX_DESCRIPTION_LENGTH
+          end
+        end
+      end
+      append_text.call(element)
+
+      return nil if text.blank?
+
+      text.truncate(MAX_DESCRIPTION_LENGTH)
+    end
+
     def image?(element)
       element.type == :img || (element.type == :html_element && element.value == "img")
     end
+
+    def paragraph?(element)
+      element.type == :p || (element.type == :html_element && element.value == "p")
+    end
   end
 
   class Text < Base
index d70c92861e9b563f7df5eab1fb933dfce1ead943..d9fabb012e18b21d0152cb2653e0a7f37e9dc470 100644 (file)
@@ -639,12 +639,5 @@ module Api
       xml.find("//osm/node").first[name] = value.to_s
       xml
     end
-
-    ##
-    # parse some xml
-    def xml_parse(xml)
-      parser = XML::Parser.string(xml)
-      parser.parse
-    end
   end
 end
index ced241a7f8456072532b3906986565831b070fec..aad759b5b68e3ebae15e20e60df50b27cf30814f 100644 (file)
@@ -744,6 +744,39 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
     assert_dom "head meta[property='og:image:alt']", :count => 0
   end
 
+  def test_show_no_og_description
+    user = create(:user)
+    diary_entry = create(:diary_entry, :user => user, :body => "![nope](https://example.com/nope.jpg)")
+
+    get diary_entry_path(user, diary_entry)
+    assert_response :success
+    assert_dom "head meta[property='og:description']" do
+      assert_dom "> @content", I18n.t("layouts.intro_text")
+    end
+  end
+
+  def test_show_og_description
+    user = create(:user)
+    diary_entry = create(:diary_entry, :user => user, :body => "# Hello\n\n![hello](https://example.com/hello.jpg)\n\nFirst paragraph.\n\nSecond paragraph.")
+
+    get diary_entry_path(user, diary_entry)
+    assert_response :success
+    assert_dom "head meta[property='og:description']" do
+      assert_dom "> @content", "First paragraph."
+    end
+  end
+
+  def test_show_article_published_time
+    user = create(:user)
+    diary_entry = create(:diary_entry, :user => user, :created_at => "2020-03-04")
+
+    get diary_entry_path(user, diary_entry)
+    assert_response :success
+    assert_dom "head meta[property='article:published_time']" do
+      assert_dom "> @content", "2020-03-04T00:00:00Z"
+    end
+  end
+
   def test_hide
     user = create(:user)
     diary_entry = create(:diary_entry, :user => user)
index e0b31527669d5f1d8583cdfbe65c483d58a6ec58..63c70b0996f59520e9cde2d4702c5f1a3b16f064 100644 (file)
@@ -250,16 +250,18 @@ class RichTextTest < ActiveSupport::TestCase
     assert_equal 141, r.spam_score.round
   end
 
-  def test_text_no_image
+  def test_text_no_opengraph_properties
     r = RichText.new("text", "foo https://example.com/ bar")
     assert_nil r.image
     assert_nil r.image_alt
+    assert_nil r.description
   end
 
-  def test_html_no_image
+  def test_html_no_opengraph_properties
     r = RichText.new("html", "foo <a href='https://example.com/'>bar</a> baz")
     assert_nil r.image
     assert_nil r.image_alt
+    assert_nil r.description
   end
 
   def test_markdown_no_image
@@ -328,6 +330,52 @@ class RichTextTest < ActiveSupport::TestCase
     assert_equal "have src", r.image_alt
   end
 
+  def test_markdown_no_description
+    r = RichText.new("markdown", "#Nope")
+    assert_nil r.description
+  end
+
+  def test_markdown_description
+    r = RichText.new("markdown", "This is an article about something.")
+    assert_equal "This is an article about something.", r.description
+  end
+
+  def test_markdown_description_after_heading
+    r = RichText.new("markdown", "#Heading\n\nHere starts the text.")
+    assert_equal "Here starts the text.", r.description
+  end
+
+  def test_markdown_description_after_image
+    r = RichText.new("markdown", "![bar](https://example.com/image.jpg)\n\nThis is below the image.")
+    assert_equal "This is below the image.", r.description
+  end
+
+  def test_markdown_description_only_first_paragraph
+    r = RichText.new("markdown", "This thing.\n\nMaybe also that thing.")
+    assert_equal "This thing.", r.description
+  end
+
+  def test_markdown_description_elements
+    r = RichText.new("markdown", "*Something* **important** [here](https://example.com/).")
+    assert_equal "Something important here.", r.description
+  end
+
+  def test_markdown_html_description
+    r = RichText.new("markdown", "<p>Can use HTML tags.</p>")
+    assert_equal "Can use HTML tags.", r.description
+  end
+
+  def test_markdown_description_max_length
+    r = RichText.new("markdown", "x" * RichText::MAX_DESCRIPTION_LENGTH)
+    assert_equal "x" * RichText::MAX_DESCRIPTION_LENGTH, r.description
+
+    r = RichText.new("markdown", "y" * (RichText::MAX_DESCRIPTION_LENGTH + 1))
+    assert_equal "#{'y' * (RichText::MAX_DESCRIPTION_LENGTH - 3)}...", r.description
+
+    r = RichText.new("markdown", "*zzzzzzzzz*z" * ((RichText::MAX_DESCRIPTION_LENGTH + 1) / 10.0).ceil)
+    assert_equal "#{'z' * (RichText::MAX_DESCRIPTION_LENGTH - 3)}...", r.description
+  end
+
   private
 
   def assert_html(richtext, &block)
diff --git a/test/system/rich_text_test.rb b/test/system/rich_text_test.rb
new file mode 100644 (file)
index 0000000..6876718
--- /dev/null
@@ -0,0 +1,16 @@
+require "application_system_test_case"
+
+class RichTextSystemTest < ApplicationSystemTestCase
+  def setup
+    create(:language, :code => "en")
+  end
+
+  test "switches to edit pane on validation failure" do
+    sign_in_as create(:user)
+    visit new_diary_entry_path
+    fill_in "Subject", :with => "My Diary Entry Title"
+    click_on "Preview"
+    click_on "Publish"
+    assert_field "Body"
+  end
+end
index f710f74aae655cbcce4b345f6cc14705f93bbcb7..6e4f5b471141548a50d7094c088843c022352b62 100644 (file)
@@ -1,6 +1,6 @@
 require "application_system_test_case"
 
-class ReportNoteTest < ApplicationSystemTestCase
+class UserBlocksSystemTest < ApplicationSystemTestCase
   test "revoke all link is absent for anonymous users when viewed user has active blocks" do
     blocked_user = create(:user)
     create(:user_block, :user => blocked_user)