pushing to test comment passing

This commit is contained in:
maxwell 2010-06-30 13:54:09 -07:00
parent df6ea31d3d
commit fcdba64828
7 changed files with 69 additions and 17 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
"<post>#{self.to_xml.to_s}</post>"
end
@ -67,7 +79,7 @@ module Diaspora
def self.build_xml_for(posts)
xml = "<XML>"
xml += Post.generate_header
xml += "<posts>"
xml += "\n <posts>"
posts.each {|x| xml << x.prep_webhook}
xml += "</posts>"
xml += "</XML>"

View file

@ -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

View file

@ -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

View file

@ -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
end