2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-18 11:25:15 -04:00
|
|
|
module Jobs
|
2019-10-02 00:01:53 -04:00
|
|
|
class CleanUpInactiveUsers < ::Jobs::Scheduled
|
2019-03-18 11:25:15 -04:00
|
|
|
every 1.day
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
return if SiteSetting.clean_up_inactive_users_after_days <= 0
|
|
|
|
|
|
|
|
User
|
2019-05-13 15:44:03 -04:00
|
|
|
.where(
|
|
|
|
last_posted_at: nil,
|
|
|
|
trust_level: TrustLevel.levels[:newuser],
|
|
|
|
admin: false,
|
|
|
|
moderator: false,
|
|
|
|
)
|
2023-05-23 04:11:23 -04:00
|
|
|
.where("users.created_at < ?", SiteSetting.clean_up_inactive_users_after_days.days.ago)
|
2019-03-18 21:53:18 -04:00
|
|
|
.where(
|
2023-05-22 14:56:24 -04:00
|
|
|
"users.last_seen_at < ? OR users.last_seen_at IS NULL",
|
2019-03-18 21:53:18 -04:00
|
|
|
SiteSetting.clean_up_inactive_users_after_days.days.ago,
|
2019-04-26 08:51:33 -04:00
|
|
|
)
|
2023-05-22 14:56:24 -04:00
|
|
|
.where
|
|
|
|
.missing(:posts, :topics)
|
2019-04-09 08:24:19 -04:00
|
|
|
.limit(1000)
|
2019-04-26 08:51:33 -04:00
|
|
|
.pluck(:id)
|
|
|
|
.each_slice(50) { |slice| destroy(slice) }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def destroy(ids)
|
|
|
|
destroyer = UserDestroyer.new(Discourse.system_user)
|
|
|
|
|
|
|
|
User.transaction do
|
|
|
|
ids.each do |id|
|
|
|
|
begin
|
2019-11-18 00:14:15 -05:00
|
|
|
user = User.find_by(id: id)
|
|
|
|
next unless user
|
2019-04-26 08:51:33 -04:00
|
|
|
destroyer.destroy(
|
|
|
|
user,
|
|
|
|
transaction: false,
|
|
|
|
context: I18n.t("user.destroy_reasons.inactive_user"),
|
|
|
|
)
|
|
|
|
rescue => e
|
|
|
|
Discourse.handle_job_exception(
|
|
|
|
e,
|
|
|
|
message: "Cleaning up inactive users",
|
|
|
|
extra: {
|
|
|
|
user_id: id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
raise e
|
|
|
|
end
|
2019-03-18 11:25:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|