A platform for community discussion. Free, open, simple.
FIX: Allow existing users to accept invites that add them to a group. (#32762)
Gary Pendergast672007549b3e
diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rbindex 148ed9e3ec2..8cb01bad9c9 100644--- a/app/controllers/invites_controller.rb+++ b/app/controllers/invites_controller.rb@@ -28,18 +28,21 @@ class InvitesController < ApplicationControllerinvite = Invite.find_by(invite_key: params[:id])- # automatically redirect to the topic if the user is logged in and can see it+ if !invite.present? || !invite.redeemable?+ show_irredeemable_invite(invite)+ return+ end++ # automatically redirect to the topic if the user is logged in and can see it,+ # but only if the invite wouldn't also add the user to a groupif current_user- if topic = invite.topics.first+ new_group_ids = invite.groups.pluck(:id) - current_user.group_users.pluck(:group_id)+ if new_group_ids.empty? && topic = invite.topics.firstreturn redirect_to(topic.url) if current_user.guardian.can_see?(topic)endend- if invite.present? && invite.redeemable?- show_invite(invite)- else- show_irredeemable_invite(invite)- end+ show_invite(invite)rescue RateLimiter::LimitExceeded => eflash.now[:error] = e.descriptionrender layout: "no_ember"diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rbindex bffbf4b1c15..dd18dbccede 100644--- a/spec/requests/invites_controller_spec.rb+++ b/spec/requests/invites_controller_spec.rb@@ -120,6 +120,44 @@ RSpec.describe InvitesController doexpect(response.status).to eq(200)end+ it "doesn't automatically redirect to the topic if the invite has expired" do+ invite.update!(topics: [Fabricate(:topic)])+ invite.update!(expires_at: 1.day.ago)++ get "/invites/#{invite.invite_key}"+ expect(response.status).to eq(200)++ expect(response.body).to have_tag(:body, with: { class: "no-ember" })+ expect(response.body).to include(I18n.t("invite.expired", base_url: Discourse.base_url))+ end++ it "shows the accept invite page when the user can access the topic, but the invite would also add them to a group" do+ invite.update!(topics: [Fabricate(:topic)])+ InvitedGroup.create!(invite: invite, group: group)++ get "/invites/#{invite.invite_key}"+ expect(response.status).to eq(200)+ expect(response.body).not_to have_tag(:body, with: { class: "no-ember" })+ expect(response.body).not_to include(+ I18n.t(+ "invite.not_found_template",+ site_name: SiteSetting.title,+ base_url: Discourse.base_url,+ ),+ )+ end++ it "automatically redirects to the topic if the user can access it, and they're already a member of the invite group" do+ invite.update!(topics: [Fabricate(:topic)])++ group.add(user)+ InvitedGroup.create!(invite: invite, group: group)++ get "/invites/#{invite.invite_key}"+ expect(response.status).to eq(302)+ expect(response.location).to eq(invite.topics.first.url)+ end+it "shows the accept invite page when user's email matches the invite email" doinvite.update_columns(email: user.email)