diff --git a/app/models/friend_request.rb b/app/models/friend_request.rb
index 46fc65124..1e95d9d09 100644
--- a/app/models/friend_request.rb
+++ b/app/models/friend_request.rb
@@ -1,3 +1,26 @@
class FriendRequest
+ include MongoMapper::Document
+ include Diaspora::Webhooks
+
+ key :url, String
+
+ attr_accessor :sender
+
+ validates_presence_of :url
+
+ before_save :shoot_off
+
+ def to_friend_xml
+ friend = Friend.new
+ friend.email = sender.email
+ friend.url = sender.url
+ friend.profile = sender.profile.clone
+
+ friend.to_xml
+ end
+
+ def shoot_off
+ push_friend_request_to_url(self.url)
+ end
end
diff --git a/lib/common.rb b/lib/common.rb
index 05f5243c3..71f88fe09 100644
--- a/lib/common.rb
+++ b/lib/common.rb
@@ -32,8 +32,9 @@ module Diaspora
objects.each do |p|
if p.is_a? Retraction
-
p.perform
+ elsif p.is_a? Friend
+ p.save
#This line checks if the sender was in the database, among other things?
elsif p.respond_to?(:person) && !(p.person.nil?) #WTF
p.save
@@ -64,6 +65,19 @@ module Diaspora
end
end
+ def push_friend_request_to_url(url)
+ if url
+ url = url + "receive/"
+ xml = "
+
+ #{self.to_friend_xml.to_s}
+
+ "
+ @@queue.add_post_request( [url], xml )
+ @@queue.process
+ end
+ end
+
def prep_webhook
"#{self.to_xml.to_s}"
end
diff --git a/spec/lib/parser_spec.rb b/spec/lib/parser_spec.rb
index bb71b6783..8ba09433c 100644
--- a/spec/lib/parser_spec.rb
+++ b/spec/lib/parser_spec.rb
@@ -108,7 +108,21 @@ describe "parser in application helper" do
store_objects_from_xml( request )
StatusMessage.count.should == 0
end
+
+ it "should create a new friend upon getting a friend request" do
+ friend_request = FriendRequest.new(:url => "http://www.googles.com/")
+ friend_request.sender = @friend
+ xml = "
+
+ #{friend_request.to_friend_xml.to_s}
+
+ "
+ @friend.destroy
+ Friend.count.should be 0
+ store_objects_from_xml(xml)
+ Friend.count.should be 1
+ end
end
end
diff --git a/spec/models/friend_request_spec.rb b/spec/models/friend_request_spec.rb
index 72aede297..281911dfe 100644
--- a/spec/models/friend_request_spec.rb
+++ b/spec/models/friend_request_spec.rb
@@ -1,4 +1,32 @@
require 'spec_helper'
describe FriendRequest do
+
+ it 'should require a url' do
+ friend_request = FriendRequest.new
+ friend_request.valid?.should be false
+ friend_request.url = "http://google.com/"
+ friend_request.valid?.should be true
+ end
+
+ it 'should generate xml for the User as a Friend' do
+ friend_request = FriendRequest.new(:url => "http://www.google.com")
+ user = Factory.create(:user)
+ friend_request.sender = user
+ friend_xml = friend_request.to_friend_xml.to_s
+ friend_xml.include?(user.email).should be true
+ friend_xml.include?(user.url).should be true
+ friend_xml.include?(user.profile.first_name).should be true
+ friend_xml.include?(user.profile.last_name).should be true
+ end
+
+ it 'should be sent to the url upon save' do
+ FriendRequest.send(:class_variable_get, :@@queue).should_receive(:add_post_request)
+
+ friend_request = FriendRequest.new(:url => "http://www.google.com")
+ friend_request.sender = Factory.create(:user)
+ friend_request.save
+
+ end
+
end