diff --git a/app/models/request.rb b/app/models/request.rb index b557a63703b3f1fd5156dfcc29ace6c3b7adaabb..a4e2b1ee6a01d104c2bceae5fe6b877aead6781e 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -10,37 +10,26 @@ class Request include Diaspora::Webhooks include ROXML - xml_reader :diaspora_handle - xml_reader :destination_url - xml_reader :callback_url + xml_reader :sender_handle + xml_reader :recipient_handle - key :aspect_id, ObjectId - key :destination_url, String - key :callback_url, String - key :exported_key, String + belongs_to :into, :class => Aspect + belongs_to :from, :class => Person + belongs_to :to, :class => Person - key :diaspora_handle, String + validates_presence_of :from, :to + #before_validation :clean_link - belongs_to :person - - validates_presence_of :destination_url, :callback_url - before_validation :clean_link - - def self.instantiate(options = {}) - person = options[:from] - self.new(:person_id => person.id, - :destination_url => options[:to], - :callback_url => person.receive_url, - :diaspora_handle => person.diaspora_handle, - :aspect_id => options[:into]) + def self.instantiate(opts = {}) + self.new(:from => opts[:from], + :to => opts[:to], + :into => opts[:into]) end def reverse_for accepting_user Request.new( - :diaspora_handle => accepting_user.diaspora_handle, - :destination_url => self.callback_url, - :callback_url => self.destination_url, - :person_id => accepting_user.person.id + :from => accepting_user.person, + :to => self.from ) end @@ -61,7 +50,23 @@ class Request Notifier.new_request(user_id, person_id).deliver end + def sender_handle + from.diaspora_handle + end + def sender_handle= sender_handle + self.from = Person.first(:diaspora_handle => sender_handle) + end + + def recipient_handle + to.diaspora_handle + end + def recipient_handle= recipient_handle + self.to = Person.first(:diaspora_handle => recipient_handle) + end + def diaspora_handle + self.from.diaspora_handle + end protected def clean_link if self.destination_url diff --git a/app/models/user.rb b/app/models/user.rb index 483134fc6c65e7ac661c7dfdd9670650b700db4b..fb090238ea84b5c46c0f47ca3eee4c71fbca8949 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -370,10 +370,7 @@ class User raise "You already invited this person" else invitable.pending_requests << Request.create( - :person => request.person, - :diaspora_handle => request.diaspora_handle, - :callback_url => request.callback_url, - :destination_url => request.destination_url) + :from => inviter.person) invitable.inviters << inviter message = attributes.delete(:invite_message) @@ -403,11 +400,17 @@ class User self.person.save! self.invitation_token = nil + friend_inviters self.save! self end end + def friend_inviters + inviters.each do |inviter| + end + end + ###Helpers############ def self.build(opts = {}) u = User.new(opts) diff --git a/app/views/people/_person.html.haml b/app/views/people/_person.html.haml index 23ed76f826154131c30d321ceb1417a340f77ef6..6339671c115565bbc2b39ac7b71b83f2b4da93fa 100644 --- a/app/views/people/_person.html.haml +++ b/app/views/people/_person.html.haml @@ -19,7 +19,7 @@ = link_to =t('.pending_request'), aspects_manage_path - else = form_for Request.new do |f| - = f.select(:aspect_id, @aspects_dropdown_array) + = f.select(:into_id, @aspects_dropdown_array) = f.hidden_field :destination_url, :value => person.diaspora_handle = f.submit t('.add_friend') diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb index 549688066228019692cb078f31201b98489b0c79..ca985b2678f75101bad9affdf2ab205cc6384fe0 100644 --- a/lib/diaspora/user/friending.rb +++ b/lib/diaspora/user/friending.rb @@ -9,13 +9,12 @@ module Diaspora # should have different exception types for these? raise "You cannot befriend yourself" if desired_friend.nil? raise "You have already sent a friend request to that person!" if self.pending_requests.detect{ - |x| x.destination_url == desired_friend.receive_url } - raise "You are already friends with that person!" if self.friends.detect{ - |x| x.person.receive_url == desired_friend.receive_url} + |x| x.to == desired_friend} + raise "You are already friends with that person!" if contact_for desired_friend request = Request.instantiate( - :to => desired_friend.receive_url, + :to => desired_friend, :from => self.person, - :into => aspect.id) + :into => aspect) if request.save self.pending_requests << request self.save @@ -30,25 +29,25 @@ module Diaspora def accept_friend_request(friend_request_id, aspect_id) request = pending_requests.find!(friend_request_id) pending_request_ids.delete(request.id.to_id) - activate_friend(request.person, aspect_by_id(aspect_id)) + activate_friend(request.from, aspect_by_id(aspect_id)) request.reverse_for(self) end def dispatch_friend_acceptance(request, requester) push_to_people request, [requester] - request.destroy unless request.callback_url.include? url + request.destroy unless request.from.owner end def accept_and_respond(friend_request_id, aspect_id) - requester = pending_requests.find!(friend_request_id).person + requester = pending_requests.find!(friend_request_id).from reversed_request = accept_friend_request(friend_request_id, aspect_id) dispatch_friend_acceptance reversed_request, requester end def ignore_friend_request(friend_request_id) request = pending_requests.find!(friend_request_id) - person = request.person + person = request.from self.pending_request_ids.delete(request.id) self.save @@ -59,28 +58,27 @@ module Diaspora def receive_friend_request(friend_request) Rails.logger.info("receiving friend request #{friend_request.to_json}") - #puts ("receiving friend request #{friend_request.to_json}") #response from a friend request you sent if original_request = original_request(friend_request) - destination_aspect = self.aspect_by_id(original_request.aspect_id) + destination_aspect = self.aspect_by_id(original_request.into_id) #pp original_request #pp friend_request #pp friend_request.person - activate_friend(friend_request.person, destination_aspect) - #puts ("#{self.real_name}'s friend request has been accepted") + activate_friend(friend_request.from, destination_aspect) + Rails.logger.info("#{self.real_name}'s friend request has been accepted") friend_request.destroy original_request.destroy - Request.send_request_accepted(self, friend_request.person, destination_aspect) + Request.send_request_accepted(self, friend_request.from, destination_aspect) #this is a new friend request elsif !request_from_me?(friend_request) self.pending_requests << friend_request - self.save - #puts ("#{self.real_name} has received a friend request") + self.save! + Rails.logger.info("#{self.real_name} has received a friend request") friend_request.save - Request.send_new_request(self, friend_request.person) + Request.send_new_request(self, friend_request.from) else raise "#{self.real_name} is trying to receive a friend request from himself." end @@ -127,15 +125,15 @@ module Diaspora end def request_from_me?(request) - request.diaspora_handle == self.diaspora_handle + request.from == self.person end def original_request(response) - pending_requests.find_by_destination_url(response.callback_url) unless response.nil? || response.id.nil? + pending_requests.first(:from_id => self.person.id, :to_id => response.from.id) end def requests_for_me - pending_requests.select{|req| req.destination_url == self.person.receive_url} + pending_requests.select{|req| req.to == self.person} end end end diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index 1bbd8edad25e795d09c90822659b983dbb684c50..655d42f8cde3d36f5fa43d56efbb987986b46b57 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -24,8 +24,13 @@ module Diaspora object = Diaspora::Parser.from_xml(xml) Rails.logger.debug("Receiving object for #{self.real_name}:\n#{object.inspect}") Rails.logger.debug("From: #{object.diaspora_handle}") - - if object.is_a?(Comment) + + if object.is_a?(Request) + salmon_author.save + object.sender_handle = salmon_author.diaspora_handle + end + + if object.is_a?(Comment) xml_author = (owns?(object.post))? object.diaspora_handle : object.post.person.diaspora_handle else xml_author = object.diaspora_handle @@ -39,7 +44,6 @@ module Diaspora e = EMWebfinger.new(object.diaspora_handle) e.on_person do |person| - if person.class == Person object.person = person if object.respond_to? :person= @@ -90,8 +94,6 @@ module Diaspora end def receive_request request, person - request.person = person - request.person.save! request.save! receive_friend_request(request) end diff --git a/spec/helper_methods.rb b/spec/helper_methods.rb index a48e8ab27f328e2b65368d2bcbbfbeb82f30fa12..6bedafbd12fbf7f05dc1f988dac30348ca4259f0 100644 --- a/spec/helper_methods.rb +++ b/spec/helper_methods.rb @@ -34,7 +34,7 @@ module HelperMethods user2.reload aspect2.reload - new_request = user2.pending_requests.find_by_destination_url!(user2.receive_url) + new_request = user2.pending_requests.find_by_from_id!(user1.person.id) user1.reload aspect1.reload diff --git a/spec/lib/diaspora/parser_spec.rb b/spec/lib/diaspora/parser_spec.rb index fd363ee4749d6ccbefe419ba0e1bebfbb697c4bd..96971b036e99eb3cc261b59855e94c1cb9555981 100644 --- a/spec/lib/diaspora/parser_spec.rb +++ b/spec/lib/diaspora/parser_spec.rb @@ -37,14 +37,21 @@ describe Diaspora::Parser do let(:good_request) { FakeHttpRequest.new(:success)} it "should create a new person upon getting a person request" do - new_person = Factory.build(:person) + remote_user = Factory.create(:user) + new_person = remote_user.person - Person.should_receive(:by_account_identifier).and_return(new_person) - request = Request.instantiate(:to =>"http://www.google.com/", :from => new_person) - xml = request.to_diaspora_xml - user + request = Request.new(:to =>user.person, :from => new_person) + xml = remote_user.salmon(request).xml_for(user.person) + request.delete + request.from.delete + remote_user.delete + new_person.delete - lambda { user.receive xml, new_person }.should change(Person, :count).by(1) + Person.should_receive(:by_account_identifier).twice.and_return(new_person) + + lambda { + user.receive_salmon xml + }.should change(Person, :count).by(1) end @@ -52,7 +59,7 @@ describe Diaspora::Parser do it "should activate the Person if I initiated a request to that url" do user.send_friend_request_to(user2.person, aspect) - request = user2.reload.pending_requests.find_by_destination_url!(user2.receive_url) + request = user2.reload.pending_requests.find_by_to_id!(user2.person.id) user2.accept_and_respond(request.id, aspect2.id) user.reload diff --git a/spec/misc_spec.rb b/spec/misc_spec.rb index bd81a1887169b123ceb7e2359f01a7b5195bad94..9a6a5b3d457c9ffadc23592ef534837363f80d77 100644 --- a/spec/misc_spec.rb +++ b/spec/misc_spec.rb @@ -55,6 +55,7 @@ describe 'making sure the spec runner works' do it 'makes the first user friends with the second' do contact = @user1.contact_for @user2.person + contact.should_not be_nil @user1.friends.include?(contact).should be_true @aspect1.people.include?(contact).should be_true contact.aspects.include?( @aspect1 ).should be true @@ -62,6 +63,7 @@ describe 'making sure the spec runner works' do it 'makes the second user friends with the first' do contact = @user2.contact_for @user1.person + contact.should_not be_nil @user2.friends.include?(contact).should be_true @aspect2.people.include?(contact).should be_true contact.aspects.include?( @aspect2 ).should be true diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index d6801a175d8e57616c3490230e220e277f85a280..8da4153c980485ee94297f8bd7e5e9c4d86f553f 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -11,25 +11,32 @@ describe Request do let(:aspect) { user.aspects.create(:name => "dudes") } let(:request){ user.send_friend_request_to user2.person, aspect } - it 'should require a destination and callback url' do - person_request = Request.new - person_request.valid?.should be false - person_request.destination_url = "http://google.com/" - person_request.callback_url = "http://foob.com/" - person_request.valid?.should be true - end - - - it 'should strip the destination url' do - person_request = Request.new - person_request.destination_url = " http://google.com/ " - person_request.send(:clean_link) - person_request.destination_url.should == "http://google.com/" + describe 'validations' do + before do + @request = Request.new(:from => user.person, :to => user2.person, :into => aspect) + end + it 'is valid' do + @request.should be_valid + @request.from.should == user.person + @request.to.should == user2.person + @request.into.should == aspect + end + it 'is from a person' do + @request.from = nil + @request.should_not be_valid + end + it 'is to a person' do + @request.to = nil + @request.should_not be_valid + end + it 'is not necessarily into an aspect' do + @request.into = nil + @request.should be_valid + end end describe '#request_from_me' do it 'recognizes requests from me' do - request user.request_from_me?(request).should be_true end @@ -42,29 +49,76 @@ describe Request do it 'finds requests for that user' do request user2.reload - user2.requests_for_me.detect{|r| r.callback_url == user.receive_url}.should_not be_nil + user2.requests_for_me.detect{|r| r.from == user.person}.should_not be_nil end end - describe 'serialization' do - before do - @request = user.send_friend_request_to person, aspect - @xml = @request.to_xml.to_s + describe '#original_request' do + it 'returns nil on a request from me' do + request + user.original_request(request).should be_nil end - it 'should not generate xml for the User as a Person' do - @xml.should_not include user.person.profile.first_name + it 'returns the original request on a response to a request from me' do + new_request = request.reverse_for(user2) + user.original_request(new_request).should == request end + end - it 'should serialize the handle and not the sender' do - @xml.should include user.person.diaspora_handle + describe 'xml' do + before do + @request = Request.new(:from => user.person, :to => user2.person, :into => aspect) + @xml = @request.to_xml.to_s end + describe 'serialization' do + it 'should not generate xml for the User as a Person' do + @xml.should_not include user.person.profile.first_name + end + + it 'should serialize the handle and not the sender' do + @xml.should include user.person.diaspora_handle + end + + it 'serializes the intended recipient handle' do + @xml.should include user2.person.diaspora_handle + end + + it 'should not serialize the exported key' do + @xml.should_not include user.person.exported_key + end - it 'should not serialize the exported key' do - @xml.should_not include user.person.exported_key + it 'does not serialize the id' do + @xml.should_not include @request.id.to_s + end end - it 'does not serialize the id' do - @xml.should_not include @request.id.to_s + describe 'marshalling' do + before do + @marshalled = Request.from_xml @xml + end + it 'marshals the sender' do + @marshalled.from.should == user.person + end + it 'marshals the recipient' do + @marshalled.to.should == user2.person + end + it 'knows nothing about the aspect' do + @marshalled.into.should be_nil + end + end + describe 'marshalling with diaspora wrapper' do + before do + @d_xml = @request.to_diaspora_xml + @marshalled = Diaspora::Parser.from_xml @d_xml + end + it 'marshals the sender' do + @marshalled.from.should == user.person + end + it 'marshals the recipient' do + @marshalled.to.should == user2.person + end + it 'knows nothing about the aspect' do + @marshalled.into.should be_nil + end end end @@ -96,7 +150,6 @@ describe Request do @mock_mail = mock() @mock_mail.should_receive(:deliver) end - describe '.send_request_accepted!' do it 'should deliver the message' do Notifier.should_receive(:request_accepted).and_return(@mock_mail) @@ -112,4 +165,4 @@ describe Request do end end end -end \ No newline at end of file +end diff --git a/spec/models/user/friending_spec.rb b/spec/models/user/friending_spec.rb index d95bb6981466b4c9dda1b61cbcfc0c4e2dd0247f..8099283528fd2b65690c6208d04e702163e5d1d3 100644 --- a/spec/models/user/friending_spec.rb +++ b/spec/models/user/friending_spec.rb @@ -31,7 +31,7 @@ describe Diaspora::UserModules::Friending do describe '#receive_friend_request' do it 'adds a request to pending if it was not sent by user' do - r = Request.instantiate(:to => user.receive_url, :from => friend) + r = Request.instantiate(:to => user.person, :from => friend) r.save user.receive_friend_request(r) user.reload.pending_requests.should include r @@ -49,9 +49,9 @@ describe Diaspora::UserModules::Friending do context 'received a friend request' do - let(:request_for_user) {Request.instantiate(:to => user.receive_url, :from => friend)} - let(:request2_for_user) {Request.instantiate(:to => user.receive_url, :from => person_one)} - let(:request_from_myself) {Request.instantiate(:to => user.receive_url, :from => user.person)} + let(:request_for_user) {Request.instantiate(:to => user.person, :from => friend)} + let(:request2_for_user) {Request.instantiate(:to => user.person, :from => person_one)} + let(:request_from_myself) {Request.instantiate(:to => user.person, :from => user.person)} before do request_for_user.save user.receive_request(request_for_user, friend) @@ -104,9 +104,9 @@ describe Diaspora::UserModules::Friending do user2.pending_requests.empty?.should be true user2.friends.empty?.should be true - @request = Request.instantiate(:to => user.receive_url, :from => person_one) - @request_two = Request.instantiate(:to => user2.receive_url, :from => person_one) - @request_three = Request.instantiate(:to => user2.receive_url, :from => user.person) + @request = Request.instantiate(:to => user.person, :from => person_one) + @request_two = Request.instantiate(:to => user2.person, :from => person_one) + @request_three = Request.instantiate(:to => user2.person, :from => user.person) @req_xml = @request.to_diaspora_xml @req_two_xml = @request_two.to_diaspora_xml @@ -183,8 +183,8 @@ describe Diaspora::UserModules::Friending do user.pending_requests.empty?.should be true user.friends.empty?.should be true - @request = Request.instantiate(:to => user.receive_url, :from => person_one) - @request_two = Request.instantiate(:to => user.receive_url, :from => person_two) + @request = Request.instantiate(:to => user.person, :from => person_one) + @request_two = Request.instantiate(:to => user.person, :from => person_two) end it "keeps the right counts of friends" do diff --git a/spec/models/user/invite_spec.rb b/spec/models/user/invite_spec.rb index c8db5ce28a90af422a4b4ce433d1999d0362d18c..1a926201ee92751d8d78c11491fd4d59e3a5011d 100644 --- a/spec/models/user/invite_spec.rb +++ b/spec/models/user/invite_spec.rb @@ -12,13 +12,6 @@ describe User do let(:inviter_with_3_invites) {Factory.create :user, :invites => 3} let(:aspect2) {inviter_with_3_invites.aspects.create(:name => "Jersey Girls")} - - before do - deliverable = Object.new - deliverable.stub!(:deliver) - ::Devise.mailer.stub!(:invitation).and_return(deliverable) - end - context "creating invites" do it 'requires an apect' do proc{inviter.invite_user(:email => "maggie@example.com")}.should raise_error /Must invite into aspect/