MS should be able to unsubscribe from an ostatus feed now, now it needs to be put in the controller

This commit is contained in:
maxwell 2010-07-23 21:16:36 -07:00
parent 716fbc7bd4
commit 311a60f7b7
7 changed files with 45 additions and 4 deletions

View file

@ -6,6 +6,7 @@ class Author
key :avatar_thumbnail, String
key :username, String
key :profile_url, String
key :hub, String
many :ostatus_posts, :class_name => 'OstatusPost', :foreign_key => :author_id
before_save :set_defaults

View file

@ -85,6 +85,13 @@ class User < Person
r
end
def unsubscribe_from_pubsub(author_id)
bad_author = Author.first(:id => author_id)
r = Request.instantiate(:to => bad_author.hub, :from => self)
r.unsubscribe_from_ostatus(bad_author.feed_url)
bad_author.destroy
end
def send_request(rel_hash)
puts rel_hash.inspect

View file

@ -1,12 +1,15 @@
module Diaspora
module OStatusParser
def self.find_hub(xml)
Nokogiri::HTML(xml).xpath('//link[@rel="hub"]').first.attribute("href").value
xml = Nokogiri::HTML(xml) if xml.is_a? String
xml.xpath('//link[@rel="hub"]').first.attribute("href").value
end
def self.process(xml)
doc = Nokogiri::HTML(xml)
author_hash = parse_author(doc)
author_hash[:hub] = find_hub(doc)
entry_hash = parse_entry(doc)
author = Author.instantiate(author_hash)
@ -139,6 +142,11 @@ module Diaspora
@@queue.process
end
def unsubscribe_from_ostatus(feed_url)
@@queue.add_hub_unsubscribe_request(self.destination_url, self.callback_url+'hubub', 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')

View file

@ -30,6 +30,9 @@ class MessageHandler
@queue.push(Message.new(:hub_subscribe, hub_url, :body => feed_url))
end
def add_hub_unsubscribe_request(hub, from, feed_url)
@queue.push(Message.new(:hub_unsubscribe, hub, :body => feed_url, :owner_url => from))
end
def process_ostatus_subscription(query_object, http)
hub = Diaspora::OStatusParser::find_hub(http.response)
@ -37,6 +40,7 @@ class MessageHandler
Diaspora::OStatusParser::process(http.response)
end
def process
@queue.pop{ |query|
case query.type
@ -59,6 +63,9 @@ class MessageHandler
when :hub_subscribe
http = EventMachine::PubSubHubbub.new(query.destination).subscribe query.body, User.owner.url + 'hubbub', :timeout => TIMEOUT
http.callback { process}
when :hub_unsubscribe
http = EventMachine::PubSubHubbub.new(query.destination).unsubscribe query.body, query.owner_url, :timeout => TIMEOUT
http.callback {process}
else
raise "message is not a type I know!"
end
@ -82,9 +89,10 @@ class MessageHandler
end
class Message
attr_accessor :type, :destination, :body, :callback, :try_count
attr_accessor :type, :destination, :body, :callback, :owner_url, :try_count
def initialize(type, dest, opts = {})
@type = type
@owner_url = opts[:owner_url]
@destination = dest
@body = opts[:body]
@callback = opts[:callback] ||= lambda{ process; process }

View file

@ -26,7 +26,6 @@ Factory.define :user do |u|
u.key_fingerprint GPGME.list_keys("Smith", true).first.subkeys.first.fingerprint
u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" )
end
Factory.define :status_message do |m|
m.sequence(:message) {|n| "jimmy's #{n} whales"}
end
@ -48,4 +47,11 @@ Factory.define :photo do |p|
p.image File.open( File.dirname(__FILE__) + '/fixtures/bp.jpeg')
end
Factory.define :author do |p|
p.hub "http://pubsubhubub.appspot.com/"
p.service "StatusNet"
p.username "danielgrippi"
p.feed_url "http://google.com"
end
Factory.define(:comment) {}

View file

@ -5,7 +5,7 @@ include Diaspora::OStatusParser
describe Author do
it 'should create from ostatus compliant xml from the parser' do
xml_path = File.dirname(__FILE__) + '/../fixtures/ostatus_update.xml'
xml_path = File.dirname(__FILE__) + '/../fixtures/identica_feed.atom'
xml = File.open(xml_path).read
Author.count.should == 0

View file

@ -46,4 +46,15 @@ describe User do
user.terse_url.should == 'example.com'
end
it 'should be able to unsubscribe from a status.net user' do
@user = Factory.create(:user)
author = Factory.create(:author)
Author.all.count.should == 1
q = Request.send :class_variable_get, :@@queue
q.stub!(:add_hub_unsubscribe_request)
q.should_receive(:add_hub_unsubscribe_request)
@user.unsubscribe_from_pubsub(author.id)
Author.all.count.should == 0
end
end