Merge branch 'develop' of bitbucket.org:jamkazam/jam-cloud into develop

This commit is contained in:
Brian Smith 2014-02-24 01:10:20 -05:00
commit 31c7cf7b45
7 changed files with 112 additions and 9 deletions

View File

@ -124,6 +124,7 @@ require "jam_ruby/models/recording_play"
require "jam_ruby/models/feed"
require "jam_ruby/models/jam_isp"
require "jam_ruby/models/geo_ip_blocks"
require "jam_ruby/models/geo_ip_locations"
include Jampb

View File

@ -56,18 +56,20 @@ module JamRuby
end
if ip_address
# todo turn ip_address string into a number, then fetch the locid and ispid and the other stuff...
# todo turn ip_address string into a number, then fetch the isp and block records
addr = JamIsp.ip_to_num(ip_address)
puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============")
#puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============")
isp = JamIsp.lookup(addr)
#puts("============= JamIsp.lookup returns #{isp.inspect} for #{addr} =============")
if isp.nil? then ispid = 0 else ispid = isp.coid end
puts("============= JamIsp.lookup returns #{ispid} for #{addr} =============")
block = GeoIpBlocks.lookup(addr)
#puts("============= GeoIpBlocks.lookup returns #{block.inspect} for #{addr} =============")
if block.nil? then locid = 0 else locid = block.locid end
puts("============= GeoIpBlocks.lookup returns #{locid} for #{addr} =============")
location = GeoIpLocations.lookup(locid)
locidispid = 0
latitude = 0.0
@ -188,18 +190,20 @@ SQL
ConnectionManager.active_record_transaction do |connection_manager|
conn = connection_manager.pg_conn
# todo turn ip_address string into a number, then fetch the locid and ispid and the other stuff...
# todo turn ip_address string into a number, then fetch the isp and block records
addr = JamIsp.ip_to_num(ip_address)
puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============")
#puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============")
isp = JamIsp.lookup(addr)
#puts("============= JamIsp.lookup returns #{isp.inspect} for #{addr} =============")
if isp.nil? then ispid = 0 else ispid = isp.coid end
puts("============= JamIsp.lookup returns #{ispid} for #{addr} =============")
block = GeoIpBlocks.lookup(addr)
#puts("============= GeoIpBlocks.lookup returns #{block.inspect} for #{addr} =============")
if block.nil? then locid = 0 else locid = block.locid end
puts("============= GeoIpBlocks.lookup returns #{locid} for #{addr} =============")
location = GeoIpLocations.lookup(locid)
locidispid = 0
latitude = 0.0

View File

@ -48,6 +48,7 @@ module ValidationMessages
INVITE_REQUIRED = "You must be invited to join this session"
FANS_CAN_NOT_JOIN = "Fans can not join this session"
CANT_JOIN_RECORDING_SESSION = "is currently recording"
CANT_JOIN_MULTIPLE_SESSIONS = 'You cannot join more than one music session'
# recordings
ALREADY_BEING_RECORDED = "already being recorded"

View File

@ -101,6 +101,17 @@ module JamRuby
if music_session.is_recording?
errors.add(:music_session, ValidationMessages::CANT_JOIN_RECORDING_SESSION)
end
unless user.admin?
num_sessions = Connection.where(:user_id => user_id)
.where(["(music_session_id IS NOT NULL) AND (aasm_state != ?)",EXPIRED_STATE.to_s])
.count
if 0 < num_sessions
errors.add(:music_session, ValidationMessages::CANT_JOIN_MULTIPLE_SESSIONS)
return false;
end
end
return true
end

View File

@ -0,0 +1,20 @@
module JamRuby
class GeoIpLocations < ActiveRecord::Base
self.table_name = 'geoiplocations'
def self.lookup(locid)
GeoIpLocations.select('locid, countrycode, region, city, latitude, longitude')
.where(:locid => locid)
.limit(1)
.first
end
def self.make_row(locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode)
c = ActiveRecord::Base.connection.raw_connection
c.prepare('blah', 'insert into geoiplocations (locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode, geog) values($1, $2, $3, $4, $5, $6, $7, $8, $9, ST_SetSRID(ST_MakePoint($7, $6), 4326)::geography)')
c.exec_prepared('blah', [locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode])
c.exec("deallocate blah")
end
end
end

View File

@ -46,7 +46,6 @@ describe ConnectionManager do
end
it "can't create two client_ids of same value" do
client_id = "client_id1"
user_id = create_user("test", "user1", "user1@jamkazam.com")
@ -425,5 +424,36 @@ describe ConnectionManager do
assert_num_connections(client_id, 0)
end
it "join_music_session fails if user has music_session already active" do
user_id = create_user("test", "user11", "user11@jamkazam.com")
user = User.find(user_id)
music_session = MusicSession.find(create_music_session(user_id))
client_id = Faker::Number.number(20)
@connman.create_connection(user_id, client_id, "1.1.1.1")
connection = @connman.join_music_session(user, client_id, music_session, true, TRACKS)
client_id = Faker::Number.number(20)
@connman.create_connection(user_id, client_id, Faker::Internet.ip_v4_address)
music_session = MusicSession.find(create_music_session(user_id))
connection = @connman.join_music_session(user, client_id, music_session, true, TRACKS)
connection.errors.size.should == 1
connection.errors.get(:music_session).should == [ValidationMessages::CANT_JOIN_MULTIPLE_SESSIONS]
user.update_attribute(:admin, true)
client_id = Faker::Number.number(20)
@connman.create_connection(user_id, client_id, "1.1.1.1")
music_session = MusicSession.find(create_music_session(user_id))
connection = @connman.join_music_session(user, client_id, music_session, true, TRACKS)
client_id = Faker::Number.number(20)
@connman.create_connection(user_id, client_id, Faker::Internet.ip_v4_address)
music_session = MusicSession.find(create_music_session(user_id))
connection = @connman.join_music_session(user, client_id, music_session, true, TRACKS)
connection.errors.size.should == 0
end
end

View File

@ -0,0 +1,36 @@
require 'spec_helper'
describe GeoIpLocations do
before do
GeoIpLocations.delete_all
GeoIpLocations.make_row(17192, 'US', 'TX', 'Austin', '78749', 30.2076, -97.8587, 635, '512')
GeoIpLocations.make_row(48086, 'MX', '28', 'Matamoros', '', 25.8833, -97.5000, nil, '')
end
it "count" do GeoIpLocations.count.should == 2 end
let(:first) { GeoIpLocations.lookup(17192) }
let(:second) { GeoIpLocations.lookup(48086) }
let(:third) { GeoIpLocations.lookup(999999) } # bogus
it "first" do
first.locid.should == 17192
first.countrycode.should eql('US')
first.region.should eql('TX')
first.city.should eql('Austin')
first.latitude.should == 30.2076
first.longitude.should == -97.8587
end
it "second" do
second.locid.should == 48086
second.countrycode.should eql('MX')
second.region.should eql('28')
second.city.should eql('Matamoros')
second.latitude.should == 25.8833
second.longitude.should == -97.5000
end
it "third" do third.should be_nil end
end