diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb index d4b2384fc1b29f8f3a5e1e1a8badcb348a50f323..add191093d0caa33ef167cefeb1091ba59937949 100644 --- a/app/controllers/requests_controller.rb +++ b/app/controllers/requests_controller.rb @@ -25,9 +25,9 @@ class RequestsController < ApplicationController end def create - url = relationship_flow(params[:request][:destination_url])[:friend] + rel_hash = relationship_flow(params[:request][:destination_url]) + @request = current_user.send_request(rel_hash) - @request = current_user.send_friend_request_to(url) unless url.include?('@')|| url == '' if @request flash[:notice] = "a friend request was sent to #{@request.destination_url}" redirect_to requests_url diff --git a/app/models/request.rb b/app/models/request.rb index c3962a0f5ef34a74e18fb2ff8010177a12bc5801..55e20bbb8331c266f09a676a5fa79f3c1d986fea 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -37,7 +37,7 @@ class Request p.active = true p.save end - + protected diff --git a/app/models/user.rb b/app/models/user.rb index 4c22fcabd52b15c5d08d409d42c5e0c01b7cfe71..5244932558a528a5acf8848147cfe352cacad01a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -75,6 +75,26 @@ class User < Person end end + ####ostatus###### + # + def subscribe_to_pubsub(feed_url) + # THIS SHOULD REALLY BE IN A REQUESR>>>> FIGURE IT OUT + r = Request.instantiate(:to => feed_url, :from => self) + + r.subscribe_to_ostatus(feed_url) + end + + + def send_request(thing) + if thing[:friend] + self.send_friend_request(thing[:friend]) + elsif thing[:subscribe] + self.subscribe_to_pubsub(thing[:subscrube]) + else + raise "you can't do anything to that url" + end + end + ###Helpers############ def mine?(post) diff --git a/lib/common.rb b/lib/common.rb index 8b8e0acf4e5dc43deecd86c08993aecc544cc8cf..322df5f4c11bb6d9d49347f33921eca3a7ce8e2d 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -1,6 +1,20 @@ module Diaspora - module XMLParser + module OStatusParser + def find_hub(xml) + Nokogiri::HTML(xml).xpath('//link[@rel="hub"]').first.attribute("href").value + end + + def parse_sender(xml) + puts xml + end + def parse_objects(xml) + + end + end + + + module XMLParser def parse_owner_from_xml(xml) doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } email = doc.xpath("//person/email").text.to_s @@ -57,8 +71,12 @@ module Diaspora end end + def subscribe_to_ostatus(hub, feed_url) + @@queue.add_subscription_request(hub, feed_url) + @@queue.process + end + def push_to(recipients) - @@queue.add_hub_notification(APP_CONFIG[:pubsub_server], User.owner.url + self.class.to_s.pluralize.underscore + '.atom') unless recipients.empty? diff --git a/lib/message_handler.rb b/lib/message_handler.rb index 641627383e11a1b044d5b662d1b5e4facea1ea1c..a031da970813a452ba941ee87736d5e28ac2db4f 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -14,11 +14,27 @@ class MessageHandler def add_post_request(destinations, body) b = CGI::escape( body ) - destinations.each{|dest| @queue.push(Message.new(:post, dest, b))} + destinations.each{|dest| @queue.push(Message.new(:post, dest, :body => b))} end def add_hub_notification(destination, feed_location) - @queue.push(Message.new(:pubhub, destination, feed_location)) + @queue.push(Message.new(:pubhub, destination, :body => feed_location)) + end + + def add_hub_subscription_request(hub, body) + @queue.push(Message.new(:pubhubsub, hub, :body => body)) + end + + def add_subscription_request(feed) + + feed_action = lambda{ + hub = Diaspora::OStatusParser::find_hub(http.response) + add_hub_subscription_request(hub, query.destination) + Diaspora::OStatus::parse_sender(http.response) + } + + + @queue.push(Message.new(:subscribe, feed, :callback => callback)) end def process @@ -30,9 +46,18 @@ class MessageHandler when :get http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT http.callback {send_to_seed(query, http.response); process} + + when :subscribe + http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT + http.callback query.callback + when :pubhub http = EventMachine::PubSubHubbub.new(query.destination).publish query.body, :timeout => TIMEOUT http.callback { process} + + when :pubhubsub + http = EventMachine::PubSubHubbub.new(query.destination).subscribe query.body, User.owner.url, :timeout => TIMEOUT + http.callback { process} else raise "message is not a type I know!" end @@ -57,10 +82,14 @@ class MessageHandler class Message attr_accessor :type, :destination, :body, :try_count - def initialize(type, dest, body= nil) + def initialize(type, dest, opts = {}) @type = type @destination = dest - @body = body + @body = opts[:body] + + opts[:callback] ||= lambda{ process; process } + + @callback = opts[:callback] @try_count = 0 end end diff --git a/spec/fixtures/identica_feed.atom b/spec/fixtures/identica_feed.atom new file mode 100644 index 0000000000000000000000000000000000000000..d590fc1f40f7516602f07b08dd20c161e37db58e --- /dev/null +++ b/spec/fixtures/identica_feed.atom @@ -0,0 +1,166 @@ +<?xml version="1.0" encoding="UTF-8"?> +<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/"> + <generator uri="http://status.net" version="0.9.3">StatusNet</generator> + <id>http://identi.ca/api/statuses/user_timeline/169966.atom</id> + <title>joindiaspora timeline</title> + <subtitle>Updates from joindiaspora on Identi.ca!</subtitle> + <logo>http://avatar.identi.ca/169966-96-20100426170852.png</logo> + <updated>2010-07-22T01:00:38+00:00</updated> +<author> + <name>joindiaspora</name> + <uri>http://identi.ca/user/169966</uri> +</author> + <link href="http://identi.ca/joindiaspora" rel="alternate" type="text/html"/> + <link href="http://identi.ca/main/sup#169966" rel="http://api.friendfeed.com/2008/03#sup" type="application/json"/> + <link href="http://identi.ca/main/push/hub" rel="hub"/> + <link href="http://identi.ca/main/salmon/user/169966" rel="http://salmon-protocol.org/ns/salmon-replies"/> + <link href="http://identi.ca/main/salmon/user/169966" rel="http://salmon-protocol.org/ns/salmon-mention"/> + <link href="http://identi.ca/api/statuses/user_timeline/169966.atom" rel="self" type="application/atom+xml"/> +<activity:subject> + <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type> + <id>http://identi.ca/user/169966</id> + <title>Diaspora</title> + <link rel="alternate" type="text/html" href="http://identi.ca/joindiaspora"/> + <link rel="avatar" type="image/png" media:width="480" media:height="480" href="http://avatar.identi.ca/169966-480-20100426170851.png"/> + <link rel="avatar" type="image/png" media:width="96" media:height="96" href="http://avatar.identi.ca/169966-96-20100426170852.png"/> + <link rel="avatar" type="image/png" media:width="48" media:height="48" href="http://avatar.identi.ca/169966-48-20100426170852.png"/> + <link rel="avatar" type="image/png" media:width="24" media:height="24" href="http://avatar.identi.ca/169966-24-20100426170852.png"/> +<poco:preferredUsername>joindiaspora</poco:preferredUsername> +<poco:displayName>Diaspora</poco:displayName> +<poco:note>Diaspora is an awesome, distributed, open source social network in the making from some young hackers at NYU</poco:note> +<poco:urls> + <poco:type>homepage</poco:type> + <poco:value>http://joindiaspora.com</poco:value> + <poco:primary>true</poco:primary> +</poco:urls> +</activity:subject> +<entry> + <title>A month of Diaspora in action. First screenshots/video! http://bit.ly/clOFXx</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/39231942"/> + <id>http://identi.ca/notice/39231942</id> + <published>2010-07-02T06:13:11+00:00</published> + <updated>2010-07-02T06:13:11+00:00</updated> + <statusnet:notice_info local_id="39231942" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/38985291"/> + <content type="html">A month of Diaspora in action. First screenshots/video! <a href="http://bit.ly/clOFXx" title="http://www.joindiaspora.com/2010/07/01/one-month-in.html" rel="external">http://bit.ly/clOFXx</a></content> +</entry> +<entry> + <title>http://www.reclaimprivacy.org/ check your FB privacy settings, automagically! !whyisntthisbuiltintofacebook :)</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/32556574"/> + <id>http://identi.ca/notice/32556574</id> + <published>2010-05-17T18:18:28+00:00</published> + <updated>2010-05-17T18:18:28+00:00</updated> + <statusnet:notice_info local_id="32556574" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/32429253"/> + <content type="html"><a href="http://www.reclaimprivacy.org/" title="http://www.reclaimprivacy.org/" rel="external">http://www.reclaimprivacy.org/</a> check your FB privacy settings, automagically! !whyisntthisbuiltintofacebook :)</content> +</entry> +<entry> + <title>@cacheson, should already be there, at http://joindiaspora.com/atom.xml... hopefully that will do the trick...</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/31824813"/> + <id>http://identi.ca/notice/31824813</id> + <published>2010-05-11T14:29:38+00:00</published> + <updated>2010-05-11T14:29:38+00:00</updated> + <statusnet:notice_info local_id="31824813" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/31712301"/> + <link rel="ostatus:attention" href="http://identi.ca/user/47185"/> + <content type="html">@<span class="vcard"><a href="http://identi.ca/user/47185" class="url" title="Chris Acheson"><span class="fn nickname">cacheson</span></a></span>, should already be there, at <a href="http://joindiaspora.com/atom.xml" title="http://joindiaspora.com/atom.xml" rel="external" class="attachment" id="attachment-15490501">http://joindiaspora.com/atom.xml</a>... hopefully that will do the trick...</content> + <link rel="enclosure" href="http://joindiaspora.com/atom.xml" type="application/xml" length="31902"/> +</entry> +<entry> + <title>@elmom, thanks for your support. we are excited to get started. helsinki is awesome, I hope to go back one day! good luck w/hackerspace!</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30760606"/> + <id>http://identi.ca/notice/30760606</id> + <published>2010-05-02T07:06:02+00:00</published> + <updated>2010-05-02T07:06:02+00:00</updated> + <statusnet:notice_info local_id="30760606" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/30668263"/> + <link rel="ostatus:attention" href="http://identi.ca/user/101533"/> + <content type="html">@<span class="vcard"><a href="http://identi.ca/user/101533" class="url" title="Elmo M&#xE4;ntynen"><span class="fn nickname">elmom</span></a></span>, thanks for your support. we are excited to get started. helsinki is awesome, I hope to go back one day! good luck w/hackerspace!</content> +</entry> +<entry> + <title>@tieguy, thanks for the great feedback, here is our response: http://bit.ly/aF2Fpl</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30549323"/> + <id>http://identi.ca/notice/30549323</id> + <published>2010-04-30T06:29:28+00:00</published> + <updated>2010-04-30T06:29:28+00:00</updated> + <statusnet:notice_info local_id="30549323" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/30461403"/> + <link rel="ostatus:attention" href="http://identi.ca/user/11"/> + <content type="html">@<span class="vcard"><a href="http://identi.ca/user/11" class="url" title="Luis Villa"><span class="fn nickname">tieguy</span></a></span>, thanks for the great feedback, here is our response: <a href="http://bit.ly/aF2Fpl" title="http://joindiaspora.com/2010/04/30/a-response-to-mr-villa.html" rel="external">http://bit.ly/aF2Fpl</a></content> +</entry> +<entry> + <title>RT @tieguy @joindiaspora hopefully constructive questions for you: http://ur1.ca/xbvq cc: @mlinksva @rejon @biella@brainbird.net</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30154486"/> + <id>http://identi.ca/notice/30154486</id> + <published>2010-04-27T06:22:14+00:00</published> + <updated>2010-04-27T06:22:14+00:00</updated> + <statusnet:notice_info local_id="30154486" source="web" favorite="false" repeated="false" repeat_of="30152015"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/30076242"/> + <ostatus:forward ref="http://identi.ca/notice/30152015" href="http://identi.ca/notice/30152015"></ostatus:forward> + <content type="html">RT @<span class="vcard"><a href="http://identi.ca/user/11" class="url" title="Luis Villa"><span class="fn nickname">tieguy</span></a></span> @<span class="vcard"><a href="http://identi.ca/user/169966" class="url" title="Diaspora"><span class="fn nickname">joindiaspora</span></a></span> hopefully constructive questions for you: <a href="http://ur1.ca/xbvq" title="http://tieguy.org/blog/2010/04/27/questions-for-the-diaspora/" rel="external">http://ur1.ca/xbvq</a> cc: @<span class="vcard"><a href="http://identi.ca/user/8" class="url" title="Mike Linksvayer"><span class="fn nickname">mlinksva</span></a></span> @<span class="vcard"><a href="http://identi.ca/user/37" class="url" title="Jon Phillips"><span class="fn nickname">rejon</span></a></span> @<span class="vcard"><a href="http://brainbird.net/biella" class="url"><span class="fn nickname">biella@brainbird.net</span></a></span></content> +</entry> +<entry> + <title>@tieguy it's on! ;) we have already thought about much of your comments, but give us a day or two to give you a good response.</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30152694"/> + <id>http://identi.ca/notice/30152694</id> + <published>2010-04-27T06:04:30+00:00</published> + <updated>2010-04-27T06:04:30+00:00</updated> + <statusnet:notice_info local_id="30152694" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/30074469"/> + <link rel="ostatus:attention" href="http://identi.ca/user/11"/> + <content type="html">@<span class="vcard"><a href="http://identi.ca/user/11" class="url" title="Luis Villa"><span class="fn nickname">tieguy</span></a></span> it's on! ;) we have already thought about much of your comments, but give us a day or two to give you a good response.</content> +</entry> +<entry> + <title>@mattkatz00 we really appreciate your kickstarter backing!</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30089749"/> + <id>http://identi.ca/notice/30089749</id> + <published>2010-04-26T19:32:19+00:00</published> + <updated>2010-04-26T19:32:19+00:00</updated> + <statusnet:notice_info local_id="30089749" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/30012647"/> + <content type="html">@mattkatz00 we really appreciate your kickstarter backing!</content> +</entry> +<entry> + <title>@danlatorre many thanks for your backing and support!</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30089410"/> + <id>http://identi.ca/notice/30089410</id> + <published>2010-04-26T19:29:27+00:00</published> + <updated>2010-04-26T19:29:27+00:00</updated> + <statusnet:notice_info local_id="30089410" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/30012313"/> + <link rel="ostatus:attention" href="http://identi.ca/user/16365"/> + <content type="html">@<span class="vcard"><a href="http://identi.ca/user/16365" class="url" title="Daniel Latorre"><span class="fn nickname">danlatorre</span></a></span> many thanks for your backing and support!</content> +</entry> +<entry> + <title>@adwilliamson thanks for backing diaspora!</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30089220"/> + <id>http://identi.ca/notice/30089220</id> + <published>2010-04-26T19:27:36+00:00</published> + <updated>2010-04-26T19:27:36+00:00</updated> + <statusnet:notice_info local_id="30089220" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/30012129"/> + <content type="html">@adwilliamson thanks for backing diaspora!</content> +</entry> +<entry> + <title>@thisisparker many thanks for your kickstarter backing!</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30088868"/> + <id>http://identi.ca/notice/30088868</id> + <published>2010-04-26T19:23:51+00:00</published> + <updated>2010-04-26T19:23:51+00:00</updated> + <statusnet:notice_info local_id="30088868" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/30011788"/> + <link rel="ostatus:attention" href="http://identi.ca/user/27657"/> + <content type="html">@<span class="vcard"><a href="http://identi.ca/user/27657" class="url" title="Parker Higgins"><span class="fn nickname">thisisparker</span></a></span> many thanks for your kickstarter backing!</content> +</entry> +<entry> + <title>We'll be building the interface for Diaspora to StatusNet, but for now, this will be from the web!</title> + <link rel="alternate" type="text/html" href="http://identi.ca/notice/30074166"/> + <id>http://identi.ca/notice/30074166</id> + <published>2010-04-26T17:07:49+00:00</published> + <updated>2010-04-26T17:07:49+00:00</updated> + <statusnet:notice_info local_id="30074166" source="web" favorite="false" repeated="false"></statusnet:notice_info> + <link rel="ostatus:conversation" href="http://identi.ca/conversation/29997414"/> + <content type="html">We'll be building the interface for Diaspora to StatusNet, but for now, this will be from the web!</content> +</entry> +</feed> + diff --git a/spec/lib/parser_spec.rb b/spec/lib/parser_spec.rb index 697d773c19bf53c8ef2bfd24a2a0b5b638acb58e..46bb23aab63edc86b9d615e3084b61e2706b7191 100644 --- a/spec/lib/parser_spec.rb +++ b/spec/lib/parser_spec.rb @@ -1,6 +1,7 @@ require File.dirname(__FILE__) + '/../spec_helper' include ApplicationHelper +include Diaspora::OStatusParser describe "parser in application helper" do before do @@ -59,6 +60,15 @@ describe "parser in application helper" do Post.count.should == 0 end + describe 'ostatus parsing' do + it 'should be able to get the hub of an ostatus feed' do + xml_path = File.dirname(__FILE__) + '/../fixtures/identica_feed.atom' + xml = File.open(xml_path).read + + Diaspora::OStatusParser::find_hub(xml).should == 'http://identi.ca/main/push/hub' + end + + end describe "parsing compliant XML object" do before do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 854cde20b3e9f2a1c6fa3328e23e5ba2cc195a92..10ed630b17816d4d223be2a455d39133b1751eb5 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -41,10 +41,29 @@ describe User do @user.send_friend_request_to( @friend.url ).should be nil end - it 'should be able to give me the terse url for webfinger' do user = Factory.create(:user) user.terse_url.should == 'example.com' end + + it 'should subscribe to a status.net user' do + @user = Factory.create(:user) + @user.request_connection + + + #@user.send_friend_request(url) + @user.send_subscription(url) + get stream <== schedule async get :inquire_subscription + { + parse out hub + subscribe to hub => s + parse_profile + parse_messages + + } + end + end +=end + end