2020-01-13 13:47:23 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime , timezone , timedelta
import time
import os
import sys
import os . path
import psycopg2
2022-08-01 11:52:30 +02:00
def delete_inactives ( deletion_accepted , query ) :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
id_array = [ ]
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
username_array = [ ]
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
conn = None
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
try :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
conn = psycopg2 . connect ( database = mailing_db , user = mailing_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
cur = conn . cursor ( )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
cur . execute ( query )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
for row in cur :
2020-03-05 12:04:08 +01:00
2022-08-01 11:52:30 +02:00
id_array . append ( row [ 0 ] )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
username_array . append ( row [ 1 ] )
2020-07-05 20:43:04 +02:00
2022-08-01 11:52:30 +02:00
i = 0
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
if len ( id_array ) == 0 :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
if deletion_accepted == True :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
print ( " None inactive users who accepted to be deleted found! " )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
elif deletion_accepted == False :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
print ( " None inactive users to be deleted! " )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
return
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
while i < len ( id_array ) :
if deletion_accepted == True :
print ( " Deleting inactive users who accepted to be deleted... " )
elif deletion_accepted == False :
print ( " Deleting inactive users who do not reply our email... " )
print ( f " \n Deleting user { str ( i ) } of { str ( len ( id_array ) ) } users " )
print ( f " Deleting user with id { str ( id_array [ i ] ) } , username: { username_array [ i ] } " )
os . system ( f " RAILS_ENV=production { rvm_ruby } { mastodon_full_path } /bin/tootctl accounts delete { username_array [ i ] } " )
delete_user ( id_array [ i ] , username_array [ i ] )
i + = 1
cur . close ( )
except ( Exception , psycopg2 . DatabaseError ) as error :
print ( error )
finally :
if conn is not None :
conn . close ( )
2020-01-13 13:47:23 +01:00
def delete_user ( id , username ) :
2022-08-01 11:52:30 +02:00
conn = None
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
try :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
conn = psycopg2 . connect ( database = mailing_db , user = mailing_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
cur = conn . cursor ( )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
cur . execute ( " DELETE FROM " + mailing_db_table + " where account_id=( %s ) " , ( str ( id ) , ) )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
print ( f " Deleting user { str ( id ) } , username { str ( username ) } " )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
conn . commit ( )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
cur . close ( )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
except ( Exception , psycopg2 . DatabaseError ) as error :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
print ( error )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
finally :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
if conn is not None :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
conn . close ( )
2020-01-13 13:47:23 +01:00
def get_parameter ( parameter , file_path ) :
2022-08-01 11:52:30 +02:00
2020-01-13 13:47:23 +01:00
# Check if secrets file exists
if not os . path . isfile ( file_path ) :
2022-08-01 11:52:30 +02:00
2020-01-13 13:47:23 +01:00
if file_path == " secrets/secrets.txt " :
2022-08-01 11:52:30 +02:00
print ( " File %s not found, exiting. Run setup.py. " % file_path )
2020-01-13 13:47:23 +01:00
elif file_path == " config.txt " :
2022-08-01 11:52:30 +02:00
print ( " File %s not found, exiting. Run db-setup.py. " % file_path )
2020-01-13 13:47:23 +01:00
sys . exit ( 0 )
# Find parameter in file
with open ( file_path ) as f :
2022-08-01 11:52:30 +02:00
2020-01-13 13:47:23 +01:00
for line in f :
2022-08-01 11:52:30 +02:00
2020-01-13 13:47:23 +01:00
if line . startswith ( parameter ) :
2022-08-01 11:52:30 +02:00
2020-01-13 13:47:23 +01:00
return line . replace ( parameter + " : " , " " ) . strip ( )
# Cannot find parameter, exit
2022-08-01 11:52:30 +02:00
print ( f " { file_path } Missing parameter { parameter } " )
2020-01-13 13:47:23 +01:00
print ( " Run setup.py " )
sys . exit ( 0 )
2022-08-01 11:52:30 +02:00
def config ( ) :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
secrets_filepath = " secrets/secrets.txt "
mastodon_full_path = get_parameter ( " mastodon_full_path " , secrets_filepath )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
# Load configuration from config file
config_filepath = " config.txt "
mailing_db = get_parameter ( " mailing_db " , config_filepath )
mailing_db_user = get_parameter ( " mailing_db_user " , config_filepath )
mailing_db_table = get_parameter ( " mailing_db_table " , config_filepath )
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
return ( mastodon_full_path , mailing_db , mailing_db_user , mailing_db_table )
2020-01-13 13:47:23 +01:00
###############################################################################
2022-08-01 11:52:30 +02:00
# main
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
if __name__ == ' __main__ ' :
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
mastodon_full_path , mailing_db , mailing_db_user , mailing_db_table = config ( )
global rvm_ruby
rvm_ruby = os . environ [ ' HOME ' ] + " /.rbenv/shims/ruby "
###############################################################################
2022-08-02 07:15:00 +02:00
# select and delete users who replied the warning email saying yes to deletion
2022-08-01 11:52:30 +02:00
deletion_accepted = True
2022-08-03 08:10:28 +02:00
query = " select account_id, username, email, to_be_deleted, feedback, recipient_error, elapsed_days from " + mailing_db_table + " where to_be_deleted and feedback "
2022-08-01 11:52:30 +02:00
delete_inactives ( deletion_accepted , query )
###############################################################################
2022-08-03 08:10:28 +02:00
# select and delete users who don't replied the email after 31 days
2022-08-01 11:52:30 +02:00
deletion_accepted = False
2022-08-03 08:10:28 +02:00
query = " select account_id, username, email, to_be_deleted, feedback, recipient_error, elapsed_days from " + mailing_db_table + " where not feedback and elapsed_days = ' 31 ' "
2020-01-13 13:47:23 +01:00
2022-08-01 11:52:30 +02:00
delete_inactives ( deletion_accepted , query )