From 3e0667e24dce867654d2a2e0471d5637ffb7f240 Mon Sep 17 00:00:00 2001 From: Scott Comer Date: Tue, 20 May 2014 09:46:45 -0500 Subject: [PATCH] added Scores.score_conns to help with making scores for tests; comments for usage of musician_filter; optional score_dt for Score.createx; betterify query for connection in scoring api --- ruby/lib/jam_ruby/models/score.rb | 6 ++++- ruby/lib/jam_ruby/models/search.rb | 24 ++++++++++++++--- .../jam_ruby/models/musician_search_spec.rb | 4 +-- ruby/spec/jam_ruby/models/score_spec.rb | 27 +++++++++++++++++++ web/app/controllers/api_scoring_controller.rb | 14 +++++----- web/app/controllers/api_search_controller.rb | 7 ++--- .../api_scoring_controller_spec.rb | 8 +++--- 7 files changed, 69 insertions(+), 21 deletions(-) diff --git a/ruby/lib/jam_ruby/models/score.rb b/ruby/lib/jam_ruby/models/score.rb index 570d409f4..9fce6a089 100644 --- a/ruby/lib/jam_ruby/models/score.rb +++ b/ruby/lib/jam_ruby/models/score.rb @@ -9,7 +9,7 @@ module JamRuby default_scope order('score_dt desc') - def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt) + def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt=nil) score_dt = Time.new.utc if score_dt.nil? Score.create(alocidispid: alocidispid, anodeid: anodeid, aaddr: aaddr, blocidispid: blocidispid, bnodeid: bnodeid, baddr: baddr, score: score, scorer: 0, score_dt: score_dt) Score.create(alocidispid: blocidispid, anodeid: bnodeid, aaddr: baddr, blocidispid: alocidispid, bnodeid: anodeid, baddr: aaddr, score: score, scorer: 1, score_dt: score_dt) if alocidispid != blocidispid @@ -25,5 +25,9 @@ module JamRuby return -1 if s.nil? return s.score end + + def self.score_conns(c1, c2, score) + self.createx(c1.locidispid, c1.client_id, c1.addr, c2.locidispid, c2.client_id, c2.addr, score) + end end end diff --git a/ruby/lib/jam_ruby/models/search.rb b/ruby/lib/jam_ruby/models/search.rb index 29931e17c..2274e1fd0 100644 --- a/ruby/lib/jam_ruby/models/search.rb +++ b/ruby/lib/jam_ruby/models/search.rb @@ -120,14 +120,32 @@ module JamRuby ordering.blank? ? keys[0] : keys.detect { |oo| oo.to_s == ordering } end - def self.musician_filter(params={}, current_user=nil) + # produce a list of musicians (users where musician is true) + # params: + # instrument - instrument to search for or blank + # handled by relation_pagination: + # page - page number to fetch (origin 1) + # per_page - number of entries per page + # handled by order_param: + # orderby - ??? (followed, plays, playing) + # handled by where_latlng: + # distance - defunct + # city - defunct + # remote_ip - defunct + def self.musician_filter(params={}, user=nil, conn=nil) rel = User.musicians unless (instrument = params[:instrument]).blank? rel = rel.joins("RIGHT JOIN musicians_instruments AS minst ON minst.user_id = users.id") .where(['minst.instrument_id = ? AND users.id IS NOT NULL', instrument]) end - # todo scott - rel = MaxMindGeo.where_latlng(rel, params, current_user) + locidispid = (conn ? conn.locidispid : (user ? user.last_jam_locidispid : 0)) + + # to find appropriate musicians we need to join users with connections to get to their locidispid, + # then join scores with alocidispid found above with the musicians' locidispid to weed out users + # with no scores or bad scores + + # todo scott - rel = MaxMindGeo.where_latlng(rel, params, user) sel_str = 'users.*' case ordering = self.order_param(params) @@ -157,7 +175,7 @@ module JamRuby srch = Search.new srch.search_type = :musicians_filter srch.page_num, srch.page_count = page, objs.total_pages - srch.musician_results_for_user(objs, current_user) + srch.musician_results_for_user(objs, user) end def self.relation_pagination(rel, params) diff --git a/ruby/spec/jam_ruby/models/musician_search_spec.rb b/ruby/spec/jam_ruby/models/musician_search_spec.rb index 40255b4a1..2a3ca0ced 100644 --- a/ruby/spec/jam_ruby/models/musician_search_spec.rb +++ b/ruby/spec/jam_ruby/models/musician_search_spec.rb @@ -3,8 +3,8 @@ require 'spec_helper' describe 'Musician search' do before(:each) do - @geocode1 = FactoryGirl.create(:geocoder) - @geocode2 = FactoryGirl.create(:geocoder) + # @geocode1 = FactoryGirl.create(:geocoder) + # @geocode2 = FactoryGirl.create(:geocoder) @users = [] @users << @user1 = FactoryGirl.create(:user) @users << @user2 = FactoryGirl.create(:user) diff --git a/ruby/spec/jam_ruby/models/score_spec.rb b/ruby/spec/jam_ruby/models/score_spec.rb index 84efb6127..604622b9e 100644 --- a/ruby/spec/jam_ruby/models/score_spec.rb +++ b/ruby/spec/jam_ruby/models/score_spec.rb @@ -92,4 +92,31 @@ describe Score do Score.findx(3456, 3456).should == -1 end + it "test shortcut for making scores from connections" do + user1 = FactoryGirl.create(:user) + conn1 = FactoryGirl.create(:connection, user: user1, addr: 0x01020304, locidispid: 5) + user2 = FactoryGirl.create(:user) + conn2 = FactoryGirl.create(:connection, user: user2, addr: 0x11121314, locidispid: 6) + user3 = FactoryGirl.create(:user) + conn3 = FactoryGirl.create(:connection, user: user3, addr: 0x21222324, locidispid: 7) + + Score.findx(5, 6).should == -1 + Score.findx(6, 5).should == -1 + Score.findx(5, 7).should == -1 + Score.findx(7, 5).should == -1 + Score.findx(6, 7).should == -1 + Score.findx(7, 6).should == -1 + + Score.score_conns(conn1, conn2, 12) + Score.score_conns(conn1, conn3, 13) + Score.score_conns(conn2, conn3, 23) + + Score.findx(5, 6).should == 12 + Score.findx(6, 5).should == 12 + Score.findx(5, 7).should == 13 + Score.findx(7, 5).should == 13 + Score.findx(6, 7).should == 23 + Score.findx(7, 6).should == 23 + end + end diff --git a/web/app/controllers/api_scoring_controller.rb b/web/app/controllers/api_scoring_controller.rb index 5ba61122d..4178fac6b 100644 --- a/web/app/controllers/api_scoring_controller.rb +++ b/web/app/controllers/api_scoring_controller.rb @@ -7,11 +7,10 @@ class ApiScoringController < ApiController clientid = params[:clientid] if clientid.nil? then render :json => {message: 'clientid not specified'}, :status => 400; return end - conn = Connection.where(client_id: clientid).first + conn = Connection.where(client_id: clientid, user_id: current_user.id).first if conn.nil? then render :json => {message: 'session not found'}, :status => 404; return end - if !current_user.id.eql?(conn.user.id) then render :json => {message: 'session not owned by user'}, :status => 403; return end + # if !current_user.id.eql?(conn.user.id) then render :json => {message: 'session not owned by user'}, :status => 403; return end - # todo this method is a stub #puts "ApiScoringController#work(#{clientid}) => locidispid #{c.locidispid}" result_client_id = JamRuby::GetWork.get_work(conn.locidispid) #result_client_id = clientid+'peer' @@ -23,11 +22,10 @@ class ApiScoringController < ApiController clientid = params[:clientid] if clientid.nil? then render :json => {message: 'clientid not specified'}, :status => 400; return end - conn = Connection.where(client_id: clientid).first + conn = Connection.where(client_id: clientid, user_id: current_user.id).first if conn.nil? then render :json => {message: 'session not found'}, :status => 404; return end - if !current_user.id.eql?(conn.user.id) then render :json => {message: 'session not owned by user'}, :status => 403; return end + # if !current_user.id.eql?(conn.user.id) then render :json => {message: 'session not owned by user'}, :status => 403; return end - # todo this method is a stub result_client_ids = JamRuby::GetWork.get_work_list(conn.locidispid) #result_client_ids = [clientid+'peer1', clientid+'peer2'] @@ -61,10 +59,10 @@ class ApiScoringController < ApiController if !score.is_a? Numeric then render :json => {message: 'score not valid numeric'}, :status => 400; return end - aconn = Connection.where(client_id: aclientid).first + aconn = Connection.where(client_id: aclientid, user_id: current_user.id).first if aconn.nil? then render :json => {message: 'a\'s session not found'}, :status => 404; return end if aAddr != aconn.addr then render :json => {message: 'a\'s session addr does not match aAddr'}, :status => 403; return end - if !current_user.id.eql?(aconn.user.id) then render :json => {message: 'a\'s session not owned by user'}, :status => 403; return end + # if !current_user.id.eql?(aconn.user.id) then render :json => {message: 'a\'s session not found'}, :status => 403; return end bconn = Connection.where(client_id: bclientid).first if bconn.nil? then render :json => {message: 'b\'s session not found'}, :status => 404; return end diff --git a/web/app/controllers/api_search_controller.rb b/web/app/controllers/api_search_controller.rb index 81baf7b6c..0ed3f9fd0 100644 --- a/web/app/controllers/api_search_controller.rb +++ b/web/app/controllers/api_search_controller.rb @@ -9,13 +9,14 @@ class ApiSearchController < ApiController if 1 == params[Search::PARAM_MUSICIAN].to_i || 1 == params[Search::PARAM_BAND].to_i query = params.clone query[:remote_ip] = request.remote_ip - if 1 == params[Search::PARAM_MUSICIAN].to_i - @search = Search.musician_filter(query, current_user) + if 1 == query[Search::PARAM_MUSICIAN].to_i + clientid = params[:clientid] + conn = (clientid ? Connection.where(client_id: clientid, user_id: current_user.id).first : nil) + @search = Search.musician_filter(query, current_user, conn) else @search = Search.band_filter(query, current_user) end respond_with @search, responder: ApiResponder, :status => 200 - elsif 1 == params[Search::PARAM_SESSION_INVITE].to_i @search = Search.session_invite_search(params[:query], current_user) else diff --git a/web/spec/controllers/api_scoring_controller_spec.rb b/web/spec/controllers/api_scoring_controller_spec.rb index 7a3b71911..11df2af83 100644 --- a/web/spec/controllers/api_scoring_controller_spec.rb +++ b/web/spec/controllers/api_scoring_controller_spec.rb @@ -103,7 +103,7 @@ describe ApiScoringController do response.should_not be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:message].should eql('session not owned by user') + json[:message].should eql('session not found') end it 'try work with mike login and mary' do @@ -112,7 +112,7 @@ describe ApiScoringController do response.should_not be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:message].should eql('session not owned by user') + json[:message].should eql('session not found') end end @@ -181,7 +181,7 @@ describe ApiScoringController do response.should_not be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:message].should eql('session not owned by user') + json[:message].should eql('session not found') end end @@ -302,7 +302,7 @@ describe ApiScoringController do response.should_not be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:message].should eql('a\'s session not owned by user') + json[:message].should eql('a\'s session not found') end it 'record with mary login, mary, mary_addr, mary, mary_addr, score' do