diff --git a/app/models/friend_request.rb b/app/models/friend_request.rb index 46fc6512471fe2570f112f9c9123e931f26b0243..1e95d9d097c314db86a1ca97bab267460381ee37 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 05f5243c38de6eeb6fcb2dcf78f24aa2f619a88b..71f88fe098544325be07a7f5f710ab16e40765f4 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 = "<XML> + <posts><post> + #{self.to_friend_xml.to_s} + </post></posts> + </XML>" + @@queue.add_post_request( [url], xml ) + @@queue.process + end + end + def prep_webhook "<post>#{self.to_xml.to_s}</post>" end diff --git a/spec/lib/parser_spec.rb b/spec/lib/parser_spec.rb index bb71b678373eaabaf81d41b7025b1c352fb1ae60..8ba09433cb6303f1251122565f31f915bc9c932e 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 = "<XML> + <posts><post> + #{friend_request.to_friend_xml.to_s} + </post></posts> + </XML>" + @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 72aede2975ab618fdfed49b0bf507279238bc4e8..281911dfe8d5f0a322ccb92397784d0b1bff7239 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