translation of invitation message, better checking of existing users and unique index on user(invitation_service and invitation_identifier)

This commit is contained in:
zhitomirskiyi 2011-01-26 16:01:57 -08:00
parent 6242ac64ed
commit 1a911a8b99
7 changed files with 48 additions and 35 deletions

View file

@ -52,15 +52,13 @@ class ServicesController < ApplicationController
def inviter def inviter
@uid = params[:uid] @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]) invited_user = current_user.invite_user(params[:aspect_id], params[:provider], params[:uid])
@message = <<MSG @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. #{t('.click_link_to_accept_invitation')}:
\n
Click here to accept your invitation: \n
#{accept_invitation_url(invited_user, :invitation_token => invited_user.invitation_token)} #{accept_invitation_url(invited_user, :invitation_token => invited_user.invitation_token)}
MSG MSG
redirect_to "https://www.facebook.com/?compose=1&id=#{@uid}&subject=#{@subject}&message=#{@message}&sk=messages" redirect_to "https://www.facebook.com/?compose=1&id=#{@uid}&subject=#{@subject}&message=#{@message}&sk=messages"
end end

View file

@ -12,7 +12,8 @@ class Invitation < ActiveRecord::Base
def self.invite(opts = {}) def self.invite(opts = {})
return false if opts[:identifier] == opts[:from].email 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 existing_user
if opts[:from].contact_for(opts[:from].person) if opts[:from].contact_for(opts[:from].person)
@ -24,10 +25,12 @@ class Invitation < ActiveRecord::Base
raise "You already invited this person" raise "You already invited this person"
end end
end end
opts[:existing_user] = existing_user
create_invitee(opts) create_invitee(opts)
end 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, existing_user = User.where(:invitation_service => service,
:invitation_identifier => identifier).first :invitation_identifier => identifier).first
if service == 'email' if service == 'email'
@ -36,9 +39,10 @@ class Invitation < ActiveRecord::Base
existing_user ||= User.joins(:services).where(:services => {:provider => service, :uid => identifier}).first existing_user ||= User.joins(:services).where(:services => {:provider => service, :uid => identifier}).first
end end
if existing_user
existing_user existing_user
else end
def self.new_user_by_service_and_identifier(service, identifier)
result = User.new() result = User.new()
result.invitation_service = service result.invitation_service = service
result.invitation_identifier = identifier result.invitation_identifier = identifier
@ -46,10 +50,9 @@ class Invitation < ActiveRecord::Base
result.valid? result.valid?
result result
end end
end
def self.create_invitee(opts = {}) 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) return invitee if opts[:service] == 'email' && !opts[:identifier].match(Devise.email_regexp)
invitee.invites = opts[:invites] || 0 invitee.invites = opts[:invites] || 0
if invitee.new_record? if invitee.new_record?

View file

@ -13,6 +13,9 @@
%p %p
= f.label :username , t('username') = f.label :username , t('username')
= f.text_field :username, :title => t('registrations.new.enter_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 %p
= f.label :password , t('password') = f.label :password , t('password')
= f.password_field :password, :title => t('registrations.new.enter_password') = f.password_field :password, :title => t('registrations.new.enter_password')

View file

@ -30,7 +30,7 @@
:rel => 'facebox' :rel => 'facebox'
- else - else
= form_tag service_inviter_path do = 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 :uid, uid
= hidden_field_tag :provider, 'facebook' = hidden_field_tag :provider, 'facebook'
= submit_tag "invite" = submit_tag "invite"

View file

@ -424,6 +424,9 @@ en:
success: "Successfully deleted authentication." success: "Successfully deleted authentication."
failure: failure:
error: "there was an error connecting that service" 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: notifier:
hello: "Hello %{name}!" hello: "Hello %{name}!"
love: "love," love: "love,"

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # 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| create_table "aspect_memberships", :force => true do |t|
t.integer "aspect_id" t.integer "aspect_id"
@ -453,6 +453,7 @@ ActiveRecord::Schema.define(:version => 20110126225202) do
end end
add_index "users", ["email"], :name => "index_users_on_email" 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", ["invitation_token"], :name => "index_users_on_invitation_token"
add_index "users", ["mongo_id"], :name => "index_users_on_mongo_id" add_index "users", ["mongo_id"], :name => "index_users_on_mongo_id"
add_index "users", ["username"], :name => "index_users_on_username", :unique => true add_index "users", ["username"], :name => "index_users_on_username", :unique => true

View file

@ -45,16 +45,8 @@ describe Invitation do
@invitation.message.should == "!" @invitation.message.should == "!"
end end
describe '.new_or_existing_user_by_email' do describe '.new_user_by_service_and_identifier' do
let(:inv){Invitation.new_or_existing_user_by_service_and_identifier(@type, @identifier)} let(:inv){Invitation.new_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
it 'returns User.new for a non-existent user for email' do it 'returns User.new for a non-existent user for email' do
@type = "email" @type = "email"
@ -77,9 +69,21 @@ describe Invitation do
inv.reload inv.reload
}.should raise_error ActiveRecord::RecordNotFound }.should raise_error ActiveRecord::RecordNotFound
end end
end
context 'returns an existing user' do describe '.find_existing_user' do
context 'active users' 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 'send a request to an existing' do
context 'active user' do
it 'by email' do it 'by email' do
@identifier = @users[3].email @identifier = @users[3].email
@type = 'email' @type = 'email'
@ -98,7 +102,7 @@ describe Invitation do
end end
end end
context 'invitated users' do context 'invitated user' do
it 'by email' do it 'by email' do
@identifier = @users[3].email @identifier = @users[3].email
@type = 'email' @type = 'email'
@ -204,6 +208,7 @@ describe Invitation do
@invitee = Invitation.create_invitee(:service => 'email', :identifier => @email) @invitee = Invitation.create_invitee(:service => 'email', :identifier => @email)
end end
it 'creates no user' do it 'creates no user' do
@valid_params[:existing_user] = @invitee
lambda { lambda {
Invitation.create_invitee(@valid_params) Invitation.create_invitee(@valid_params)
}.should_not change(User, :count) }.should_not change(User, :count)