Merged mongomapper and master
This commit is contained in:
commit
179af4fbfa
15 changed files with 96 additions and 63 deletions
|
|
@ -14,8 +14,9 @@ module ApplicationHelper
|
||||||
|
|
||||||
def parse_sender_object_from_xml(xml)
|
def parse_sender_object_from_xml(xml)
|
||||||
sender_id = parse_sender_id_from_xml(xml)
|
sender_id = parse_sender_id_from_xml(xml)
|
||||||
Person.where(:email => sender_id).first
|
Friend.where(:email => sender_id).first
|
||||||
end
|
|
||||||
|
end
|
||||||
|
|
||||||
def parse_body_contents_from_xml(xml)
|
def parse_body_contents_from_xml(xml)
|
||||||
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
||||||
|
|
@ -24,11 +25,13 @@ module ApplicationHelper
|
||||||
|
|
||||||
def parse_posts_from_xml(xml)
|
def parse_posts_from_xml(xml)
|
||||||
posts = []
|
posts = []
|
||||||
|
sender = parse_sender_object_from_xml(xml)
|
||||||
body = parse_body_contents_from_xml(xml)
|
body = parse_body_contents_from_xml(xml)
|
||||||
body.children.each do |post|
|
body.children.each do |post|
|
||||||
begin
|
begin
|
||||||
object = post.name.camelize.constantize.from_xml post.to_s
|
object = post.name.camelize.constantize.from_xml post.to_s
|
||||||
posts << object if object.is_a? Post
|
object.person = sender
|
||||||
|
posts << object if object.is_a? Post
|
||||||
rescue
|
rescue
|
||||||
puts "Not a real type: #{post.to_s}"
|
puts "Not a real type: #{post.to_s}"
|
||||||
end
|
end
|
||||||
|
|
@ -37,12 +40,10 @@ module ApplicationHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_posts_from_xml(xml)
|
def store_posts_from_xml(xml)
|
||||||
sender_object = parse_sender_object_from_xml(xml)
|
|
||||||
posts = parse_posts_from_xml(xml)
|
posts = parse_posts_from_xml(xml)
|
||||||
|
|
||||||
posts.each do |p|
|
posts.each do |p|
|
||||||
p.person = sender_object
|
p.save unless p.person.nil?
|
||||||
p.save
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,5 +13,8 @@ class Person
|
||||||
many :posts, :class_name => 'Post', :foreign_key => :person_id
|
many :posts, :class_name => 'Post', :foreign_key => :person_id
|
||||||
|
|
||||||
validates_presence_of :email, :real_name
|
validates_presence_of :email, :real_name
|
||||||
|
|
||||||
|
# def newest(type = nil)
|
||||||
|
# type.constantize.where(:person_id => id).last
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,19 @@ class Post
|
||||||
yield self
|
yield self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.newest(person = nil)
|
||||||
|
return self.last if person.nil?
|
||||||
|
self.where(:person_id => person.id).sort(:created_at.desc)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.my_newest
|
||||||
|
self.newest(User.first)
|
||||||
|
end
|
||||||
|
def self.newest_by_email(email)
|
||||||
|
self.where(:person_id => Person.where(:email => email).first.id).last
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def send_to_view
|
def send_to_view
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@ class StatusMessage < Post
|
||||||
|
|
||||||
validates_presence_of :message
|
validates_presence_of :message
|
||||||
|
|
||||||
def self.my_newest
|
def ==(other)
|
||||||
StatusMessage.where(:person_id => User.first.id).sort(:created_at.desc).first
|
(self.message == other.message) && (self.person.email == other.person.email)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
module Diaspora
|
module Diaspora
|
||||||
|
|
||||||
module Webhooks
|
module Webhooks
|
||||||
|
include ApplicationHelper
|
||||||
def self.included(klass)
|
def self.included(klass)
|
||||||
klass.class_eval do
|
klass.class_eval do
|
||||||
after_save :notify_friends
|
after_save :notify_friends
|
||||||
@@queue = MessageHandler.new
|
@@queue = MessageHandler.new
|
||||||
|
|
||||||
def notify_friends
|
def notify_friends
|
||||||
#if self.owner == User.first.email
|
if mine? self
|
||||||
xml = Post.build_xml_for(self)
|
xml = Post.build_xml_for(self)
|
||||||
@@queue.add_post_request( friends_with_permissions, xml )
|
@@queue.add_post_request( friends_with_permissions, xml )
|
||||||
@@queue.process
|
@@queue.process
|
||||||
#end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def prep_webhook
|
def prep_webhook
|
||||||
|
|
@ -24,7 +24,7 @@ module Diaspora
|
||||||
|
|
||||||
def self.build_xml_for(posts)
|
def self.build_xml_for(posts)
|
||||||
xml = "<XML>"
|
xml = "<XML>"
|
||||||
#xml += Post.generate_header
|
xml += Post.generate_header
|
||||||
xml += "<posts>"
|
xml += "<posts>"
|
||||||
posts.each {|x| xml << x.prep_webhook}
|
posts.each {|x| xml << x.prep_webhook}
|
||||||
xml += "</posts>"
|
xml += "</posts>"
|
||||||
|
|
|
||||||
5
science
5
science
|
|
@ -1,5 +0,0 @@
|
||||||
#!/usr/bin/env ruby
|
|
||||||
`sprinkle -s config/sprinkle/provision.rb -v`
|
|
||||||
`cap deploy:setup`
|
|
||||||
`cap deploy:cold`
|
|
||||||
puts 'bababababa bababababa'
|
|
||||||
|
|
@ -61,7 +61,7 @@ describe StatusMessagesController do
|
||||||
get :index
|
get :index
|
||||||
StatusMessage.all.each do |message|
|
StatusMessage.all.each do |message|
|
||||||
response.body.include?(message.message).should be true
|
response.body.include?(message.message).should be true
|
||||||
response.body.include?(message.owner).should be true
|
response.body.include?(message.person.email).should be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,4 @@ Factory.define :bookmark do |b|
|
||||||
end
|
end
|
||||||
|
|
||||||
Factory.define :post do |p|
|
Factory.define :post do |p|
|
||||||
p.source "New York Times"
|
|
||||||
p.sequence(:snippet) {|n| "This is some information #{n}"}
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,38 +4,61 @@ include ApplicationHelper
|
||||||
|
|
||||||
describe ApplicationHelper do
|
describe ApplicationHelper do
|
||||||
before do
|
before do
|
||||||
@user = Factory.create(:user, :email => "bob@aol.com")
|
@user = Factory.create(:user, :email => "bob@aol.com")
|
||||||
@friend =Factory.create(:friend, :email => "bill@gates.com")
|
@friend =Factory.create(:friend, :email => "bill@gates.com")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should store objects sent from xml" do
|
it "should not store posts from me" do
|
||||||
status_messages = []
|
status_messages = []
|
||||||
10.times { status_messages << Factory.build(:status_message)}
|
10.times { status_messages << Factory.build(:status_message, :person => @user)}
|
||||||
|
|
||||||
xml = Post.build_xml_for(status_messages)
|
xml = Post.build_xml_for(status_messages)
|
||||||
|
|
||||||
store_posts_from_xml(xml)
|
store_posts_from_xml(xml)
|
||||||
StatusMessage.count.should == 10
|
StatusMessage.count.should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should discard posts where it does not know the type' do
|
it 'should discard posts where it does not know the type' do
|
||||||
xml = "<XML>
|
xml = "<XML>
|
||||||
<head>
|
<head>
|
||||||
<sender>
|
<sender>
|
||||||
<email>#{User.first.email}</email>
|
<email>#{Friend.first.email}</email>
|
||||||
</sender>
|
</sender>
|
||||||
</head><posts>
|
</head><posts>
|
||||||
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> <post><not_a_real_type></not_a_real_type></post> <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> <post><not_a_real_type></not_a_real_type></post> <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
</posts></XML>"
|
</posts></XML>"
|
||||||
store_posts_from_xml(xml)
|
store_posts_from_xml(xml)
|
||||||
Post.count.should == 2
|
Post.count.should == 2
|
||||||
|
Post.first.person.email.should == Friend.first.email
|
||||||
end
|
end
|
||||||
|
it "should reject xml with no sender" do
|
||||||
|
xml = "<XML>
|
||||||
|
<head>
|
||||||
|
</head><posts>
|
||||||
|
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
|
<post><friend></friend></post>
|
||||||
|
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
|
</posts></XML>"
|
||||||
|
store_posts_from_xml(xml)
|
||||||
|
Post.count.should == 0
|
||||||
|
|
||||||
|
end
|
||||||
|
it "should reject xml with a sender not in the database" do
|
||||||
|
xml = "<XML>
|
||||||
|
<head>
|
||||||
|
<sender>
|
||||||
|
<email>foo@example.com</email>
|
||||||
|
</sender>
|
||||||
|
</head><posts>
|
||||||
|
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
|
<post><friend></friend></post>
|
||||||
|
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
|
</posts></XML>"
|
||||||
|
store_posts_from_xml(xml)
|
||||||
|
Post.count.should == 0
|
||||||
|
end
|
||||||
it 'should discard types which are not of type post' do
|
it 'should discard types which are not of type post' do
|
||||||
xml = "<XML>
|
xml = "<XML>
|
||||||
<head>
|
<head>
|
||||||
<sender>
|
<sender>
|
||||||
<email>#{User.first.email}</email>
|
<email>#{Friend.first.email}</email>
|
||||||
</sender>
|
</sender>
|
||||||
</head><posts>
|
</head><posts>
|
||||||
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
|
|
@ -44,6 +67,7 @@ describe ApplicationHelper do
|
||||||
</posts></XML>"
|
</posts></XML>"
|
||||||
store_posts_from_xml(xml)
|
store_posts_from_xml(xml)
|
||||||
Post.count.should == 2
|
Post.count.should == 2
|
||||||
|
Post.first.person.email.should == Friend.first.email
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -58,9 +82,6 @@ describe ApplicationHelper do
|
||||||
parse_sender_id_from_xml(@xml).should == @user.email
|
parse_sender_id_from_xml(@xml).should == @user.email
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should be able to retrieve the sender\'s local Person object' do
|
|
||||||
parse_sender_object_from_xml(@xml).should == @user
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should be able to parse the body\'s contents' do
|
it 'should be able to parse the body\'s contents' do
|
||||||
body = parse_body_contents_from_xml(@xml).to_s
|
body = parse_body_contents_from_xml(@xml).to_s
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,8 @@ describe Diaspora do
|
||||||
it "should check that it does not send a friends post to an owners friends" do
|
it "should check that it does not send a friends post to an owners friends" do
|
||||||
Post.stub(:build_xml_for).and_return(true)
|
Post.stub(:build_xml_for).and_return(true)
|
||||||
Post.should_not_receive(:build_xml_for)
|
Post.should_not_receive(:build_xml_for)
|
||||||
Factory.create(:status_message, :owner => "nottheowner@post.com")
|
|
||||||
|
Factory.create(:status_message, :person => Factory.create(:friend))
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should ensure one url is created for every friend" do
|
it "should ensure one url is created for every friend" do
|
||||||
|
|
|
||||||
|
|
@ -16,24 +16,30 @@ describe Blog do
|
||||||
|
|
||||||
it "should add an owner if none is present" do
|
it "should add an owner if none is present" do
|
||||||
b = Factory.create(:blog)
|
b = Factory.create(:blog)
|
||||||
b.owner.should == "bob@aol.com"
|
b.person.email.should == "bob@aol.com"
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "newest" do
|
describe "newest" do
|
||||||
before do
|
before do
|
||||||
(2..4).each { Factory.create(:blog, :owner => "some@dudes.com") }
|
@friend_one = Factory.create(:friend, :email => "some@dudes.com")
|
||||||
|
@friend_two = Factory.create(:friend, :email => "other@dudes.com")
|
||||||
|
(2..4).each { Factory.create(:blog, :person => @friend_one) }
|
||||||
(5..8).each { Factory.create(:blog) }
|
(5..8).each { Factory.create(:blog) }
|
||||||
(9..11).each { Factory.create(:blog, :owner => "other@dudes.com") }
|
(9..11).each { Factory.create(:blog, :person => @friend_two) }
|
||||||
|
Factory.create(:status_message)
|
||||||
|
Factory.create(:bookmark)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should give the most recent blog title and body from owner" do
|
it "should give the most recent blog title and body from owner" do
|
||||||
blog = Blog.my_newest
|
blog = Blog.newest(User.first)
|
||||||
|
blog.class.should == Blog
|
||||||
blog.title.should == "bobby's 8 penguins"
|
blog.title.should == "bobby's 8 penguins"
|
||||||
blog.body.should == "jimmy's huge 8 whales"
|
blog.body.should == "jimmy's huge 8 whales"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should give the most recent blog body for a given email" do
|
it "should give the most recent blog body for a given email" do
|
||||||
blog = Blog.newest("some@dudes.com")
|
blog = Blog.newest_by_email("some@dudes.com")
|
||||||
|
blog.class.should == Blog
|
||||||
blog.title.should == "bobby's 14 penguins"
|
blog.title.should == "bobby's 14 penguins"
|
||||||
blog.body.should == "jimmy's huge 14 whales"
|
blog.body.should == "jimmy's huge 14 whales"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ describe Bookmark do
|
||||||
it "should add an owner if none is present" do
|
it "should add an owner if none is present" do
|
||||||
Factory.create(:user, :email => "bob@aol.com")
|
Factory.create(:user, :email => "bob@aol.com")
|
||||||
n = Factory.create(:bookmark)
|
n = Factory.create(:bookmark)
|
||||||
n.owner.should == "bob@aol.com"
|
n.person.email.should == "bob@aol.com"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should validate its link' do
|
it 'should validate its link' do
|
||||||
|
|
@ -67,11 +67,10 @@ describe Bookmark do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should marshal serialized XML to object' do
|
it 'should marshal serialized XML to object' do
|
||||||
xml = "<bookmark><title>Reddit</message><link>http://reddit.com/</link><owner>bob@aol.com</owner></bookmark>"
|
xml = "<bookmark><title>Reddit</message><link>http://reddit.com/</link></bookmark>"
|
||||||
parsed = Bookmark.from_xml(xml)
|
parsed = Bookmark.from_xml(xml)
|
||||||
parsed.title.should == "Reddit"
|
parsed.title.should == "Reddit"
|
||||||
parsed.link.should == "http://reddit.com/"
|
parsed.link.should == "http://reddit.com/"
|
||||||
parsed.owner.should == "bob@aol.com"
|
|
||||||
parsed.valid?.should be_true
|
parsed.valid?.should be_true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -8,24 +8,13 @@ describe Post do
|
||||||
describe 'defaults' do
|
describe 'defaults' do
|
||||||
before do
|
before do
|
||||||
WebSocket.stub!(:update_clients)
|
WebSocket.stub!(:update_clients)
|
||||||
@post = Factory.create(:post, :person => nil, :owner => nil, :source => nil, :snippet => nil)
|
@post = Factory.create(:post, :person => nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should associate the owner if none is present" do
|
it "should associate the owner if none is present" do
|
||||||
@post.person.should == User.first
|
@post.person.should == User.first
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should add an owner if none is present" do
|
|
||||||
@post.owner.should == "bob@aol.com"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should add a source if none is present" do
|
|
||||||
@post.source.should == "bob@aol.com"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should add a snippet if none is present" do
|
|
||||||
@post.snippet.should == "bob@aol.com"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,19 +12,27 @@ describe StatusMessage do
|
||||||
n.valid?.should be true
|
n.valid?.should be true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
it "should add an owner if none is present" do
|
||||||
|
n = Factory.create(:status_message)
|
||||||
|
n.person.email.should == "bob@aol.com"
|
||||||
|
end
|
||||||
|
|
||||||
describe "newest" do
|
describe "newest" do
|
||||||
before do
|
before do
|
||||||
@friend = Factory.build(:friend, :email => "robert@grimm.com")
|
@person_one = Factory.create(:friend,:email => "some@dudes.com")
|
||||||
(1..5).each { Factory.create(:status_message, :person => @friend) }
|
(1..10).each { Factory.create(:status_message, :person => @person_one) }
|
||||||
(6..10).each { Factory.create(:status_message) }
|
(1..5).each { Factory.create(:status_message) }
|
||||||
|
Factory.create(:bookmark)
|
||||||
|
Factory.create(:bookmark, :person => @person_one)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should give the most recent message from owner" do
|
it "should give the most recent message from a friend" do
|
||||||
StatusMessage.my_newest.message.should == "jimmy's 11 whales"
|
StatusMessage.newest(@person_one).message.should == "jimmy's 13 whales"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should give the most recent message for a given email" do
|
it "should give the most recent message for a given email" do
|
||||||
StatusMessage.newest("some@dudes.com").message.should == "jimmy's 16 whales"
|
StatusMessage.newest_by_email(@person_one.email).message.should == "jimmy's 28 whales"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -35,10 +43,9 @@ describe StatusMessage do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should marshal serialized XML to object' do
|
it 'should marshal serialized XML to object' do
|
||||||
xml = "<statusmessage><message>I hate WALRUSES!</message><owner>Bob@rob.ert</owner></statusmessage>"
|
xml = "<statusmessage><message>I hate WALRUSES!</message></statusmessage>"
|
||||||
parsed = StatusMessage.from_xml(xml)
|
parsed = StatusMessage.from_xml(xml)
|
||||||
parsed.message.should == "I hate WALRUSES!"
|
parsed.message.should == "I hate WALRUSES!"
|
||||||
parsed.owner.should == "Bob@rob.ert"
|
|
||||||
parsed.valid?.should be_true
|
parsed.valid?.should be_true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ Rspec.configure do |config|
|
||||||
DatabaseCleaner.orm = "mongo_mapper"
|
DatabaseCleaner.orm = "mongo_mapper"
|
||||||
|
|
||||||
config.before(:suite) do
|
config.before(:suite) do
|
||||||
DatabaseCleaner.strategy = :transaction
|
|
||||||
DatabaseCleaner.clean_with(:truncation)
|
DatabaseCleaner.clean_with(:truncation)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -32,6 +31,7 @@ Rspec.configure do |config|
|
||||||
|
|
||||||
config.after(:each) do
|
config.after(:each) do
|
||||||
DatabaseCleaner.clean
|
DatabaseCleaner.clean
|
||||||
|
#Factory.sequences.each{ |s| s.reset}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue