This is my first custom Ruby script ever. In almost 50 years of coding with over 20 languages and dialects, I’ve never needed (or wanted ) to write in Ruby, but OK, you got me … please be gentle.
My goal is to identify user accounts that have not been authenticated by email links, and suspend the accounts so that I can check them before deletion. During initial setup I had settings in various stages of development. The 2FA emails didn’t go out for some registrations, and there were a lot of bogus registrations that I’d now like to filter out.
Version 0.0.1
rails c
# First ensure user TLs conform to latest definitions
User.all.find_each do |user|
Promotion.recalculate(user)
end
Group.ensure_consistency!
# Prep for loop
logger = StaffActionLogger.new(User.where("id = 'admin'"))
# temporary date, users will be removed before this date
suspend_till = DateTime.new(2028, 12, 31)
suspend_at = DateTime.now
reason = 'Inactive'
# Identify potentially dead accounts
User.where("views = 0 OR approved = FALSE OR last_seen_at IS NULL")
.where("id > 0")
.find_each do |user|
user.suspended_till = suspend_till
user.suspended_at = suspend_at
user.change_trust_level!(TrustLevel[0])
# save the user, log the action, and ... do whatever is done for this trigger
user.save!
logger.log_user_suspend(user, reason)
DiscourseEvent.trigger(:user_suspended, user: user)
# avoid abusing the mail server
sleep(10)
end
By triggering the event, I’d like emails to go out to the users - there will be a bunch of bounces, and there might be a few people who come back to authenticate. After a week I’ll select all accounts that are still suspended and delete them.
Questions:
- In general, will this do what is intended?
- Is there a better approach?
- Is the logging redundant with the event trigger?
- Can we do this with JavaScript?
- Should I post this to another category?
- Any other words of advice?
Thank you!!