Merge remote-tracking branch 'upstream/pull/3523'

This commit is contained in:
Tom Hughes 2022-04-12 17:12:16 +01:00
commit 9c0582f88f
9 changed files with 41 additions and 54 deletions

View file

@ -21,19 +21,6 @@ require:
Layout/LineLength: Layout/LineLength:
Max: 270 Max: 270
# Offense count: 62
# Cop supports --auto-correct.
Lint/AmbiguousOperatorPrecedence:
Exclude:
- 'app/controllers/geocoder_controller.rb'
- 'app/models/user.rb'
- 'lib/bounding_box.rb'
- 'lib/osm.rb'
- 'lib/rich_text.rb'
- 'lib/short_link.rb'
- 'test/controllers/api/old_nodes_controller_test.rb'
- 'test/lib/short_link_test.rb'
# Offense count: 34 # Offense count: 34
# Configuration parameters: AllowSafeAssignment. # Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition: Lint/AssignmentInCondition:

View file

@ -327,11 +327,11 @@ class GeocoderController < ApplicationController
def ddm_to_decdeg(captures) def ddm_to_decdeg(captures)
begin begin
Float(captures[0]) Float(captures[0])
lat = captures[3].casecmp("s").zero? ? -(captures[0].to_f + captures[1].to_f / 60) : captures[0].to_f + captures[1].to_f / 60 lat = captures[3].casecmp("s").zero? ? -(captures[0].to_f + (captures[1].to_f / 60)) : captures[0].to_f + (captures[1].to_f / 60)
lon = captures[7].casecmp("w").zero? ? -(captures[4].to_f + captures[5].to_f / 60) : captures[4].to_f + captures[5].to_f / 60 lon = captures[7].casecmp("w").zero? ? -(captures[4].to_f + (captures[5].to_f / 60)) : captures[4].to_f + (captures[5].to_f / 60)
rescue StandardError rescue StandardError
lat = captures[0].casecmp("s").zero? ? -(captures[1].to_f + captures[2].to_f / 60) : captures[1].to_f + captures[2].to_f / 60 lat = captures[0].casecmp("s").zero? ? -(captures[1].to_f + (captures[2].to_f / 60)) : captures[1].to_f + (captures[2].to_f / 60)
lon = captures[4].casecmp("w").zero? ? -(captures[5].to_f + captures[6].to_f / 60) : captures[5].to_f + captures[6].to_f / 60 lon = captures[4].casecmp("w").zero? ? -(captures[5].to_f + (captures[6].to_f / 60)) : captures[5].to_f + (captures[6].to_f / 60)
end end
{ :lat => lat, :lon => lon } { :lat => lat, :lon => lon }
end end
@ -339,11 +339,11 @@ class GeocoderController < ApplicationController
def dms_to_decdeg(captures) def dms_to_decdeg(captures)
begin begin
Float(captures[0]) Float(captures[0])
lat = captures[4].casecmp("s").zero? ? -(captures[0].to_f + (captures[1].to_f + captures[2].to_f / 60) / 60) : captures[0].to_f + (captures[1].to_f + captures[2].to_f / 60) / 60 lat = captures[4].casecmp("s").zero? ? -(captures[0].to_f + ((captures[1].to_f + (captures[2].to_f / 60)) / 60)) : captures[0].to_f + ((captures[1].to_f + (captures[2].to_f / 60)) / 60)
lon = captures[9].casecmp("w").zero? ? -(captures[5].to_f + (captures[6].to_f + captures[7].to_f / 60) / 60) : captures[5].to_f + (captures[6].to_f + captures[7].to_f / 60) / 60 lon = captures[9].casecmp("w").zero? ? -(captures[5].to_f + ((captures[6].to_f + (captures[7].to_f / 60)) / 60)) : captures[5].to_f + ((captures[6].to_f + (captures[7].to_f / 60)) / 60)
rescue StandardError rescue StandardError
lat = captures[0].casecmp("s").zero? ? -(captures[1].to_f + (captures[2].to_f + captures[3].to_f / 60) / 60) : captures[1].to_f + (captures[2].to_f + captures[3].to_f / 60) / 60 lat = captures[0].casecmp("s").zero? ? -(captures[1].to_f + ((captures[2].to_f + (captures[3].to_f / 60)) / 60)) : captures[1].to_f + ((captures[2].to_f + (captures[3].to_f / 60)) / 60)
lon = captures[5].casecmp("w").zero? ? -(captures[6].to_f + (captures[7].to_f + captures[8].to_f / 60) / 60) : captures[6].to_f + (captures[7].to_f + captures[8].to_f / 60) / 60 lon = captures[5].casecmp("w").zero? ? -(captures[6].to_f + ((captures[7].to_f + (captures[8].to_f / 60)) / 60)) : captures[6].to_f + ((captures[7].to_f + (captures[8].to_f / 60)) / 60)
end end
{ :lat => lat, :lon => lon } { :lat => lat, :lon => lon }
end end

View file

@ -370,7 +370,7 @@ class User < ApplicationRecord
account_age_in_hours = account_age_in_seconds / 3600 account_age_in_hours = account_age_in_seconds / 3600
recent_messages = messages.where("sent_on >= ?", Time.now.utc - 3600).count recent_messages = messages.where("sent_on >= ?", Time.now.utc - 3600).count
active_reports = issues.with_status(:open).sum(:reports_count) active_reports = issues.with_status(:open).sum(:reports_count)
max_messages = account_age_in_hours.ceil + recent_messages - active_reports * 10 max_messages = account_age_in_hours.ceil + recent_messages - (active_reports * 10)
max_messages.clamp(0, Settings.max_messages_per_hour) max_messages.clamp(0, Settings.max_messages_per_hour)
end end
@ -379,7 +379,7 @@ class User < ApplicationRecord
account_age_in_hours = account_age_in_seconds / 3600 account_age_in_hours = account_age_in_seconds / 3600
recent_friends = Friendship.where(:befriendee => self).where("created_at >= ?", Time.now.utc - 3600).count recent_friends = Friendship.where(:befriendee => self).where("created_at >= ?", Time.now.utc - 3600).count
active_reports = issues.with_status(:open).sum(:reports_count) active_reports = issues.with_status(:open).sum(:reports_count)
max_friends = account_age_in_hours.ceil + recent_friends - active_reports * 10 max_friends = account_age_in_hours.ceil + recent_friends - (active_reports * 10)
max_friends.clamp(0, Settings.max_friends_per_hour) max_friends.clamp(0, Settings.max_friends_per_hour)
end end

View file

@ -39,19 +39,19 @@ class BoundingBox
if bbox.complete? if bbox.complete?
if bbox.min_lon < min_lon if bbox.min_lon < min_lon
@min_lon = [-SCALED_LON_LIMIT, @min_lon = [-SCALED_LON_LIMIT,
bbox.min_lon + margin * (min_lon - max_lon)].max bbox.min_lon + (margin * (min_lon - max_lon))].max
end end
if bbox.min_lat < min_lat if bbox.min_lat < min_lat
@min_lat = [-SCALED_LAT_LIMIT, @min_lat = [-SCALED_LAT_LIMIT,
bbox.min_lat + margin * (min_lat - max_lat)].max bbox.min_lat + (margin * (min_lat - max_lat))].max
end end
if bbox.max_lon > max_lon if bbox.max_lon > max_lon
@max_lon = [+SCALED_LON_LIMIT, @max_lon = [+SCALED_LON_LIMIT,
bbox.max_lon + margin * (max_lon - min_lon)].min bbox.max_lon + (margin * (max_lon - min_lon))].min
end end
if bbox.max_lat > max_lat if bbox.max_lat > max_lat
@max_lat = [+SCALED_LAT_LIMIT, @max_lat = [+SCALED_LAT_LIMIT,
bbox.max_lat + margin * (max_lat - min_lat)].min bbox.max_lat + (margin * (max_lat - min_lat))].min
end end
end end
self self
@ -109,16 +109,16 @@ class BoundingBox
end end
def slippy_width(zoom) def slippy_width(zoom)
width * 256.0 * 2.0**zoom / 360.0 width * 256.0 * (2.0**zoom) / 360.0
end end
def slippy_height(zoom) def slippy_height(zoom)
min = min_lat * Math::PI / 180.0 min = min_lat * Math::PI / 180.0
max = max_lat * Math::PI / 180.0 max = max_lat * Math::PI / 180.0
Math.log((Math.tan(max) + 1.0 / Math.cos(max)) / Math.log((Math.tan(max) + (1.0 / Math.cos(max))) /
(Math.tan(min) + 1.0 / Math.cos(min))) * (Math.tan(min) + (1.0 / Math.cos(min)))) *
(128.0 * 2.0**zoom / Math::PI) (128.0 * (2.0**zoom) / Math::PI)
end end
# there are two forms used for bounds with and without an underscore, # there are two forms used for bounds with and without an underscore,

View file

@ -365,23 +365,23 @@ module OSM
yscale = ysize / height yscale = ysize / height
scale = [xscale, yscale].max scale = [xscale, yscale].max
xpad = width * scale - xsize xpad = (width * scale) - xsize
ypad = height * scale - ysize ypad = (height * scale) - ysize
@width = width @width = width
@height = height @height = height
@tx = xsheet(min_lon) - xpad / 2 @tx = xsheet(min_lon) - (xpad / 2)
@ty = ysheet(min_lat) - ypad / 2 @ty = ysheet(min_lat) - (ypad / 2)
@bx = xsheet(max_lon) + xpad / 2 @bx = xsheet(max_lon) + (xpad / 2)
@by = ysheet(max_lat) + ypad / 2 @by = ysheet(max_lat) + (ypad / 2)
end end
# the following two functions will give you the x/y on the entire sheet # the following two functions will give you the x/y on the entire sheet
def ysheet(lat) def ysheet(lat)
log(tan(PI / 4 + (lat * PI / 180 / 2))) / (PI / 180) log(tan((PI / 4) + (lat * PI / 180 / 2))) / (PI / 180)
end end
def xsheet(lon) def xsheet(lon)
@ -417,7 +417,7 @@ module OSM
def distance(lat, lon) def distance(lat, lon)
lat = lat * PI / 180 lat = lat * PI / 180
lon = lon * PI / 180 lon = lon * PI / 180
6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2)**2 + cos(@lat) * cos(lat) * sin((lon - @lon) / 2)**2)) 6372.795 * 2 * asin(sqrt((sin((lat - @lat) / 2)**2) + (cos(@lat) * cos(lat) * (sin((lon - @lon) / 2)**2))))
end end
# get the worst case bounds for a given radius from the base position # get the worst case bounds for a given radius from the base position
@ -425,7 +425,7 @@ module OSM
latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2)**2)) latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2)**2))
begin begin
lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2)**2 / cos(@lat)**2)) lonradius = 2 * asin(sqrt((sin(radius / 6372.795 / 2)**2) / (cos(@lat)**2)))
rescue Errno::EDOM, Math::DomainError rescue Errno::EDOM, Math::DomainError
lonradius = PI lonradius = PI
end end

View file

@ -44,9 +44,9 @@ module RichText
doc.content.include?(phrase) doc.content.include?(phrase)
end end
[link_proportion - 0.2, 0.0].max * 200 + ([link_proportion - 0.2, 0.0].max * 200) +
link_count * 40 + (link_count * 40) +
spammy_phrases * 40 (spammy_phrases * 40)
end end
protected protected

View file

@ -47,21 +47,21 @@ module ShortLink
y <<= (32 - z) y <<= (32 - z)
# project the parameters back to their coordinate ranges. # project the parameters back to their coordinate ranges.
[(x * 360.0 / 2**32) - 180.0, [(x * 360.0 / (2**32)) - 180.0,
(y * 180.0 / 2**32) - 90.0, (y * 180.0 / (2**32)) - 90.0,
z - 8 - (z_offset % 3)] z - 8 - (z_offset % 3)]
end end
## ##
# given a location and zoom, return a short string representing it. # given a location and zoom, return a short string representing it.
def encode(lon, lat, z) def encode(lon, lat, z)
code = interleave_bits(((lon + 180.0) * 2**32 / 360.0).to_i, code = interleave_bits(((lon + 180.0) * (2**32) / 360.0).to_i,
((lat + 90.0) * 2**32 / 180.0).to_i) ((lat + 90.0) * (2**32) / 180.0).to_i)
str = "" str = ""
# add eight to the zoom level, which approximates an accuracy of # add eight to the zoom level, which approximates an accuracy of
# one pixel in a tile. # one pixel in a tile.
((z + 8) / 3.0).ceil.times do |i| ((z + 8) / 3.0).ceil.times do |i|
digit = (code >> (58 - 6 * i)) & 0x3f digit = (code >> (58 - (6 * i))) & 0x3f
str << ARRAY[digit] str << ARRAY[digit]
end end
# append characters onto the end of the string to represent # append characters onto the end of the string to represent

View file

@ -65,8 +65,8 @@ module Api
# randomly move the node about # randomly move the node about
3.times do 3.times do
# move the node somewhere else # move the node somewhere else
xml_node["lat"] = precision(rand * 180 - 90).to_s xml_node["lat"] = precision((rand * 180) - 90).to_s
xml_node["lon"] = precision(rand * 360 - 180).to_s xml_node["lon"] = precision((rand * 360) - 180).to_s
with_controller(NodesController.new) do with_controller(NodesController.new) do
put api_node_path(:id => nodeid), :params => xml_doc.to_s, :headers => auth_header put api_node_path(:id => nodeid), :params => xml_doc.to_s, :headers => auth_header
assert_response :forbidden, "Should have rejected node update" assert_response :forbidden, "Should have rejected node update"
@ -113,8 +113,8 @@ module Api
# randomly move the node about # randomly move the node about
3.times do 3.times do
# move the node somewhere else # move the node somewhere else
xml_node["lat"] = precision(rand * 180 - 90).to_s xml_node["lat"] = precision((rand * 180) - 90).to_s
xml_node["lon"] = precision(rand * 360 - 180).to_s xml_node["lon"] = precision((rand * 360) - 180).to_s
with_controller(NodesController.new) do with_controller(NodesController.new) do
put api_node_path(:id => nodeid), :params => xml_doc.to_s, :headers => auth_header put api_node_path(:id => nodeid), :params => xml_doc.to_s, :headers => auth_header
assert_response :success assert_response :success

View file

@ -7,7 +7,7 @@ class ShortLinkTest < ActiveSupport::TestCase
def test_encode_decode def test_encode_decode
cases = [] cases = []
1000.times do 1000.times do
cases << [180.0 * rand - 90.0, 360.0 * rand - 180.0, (18 * rand).to_i] cases << [(180.0 * rand) - 90.0, (360.0 * rand) - 180.0, (18 * rand).to_i]
end end
cases.each do |lat, lon, zoom| cases.each do |lat, lon, zoom|
@ -18,7 +18,7 @@ class ShortLinkTest < ActiveSupport::TestCase
# one pixel (i.e: zoom + 8). the sqrt(5) is because each position # one pixel (i.e: zoom + 8). the sqrt(5) is because each position
# has an extra bit of accuracy in the lat coordinate, due to the # has an extra bit of accuracy in the lat coordinate, due to the
# smaller range. # smaller range.
distance = Math.sqrt((lat - lat2)**2 + (lon - lon2)**2) distance = Math.sqrt(((lat - lat2)**2) + ((lon - lon2)**2))
max_distance = 360.0 / (1 << (zoom + 8)) * 0.5 * Math.sqrt(5) max_distance = 360.0 / (1 << (zoom + 8)) * 0.5 * Math.sqrt(5)
assert max_distance > distance, "Maximum expected error exceeded: #{max_distance} <= #{distance} for (#{lat}, #{lon}, #{zoom})." assert max_distance > distance, "Maximum expected error exceeded: #{max_distance} <= #{distance} for (#{lat}, #{lon}, #{zoom})."
end end