From 2c8e1d3578cd35112160f879052e347ac169dc86 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Thu, 7 Mar 2019 15:19:46 +0530 Subject: [PATCH] FEATURE: remove all expired invitations by default --- .../controllers/user-invited-show.js.es6 | 1 - app/controllers/invites_controller.rb | 2 +- app/models/invite.rb | 5 +++-- config/locales/client.en.yml | 6 +++--- spec/models/invite_spec.rb | 15 ++++++++++----- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/user-invited-show.js.es6 b/app/assets/javascripts/discourse/controllers/user-invited-show.js.es6 index a157abee6ac..1bb292b72b9 100644 --- a/app/assets/javascripts/discourse/controllers/user-invited-show.js.es6 +++ b/app/assets/javascripts/discourse/controllers/user-invited-show.js.es6 @@ -90,7 +90,6 @@ export default Ember.Controller.extend({ Invite.rescindAll() .then(() => { this.set("rescindedAll", true); - this.get("model.invites").clear(); }) .catch(popupAjaxError); } diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb index 884f67ec8f7..30e06c2f65e 100644 --- a/app/controllers/invites_controller.rb +++ b/app/controllers/invites_controller.rb @@ -143,7 +143,7 @@ class InvitesController < ApplicationController def rescind_all_invites guardian.ensure_can_rescind_all_invites!(current_user) - Invite.rescind_all_invites_from(current_user) + Invite.rescind_all_expired_invites_from(current_user) render body: nil end diff --git a/app/models/invite.rb b/app/models/invite.rb index b2012405365..6e022424381 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -226,8 +226,9 @@ class Invite < ActiveRecord::Base end end - def self.rescind_all_invites_from(user) - Invite.where('invites.user_id IS NULL AND invites.email IS NOT NULL AND invited_by_id = ?', user.id).find_each do |invite| + def self.rescind_all_expired_invites_from(user) + Invite.where('invites.user_id IS NULL AND invites.email IS NOT NULL AND invited_by_id = ? AND invites.created_at < ?', + user.id, SiteSetting.invite_expiry_days.days.ago).find_each do |invite| invite.trash!(user) end end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 5ae7ddc8626..3ee598ca117 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1000,9 +1000,9 @@ en: expired: "This invite has expired." rescind: "Remove" rescinded: "Invite removed" - rescind_all: "Remove all Invites" - rescinded_all: "All Invites removed!" - rescind_all_confirm: "Are you sure you want to remove all invites?" + rescind_all: "Remove all Expired Invites" + rescinded_all: "All Expired Invites removed!" + rescind_all_confirm: "Are you sure you want to remove all expired invites?" reinvite: "Resend Invite" reinvite_all: "Resend all Invites" reinvite_all_confirm: "Are you sure you want to resend all invites?" diff --git a/spec/models/invite_spec.rb b/spec/models/invite_spec.rb index 15ebea3f32d..bdf7a02faa4 100644 --- a/spec/models/invite_spec.rb +++ b/spec/models/invite_spec.rb @@ -477,16 +477,21 @@ describe Invite do end - describe '.rescind_all_invites_from' do - it 'removes all invites sent by a user' do + describe '.rescind_all_expired_invites_from' do + it 'removes all expired invites sent by a user' do + SiteSetting.invite_expiry_days = 1 user = Fabricate(:user) invite_1 = Fabricate(:invite, invited_by: user) invite_2 = Fabricate(:invite, invited_by: user) - Invite.rescind_all_invites_from(user) + expired_invite = Fabricate(:invite, invited_by: user) + expired_invite.update!(created_at: 2.days.ago) + Invite.rescind_all_expired_invites_from(user) invite_1.reload invite_2.reload - expect(invite_1.deleted_at).to be_present - expect(invite_2.deleted_at).to be_present + expired_invite.reload + expect(invite_1.deleted_at).to eq(nil) + expect(invite_2.deleted_at).to eq(nil) + expect(expired_invite.deleted_at).to be_present end end end