diff --git a/app/models/email_token.rb b/app/models/email_token.rb index dc511fbc726..0d02e0d16a1 100644 --- a/app/models/email_token.rb +++ b/app/models/email_token.rb @@ -57,6 +57,8 @@ class EmailToken < ActiveRecord::Base user.save! end end + # redeem invite, if available + Invite.redeem_from_email(user.email) user rescue ActiveRecord::RecordInvalid # If the user's email is already taken, just return nil (failure) diff --git a/app/models/invite.rb b/app/models/invite.rb index bbdf5edbdfc..f60a69f16ba 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -130,6 +130,14 @@ class Invite < ActiveRecord::Base i end + def self.redeem_from_email(email) + invite = Invite.find_by(email: Email.downcase(email)) + if invite + InviteRedeemer.new(invite).redeem + end + invite + end + def self.base_directory File.join(Rails.root, "public", "csv", RailsMultisite::ConnectionManagement.current_db) end diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb index 19cf4d3677f..b4ec857df23 100644 --- a/app/models/invite_redeemer.rb +++ b/app/models/invite_redeemer.rb @@ -19,6 +19,10 @@ InviteRedeemer = Struct.new(:invite) do # extracted from User cause it is very specific to invites def self.create_user_from_invite(invite) + + user_exists = User.find_by_email(invite.email) + return user if user_exists + username = UserNameSuggester.suggest(invite.email) DiscourseHub.username_operation do