From 1e8d2bb724b60034a8905beeb865470c067d0bb3 Mon Sep 17 00:00:00 2001 From: Scott Comer Date: Sun, 2 Mar 2014 23:16:48 -0600 Subject: [PATCH] update user with connection address on login or reconnect; make the better test data useable; fix up tests for geo stuff --- .gitignore | 4 +- ruby/lib/jam_ruby/connection_manager.rb | 17 +++++-- ruby/lib/jam_ruby/models/jam_isp.rb | 8 ++++ .../jam_ruby/models/geo_ip_blocks_spec.rb | 38 +++++++++------ .../jam_ruby/models/geo_ip_locations_spec.rb | 44 +++++++++--------- ruby/spec/jam_ruby/models/jam_isp_spec.rb | 46 ++++++++----------- ruby/spec/spec_helper.rb | 6 +-- .../lib/jam_websockets/router.rb | 26 ++++++++++- 8 files changed, 116 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index 40e3510c7..a1b1f7199 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ */vendor/cache HTML .DS_Store -coverage/ +coverage +update2 + diff --git a/ruby/lib/jam_ruby/connection_manager.rb b/ruby/lib/jam_ruby/connection_manager.rb index 91368427c..8b21b2767 100644 --- a/ruby/lib/jam_ruby/connection_manager.rb +++ b/ruby/lib/jam_ruby/connection_manager.rb @@ -44,7 +44,7 @@ module JamRuby end # reclaim the existing connection, if ip_address is not nil then perhaps a new address as well - def reconnect(conn, reconnect_music_session_id, ip_address) + def reconnect(conn, reconnect_music_session_id, ip_address, &blk) music_session_id = nil reconnected = false @@ -55,7 +55,7 @@ module JamRuby music_session_id_expression = "(CASE WHEN music_session_id='#{reconnect_music_session_id}' THEN music_session_id ELSE NULL END)" end - if ip_address + if ip_address and !ip_address.eql?(conn.ip_address) # turn ip_address string into a number, then fetch the isp and block records and update location info addr = JamIsp.ip_to_num(ip_address) @@ -71,6 +71,7 @@ module JamRuby location = GeoIpLocations.lookup(locid) if location.nil? + # todo what's a better default location? locidispid = 0 latitude = 0.0 longitude = 0.0 @@ -87,6 +88,7 @@ module JamRuby end conn.ip_address = ip_address + conn.addr = addr conn.locidispid = locidispid conn.latitude = latitude conn.longitude = longitude @@ -94,6 +96,9 @@ module JamRuby conn.region = region conn.city = city conn.save!(validate: false) + + # we're passing all this stuff so that the user record might be updated as well... + blk.call(addr, locidispid, latitude, longitude, countrycode, region, city) unless blk.nil? end sql =< %w[instruments genres icecast_server_groups] } - DatabaseCleaner.clean_with(:truncation, {:except => %w[instruments genres icecast_server_groups] }) + DatabaseCleaner.strategy = :truncation, {:except => %w[instruments genres icecast_server_groups jamcompany jamisp geoipblocks geoipisp geoiplocations] } + DatabaseCleaner.clean_with(:truncation, {:except => %w[instruments genres icecast_server_groups jamcompany jamisp geoipblocks geoipisp geoiplocations] }) end config.before(:each) do diff --git a/websocket-gateway/lib/jam_websockets/router.rb b/websocket-gateway/lib/jam_websockets/router.rb index d41eb05e2..ab77d4e99 100644 --- a/websocket-gateway/lib/jam_websockets/router.rb +++ b/websocket-gateway/lib/jam_websockets/router.rb @@ -485,7 +485,19 @@ module JamWebsockets context = nil ConnectionManager.active_record_transaction do |connection_manager| - music_session_id, reconnected = connection_manager.reconnect(connection, reconnect_music_session_id, remote_ip) + music_session_id, reconnected = connection_manager.reconnect(connection, reconnect_music_session_id, remote_ip) do |addr, locidispid, latitude, longitude, countrycode, region, city| + # update user + user.ip_address = remote_ip + user.addr = addr + user.locidispid = locidispid + user.lat = latitude + user.lng = longitude + user.country = countrycode + user.state = region + user.city = city + user.save!(validate: false) + end + context = @client_lookup[client_id] if music_session_id.nil? # if this is a reclaim of a connection, but music_session_id comes back null, then we need to check if this connection was IN a music session before. @@ -522,7 +534,17 @@ module JamWebsockets unless connection # log this connection in the database ConnectionManager.active_record_transaction do |connection_manager| - connection_manager.create_connection(user.id, client.client_id, remote_ip) do |conn, count| + connection_manager.create_connection(user.id, client.client_id, remote_ip) do |conn, count, addr, locidispid, latitude, longitude, countrycode, region, city| + # first update the user record with the current location + user.addr = addr + user.locidispid = locidispid + user.lat = latitude + user.lng = longitude + user.country = countrycode + user.state = region + user.city = city; + user.save! + if count == 1 Notification.send_friend_update(user.id, true, conn) end