Merge pull request #2542 from techAPJ/patch-1
FEATURE: topic support in disposable invites
This commit is contained in:
commit
6d7531f690
|
@ -57,17 +57,23 @@ class InvitesController < ApplicationController
|
||||||
|
|
||||||
def redeem_disposable_invite
|
def redeem_disposable_invite
|
||||||
params.require(:email)
|
params.require(:email)
|
||||||
params.permit(:username, :name)
|
params.permit(:username, :name, :topic)
|
||||||
|
|
||||||
invite = Invite.find_by(invite_key: params[:token])
|
invite = Invite.find_by(invite_key: params[:token])
|
||||||
|
|
||||||
if invite.present?
|
if invite.present?
|
||||||
user = Invite.redeem_from_token(params[:token], params[:email], params[:username], params[:name])
|
user = Invite.redeem_from_token(params[:token], params[:email], params[:username], params[:name], params[:topic].to_i)
|
||||||
if user.present?
|
if user.present?
|
||||||
log_on_user(user)
|
log_on_user(user)
|
||||||
|
|
||||||
# Send a welcome message if required
|
# Send a welcome message if required
|
||||||
user.enqueue_welcome_message('welcome_invite') if user.send_welcome_message
|
user.enqueue_welcome_message('welcome_invite') if user.send_welcome_message
|
||||||
|
|
||||||
|
topic = invite.topics.first
|
||||||
|
if topic.present?
|
||||||
|
redirect_to "#{Discourse.base_uri}#{topic.relative_url}"
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -168,10 +168,11 @@ class Invite < ActiveRecord::Base
|
||||||
invite
|
invite
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.redeem_from_token(token, email, username=nil, name=nil)
|
def self.redeem_from_token(token, email, username=nil, name=nil, topic_id=nil)
|
||||||
invite = Invite.find_by(invite_key: token)
|
invite = Invite.find_by(invite_key: token)
|
||||||
if invite
|
if invite
|
||||||
invite.update_column(:email, email)
|
invite.update_column(:email, email)
|
||||||
|
invite.topic_invites.create!(invite_id: invite.id, topic_id: topic_id) if topic_id && Topic.find_by_id(topic_id)
|
||||||
user = InviteRedeemer.new(invite, username, name).redeem
|
user = InviteRedeemer.new(invite, username, name).redeem
|
||||||
end
|
end
|
||||||
user
|
user
|
||||||
|
|
|
@ -215,6 +215,50 @@ describe InvitesController do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '.redeem_disposable_invite' do
|
||||||
|
|
||||||
|
context 'with an invalid invite token' do
|
||||||
|
before do
|
||||||
|
get :redeem_disposable_invite, email: "name@example.com", token: "doesn't exist"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "redirects to the root" do
|
||||||
|
response.should redirect_to("/")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not change the session" do
|
||||||
|
session[:current_user_id].should be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with a valid invite token' do
|
||||||
|
let(:topic) { Fabricate(:topic) }
|
||||||
|
let(:invitee) { Fabricate(:user) }
|
||||||
|
let(:invite) { Invite.create!(invited_by: invitee) }
|
||||||
|
|
||||||
|
it 'redeems the invite' do
|
||||||
|
Invite.expects(:redeem_from_token).with(invite.invite_key, "name@example.com", nil, nil, topic.id)
|
||||||
|
get :redeem_disposable_invite, email: "name@example.com", token: invite.invite_key, topic: topic.id
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when redeem returns a user' do
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
Invite.expects(:redeem_from_token).with(invite.invite_key, user.email, nil, nil, topic.id).returns(user)
|
||||||
|
get :redeem_disposable_invite, email: user.email, token: invite.invite_key, topic: topic.id
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'logs in user' do
|
||||||
|
session[:current_user_id].should == user.id
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
context '.check_csv_chunk' do
|
context '.check_csv_chunk' do
|
||||||
it 'requires you to be logged in' do
|
it 'requires you to be logged in' do
|
||||||
lambda {
|
lambda {
|
||||||
|
|
Loading…
Reference in New Issue