diff --git a/app/models/comment.rb b/app/models/comment.rb index af10726bd..b39d866d3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,8 +1,10 @@ class Comment include MongoMapper::Document include ROXML + include Diaspora::Webhooks + xml_accessor :text - xml_accessor :person, :as => Person + xml_reader :person, :to_xml => proc {|person| person.email} key :text, String key :target, String diff --git a/app/models/post.rb b/app/models/post.rb index 5cd679503..a74f66cb6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -20,6 +20,7 @@ class Post timestamps! after_save :send_to_view + after_save :notify_friends def self.stream Post.sort(:created_at.desc).all diff --git a/app/models/user.rb b/app/models/user.rb index b1d3f96bc..7cd6db852 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,7 +7,16 @@ class User < Person def comment(text, options = {}) raise "Comment on what, motherfucker?" unless options[:on] - Comment.new(:person_id => self.id, :text => text, :post => options[:on]).save + c = Comment.new(:person_id => self.id, :text => text, :post => options[:on]) + if c.save + if mine?(c.post) + c.push_to(c.post.friends_with_permissions) # should return plucky query + else + c.push_to([c.post.person]) + end + true + end + false end validates_presence_of :profile @@ -16,4 +25,9 @@ class User < Person def do_bad_things self.password_confirmation = self.password end + + def mine?(post) + self == post.person + end + end diff --git a/lib/common.rb b/lib/common.rb index 99cbb3f50..4591b8392 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -9,6 +9,13 @@ module Diaspora sender_id = parse_sender_id_from_xml(xml) Friend.where(:email => sender_id).first end + + + def parse_owner_from_xml(xml) + doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } + email = doc.xpath("/person/email").text.to_s + Friend.where(:email => sender_id).first + end def parse_body_contents_from_xml(xml) doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } @@ -22,7 +29,8 @@ module Diaspora body.children.each do |post| begin object = post.name.camelize.constantize.from_xml post.to_s - object.person = sender if object.is_a? Post + puts post.to_s + object.person = parse_owner_from_xml post.to_s #if object.is_a? Post objects << object rescue puts "Not a real type: #{object.to_s}" @@ -39,23 +47,27 @@ module Diaspora #p.save if p.respond_to?(:person) && !(p.person == nil) #WTF end end - - end + module Webhooks def self.included(klass) klass.class_eval do - after_save :notify_friends @@queue = MessageHandler.new def notify_friends if self.person_id == User.first.id - xml = Post.build_xml_for([self]) - @@queue.add_post_request( friends_with_permissions, CGI::escape(xml) ) - @@queue.process + push_to(friends_with_permissions) end end - + + + def push_to(recipients) + xml = self.class.build_xml_for([self]) + @@queue.add_post_request( recipients, xml ) + @@queue.process + end + + def prep_webhook "#{self.to_xml.to_s}" end @@ -67,7 +79,7 @@ module Diaspora def self.build_xml_for(posts) xml = "" xml += Post.generate_header - xml += "" + xml += "\n " posts.each {|x| xml << x.prep_webhook} xml += "" xml += "" diff --git a/lib/message_handler.rb b/lib/message_handler.rb index dfa98c5e5..10268e1bc 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -13,7 +13,8 @@ class MessageHandler def add_post_request(destinations, body) - destinations.each{|dest| @queue.push(Message.new(:post, dest, body))} + b = CGI::escape(body) + destinations.each{|dest| @queue.push(Message.new(:post, dest, b))} end def process diff --git a/spec/helpers/parser_spec.rb b/spec/helpers/parser_spec.rb index 3cc11f71c..fc9e0e885 100644 --- a/spec/helpers/parser_spec.rb +++ b/spec/helpers/parser_spec.rb @@ -62,9 +62,9 @@ describe "parser in application helper" do describe "parsing compliant XML object" do before do - status_messages = [] - 10.times { status_messages << Factory.build(:status_message)} - @xml = Post.build_xml_for(status_messages) + @status_messages = [] + 10.times { @status_messages << Factory.build(:status_message)} + @xml = Post.build_xml_for(@status_messages) end it 'should be able to parse the sender\'s unique id' do @@ -103,7 +103,20 @@ describe "parser in application helper" do comment.post.should == post end - end + + + it 'should parse a person out of a post' do + @user.comment "foo", :on => @status_messages.first + xml = Comment.build_xml_for([Comment.first]) + puts xml + objs = parse_objects_from_xml(xml) + + puts objs.inspect + + + + end + end end diff --git a/spec/models/comments_spec.rb b/spec/models/comments_spec.rb index 47aa7a39e..00f8c1240 100644 --- a/spec/models/comments_spec.rb +++ b/spec/models/comments_spec.rb @@ -22,5 +22,14 @@ describe Comment do StatusMessage.first.comments.first.person.should == @user end + + it 'should be able to send a post owner any new comments a user adds' do + friend = Factory.create(:friend) + status = Factory.create(:status_message, :person => friend) + + Comment.send(:class_variable_get, :@@queue).should_receive(:add_post_request) + @user.comment "yo", :on => status + end + end -end \ No newline at end of file +end