From 8b67a534a071400414e3defe7518010215699d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Wed, 1 Mar 2023 12:20:38 +0100 Subject: [PATCH] FIX: Allow floats for zoom level in Google Maps onebox MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sometimes we get Maps URL containing a zoom level as a float (17.5z and not 17z) but this doesn’t work with our current onebox implementation. While Google accepts those float zoom levels, it removes automatically the floating part in the URL (thus when visiting a Maps URL containing 17.5z, the URL will be rewritten shortly after as 17z). When putting a float zoom level in an embedded URL, this actually breaks (Maps API returns a 400 error). This patch addresses the issue by allowing the onebox engine to match on a zoom level expressed as a float but we only keep the integer part thus rendering properly maps. --- lib/onebox/engine/google_maps_onebox.rb | 2 +- spec/lib/onebox/engine/google_maps_onebox_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/onebox/engine/google_maps_onebox.rb b/lib/onebox/engine/google_maps_onebox.rb index 828c7fd5dc..cd3072a93a 100644 --- a/lib/onebox/engine/google_maps_onebox.rb +++ b/lib/onebox/engine/google_maps_onebox.rb @@ -96,7 +96,7 @@ module Onebox # Fallback for map URLs that don't resolve into an easily embeddable old-style URI # Roadmaps use a "z" zoomlevel, satellite maps use "m" the horizontal width in meters # TODO: tilted satellite maps using "a,y,t" - match = @url.match(/@(?[\d.-]+),(?[\d.-]+),(?\d+)(?[mz])/) + match = @url.match(/@(?[\d.-]+),(?[\d.-]+),(?\d+)(\.\d+)?(?[mz])/) raise "unexpected standard url #{@url}" unless match zoom = match[:mz] == "z" ? match[:zoom] : Math.log2(57280048.0 / match[:zoom].to_f).round location = "#{match[:lon]},#{match[:lat]}" diff --git a/spec/lib/onebox/engine/google_maps_onebox_spec.rb b/spec/lib/onebox/engine/google_maps_onebox_spec.rb index 498d4289f4..4ccecf62ef 100644 --- a/spec/lib/onebox/engine/google_maps_onebox_spec.rb +++ b/spec/lib/onebox/engine/google_maps_onebox_spec.rb @@ -34,10 +34,10 @@ RSpec.describe Onebox::Engine::GoogleMapsOnebox do }, unresolveable: { test: - "https://www.google.com/maps/place/Den+Abattoir/@51.2285173,4.4336702,17z/data=!4m7!1m4!3m3!1s0x47c3f7a5ac48e237:0x63d716018f584a33!2zUGnDqXRyYWlu!3b1!3m1!1s0x0000000000000000:0xfbfac0c41c32471a", + "https://www.google.com/maps/place/Den+Abattoir/@51.2285173,4.4336702,17.5z/data=!4m7!1m4!3m3!1s0x47c3f7a5ac48e237:0x63d716018f584a33!2zUGnDqXRyYWlu!3b1!3m1!1s0x0000000000000000:0xfbfac0c41c32471a", redirect: [ 302, - "https://www.google.com/maps/place/Den+Abattoir/@51.2285173,4.4336702,17z/data=!4m7!1m4!3m3!1s0x47c3f7a5ac48e237:0x63d716018f584a33!2zUGnDqXRyYWlu!3b1!3m1!1s0x0000000000000000:0xfbfac0c41c32471a?dg=dbrw&newdg=1", + "https://www.google.com/maps/place/Den+Abattoir/@51.2285173,4.4336702,17.5z/data=!4m7!1m4!3m3!1s0x47c3f7a5ac48e237:0x63d716018f584a33!2zUGnDqXRyYWlu!3b1!3m1!1s0x0000000000000000:0xfbfac0c41c32471a?dg=dbrw&newdg=1", ], expect: "https://maps.google.com/maps?ll=51.2285173,4.4336702&z=17&output=embed&dg=ntvb&q=Den+Abattoir&cid=18157036796216755994",