diff --git a/app/helpers/requests_helper.rb b/app/helpers/requests_helper.rb index 4d3c37ed3..96e1872a9 100644 --- a/app/helpers/requests_helper.rb +++ b/app/helpers/requests_helper.rb @@ -1,16 +1,49 @@ module RequestsHelper - def diaspora_url(identifier) - if identifier.include? '@' - - begin - f = Redfinger.finger(identifier) - good_links = f.links.map{|x| return x.href if x.rel =='http://joindiaspora.com/seed_location'} - identifier = good_links.first unless good_links.first.nil? - rescue - - end + + def subscription_mode(profile) + if diaspora?(profile) + :friend + elsif ostatus?(profile) + :subscribe + else + :none + end + end + + def diaspora?(profile) + profile_contains(profile, 'http://joindiaspora.com/seed_location') + end + + def ostatus?(profile) + profile_contains(profile, 'http://ostatus.org/schema/1.0/subscribe') + end + + def profile_contains(profile, rel) + profile.links.each{|x| return true if x.rel == rel} + false + end + + def subscription_url(action, profile) + if action == :subscribe + pp profile.links + profile.links.select{|x| x.rel == 'http://schemas.google.com/g/2010#updates-from'}.first.href + elsif action == :friend + profile.links.select{|x| x.rel == 'http://joindiaspora.com/seed_location'}.first.href + else + '' + end + end + + def relationship_flow(identifier) + unless identifier.include?( '@') + return identifier end - identifier + f = Redfinger.finger(identifier) + action = subscription_mode(f) + url = subscription_url(action, f) + + { action => url } end + end diff --git a/spec/helpers/requests_helper_spec.rb b/spec/helpers/requests_helper_spec.rb index b3a20f989..dd8341217 100644 --- a/spec/helpers/requests_helper_spec.rb +++ b/spec/helpers/requests_helper_spec.rb @@ -3,11 +3,18 @@ require File.dirname(__FILE__) + '/../spec_helper' include RequestsHelper describe RequestsHelper do + + before do + @tom = Redfinger.finger('tom@tom.joindiaspora.com') + @evan = Redfinger.finger('evan@status.net') + @max = Redfinger.finger('mbs348@gmail.com') + end + + describe "profile" do it 'should fetch the public webfinger profile on request' do - pending "Can we please find a way to do this that doesn't freak me out if my internet connection is down? Thanks, Rafi" + pending #post :create {:request => {:destination_url => 'tom@tom.joindiaspora.com'} - url = diaspora_url('http://tom.joindiaspora.com/') url.should == 'http://tom.joindiaspora.com/' @@ -15,5 +22,19 @@ describe RequestsHelper do url = diaspora_url('tom@tom.joindiaspora.com') url.should == 'http://tom.joindiaspora.com/' end + + it 'should detect how to subscribe to a diaspora or ostatus webfinger profile' do + subscription_mode(@tom).should == :friend + subscription_mode(@evan).should == :subscribe + subscription_mode(@max).should == :none + end + + it 'should return the correct tag and url for a given address' do + relationship_flow('tom@tom.joindiaspora.com')[:friend].should == 'http://tom.joindiaspora.com/' + relationship_flow('evan@status.net')[:subscribe].should == 'http://evan.status.net/api/statuses/user_timeline/1.atom' + end + end + + end