From 6b97b8044ba4bd1ed940b7935b4494892c5694ed Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Thu, 9 Feb 2012 00:02:01 -0800 Subject: [PATCH] small updates, and it looks like we are mostly working --- app/controllers/admins_controller.rb | 10 +- app/controllers/invitations_controller.rb | 1 + app/controllers/services_controller.rb | 34 ----- app/helpers/notifier_helper.rb | 7 +- app/models/invitation.rb | 48 +------ app/views/notifier/invite.erb | 140 ------------------- app/views/notifier/invite.html.erb | 32 ++--- app/views/notifier/invite.text.erb | 2 +- features/accepts_invitation.feature | 2 +- features/step_definitions/user_steps.rb | 9 +- features/support/paths.rb | 2 +- lib/email_inviter.rb | 5 +- lib/rake_helpers.rb | 2 +- spec/controllers/services_controller_spec.rb | 47 ------- tmp/.gitkeep | 0 15 files changed, 44 insertions(+), 297 deletions(-) delete mode 100644 app/views/notifier/invite.erb delete mode 100644 tmp/.gitkeep diff --git a/app/controllers/admins_controller.rb b/app/controllers/admins_controller.rb index 7b1fafa4d..12d3cdbf9 100644 --- a/app/controllers/admins_controller.rb +++ b/app/controllers/admins_controller.rb @@ -11,12 +11,14 @@ class AdminsController < ApplicationController end def admin_inviter - user = User.find_by_email params[:idenitifer] + email = params[:idenitifer] + user = User.find_by_email(email) + unless user - Invitation.create(:service => 'email', :identifier => params[:identifier], :admin => true) - flash[:notice] = "invitation sent to #{params[:identifier]}" + EmailInviter.new(email).send! + flash[:notice] = "invitation sent to #{email}" else - flash[:notice]= "error sending invite to #{params[:identifier]}" + flash[:notice]= "error sending invite to #{email}" end redirect_to user_search_path, :notice => flash[:notice] end diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index 9ca07d839..cefd53162 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -27,6 +27,7 @@ class InvitationsController < ApplicationController def create inviter = EmailInviter.new(params[:email_inviter][:emails], params[:email_inviter]) + inviter.send! redirect_to :back, :notice => "Great! Invites were sent off to #{inviter.emails.join(', ')}" end diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index c67f609bb..5f44071bf 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -69,38 +69,4 @@ class ServicesController < ApplicationController @service = current_user.services.where(:type => "Services::#{params[:provider].titleize}").first @friends = @service ? @service.finder(:remote => params[:remote]).paginate( :page => params[:page], :per_page => 15) : [] end - - def inviter - @uid = params[:uid] - - if i_id = params[:invitation_id] - invite = Invitation.find(i_id) - invited_user = invite.recipient - else - invite = Invitation.create(:service => params[:provider], :identifier => @uid, :sender => current_user, :aspect => current_user.aspects.find(params[:aspect_id])) - invited_user = invite.attach_recipient! - end - - #to make sure a friend you just invited from facebook shows up as invited - service = current_user.services.where(:type => "Services::Facebook").first - su = ServiceUser.where(:service_id => service.id, :uid => @uid).first - su.attach_local_models - su.save - - respond_to do |format| - format.html{ invite_redirect_url(invite, invited_user, su)} - format.json{ render :json => invite_redirect_json(invite, invited_user, su) } - end - end - - def facebook_message_url(user, facebook_uid) - subject = t('services.inviter.join_me_on_diaspora') - message = < :admin? validates_uniqueness_of :sender_id, :scope => [:identifier, :service], :unless => :admin? - after_create :queue_send! #TODO make this after_commit :queue_saved!, :on => :create - # @note options hash is passed through to [Invitation.new] # @see [Invitation.new] @@ -33,7 +31,7 @@ class Invitation < ActiveRecord::Base # @option opts [Aspect] :aspect # @option opts [String] :service # @return [Array] An array of [Invitation] models - # the valid ones are saved, and the invalid ones are not. + # the valid optsnes are saved, and the invalid ones are not. def self.batch_invite(emails, opts) users_on_pod = User.where(:email => emails, :invitation_token => nil) @@ -66,36 +64,16 @@ class Invitation < ActiveRecord::Base !email_like_identifer end - # Attach a recipient [User] to the [Invitation] unless - # there is one already present. - # - # @return [User] The recipient. - def attach_recipient! - unless self.recipient.present? - self.recipient = User.find_or_create_by_invitation(self) - self.save - end - self.recipient - end - # Find or create user, and send that resultant User an # invitation. # # @return [Invitation] self def send! - self.attach_recipient! - - # Sets an instance variable in User (set by devise invitable) - # This determines whether an email should be sent to the recipient. - recipient.skip_invitation = self.skip_email? - - recipient.invite! - - # Logging the invitation action - log_hash = {:event => :invitation_sent, :to => self[:identifier], :service => self[:service]} - log_hash.merge({:inviter => self.sender.diaspora_handle, :invitier_uid => self.sender.id, :inviter_created_at_unix => self.sender.created_at.to_i}) if self.sender - Rails.logger.info(log_hash) - + if email_like_identifer + EmailInviter.new(self.identifier).send! + else + puts "broken facebook invitation_token" + end self end @@ -135,19 +113,7 @@ class Invitation < ActiveRecord::Base when 'email' self.identifier when 'facebook' - if username = ServiceUser.username_of_service_user_by_uid(self.identifier) - unless username.include?('profile.php?') - "#{username}@facebook.com" - else - nil - end - end - end - end - - def queue_send! - unless self.recipient.present? - Resque.enqueue(Jobs::Mail::InviteUserByEmail, self.id) + false end end diff --git a/app/views/notifier/invite.erb b/app/views/notifier/invite.erb deleted file mode 100644 index 7093e499f..000000000 --- a/app/views/notifier/invite.erb +++ /dev/null @@ -1,140 +0,0 @@ -<%- self.extend NotifierHelper -%> - - <%=invite_email_title %> - -

<%= t('.displaying_correctly', :link => link_to(t('.view_in'), invite_email_url(:invitation_token => @resource.invitation_token), :style => "color: #3F8FBA; text-decoration: none;")).html_safe %>

- - - - -
- - - - - - - - - - - - - - - - - - - <% if @inviter.present? %> - <%= @inviter.inspect %> - <% end %> - - - - - - - - - - - - - - - - - - - - -
- - Diaspora - -
- <%= t('.finally') %>
-
- -
- <%= t('.arrived', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %> -
-
- <%= link_to(t('.sign_up_now').html_safe, invite_code_url(@invitation_code), :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank").html_safe %> -
- 1. <%= t('.get_connected') %>
- - - - - -
- - - <%= t('.get_connected_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %> -
- -
-
- 2. <%= t('.be_yourself') %>
- - - - - - -
- - <%= t('.be_yourself_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %> - -
- -
- -
-
- 3. <%= t('.have_fun') %>
- - - - - -
- - - - <%= t('.have_fun_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*"), :link => link_to(t('.cubbies'), "https://cubbi.es", :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank")).html_safe %> - -
-
- - - - - - -
- <%= link_to(t('.sign_up_now').html_safe, invite_code_url(@invitation_code), :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank").html_safe %> -
-
- - - - - - -
- <%= t('.made_by_people', :strong_diaspora => content_tag(:strong, "DIASPORA*"), :jointeam => link_to(t('.join_team'), "https://github.com/diaspora/diaspora/wiki/Become-a-Contributor", :style =>"color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 18px;", :target => "_blank"), :helpfund => link_to(t('.help_fund'), "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QG4L6VYD8YGPU", :style =>"color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 18px;", :target => "_blank")).html_safe %> - -
-
- <%= t('.love') %>
- <%= t('.team_diaspora') %>
-
- <% if AppConfig[:pod_uri].host.match(/joindiaspora.com/) %> - <%= t('.unsubscribe', :link => link_to(t('.here'), "http://joindiaspora.us1.list-manage.com/unsubscribe?u=d759919b94f9cdcf39d204f3f&id=7b5ceb2f8b", :style => "color: #3F8FBA; text-decoration: none;")).html_safe %> - <% end %> - <%= t('.email_us', :email => link_to(t('.email_address'), "mailto:questions@joindiaspora.com", :style => "color: #3F8FBA; text-decoration: none;")).html_safe %> -
-
diff --git a/app/views/notifier/invite.html.erb b/app/views/notifier/invite.html.erb index 7093e499f..be58a6716 100644 --- a/app/views/notifier/invite.html.erb +++ b/app/views/notifier/invite.html.erb @@ -2,7 +2,7 @@ <%=invite_email_title %> -

<%= t('.displaying_correctly', :link => link_to(t('.view_in'), invite_email_url(:invitation_token => @resource.invitation_token), :style => "color: #3F8FBA; text-decoration: none;")).html_safe %>

+

<%= t('devise.mailer.invitation_instructions.displaying_correctly', :link => link_to(t('devise.mailer.invitation_instructions.view_in'), invite_email_url(:invitation_code => @invitation_code), :style => "color: #3F8FBA; text-decoration: none;")).html_safe %>

@@ -19,7 +19,7 @@ @@ -29,10 +29,10 @@ @@ -43,7 +43,7 @@ + 1. <%= t('devise.mailer.invitation_instructions.get_connected') %>
- <%= t('.finally') %>
+ <%= t('devise.mailer.invitation_instructions.finally') %>
- <%= t('.arrived', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %> + <%= t('devise.mailer.invitation_instructions.arrived', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %>

- <%= link_to(t('.sign_up_now').html_safe, invite_code_url(@invitation_code), :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank").html_safe %> + <%= link_to(t('devise.mailer.invitation_instructions.sign_up_now').html_safe, invite_code_url(@invitation_code), :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank").html_safe %>
- 1. <%= t('.get_connected') %>
@@ -51,7 +51,7 @@ @@ -60,13 +60,13 @@ + 2. <%= t('devise.mailer.invitation_instructions.be_yourself') %>
- <%= t('.get_connected_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %> + <%= t('devise.mailer.invitation_instructions.get_connected_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %>
- 2. <%= t('.be_yourself') %>
+ 3. <%= t('devise.mailer.invitation_instructions.have_fun') %>
- <%= t('.be_yourself_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %> + <%= t('devise.mailer.invitation_instructions.be_yourself_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*")).html_safe %>
@@ -81,7 +81,7 @@
- 3. <%= t('.have_fun') %>
@@ -90,7 +90,7 @@ @@ -102,7 +102,7 @@ @@ -114,7 +114,7 @@ @@ -123,14 +123,14 @@
- <%= t('.have_fun_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*"), :link => link_to(t('.cubbies'), "https://cubbi.es", :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank")).html_safe %> + <%= t('devise.mailer.invitation_instructions.have_fun_paragraph', :strong_diaspora => content_tag(:strong, "DIASPORA*"), :link => link_to(t('devise.mailer.invitation_instructions.cubbies'), "https://cubbi.es", :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank")).html_safe %>
- <%= link_to(t('.sign_up_now').html_safe, invite_code_url(@invitation_code), :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank").html_safe %> + <%= link_to(t('devise.mailer.invitation_instructions.sign_up_now').html_safe, invite_code_url(@invitation_code), :style => "color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 20px;", :target => "_blank").html_safe %>
- <%= t('.made_by_people', :strong_diaspora => content_tag(:strong, "DIASPORA*"), :jointeam => link_to(t('.join_team'), "https://github.com/diaspora/diaspora/wiki/Become-a-Contributor", :style =>"color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 18px;", :target => "_blank"), :helpfund => link_to(t('.help_fund'), "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QG4L6VYD8YGPU", :style =>"color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 18px;", :target => "_blank")).html_safe %> + <%= t('devise.mailer.invitation_instructions.made_by_people', :strong_diaspora => content_tag(:strong, "DIASPORA*"), :jointeam => link_to(t('devise.mailer.invitation_instructions.join_team'), "https://github.com/diaspora/diaspora/wiki/Become-a-Contributor", :style =>"color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 18px;", :target => "_blank"), :helpfund => link_to(t('devise.mailer.invitation_instructions.help_fund'), "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QG4L6VYD8YGPU", :style =>"color: #3F8FBA; text-decoration: underline; font-weight: bold; font-size: 18px;", :target => "_blank")).html_safe %>
- <%= t('.love') %>
- <%= t('.team_diaspora') %>
+ <%= t('devise.mailer.invitation_instructions.love') %>
+ <%= t('devise.mailer.invitation_instructions.team_diaspora') %>
<% if AppConfig[:pod_uri].host.match(/joindiaspora.com/) %> - <%= t('.unsubscribe', :link => link_to(t('.here'), "http://joindiaspora.us1.list-manage.com/unsubscribe?u=d759919b94f9cdcf39d204f3f&id=7b5ceb2f8b", :style => "color: #3F8FBA; text-decoration: none;")).html_safe %> + <%= t('devise.mailer.invitation_instructions.unsubscribe', :link => link_to(t('devise.mailer.invitation_instructions.here'), "http://joindiaspora.us1.list-manage.com/unsubscribe?u=d759919b94f9cdcf39d204f3f&id=7b5ceb2f8b", :style => "color: #3F8FBA; text-decoration: none;")).html_safe %> <% end %> - <%= t('.email_us', :email => link_to(t('.email_address'), "mailto:questions@joindiaspora.com", :style => "color: #3F8FBA; text-decoration: none;")).html_safe %> + <%= t('devise.mailer.invitation_instructions.email_us', :email => link_to(t('devise.mailer.invitation_instructions.email_address'), "mailto:questions@joindiaspora.com", :style => "color: #3F8FBA; text-decoration: none;")).html_safe %>
diff --git a/app/views/notifier/invite.text.erb b/app/views/notifier/invite.text.erb index 90cb006df..947541336 100644 --- a/app/views/notifier/invite.text.erb +++ b/app/views/notifier/invite.text.erb @@ -4,7 +4,7 @@ You have been invited to join Diaspora*! Click this link to get started -<%= invitation_code_url(@invitation_code)%> +<%= invite_code_url(@invitation_code)%> Love, diff --git a/features/accepts_invitation.feature b/features/accepts_invitation.feature index 98635b492..70d9dccad 100644 --- a/features/accepts_invitation.feature +++ b/features/accepts_invitation.feature @@ -40,6 +40,6 @@ Feature: invitation acceptance Given a user with email "bob@bob.bob" When I sign in as "bob@bob.bob" And I follow "By email" - And I fill in "user_email" with "alex@example.com" + And I fill in "email_inviter_emails" with "alex@example.com" And I press "Send an invitation" Then I should have 1 Devise email delivery diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb index 350a3730a..e60922edd 100644 --- a/features/step_definitions/user_steps.rb +++ b/features/step_definitions/user_steps.rb @@ -26,15 +26,14 @@ Given /^a nsfw user with email "([^\"]*)"$/ do |email| end Given /^I have been invited by an admin$/ do - i = Invitation.create!(:admin => true, :service => 'email', :identifier => "new_invitee@example.com") - @me = i.attach_recipient! + i = EmailInviter.new("new_invitee@example.com") + i.send! end Given /^I have been invited by a user$/ do @inviter = Factory(:user) - aspect = @inviter.aspects.create(:name => "Rocket Scientists") - i = Invitation.create!(:aspect => aspect, :sender => @inviter, :service => 'email', :identifier => "new_invitee@example.com", :message =>"Hey, tell me about your rockets!") - @me = i.attach_recipient! + i = EmailInviter.new("new_invitee@example.com", :inviter => @inviter) + i.send! end When /^I click on my name$/ do diff --git a/features/support/paths.rb b/features/support/paths.rb index b6eb520df..318d39f7f 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -20,7 +20,7 @@ module NavigationHelpers when /^my profile page$/ person_path(@me.person) when /^my acceptance form page$/ - accept_user_invitation_path(:invitation_token => @me.invitation_token) + invite_code_path(InvitationCode.first) when /^the requestors profile$/ person_path(Request.where(:recipient_id => @me.person.id).first.sender) when /^"([^\"]*)"'s page$/ diff --git a/lib/email_inviter.rb b/lib/email_inviter.rb index d5ec92b81..0585b866b 100644 --- a/lib/email_inviter.rb +++ b/lib/email_inviter.rb @@ -15,10 +15,11 @@ class EmailInviter end def invitation_code - @invitation_code ||= inviter.nil? ? self.admin_code : inviter.invitation_code + @invitation_code ||= inviter.nil? ? EmailInviter.admin_code : inviter.invitation_code end def self.admin_code + puts "FIX ME" "foo" end @@ -29,6 +30,6 @@ class EmailInviter private def mail(email) - Notifier.invite(email, message, inviter, invitation_code, locale) + Notifier.invite(email, message, inviter, invitation_code, locale).deliver! end end \ No newline at end of file diff --git a/lib/rake_helpers.rb b/lib/rake_helpers.rb index d0f2a5a44..d71fa3e83 100644 --- a/lib/rake_helpers.rb +++ b/lib/rake_helpers.rb @@ -29,7 +29,7 @@ module RakeHelpers unless possible_user puts "#{n}: sending email to: #{backer_name} #{backer_email}" unless Rails.env == 'test' unless test - i = Invitation.new(:service => 'email', :identifier => backer_email, :admin => true) + i = EmailInviter.new(backer_email) i.send! end else diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb index fd11b2150..9017c3959 100644 --- a/spec/controllers/services_controller_spec.rb +++ b/spec/controllers/services_controller_spec.rb @@ -143,51 +143,4 @@ describe ServicesController do Nokogiri(response.body).css('.translation_missing').should be_empty end end - - describe '#inviter' do - before do - @uid = "abc" - fb = Factory(:service, :type => "Services::Facebook", :user => @user) - fb = Services::Facebook.find(fb.id) - @su = Factory(:service_user, :service => fb, :uid => @uid) - @invite_params = {:provider => 'facebook', :uid => @uid, :aspect_id => @user.aspects.first.id} - end - - it 'redirects to a prefilled facebook message url' do - put :inviter, @invite_params - response.location.should match(/https:\/\/www\.facebook\.com\/messages\/.*?msg_prefill=.*/) - end - - it 'creates an invitation' do - lambda { - put :inviter, @invite_params - }.should change(Invitation, :count).by(1) - end - - it 'sets the invitation_id on the service_user' do - post :inviter, @invite_params - @su.reload.invitation.should_not be_nil - end - - it 'does not create a duplicate invitation' do - invited_user = Factory.build(:user, :username =>nil) - invited_user.save(:validate => false) - inv = Invitation.create!(:sender => @user, :recipient => invited_user, :aspect => @user.aspects.first, :identifier => eve.email) - @invite_params[:invitation_id] = inv.id - - lambda { - put :inviter, @invite_params - }.should_not change(Invitation, :count) - end - - it 'disregards the amount of invites if open_invitations are enabled' do - open_bit = AppConfig[:open_invitations] - AppConfig[:open_invitations] = true - - lambda { - put :inviter, @invite_params - }.should change(Invitation, :count).by(1) - AppConfig[:open_invitations] = open_bit - end - end end diff --git a/tmp/.gitkeep b/tmp/.gitkeep deleted file mode 100644 index e69de29bb..000000000