diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 4abf12a75..a83e34119 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -35,6 +35,7 @@ class PeopleController < ApplicationController if diaspora_id?(search_query) @people = Person.where(:diaspora_handle => search_query.downcase) Webfinger.in_background(search_query) if @people.empty? + @background_query = search_query.downcase end @people = @people.paginate(:page => params[:page], :per_page => 15) @hashes = hashes_for_people(@people, @aspects) @@ -42,6 +43,20 @@ class PeopleController < ApplicationController end end + def refresh_search + @aspect = :search + @people = Person.where(:diaspora_handle => search_query.downcase) + @answer_html = "" + unless @people.empty? + @hashes = hashes_for_people(@people, @aspects) + + self.formats = self.formats + [:html] + @answer_html = render_to_string :partial => 'people/person', :locals => @hashes.first + end + render :json => { :search_count => @people.count, :search_html => @answer_html }.to_json + end + + def tag_index profiles = Profile.tagged_with(params[:name]).where(:searchable => true).select('profiles.id, profiles.person_id') @people = Person.where(:id => profiles.map{|p| p.person_id}).paginate(:page => params[:page], :per_page => 15) diff --git a/app/views/people/index.html.haml b/app/views/people/index.html.haml index e6e4eb048..277c0629c 100644 --- a/app/views/people/index.html.haml +++ b/app/views/people/index.html.haml @@ -18,9 +18,14 @@ .span-15.append-1 #people_stream.stream - if @hashes.empty? - %p - =t('.no_one_found') - + - if @background_query.present? + %p + =t('.searching') + :javascript + $(document).ready(function() { List.startSearchDelay('#{@background_query}') } ); + - else + %p + =t('.no_one_found') - else - for hash in @hashes = render :partial => 'people/person', :locals => hash diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 5395aff1f..dc8661697 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -536,6 +536,7 @@ en: no_results: "Hey! You need to search for something." couldnt_find_them_send_invite: "Couldn't find them? Send an invite!" no_one_found: "...and no one was found." + searching: "searching, please be patient..." looking_for: "Looking for posts tagged %{tag_link}?" webfinger: fail: "Sorry, we couldn't find %{handle}." diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml index f0cba1913..a04f3e186 100644 --- a/config/locales/javascript/javascript.en.yml +++ b/config/locales/javascript/javascript.en.yml @@ -29,8 +29,8 @@ en: my_stream: "Stream" videos: - watch: "Watch this video on <%= provider %>" - unknown: "Unknown video type" + watch: "Watch this video on <%= provider %>" + unknown: "Unknown video type" search_for: "Search for <%= name %>" publisher: at_least_one_aspect: "You must publish to at least one aspect" @@ -73,6 +73,8 @@ en: looking_good: "OMG, you look awesome!" tags: wasnt_that_interesting: "OK, I suppose #<%= tagName %> wasn't all that interesting..." + people: + not_found: "and no one was found..." stream: hide: "Hide" diff --git a/config/routes.rb b/config/routes.rb index cdeff6936..44e6aa3b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -124,6 +124,7 @@ Diaspora::Application.routes.draw do get 'community_spotlight' => "contacts#spotlight", :as => 'community_spotlight' + get 'people/refresh_search' => "people#refresh_search" resources :people, :except => [:edit, :update] do resources :status_messages resources :photos diff --git a/public/javascripts/contact-list.js b/public/javascripts/contact-list.js index 17472e762..9321dc4b0 100644 --- a/public/javascripts/contact-list.js +++ b/public/javascripts/contact-list.js @@ -32,7 +32,26 @@ var List = { }; } }); + }, + runDelayedSearch: function( searchTerm ) { + $.ajax({ + dataType: 'json', + url: '/people/refresh_search', + data: { q: searchTerm }, + success: List.handleSearchRefresh + }); + }, + handleSearchRefresh: function( data ) { + if ( data.search_count > 0 ) { + $("#people_stream.stream").html( data.search_html ); + } else { + $("#people_stream.stream").html( "
" + Diaspora.I18n.t("people.not_found") + "
" ); + } + }, + startSearchDelay: function ( theSearch ) { + setTimeout( "List.runDelayedSearch('" + theSearch + "')", 10000); } + }; $(document).ready(function() { diff --git a/public/javascripts/pages/contacts-index.js b/public/javascripts/pages/contacts-index.js index 0e915097a..ac2e37eeb 100644 --- a/public/javascripts/pages/contacts-index.js +++ b/public/javascripts/pages/contacts-index.js @@ -5,4 +5,5 @@ Diaspora.Pages.ContactsIndex = function() { self.infiniteScroll = self.instantiate("InfiniteScroll"); $('.conversation_button').twipsy({position: 'below'}); }); + }; diff --git a/spec/controllers/jasmine_fixtures/people_spec.rb b/spec/controllers/jasmine_fixtures/people_spec.rb new file mode 100644 index 000000000..8216656ff --- /dev/null +++ b/spec/controllers/jasmine_fixtures/people_spec.rb @@ -0,0 +1,22 @@ +# Copyright (c) 2010-2011, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe PeopleController do + describe '#index' do + before do + sign_in :user, bob + end + + it "generates a jasmine fixture with no query", :fixture => true do + get :index + save_fixture(html_for("body"), "empty_people_search") + end + it "generates a jasmine fixture trying an external search", :fixture => true do + get :index, :q => "sample@diaspor.us" + save_fixture(html_for("body"), "pending_external_people_search") + end + end +end diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index ea1f832e3..d195a1442 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -52,6 +52,11 @@ describe PeopleController do get :index, :q => "Eugene@Example.ORG" assigns[:people][0].id.should == @unsearchable_eugene.id end + + it 'sets the background query task' do + get :index, :q => "Eugene@Example.ORG" + assigns[:background_query].should == "eugene@example.org" + end end context 'query is a tag' do @@ -77,6 +82,11 @@ describe PeopleController do assigns[:hashes].should_not be_nil end + it 'does not set the background query task' do + get :index, :q => "Korth" + assigns[:background_query].should_not be_present + end + it "assigns people" do eugene2 = Factory(:person, :profile => Factory.build(:profile, :first_name => "Eugene", @@ -351,6 +361,34 @@ describe PeopleController do end end + + + describe '#refresh_search ' do + before(:each)do + @eugene = Factory(:person, + :profile => Factory.build(:profile, :first_name => "Eugene", :last_name => "w")) + @korth = Factory(:person, + :profile => Factory.build(:profile, :first_name => "Evan", :last_name => "Korth")) + end + + describe 'via json' do + it 'returns a zero count when a search fails' do + get :refresh_search, :q => "weweweKorth", :format => 'json' + response.body.should == {:search_count=>0, :search_html=>""}.to_json + end + + it 'returns with a zero count unless a fully composed name is sent' do + get :refresh_search, :q => "Korth" + response.body.should == {:search_count=>0, :search_html=>""}.to_json + end + it 'returns with a found name' do + get :refresh_search, :q => @korth.diaspora_handle + JSON.parse( response.body )["search_count"].should == 1 + end + end + end + + describe '#contacts' do it 'assigns the contacts of a person' do contact = alice.contact_for(bob.person) diff --git a/spec/javascripts/search-spec.js b/spec/javascripts/search-spec.js new file mode 100644 index 000000000..7a6f9751f --- /dev/null +++ b/spec/javascripts/search-spec.js @@ -0,0 +1,31 @@ +/* Copyright (c) 2010-2011, Diaspora Inc. This file is +* licensed under the Affero General Public License version 3 or later. See +* the COPYRIGHT file. +*/ + +describe("List", function() { + + describe("runDelayedSearch", function() { + beforeEach( function(){ + }); + + it('gets called on initialize', function(){ + spyOn( List, 'startSearchDelay'); + spec.loadFixture('pending_external_people_search'); + expect(List.startSearchDelay).toHaveBeenCalled(); + }); + }); + + describe("runDelayedSearch", function() { + beforeEach( function(){ + spec.loadFixture('empty_people_search'); + List.initialize(); + }); + + it('inserts contact html', function(){ + List.handleSearchRefresh( { count:1,search_html: '