Remove urls from requests, invitations are broken

This commit is contained in:
Raphael 2010-11-08 18:37:04 -08:00
parent cf4b810fc8
commit 71ab919ad3
11 changed files with 172 additions and 109 deletions

View file

@ -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

View file

@ -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)

View file

@ -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')

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
describe '#original_request' do
it 'returns nil on a request from me' do
request
user.original_request(request).should be_nil
end
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
describe 'xml' do
before do
@request = user.send_friend_request_to person, aspect
@request = Request.new(:from => user.person, :to => user2.person, :into => aspect)
@xml = @request.to_xml.to_s
end
it 'should not generate xml for the User as a Person' do
@xml.should_not include user.person.profile.first_name
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 'does not serialize the id' do
@xml.should_not include @request.id.to_s
end
end
it 'should serialize the handle and not the sender' do
@xml.should include user.person.diaspora_handle
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
it 'should not serialize the exported key' do
@xml.should_not include user.person.exported_key
end
it 'does not serialize the id' do
@xml.should_not include @request.id.to_s
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
end

View file

@ -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

View file

@ -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/