invites are now on the queue
This commit is contained in:
parent
0684c40660
commit
0a831cfbf4
11 changed files with 63 additions and 64 deletions
|
|
@ -13,30 +13,21 @@ class InvitationsController < Devise::InvitationsController
|
|||
redirect_to :back
|
||||
return
|
||||
end
|
||||
begin
|
||||
params[:user][:aspect_id] = params[:user].delete(:aspects)
|
||||
message = params[:user].delete(:invite_messages)
|
||||
params[:user][:invite_message] = message unless message == ""
|
||||
|
||||
emails = params[:user][:email].split(/, */)
|
||||
invited_users = emails.map { |e| current_user.invite_user(params[:user].merge({:email => e}))}
|
||||
good_users, rejected_users = invited_users.partition {|u| u.persisted? }
|
||||
|
||||
flash[:notice] = I18n.t('invitations.create.sent') + good_users.map{|x| x.email}.join(', ')
|
||||
if rejected_users.any?
|
||||
flash[:error] = I18n.t('invitations.create.rejected') + rejected_users.map{|x| x.email}.join(', ')
|
||||
end
|
||||
rescue RuntimeError => e
|
||||
if e.message == "You have no invites"
|
||||
flash[:error] = I18n.t 'invitations.create.no_more'
|
||||
elsif e.message == "You already invited this person"
|
||||
flash[:error] = I18n.t 'invitations.create.already_sent'
|
||||
elsif e.message == "You are already connected to this person"
|
||||
flash[:error] = I18n.t 'invitations.create.already_contacts'
|
||||
good_emails, bad_emails = emails.partition{|e| e.try(:match, Devise.email_regexp)}
|
||||
|
||||
good_emails.each{|e| Resque.enqueue(Jobs::InviteUser, current_user.id, params[:user].merge({:email => e}))}
|
||||
|
||||
if bad_emails.any?
|
||||
flash[:error] = I18n.t('invitations.create.sent') + good_emails.join(', ') + " "+ I18n.t('invitations.create.rejected') + bad_emails.join(', ')
|
||||
else
|
||||
raise e
|
||||
end
|
||||
flash[:notice] = I18n.t('invitations.create.sent') + good_emails.join(', ')
|
||||
end
|
||||
|
||||
redirect_to :back
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ class Invitation
|
|||
invitee.reload
|
||||
end
|
||||
|
||||
invitee.serialized_private_key ||= User.generate_key
|
||||
invitee.send(:generate_invitation_token)
|
||||
invitee.invite!
|
||||
Rails.logger.info("event=invitation_sent to=#{opts[:email]} #{"inviter=#{opts[:from].diaspora_handle}" if opts[:from]}")
|
||||
|
|
|
|||
9
app/models/jobs/invite_user.rb
Normal file
9
app/models/jobs/invite_user.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
module Jobs
|
||||
class InviteUser
|
||||
@queue = :email
|
||||
def self.perform(sender_id, params)
|
||||
user = User.find(sender_id)
|
||||
user.invite_user(params)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -7,4 +7,3 @@ module Jobs
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -364,18 +364,14 @@ class User
|
|||
###Invitations############
|
||||
def invite_user(opts = {})
|
||||
aspect_id = opts.delete(:aspect_id)
|
||||
if aspect_id == nil
|
||||
raise "Must invite into aspect"
|
||||
end
|
||||
aspect_object = self.aspects.find_by_id(aspect_id)
|
||||
if !(aspect_object)
|
||||
raise "Must invite to your aspect"
|
||||
else
|
||||
if aspect_object
|
||||
Invitation.invite(:email => opts[:email],
|
||||
:from => self,
|
||||
:into => aspect_object,
|
||||
:message => opts[:invite_message])
|
||||
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -419,10 +415,10 @@ class User
|
|||
self.person = Person.new(opts[:person])
|
||||
self.person.diaspora_handle = "#{opts[:username]}@#{APP_CONFIG[:terse_pod_url]}"
|
||||
self.person.url = APP_CONFIG[:pod_url]
|
||||
new_key = User.generate_key
|
||||
self.serialized_private_key = new_key
|
||||
self.person.serialized_public_key = new_key.public_key
|
||||
|
||||
|
||||
self.serialized_private_key ||= User.generate_key
|
||||
self.person.serialized_public_key = OpenSSL::PKey::RSA.new(self.serialized_private_key).public_key
|
||||
self
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -43,5 +43,4 @@ begin
|
|||
rescue LoadError => ignore_if_database_cleaner_not_present
|
||||
puts "Error on cleaner"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -45,6 +45,18 @@ end
|
|||
require File.join(File.dirname(__FILE__), "..", "..", "spec", "helper_methods")
|
||||
include HelperMethods
|
||||
|
||||
class User
|
||||
def send_contact_request_to(desired_contact, aspect)
|
||||
request = Request.instantiate(:to => desired_contact,
|
||||
:from => self.person,
|
||||
:into => aspect)
|
||||
if request.save!
|
||||
dispatch_request request
|
||||
end
|
||||
request
|
||||
end
|
||||
end
|
||||
|
||||
Before do
|
||||
UserFixer.regenerate_user_fixtures
|
||||
UserFixer.load_user_fixtures
|
||||
|
|
|
|||
|
|
@ -14,6 +14,20 @@ describe InvitationsController do
|
|||
|
||||
before do
|
||||
request.env["devise.mapping"] = Devise.mappings[:user]
|
||||
module Resque
|
||||
def enqueue(mod, *args)
|
||||
mod.send(:perform, *args)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
after do
|
||||
module Resque
|
||||
def enqueue(mod, *args)
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
|
@ -22,42 +36,23 @@ describe InvitationsController do
|
|||
user.invites = 5
|
||||
|
||||
sign_in :user, user
|
||||
@invite = {:invite_messages=>"test", :aspects=> aspect.id.to_s, :email=>"abc@example.com"}
|
||||
@invite = {:invite_message=>"test", :aspect_id=> aspect.id.to_s, :email=>"abc@example.com"}
|
||||
@controller.stub!(:current_user).and_return(user)
|
||||
request.env["HTTP_REFERER"]= 'http://test.host/cats/foo'
|
||||
end
|
||||
|
||||
it 'invites the requested user' do
|
||||
user.should_receive(:invite_user).and_return(make_user)
|
||||
it 'should call the resque job Jobs::InviteUser' do
|
||||
Resque.should_receive(:enqueue)
|
||||
post :create, :user => @invite
|
||||
end
|
||||
|
||||
it 'creates an invitation' do
|
||||
lambda{
|
||||
post :create, :user => @invite
|
||||
}.should change(Invitation, :count).by(1)
|
||||
end
|
||||
|
||||
it 'creates an invited user with five invites' do
|
||||
lambda{
|
||||
post :create, :user => @invite
|
||||
}.should change(User, :count).by(1)
|
||||
User.find_by_email("abc@example.com").invites.should == 5
|
||||
end
|
||||
|
||||
it 'can handle a comma seperated list of emails' do
|
||||
lambda {
|
||||
Resque.should_receive(:enqueue).twice()
|
||||
post :create, :user => @invite.merge(:email => "foofoofoofoo@example.com, mbs@gmail.com")
|
||||
}.should change(User, :count).by(2)
|
||||
end
|
||||
|
||||
it 'displays a message that tells you how many invites were sent, and which REJECTED' do
|
||||
post :create, :user => @invite.merge(:email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
|
||||
flash[:notice].should_not be_empty
|
||||
flash[:notice].should =~ /mbs@gmail\.com/
|
||||
flash[:notice].should =~ /foo@bar\.com/
|
||||
flash[:notice].should =~ /cool@bar\.com/
|
||||
|
||||
flash[:error].should_not be_empty
|
||||
flash[:error].should =~ /foo\.com/
|
||||
flash[:error].should =~ /lala@foo/
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ describe PublicsController do
|
|||
let(:xml) { "<walruses></walruses>" }
|
||||
context 'success cases' do
|
||||
it 'should 200 on successful receipt of a request' do
|
||||
Resque.should_receive(:enqueue)
|
||||
post :receive, :id =>user.person.id, :xml => xml
|
||||
response.code.should == '200'
|
||||
end
|
||||
|
|
|
|||
|
|
@ -14,15 +14,11 @@ describe User do
|
|||
|
||||
context "creating invites" do
|
||||
it 'requires an apect' do
|
||||
proc{
|
||||
inviter.invite_user(:email => "maggie@example.com")
|
||||
}.should raise_error /Must invite into aspect/
|
||||
inviter.invite_user(:email => "maggie@example.com").should == false
|
||||
end
|
||||
|
||||
it 'requires your aspect' do
|
||||
proc{
|
||||
inviter.invite_user(:email => "maggie@example.com", :aspect_id => wrong_aspect.id)
|
||||
}.should raise_error /Must invite to your aspect/
|
||||
inviter.invite_user(:email => "maggie@example.com", :aspect_id => wrong_aspect.id).should == false
|
||||
end
|
||||
|
||||
it 'calls Invitation.invite' do
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ module Resque
|
|||
end
|
||||
end
|
||||
|
||||
module Diaspora::UserModules::Connecting
|
||||
class User
|
||||
def send_contact_request_to(desired_contact, aspect)
|
||||
request = Request.instantiate(:to => desired_contact,
|
||||
:from => self.person,
|
||||
|
|
|
|||
Loading…
Reference in a new issue