From fb93be11927efb74816bb2b84f89a29c563df2e2 Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Sun, 5 Jan 2014 23:36:42 -0300 Subject: [PATCH] Improved registered users invitation using the user interface --- app/controllers/public/invite_controller.rb | 30 ++++++++++++++++++++++++++++-- app/views/friends/index.rhtml | 4 ++-- app/views/invite/_personalize_invitation_mail.rhtml | 10 ++++++++++ app/views/invite/_select_address_book.rhtml | 40 ++++++++++++++++++++++++++++++++++++++++ app/views/invite/invite_friends.rhtml | 27 +++++++++++++++++++++++++++ app/views/invite/select_address_book.rhtml | 51 --------------------------------------------------- app/views/invite/select_friends.rhtml | 13 ++----------- app/views/profile/friends.rhtml | 2 +- app/views/profile/members.rhtml | 2 +- app/views/profile_members/_index_buttons.rhtml | 2 +- config/routes.rb | 2 +- plugins/stoa/lib/stoa_plugin.rb | 2 +- plugins/stoa/test/functional/invite_controller_test.rb | 6 +++--- test/functional/invite_controller_test.rb | 51 +++++++++++++++++++++++++++++++++++++++++++-------- test/integration/routing_test.rb | 2 +- 15 files changed, 161 insertions(+), 83 deletions(-) create mode 100644 app/views/invite/_personalize_invitation_mail.rhtml create mode 100644 app/views/invite/_select_address_book.rhtml create mode 100644 app/views/invite/invite_friends.rhtml delete mode 100644 app/views/invite/select_address_book.rhtml diff --git a/app/controllers/public/invite_controller.rb b/app/controllers/public/invite_controller.rb index 5c448b5..79033c9 100644 --- a/app/controllers/public/invite_controller.rb +++ b/app/controllers/public/invite_controller.rb @@ -4,8 +4,9 @@ class InviteController < PublicController before_filter :login_required before_filter :check_permissions_to_invite - def select_address_book + def invite_friends @import_from = params[:import_from] || "manual" + @mail_template = params[:mail_template] || environment.invitation_mail_template(profile) if request.post? contact_list = ContactList.create Delayed::Job.enqueue GetEmailContactsJob.new(@import_from, params[:login], params[:password], contact_list.id) if @import_from != 'manual' @@ -52,7 +53,32 @@ class InviteController < PublicController def cancel_fetching_emails contact_list = ContactList.find(params[:contact_list]) contact_list.destroy - redirect_to :action => 'select_address_book' + redirect_to :action => 'invite_friends' + end + + def invite_registered_friend + contacts_to_invite = params['q'].split(',') + mail_template = params[:mail_template] || environment.invitation_mail_template(profile) + if !contacts_to_invite.empty? + Delayed::Job.enqueue InvitationJob.new(current_user.person.id, contacts_to_invite, mail_template, profile.id, nil, locale) + session[:notice] = _('Your invitations are being sent.') + if profile.person? + redirect_to :controller => 'profile', :action => 'friends' + else + redirect_to :controller => 'profile', :action => 'members' + end + return + else + redirect_to :action => 'invite_friends' + session[:notice] = _('Please enter a valid profile.') + end + end + + def search_friend + fields = %w[name identifier email] + values = ["%#{params['q']}%"] * fields.count + render :text => environment.people.find(:all, :joins => ['INNER JOIN users ON profiles.user_id=users.id'], :conditions => [fields.map {|field| "LOWER(#{field}) LIKE ?"}.join(' OR '), *values]). + map {|person| {:id => person.id, :name => person.name} }.to_json end protected diff --git a/app/views/friends/index.rhtml b/app/views/friends/index.rhtml index 710cd30..49a28c9 100644 --- a/app/views/friends/index.rhtml +++ b/app/views/friends/index.rhtml @@ -15,7 +15,7 @@ <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %> <%= button(:search, _('Find people'), :controller => 'search', :action => 'assets', :asset => 'people') %> <% unless @plugins.dispatch(:remove_invite_friends_button).include?(true) %> - <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'select_address_book') %> + <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'invite_friends') %> <% end %> <% end %> <% end %> @@ -46,7 +46,7 @@ <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %> <%= button(:search, _('Find people'), :controller => 'search', :action => 'assets', :asset => 'people') %> <% unless @plugins.dispatch(:remove_invite_friends_button).include?(true) %> - <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'select_address_book') %> + <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'invite_friends') %> <% end %> <% end %> <% end %> diff --git a/app/views/invite/_personalize_invitation_mail.rhtml b/app/views/invite/_personalize_invitation_mail.rhtml new file mode 100644 index 0000000..088426a --- /dev/null +++ b/app/views/invite/_personalize_invitation_mail.rhtml @@ -0,0 +1,10 @@ +
+ +<%= link_to_function(_('Personalize invitation mail'), nil) do |page| + page['invitation-mail_template'].show +end %> + + diff --git a/app/views/invite/_select_address_book.rhtml b/app/views/invite/_select_address_book.rhtml new file mode 100644 index 0000000..7344ef8 --- /dev/null +++ b/app/views/invite/_select_address_book.rhtml @@ -0,0 +1,40 @@ +

<%= _('Step 1 of 2: Select address book') %>

+ +<% form_tag do %> + + <%= [ + radio_button_tag(:import_from, "manual", @import_from == "manual", :onclick => 'hide_invite_friend_login_password()') + content_tag('label', _('Manually (empty field)'), :for => "import_from_manual"), + radio_button_tag(:import_from, "gmail", @import_from == "gmail", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Gmail', :for => 'import_from_gmail'), + radio_button_tag(:import_from, "yahoo", @import_from == "yahoo", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Yahoo', :for => "import_from_yahoo"), + radio_button_tag(:import_from, "hotmail", @import_from == "hotmail", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Hotmail', :for => "import_from_hotmail") + ].join("\n
\n") %> + + +
> +
+ <%= ui_icon('ui-icon-alert') %> + <%= _('Please type your username in the format yourname@example.com') %> +
+ + <%= labelled_form_field(_("Username") + ":", text_field_tag(:login, @login)) %> + <%= labelled_form_field(_("Password") + ":", password_field_tag(:password)) %> +
+ + <% button_bar do %> + <%= submit_button(:forward, _("Next")) %> + <% end %> +

<%= _("We won't store your password or contact anyone without your permission.") %>

+<% end %> diff --git a/app/views/invite/invite_friends.rhtml b/app/views/invite/invite_friends.rhtml new file mode 100644 index 0000000..5dc4375 --- /dev/null +++ b/app/views/invite/invite_friends.rhtml @@ -0,0 +1,27 @@ +<% if profile.person? %> +

<%= _('Invite your friends') %>

+<% else %> +

<%= _('Invite your friends to join %s') % profile.name %>

+<% end %> + +<%= render :partial => 'invite/select_address_book' %> + +<% unless profile.person? %> +

<%= _('Invite others registered users') %>

+

<%= _('You can also search for your friends\' profiles and invite them to join this community.') %>

+ <% form_tag :action => 'invite_registered_friend' do %> + <% search_action = url_for(:action => 'search_friend') %> + <%= token_input_field_tag(:q, 'search-friends', search_action, + { :hint_text => _('Type in a search for your friend'), + :focus => false }) %> + + <%= render :partial => 'invite/personalize_invitation_mail', :locals => {:mail_template => @mail_template } %> + + <% button_bar do %> + <%= submit_button('save', _('Invite'))%> + <%= button('cancel', _('Cancel'), profile.url)%> + <% end %> + <% end %> +<% end %> + +
diff --git a/app/views/invite/select_address_book.rhtml b/app/views/invite/select_address_book.rhtml deleted file mode 100644 index 64b348c..0000000 --- a/app/views/invite/select_address_book.rhtml +++ /dev/null @@ -1,51 +0,0 @@ -<% if profile.person? %> -

<%= _('Invite your friends') %>

-<% else %> -

<%= _('Invite your friends to join %s') % profile.name %>

-<% end %> - -

<%= _('Step 1 of 2: Select address book') %>

- -<% form_tag do %> - - <%= [ - radio_button_tag(:import_from, "manual", @import_from == "manual", :onclick => 'hide_invite_friend_login_password()') + content_tag('label', _('Manually (empty field)'), :for => "import_from_manual"), - radio_button_tag(:import_from, "gmail", @import_from == "gmail", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Gmail', :for => 'import_from_gmail'), - radio_button_tag(:import_from, "yahoo", @import_from == "yahoo", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Yahoo', :for => "import_from_yahoo"), - radio_button_tag(:import_from, "hotmail", @import_from == "hotmail", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Hotmail', :for => "import_from_hotmail") - ].join("\n
\n") %> - - -
> -
- <%= ui_icon('ui-icon-alert') %> - <%= _('Please type your username in the format yourname@example.com') %> -
- - <%= labelled_form_field(_("Username") + ":", text_field_tag(:login, @login)) %> - <%= labelled_form_field(_("Password") + ":", password_field_tag(:password)) %> -
- - <% button_bar do %> - <%= submit_button(:forward, _("Next")) %> - <% end %> -

<%= _("We won't store your password or contact anyone without your permission.") %>

-<% end %> - -
- - - diff --git a/app/views/invite/select_friends.rhtml b/app/views/invite/select_friends.rhtml index 721a377..c264790 100644 --- a/app/views/invite/select_friends.rhtml +++ b/app/views/invite/select_friends.rhtml @@ -9,7 +9,7 @@

<%= _('Step 2 of 2: Selecting Friends') %>

-<%= button(:back, _('Back'), { :action => 'select_address_book' }, :id => 'invitation_back_button') %> +<%= button(:back, _('Back'), { :action => 'invite_friends' }, :id => 'invitation_back_button') %>

<%= _('Indicate which friends you want to invite.') %> @@ -30,16 +30,7 @@ <% end -%> -
- - <%= link_to_function(_('Personalize invitation mail'), nil) do |page| - page['invitation-mail_template'].show - end %> - -

+ <%= render :partial => 'invite/personalize_invitation_mail', :locals => {:mail_template => @mail_template } %> <% button_bar do %> <%= submit_button(:ok, _("Invite my friends!")) %> diff --git a/app/views/profile/friends.rhtml b/app/views/profile/friends.rhtml index 1da9c59..79cde4e 100644 --- a/app/views/profile/friends.rhtml +++ b/app/views/profile/friends.rhtml @@ -18,7 +18,7 @@ <%= button :back, _('Go back'), { :controller => 'profile' } %> <% if user == profile %> <%= button :edit, _('Manage my friends'), :controller => 'friends', :action => 'index', :profile => profile.identifier %> - <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'select_address_book') %> + <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'invite_friends') %> <% end %> <% end %> diff --git a/app/views/profile/members.rhtml b/app/views/profile/members.rhtml index 3639c52..1df0e7c 100644 --- a/app/views/profile/members.rhtml +++ b/app/views/profile/members.rhtml @@ -18,7 +18,7 @@ <%= button :back, _('Go back'), { :controller => 'profile' } %> <% if profile.community? and user %> <% if user.has_permission?(:invite_members, profile) %> - <%= button :search, _('Invite your friends to join %s') % profile.name, :controller => 'invite', :action => 'select_address_book' %> + <%= button :search, _('Invite your friends to join %s') % profile.name, :controller => 'invite', :action => 'invite_friends' %> <% end %> <% if user.has_permission?(:send_mail_to_members, profile) %> <%= button :send, _('Send e-mail to members'), :controller => 'profile', :action => 'send_mail' %> diff --git a/app/views/profile_members/_index_buttons.rhtml b/app/views/profile_members/_index_buttons.rhtml index 1e12ad3..c7c92d1 100644 --- a/app/views/profile_members/_index_buttons.rhtml +++ b/app/views/profile_members/_index_buttons.rhtml @@ -2,7 +2,7 @@ <%= button :back, _('Back'), :controller => 'profile_editor' %> <%= button :add, _('Add members'), :action => 'add_members' if profile.enterprise? %> <% if profile.community? and user.has_permission?(:invite_members, profile) %> - <%= button :search, _('Invite your friends to join %s') % profile.short_name, :controller => 'invite', :action => 'select_address_book' %> + <%= button :search, _('Invite your friends to join %s') % profile.short_name, :controller => 'invite', :action => 'invite_friends' %> <% end %> <% if profile.community? and user.has_permission?(:send_mail_to_members, profile) %> <%= button :send, _('Send e-mail to members'), :controller => 'profile', :action => 'send_mail' %> diff --git a/config/routes.rb b/config/routes.rb index fa98554..141e645 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,7 +64,7 @@ ActionController::Routing::Routes.draw do |map| map.catalog 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format}/ # invite - map.invite 'profile/:profile/invite/friends', :controller => 'invite', :action => 'select_address_book', :profile => /#{Noosfero.identifier_format}/ + map.invite 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format}/ map.invite 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format}/ # feeds per tag diff --git a/plugins/stoa/lib/stoa_plugin.rb b/plugins/stoa/lib/stoa_plugin.rb index 96d1457..48201f6 100644 --- a/plugins/stoa/lib/stoa_plugin.rb +++ b/plugins/stoa/lib/stoa_plugin.rb @@ -113,7 +113,7 @@ class StoaPlugin < Noosfero::Plugin { :title => _('Invite friends'), :icon => 'invite-friends', :url => {:controller => 'invite', - :action => 'select_address_book'} } if context.send(:user) && context.send(:user).usp_id.present? + :action => 'invite_friends'} } if context.send(:user) && context.send(:user).usp_id.present? end def remove_invite_friends_button diff --git a/plugins/stoa/test/functional/invite_controller_test.rb b/plugins/stoa/test/functional/invite_controller_test.rb index dcf8d3e..1c46a0f 100644 --- a/plugins/stoa/test/functional/invite_controller_test.rb +++ b/plugins/stoa/test/functional/invite_controller_test.rb @@ -20,7 +20,7 @@ class InviteControllerTest < ActionController::TestCase person_without_usp_id = User.create!(:login => 'user-without', :email => 'user-without@example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:invitation_code => 12345678}).person login_as(person_without_usp_id.identifier) - get :select_address_book, :profile => person_without_usp_id.identifier + get :invite_friends, :profile => person_without_usp_id.identifier assert_response 403 get :select_friends, :profile => person_without_usp_id.identifier assert_response 403 @@ -30,7 +30,7 @@ class InviteControllerTest < ActionController::TestCase person_with_usp_id = User.create!(:login => 'user-with', :email => 'user-with@example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:usp_id => 12345678}).person login_as(person_with_usp_id.identifier) - get :select_address_book, :profile => person_with_usp_id.identifier + get :invite_friends, :profile => person_with_usp_id.identifier assert_response 200 get :select_friends, :profile => person_with_usp_id.identifier, :contact_list => ContactList.create.id assert_response 200 @@ -42,7 +42,7 @@ class InviteControllerTest < ActionController::TestCase organization.add_admin(person_with_usp_id) login_as(person_with_usp_id.identifier) - get :select_address_book, :profile => organization.identifier + get :invite_friends, :profile => organization.identifier assert_response 200 get :select_friends, :profile => organization.identifier, :contact_list => ContactList.create.id assert_response 200 diff --git a/test/functional/invite_controller_test.rb b/test/functional/invite_controller_test.rb index 41104bd..dba423c 100644 --- a/test/functional/invite_controller_test.rb +++ b/test/functional/invite_controller_test.rb @@ -98,7 +98,7 @@ class InviteControllerTest < ActionController::TestCase end should 'display invitation page' do - get :select_address_book, :profile => profile.identifier + get :invite_friends, :profile => profile.identifier assert_response :success assert_tag :tag => 'h1', :content => 'Invite your friends' end @@ -117,8 +117,8 @@ class InviteControllerTest < ActionController::TestCase assert_equal InviteFriend.mail_template, assigns(:mail_template) end - should 'deny select_address_book f user has no rights to invite members' do - get :select_address_book, :profile => community.identifier + should 'deny invite_friends if user has no rights to invite members' do + get :invite_friends, :profile => community.identifier assert_response 403 # forbidden end @@ -127,13 +127,13 @@ class InviteControllerTest < ActionController::TestCase assert_response 403 # forbidden end - should 'deny select_address_book access when trying to invite friends to another user' do - get :select_address_book, :profile => friend.identifier + should 'deny invite_friends access when trying to invite friends to another user' do + get :invite_friends, :profile => friend.identifier assert_response 403 # forbidden end should 'deny select_friends access when trying to invite friends to another user' do - get :select_address_book, :profile => friend.identifier + get :select_friends, :profile => friend.identifier assert_response 403 # forbidden end @@ -154,7 +154,7 @@ class InviteControllerTest < ActionController::TestCase community.add_admin(profile) contact_list = ContactList.create assert_difference Delayed::Job, :count, 1 do - post :select_address_book, :profile => community.identifier, :contact_list => contact_list.id, :import_from => 'gmail' + post :invite_friends, :profile => community.identifier, :contact_list => contact_list.id, :import_from => 'gmail' end end @@ -222,7 +222,7 @@ class InviteControllerTest < ActionController::TestCase assert_difference ContactList, :count, -1 do get :cancel_fetching_emails, :profile => profile.identifier, :contact_list => contact_list.id end - assert_redirected_to :action => 'select_address_book' + assert_redirected_to :action => 'invite_friends' end should 'set locale in the background job' do @@ -233,6 +233,41 @@ class InviteControllerTest < ActionController::TestCase assert_equal 'pt', Delayed::Job.first.payload_object.locale end + should 'search friends profiles by name, email or identifier' do + friend1 = create_user('willy').person + friend2 = create_user('william').person + friend1.name = 'cris' + friend2.email = 'me@example.com' + friend1.save + friend2.save + + get :search_friend, :profile => profile.identifier, :q => 'me@' + + assert_equal 'text/html', @response.content_type + assert_equal [{"name" => friend2.name, "id" => friend2.id}].to_json, @response.body + + get :search_friend, :profile => profile.identifier, :q => 'cri' + + assert_equal [{"name" => friend1.name, "id" => friend1.id}].to_json, @response.body + + get :search_friend, :profile => profile.identifier, :q => 'will' + + assert_equal [{"name" => friend1.name, "id" => friend1.id}, {"name" => friend2.name, "id" => friend2.id}].to_json, @response.body + end + + should 'invite registered users through profile id' do + friend1 = create_user('testuser1').person + friend2 = create_user('testuser2').person + assert_difference Delayed::Job, :count, 1 do + post :invite_registered_friend, :profile => profile.identifier, :q => "#{friend1.id},#{friend2.id}", :mail_template => "click: " + assert_redirected_to :controller => 'profile', :action => 'friends' + end + + assert_difference InviteFriend, :count, 2 do + process_delayed_job_queue + end + end + private def json_response diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index f3b5ff0..9cee82f 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -213,7 +213,7 @@ class RoutingTest < ActionController::IntegrationTest end def test_invite_routing - assert_routing('/profile/colivre/invite/friends', :controller => 'invite', :action => 'select_address_book', :profile => 'colivre') + assert_routing('/profile/colivre/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => 'colivre') end def test_chat_routing -- libgit2 0.21.2