FEATURE: Clean up inactive users. (#7172)

This commit is contained in:
Bianca Nenciu 2019-03-18 17:25:15 +02:00 committed by Régis Hanol
parent 7e9afdace3
commit 2347661a74
4 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,29 @@
module Jobs
class CleanUpInactiveUsers < Jobs::Scheduled
every 1.day
def execute(args)
return if SiteSetting.clean_up_inactive_users_after_days <= 0
destroyer = UserDestroyer.new(Discourse.system_user)
User.joins("LEFT JOIN posts ON posts.user_id = users.id")
.where(last_posted_at: nil)
.where("posts.user_id IS NULL")
.where("users.last_seen_at < ?", SiteSetting.clean_up_inactive_users_after_days.days.ago)
.where(trust_level: 0)
.find_each do |user|
begin
destroyer.destroy(user, context: I18n.t("user.destroy_reasons.inactive_user"))
rescue => e
Discourse.handle_job_exception(e,
message: "Cleaning up inactive users",
extra: { user_id: user.id }
)
end
end
end
end
end

View File

@ -1829,6 +1829,8 @@ en:
ignored_users_message_gap_days: "How long to wait before notifying moderators again about a user who has been ignored by many others." ignored_users_message_gap_days: "How long to wait before notifying moderators again about a user who has been ignored by many others."
clean_up_inactive_users_after_days: "Number of days before an inactive user (trust level 0 without any posts) is removed."
user_website_domains_whitelist: "User website will be verified against these domains. Pipe-delimited list." user_website_domains_whitelist: "User website will be verified against these domains. Pipe-delimited list."
allow_profile_backgrounds: "Allow users to upload profile backgrounds." allow_profile_backgrounds: "Allow users to upload profile backgrounds."
@ -2201,6 +2203,7 @@ en:
unused_staged_user: "Unused staged user" unused_staged_user: "Unused staged user"
fixed_primary_email: "Fixed primary email for staged user" fixed_primary_email: "Fixed primary email for staged user"
same_ip_address: "Same IP address (%{ip_address}) as other users" same_ip_address: "Same IP address (%{ip_address}) as other users"
inactive_user: "Inactive user"
flags_reminder: flags_reminder:
flags_were_submitted: flags_were_submitted:

View File

@ -541,6 +541,8 @@ users:
default: 365 default: 365
client: true client: true
min: 1 min: 1
clean_up_inactive_users_after_days:
default: 730
groups: groups:
enable_group_directory: enable_group_directory:

View File

@ -0,0 +1,30 @@
require 'rails_helper'
RSpec.describe Jobs::CleanUpInactiveUsers do
context 'when user is inactive' do
let(:user) { Fabricate(:user) }
let(:active_user) { Fabricate(:active_user) }
it 'should clean up the user' do
user.update!(last_seen_at: 3.years.ago, trust_level: 0)
active_user
expect { described_class.new.execute({}) }.to change { User.count }.by(-1)
expect(User.find_by(id: user.id)).to eq(nil)
end
end
context 'when user is not inactive' do
let!(:active_user_1) { Fabricate(:post, user: Fabricate(:user, trust_level: 0)).user }
let!(:active_user_2) { Fabricate(:user, trust_level: 0, last_seen_at: 2.days.ago) }
let!(:active_user_3) { Fabricate(:user, trust_level: 1) }
it 'should not clean up active users' do
expect { described_class.new.execute({}) }.to_not change { User.count }
expect(User.find_by(id: active_user_1.id)).to_not eq(nil)
expect(User.find_by(id: active_user_2.id)).to_not eq(nil)
expect(User.find_by(id: active_user_3.id)).to_not eq(nil)
end
end
end