Skip to content
Extraits de code Groupes Projets
Valider 71ab919a rédigé par Raphael's avatar Raphael
Parcourir les fichiers

Remove urls from requests, invitations are broken

parent cf4b810f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -10,37 +10,26 @@ class Request ...@@ -10,37 +10,26 @@ class Request
include Diaspora::Webhooks include Diaspora::Webhooks
include ROXML include ROXML
xml_reader :diaspora_handle xml_reader :sender_handle
xml_reader :destination_url xml_reader :recipient_handle
xml_reader :callback_url
key :aspect_id, ObjectId belongs_to :into, :class => Aspect
key :destination_url, String belongs_to :from, :class => Person
key :callback_url, String belongs_to :to, :class => Person
key :exported_key, String
key :diaspora_handle, String validates_presence_of :from, :to
#before_validation :clean_link
belongs_to :person def self.instantiate(opts = {})
self.new(:from => opts[:from],
validates_presence_of :destination_url, :callback_url :to => opts[:to],
before_validation :clean_link :into => opts[:into])
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])
end end
def reverse_for accepting_user def reverse_for accepting_user
Request.new( Request.new(
:diaspora_handle => accepting_user.diaspora_handle, :from => accepting_user.person,
:destination_url => self.callback_url, :to => self.from
:callback_url => self.destination_url,
:person_id => accepting_user.person.id
) )
end end
...@@ -61,7 +50,23 @@ class Request ...@@ -61,7 +50,23 @@ class Request
Notifier.new_request(user_id, person_id).deliver Notifier.new_request(user_id, person_id).deliver
end 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 protected
def clean_link def clean_link
if self.destination_url if self.destination_url
......
...@@ -370,10 +370,7 @@ class User ...@@ -370,10 +370,7 @@ class User
raise "You already invited this person" raise "You already invited this person"
else else
invitable.pending_requests << Request.create( invitable.pending_requests << Request.create(
:person => request.person, :from => inviter.person)
:diaspora_handle => request.diaspora_handle,
:callback_url => request.callback_url,
:destination_url => request.destination_url)
invitable.inviters << inviter invitable.inviters << inviter
message = attributes.delete(:invite_message) message = attributes.delete(:invite_message)
...@@ -403,11 +400,17 @@ class User ...@@ -403,11 +400,17 @@ class User
self.person.save! self.person.save!
self.invitation_token = nil self.invitation_token = nil
friend_inviters
self.save! self.save!
self self
end end
end end
def friend_inviters
inviters.each do |inviter|
end
end
###Helpers############ ###Helpers############
def self.build(opts = {}) def self.build(opts = {})
u = User.new(opts) u = User.new(opts)
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
= link_to =t('.pending_request'), aspects_manage_path = link_to =t('.pending_request'), aspects_manage_path
- else - else
= form_for Request.new do |f| = 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.hidden_field :destination_url, :value => person.diaspora_handle
= f.submit t('.add_friend') = f.submit t('.add_friend')
......
...@@ -9,13 +9,12 @@ module Diaspora ...@@ -9,13 +9,12 @@ module Diaspora
# should have different exception types for these? # should have different exception types for these?
raise "You cannot befriend yourself" if desired_friend.nil? 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{ raise "You have already sent a friend request to that person!" if self.pending_requests.detect{
|x| x.destination_url == desired_friend.receive_url } |x| x.to == desired_friend}
raise "You are already friends with that person!" if self.friends.detect{ raise "You are already friends with that person!" if contact_for desired_friend
|x| x.person.receive_url == desired_friend.receive_url}
request = Request.instantiate( request = Request.instantiate(
:to => desired_friend.receive_url, :to => desired_friend,
:from => self.person, :from => self.person,
:into => aspect.id) :into => aspect)
if request.save if request.save
self.pending_requests << request self.pending_requests << request
self.save self.save
...@@ -30,25 +29,25 @@ module Diaspora ...@@ -30,25 +29,25 @@ module Diaspora
def accept_friend_request(friend_request_id, aspect_id) def accept_friend_request(friend_request_id, aspect_id)
request = pending_requests.find!(friend_request_id) request = pending_requests.find!(friend_request_id)
pending_request_ids.delete(request.id.to_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) request.reverse_for(self)
end end
def dispatch_friend_acceptance(request, requester) def dispatch_friend_acceptance(request, requester)
push_to_people request, [requester] push_to_people request, [requester]
request.destroy unless request.callback_url.include? url request.destroy unless request.from.owner
end end
def accept_and_respond(friend_request_id, aspect_id) 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) reversed_request = accept_friend_request(friend_request_id, aspect_id)
dispatch_friend_acceptance reversed_request, requester dispatch_friend_acceptance reversed_request, requester
end end
def ignore_friend_request(friend_request_id) def ignore_friend_request(friend_request_id)
request = pending_requests.find!(friend_request_id) request = pending_requests.find!(friend_request_id)
person = request.person person = request.from
self.pending_request_ids.delete(request.id) self.pending_request_ids.delete(request.id)
self.save self.save
...@@ -59,28 +58,27 @@ module Diaspora ...@@ -59,28 +58,27 @@ module Diaspora
def receive_friend_request(friend_request) def receive_friend_request(friend_request)
Rails.logger.info("receiving friend request #{friend_request.to_json}") 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 #response from a friend request you sent
if original_request = original_request(friend_request) 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 original_request
#pp friend_request #pp friend_request
#pp friend_request.person #pp friend_request.person
activate_friend(friend_request.person, destination_aspect) activate_friend(friend_request.from, destination_aspect)
#puts ("#{self.real_name}'s friend request has been accepted") Rails.logger.info("#{self.real_name}'s friend request has been accepted")
friend_request.destroy friend_request.destroy
original_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 #this is a new friend request
elsif !request_from_me?(friend_request) elsif !request_from_me?(friend_request)
self.pending_requests << friend_request self.pending_requests << friend_request
self.save self.save!
#puts ("#{self.real_name} has received a friend request") Rails.logger.info("#{self.real_name} has received a friend request")
friend_request.save friend_request.save
Request.send_new_request(self, friend_request.person) Request.send_new_request(self, friend_request.from)
else else
raise "#{self.real_name} is trying to receive a friend request from himself." raise "#{self.real_name} is trying to receive a friend request from himself."
end end
...@@ -127,15 +125,15 @@ module Diaspora ...@@ -127,15 +125,15 @@ module Diaspora
end end
def request_from_me?(request) def request_from_me?(request)
request.diaspora_handle == self.diaspora_handle request.from == self.person
end end
def original_request(response) 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 end
def requests_for_me 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 end
end end
......
...@@ -24,8 +24,13 @@ module Diaspora ...@@ -24,8 +24,13 @@ module Diaspora
object = Diaspora::Parser.from_xml(xml) object = Diaspora::Parser.from_xml(xml)
Rails.logger.debug("Receiving object for #{self.real_name}:\n#{object.inspect}") Rails.logger.debug("Receiving object for #{self.real_name}:\n#{object.inspect}")
Rails.logger.debug("From: #{object.diaspora_handle}") 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 xml_author = (owns?(object.post))? object.diaspora_handle : object.post.person.diaspora_handle
else else
xml_author = object.diaspora_handle xml_author = object.diaspora_handle
...@@ -39,7 +44,6 @@ module Diaspora ...@@ -39,7 +44,6 @@ module Diaspora
e = EMWebfinger.new(object.diaspora_handle) e = EMWebfinger.new(object.diaspora_handle)
e.on_person do |person| e.on_person do |person|
if person.class == Person if person.class == Person
object.person = person if object.respond_to? :person= object.person = person if object.respond_to? :person=
...@@ -90,8 +94,6 @@ module Diaspora ...@@ -90,8 +94,6 @@ module Diaspora
end end
def receive_request request, person def receive_request request, person
request.person = person
request.person.save!
request.save! request.save!
receive_friend_request(request) receive_friend_request(request)
end end
......
...@@ -34,7 +34,7 @@ module HelperMethods ...@@ -34,7 +34,7 @@ module HelperMethods
user2.reload user2.reload
aspect2.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 user1.reload
aspect1.reload aspect1.reload
......
...@@ -37,14 +37,21 @@ describe Diaspora::Parser do ...@@ -37,14 +37,21 @@ describe Diaspora::Parser do
let(:good_request) { FakeHttpRequest.new(:success)} let(:good_request) { FakeHttpRequest.new(:success)}
it "should create a new person upon getting a person request" do 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.new(:to =>user.person, :from => new_person)
request = Request.instantiate(:to =>"http://www.google.com/", :from => new_person) xml = remote_user.salmon(request).xml_for(user.person)
xml = request.to_diaspora_xml request.delete
user 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 end
...@@ -52,7 +59,7 @@ describe Diaspora::Parser do ...@@ -52,7 +59,7 @@ describe Diaspora::Parser do
it "should activate the Person if I initiated a request to that url" do it "should activate the Person if I initiated a request to that url" do
user.send_friend_request_to(user2.person, aspect) 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) user2.accept_and_respond(request.id, aspect2.id)
user.reload user.reload
......
...@@ -55,6 +55,7 @@ describe 'making sure the spec runner works' do ...@@ -55,6 +55,7 @@ describe 'making sure the spec runner works' do
it 'makes the first user friends with the second' do it 'makes the first user friends with the second' do
contact = @user1.contact_for @user2.person contact = @user1.contact_for @user2.person
contact.should_not be_nil
@user1.friends.include?(contact).should be_true @user1.friends.include?(contact).should be_true
@aspect1.people.include?(contact).should be_true @aspect1.people.include?(contact).should be_true
contact.aspects.include?( @aspect1 ).should be true contact.aspects.include?( @aspect1 ).should be true
...@@ -62,6 +63,7 @@ describe 'making sure the spec runner works' do ...@@ -62,6 +63,7 @@ describe 'making sure the spec runner works' do
it 'makes the second user friends with the first' do it 'makes the second user friends with the first' do
contact = @user2.contact_for @user1.person contact = @user2.contact_for @user1.person
contact.should_not be_nil
@user2.friends.include?(contact).should be_true @user2.friends.include?(contact).should be_true
@aspect2.people.include?(contact).should be_true @aspect2.people.include?(contact).should be_true
contact.aspects.include?( @aspect2 ).should be true contact.aspects.include?( @aspect2 ).should be true
......
...@@ -11,25 +11,32 @@ describe Request do ...@@ -11,25 +11,32 @@ describe Request do
let(:aspect) { user.aspects.create(:name => "dudes") } let(:aspect) { user.aspects.create(:name => "dudes") }
let(:request){ user.send_friend_request_to user2.person, aspect } let(:request){ user.send_friend_request_to user2.person, aspect }
it 'should require a destination and callback url' do describe 'validations' do
person_request = Request.new before do
person_request.valid?.should be false @request = Request.new(:from => user.person, :to => user2.person, :into => aspect)
person_request.destination_url = "http://google.com/" end
person_request.callback_url = "http://foob.com/" it 'is valid' do
person_request.valid?.should be true @request.should be_valid
end @request.from.should == user.person
@request.to.should == user2.person
@request.into.should == aspect
it 'should strip the destination url' do end
person_request = Request.new it 'is from a person' do
person_request.destination_url = " http://google.com/ " @request.from = nil
person_request.send(:clean_link) @request.should_not be_valid
person_request.destination_url.should == "http://google.com/" 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 end
describe '#request_from_me' do describe '#request_from_me' do
it 'recognizes requests from me' do it 'recognizes requests from me' do
request
user.request_from_me?(request).should be_true user.request_from_me?(request).should be_true
end end
...@@ -42,29 +49,76 @@ describe Request do ...@@ -42,29 +49,76 @@ describe Request do
it 'finds requests for that user' do it 'finds requests for that user' do
request request
user2.reload 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
end end
describe 'serialization' do describe '#original_request' do
before do it 'returns nil on a request from me' do
@request = user.send_friend_request_to person, aspect request
@xml = @request.to_xml.to_s user.original_request(request).should be_nil
end end
it 'should not generate xml for the User as a Person' do it 'returns the original request on a response to a request from me' do
@xml.should_not include user.person.profile.first_name new_request = request.reverse_for(user2)
user.original_request(new_request).should == request
end end
end
it 'should serialize the handle and not the sender' do describe 'xml' do
@xml.should include user.person.diaspora_handle before do
@request = Request.new(:from => user.person, :to => user2.person, :into => aspect)
@xml = @request.to_xml.to_s
end 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 it 'does not serialize the id' do
@xml.should_not include user.person.exported_key @xml.should_not include @request.id.to_s
end
end end
it 'does not serialize the id' do describe 'marshalling' do
@xml.should_not include @request.id.to_s 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
end end
...@@ -96,7 +150,6 @@ describe Request do ...@@ -96,7 +150,6 @@ describe Request do
@mock_mail = mock() @mock_mail = mock()
@mock_mail.should_receive(:deliver) @mock_mail.should_receive(:deliver)
end end
describe '.send_request_accepted!' do describe '.send_request_accepted!' do
it 'should deliver the message' do it 'should deliver the message' do
Notifier.should_receive(:request_accepted).and_return(@mock_mail) Notifier.should_receive(:request_accepted).and_return(@mock_mail)
...@@ -112,4 +165,4 @@ describe Request do ...@@ -112,4 +165,4 @@ describe Request do
end end
end end
end end
end end
\ No newline at end of file
...@@ -31,7 +31,7 @@ describe Diaspora::UserModules::Friending do ...@@ -31,7 +31,7 @@ describe Diaspora::UserModules::Friending do
describe '#receive_friend_request' do describe '#receive_friend_request' do
it 'adds a request to pending if it was not sent by user' 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 r.save
user.receive_friend_request(r) user.receive_friend_request(r)
user.reload.pending_requests.should include r user.reload.pending_requests.should include r
...@@ -49,9 +49,9 @@ describe Diaspora::UserModules::Friending do ...@@ -49,9 +49,9 @@ describe Diaspora::UserModules::Friending do
context 'received a friend request' do context 'received a friend request' do
let(:request_for_user) {Request.instantiate(:to => user.receive_url, :from => friend)} let(:request_for_user) {Request.instantiate(:to => user.person, :from => friend)}
let(:request2_for_user) {Request.instantiate(:to => user.receive_url, :from => person_one)} let(:request2_for_user) {Request.instantiate(:to => user.person, :from => person_one)}
let(:request_from_myself) {Request.instantiate(:to => user.receive_url, :from => user.person)} let(:request_from_myself) {Request.instantiate(:to => user.person, :from => user.person)}
before do before do
request_for_user.save request_for_user.save
user.receive_request(request_for_user, friend) user.receive_request(request_for_user, friend)
...@@ -104,9 +104,9 @@ describe Diaspora::UserModules::Friending do ...@@ -104,9 +104,9 @@ describe Diaspora::UserModules::Friending do
user2.pending_requests.empty?.should be true user2.pending_requests.empty?.should be true
user2.friends.empty?.should be true user2.friends.empty?.should be true
@request = Request.instantiate(:to => user.receive_url, :from => person_one) @request = Request.instantiate(:to => user.person, :from => person_one)
@request_two = Request.instantiate(:to => user2.receive_url, :from => person_one) @request_two = Request.instantiate(:to => user2.person, :from => person_one)
@request_three = Request.instantiate(:to => user2.receive_url, :from => user.person) @request_three = Request.instantiate(:to => user2.person, :from => user.person)
@req_xml = @request.to_diaspora_xml @req_xml = @request.to_diaspora_xml
@req_two_xml = @request_two.to_diaspora_xml @req_two_xml = @request_two.to_diaspora_xml
...@@ -183,8 +183,8 @@ describe Diaspora::UserModules::Friending do ...@@ -183,8 +183,8 @@ describe Diaspora::UserModules::Friending do
user.pending_requests.empty?.should be true user.pending_requests.empty?.should be true
user.friends.empty?.should be true user.friends.empty?.should be true
@request = Request.instantiate(:to => user.receive_url, :from => person_one) @request = Request.instantiate(:to => user.person, :from => person_one)
@request_two = Request.instantiate(:to => user.receive_url, :from => person_two) @request_two = Request.instantiate(:to => user.person, :from => person_two)
end end
it "keeps the right counts of friends" do it "keeps the right counts of friends" do
......
...@@ -12,13 +12,6 @@ describe User do ...@@ -12,13 +12,6 @@ describe User do
let(:inviter_with_3_invites) {Factory.create :user, :invites => 3} let(:inviter_with_3_invites) {Factory.create :user, :invites => 3}
let(:aspect2) {inviter_with_3_invites.aspects.create(:name => "Jersey Girls")} 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 context "creating invites" do
it 'requires an apect' do it 'requires an apect' do
proc{inviter.invite_user(:email => "maggie@example.com")}.should raise_error /Must invite into aspect/ proc{inviter.invite_user(:email => "maggie@example.com")}.should raise_error /Must invite into aspect/
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter