diff --git a/app/jobs/scheduled/weekly.rb b/app/jobs/scheduled/weekly.rb index 156cfd74e45..f137c9d6408 100644 --- a/app/jobs/scheduled/weekly.rb +++ b/app/jobs/scheduled/weekly.rb @@ -11,6 +11,7 @@ module Jobs Post.calculate_avg_time Topic.calculate_avg_time ScoreCalculator.new.calculate + User.purge_inactive end end end diff --git a/app/models/user.rb b/app/models/user.rb index d977401b4c1..67e82e99b99 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/db/migrate/20140813175357_add_default_to_active.rb b/db/migrate/20140813175357_add_default_to_active.rb new file mode 100644 index 00000000000..5d5000fe32a --- /dev/null +++ b/db/migrate/20140813175357_add_default_to_active.rb @@ -0,0 +1,5 @@ +class AddDefaultToActive < ActiveRecord::Migration + def change + change_column :users, :active, :boolean, default: false, null: false + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c342d3bb262..bcfcea9a6af 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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