diff --git a/app/assets/javascripts/discourse/templates/user/invited.js.handlebars b/app/assets/javascripts/discourse/templates/user/invited.js.handlebars
index 7262dab5787..cf3da9bdc24 100644
--- a/app/assets/javascripts/discourse/templates/user/invited.js.handlebars
+++ b/app/assets/javascripts/discourse/templates/user/invited.js.handlebars
@@ -41,6 +41,9 @@
{{else}}
{{unbound email}} |
+ {{#if expired}}
+ {{i18n user.invited.expired}}
+ {{/if}}
{{#if rescinded}}
{{i18n user.invited.rescinded}}
{{else}}
diff --git a/app/models/invite.rb b/app/models/invite.rb
index d491a9ab06a..53c0f7c527a 100644
--- a/app/models/invite.rb
+++ b/app/models/invite.rb
@@ -50,7 +50,15 @@ class Invite < ActiveRecord::Base
# Return the previously existing invite if already exists. Returns nil if the invite can't be created.
def self.invite_by_email(email, invited_by, topic=nil)
lower_email = Email.downcase(email)
- invite = Invite.with_deleted.where('invited_by_id = ? and email = ?', invited_by.id, lower_email).first
+ invite = Invite.with_deleted
+ .where('invited_by_id = ? and email = ?', invited_by.id, lower_email)
+ .order('created_at DESC')
+ .first
+
+ if invite && invite.expired?
+ invite.destroy
+ invite = nil
+ end
if invite.blank?
invite = Invite.create(invited_by: invited_by, email: lower_email)
diff --git a/app/serializers/invite_serializer.rb b/app/serializers/invite_serializer.rb
index 73989b96e93..87a3cf7f081 100644
--- a/app/serializers/invite_serializer.rb
+++ b/app/serializers/invite_serializer.rb
@@ -1,10 +1,14 @@
class InviteSerializer < ApplicationSerializer
- attributes :email, :created_at, :redeemed_at
+ attributes :email, :created_at, :redeemed_at, :expired
has_one :user, embed: :objects, serializer: InvitedUserSerializer
def include_email?
!object.redeemed?
end
+ def expired
+ object.expired?
+ end
+
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index e1c96f6398d..7d0ead81388 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -356,6 +356,7 @@ en:
pending: "Pending Invites"
topics_entered: "Topics Viewed"
posts_read_count: "Posts Read"
+ expired: "This invite has expired."
rescind: "Remove Invitation"
rescinded: "Invite removed"
time_read: "Read Time"
diff --git a/spec/models/invite_spec.rb b/spec/models/invite_spec.rb
index 8478ad44287..00ce3fc79ec 100644
--- a/spec/models/invite_spec.rb
+++ b/spec/models/invite_spec.rb
@@ -100,6 +100,15 @@ describe Invite do
it 'matches case sensitively for the local part' do
topic.invite_by_email(inviter, 'ICEKING@adventuretime.ooo').should_not == @invite
end
+
+ it 'returns a new invite if the other has expired' do
+ SiteSetting.stubs(:invite_expiry_days).returns(1)
+ @invite.created_at = 2.days.ago
+ @invite.save
+ new_invite = topic.invite_by_email(inviter, 'iceking@adventuretime.ooo')
+ new_invite.should_not == @invite
+ new_invite.should_not be_expired
+ end
end
context 'when adding to another topic' do
|