aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Script/Inactive.pm
blob: 8dd524ce1d55f922347e481f74a7351a59fc73fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package FixMyStreet::Script::Inactive;

use v5.14;
use warnings;

use Moo;
use CronFns;
use FixMyStreet;
use FixMyStreet::Cobrand;
use FixMyStreet::DB;
use FixMyStreet::Email;

has anonymize => ( is => 'ro' );
has close => ( is => 'ro' );
has delete => ( is => 'ro' );
has email => ( is => 'ro' );
has verbose => ( is => 'ro' );
has dry_run => ( is => 'ro' );

has cobrand => (
    is => 'ro',
    coerce => sub { FixMyStreet::Cobrand->get_class_for_moniker($_[0])->new },
);

sub BUILDARGS {
    my ($cls, %args) = @_;
    $args{dry_run} = delete $args{'dry-run'};
    return \%args;
}

has base_cobrand => (
    is => 'lazy',
    default => sub {
        my $base_url = FixMyStreet->config('BASE_URL');
        my $site = CronFns::site($base_url);
        my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($site)->new;
        $cobrand->set_lang_and_domain(undef, 1);
        $cobrand;
    },
);

has anonymous_user => (
    is => 'lazy',
    default => sub {
        FixMyStreet::DB->resultset("User")->find_or_create({
            email => 'removed-automatically@' . FixMyStreet->config('EMAIL_DOMAIN'),
        });
    }
);

sub users {
    my $self = shift;

    say "DRY RUN" if $self->dry_run;
    $self->anonymize_users;
    $self->email_inactive_users if $self->email;
}

sub reports {
    my $self = shift;

    say "DRY RUN" if $self->dry_run;
    $self->anonymize_reports if $self->anonymize;
    $self->delete_reports if $self->delete;
    $self->close_updates if $self->close;
}

sub close_updates {
    my $self = shift;

    my $problems = FixMyStreet::DB->resultset("Problem")->search({
        lastupdate => { '<', interval($self->close) },
        state => [ FixMyStreet::DB::Result::Problem->closed_states(), FixMyStreet::DB::Result::Problem->fixed_states() ],
        extra => [ undef, { -not_like => '%closed_updates%' } ],
    });
    $problems = $problems->search({ cobrand => $self->cobrand->moniker }) if $self->cobrand;

    while (my $problem = $problems->next) {
        say "Closing updates on problem #" . $problem->id if $self->verbose;
        next if $self->dry_run;
        $problem->set_extra_metadata( closed_updates => 1 );
        $problem->update;
    }
}

sub _relevant_reports {
    my ($self, $time) = @_;
    my $problems = FixMyStreet::DB->resultset("Problem")->search({
        lastupdate => { '<', interval($time) },
        state => [
            FixMyStreet::DB::Result::Problem->closed_states(),
            FixMyStreet::DB::Result::Problem->fixed_states(),
            FixMyStreet::DB::Result::Problem->hidden_states(),
        ],
    });
    if ($self->cobrand) {
        $problems = $problems->search({ cobrand => $self->cobrand->moniker });
        $problems = $self->cobrand->call_hook(inactive_reports_filter => $time, $problems) || $problems;
    }
    return $problems;
}

sub anonymize_reports {
    my $self = shift;

    # Need to look though them all each time, in case any new updates/alerts
    my $problems = $self->_relevant_reports($self->anonymize);

    while (my $problem = $problems->next) {
        say "Anonymizing problem #" . $problem->id if $self->verbose;
        next if $self->dry_run;

        # Remove personal data from the report
        $problem->update({
            user => $self->anonymous_user,
            name => '',
            anonymous => 1,
            send_questionnaire => 0,
        }) if $problem->user != $self->anonymous_user;

        # Remove personal data from the report's updates
        $problem->comments->search({
            user_id => { '!=' => $self->anonymous_user->id },
        })->update({
            user_id => $self->anonymous_user->id,
            name => '',
            anonymous => 1,
        });

        # Remove alerts - could just delete, but of interest how many there were, perhaps?
        $problem->alerts->search({
            user_id => { '!=' => $self->anonymous_user->id },
        })->update({
            user_id => $self->anonymous_user->id,
            whendisabled => \'current_timestamp',
        });
    }
}

sub delete_reports {
    my $self = shift;

    my $problems = $self->_relevant_reports($self->delete);

    while (my $problem = $problems->next) {
        say "Deleting associated data of problem #" . $problem->id if $self->verbose;
        next if $self->dry_run;

        $problem->comments->delete;
        $problem->questionnaires->delete;
        $problem->alerts->delete;
    }
    say "Deleting all matching problems" if $self->verbose;
    return if $self->dry_run;
    $problems->delete;
}

sub anonymize_users {
    my $self = shift;

    my $users = FixMyStreet::DB->resultset("User")->search({
        last_active => { '<', interval($self->anonymize) },
        email => { -not_like => 'removed-%@' . FixMyStreet->config('EMAIL_DOMAIN') },
    });

    while (my $user = $users->next) {
        say "Anonymizing user #" . $user->id if $self->verbose;
        next if $self->dry_run;
        $user->anonymize_account;
    }
}

sub email_inactive_users {
    my $self = shift;

    my $users = FixMyStreet::DB->resultset("User")->search({
       last_active => [ -and => { '<', interval($self->email) },
           { '>=', interval($self->anonymize) } ],
    });
    while (my $user = $users->next) {
        next if $user->get_extra_metadata('inactive_email_sent');

        say "Emailing user #" . $user->id if $self->verbose;
        next if $self->dry_run;
        FixMyStreet::Email::send_cron(
            $user->result_source->schema,
            'inactive-account.txt',
            {
                email_from => $self->email,
                anonymize_from => $self->anonymize,
                user => $user,
                url => $self->base_cobrand->base_url_with_lang . '/my',
            },
            { To => [ [ $user->email, $user->name ] ] },
            undef, 0, $self->base_cobrand,
        );

        $user->set_extra_metadata('inactive_email_sent', 1);
        $user->update;
    }
}

sub interval {
    my $interval = shift;
    my $s = "current_timestamp - '$interval months'::interval";
    return \$s;
}

1;