2019-03-25 18:54:49 +00:00
|
|
|
package Travelynx::Command::maintenance;
|
|
|
|
use Mojo::Base 'Mojolicious::Command';
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
|
|
|
|
has description => 'Prune unverified users etc';
|
|
|
|
|
|
|
|
has usage => sub { shift->extract_usage };
|
|
|
|
|
|
|
|
sub run {
|
|
|
|
my ( $self, $filename ) = @_;
|
|
|
|
|
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
2019-04-22 11:42:41 +00:00
|
|
|
my $verification_deadline = $now->clone->subtract( hours => 48 );
|
|
|
|
my $deletion_deadline = $now->clone->subtract( hours => 72 );
|
2019-04-30 10:47:32 +00:00
|
|
|
my $old_deadline = $now->clone->subtract( years => 1 );
|
2019-04-22 11:42:41 +00:00
|
|
|
|
|
|
|
my $db = $self->app->pg->db;
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
|
|
my $unverified = $db->select(
|
|
|
|
'users',
|
|
|
|
'id, email, extract(epoch from registered_at) as registered_ts',
|
|
|
|
{
|
|
|
|
status => 0,
|
|
|
|
registered_at => { '<', $verification_deadline }
|
2019-03-25 18:54:49 +00:00
|
|
|
}
|
2019-04-22 11:42:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
for my $user ( $unverified->hashes->each ) {
|
|
|
|
my $mail = $user->{email};
|
|
|
|
my $reg_date = DateTime->from_epoch(
|
|
|
|
epoch => $user->{registered_ts},
|
|
|
|
time_zone => 'Europe/Berlin'
|
|
|
|
);
|
|
|
|
|
|
|
|
my $pending
|
2019-04-30 10:08:51 +00:00
|
|
|
= $db->select( 'mail_blacklist', ['num_tries'], { email => $mail } );
|
2019-04-22 11:42:41 +00:00
|
|
|
my $pending_h = $pending->hash;
|
|
|
|
|
|
|
|
if ($pending_h) {
|
|
|
|
my $num_tries = $pending_h->{num_tries} + 1;
|
|
|
|
$db->update(
|
2019-04-30 10:08:51 +00:00
|
|
|
'mail_blacklist',
|
2019-04-22 11:42:41 +00:00
|
|
|
{
|
|
|
|
num_tries => $num_tries,
|
|
|
|
last_try => $reg_date
|
|
|
|
},
|
|
|
|
{ email => $mail }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$db->insert(
|
2019-04-30 10:08:51 +00:00
|
|
|
'mail_blacklist',
|
2019-04-22 11:42:41 +00:00
|
|
|
{
|
|
|
|
email => $mail,
|
|
|
|
num_tries => 1,
|
|
|
|
last_try => $reg_date
|
|
|
|
}
|
|
|
|
);
|
2019-03-31 15:58:05 +00:00
|
|
|
}
|
2019-04-22 11:42:41 +00:00
|
|
|
$db->delete( 'users', { id => $user->{id} } );
|
|
|
|
printf( "Pruned unverified user %d\n", $user->{id} );
|
2019-03-31 15:58:05 +00:00
|
|
|
}
|
2019-04-22 11:42:41 +00:00
|
|
|
|
2019-04-29 18:12:59 +00:00
|
|
|
my $res = $db->delete( 'pending_passwords',
|
|
|
|
{ requested_at => { '<', $verification_deadline } } );
|
|
|
|
|
|
|
|
if ( my $rows = $res->rows ) {
|
|
|
|
printf( "Pruned %d pending password reset(s)\n", $rows );
|
|
|
|
}
|
|
|
|
|
2019-04-30 10:08:51 +00:00
|
|
|
$res = $db->delete( 'pending_mails',
|
|
|
|
{ requested_at => { '<', $verification_deadline } } );
|
|
|
|
|
|
|
|
if ( my $rows = $res->rows ) {
|
|
|
|
printf( "Pruned %d pending mail change(s)\n", $rows );
|
|
|
|
}
|
|
|
|
|
2019-04-22 11:42:41 +00:00
|
|
|
my $to_delete = $db->select( 'users', ['id'],
|
|
|
|
{ deletion_requested => { '<', $deletion_deadline } } );
|
|
|
|
my @uids_to_delete = $to_delete->arrays->map( sub { shift->[0] } )->each;
|
|
|
|
|
2019-04-30 10:47:32 +00:00
|
|
|
$to_delete
|
|
|
|
= $db->select( 'users', ['id'], { last_seen => { '<', $old_deadline } } );
|
|
|
|
|
|
|
|
push( @uids_to_delete,
|
|
|
|
$to_delete->arrays->map( sub { shift->[0] } )->each );
|
|
|
|
|
2019-04-22 11:42:41 +00:00
|
|
|
if ( @uids_to_delete > 10 ) {
|
|
|
|
printf STDERR (
|
|
|
|
"About to delete %d accounts, which is quite a lot.\n",
|
2019-03-31 15:58:05 +00:00
|
|
|
scalar @uids_to_delete
|
|
|
|
);
|
2019-04-22 11:42:41 +00:00
|
|
|
say STDERR 'Aborting maintenance. Please investigate.';
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $uid (@uids_to_delete) {
|
|
|
|
say "Deleting uid ${uid}...";
|
2019-04-24 06:45:56 +00:00
|
|
|
my $tokens_res = $db->delete( 'tokens', { user_id => $uid } );
|
|
|
|
my $stats_res = $db->delete( 'journey_stats', { user_id => $uid } );
|
|
|
|
my $journeys_res = $db->delete( 'journeys', { user_id => $uid } );
|
|
|
|
my $transit_res = $db->delete( 'in_transit', { user_id => $uid } );
|
2019-04-29 18:32:04 +00:00
|
|
|
my $password_res
|
|
|
|
= $db->delete( 'pending_passwords', { user_id => $uid } );
|
|
|
|
my $user_res = $db->delete( 'users', { id => $uid } );
|
2019-04-24 06:45:56 +00:00
|
|
|
|
|
|
|
printf( " %d tokens, %d monthly stats, %d journeys\n",
|
|
|
|
$tokens_res->rows, $stats_res->rows, $journeys_res->rows );
|
2019-04-22 11:42:41 +00:00
|
|
|
|
|
|
|
if ( $user_res->rows != 1 ) {
|
|
|
|
printf STDERR (
|
|
|
|
"Deleted %d rows from users, expected 1. Rollback and abort.\n",
|
|
|
|
$user_res->rows
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-03-31 15:58:05 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 11:42:41 +00:00
|
|
|
$tx->commit;
|
2019-03-25 18:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
Usage: index.pl maintenance
|
|
|
|
|
|
|
|
Prunes unverified users.
|