diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index 452cc0319a2394b75727b3319821196cbf0f0f0f..55838a21bab4a890b4e4647ffa033cc419a1cf9d 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -52,15 +52,13 @@ class ServicesController < ApplicationController def inviter @uid = params[:uid] - @subject = "Join me on DIASPORA*" - + @subject = t('.join_me_on_diaspora') invited_user = current_user.invite_user(params[:aspect_id], params[:provider], params[:uid]) - @message = <<MSG - Diaspora* is the social network that puts you in control of your information. You decide what you'd like to share, and with whom. You retain full ownership of all your information, including friend lists, messages, photos, and profile details. - - Click here to accept your invitation: - #{accept_invitation_url(invited_user, :invitation_token => invited_user.invitation_token)} +#{t('.click_link_to_accept_invitation')}: +\n +\n +#{accept_invitation_url(invited_user, :invitation_token => invited_user.invitation_token)} MSG redirect_to "https://www.facebook.com/?compose=1&id=#{@uid}&subject=#{@subject}&message=#{@message}&sk=messages" end diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 134cb28cf824224474c59d058beefbc533b06f0a..ddee7c94266fc548dc5d06341e3e188d49ff236c 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -12,7 +12,8 @@ class Invitation < ActiveRecord::Base def self.invite(opts = {}) return false if opts[:identifier] == opts[:from].email - existing_user = User.where(:email => opts[:identifier]).first + + existing_user = self.find_existing_user(opts[:service], opts[:identifier]) if existing_user if opts[:from].contact_for(opts[:from].person) @@ -24,10 +25,12 @@ class Invitation < ActiveRecord::Base raise "You already invited this person" end end + + opts[:existing_user] = existing_user create_invitee(opts) end - def self.new_or_existing_user_by_service_and_identifier(service, identifier) + def self.find_existing_user(service, identifier) existing_user = User.where(:invitation_service => service, :invitation_identifier => identifier).first if service == 'email' @@ -36,20 +39,20 @@ class Invitation < ActiveRecord::Base existing_user ||= User.joins(:services).where(:services => {:provider => service, :uid => identifier}).first end - if existing_user - existing_user - else - result = User.new() - result.invitation_service = service - result.invitation_identifier = identifier - result.email = identifier if service == 'email' - result.valid? - result - end + existing_user + end + + def self.new_user_by_service_and_identifier(service, identifier) + result = User.new() + result.invitation_service = service + result.invitation_identifier = identifier + result.email = identifier if service == 'email' + result.valid? + result end def self.create_invitee(opts = {}) - invitee = new_or_existing_user_by_service_and_identifier(opts[:service], opts[:identifier]) + invitee = opts[:existing_user] || new_user_by_service_and_identifier(opts[:service], opts[:identifier]) return invitee if opts[:service] == 'email' && !opts[:identifier].match(Devise.email_regexp) invitee.invites = opts[:invites] || 0 if invitee.new_record? diff --git a/app/views/invitations/edit.html.haml b/app/views/invitations/edit.html.haml index 0377c128c24e53dd7dfd82cc93493a3e0d074b82..42c526137fee7abb173d41603e186b66266dc303 100644 --- a/app/views/invitations/edit.html.haml +++ b/app/views/invitations/edit.html.haml @@ -13,6 +13,9 @@ %p = f.label :username , t('username') = f.text_field :username, :title => t('registrations.new.enter_username') + %p + = f.label :email , t('email') + = f.text_field :email, :title => t('registrations.new.enter_email') %p = f.label :password , t('password') = f.password_field :password, :title => t('registrations.new.enter_password') diff --git a/app/views/services/finder.html.haml b/app/views/services/finder.html.haml index 502fb49a0ddeaaa435f63151b3a2ed5ae4d6ab55..1d5f17c106d3ee2409fdf8adeab250850f1ecb1c 100644 --- a/app/views/services/finder.html.haml +++ b/app/views/services/finder.html.haml @@ -30,7 +30,7 @@ :rel => 'facebox' - else = form_tag service_inviter_path do - = select_tag (:aspect_id, options_from_collection_for_select(@all_aspects, 'id', 'name')) + = select_tag(:aspect_id, options_from_collection_for_select(@all_aspects, 'id', 'name')) = hidden_field_tag :uid, uid = hidden_field_tag :provider, 'facebook' = submit_tag "invite" diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 3acbc3bb8bf68fa70c6afac3c82b06010aefec35..d927ba72136a13bcb73fab53978187c88031e405 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -424,6 +424,9 @@ en: success: "Successfully deleted authentication." failure: error: "there was an error connecting that service" + inviter: + join_me_on_diaspora: "Join me on DIASPORA*" + click_link_to_accept_invitation: "Click this link to accept your invitation" notifier: hello: "Hello %{name}!" love: "love," diff --git a/db/schema.rb b/db/schema.rb index 85b57776edb966b927563a0788ffa5445fc8e8ae..f4baacd553956f9a7c6649b0cd08997c1b26ed2a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110126225202) do +ActiveRecord::Schema.define(:version => 20110126232040) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id" @@ -453,6 +453,7 @@ ActiveRecord::Schema.define(:version => 20110126225202) do end add_index "users", ["email"], :name => "index_users_on_email" + add_index "users", ["invitation_service", "invitation_identifier"], :name => "index_users_on_invitation_service_and_invitation_identifier", :unique => true add_index "users", ["invitation_token"], :name => "index_users_on_invitation_token" add_index "users", ["mongo_id"], :name => "index_users_on_mongo_id" add_index "users", ["username"], :name => "index_users_on_username", :unique => true diff --git a/spec/models/invitation_spec.rb b/spec/models/invitation_spec.rb index 4c7ebe3dec81a3911380c27c9ef53cc3e5441124..ba18082894605654227571db1ec8fbf600715022 100644 --- a/spec/models/invitation_spec.rb +++ b/spec/models/invitation_spec.rb @@ -45,16 +45,8 @@ describe Invitation do @invitation.message.should == "!" end - describe '.new_or_existing_user_by_email' do - let(:inv){Invitation.new_or_existing_user_by_service_and_identifier(@type, @identifier)} - before do - @users = [] - 8.times do - @users << Factory.create(:user) - end - @user_fb_id = 'abc123' - @user_fb = Factory.create(:user, :invitation_service => "facebook", :invitation_identifier => @user_fb_id) - end + describe '.new_user_by_service_and_identifier' do + let(:inv){Invitation.new_user_by_service_and_identifier(@type, @identifier)} it 'returns User.new for a non-existent user for email' do @type = "email" @@ -77,9 +69,21 @@ describe Invitation do inv.reload }.should raise_error ActiveRecord::RecordNotFound end + end + + describe '.find_existing_user' do + let(:inv){Invitation.find_existing_user(@type, @identifier)} + before do + @users = [] + 8.times do + @users << Factory.create(:user) + end + @user_fb_id = 'abc123' + @user_fb = Factory.create(:user, :invitation_service => "facebook", :invitation_identifier => @user_fb_id) + end - context 'returns an existing user' do - context 'active users' do + context 'send a request to an existing' do + context 'active user' do it 'by email' do @identifier = @users[3].email @type = 'email' @@ -98,7 +102,7 @@ describe Invitation do end end - context 'invitated users' do + context 'invitated user' do it 'by email' do @identifier = @users[3].email @type = 'email' @@ -204,6 +208,7 @@ describe Invitation do @invitee = Invitation.create_invitee(:service => 'email', :identifier => @email) end it 'creates no user' do + @valid_params[:existing_user] = @invitee lambda { Invitation.create_invitee(@valid_params) }.should_not change(User, :count)