Purge inactive accounts that are older than 7 days
This commit is contained in:
parent
9a1580244a
commit
3c6673aceb
|
@ -11,6 +11,7 @@ module Jobs
|
|||
Post.calculate_avg_time
|
||||
Topic.calculate_avg_time
|
||||
ScoreCalculator.new.calculate
|
||||
User.purge_inactive
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -712,6 +712,23 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
# Delete inactive accounts that are over a week old
|
||||
def self.purge_inactive
|
||||
|
||||
# You might be wondering why this query matches on post_count = 0. The reason
|
||||
# is a long time ago we had a bug where users could post before being activated
|
||||
# and some sites still have those records which can't be purged.
|
||||
to_destroy = User.where(active: false)
|
||||
.joins('INNER JOIN user_stats AS us ON us.user_id = users.id')
|
||||
.where("created_at < ?", 1.week.ago)
|
||||
.where('us.post_count = 0')
|
||||
|
||||
destroyer = UserDestroyer.new(Discourse.system_user)
|
||||
to_destroy.each do |u|
|
||||
destroyer.destroy(u)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def previous_visit_at_update_required?(timestamp)
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
class AddDefaultToActive < ActiveRecord::Migration
|
||||
def change
|
||||
change_column :users, :active, :boolean, default: false, null: false
|
||||
end
|
||||
end
|
|
@ -1136,4 +1136,18 @@ describe User do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#purge_inactive" do
|
||||
let!(:user) { Fabricate(:user) }
|
||||
let!(:inactive) { Fabricate(:user, active: false) }
|
||||
let!(:inactive_old) { Fabricate(:user, active: false, created_at: 1.month.ago) }
|
||||
|
||||
it 'should only remove old, inactive users' do
|
||||
User.purge_inactive
|
||||
all_users = User.all
|
||||
all_users.include?(user).should be_true
|
||||
all_users.include?(inactive).should be_true
|
||||
all_users.include?(inactive_old).should be_false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue