Skip to content

Instantly share code, notes, and snippets.

@lgrn
Created February 24, 2024 13:58
Show Gist options
  • Save lgrn/36dcf095494c9135676eee95d93ebc00 to your computer and use it in GitHub Desktop.
Save lgrn/36dcf095494c9135676eee95d93ebc00 to your computer and use it in GitHub Desktop.
disable any mastodon users not active for the past 7890000 seconds (91.3 days)
#!/bin/bash
DB_HOST=1.2.3.4
DB_PORT=5432
DB_NAME=mastodon_production
DB_USER=mastodon
PGPASSWORD=yourpassword psql -h ${DB_HOST} -p ${DB_PORT} -d ${DB_NAME} -U ${DB_USER} -w -1 -t -A -F" " -c "SELECT accounts.username, TRUNC(EXTRACT(EPOCH FROM (NOW() - current_sign_in_at))) AS diff FROM users JOIN accounts ON users.account_id = accounts.id WHERE (approved = TRUE AND disabled = FALSE)" | while read -r user time;
do
if [[ -v user && time -ge 7890000 ]]; then
RAILS_ENV='production' /home/mastodon/live/bin/tootctl accounts modify $user --disable
echo "$user SUSPENDED for exceeding 7 890 000 seconds"
else
echo "$user is OK!"
fi
done
@SISheogorath
Copy link

I don't think you have to bother with the time comparison in the shell script, let the DB do the heavy lifting:

SELECT accounts.username FROM users JOIN accounts ON users.account_id = accounts.id WHERE (approved = TRUE AND disabled = FALSE AND current_sign_in_at < NOW() - INTERVAL '90 days');

@lgrn
Copy link
Author

lgrn commented Feb 25, 2024

@SISheogorath good idea, almost certainly scales a lot better. In our case the size of the instance is minimal though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment