From e90815a4295d35be39a5dc7fc553b9bb79cadf07 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Tue, 5 Apr 2022 14:57:00 +0300 Subject: [PATCH] FIX: Redirect user to topic they were invited to (#16298) This did not work properly everytime because the destination URL was saved in a cookie and that can be lost for various reasons. This commit redirects the user to invited topic if it exists. --- app/controllers/users_controller.rb | 21 +++++++++++++++++++-- spec/requests/users_controller_spec.rb | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5c721ac698b..768053e00a6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1065,10 +1065,27 @@ class UsersController < ApplicationController @user.enqueue_welcome_message('welcome_user') if @user.send_welcome_message log_on_user(@user) + # invites#perform_accept_invitation already sets destination_url, but + # sometimes it is lost (user changes browser, uses incognito, etc) + # + # The code below checks if the user was invited and redirects them to + # the topic they were originally invited to. + destination_url = cookies.delete(:destination_url) + if destination_url.blank? + topic = Invite + .joins(:invited_users) + .find_by(invited_users: { user_id: @user.id }) + &.topics + &.first + + if @user.guardian.can_see?(topic) + destination_url = path(topic.relative_url) + end + end + if Wizard.user_requires_completion?(@user) return redirect_to(wizard_path) - elsif destination_url = cookies[:destination_url] - cookies[:destination_url] = nil + elsif destination_url.present? return redirect_to(destination_url) elsif SiteSetting.enable_discourse_connect_provider && payload = cookies.delete(:sso_payload) return redirect_to(session_sso_provider_url + "?" + payload) diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index c978b8cc057..163aaf84aa8 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -147,6 +147,23 @@ describe UsersController do expect(response).to redirect_to(destination_url) end end + + context 'when cookies does not contain a destination URL but users was invited to topic' do + let(:invite) { Fabricate(:invite) } + let(:topic) { Fabricate(:topic) } + + before do + TopicInvite.create!(topic: topic, invite: invite) + Fabricate(:invited_user, invite: invite, user: email_token.user) + invite.reload + end + + it 'should redirect to the topic' do + put "/u/activate-account/#{email_token.token}" + + expect(response).to redirect_to(topic.relative_url) + end + end end describe '#password_reset' do