diff --git a/cleanup-mastodon-users.sh b/cleanup-mastodon-users.sh index 18c1036..665151c 100644 --- a/cleanup-mastodon-users.sh +++ b/cleanup-mastodon-users.sh @@ -4,8 +4,9 @@ # UPPERLIMIT="'7 months'" or "'28 weeks'" # this defines the range of inactivity where users get notified # before the account will be deleted when UPPERLIMIT is surpassed. -LOWERLIMIT="6 months" -UPPERLIMIT="7 months" +LOWERLIMIT="6 months" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months +UPPERLIMIT="7 months" # inactive accounts older than 7 months will be deactivated +DELDELIMIT="1 year" # all accounts not used within a year will get deleted TOOTCTL="~/live/bin/tootctl" @@ -135,16 +136,18 @@ case ${mode} in # the intention is that you can better judge how often you need to invoke the cron option # or by increasing the limit_delete variable # the backlog queue shouldn't pile up but also not run empty to fast to reduce the load - num_deactivated_total=$(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at > now()-'${UPPERLIMIT}'::interval and username not in ($sqlprotectedusers) limit ${limit_delete}" | tr -d " " ) + num_deactivated_overgrace=$(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($sqlprotectedusers)" | tr -d " " ) + num_deactivated_total=$(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and username not in ($sqlprotectedusers)" | tr -d " " ) # when there is less then 1 user to delete (=0) then exit - if [ ${num_deactivated_total} -lt 1 ]; then + if [ ${num_deactivated_overgrace} -lt 1 ]; then exit 0 fi echo "===================================" - echo "Number deactivated accounts: ${num_deactivated_total}" + echo "Total deactivated accounts: ${num_deactivated_total}" + echo "Number deactivated accounts over grace: ${num_deactivated_overgrace}" echo "Deleting this many accounts: ${limit_delete}" echo "===================================" - for u in $(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at > now()-'${UPPERLIMIT}'::interval and username not in ($sqlprotectedusers) order by current_sign_in_at limit ${limit_delete}" | tr -d " " ); do + for u in $(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($sqlprotectedusers) order by current_sign_in_at limit ${limit_delete}" | tr -d " " ); do #echo ${u} username=$(echo "${u}" | awk -F ";" '{print $1}') dispname=$(echo "${u}" | awk -F ";" '{print $2}')