diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index cf600b608..1b03a3660 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -54,6 +54,9 @@ class ServicesController < ApplicationController @uid = params[:uid] @subject = "Join me on DIASPORA*" @message = "" + + current_user.invite_user(aspect_id, :service => params[:provider], + :identifier => params[:uid]) redirect_to "http://facebook.com" end diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 108b75fb4..822304c0c 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -11,8 +11,9 @@ class Invitation < ActiveRecord::Base validates_presence_of :sender, :recipient, :aspect def self.invite(opts = {}) - return false if opts[:email] == opts[:from].email - existing_user = User.where(:email => opts[:email]).first + return false if opts[:identifier] == opts[:from].email + existing_user = User.where(:email => opts[:identifier]).first + if existing_user if opts[:from].contact_for(opts[:from].person) raise "You are already connceted to this person" @@ -39,8 +40,8 @@ class Invitation < ActiveRecord::Base end def self.create_invitee(opts = {}) - invitee = new_or_existing_user_by_email(opts[:email]) - return invitee unless opts[:email].match Devise.email_regexp + invitee = new_or_existing_user_by_email(opts[:identifier]) + return invitee unless opts[:identifier].match Devise.email_regexp invitee.invites = opts[:invites] || 0 if invitee.new_record? invitee.errors.clear @@ -62,7 +63,7 @@ class Invitation < ActiveRecord::Base invitee.reload end invitee.invite! - Rails.logger.info("event=invitation_sent to=#{opts[:email]} #{"inviter=#{opts[:from].diaspora_handle}" if opts[:from]}") + Rails.logger.info("event=invitation_sent to=#{opts[:identifier]} #{"inviter=#{opts[:from].diaspora_handle}" if opts[:from]}") invitee end diff --git a/app/models/jobs/invite_user.rb b/app/models/jobs/invite_user.rb index 273f76279..f316afddc 100644 --- a/app/models/jobs/invite_user.rb +++ b/app/models/jobs/invite_user.rb @@ -8,7 +8,7 @@ module Job @queue = :mail def self.perform_delegate(sender_id, email, aspect_id, invite_message) user = User.find(sender_id) - user.invite_user(email, aspect_id, invite_message) + user.invite_user(aspect_id, 'email', email, invite_message) end end end diff --git a/app/models/user.rb b/app/models/user.rb index fcc4ba3b5..5147904bd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -204,10 +204,11 @@ class User < ActiveRecord::Base end ###Invitations############ - def invite_user(email, aspect_id, invite_message = "") + def invite_user(aspect_id, service, identifier, invite_message = "") aspect = aspects.find(aspect_id) if aspect - Invitation.invite(:email => email, + Invitation.invite(:service => service, + :identifier => identifier, :from => self, :into => aspect, :message => invite_message) diff --git a/lib/rake_helpers.rb b/lib/rake_helpers.rb index bd8624078..6ff16ef7f 100644 --- a/lib/rake_helpers.rb +++ b/lib/rake_helpers.rb @@ -21,7 +21,7 @@ module RakeHelpers backer_email = backers[n+offset][1].to_s.gsub('.ksr', '').strip unless User.find_by_email(backer_email) puts "sending email to: #{backer_name} #{backer_email}" unless Rails.env == 'test' - Invitation.create_invitee(:email => backer_email, :name => backer_name, :invites => num_invites) unless test + Invitation.create_invitee(:service => 'email', :identifier => backer_email, :name => backer_name, :invites => num_invites) unless test else puts "user with the email exists: #{backer_email} , #{backer_name} " unless Rails.env == 'test' end diff --git a/spec/controllers/invitations_controller_spec.rb b/spec/controllers/invitations_controller_spec.rb index 0b3e8caca..b27d63294 100644 --- a/spec/controllers/invitations_controller_spec.rb +++ b/spec/controllers/invitations_controller_spec.rb @@ -68,7 +68,7 @@ describe InvitationsController do describe "#update" do before do @user.invites = 5 - @invited_user = @user.invite_user("a@a.com", @aspect.id) + @invited_user = @user.invite_user(@aspect.id, 'email', "a@a.com") @accept_params = {:user=> {:password_confirmation =>"password", :username=>"josh", diff --git a/spec/models/invitation_spec.rb b/spec/models/invitation_spec.rb index 29fe1e135..38b09f4d2 100644 --- a/spec/models/invitation_spec.rb +++ b/spec/models/invitation_spec.rb @@ -70,41 +70,41 @@ describe Invitation do describe '.invite' do it 'creates an invitation' do lambda { - Invitation.invite(:email => @email, :from => user, :into => aspect) + Invitation.invite(:service => 'email', :identifier => @email, :from => user, :into => aspect) }.should change(Invitation, :count).by(1) end it 'associates the invitation with the inviter' do lambda { - Invitation.invite(:email => @email, :from => user, :into => aspect) + Invitation.invite(:service => 'email', :identifier => @email, :from => user, :into => aspect) }.should change{user.reload.invitations_from_me.count}.by(1) end it 'associates the invitation with the invitee' do - new_user = Invitation.invite(:email => @email, :from => user, :into => aspect) + new_user = Invitation.invite(:service => 'email', :identifier => @email, :from => user, :into => aspect) new_user.invitations_to_me.count.should == 1 end it 'creates a user' do lambda { - Invitation.invite(:from => user, :email => @email, :into => aspect) + Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect) }.should change(User, :count).by(1) end it 'returns the new user' do - new_user = Invitation.invite(:from => user, :email => @email, :into => aspect) + new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect) new_user.is_a?(User).should be_true new_user.email.should == @email end it 'adds the inviter to the invited_user' do - new_user = Invitation.invite(:from => user, :email => @email, :into => aspect) + new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect) new_user.invitations_to_me.first.sender.should == user end it 'adds an optional message' do message = "How've you been?" - new_user = Invitation.invite(:from => user, :email => @email, :into => aspect, :message => message) + new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message) new_user.invitations_to_me.first.message.should == message end @@ -114,13 +114,13 @@ describe Invitation do a.should == user2.person b.should == aspect } - Invitation.invite(:from => user, :email => user2.email, :into => aspect) + Invitation.invite(:from => user, :service => 'email', :identifier => user2.email, :into => aspect) end it 'decrements the invite count of the from user' do message = "How've you been?" lambda{ - new_user = Invitation.invite(:from => user, :email => @email, :into => aspect, :message => message) + new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message) }.should change(user, :invites).by(-1) end @@ -129,13 +129,13 @@ describe Invitation do user.save! message = "How've you been?" lambda { - new_user = Invitation.invite(:from => user, :email => @email, :into => aspect, :message => message) + new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message) }.should_not change(user, :invites) end context 'invalid email' do it 'return a user with errors' do - new_user = Invitation.invite(:email => "fkjlsdf", :from => user, :into => aspect) + new_user = Invitation.invite(:service => 'email', :identifier => "fkjlsdf", :from => user, :into => aspect) new_user.should have(1).errors_on(:email) new_user.should_not be_persisted end @@ -146,10 +146,11 @@ describe Invitation do context 'with an existing invitee' do before do @valid_params = {:from => user, - :email => @email, + :service => 'email', + :identifier => @email, :into => aspect, :message => @message} - @invitee = Invitation.create_invitee(:email => @email) + @invitee = Invitation.create_invitee(:service => 'email', :identifier => @email) end it 'creates no user' do lambda { @@ -176,7 +177,7 @@ describe Invitation do context 'with an inviter' do before do @message = "whatever" - @valid_params = {:from => user, :email => @email, :into => aspect, :message => @message} + @valid_params = {:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => @message} end it 'sends mail' do @@ -196,7 +197,7 @@ describe Invitation do end it "doesn't create an invitation if the email is invalid" do - new_user = Invitation.create_invitee(@valid_params.merge(:email => 'fdfdfdfdf')) + new_user = Invitation.create_invitee(@valid_params.merge(:identifier => 'fdfdfdfdf')) new_user.should_not be_persisted new_user.should have(1).error_on(:email) end @@ -204,27 +205,27 @@ describe Invitation do context 'with no inviter' do it 'sends an email that includes the right things' do - Invitation.create_invitee(:email => @email) + Invitation.create_invitee(:service => 'email', :identifier => @email) Devise.mailer.deliveries.first.to_s.include?("Welcome #{@email}").should == true end it 'creates a user' do lambda { - Invitation.create_invitee(:email => @email) + Invitation.create_invitee(:service => 'email', :identifier => @email) }.should change(User, :count).by(1) end it 'sends email to the invited user' do lambda { - Invitation.create_invitee(:email => @email) + Invitation.create_invitee(:service => 'email', :identifier => @email) }.should change{Devise.mailer.deliveries.size}.by(1) end it 'does not render nonsensical emails' do - Invitation.create_invitee(:email => @email) + Invitation.create_invitee(:service => 'email', :identifier => @email) Devise.mailer.deliveries.first.body.raw_source.match(/have invited you to join/i).should be_false end it 'creates an invitation' do pending "Invitations should be more flexible, allowing custom messages to be passed in without an inviter." lambda { - Invitation.create_invitee(:email => @email) + Invitation.create_invitee(:service => 'email', :identifier => @email) }.should change(Invitation, :count).by(1) end end @@ -232,7 +233,7 @@ describe Invitation do describe '#to_request!' do before do - @new_user = Invitation.invite(:from => user, :email => @email, :into => aspect) + @new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect) acceptance_params = {:invitation_token => "abc", :username => "user", :password => "secret", diff --git a/spec/models/user/invite_spec.rb b/spec/models/user/invite_spec.rb index 7f5c9612a..ff1323f34 100644 --- a/spec/models/user/invite_spec.rb +++ b/spec/models/user/invite_spec.rb @@ -12,24 +12,40 @@ describe User do let(:inviter_with_3_invites) { new_user = Factory.create(:user); new_user.invites = 3; new_user.save; new_user;} let(:aspect2) {inviter_with_3_invites.aspects.create(:name => "Jersey Girls")} + before do + @email = "bob@bob.com" + end + context "creating invites" do it 'requires your aspect' do lambda { - inviter.invite_user("maggie@example.com", wrong_aspect.id) + inviter.invite_user(wrong_aspect.id, "email", "maggie@example.com") }.should raise_error ActiveRecord::RecordNotFound end + + it 'takes a service parameter' do + @invite_params = {:service => 'email'} + Invitation.should_receive(:invite).with(hash_including(@invite_params)) + inviter.invite_user(aspect.id, 'email', @email) + end + + it 'takes an indentifier parameter' do + @invite_params = {:identifier => @email} + Invitation.should_receive(:invite).with(hash_including(@invite_params)) + inviter.invite_user(aspect.id, 'email', @email) + end it 'calls Invitation.invite' do Invitation.should_receive(:invite) - inviter.invite_user(@email, aspect.id) + inviter.invite_user(aspect.id, 'email', @email) end it 'has an invitation' do - inviter.invite_user("joe@example.com", aspect.id).invitations_to_me.count.should == 1 + inviter.invite_user(aspect.id, 'email', @email).invitations_to_me.count.should == 1 end it 'creates it with an email' do - inviter.invite_user("joe@example.com", aspect.id).email.should == "joe@example.com" + inviter.invite_user(aspect.id, 'email', @email).email.should == @email end @@ -37,7 +53,7 @@ describe User do connect_users(inviter, aspect, another_user, wrong_aspect) inviter.reload proc{ - inviter.invite_user(another_user.email, aspect.id) + inviter.invite_user(aspect.id, 'email', another_user.email) }.should raise_error ActiveRecord::RecordInvalid end @@ -46,9 +62,9 @@ describe User do context "limit on invites" do it 'does not invite people I already invited' do - inviter_with_3_invites.invite_user("email1@example.com", aspect2.id) + inviter_with_3_invites.invite_user(aspect2.id, 'email', "email1@example.com") proc{ - inviter_with_3_invites.invite_user("email1@example.com", aspect2.id) + inviter_with_3_invites.invite_user(aspect2.id, 'email', "email1@example.com") }.should raise_error /You already invited this person/ end end @@ -63,7 +79,7 @@ describe User do :last_name => "Smith"}} )} before do - @invited_user_pre = Invitation.invite(:from => inviter, :email => 'invitee@example.org', :into => aspect).reload + @invited_user_pre = Invitation.invite(:from => inviter, :service => 'email', :identifier => 'invitee@example.org', :into => aspect).reload @person_count = Person.count end