the Devise email regex was not catching pods that were anything other than a standard url

This commit is contained in:
Ilya Zhitomirskiy 2011-09-09 18:05:58 -07:00
parent aeef0b5f6e
commit ad11bd2a31
2 changed files with 50 additions and 5 deletions

View file

@ -32,7 +32,7 @@ class PeopleController < ApplicationController
format.html do format.html do
#only do it if it is an email address #only do it if it is an email address
if params[:q].try(:match, Devise.email_regexp) if diaspora_id?(params[:q])
people = Person.where(:diaspora_handle => params[:q].downcase) people = Person.where(:diaspora_handle => params[:q].downcase)
webfinger(params[:q]) if people.empty? webfinger(params[:q]) if people.empty?
else else
@ -43,7 +43,7 @@ class PeopleController < ApplicationController
end end
format.mobile do format.mobile do
#only do it if it is an email address #only do it if it is an email address
if params[:q].try(:match, Devise.email_regexp) if diaspora_id?(params[:q])
people = Person.where(:diaspora_handle => params[:q]) people = Person.where(:diaspora_handle => params[:q])
webfinger(params[:q]) if people.empty? webfinger(params[:q]) if people.empty?
else else
@ -165,13 +165,18 @@ class PeopleController < ApplicationController
end end
end end
def diaspora_id?(query)
!query.try(:match, /^(\w)*@([a-zA-Z0-9]|[-]|[.]|[:])*$/).nil?
end
private private
def webfinger(account, opts = {}) def webfinger(account, opts = {})
Resque.enqueue(Job::SocketWebfinger, current_user.id, account, opts) Resque.enqueue(Job::SocketWebfinger, current_user.id, account, opts)
end end
def remote_profile_with_no_user_session? def remote_profile_with_no_user_session?
@person && @person.remote? && !user_signed_in? @person && @person.remote? && !user_signed_in?
end end
end end

View file

@ -58,6 +58,12 @@ describe PeopleController do
assigns[:people][0].id.should == eugene2.id assigns[:people][0].id.should == eugene2.id
end end
it "allows unsearchable people to be found by handle" do
d_id = "eugene@example.org"
@controller.should_receive(:diaspora_id?).with(d_id)
get :index, :q => d_id
end
it "downcases the handle before trying to find someone by it" do it "downcases the handle before trying to find someone by it" do
eugene2 = Factory.create(:person, :diaspora_handle => "eugene@example.org", eugene2 = Factory.create(:person, :diaspora_handle => "eugene@example.org",
:profile => Factory.build(:profile, :first_name => "Eugene", :profile => Factory.build(:profile, :first_name => "Eugene",
@ -345,6 +351,40 @@ describe PeopleController do
end end
end end
describe '#diaspora_id?' do
it 'returns true for pods on urls' do
@controller.diaspora_id?("ilya_123@pod.geraspora.de").should be_true
end
it 'returns true for pods on urls with port' do
@controller.diaspora_id?("ilya_123@pod.geraspora.de:12314").should be_true
end
it 'returns true for pods on localhost' do
@controller.diaspora_id?("ilya_123@localhost").should be_true
end
it 'returns true for pods on localhost and port' do
@controller.diaspora_id?("ilya_123@localhost:1234").should be_true
end
it 'returns true for pods on ip' do
@controller.diaspora_id?("ilya_123@1.1.1.1").should be_true
end
it 'returns true for pods on ip and port' do
@controller.diaspora_id?("ilya_123@1.2.3.4:1234").should be_true
end
it 'returns false for pods on with invalid url characters' do
@controller.diaspora_id?("ilya_123@join_diaspora.com").should be_false
end
it 'returns false for invalid usernames' do
@controller.diaspora_id?("ilya_2%3@joindiaspora.com").should be_false
end
end
describe '#webfinger' do describe '#webfinger' do
it 'enqueues a webfinger job' do it 'enqueues a webfinger job' do
Resque.should_receive(:enqueue).with(Job::SocketWebfinger, @user.id, @user.diaspora_handle, anything).once Resque.should_receive(:enqueue).with(Job::SocketWebfinger, @user.id, @user.diaspora_handle, anything).once